def irc_RPL_ENDOFWHO(self, *nargs): '''End of WHO reply ''' # Extract returned information if self.who_reply[0] is not None: nick = self.who_reply[-1][5] username = self.who_reply[-1][2] host = self.who_reply[-1][3] full = u'{} ({}@{})'.format(nick, username, host) reply = u'{} is online'.format(full) else: full = None reply = u'{} is not online'.format(self.who_reply[-1]) # Check if the nick is in the tell dict if hasattr(self, '_telld') and self._tell_check is not None: if full is not None and nick in self._telld: # Don't send if the user is currently online src, _ = self._telld[nick] reply = u'{} is online. Send a PM youself, silly ass.'.\ format(nick) del self._telld[nick] # Save the telld to disk self.fact.commands['tell'].saveTellDict(self) else: src, dst = self._tell_check reply = u'Message queued for {}'.format(dst) self._tell_check = None self.msg(src, encode(reply)) return # Otherwise, respond to the online command self.msg(self.channel if not self.pm else self.sender, encode(reply))
def privmsg(self, user, channel, msg): # Save needed information self.sender = user.split('!', 1)[0] self.channel = channel self.pm = channel == self.nickname self.error = False msg = decode(msg) cmnd = self.getCommand(msg) if cmnd: try: response = self.eval(self.parse(cmnd)) if response: # Log each command responded to if self.logging: log.msg('[{}] <{}> {}'.format(channel, user, encode(msg))) return self.moreSend(\ self.sender if self.pm else channel, response) except pb.CommandError, ce: return self.msg(self.sender if ce.pm else channel, encode(u'{}'.format(se))) except SyntaxError, se: return self.msg(self.sender if self.pm else channel, encode(u'{}'.format(se)))
def privmsg(self, user, channel, msg): # Save needed information self.sender = user.split('!', 1)[0] self.channel = channel self.pm = channel == self.nickname self.error = False msg = decode(msg) # Then, check for the presence of a command cmnd = self.getCommand(msg) if cmnd: try: response = self.eval(self.parse(cmnd)) if response: self.moreSend(self.sender if self.pm else channel, response) except pb.CommandError, ce: self.msg(self.sender if ce.pm else channel, encode(u'{}'.format(ce))) except SyntaxError, se: self.msg(self.sender if self.pm else channel, encode(u'{}'.format(se)))
def more(self, args, irc): if not hasattr(irc, '_mored'): # For sending long lines of text # Maps a nickname to the text # left to send irc._mored = {} if not irc._mored.get(irc.sender): return lines = irc._mored[irc.sender] line = lines.pop(0) suffix = u' \x02({} more messages)\x02'.\ format(len(lines)) if lines \ else u'' log.msg('more: {}'.format(line)) irc.msg(irc.sender if irc.pm else irc.channel, encode(u'{}{}'.format(line, suffix)))
def eval(self, expr): '''Evaluate an expression, which might contain nested expressions. ''' # Evaluate each inner list for i in range(len(expr) - 1, -1, -1): if type(expr[i]) == list: expr[i] = self.eval(expr[i]) self.cmnd, rest = expr[0].lstrip(self.fact.prefix), expr[1:] rest = [word for word in rest if word and not word.isspace()] if self.cmnd in self.fact.commands: response = self.fact.commands[self.cmnd](rest, self) if response: return encode(response) else: return u'' else: return u''
def moreSend(self, to, msg): '''Sends a maximum amount of text at a time and stores the rest which can be sent with the more plugin. ''' # Get the msg length to split up the text into lines lines = textwrap.wrap(msg, self.fact.max_line_len) lines = lines[:self.fact.max_more_lines] # Save it in the dict for the more plugin self._mored[self.sender] = lines # Get the next line to send line = lines.pop(0) # What to put at the end of the line suffix = u' \x02({} more messages)\x02'.format(len(lines)) if lines \ else u'' # Send the message with the suffix if type(line) == str: line = unicode(line, "utf-8", errors="ignore") self.msg(to, encode(u'{}{}'.format(line, suffix)))
def noticed(self, user, channel, message): '''Called when a NOTICE message is sent. ''' if self.logging: log.msg('[{}] <{}> {}'.format(channel, user, encode(message)))
if self.logging: log.msg('[{}] <{}> {}'.format(channel, user, encode(msg))) return self.moreSend(\ self.sender if self.pm else channel, response) except pb.CommandError, ce: return self.msg(self.sender if ce.pm else channel, encode(u'{}'.format(se))) except SyntaxError, se: return self.msg(self.sender if self.pm else channel, encode(u'{}'.format(se))) # Check the non-command plugins for name in self.fact.line_plugins: if self.fact.plugins[name].hasResponse(msg, self): self.msg(self.sender if self.pm else channel, encode(self.response)) def readFrom(self, tokens, depth=0): '''Read, and return, an expression, i.e., a list of unicode objects and/or other expressions, from a sequence of tokens. Taken from http://norvig.com/lis.py ''' if not tokens: raise SyntaxError(self.eol_error) token = tokens.pop(0) if self.fact.nested_prefix == token: if depth == self.fact.max_nesting + 1: raise SyntaxError(self.fact.max_nesting_error.\
log.msg('[{}] <{}> {}'.format(channel, user, encode(msg))) return self.moreSend(\ self.sender if self.pm else channel, response) except pb.CommandError, ce: return self.msg(self.sender if ce.pm else channel, encode(u'{}'.format(se))) except SyntaxError, se: return self.msg(self.sender if self.pm else channel, encode(u'{}'.format(se))) # Check the non-command plugins for name in self.fact.line_plugins: if self.fact.plugins[name].hasResponse(msg, self): self.msg(self.sender if self.pm else channel, encode(self.response)) def readFrom(self, tokens, depth=0): '''Read, and return, an expression, i.e., a list of unicode objects and/or other expressions, from a sequence of tokens. Taken from http://norvig.com/lis.py ''' if not tokens: raise SyntaxError(self.eol_error) token = tokens.pop(0) if self.fact.nested_prefix == token: if depth == self.fact.max_nesting + 1: raise SyntaxError(self.fact.max_nesting_error.\
cmnd = self.getCommand(msg) if cmnd: try: response = self.eval(self.parse(cmnd)) if response: self.moreSend(self.sender if self.pm else channel, response) except pb.CommandError, ce: self.msg(self.sender if ce.pm else channel, encode(u'{}'.format(ce))) except SyntaxError, se: self.msg(self.sender if self.pm else channel, encode(u'{}'.format(se))) finally: # Log each command responded to if self.logging: log.msg('[{}] <{}> {}'.format(channel, user, encode(msg))) # Check the non-command plugins for name in self.fact.line_plugins: if self.fact.plugins[name].hasResponse(msg, self): self.msg(self.sender if self.pm else channel, encode(self.response)) def readFrom(self, tokens, depth=0): '''Read, and return, an expression, i.e., a list of unicode objects and/or other expressions, from a sequence of tokens. Taken from http://norvig.com/lis.py ''' if not tokens: