def __init__(self, cfg, contacts): threading.Thread.__init__(self) self.must_run = True self.irc_server = cfg["irc_server_name"] self.irc_port = cfg["irc_server_port"] self.owner_nick = cfg["bot_owner_nick"] self.wa_phone = cfg["wa_phone"] self.log_file = cfg["log_file"] self.irc_nick = cfg["irc_nick"] self.wa_password = cfg["wa_password"] self.contacts = contacts self.spamcheck = cfg["spamcheck"] self.logging = cfg["logging"] self.spamcheck_nick = cfg["spamcheck_nick"] self.verbose = cfg["verbose"] fwd_filters = cfg["filter"] if fwd_filters != "": info(2, "Filter messages: "+fwd_filters) filters = fwd_filters.split(",") self.filters = filters info(2, "Found %s Filters" %len(filters)) else: self.filters = [] if len(self.filters) == 0: info(2, "Filtering disabled") self.irc_i = IRCInterface(self.irc_server, self.irc_port, self.irc_nick, self.owner_nick, channels_from_contacts(self.contacts), self.irc_msg_received, self.stop) self.irc_i.spamcheck_enabled = self.spamcheck self.wa_i = WAInterface(self.wa_phone, self.wa_password, self.wa_msg_received, self.stop)
def __init__(self, wa_phone, wa_password, contacts, irc_server, irc_port, owner_nick, log_file): threading.Thread.__init__(self) self.must_run = True self.irc_server = irc_server self.irc_port = irc_port self.owner_nick = owner_nick self.wa_phone = wa_phone self.log_file = log_file irc_nick = contacts[wa_phone] self.irc_nick = irc_nick self.wa_password = wa_password self.contacts = contacts self.irc_i = IRCInterface(self.irc_server, self.irc_port, self.irc_nick, channels_from_contacts(self.contacts), self.irc_msg_received, self.stop) self.wa_i = WAInterface(self.wa_phone, self.wa_password, self.wa_msg_received, self.stop)
class Bot(threading.Thread): def __init__(self, wa_phone, wa_password, contacts, irc_server, irc_port, owner_nick, log_file): threading.Thread.__init__(self) self.must_run = True self.irc_server = irc_server self.irc_port = irc_port self.owner_nick = owner_nick self.wa_phone = wa_phone self.log_file = log_file irc_nick = contacts[wa_phone] self.irc_nick = irc_nick self.wa_password = wa_password self.contacts = contacts self.irc_i = IRCInterface(self.irc_server, self.irc_port, self.irc_nick, channels_from_contacts(self.contacts), self.irc_msg_received, self.stop) self.wa_i = WAInterface(self.wa_phone, self.wa_password, self.wa_msg_received, self.stop) @catch_them_all def run(self): try: self.must_run = True info("Starting IRC") self.irc_i.start() info("Waiting for IRC") self.irc_i.wait_connected() info("Starting WA") self.wa_i.start() info("Waiting for WA") self.wa_i.wait_connected() info("Main loop running") except: info("Main loop closing") self.stop() def stop(self): info("Stop instructed, about to stop main loop") self.must_run = False self.irc_i.stop() self.wa_i.stop() def get_group_from_chan(self, contacts, irc_channel): for k,v in contacts.items(): if v.lower() == irc_channel.lower(): return k raise Exception("Channel not found in contact list") @catch_them_all def irc_msg_received(self, message): store_msg(message, self.log_file) info(" <<< Received IRC message: %s" %message) if message.chan == self.irc_nick: if message.target is None: raise Exception("Private message sent to no one?") try: wa_target = self.contacts[message.target] #try by phone except KeyError: wa_target = self.get_group_from_chan(self.contacts, message.target) #try by nick wa_target += "@s.whatsapp.net" msg = "<%s> %s" %(message.get_nick(), message.msg.split(":", 1)[1]) self.wa_i.send(wa_target, msg) else: msg = "<%s> %s" %(message.get_nick(), message.msg) try: group = self.get_group_from_chan(self.contacts, message.chan) self.wa_i.send(group, msg) except Exception, e: error(traceback.print_exc()) error("Cannot send message to channel %s: %s" %(message.chan, e))
class Bot(threading.Thread): def __init__(self, wa_phone, wa_password, contacts, irc_server, irc_port, owner_nick, log_file): threading.Thread.__init__(self) self.must_run = True self.irc_server = irc_server self.irc_port = irc_port self.owner_nick = owner_nick self.wa_phone = wa_phone self.log_file = log_file irc_nick = contacts[wa_phone] self.irc_nick = irc_nick self.wa_password = wa_password self.contacts = contacts self.irc_i = IRCInterface(self.irc_server, self.irc_port, self.irc_nick, channels_from_contacts(self.contacts), self.irc_msg_received, self.stop) self.wa_i = WAInterface(self.wa_phone, self.wa_password, self.wa_msg_received, self.stop) @catch_them_all def run(self): try: self.must_run = True info("Connecting IRC client (%s@%s:%s)" %(self.irc_nick, self.irc_server, self.irc_port)) self.irc_i.start() self.irc_i.wait_connected() info("Connecting WA client (%s)" %self.wa_phone) self.wa_i.start() self.wa_i.wait_connected() info("Bot ready.") except: info("Main loop closing") self.stop() def stop(self): info("Bot stopping...") self.must_run = False self.irc_i.stop() self.wa_i.stop() def get_group_from_chan(self, contacts, irc_channel): for k,v in contacts.items(): if v.lower() == irc_channel.lower(): return k raise Exception("Channel not found in contact list") @catch_them_all def irc_msg_received(self, message): store_msg(message, self.log_file) info(" <<< IRC %s" %message) if message.chan == self.irc_nick: if message.target is None: raise Exception("Target not specified. Please prefix you private messages with a nickname (e.g. 'person1: hello') or phone number (e.g. '+34555555373: hello')") try: wa_target = self.contacts[message.target] #try by phone except KeyError: wa_target = self.get_group_from_chan(self.contacts, message.target) #try by nick wa_target += "@s.whatsapp.net" msg = "<%s> %s" %(message.get_nick(), message.msg.split(":", 1)[1]) self.wa_i.send(wa_target, msg) else: msg = "<%s> %s" %(message.get_nick(), message.msg) try: group = self.get_group_from_chan(self.contacts, message.chan) self.wa_i.send(group, msg) except Exception, e: error("Cannot send message to channel %s: %s" %(message.chan, e))
class Bot(threading.Thread): def __init__(self, wa_phone, wa_password, contacts, irc_server, irc_port, owner_nick, log_file): threading.Thread.__init__(self) self.must_run = True self.irc_server = irc_server self.irc_port = irc_port self.owner_nick = owner_nick self.wa_phone = wa_phone self.log_file = log_file irc_nick = contacts[wa_phone] self.irc_nick = irc_nick self.wa_password = wa_password self.contacts = contacts self.irc_i = IRCInterface(self.irc_server, self.irc_port, self.irc_nick, channels_from_contacts(self.contacts), self.irc_msg_received, self.stop) self.wa_i = WAInterface(self.wa_phone, self.wa_password, self.wa_msg_received, self.stop) @catch_them_all def run(self): try: self.must_run = True info("Connecting IRC client (%s@%s:%s)" % (self.irc_nick, self.irc_server, self.irc_port)) self.irc_i.start() self.irc_i.wait_connected() info("Connecting WA client (%s)" % self.wa_phone) self.wa_i.start() self.wa_i.wait_connected() info("Bot ready.") except: info("Main loop closing") self.stop() def stop(self): info("Bot stopping...") self.must_run = False self.irc_i.stop() self.wa_i.stop() def get_wa_id_from_name(self, contacts, name): for k, v in contacts.items(): if v.lower() == name.lower(): return k raise KeyError("Name %s not found in contact list" % name) @catch_them_all def irc_msg_received(self, message): store_msg(message, self.log_file) info(" <<< IRC %s" % message) if message.chan == self.irc_nick: if message.target is None: raise Exception( "Target not specified. Please prefix you private messages with a nickname (e.g. 'person1: hello') or phone number (e.g. '+34555555373: hello')" ) wa_target = message.target if message.target not in self.contacts: try: wa_target = self.get_wa_id_from_name( self.contacts, message.target) #get by nick except KeyError: if not wa_target.isdigit(): raise Exception( "Whatsapp identifier '%s' not found in contact list, and does not look like a phone number" % message.target) warning( "Phone number '%s' not found in contact list. Trying to send anyway..." % message.target) wa_target += "@s.whatsapp.net" msg = "<%s> %s" % (message.get_nick(), message.msg) self.wa_i.send(wa_target, msg) else: msg = "<%s> %s" % (message.get_nick(), message.msg) try: group = self.get_wa_id_from_name(self.contacts, message.chan) self.wa_i.send(group, msg) except Exception, e: error("Cannot send message to channel %s: %s" % (message.chan, e))
class Bot(threading.Thread): def __init__(self, cfg, contacts): threading.Thread.__init__(self) self.must_run = True self.irc_server = cfg["irc_server_name"] self.irc_port = cfg["irc_server_port"] self.owner_nick = cfg["bot_owner_nick"] self.wa_phone = cfg["wa_phone"] self.log_file = cfg["log_file"] self.irc_nick = cfg["irc_nick"] self.wa_password = cfg["wa_password"] self.contacts = contacts self.spamcheck = cfg["spamcheck"] self.logging = cfg["logging"] self.spamcheck_nick = cfg["spamcheck_nick"] self.verbose = cfg["verbose"] fwd_filters = cfg["filter"] if fwd_filters != "": info(2, "Filter messages: "+fwd_filters) filters = fwd_filters.split(",") self.filters = filters info(2, "Found %s Filters" %len(filters)) else: self.filters = [] if len(self.filters) == 0: info(2, "Filtering disabled") self.irc_i = IRCInterface(self.irc_server, self.irc_port, self.irc_nick, self.owner_nick, channels_from_contacts(self.contacts), self.irc_msg_received, self.stop) self.irc_i.spamcheck_enabled = self.spamcheck self.wa_i = WAInterface(self.wa_phone, self.wa_password, self.wa_msg_received, self.stop) @catch_them_all def run(self): try: self.must_run = True info(1, "Starting IRC") self.irc_i.start() info(1, "Waiting for IRC") self.irc_i.wait_connected() info(1, "Starting WA") self.wa_i.start() info(1, "Waiting for WA") self.wa_i.wait_connected() info(1, "Program ready") info(3, "Main loop running") except: info(3, "Main loop closing") self.stop() def stop(self): info(2, "Stop instructed, about to stop main loop") self.must_run = False self.irc_i.stop() self.wa_i.stop() def get_group_from_chan(self, contacts, irc_channel): for k,v in contacts.items(): if v.lower() == irc_channel.lower(): return k raise Exception("Channel not found in contact list") @catch_them_all def irc_msg_received(self, message): store_msg(message, self.log_file) info(3, " <<< IRC %s" %message) if message.get_nick() == self.owner_nick and message.msg == "!die": info(1, "Got die from Owner") self.irc_i.cli.send("QUIT :Die from owner") self.stop() elif message.msg == "!die": info(1, "%s in %s tried to die me. Slap around him." %(message.get_nick(), message.chan)) self.irc_i.send_queue.put("PRIVMSG %s :\001ACTION slaps %s around with a large trout\001" %(message.chan, message.get_nick())) if self.spamcheck == 1: #if message.target is None: #raise Exception("Private message sent to no one?") if message.get_nick() != self.spamcheck_nick and message.target != None: try: wa_target = self.contacts[message.target] #try by phone except KeyError: try: wa_target = self.get_group_from_chan(self.contacts, message.target) #try by nick except: wa_target = "" info(2, "Group not recognized") if wa_target != "": wa_target += "@s.whatsapp.net" msg = "<%s> %s" %(message.get_nick(), message.msg.split(":", 1)[1]) self.wa_i.send(wa_target, msg) elif message.get_nick() == self.spamcheck_nick: if self.irc_i.server_spamcheck == False: self.irc_i.server_spamcheck = True info(3, "SpamScanner scan is done. Now joining channels.") self.irc_i.join_channels() else: msg = "<%s> %s" %(message.get_nick(), message.msg) if len(self.filters) > 0: matches = 0 for f in self.filters: if re.search(f, message.msg, re.I): matches = matches + 1 if matches > 0: info(2, "IRC Message matching filters. Forwarding Message.") try: group = self.get_group_from_chan(self.contacts, message.chan) self.wa_i.send(group, msg) except Exception, e: error(traceback.print_exc()) error("Cannot send message to channel %s: %s" %(message.chan, e)) else: info(3, "IRC Message filtering disabled. Forwarding Message.") try: group = self.get_group_from_chan(self.contacts, message.chan) self.wa_i.send(group, msg) except Exception, e: error(traceback.print_exc()) error("Cannot send message to channel %s: %s" %(message.chan, e))