def get_package(self, command, data): """Return the package version and url, or alternatives if not found""" args = " ".join(command.split(":")[1:]).strip() # Allowed chars from http://legacy.python.org/dev/peps/pep-0426/#name allowed_chars = string.ascii_letters + string.digits + "_-." for char in args: if char not in allowed_chars: reply = 'Invalid name: Cannot contain "{}"'.format(char) return reply_to_user(data, reply) response = self.xml_rpc.search({"name": args}) alts = [] for item in response: if item["name"].lower() == args.lower(): wanted_data = item break elif args.lower() in item["name"].lower(): alts.append(item["name"]) else: if alts: reply = "Package {} not found. Alternatives: {}".format(args, " ".join(alts[:10])) return reply_to_user(data, reply) else: return reply_to_user(data, "Package {} not found".format(args)) response = self.xml_rpc.release_data(wanted_data["name"], wanted_data["version"]) reply = "{} {}: {} {}".format(wanted_data["name"], wanted_data["version"], response["summary"], response["home_page"]) return reply_to_user(data, reply)
def cmd_whereis(self, command, data): splitcmd = [a.strip() for a in command.split(':')] who = splitcmd[1] reply = [] try: loc_data = shelve.open(self.loc_file) if who not in loc_data: replystr = "No idea!" elif loc_data[who][-1]['where'].lower() in NOLOC: replystr = "Who knows?" else: replyfmtstr = "%(who)s %(atstr)s %(where)s (%(whenstr)s)" replydict = {} replydict['who'] = ('you' if who == data['source_user'] else who) replydict.update(loc_data[who][-1]) replydict['whenstr'] = prettier_date(replydict['when']) replystr = replyfmtstr % replydict except: reply = ['(shh.. someone made me do something wrong!)'] finally: reply.append(replystr) loc_data.close() return reply_to_user(data, reply)
def cmd_whereis(self, command, data): splitcmd = [a.strip() for a in command.split(':')] who = splitcmd[1] reply = [] if who == data['source_user']: reply.append("Don't you know where you are?") try: loc_data = shelve.open(self.loc_file) if who not in loc_data: replystr = "No idea!" elif loc_data[who][-1]['where'].lower() in NOLOC: replystr = "Who knows?" else: replyfmtstr = "%(who)s %(atstr)s %(where)s (%(whenstr)s)" replydict = {'who': who} replydict.update(loc_data[who][-1]) replydict['whenstr'] = prettier_date(replydict['when']) replystr = replyfmtstr % replydict except: reply = ['(shh.. someone made me do something wrong!)'] finally: reply.append(replystr) loc_data.close() return reply_to_user(data, reply)
def cmd_dblist(self, command, data): splitcmd = [a.strip() for a in command.split(':')] reply = [] if 'trans' in splitcmd: reply.append(str(self.translate_dbs.keys)) elif 'other' in splitcmd or 'normal' in splitcmd: reply.append(str(self.other_dbs)) else: reply.append(str(self.other_dbs + self.translate_dbs)) return reply_to_user(data, reply)
def cmd_eval(self, command, data): splitcmd = [a.strip() for a in command.split(':')] command = splitcmd[0] code = splitcmd[1] proc = Popen( [self.bin], stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False ) try: outs, errs = proc.communicate(input=code.encode('UTF-8')) except OSError as e: proc.kill() return reply_to_user(data, 'Subprocess error', e) if errs: return reply_to_user(data, self.truncate_output(errs, 200, 1)) else: return reply_to_user(data, self.truncate_output(outs, 200, 5))
def cmd_help_cmd(self, command, data): splitcmd = [a.strip() for a in command.split(':')] _, args = splitcmd[0], splitcmd[1:] cmds = self._get_cmd_dict() if len(args) == 1 and args[0] in cmds: cmd = cmds[args[0]] reply = cmd.get('help', cmd.get('description', None)) if not reply: reply = "No help found for %s" % cmd else: reply = self.cmds['help']['usage'] % {'nick': data['conn']._nickname} return reply_to_user(data, reply)
def cmd_date(self, command, data): lcmd = command.lower() dt = datetime.now() fmt = self.formats.get(lcmd, {}).get('format') if fmt: output = dt.strftime(fmt) elif lcmd == 'isotime': output = dt.time().isoformat() elif lcmd == 'isodate': output = dt.date().isoformat() elif lcmd == 'isodatetime': output = dt.isoformat() return reply_to_user(data, output)
def cmd_help_cmd(self, command, data): splitcmd = [a.strip() for a in command.split(':')] _, args = splitcmd[0], splitcmd[1:] cmds = self._get_cmd_dict() if len(args) == 1 and args[0] in cmds: cmd = cmds[args[0]] reply = cmd.get('help', cmd.get('description', None)) if not reply: reply = "No help found for %s" % cmd else: reply = self.cmds['help']['usage'] % { 'nick': data['conn']._nickname } return reply_to_user(data, reply)
def cmd_date(self, command, data): splitcmd = command.split(':') lcmd, argstr = splitcmd[0].lower(), ':'.join(splitcmd[1:]) dt = datetime.now() fmt = self.formats.get(lcmd, {}).get('format') if fmt: argoutput = dt.strftime(argstr) if argstr else '' output = (dt.strftime(fmt) if argoutput.strip() == argstr.strip() else argoutput) elif lcmd == 'isotime': output = dt.time().isoformat() elif lcmd == 'isodate': output = dt.date().isoformat() elif lcmd == 'isodatetime': output = dt.isoformat() return reply_to_user(data, output)
def cmd_definition(self, command, data): splitcmd = [a.strip() for a in command.split(':')] command = splitcmd[0] defterm = splitcmd[1] reply = [] if command in self.dbs: db = command server = self.dbs[command].get('server', None) else: db = '*' server = normal_server if server is not None: c = dictclient.Connection(server) definitions = c.define(db, defterm) if not definitions: reply.append('No definitions found in ' + command) reply.extend( self.process_definition(d) for d in definitions[:MAX_DEFS]) return reply_to_user(data, reply)
def cmd_definition(self, command, data): splitcmd = [a.strip() for a in command.split(':')] command = splitcmd[0] defterm = splitcmd[1] reply = [] if command in self.dbs: db = command server = self.dbs[command].get('server', None) else: db = '*' server = normal_server if server is not None: c = dictclient.Connection(server) definitions = c.define(db, defterm) if not definitions: reply.append('No definitions found in ' + command) reply.extend(self.process_definition(d) for d in definitions[:MAX_DEFS]) return reply_to_user(data, reply)
def cmd_whereiseveryone(self, command, data): try: loc_data = shelve.open(self.loc_file) asker = data['source_user'] reply = [ '_{0}_: {1} ({2})'.format(nick, loc[-1]['where'], prettier_date(loc[-1]['when'])) for nick, loc in loc_data.items() if nick != asker and loc[-1]['where'].lower() not in NOLOC ] if not reply: if asker in loc_data: reply = ['You are the only one I know about!'] else: reply = ['Nobody has told me anything!'] except: reply = ['(shh.. someone made me do something wrong!)'] finally: loc_data.close() return reply_to_user(data, reply)
def actual_eval(self, js): logging.debug("Running `%s` in %s" % (self.bin, self.cwd)) proc = Popen( [self.bin], cwd=self.cwd, stdin=PIPE, stdout=PIPE, stderr=STDOUT, shell=False ) try: outs, errs = proc.communicate(input=js.encode('UTF-8')) except OSError as e: proc.kill() return reply_to_user(data, 'Subprocess error', e) lines = outs.decode('utf-8').split("\n") logs = lines[:-1] result = lines[-1] output = logs[:5] if len(logs) > 5: output.append('<... console output truncated ...>') output.append(result) return output
def cmd_list(self, command, data): cmds = self._get_cmd_list() return reply_to_user(data, cmds)
def cmd_greet(self, command, data): return reply_to_user(data, random.choice(self.local_data['replies']))
def cmd_eval(self, command, data): splitcmd = [a.strip() for a in command.split(':')] command = splitcmd[0] code = splitcmd[1] output = self.actual_eval(code) return reply_to_user(data, output)