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
|
use irc::client::prelude::*;
use irc::error::Error as IrcError;
use std::collections::HashMap;
use std::sync::Mutex;
use plugin::*;
#[derive(PluginName, Debug)]
pub struct Factoids {
factoids: Mutex<HashMap<String, String>>,
}
macro_rules! try_lock {
( $m:expr ) => {
match $m.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
}
}
}
impl Factoids {
pub fn new() -> Factoids {
Factoids { factoids: Mutex::new(HashMap::new()) }
}
fn add(&self, server: &IrcServer, command: &mut PluginCommand) -> Result<(), IrcError> {
if command.tokens.len() < 2 {
return self.invalid_command(server, command);
}
let name = command.tokens.remove(0);
try_lock!(self.factoids)
.insert(name, command.tokens.join(" "));
server.send_notice(&command.source, "Successfully added")
}
fn get(&self, server: &IrcServer, command: &PluginCommand) -> Result<(), IrcError> {
if command.tokens.len() < 1 {
self.invalid_command(server, command)
} else {
let factoids = try_lock!(self.factoids);
let factoid = match factoids.get(&command.tokens[0]) {
Some(v) => v,
None => return self.invalid_command(server, command),
};
server.send_privmsg(&command.target, factoid)
}
}
fn invalid_command(&self, server: &IrcServer, command: &PluginCommand) -> Result<(), IrcError> {
server.send_notice(&command.source, "Invalid Command")
}
}
impl Plugin for Factoids {
fn is_allowed(&self, _: &IrcServer, message: &Message) -> bool {
match message.command {
Command::PRIVMSG(_, ref content) => content.starts_with('!'),
_ => false,
}
}
fn execute(&self, server: &IrcServer, message: &Message) -> Result<(), IrcError> {
if let Command::PRIVMSG(_, mut content) = message.command.clone() {
content.remove(0);
let t: Vec<String> = content
.split(' ')
.map(ToOwned::to_owned)
.collect();
let c = PluginCommand {
source: message.source_nickname().unwrap().to_string(),
target: message.response_target().unwrap().to_string(),
tokens: t,
};
self.get(server, &c)
} else {
Ok(())
}
}
fn command(&self, server: &IrcServer, mut command: PluginCommand) -> Result<(), IrcError> {
if command.tokens.is_empty() {
self.invalid_command(server, &command)
} else if command.tokens[0].to_lowercase() == "add" {
command.tokens.remove(0);
self.add(server, &mut command)
} else if command.tokens[0].to_lowercase() == "get" {
command.tokens.remove(0);
self.get(server, &command)
} else {
self.invalid_command(server, &command)
}
}
}
|