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
use anyhow::Result;
use irc::client::prelude::*;
use irc::proto::mode::ChannelMode::Ban;
use lazy_static::lazy_static;
use regex::Regex;
use serde::Deserialize;
use std::collections::{HashMap, HashSet};

const FOUNDER: &[char; 11] =
    &['A', 'F', 'R', 'e', 'f', 'i', 'o', 'r', 's', 't', 'v'];
const CRAT: &[char; 7] = &['A', 'f', 'i', 'o', 'r', 't', 'v'];
const OP: &[char; 5] = &['A', 'i', 'o', 't', 'v'];
const PLUS_O: &[char; 1] = &['o'];
const AUTOVOICE: &[char; 2] = &['V', 'v'];

// TODO: set forward to -overflow
const GLOBAL_BANS: &str = "$j:#wikimedia-bans";
const LIBERA_STAFF: &str = "*!*@libera/staff/*";
const LITHARGE: &str = "litharge";
const WMOPBOT: &str = "wmopbot";

fn parse_flags(input: &str) -> HashSet<char> {
    let mut set = HashSet::new();
    for char in input.chars() {
        if char == '+' || char == '-' {
            continue;
        }
        set.insert(char);
    }
    set
}

#[derive(Debug, Default, Deserialize)]
pub struct ConfiguredChannel {
    #[serde(default)]
    pub founders: HashSet<String>,
    #[serde(default)]
    pub crats: HashSet<String>,
    #[serde(default)]
    pub ops: HashSet<String>,
    #[serde(default)]
    pub plus_o: HashSet<String>,
    #[serde(default)]
    pub autovoice: HashSet<String>,
    #[serde(default)]
    pub global_bans: bool,
    /// Gives Libera staff and litharge +o rights
    #[serde(default)]
    pub libera_staff: bool,
    /// Gives wmopbot +ot rights
    #[serde(default)]
    pub wmopbot: bool,
    #[serde(default)]
    pub invexes: HashSet<String>,
}

#[derive(Debug, Default, Deserialize)]
pub struct ManagedChannel {
    #[serde(default)]
    pub bans: HashSet<String>,
    #[serde(default)]
    pub invexes: HashSet<String>,
    // unknown modes
    #[serde(default)]
    pub current: HashMap<String, HashSet<char>>,
    // state stuff
    #[serde(default)]
    pub flags_done: bool,
    #[serde(default)]
    pub bans_done: bool,
    #[serde(default)]
    pub invexes_done: bool,
}

#[derive(Default, Debug)]
struct FlagChange {
    current: HashSet<char>,
    should: HashSet<char>,
}

impl FlagChange {
    fn to_mode(&self) -> Option<String> {
        if self.current == self.should {
            // No changes needed
            return None;
        }
        dbg!(self);
        // Flags currently held but shouldn't hold
        let mut remove: Vec<char> =
            self.current.difference(&self.should).cloned().collect();
        remove.sort_unstable();
        // Flags that should be held but currently aren't
        let mut add: Vec<char> =
            self.should.difference(&self.current).cloned().collect();
        add.sort_unstable();
        let mut mode = "".to_string();
        if !remove.is_empty() {
            mode.push('-');
            mode.extend(remove);
        }
        if !add.is_empty() {
            mode.push('+');
            mode.extend(add);
        }
        Some(mode)
    }
}

impl ManagedChannel {
    pub fn is_done(&self) -> bool {
        self.flags_done && self.bans_done && self.invexes_done
    }

    pub fn fix_flags(&self, cfg: &ConfiguredChannel) -> Vec<(String, String)> {
        let mut changes: HashMap<String, FlagChange> = HashMap::new();
        for (name, flags) in self.current.iter() {
            changes
                .entry(name.to_string())
                .or_default()
                .current
                .extend(flags);
        }

        for name in &cfg.founders {
            changes
                .entry(name.to_string())
                .or_default()
                .should
                .extend(FOUNDER.iter());
        }
        for name in &cfg.crats {
            changes
                .entry(name.to_string())
                .or_default()
                .should
                .extend(CRAT.iter());
        }
        for name in &cfg.ops {
            changes
                .entry(name.to_string())
                .or_default()
                .should
                .extend(OP.iter());
        }
        for name in &cfg.plus_o {
            changes
                .entry(name.to_string())
                .or_default()
                .should
                .extend(PLUS_O.iter());
        }
        for name in &cfg.autovoice {
            changes
                .entry(name.to_string())
                .or_default()
                .should
                .extend(AUTOVOICE.iter());
        }
        if cfg.libera_staff {
            changes
                .entry(LIBERA_STAFF.to_string())
                .or_default()
                .should
                .extend(PLUS_O.iter());
            changes
                .entry(LITHARGE.to_string())
                .or_default()
                .should
                .extend(PLUS_O.iter());
        }
        if cfg.wmopbot {
            changes
                .entry(WMOPBOT.to_string())
                .or_default()
                .should
                .extend(&['o', 't'])
        }

        changes
            .iter()
            .filter_map(|(username, change)| {
                change.to_mode().map(|mode| (username.to_string(), mode))
            })
            .collect()
    }

