def ev_msg(self, user, msg, raw_msg): user = utils.getjid(user.getStripped()) # Is this a command? if msg[:1] in iMan.config.system.commandprefix: self.command(user, msg[1:]) elif user != utils.getjid(server.username): if self.hook(const.LOC_EV_MSG, user, msg): # self.log("<%s> %s" % (getnickname(user), msg)) self.sendtoall("<%s> %s" % (utils.getnickname(user), msg), butnot=[unicode(user)] )
def ev_msg(self, msg): user = utils.getjid(msg.from_user.getStripped()) if user != utils.getjid(server.username): if self.hook(const.LOC_EV_MSG, msg): # TODO: Log all incoming to the console and # all outgoing to a file. logging.getLogger('pygab.chat').info('%s -> %s' % ( utils.getnickname(msg.from_user), msg.text )) return # self.log("<%s> %s" % (getnickname(user), msg)) text = '<%s> %s' % (utils.getnickname(msg.from_user), msg.text) self.sendtoall(text, butnot=[unicode(user)])
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 sendto(self, user, text): '''Send msg to user via self._send_msg''' logging.getLogger('pygab.chat').info('%s <- %s' % (utils.getnickname(user), text)) message = self._build_msg(utils.getjid(user), text) if self.hook(const.LOC_SEND_MSG_PER_MSG, message): return self._send_msg(message)
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 sendtoall(self, text, butnot=[]): '''Send msg to all online users excluding anyone in butnot.''' logging.getLogger('pygab.chat').info('All <- %s' % text) if self.hook(const.LOC_SEND_MSG_PER_MSG, text): return for user in self.getRoster(): if user in butnot: continue message = self._build_msg(utils.getjid(user), text) self._send_msg(message)
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__)
def thread(self, user, args, whisper): if not args: raise const.CommandHelp args = args.split(' ', 1) cmd, message = args[0], args[1:] cmd = cmd.lower() username = utils.getname(user).lower() if cmd == 'get': if username in iMan.mail: letters = iMan.mail[username].items() # Only take the top letter. sender, message = letters[0] self.parent.sendto(user, "%s says '%s'" % (sender, message)) self.parent.sendto( user, "You have %s more letter%s." % ((len(letters) - 1) or 'no', ((len(letters) - 1) != 1 and 's') or '' ) ) del iMan.mail[username][sender] if not iMan.mail[username]: del iMan.mail[username] else: self.parent.sendto(user, "I have no letters for you.") elif cmd == 'check': letters = len(iMan.mail.get(username, [])) self.parent.sendto( user, 'I have %s letter%s for you.' % ( letters or 'no', (letters != 1 and 's') or '' ) ) else: if not message: raise const.CommandHelp, "Missing message" target = cmd.lower() if target == iMan.config.server.displayname.lower(): self.parent.sendto(user, "Why do you need to send me mail?") return elif target not in iMan.roster: self.parent.sendto(user, "I don't know %s and, therefore, " "can't send him a letter." % cmd) else: iMan.mail[target][utils.getname(user)] = ' '.join(message) target_jid = utils.getjid(target) if self.parent.was_whispered and \ utils.isonline(self.parent, target_jid): self.parent.sendto(target_jid, '%s has mailed you a message. ' 'Please use "/w iPal !mail get" to ' 'retrive it.' % utils.getname(user)) self.parent.sendto(user, "I've notified %s about your " "message." % target) else: self.parent.sendto(user, "I have mailed your message to %s. " "He will notified it when he logs in." % cmd) iMan.mail.save()