summaryrefslogtreecommitdiffstats
path: root/src/plugins/keepnick.rs
blob: 4a40e8fd85954593bfa9d023ac8f6e42f4062a60 (plain) (blame)
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
use irc::client::prelude::*;
use irc::error::IrcError;

use plugin::*;

#[derive(PluginName, Default, Debug)]
pub struct KeepNick;

impl KeepNick {
    pub fn new() -> KeepNick {
        KeepNick {}
    }

    fn check_nick(&self, server: &IrcClient, leaver: &str) -> Result<(), IrcError> {
        let cfg_nick = match server.config().nickname {
            Some(ref nick) => nick.clone(),
            None => return Ok(()),
        };

        if leaver != cfg_nick {
            return Ok(());
        }

        let server_nick = server.current_nickname();

        if server_nick != cfg_nick {
            info!("Trying to switch nick from {} to {}", server_nick, cfg_nick);
            server.send(Command::NICK(cfg_nick))

        } else {
            Ok(())
        }
    }
}

impl Plugin for KeepNick {
    fn is_allowed(&self, _: &IrcClient, message: &Message) -> bool {
        match message.command {
            Command::QUIT(_) => true,
            _ => false,
        }
    }

    fn execute(&self, server: &IrcClient, message: &Message) -> Result<(), IrcError> {
        match message.command {
            Command::QUIT(ref nick) => {
                self.check_nick(server, &nick.clone().unwrap_or_else(|| String::new()))
            }
            _ => Ok(()),
        }
    }

    fn command(&self, server: &IrcClient, command: PluginCommand) -> Result<(), IrcError> {
        server.send_notice(&command.source,
                           "This Plugin does not implement any commands.")
    }

    fn evaluate(&self, _: &IrcClient, _: PluginCommand) -> Result<String, String> {
        Err(String::from("This Plugin does not implement any commands."))
    }
}

#[cfg(test)]
mod tests {}