    pub fn fix_modes(&self, cfg: &ConfiguredChannel) -> Vec<Mode<ChannelMode>> {
        let mut cmds = vec![];
        if cfg.global_bans && !self.bans.contains(GLOBAL_BANS) {
            cmds.push(Mode::Plus(Ban, Some(GLOBAL_BANS.to_string())));
        } else if !cfg.global_bans && self.bans.contains(GLOBAL_BANS) {
            cmds.push(Mode::Minus(Ban, Some(GLOBAL_BANS.to_string())));
        }

        cmds
    }

    pub fn add_flags_from_chanserv(&mut self, line: &str) -> Result<()> {
        // 2        legoktm                +AFRefiorstv         (FOUNDER) [modified...
        lazy_static! {
            static ref RE: Regex =
                Regex::new(r"^[0-9]+\s+([^\s]+)\s+\+([A-z]+)").unwrap();
        }
        if let Some(caps) = RE.captures(line) {
            if caps.len() < 3 {
                return Err(anyhow::anyhow!("Not enough captures: {}", line));
            }
            let account = caps[1].to_string();
            self.current.insert(account, parse_flags(&caps[2]));
            Ok(())
        } else {
            Err(anyhow::anyhow!("Couldn't parse: {}", line))
        }
    }
}

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

    fn set(arg: &str) -> HashSet<String> {
        let mut set = HashSet::new();
        set.insert(arg.to_string());
        set
    }

    #[test]
    fn test_fix_flags() {
        let managed = ManagedChannel {
            current: [("foo".to_string(), FOUNDER.iter().cloned().collect())]
                .iter()
                .cloned()
                .collect(),
            ..Default::default()
        };
        let cfg = ConfiguredChannel {
            founders: set("bar"),
            ops: set("foo"),
            ..Default::default()
        };
        let mut res = managed.fix_flags(&cfg);
        res.sort();
        let expected = vec![
            ("bar".to_string(), "+AFRefiorstv".to_string()),
            ("foo".to_string(), "-FRefrs".to_string()),
        ];
        assert_eq!(expected, res);
    }

    #[test]
    fn test_libera_staff() {
        let cfg = ConfiguredChannel {
            libera_staff: true,
            ..Default::default()
        };
        let mut res = ManagedChannel::default().fix_flags(&cfg);
        res.sort();
        let expected = vec![
            (LIBERA_STAFF.to_string(), "+o".to_string()),
            (LITHARGE.to_string(), "+o".to_string()),
        ];
        assert_eq!(expected, res);
    }

    #[test]
    fn test_flag_change() {
        let mut change = FlagChange::default();
        change.current.extend(['A', 'B', 'C'].iter());
        assert_eq!(&change.to_mode().unwrap(), "-ABC");
        change.should.extend(['A', 'B', 'D'].iter());
        assert_eq!(&change.to_mode().unwrap(), "-C+D");
        change.current.clear();
        assert_eq!(&change.to_mode().unwrap(), "+ABD");
    }

    #[test]
    fn test_add_chanserv_flags_line() {
        // 1        legoktm                +AFRefiorstv         (FOUNDER) [modified 9s ago, on May 30 09:21:18 2021 +0000, by legoktm]
        // 2        addshore               +Aiotv               [modified 30m 34s ago, on May 30 08:50:53 2021 +0000, by legoktm]
        let mut channel = ManagedChannel::default();
        channel.add_flags_from_chanserv(
            "1        legoktm                +AFRefiorstv         (FOUNDER) [modified 9s ago, on May 30 09:21:18 2021 +0000, by legoktm]"
        ).unwrap();
        assert_eq!(
            channel.current,
            [("legoktm".to_string(), "AFRefiorstv".chars().collect())]
                .iter()
                .cloned()
                .collect()
        );
        channel.add_flags_from_chanserv(
            "2        addshore               +Aiotv               [modified 30m 34s ago, on May 30 08:50:53 2021 +0000, by legoktm]"
        ).unwrap();
        assert_eq!(
            channel.current,
            [
                ("legoktm".to_string(), "AFRefiorstv".chars().collect()),
                ("addshore".to_string(), "Aiotv".chars().collect())
            ]
            .iter()
            .cloned()
            .collect()
        );
    }

    #[test]
    fn test_parse_flags() {
        assert_eq!(parse_flags("+Vv"), vec!['v', 'V'].into_iter().collect(),);
    }
}