summaryrefslogtreecommitdiffstats
path: root/src/plugins/keepnick.rs
blob: 6ba16c1c0df66ba436fcb71d33cacbfb438e40e6 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::marker::PhantomData;

use irc::client::prelude::*;

use plugin::*;
use FrippyClient;

use error::ErrorKind as FrippyErrorKind;
use error::FrippyError;
use failure::ResultExt;

#[derive(PluginName, Default, Debug)]
pub struct KeepNick<C> {
    phantom: PhantomData<C>,
}

impl<C: FrippyClient> KeepNick<C> {
    pub fn new() -> Self {
        KeepNick {
            phantom: PhantomData,
        }
    }

    fn check_nick(&self, client: &C, leaver: &str) -> ExecutionStatus {
        let cfg_nick = match client.config().nickname {
            Some(ref nick) => nick.clone(),
            None => return ExecutionStatus::Done,
        };

        if leaver != cfg_nick {
            return ExecutionStatus::Done;
        }

        let client_nick = client.current_nickname();

        if client_nick != cfg_nick {
            info!("Trying to switch nick from {} to {}", client_nick, cfg_nick);
            match client
                .send(Command::NICK(cfg_nick))
                .context(FrippyErrorKind::Connection)
            {
                Ok(_) => ExecutionStatus::Done,
                Err(e) => ExecutionStatus::Err(e.into()),
            }
        } else {
            ExecutionStatus::Done
        }
    }
}

impl<C: FrippyClient> Plugin for KeepNick<C> {
    type Client = C;
    fn execute(&self, client: &Self::Client, message: &Message) -> ExecutionStatus {
        match message.command {
            Command::QUIT(ref nick) => {
                self.check_nick(client, &nick.clone().unwrap_or_else(String::new))
            }
            _ => ExecutionStatus::Done,
        }
    }

    fn execute_threaded(&self, _: &Self::Client, _: &Message) -> Result<(), FrippyError> {
        panic!("Tell should not use threading")
    }

    fn command(&self, client: &Self::Client, command: PluginCommand) -> Result<(), FrippyError> {
        client
            .send_notice(
                &command.source,
                "This Plugin does not implement any commands.",
            )
            .context(FrippyErrorKind::Connection)?;

        Ok(())
    }

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