def number(self, server, channel, nick, params): """Usage: number NUMBER - translates arabic numerals to english text.""" if not check_available('number'): return 'The number command is not installed on my host.' if len(params) < 1: return clean_string(self.number.__doc__) try: return get_command_output('number -l ' + str(int(params[0]))) except ValueError: return 'This is not a real number.'
def join(self, server, channel, nick, params): """ Usage: join #CHANNEL [PASSWORD] - the bot will enter the specified channel. A password may be provided optionally, if it is required. """ if len(params) < 1: return clean_string(self.join.__doc__) password = '' if len(params) > 1: password = params[0] self.bot.client.send('JOIN', params[0], password)
def classified_memo(self, server, channel, nick, params): """ Usage: classified_memo RECIPIENT MESSAGE - delivers MESSAGE to RECIPIENT as soon as they join a channel where I am present, or show any other activity. The message is delivered via query. """ if len(params) < 2: return clean_string(self.classified_memo.__doc__) self._add_message(params[0], nick.split('!')[0], ' '.join(params[1:]), True) return 'Your classified memo will be delifered ASAP.'
def memo(self, server, channel, nick, params): """ Usage: memo RECIPIENT MESSAGE - delivers MESSAGE to RECIPIENT as soon as they join a channel where I am present, or they show any other activity. For confidential delivery, use the classified_memo command. """ if len(params) < 2: return clean_string(self.memo.__doc__) self._add_message(params[0], nick.split('!')[0], ' '.join(params[1:]), False) return 'Your memo will be delivered ASAP.'
def help(self, server, channel, nick, params): """ Usage: help PLUGIN [COMMAND] - prints a help message. If only PLUGIN is specified, you get general plugin information and a list of available commands. Otherwise, the help for the specific COMMAND of the PLUGIN is provided. If PLUGIN can't be found, I will look for a command with that name. """ if len(params) < 1: return clean_string(self.help.__doc__) if len(params) < 2: if params[0] in self.bot.plugins: # Plugin found help_msg = clean_string(self.bot.plugins[params[0]].__doc__ or \ 'Sorry, no help message available.') commands = sorted(list(name \ for name, member \ in inspect.getmembers(self.bot.plugins[params[0]]) if hasattr(member, '__annotations__') \ and 'command' in member.__annotations__)) if len(commands) > 0: help_msg += ' Commands: %s' % pretty_list(commands) return clean_string(help_msg) elif params[0] in self.bot.commands: # Command found return clean_string(getattr( self.bot.commands[params[0]], params[0]).__doc__ or \ 'Sorry, no help message available.') else: return 'Plugin or command "%s" not found.' % params[0] # Only Plugin->Command left now. Try to find it... if params[1] in self.bot.commands and \ self.bot.commands[params[1]].__class__.__name__.lower() \ == params[0]: return clean_string(getattr( self.bot.plugins[params[0]], params[1]).__doc__ or \ 'Sorry, no help message available.') # If everything fails: return 'Command "%s" from plugin "%s" not found.' % (params[1], params[0])
def seen(self, server, channel, nick, params): """ Usage: seen NICK - Shows how long ago the given nick was seen for the last time, and what they were doing then. """ if len(params) < 1: return clean_string(self.seen.__doc__) subject = params[0] if not subject in self.memory: return 'I have not seen %s before.' % subject entry = self.memory[subject] return '%s was last seen %s ago in %s, %s.' % (subject, humanize_time(datetime.datetime.now() - entry[0]), entry[1], entry[2])
def morse(self, server, channel, nick, params): """ Usage: morse decode|encode TEXT - translates a given TEXT to and from morse code. """ if not check_available('morse'): return 'The morse command is not installed on my host.' if len(params) < 2 or not params[0] in ('decode', 'encode'): return clean_string(self.morse.__doc__) text = ' '.join(params[1:]) # Check for illegal characters to avoid shell access. if not self._morse_pattern.match(text): return 'Illegal characters in input text.' if params[0] == 'decode': return get_command_output('morse -d -- ' + text) if params[0] == 'encode': return get_command_output('morse -s -- ' + text)
def caesar(self, server, channel, nick, params): """ Usage: caesar ROTATION MESSAGE - encrypts a MESSAGE using the caesar cipher. ROTATION is the number by which the letters in MESSAGE are shifted. """ if not check_available('caesar'): return 'The caesar command is not installed on my host.' if len(params) < 2: return clean_string(self.caesar.__doc__) rotation = 0 try: rotation = str(int(params[0])) except ValueError: return 'Invalid rotation. Not a real number.' message = ' '.join(params[1:]) if not self._morse_pattern.match(message): return 'Illegal characters in input text.' return get_command_output('echo ' + message + ' | caesar ' + rotation)
def nick(self, server, channel, nick, params): """Usage: nick NEW_NICKNAME - changes the bot's nickname.""" if len(params) < 1: return clean_string(self.nick.__doc__) self.bot.client.send('NICK', params[0])
def get_command_output(command): """Returns the output of a console command. Discards STDERR.""" process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) stdout_value, stderr_value = process.communicate() return clean_string(stdout_value.decode('utf-8'))