Last active
August 29, 2015 14:01
-
-
Save db48x/7d3805013e583f6739a2 to your computer and use it in GitHub Desktop.
a hypothetical way to write an IRC bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use seen; | |
struct Bot { | |
modules: Vec<BotModule> | |
} | |
trait BotCmd { } | |
struct BotModule { | |
name: str. | |
// this is probably impossible | |
commands: HashTable<str, HashMap<str, |cmd: &str, rest: &str| -> BotCmd> | |
} | |
impl Bot { | |
pub fn start(&self) { | |
let client = irc::client(server, yadda, yadda, yadda); | |
seen::start(client); | |
client.monitor(bot::allExceptBlacklist, | |
|msg: irc::Message| { | |
match msg { | |
irc::PrivMsg(sender, recip, msg) => { | |
let (first, rest) = parseFirstWord(msg); | |
if (first.starts_with("!")) { | |
let name = first.rest(); | |
self.modules.iter().enumerate(|i, m| { | |
let cmd = m.commands.find(cmd).get(0)(name, rest); | |
self.modules[i].oncommand(cmd); | |
}); | |
} | |
} | |
_ => ignore | |
} | |
}); | |
} | |
pub fn register(&self, n: str, cmds: HashMap<&str, |cmd: &str, rest: &str| -> BotCmd>) { | |
self.modules.append(BotModule { name: n, commands: cmds }) | |
} | |
} | |
fn allExceptBlacklist(sender: irc::Recipient) -> bool { | |
true | |
} | |
mod seen { | |
enum SeenCmd { | |
Query(irc::Recipient), | |
Ignore(irc::Recipient), | |
} | |
impl BotCmd for SeenCmd { } | |
pub fn start(bot) { | |
// listening to the activity around us | |
bot.client.monitor(bot::allExceptBlacklist, | |
|msg: irc::Message| { | |
match msg { | |
irc::JoinMsg(sender, chan) => saw_joining(sender, chan), | |
irc::PrivMsg(sender, recip, msg) => saw_saying(sender, recip, msg), | |
irc::PartMsg(sender, chan, msg) => saw_leaving(sender, chan, msg), | |
_ => ignore | |
} | |
}); | |
// register commands that users can send us | |
// could probably just return them | |
let mut cmds = HashMap<str, |cmd: &str, rest: &str| -> BotCmd>; | |
cmds.insert("seen", |cmd, rest| { Query(bot::parseWords(rest).get(0)) }); | |
cmds.insert("ignore", |cmd, rest| { Ignore(bot::parseWords(rest).get(0)) }); | |
bot.register(cmds); | |
// process commands we we get them | |
bot.oncommand(client, | |
|chan: irc::Recipient, cmd: SeenCmd| -> irc::PrivMsg { | |
PrivMsg(chan, | |
match cmd { | |
Query(target) => query(target), | |
Ignore(target) => ignore(target), | |
_ => "fail" | |
}) | |
}); | |
} | |
fn saw_joining(sender: irc::Recipient, channel: irc::Recipient) { } | |
fn saw_saying(sender: irc::Recipient, channel: irc::Recipient, msg: irc::Message) { } | |
fn saw_leaving(sender: irc::Recipient, channel: irc::Recipient, msg: irc::Message) { } | |
fn query(target: irc::Recipient) { } | |
fn ignore(target: irc::Recipient) { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment