-
Notifications
You must be signed in to change notification settings - Fork 72
/
server.rs
166 lines (149 loc) · 5.13 KB
/
server.rs
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
use std::collections::BTreeMap;
use std::{fmt, str};
use tokio::fs;
use tokio::process::Command;
use futures::channel::mpsc::Sender;
use irc::proto;
use serde::{Deserialize, Serialize};
use crate::config;
use crate::config::server::Sasl;
use crate::config::Error;
pub type Handle = Sender<proto::Message>;
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Server(String);
impl From<&str> for Server {
fn from(value: &str) -> Self {
Server(value.to_string())
}
}
impl fmt::Display for Server {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl AsRef<str> for Server {
fn as_ref(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone)]
pub struct Entry {
pub server: Server,
pub config: config::Server,
}
impl<'a> From<(&'a Server, &'a config::Server)> for Entry {
fn from((server, config): (&'a Server, &'a config::Server)) -> Self {
Self {
server: server.clone(),
config: config.clone(),
}
}
}
#[derive(Debug, Clone, Default, Deserialize)]
pub struct Map(BTreeMap<Server, config::Server>);
async fn read_from_command(pass_command: &str) -> Result<String, Error> {
let output = if cfg!(target_os = "windows") {
Command::new("cmd")
.arg("/C")
.arg(pass_command)
.output()
.await?
} else {
Command::new("sh")
.arg("-c")
.arg(pass_command)
.output()
.await?
};
if output.status.success() {
// we remove trailing whitespace, which might be present from unix pipelines with a
// trailing newline
Ok(str::from_utf8(&output.stdout)?.trim_end().to_string())
} else {
Err(Error::ExecutePasswordCommand(String::from_utf8(
output.stderr,
)?))
}
}
impl Map {
pub fn insert(&mut self, name: Server, server: config::Server) {
self.0.insert(name, server);
}
pub fn remove(&mut self, server: &Server) {
self.0.remove(server);
}
pub fn contains(&self, server: &Server) -> bool {
self.0.contains_key(server)
}
pub fn keys(&self) -> impl Iterator<Item = &Server> {
self.0.keys()
}
pub fn entries(&self) -> impl Iterator<Item = Entry> + '_ {
self.0.iter().map(Entry::from)
}
pub async fn read_passwords(&mut self) -> Result<(), Error> {
for (_, config) in self.0.iter_mut() {
if let Some(pass_file) = &config.password_file {
if config.password.is_some() || config.password_command.is_some() {
return Err(Error::DuplicatePassword);
}
let pass = fs::read_to_string(pass_file).await?;
config.password = Some(pass);
}
if let Some(pass_command) = &config.password_command {
if config.password.is_some() {
return Err(Error::DuplicatePassword);
}
config.password = Some(read_from_command(pass_command).await?);
}
if let Some(nick_pass_file) = &config.nick_password_file {
if config.nick_password.is_some() || config.nick_password_command.is_some() {
return Err(Error::DuplicateNickPassword);
}
let nick_pass = fs::read_to_string(nick_pass_file).await?;
config.nick_password = Some(nick_pass);
}
if let Some(nick_pass_command) = &config.nick_password_command {
if config.password.is_some() {
return Err(Error::DuplicateNickPassword);
}
config.password = Some(read_from_command(nick_pass_command).await?);
}
if let Some(sasl) = &mut config.sasl {
match sasl {
Sasl::Plain {
password: Some(_),
password_file: None,
password_command: None,
..
} => {}
Sasl::Plain {
password: password @ None,
password_file: Some(pass_file),
password_command: None,
..
} => {
let pass = fs::read_to_string(pass_file).await?;
*password = Some(pass);
}
Sasl::Plain {
password: password @ None,
password_file: None,
password_command: Some(pass_command),
..
} => {
let pass = read_from_command(pass_command).await?;
*password = Some(pass);
}
Sasl::Plain { .. } => {
return Err(Error::DuplicateSaslPassword);
}
Sasl::External { .. } => {
// no passwords to read
}
}
}
}
Ok(())
}
}