def command(self, user, msg, whisper=False): args = "" if " " in msg: cmd, args = msg.split(" ", 1) cmd = cmd.lower() else: cmd = msg.strip().lower() # FIXME: This is a work around for shlex's poor unicode support. # args = unicode(args, 'utf-8', 'replace') args = args.encode("utf-8", "replace") # <<name>> Prefix. Used by the bot to redirect a whispers output to <name> m = self.redirect_check.search(cmd) if m: self.redirect_to_user = m.group("user") cmd = self.redirect_check.sub("", cmd) # [<name>] Prefix. Replaces the calling user with the jid of <name>. m = self.mimic_check.search(cmd) if m and utils.has_attr(utils.getname(user).lower(), "rank", const.RANK_ADMIN): user = utils.getjid(m.group("user")) cmd = self.mimic_check.sub("", cmd) try: cmd_func = mounts.CommandMount.plugins.get(cmd) if not cmd_func: self.error(user, "Unknown command, try !help") return # Class objects are types while class instances are not. # When cmd_func is not a type it's already been initialized if isinstance(cmd_func, type): # Initialize the hook to define it's default variables. cmd_func = cmd_func(self) # assert isinstance(cmd, CommandMount) authorized = True if cmd_func.rank in [const.RANK_USER, const.RANK_HIDDEN]: pass elif cmd_func.rank == const.RANK_MOD: if not utils.ismod(user) or not utils.isadmin(user): authorized = False self.error(user, "You must be a moderator to use that command.") elif cmd_func.rank == const.RANK_ADMIN: if not utils.isadmin(user): authorized = False self.error(user, "You must be an admin to use that command.") else: authorized = False self.error(user, "Unknown command, try !help") if authorized: cmd_func.process(user, args, whisper) except const.CommandHelp, args: self.sys(user, cmd_func.__doc__)
def thread(self, user, args): statuses = {"admins": [], "online": [], "offline": [], "away": [], "idle": [], "busy": []} for sid in self.parent.getRoster(): i = utils.getjid(sid) name = utils.getnickname(i) if name == iMan.config.server.username: continue if not utils.isonline(self.parent, sid): # statuses['offline'].append('(%s)' % name) continue jid_status = self.parent.getJidStatus(sid) for who, (status, display) in jid_status.iteritems(): if "@" not in unicode(who): continue if utils.isbanned(who): name = "#%s" % name continue if utils.isactive(self.parent, who): if utils.isadmin(who): name = "@%s" % name # statuses['admins'].append(name) elif utils.ismod(who): name = "%" + "%s" % name # statuses['admins'].append(name) statuses["online"].append(name) break # Anyone not "available". elif utils.isaway(self.parent, who): if status in [u"away", u"xa"]: name = "-%s" % name statuses["idle"].append(name) elif status == u"dnd": name = "!%s" % name statuses["busy"].append(name) break # Setup the header with a header for total number of users. reply = "Users (%s):\n" total = 0 for status, users in statuses.iteritems(): if not users: continue reply += "%s (%s): %s\n" % (status, len(users), " ".join(users)) total += len(users) # Tack on the total number of users. reply = reply % total self.parent.sendto(user, reply)
def thread(self, user, args, whisper): args = self.help_parser.parse_args(shlex.split(args)) if args.cmd: if args.cmd in mounts.CommandMount.plugins.keys(): self.parent.sendto(user, mounts.CommandMount.plugins[args.cmd].__doc__) else: self.parent.error(user, "I don't know that command (%s)." " Please check your spelling." % args.cmd) return reply = "Cmd Prefix: %s" % (iMan.config.system.commandprefix) user_cmd_list = [] mod_cmd_list = [] admin_cmd_list = [] disabled_cmd_list = [] for cmd in mounts.CommandMount.plugins.itervalues(): # Skip commands if a user doesn't match the command's rank. #if cmd.rank == CommandMount.RANK_USER: if cmd.rank == const.RANK_USER: user_cmd_list.append(cmd.name) elif cmd.rank == const.RANK_MOD: mod_cmd_list.append(cmd.name) elif cmd.rank == const.RANK_ADMIN: admin_cmd_list.append(cmd.name) elif cmd.rank == const.RANK_DISABLED: disabled_cmd_list.append(cmd.name) reply += " | User Cmds: " reply += ', '.join(user_cmd_list) if mod_cmd_list and (utils.ismod(user) or utils.isadmin(user)): reply += " | Mod Cmds: " reply += ', '.join(mod_cmd_list) if admin_cmd_list and utils.isadmin(user): reply += " | Admin Cmds: " reply += ', '.join(admin_cmd_list) if disabled_cmd_list and (utils.ismod(user) or utils.isadmin(user)): reply += " | Disabled Cmds: " reply += ', '.join(disabled_cmd_list) self.parent.sendto(user,reply)
def thread(self, msg): text = msg.text.strip() user = msg.from_user # TODO: Replace with the following when I feel like figuring out how # best to strip a dynamic length delimter #if not [x for x in iMan.config.system.commandprefix if text.startswith(x)]: if text[:1] not in iMan.config.system.commandprefix: return False text = text[1:] args = '' if " " in text: cmd, args = text.split(" ",1) cmd = cmd.lower() else: cmd = text.lower() #FIXME: This is a work around for shlex's poor unicode support. #args = unicode(args, 'utf-8', 'replace') args = args.encode('utf-8', 'replace') # <<name>> Prefix. Used by the bot to redirect a whispers output to <name> m = self.redirect_check.search(cmd) if m: self.parent.redirect_to_user = utils.getjid(m.group('user')) cmd = self.redirect_check.sub('', cmd) # [<name>] Prefix. Replaces the calling user with the jid of <name>. m = self.mimic_check.search(cmd) if m and utils.isadmin(user): user = utils.getjid(m.group('user')) cmd = self.mimic_check.sub('', cmd) try: cmd_func = mounts.CommandMount.plugins.get(cmd) if not cmd_func: self.parent.error(user, "Unknown command, try !help") return # Class objects are types while class instances are not. # When cmd_func is not a type it's already been initialized if isinstance(cmd_func, type): # Initialize the hook to define it's default variables. cmd_func = cmd_func(self.parent) authorized = True if cmd_func.rank in [const.RANK_USER, const.RANK_HIDDEN]: pass elif cmd_func.rank == const.RANK_MOD: if not utils.ismod(user) or not utils.isadmin(user): authorized = False self.parent.error(user, "You must be a moderator to use that command.") elif cmd_func.rank == const.RANK_ADMIN: if not utils.isadmin(user): authorized = False self.parent.error(user, "You must be an admin to use that command.") else: authorized = False self.parent.error(user, "Unknown command, try !help") if authorized: cmd_func.process(user, args) except const.CommandHelp, args: self.parent.sys(user, cmd_func.__doc__)