Ejemplo n.º 1
0
    def __init__(self, channel, nickname, server, port=6667, username=None, password=None, **connect_params):
        # SimpleIRCClient init
        self.ircobj = RateLimitedIRC()
        self.connection = self.ircobj.server()
        self.dcc_connections = []
        self.ircobj.add_global_handler("all_events", self._dispatcher, -10)
        self.ircobj.add_global_handler("dcc_disconnect", self._dcc_disconnect, -10)

        # SingleServerIRCBot init
        self._SingleServerIRCBot__connect_params = connect_params
        self.channels = IRCDict()
        self.server_list = [irc.bot.ServerSpec(server, port)]
        self.reconnection_interval = 30
        self.pinged_at = datetime.datetime.utcnow()

        self._nickname = nickname
        self._realname = nickname
        for i in ["disconnect", "join", "kick", "mode", "namreply", "nick", "part", "quit"]:
            self.connection.add_global_handler(i, getattr(self, "_on_" + i), -20)
        self.connection.add_global_handler("ping", self.pinged, -41)
        self.connection.add_global_handler("pong", self.pinged, -41)

        self.connection.add_global_handler("join", self.joined, -10)

        self.username = username
        self.password = password
        self.channel = channel
        self.actions = {}

        self.activity = Event()
        self.part = Event()
        self.death = Event()
        self.join = Event()
Ejemplo n.º 2
0
class Bot(irc.bot.SingleServerIRCBot):
    def __init__(self, channel, nickname, server, port=6667, username=None, password=None, **connect_params):
        # SimpleIRCClient init
        self.ircobj = RateLimitedIRC()
        self.connection = self.ircobj.server()
        self.dcc_connections = []
        self.ircobj.add_global_handler("all_events", self._dispatcher, -10)
        self.ircobj.add_global_handler("dcc_disconnect", self._dcc_disconnect, -10)

        # SingleServerIRCBot init
        self._SingleServerIRCBot__connect_params = connect_params
        self.channels = IRCDict()
        self.server_list = [irc.bot.ServerSpec(server, port)]
        self.reconnection_interval = 30
        self.pinged_at = datetime.datetime.utcnow()

        self._nickname = nickname
        self._realname = nickname
        for i in ["disconnect", "join", "kick", "mode", "namreply", "nick", "part", "quit"]:
            self.connection.add_global_handler(i, getattr(self, "_on_" + i), -20)
        self.connection.add_global_handler("ping", self.pinged, -41)
        self.connection.add_global_handler("pong", self.pinged, -41)

        self.connection.add_global_handler("join", self.joined, -10)

        self.username = username
        self.password = password
        self.channel = channel
        self.actions = {}

        self.activity = Event()
        self.part = Event()
        self.death = Event()
        self.join = Event()

    def pinged(self, connection, event):
        self.pinged_at = datetime.datetime.utcnow()

    def joined(self, connection, event):
        self.join(connection, event)

    def ping_check(self):
        logger.info("Checking for recent pings: last ping was at %s", self.pinged_at)
        if self.connection.is_connected():
            self.connection.ping(self._nickname)
            if datetime.datetime.utcnow() - self.pinged_at > datetime.timedelta(seconds=120):
                self.disconnect()
        self.connection.execute_delayed(60, self.ping_check)

    def on_nicknameinuse(self, connection, event):
        logger.info("Nickname in use.")
        connection.nick(connection.get_nickname() + "_")

    def on_welcome(self, connection, event):
        logger.info("Received welcome.")
        if self.username and self.password:
            self.authenticate()
        connection.mode(connection.get_nickname(), "+x")
        time.sleep(5)
        logger.info("Joining channel.")
        connection.join(self.channel)

        self.ping_check()

    def on_privmsg(self, connection, event):
        logger.info("PRIVMSG {0}".format(event.arguments[0]))
        self.process_command(event, event.source.nick)

    def on_pubmsg(self, connection, event):
        self.process_command(event, self.channel)
        self.activity(event.source.nick)

    def _on_part(self, connection, event):
        irc.bot.SingleServerIRCBot._on_part(self, connection, event)
        self.part(event.source.nick)

    def _on_quit(self, connection, event):
        irc.bot.SingleServerIRCBot._on_quit(self, connection, event)
        self.part(event.source.nick)

    def _on_disconnect(self, connection, event):
        logger.info("Disconnected from server - will try and reconnect in %s seconds", self.reconnection_interval)
        irc.bot.SingleServerIRCBot._on_disconnect(self, connection, event)

    def authenticate(self):
        logger.info("Authenticating with Q as {0}.".format(self.username))
        self.connection.privmsg("*****@*****.**", "AUTH {0} {1}".format(self.username, self.password))

    def process_command(self, event, target):
        request = event.arguments[0].strip()

        for regex, fn in self.actions.values():
            match = re.match(regex, request)
            if match:
                try:
                    fn(self, event, *match.groups())
                except Exception, e:
                    logger.exception(e)
                    self.say("Something went wrong with that command.")
                break