def _handleINVITE(self, nick, ident, host, params): if nick in self.bot.users: inviter = self.bot.users[nick] else: inviter = IRCUser(nick, ident, host) invitee = None if 'invite-notify' in self.bot.capabilities['finished'] and len( params) > 1: if params[0] not in self.bot.users: invitee = IRCUser(params[0]) else: invitee = self.bot.users[nick] chanName = params[1] else: chanName = params[0] if chanName not in self.bot.channels: channel = IRCChannel(params[0], self.bot) else: channel = self.bot.channels[chanName] if not invitee or invitee.nick == self.bot.nick: self.bot.output.cmdJOIN(chanName) message = IRCMessage('INVITE', inviter, channel, '', self.bot, {'invitee': invitee}) self.handleMessage(message)
def _handleNOTICE(self, nick, ident, host, params): user = None if params[0][0] in self.connection.supportHelper.chanTypes: if params[0] in self.connection.channels: source = self.connection.channels[params[0]] else: # We got a notice for an unknown channel. Create a temporary IRCChannel object for it. source = IRCChannel(params[0], self) if nick in self.connection.users: user = self.connection.users[nick] else: user = IRCUser(nick, ident, host) elif nick in self.connection.users: source = self.connection.users[nick] else: # We got a notice from an unknown user. Create a temporary IRCUser object for them. source = IRCUser(nick, ident, host) if isinstance(source, IRCChannel): self.moduleHandler.runGenericAction("notice-channel", self.connection.name, source, user, params[1]) else: self.moduleHandler.runGenericAction("notice-user", self.connection.name, source, params[1])
def _handlePRIVMSG(self, nick, ident, host, params): user = None if params[0][0] in self.connection.supportHelper.chanTypes: if params[0] in self.connection.channels: source = self.connection.channels[params[0]] else: # We got a message for an unknown channel. Create a temporary IRCChannel object for it. source = IRCChannel(params[0], self.connection) if nick in self.connection.users: user = self.connection.users[nick] else: user = IRCUser(nick, ident, host) elif nick in self.connection.users: source = self.connection.users[nick] user = source else: # We got a message from an unknown user. Create a temporary IRCUser object for them. source = IRCUser(nick, ident, host) if params[1][0] == "\x01": message = params[1][1:len(params[1]) - 1] self.moduleHandler.runGenericAction("ctcp-message", self.connection.name, source, user, message) elif isinstance(source, IRCChannel): self.moduleHandler.runGenericAction("message-channel", self.connection.name, source, user, params[1]) else: self.moduleHandler.runGenericAction("message-user", self.connection.name, source, params[1])
def irc_RPL_WHOREPLY(self, prefix, params): channel = self.getChannel(params[1]) user = channel.users[params[5]] if not user: user = IRCUser("{}!{}@{}".format(params[5], params[2], params[3])) else: user.username = params[2] user.hostname = params[3] user.server = params[4] # The RFC is weird and puts hops and realname in the same parameter hopsRealnameParam = params[7].split(" ") user.hops = int(hopsRealnameParam[0]) user.realname = hopsRealnameParam[1] flags = params[6] statusFlags = None if flags[0] == "G": user.away = True if len(flags) > 1: if flags[1] == "*": user.oper = True statusFlags = flags[2:] else: statusFlags = flags[1:] statusModes = "" if statusFlags: for flag in statusFlags: statusModes = statusModes + self.serverInfo.prefixesCharToMode[flag] channel.ranks[user.nickname] = statusModes
def _handleTOPIC(self, nick, ident, host, params): if params[0] not in self.bot.channels: self.bot.logger.warning(f'Received TOPIC message for unknown channel {params[0]}.') return channel = self.bot.channels[params[0]] if nick not in self.bot.users: # A user that's not in the channel can change the topic so let's make a temporary user. user = IRCUser(nick, ident, host) else: user = self.bot.users[nick] oldTopic = channel.topic channel.topic = params[1] channel.topicSetter = user.fullUserPrefix() channel.topicTimestamp = timestamp(now()) message = IRCMessage('TOPIC', user, channel, params[1], self.bot, {'oldtopic': oldTopic}) self.handleMessage(message)
def _handleMODE(self, nick, ident, host, params): message = None if nick in self.bot.users: user = self.bot.users[nick] else: user = IRCUser(nick, ident, host) if len(params) > 2: modeParams = params[2:] else: modeParams = [] if params[0][0] in self.bot.supportHelper.chanTypes: if params[0] not in self.bot.channels: self.bot.logger.warning( f'Received MODE message for unknown channel {params[0]}.') return channel = self.bot.channels[params[0]] modes = channel.setModes(params[1], modeParams) if not modes: return if len(modes['added']) > 0 or len(modes['removed']) > 0: message = IRCMessage('MODE', user, channel, '', self.bot, modes) elif params[0] == self.bot.nick: modes = self.bot.setUserModes(params[1]) if not modes: return if len(modes['added']) > 0 or len(modes['removed']) > 0: message = IRCMessage('MODE', user, None, '', self.bot, modes) if message: self.handleMessage(message)
def _handleKICK(self, nick, ident, host, params): if params[0] not in self.bot.channels: self.bot.logger.warning( f'Received KICK message for unknown channel {params[0]}.') return channel = self.bot.channels[params[0]] if params[1] not in channel.users: self.bot.logger.warning( f'Received KICK message for unknown user {nick} in channel {params[0]}.' ) return if nick not in self.bot.users: # Technically opers could KICK a user despite not being in the channel themselves kicker = IRCUser(nick, ident, host) else: kicker = self.bot.users[nick] kicked = self.bot.users[params[1]] reason = '' if len(params) > 2: reason = params[2] message = IRCMessage('KICK', kicker, channel, reason, self.bot, {'kicked': kicked}) self.handleMessage(message) # We need to run the action before we actually get rid of the user if kicked.nick == self.bot.nick: del self.bot.channels[params[0]] else: del channel.users[kicked.nick] del channel.ranks[kicked.nick]
def _handleKICK(self, nick, ident, host, params): if params[0] not in self.connection.channels: self._logWarning( "Received KICK message for unknown channel {}.".format( params[0])) return channel = self.connection.channels[params[0]] if params[1] not in channel.users: self._logWarning( "Received KICK message for unknown user {} in channel {}.". format(nick, params[0])) return if nick not in self.connection.users: # Technically opers could KICK a user despite not being in the channel themselves kicker = IRCUser(nick, ident, host) else: kicker = self.connection.users[nick] kicked = self.connection.users[params[1]] reason = "" if len(params) > 2: reason = params[2] # We need to run the action before we actually get rid of the user self.moduleHandler.runGenericAction("channelkick", self.connection.name, channel, kicker, kicked, reason) if kicked.nick == self.connection.nick: del self.connection.channels[params[0]] else: del channel.users[kicked.nick] del channel.ranks[kicked.nick]
def _handleINVITE(self, nick, ident, host, params): if nick in self.connection.users: inviter = self.connection.users[nick] else: inviter = IRCUser(nick, ident, host) self.moduleHandler.runGenericAction("channelinvite", params[1], inviter) self.connection.outputHandler.cmdJOIN(params[1])
def _handleNumeric324(self, prefix, params): # 324: RPL_CHANNELMODEIS channel = self.bot.channels[params[1]] modeParams = params[3].split() if len(params) > 3 else [] modes = channel.setModes(params[2], modeParams) if modes: message = IRCMessage('324', IRCUser(prefix), None, '', self.bot, modes) self.handleMessage(message)
def _handleNumeric001(self, prefix, params): # 001: RPL_WELCOME self.bot.loggedIn = True self.bot.factory.connectionAttempts = 0 message = IRCMessage('001', IRCUser(prefix), None, '', self.bot) self.handleMessage(message) channels = self.bot.config.getWithDefault('channels', {}) for channel, key in channels.items(): self.bot.output.cmdJOIN(channel, key if key else '')
def _handleTOPIC(self, nick, ident, host, params): if params[0] not in self.bot.channels: self.bot.logger.warning( f'Received TOPIC message for unknown channel {params[0]}.') return channel = self.bot.channels[params[0]] if nick not in self.bot.users: # A user that's not in the channel can change the topic so let's make a temporary user. user = IRCUser(nick, ident, host) else: user = self.bot.users[nick] oldTopic = channel.topic channel.topic = params[1] channel.topicSetter = user.fullUserPrefix() channel.topicTimestamp = timestamp(now()) message = IRCMessage('TOPIC', user, channel, params[1], self.bot, {'oldtopic': oldTopic}) self.handleMessage(message)
def _handleTOPIC(self, nick, ident, host, params): if params[0] not in self.connection.channels: self._logWarning( "Received TOPIC message for unknown channel {}.".format( params[0])) return channel = self.connection.channels[params[0]] if nick not in self.connection.users: # A user that's not in the channel can change the topic so let's make a temporary user. user = IRCUser(nick, ident, host) else: user = self.connection.users[nick] oldTopic = channel.topic channel.topic = params[1] channel.topicSetter = user.fullUserPrefix() channel.topicTimestamp = timeutils.timestamp(timeutils.now()) self.moduleHandler.runGenericAction("changetopic", self.connection.name, channel, user, oldTopic, params[1])
def activate(self): commandStr = '{}{} {}'.format(self.bot.commandChar, self.command, ' '.join(self.params)) self.logger.info("Activated {!r}".format(commandStr)) message = IRCMessage('PRIVMSG', IRCUser(self.user), IRCChannel(self.channel, self.bot), commandStr, self.bot) trigger = self.bot.moduleHandler.mappedTriggers[self.command].execute return trigger(message)
def _handleNOTICE(self, nick, ident, host, params): user = None if params[0][0] in self.bot.supportHelper.chanTypes: if params[0] in self.bot.channels: source = self.bot.channels[params[0]] else: # We got a notice for an unknown channel. Create a temporary IRCChannel object for it. source = IRCChannel(params[0], self.bot) if nick in self.bot.users: user = self.bot.users[nick] else: user = IRCUser(nick, ident, host) elif nick in self.bot.users: source = self.bot.users[nick] else: # We got a notice from an unknown user. Create a temporary IRCUser object for them. source = IRCUser(nick, ident, host) if isinstance(source, IRCChannel): message = IRCMessage('NOTICE', user, source, params[1], self.bot) else: message = IRCMessage('NOTICE', source, None, params[1], self.bot) self.handleMessage(message)
def _handleJOIN(self, nick, ident, host, params): if nick not in self.bot.users: user = IRCUser(nick, ident, host) if 'extended-join' in self.bot.capabilities['finished'] and len(params) > 1: if params[1] != '*': user.account = params[1] user.gecos = params[2] self.bot.users[nick] = user else: user = self.bot.users[nick] if params[0] not in self.bot.channels: channel = IRCChannel(params[0], self.bot) self.bot.output.cmdWHO(params[0]) self.bot.output.cmdMODE(params[0]) self.bot.channels[params[0]] = channel else: channel = self.bot.channels[params[0]] channel.users[nick] = user channel.ranks[nick] = '' message = IRCMessage('JOIN', user, channel, '', self.bot) self.handleMessage(message)
def _handleJOIN(self, nick, ident, host, params): if nick not in self.bot.users: user = IRCUser(nick, ident, host) if 'extended-join' in self.bot.capabilities['finished'] and len( params) > 1: if params[1] != '*': user.account = params[1] user.gecos = params[2] self.bot.users[nick] = user else: user = self.bot.users[nick] if params[0] not in self.bot.channels: channel = IRCChannel(params[0], self.bot) self.bot.output.cmdWHO(params[0]) self.bot.output.cmdMODE(params[0]) self.bot.channels[params[0]] = channel else: channel = self.bot.channels[params[0]] channel.users[nick] = user channel.ranks[nick] = '' message = IRCMessage('JOIN', user, channel, '', self.bot) self.handleMessage(message)
def _handlePRIVMSG(self, nick, ident, host, params): user = None if params[0][0] in self.bot.supportHelper.chanTypes: if params[0] in self.bot.channels: source = self.bot.channels[params[0]] else: # We got a message for an unknown channel. Create a temporary IRCChannel object for it. source = IRCChannel(params[0], self.bot) if nick in self.bot.users: user = self.bot.users[nick] else: user = IRCUser(nick, ident, host) elif nick in self.bot.users: source = self.bot.users[nick] user = source else: # We got a message from an unknown user. Create a temporary IRCUser object for them. source = IRCUser(nick, ident, host) if len(params) == 1: self.bot.logger.warning('Received a blank PRIVMSG') params.append('') if params[1] and params[1][0] == '\x01': msgType = 'CTCP' msgStr = params[1][1:len(params[1]) - 1] if msgStr.upper().startswith('ACTION'): msgType = 'ACTION' msgStr = msgStr[7:] if isinstance(source, IRCChannel): message = IRCMessage(msgType, user, source, msgStr, self.bot) else: message = IRCMessage(msgType, source, None, msgStr, self.bot) elif isinstance(source, IRCChannel): message = IRCMessage('PRIVMSG', user, source, params[1], self.bot) else: message = IRCMessage('PRIVMSG', source, None, params[1], self.bot) self.handleMessage(message)
def _handleJOIN(self, nick, ident, host, params): if nick not in self.connection.users: user = IRCUser(nick, ident, host) self.connection.users[nick] = user else: user = self.connection.users[nick] if params[0] not in self.connection.channels: channel = IRCChannel(params[0], self.connection) self.connection.outputHandler.cmdWHO(params[0]) self.connection.outputHandler.cmdMODE(params[0]) self.connection.channels[params[0]] = channel else: channel = self.connection.channels[params[0]] channel.users[nick] = user channel.ranks[nick] = "" self.moduleHandler.runGenericAction("channeljoin", self.connection.name, channel, user)
def logBotMessage(self, server, source, body): if source[0] not in self.bot.servers[server].supportHelper.chanTypes: return if source not in self.bot.servers[server].channels: source = IRCChannel(source, self.bot.servers[server]) else: source = self.bot.servers[server].channels[source] if self.bot.servers[server].nick not in self.bot.servers[server].users: user = IRCUser(self.bot.servers[server].nick, None, None) else: user = self.bot.servers[server].users[ self.bot.servers[server].nick] if body.startswith(":\x01ACTION"): message = "* {} {}".format(user.nick, body[9:len(body) - 1]) else: message = "<{}{}> {}".format(source.getHighestStatusOfUser(user), user.nick, body[1:]) self._writeLog(server, source.name, message)
def _handleMODE(self, nick, ident, host, params): if nick in self.connection.users: user = self.connection.users[nick] else: user = IRCUser(nick, ident, host) if len(params) > 2: modeParams = params[2:] else: modeParams = [] if params[0][0] in self.connection.supportHelper.chanTypes: if params[0] not in self.connection.channels: self._logWarning( "Received MODE message for unknown channel {}.".format( params[0])) return channel = self.connection.channels[params[0]] modes = channel.setModes(params[1], modeParams) if not modes: return if len(modes["added"]) > 0: self.moduleHandler.runGenericAction("modeschanged-channel", self.connection.name, user, channel, modes["added"], modes["addedParams"], True) if len(modes["removed"]) > 0: self.moduleHandler.runGenericAction("modeschanged-channel", self.connection.name, user, channel, modes["removed"], modes["removedParams"], False) elif params[0] == self.connection.nick: modes = self.connection.setUserModes(params[1]) if not modes: return if len(modes["added"]) > 0: self.moduleHandler.runGenericAction("modeschanged-user", self.connection.name, modes["added"], True) if len(modes["removed"]) > 0: self.moduleHandler.runGenericAction("modeschanged-user", self.connection.name, modes["removed"], False)
def _handleNumeric353(self, prefix, params): # 353: RPL_NAMREPLY channel = self.connection.channels[params[2]] if channel.userlistComplete: channel.userlistComplete = False channel.users.clear() channel.ranks.clear() for userPrefix in params[3].split(): parsedPrefix = parseUserPrefix(userPrefix) nick = parsedPrefix[0] ranks = "" while nick[0] in self.connection.supportHelper.statusSymbols: ranks += self.connection.supportHelper.statusSymbols[nick[0]] nick = nick[1:] if nick in self.connection.users: user = self.connection.users[nick] user.ident = parsedPrefix[1] user.host = parsedPrefix[2] else: user = IRCUser(nick, parsedPrefix[1], parsedPrefix[2]) self.connection.users[nick] = user channel.users[nick] = user channel.ranks[nick] = ranks