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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! phetch will load `~/.config/phetch/phetch.conf` or a file you
//! specify with the `--config` command line option when it starts.
//!
//! An example default config is provided but unused by this module.

use {
    crate::{
        encoding::Encoding,
        phetchdir,
        theme::{to_color, Theme},
        ui,
    },
    std::{
        collections::HashMap,
        fs::OpenOptions,
        io::{self, Read, Result},
        sync::{Arc, RwLock},
    },
};

/// Global, shared config.
pub type SharedConfig = Arc<RwLock<Config>>;

/// phetch will look for this file on load.
const CONFIG_FILE: &str = "phetch.conf";

/// Default start page.
const DEFAULT_START: &str = "gopher://phetch/1/home";

/// Default media player.
const DEFAULT_MEDIA_PLAYER: &str = "mpv";

/// Example of what a default phetch.conf would be.
pub const DEFAULT_CONFIG: &str = "## default config file for the phetch gopher client
## gopher://phkt.io/1/phetch

# Page to load when launched with no URL argument.
start gopher://phetch/1/home

# Always use TLS mode. (--tls)
tls no

# Connect using local Tor proxy. (--tor)
tor no

# Always start in wide mode. (--wide)
wide no

# Program to use to open media files.
media mpv

# Whether to auto play media
autoplay no

# Use emoji indicators for TLS & Tor. (--emoji)
emoji no

# Encoding. Only CP437 and UTF8 are supported.
encoding utf8

# Wrap text at N columns. 0 = off (--wrap)
wrap 0

# How many lines to page up/down by? 0 = full screen
scroll 0

# Path to theme file, if any
# theme ~/.config/phetch/pink.theme

# Inline Theme
ui.cursor white bold
ui.number magenta
ui.menu yellow
ui.text white
item.text cyan
item.menu blue
item.error red
item.search white
item.telnet grey
item.external green
item.download white underline
item.media green underline
item.unsupported whitebg red
";

/// Not all the config options are available in the phetch.conf. We
/// also use this struct to keep track of our session's overall state,
/// such as the UI mode (Print, Run, Raw, etc).
#[derive(Debug)]
pub struct Config {
    /// Gopher URL to open on bare launch
    pub start: String,
    /// Whether to use TLS or not
    pub tls: bool,
    /// Using Tor proxy?
    pub tor: bool,
    /// Wide mode
    pub wide: bool,
    /// Render connection status as emoji
    pub emoji: bool,
    /// Media player to use.
    pub media: Option<String>,
    /// Whether to automatically play media
    pub autoplay: bool,
    /// Default encoding
    pub encoding: Encoding,
    /// UI mode. Can't be set in conf file.
    pub mode: ui::Mode,
    /// Column to wrap lines. 0 = off
    pub wrap: usize,
    /// Scroll by how many lines? 0 = full screen
    pub scroll: usize,
    /// Color Scheme
    pub theme: Theme,
}

impl Default for Config {
    fn default() -> Self {
        Config {
            start: String::from(DEFAULT_START),
            tls: false,
            tor: false,
            wide: false,
            emoji: false,
            media: Some(DEFAULT_MEDIA_PLAYER.into()),
            autoplay: false,
            encoding: Encoding::default(),
            mode: ui::Mode::default(),
            wrap: 0,
            scroll: 0,
            theme: Theme::default(),
        }
    }
}

/// Returns the config phetch uses when launched with no flags or
/// config file modification.
pub fn default() -> Config {
    Default::default()
}

/// Attempt to load ~/.config/phetch/phetch.conf from disk.
pub fn load() -> Result<Config> {
    let mut reader = phetchdir::load(CONFIG_FILE)?;
    let mut file = String::new();
    reader.read_to_string(&mut file)?;
    parse(&file)
}

/// Attempt to load a config from disk.
pub fn load_file(path: &str) -> Result<Config> {
    let mut reader = OpenOptions::new().read(true).open(path)?;
    let mut file = String::new();
    reader.read_to_string(&mut file)?;
    parse(&file)
}

/// Does the config file exist?
pub fn exists() -> bool {
    phetchdir::exists(CONFIG_FILE)
}

/// Parses a phetch config file into a Config struct.
fn parse(text: &str) -> Result<Config> {
    let mut cfg = Config::default();
    let mut keys: HashMap<&str, bool> = HashMap::new();

    for (mut linenum, line) in text.split_terminator('\n').enumerate() {
        linenum += 1;
        // skip empty lines
        if line.trim().is_empty() {
            continue;
        }

        // skip comments
        if let Some('#') = line.chars().next() {
            continue;
        }

        // line format: "KEY VALUE"
        let parts: Vec<&str> = line.splitn(2, ' ').collect();
        if parts.len() != 2 {
            return Err(error!(
                r#"Expected "key value" format on line {}: {:?}"#,
                linenum, line
            ));
        }
        let (key, val) = (parts[0], parts[1]);
        if keys.contains_key(key) {
            return Err(error!("Duplicate key on line {}: {}", linenum, key));
        }
        match key {
            "start" => cfg.start = val.into(),
            "emoji" => cfg.emoji = to_bool(val)?,
            "tls" => cfg.tls = to_bool(val)?,
            "tor" => cfg.tor = to_bool(val)?,
            "wide" => cfg.wide = to_bool(val)?,
            "wrap" => {
                if let Ok(num) = val.parse() {
                    cfg.wrap = num;
                } else {
                    return Err(error!(
                        "`wrap` expects a number value on line {}: {}",
                        linenum, val
                    ));
                }
            }
            "scroll" => {
                if let Ok(num) = val.parse() {
                    cfg.scroll = num;
                } else {
                    return Err(error!(
                        "`scroll` expects a number value on line {}: {}",
                        linenum, val
                    ));
                }
            }
            "media" => {
                cfg.media = match val.to_lowercase().as_ref() {
                    "false" | "none" => None,
                    _ => Some(val.into()),
                }
            }
            "autoplay" => cfg.autoplay = to_bool(val)?,
            "encoding" => {
                cfg.encoding = Encoding::from_str(val)
                    .map_err(|e| error!("{} on line {}: {:?}", e, linenum, line))?;
            }

            "theme" => {
                let homevar = std::env::var("HOME");
                if homevar.is_err() && val.contains('~') {
                    return Err(error!("$HOME not set, can't decode `~`"));
                }
                cfg.theme = match load_file(&val.replace('~', &homevar.unwrap())) {
                    Ok(cfg) => cfg.theme,
                    Err(e) => {
                        if matches!(e.kind(), io::ErrorKind::NotFound) {
                            return Err(error!(
                                "error loading theme: File not found on line {}: {}",
                                linenum, val
                            ));
                        } else {
                            return Err(error!("error loading theme: {:?}", e));
                        }
                    }
                };
            }

            // color scheme
            "ui.cursor" => cfg.theme.ui_cursor = to_color(val),
            "ui.number" => cfg.theme.ui_number = to_color(val),
            "ui.menu" => cfg.theme.ui_menu = to_color(val),
            "ui.text" => cfg.theme.ui_text = to_color(val),

            "item.text" => cfg.theme.item_text = to_color(val),
            "item.menu" => cfg.theme.item_menu = to_color(val),
            "item.error" => cfg.theme.item_error = to_color(val),
            "item.search" => cfg.theme.item_search = to_color(val),
            "item.telnet" => cfg.theme.item_telnet = to_color(val),
            "item.external" => cfg.theme.item_external = to_color(val),
            "item.download" => cfg.theme.item_download = to_color(val),
            "item.media" => cfg.theme.item_media = to_color(val),
            "item.unsupported" => cfg.theme.item_unsupported = to_color(val),

            _ => return Err(error!("Unknown key on line {}: {}", linenum, key)),
        }
        keys.insert(key, true);
    }

    Ok(cfg)
}

