Ejemplo n.º 1
0
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(" <<< 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(traceback.print_exc())
                error("Cannot send message to channel %s: %s" %
                      (message.chan, e))
Ejemplo n.º 2
0
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))