extern crate irc;
extern crate rusqlite;
extern crate tellirc;
use irc::client::prelude::*;
use rusqlite::Connection;
use tellirc::{DaoTrait, get_actual_message, new_config_from_file};
fn main() {
let mut args = std::env::args();
args.next();
let file_name = match args.next() {
Some(file) => file,
None => { eprintln!("Can not open config file"); return; }
};
let config = new_config_from_file(&file_name).unwrap();
let mut reactor = IrcReactor::new().unwrap();
let client = reactor.prepare_client_and_connect(&config).unwrap();
client.identify().unwrap();
let sql_conn = Connection::open("tellirc.sqlite").unwrap();
let dao = tellirc::Dao::new(sql_conn);
reactor.register_client_with_handler(client, move|client, message| {
println!("{}", message);
if let Command::PRIVMSG(ref __, ref msg) = message.command {
let target = message.response_target().unwrap().clone();
let sender = message.source_nickname().unwrap().clone();
if !target.starts_with("#") {
client.send_privmsg(target, "Please don't PM me sir, this is WIP")?;
return Ok(())
}
if msg.starts_with("!tell") {
let tks: Vec<&str> = msg.split(" ").collect();
let tks_len = tks.len();
if tks_len > 2 && tks[0].eq_ignore_ascii_case("!tell@aa") {
let add_res = dao.add_alias_for(tks[1], tks[2]);
if let Ok(als) = add_res {
client.send_privmsg(target, format!("{:?} are now aliases", als)).unwrap();
return Ok(())
} else if let Err(e) = add_res {
client.send_privmsg(target, format!("{:?}", e)).unwrap();
return Ok(())
}
} else if tks_len > 1 && tks[0].eq_ignore_ascii_case("!tell@ga") {
let get_res = dao.get_alias_for(tks[1]);
if let Ok((_, als)) = get_res {
client.send_privmsg(target, format!("{:?} are now aliases", als)).unwrap();
return Ok(())
}
} else if tks_len > 2 && tks[0].eq_ignore_ascii_case("!tell@da") {
let del_res = dao.del_alias_for(tks[1], tks[2]);
if let Ok(ref als) = del_res {
client.send_privmsg(target, format!("{:?} are now aliases", als)).unwrap();
return Ok(())
}
} else if tks_len > 0 && tks[0].eq_ignore_ascii_case("!tell@quit") {
client.send_privmsg(target, "No! >:(").unwrap();
return Ok(())
} else if tks_len > 1 && tks[0].eq_ignore_ascii_case("!tell@join") {
let chan = tks[1];
match client.send_join(chan) {
Ok(_) => return Ok(()),
Err(e) => client.send_privmsg(target, format!("Cannot join {}, Error: {:#?}", chan, e)).unwrap(),
};
} else if tks_len > 2 && tks[0].eq_ignore_ascii_case("!tell") {
let who = tks[1];
let als = dao.get_alias_for(who).unwrap().1;
if client.list_users(target).unwrap().iter().any(|user| { als.contains(&String::from(user.get_nickname())) } ) {
client.send_privmsg(target, format!("{} is here! Why don't you tell that to them yourself??", who))?;
return Ok(())
}
let actual_msg = get_actual_message(&msg);
let dao_res = dao.add_message_for(who, actual_msg, &Some(target), &sender);
match dao_res {
Ok(_) => client.send_privmsg(target, format!("{}: Got it, sending \"{}\" to {} upon joining", sender, actual_msg, target))?,
Err(err) => client.send_privmsg(target, err)?,
}
} else {
client.send_privmsg(sender, "Usage: (WIP)")?;
}
}
} else if let Command::JOIN(ref chanlist, ref __, ref ___) = message.command {
let target = message.response_target().unwrap().clone();
let dao_res = dao.get_message_for(&target, chanlist);
match dao_res {
Ok(Some(msgs)) => msgs.iter().for_each(|msg| {
client.send_privmsg(chanlist, format!("{}: {} tells you \"{}\"", target, msg.from_name, msg.message)).unwrap();
dao.del_message(msg).unwrap();
}),
_ => ()
}
return Ok(())
}
Ok(())
});
reactor.run().unwrap();
}