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
use std::collections::HashMap;
use std::fs::{self, File};
use std::io::Read;
use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
use futures::{future::TryFutureExt, stream::StreamExt};
use inotify::{Inotify, WatchMask};
use tokio::{sync::watch, task::JoinHandle};
use xdg::BaseDirectories;
use crate::report_err;
pub type ConfigWatcher = watch::Receiver<Config>;
#[derive(Default, Serialize, Deserialize, Clone, Debug)]
pub struct Config {
pub version: String,
pub data_dir: PathBuf,
#[serde(rename = "mail")]
pub mail_accounts: HashMap<String, MailAccountConfig>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct MailAccountConfig {
pub imap: ImapConfig,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ImapConfig {
pub server: String,
pub port: u16,
pub tls: TlsMethod,
#[serde(flatten)]
pub auth: ImapAuth,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(tag = "auth")]
pub enum ImapAuth {
#[serde(rename = "plain")]
#[allow(missing_docs)]
Plain { username: String, password: String },
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum TlsMethod {
#[serde(rename = "on")]
On,
#[serde(rename = "starttls")]
Starttls,
#[serde(rename = "off")]
Off,
}
async fn read_config(path: impl AsRef<Path>) -> Result<Config> {
let mut file = File::open(path.as_ref())?;
let mut contents = Vec::new();
file.read_to_end(&mut contents)?;
let config = toml::from_slice(&contents)?;
Ok(config)
}
async fn start_inotify_stream(
mut inotify: Inotify,
config_home: impl AsRef<Path>,
config_tx: watch::Sender<Config>,
) -> Result<()> {
let mut buffer = vec![0; 1024];
let mut event_stream = inotify.event_stream(&mut buffer)?;
let config_home = config_home.as_ref().to_path_buf();
let config_path = config_home.join("panorama.toml");
{
let config = read_config(&config_path).await?;
config_tx.send(config)?;
}
debug!("listening for inotify events");
while let Some(v) = event_stream.next().await {
let event = v.context("event")?;
debug!("inotify event: {:?}", event);
if let Some(name) = event.name {
let path = PathBuf::from(name);
let path_c = config_home
.clone()
.join(path.clone())
.canonicalize()
.context("osu")?;
if !path_c.exists() {
debug!("path {:?} doesn't exist", path_c);
continue;
}
let config_path_c = config_path.canonicalize().context("cfg_path")?;
if config_path_c != path_c {
debug!("did not match {:?} {:?}", config_path_c, path_c);
continue;
}
debug!("reading config from {:?}", path_c);
let config = read_config(path_c).await.context("read")?;
config_tx.send(config)?;
}
}
Ok(())
}
pub fn spawn_config_watcher_system() -> Result<(JoinHandle<()>, ConfigWatcher)> {
let mut inotify = Inotify::init()?;
let xdg = BaseDirectories::new()?;
let config_home = xdg.get_config_home().join("panorama");
if !config_home.exists() {
fs::create_dir_all(&config_home)?;
}
inotify
.add_watch(&config_home, WatchMask::CLOSE_WRITE)
.context("adding watch for config home")?;
debug!("watching {:?}", config_home);
let (config_tx, config_update) = watch::channel(Config::default());
let handle = tokio::spawn(
start_inotify_stream(inotify, config_home, config_tx).unwrap_or_else(report_err),
);
Ok((handle, config_update))
}