Exemple #1
0
 def __init__(self, wa_logins, contacts, irc_server, irc_port, owner_nick, irc_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.log_file = log_file
     self.irc_nick = irc_nick
     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_m = WAMutexInterface(wa_logins, self.wa_msg_received, self.stop, self.wa_msg_ignored, self.stop)
Exemple #2
0
class Bot(threading.Thread):
    def __init__(self, wa_logins, contacts, irc_server, irc_port, owner_nick, irc_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.log_file = log_file
        self.irc_nick = irc_nick
        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_m = WAMutexInterface(wa_logins, self.wa_msg_received, self.stop, self.wa_msg_ignored, 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 clients")
            self.wa_m.start()
            self.wa_m.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_m.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_m.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_m.send(group, msg)
            except Exception, e:
                error("Cannot send message to channel %s: %s" %(message.chan, e))