1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use anyhow::Context;
use camino::{
    Utf8Path,
    Utf8PathBuf,
};
use serde::{
    Deserialize,
    Serialize,
};
use serenity::{
    model::prelude::GuildId,
    utils::validate_token,
};
use std::{
    borrow::Cow,
    collections::HashMap,
};

fn default_prefix() -> String {
    "p!".to_string()
}

/// The bot config
#[derive(Deserialize, Debug)]
pub struct Config {
    /// The discord token
    pub token: String,

    /// The application id
    pub application_id: u64,

    /// Prefix for the bot
    #[serde(default = "default_prefix")]
    pub prefix: String,

    /// Status config
    pub status: Option<StatusConfig>,

    /// Data dir
    pub data_dir: Utf8PathBuf,

    /// The test guild
    pub test_guild: Option<GuildId>,

    /// FML config
    pub fml: FmlConfig,

    /// DeviantArt config
    pub deviantart: DeviantArtConfig,

    /// SauceNao config
    pub sauce_nao: SauceNaoConfig,

    /// Open AI config
    #[serde(rename = "open-ai")]
    pub open_ai: OpenAiConfig,

    /// The log config
    #[serde(default)]
    pub log: LogConfig,

    /// Unknown extra data
    #[serde(flatten)]
    pub extra: HashMap<String, toml::Value>,
}

/// FML config
#[derive(Deserialize, Debug)]
pub struct FmlConfig {
    /// FML API key
    pub key: String,
}

/// Deviant Config
#[derive(Deserialize, Debug)]
pub struct DeviantArtConfig {
    /// Username
    pub username: String,

    /// Password
    pub password: String,
}

/// SauceNao Config
#[derive(Deserialize, Debug)]
pub struct SauceNaoConfig {
    /// The api key
    pub api_key: String,

    /// Unknown extra data
    #[serde(flatten)]
    pub extra: HashMap<String, toml::Value>,
}

/// Open AI Config
#[derive(Deserialize, Debug)]
pub struct OpenAiConfig {
    /// The api key
    #[serde(rename = "api-key")]
    pub api_key: String,

    /// Unknown extra data
    #[serde(flatten)]
    pub extra: HashMap<String, toml::Value>,
}

/// Log Config
#[derive(Deserialize, Debug)]
pub struct LogConfig {
    /// The logging directives.
    #[serde(default = "LogConfig::default_directives")]
    pub directives: Vec<String>,

    /// Whether to use opentelemetry
    #[serde(default, rename = "opentelemetry")]
    pub opentelemetry: bool,

    /// The OTLP endpoint
    pub endpoint: Option<String>,

    /// Headers
    #[serde(default)]
    pub headers: HashMap<String, String>,
}

impl LogConfig {
    /// If logging directives not given, choose some defaults.
    fn default_directives() -> Vec<String> {
        // Only enable pikadick since serenity likes puking in the logs during connection failures
        // serenity's framework section seems ok as well
        vec![
            "pikadick=info".to_string(),
            "serenity::framework::standard=info".to_string(),
        ]
    }
}

impl Default for LogConfig {
    fn default() -> Self {
        Self {
            directives: Self::default_directives(),

            opentelemetry: false,
            endpoint: None,
            headers: HashMap::new(),
        }
    }
}

impl Config {
    /// Shortcut for getting the status name
    pub fn status_name(&self) -> Option<&str> {
        self.status.as_ref().map(|s| s.name.as_str())
    }

    /// Shortcut for getting the status url
    pub fn status_url(&self) -> Option<&str> {
        self.status.as_ref().and_then(|s| s.url.as_deref())
    }

    /// Shortcut for getting the status type
    pub fn status_type(&self) -> Option<ActivityKind> {
        self.status.as_ref().and_then(|s| s.kind)
    }

    /// The log file dir
    pub fn log_file_dir(&self) -> Utf8PathBuf {
        self.data_dir.join("logs")
    }

    /// The cache dir
    pub fn cache_dir(&self) -> Utf8PathBuf {
        self.data_dir.join("cache")
    }

    /// Load a config from a path
    pub fn load_from_path<P>(path: P) -> anyhow::Result<Self>
    where
        P: AsRef<Utf8Path>,
    {
        let path = path.as_ref();
        std::fs::read_to_string(path)
            .with_context(|| format!("failed to read config from '{}'", path))
            .and_then(|b| Self::load_from_str(&b))
    }

    /// Load a config from a str
    pub fn load_from_str(s: &str) -> anyhow::Result<Self> {
        toml::from_str(s).context("failed to parse config")
    }

    /// Validate a config
    pub fn validate(&mut self) -> Vec<ValidationMessage> {
        let mut errors = Vec::with_capacity(3);

        if let Err(_e) = validate_token(&self.token) {
            errors.push(ValidationMessage {
                severity: Severity::Error,
                error: ValidationError::InvalidToken,
            });
        }

        if let Some(config) = &self.status {
            if let (Some(ActivityKind::Streaming), None) = (config.kind, &config.url) {
                errors.push(ValidationMessage {
                    severity: Severity::Error,
                    error: ValidationError::MissingStreamUrl,
                });
            }

            if let (None, _) = (config.kind, &config.url) {
                errors.push(ValidationMessage {
                    severity: Severity::Warn,
                    error: ValidationError::MissingStatusType,
                });
            }
        }

        errors
    }
}

#[derive(Deserialize, Debug)]
pub struct StatusConfig {
    #[serde(rename = "type")]
    #[serde(default)]
    kind: Option<ActivityKind>,
    name: String,
    url: Option<String>,

    #[serde(flatten)]
    pub extra: HashMap<String, toml::Value>,
}

#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq, Deserialize, Serialize, Default)]
pub enum ActivityKind {
    Listening,
    #[default]
    Playing,
    Streaming,
}

#[derive(Debug)]
pub struct ValidationMessage {
    severity: Severity,
    error: ValidationError,
}

impl ValidationMessage {
    pub fn severity(&self) -> Severity {
        self.severity
    }

    pub fn error(&self) -> &ValidationError {
        &self.error
    }
}

/// Validation Errors
#[derive(Debug, thiserror::Error)]
pub enum ValidationError {
    #[error("invalid token")]
    InvalidToken,
    #[error("missing status type")]
    MissingStatusType,
    #[error("missing stream url type")]
    MissingStreamUrl,

    #[error("{0}")]
    Generic(Cow<'static, str>),
}

#[derive(Copy, Clone, Debug)]
pub enum Severity {
    Warn,
    Error,
}