/// Converts a config file's boolean value like "yes" or "false" to a
/// real bool.
fn to_bool(val: &str) -> Result<bool> {
    match val.to_lowercase().as_ref() {
        "yes" | "true" | "y" => Ok(true),
        "no" | "false" | "n" => Ok(false),
        _ => Err(error!("Not a boolean: {}", val)),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_default() {
        let config = parse(DEFAULT_CONFIG).expect("Couldn't parse config");
        assert_eq!(config.tls, false);
        assert_eq!(config.tor, false);
        assert_eq!(config.wide, false);
        assert_eq!(config.emoji, false);
        assert_eq!(config.start, "gopher://phetch/1/home");
        assert_eq!(config.media, Some("mpv".to_string()));
    }

    #[test]
    fn test_bad_key() {
        let res = parse("random-key yes");
        assert_eq!(res.is_err(), true);
    }

    #[test]
    fn test_new_start() {
        let cfg = parse("start bitreich.org/1/lawn").unwrap();
        assert_eq!(cfg.start, "bitreich.org/1/lawn");
    }

    #[test]
    fn test_comments_ignored() {
        let cfg = parse("# wide yes\ntls yes").unwrap();
        assert_eq!(cfg.wide, false);
        assert_eq!(cfg.tls, true);
    }

    #[test]
    fn test_yes_or_true() {
        let cfg = parse("tls yes\nwide true").unwrap();
        assert_eq!(cfg.tls, true);
        assert_eq!(cfg.wide, true);
    }

    #[test]
    fn test_media() {
        let cfg = parse("media FALSE").unwrap();
        assert_eq!(cfg.media, None);

        let cfg = parse("media None").unwrap();
        assert_eq!(cfg.media, None);

        let cfg = parse("media /path/to/media-player").unwrap();
        assert_eq!(cfg.media, Some("/path/to/media-player".to_string()));

        let cfg = parse("media vlc").unwrap();
        assert_eq!(cfg.media, Some("vlc".to_string()));
    }

    #[test]
    fn test_no_or_false() {
        let cfg = parse("tls false\nwide no\ntor n").unwrap();
        assert_eq!(cfg.tls, false);
        assert_eq!(cfg.tor, false);
        assert_eq!(cfg.wide, false);
    }
    #[test]
    fn test_no_dupe_keys() {
        let res = parse("tls false\nwide no\nemoji yes\ntls yes");
        assert_eq!(res.is_err(), true);
        let e = res.unwrap_err();
        assert_eq!(format!("{}", e), "Duplicate key on line 4: tls");
    }

    #[test]
    fn test_encoding() {
        let cfg = parse("tls true\nwide no\nemoji yes").unwrap();
        assert_eq!(cfg.tls, true);
        assert_eq!(cfg.encoding, Encoding::default());

        let cfg = parse("tls true\nencoding utf8\n").unwrap();
        assert_eq!(cfg.tls, true);
        assert_eq!(cfg.encoding, Encoding::UTF8);

        let cfg = parse("tls true\nencoding CP437\n").unwrap();
        assert_eq!(cfg.tls, true);
        assert_eq!(cfg.encoding, Encoding::CP437);

        let res = parse("tls true\nencoding what\n");
        assert!(res.is_err());
    }

    #[test]
    fn test_missing_theme() {
        if let Err(e) = parse("theme /dont/exists.txt") {
            assert_eq!(
                format!("{}", e),
                "error loading theme: File not found on line 1: /dont/exists.txt"
            );
        }
    }

    #[test]
    fn test_theme_file() {
        use crate::theme::to_words;

        let cfg = parse("theme ./tests/pink.theme").unwrap();
        assert_eq!(to_words(cfg.theme.item_text), "magenta");
        assert_eq!(to_words(cfg.theme.item_menu), "magenta");
        assert_eq!(to_words(cfg.theme.item_error), "red");
        assert_eq!(to_words(cfg.theme.ui_menu), "cyan");
    }

    #[test]
    fn test_theme() {
        use crate::theme::to_words;

        let cfg = parse("item.text green\nitem.download red underline").unwrap();
        assert_eq!(to_words(cfg.theme.item_text), "green");
        assert_eq!(to_words(cfg.theme.item_download), "red underline");
    }

    #[test]
    fn test_theme_bad_values() {
        use crate::theme::to_words;

        let cfg = parse("item.text skyblue\nitem.download green underline\nitem.error invisible\nitem.search red green blue")
            .unwrap();
        assert_eq!(to_words(cfg.theme.item_text), "white");
        assert_eq!(to_words(cfg.theme.item_download), "green underline");
        assert_eq!(to_words(cfg.theme.item_error), "white");
        assert_eq!(to_words(cfg.theme.item_search), "red green blue");
    }
}