def cmd(send, msg, args): """Gives help. Syntax: !help <command> """ cmdchar = args['config']['core']['cmdchar'] if msg: if msg.startswith(cmdchar): msg = msg[len(cmdchar):] if len(msg.split()) > 1: send("One argument only") elif not is_registered(msg): send("Not a module.") else: doc = get_command(msg).get_doc() if doc is None: send("No documentation found.") else: for line in doc.splitlines(): send(line) else: modules = sorted(get_commands()) num = int(len(modules) / 2) cmdlist1 = (' %s' % cmdchar).join([x for x in modules[:num]]) cmdlist2 = (' %s' % cmdchar).join([x for x in modules[num:]]) send('Commands: %s%s' % (cmdchar, cmdlist1), target=args['nick']) send('%s%s' % (cmdchar, cmdlist2), target=args['nick']) send('%shelp <command> for more info on a command.' % cmdchar, target=args['nick'])
def cmd(send, msg, args): """Gives help. Syntax: !help <command> """ cmdchar = args['config']['core']['cmdchar'] if msg: if msg.startswith(cmdchar): msg = msg[len(cmdchar):] if len(msg.split()) > 1: send("One argument only") elif not is_registered(msg): send("Not a module.") else: doc = get_command(msg).get_doc() if doc is None: send("No documentation found.") else: for line in doc.splitlines(): send(line) else: modules = sorted(get_commands()) num = int(len(modules) / 2) cmdlist1 = (' %s' % cmdchar).join([x for x in modules[:num]]) cmdlist2 = (' %s' % cmdchar).join([x for x in modules[num:]]) send('Commands: %s%s' % (cmdchar, cmdlist1), target=args['nick']) send('%s%s' % (cmdchar, cmdlist2), target=args['nick']) send('%shelp <command> for more info on a command.' % cmdchar, target=args['nick'])
def cmd(send, msg, args): """Gives help. Syntax: {command} [command] """ cmdchar = args['config']['core']['cmdchar'] if msg: if msg.startswith(cmdchar): msg = msg[len(cmdchar):] if len(msg.split()) > 1: send("One argument only") elif not is_registered(msg): send("Not a module.") else: doc = get_command(msg).get_doc() if doc is None: send("No documentation found.") else: for line in doc.splitlines(): send(line.format(command=cmdchar + msg)) else: modules = sorted(get_commands()) cmdlist = (' %s' % cmdchar).join(modules) send('Commands: %s%s' % (cmdchar, cmdlist), target=args['nick'], ignore_length=True) send('%shelp <command> for more info on a command.' % cmdchar, target=args['nick'])
def cmd(send, msg, args): """Gets stats. Syntax: {command} <--high|--low|--userhigh|--nick <nick>|command> """ parser = arguments.ArgParser(args['config']) group = parser.add_mutually_exclusive_group() group.add_argument('--high', action='store_true') group.add_argument('--low', action='store_true') group.add_argument('--userhigh', action='store_true') group.add_argument('--nick', action=arguments.NickParser) group.add_argument('command', nargs='?') try: cmdargs = parser.parse_args(msg) except arguments.ArgumentException as e: send(str(e)) return session = args['db'] commands = get_commands(session) totals = get_command_totals(session, commands) sortedtotals = sorted(totals, key=totals.get) if is_registered(cmdargs.command): nicktotals = get_nick_totals(session, cmdargs.command) maxuser = sorted(nicktotals, key=nicktotals.get) if not maxuser: send("Nobody has used that command.") else: maxuser = maxuser[-1] send("%s is the most frequent user of %s with %d out of %d uses." % (maxuser, cmdargs.command, nicktotals[maxuser], totals[cmdargs.command])) elif cmdargs.high: send('Most Used Commands:') high = list(reversed(sortedtotals)) for x in range(3): if x < len(high): send("%s: %s" % (high[x], totals[high[x]])) elif cmdargs.low: send('Least Used Commands:') low = sortedtotals for x in range(3): if x < len(low): send("%s: %s" % (low[x], totals[low[x]])) elif cmdargs.userhigh: totals = get_nick_totals(session) sortedtotals = sorted(totals, key=totals.get) high = list(reversed(sortedtotals)) send('Most active bot users:') for x in range(3): if x < len(high): send("%s: %s" % (high[x], totals[high[x]])) elif cmdargs.nick: totals = get_nick_totals(session) if cmdargs.nick not in totals.keys(): send("%s has never used a bot command." % cmdargs.nick) else: send("%s has used %d bot commands." % (cmdargs.nick, totals[cmdargs.nick])) else: command = choice(list(totals.keys())) send("%s has been used %s times." % (command, totals[command]))
def cmd(send, msg, args): """Gets stats. Syntax: {command} <--high|--low|--userhigh|--nick <nick>|command> """ parser = arguments.ArgParser(args['config']) group = parser.add_mutually_exclusive_group() group.add_argument('--high', action='store_true') group.add_argument('--low', action='store_true') group.add_argument('--userhigh', action='store_true') group.add_argument('--nick', action=arguments.NickParser) group.add_argument('command', nargs='?') try: cmdargs = parser.parse_args(msg) except arguments.ArgumentException as e: send(str(e)) return session = args['db'] commands = get_commands(session) totals = get_command_totals(session, commands) sortedtotals = sorted(totals, key=totals.get) if is_registered(cmdargs.command): nicktotals = get_nick_totals(session, cmdargs.command) maxuser = sorted(nicktotals, key=nicktotals.get) if not maxuser: send("Nobody has used that command.") else: maxuser = maxuser[-1] send("%s is the most frequent user of %s with %d out of %d uses." % (maxuser, cmdargs.command, nicktotals[maxuser], totals[cmdargs.command])) elif cmdargs.high: send('Most Used Commands:') high = list(reversed(sortedtotals)) for x in range(3): if x < len(high): send("%s: %s" % (high[x], totals[high[x]])) elif cmdargs.low: send('Least Used Commands:') low = sortedtotals for x in range(3): if x < len(low): send("%s: %s" % (low[x], totals[low[x]])) elif cmdargs.userhigh: totals = get_nick_totals(session) sortedtotals = sorted(totals, key=totals.get) high = list(reversed(sortedtotals)) send('Most active bot users:') for x in range(3): if x < len(high): send("%s: %s" % (high[x], totals[high[x]])) elif cmdargs.nick: totals = get_nick_totals(session) if cmdargs.nick not in totals.keys(): send("%s has never used a bot command." % cmdargs.nick) else: send("%s has used %d bot commands." % (cmdargs.nick, totals[cmdargs.nick])) else: command = choice(list(totals.keys())) send("%s has been used %s times." % (command, totals[command]))
def cmd(send, msg, args): """Gets stats. Syntax: !stats <--high|--low|command> """ session = args['db'] commands = get_commands(session) totals = get_command_totals(session, commands) if is_registered(msg): nicktotals = get_nick_totals(session, commands, msg) maxuser = sorted(nicktotals, key=nicktotals.get) if not maxuser: send("Nobody has used that command.") else: maxuser = maxuser[-1] send("%s is the most frequent user of %s with %d out of %d uses." % (maxuser, msg, nicktotals[maxuser], totals[msg])) else: match = re.match('--(.*)', msg) sortedtotals = sorted(totals, key=totals.get) if match: if match.group(1) == 'high': send('Most Used Commands:') high = list(reversed(sortedtotals)) for x in range(0, 3): if x < len(high): send("%s: %s" % (high[x], totals[high[x]])) elif match.group(1) == 'low': send('Least Used Commands:') low = sortedtotals for x in range(0, 3): if x < len(low): send("%s: %s" % (low[x], totals[low[x]])) else: send("%s is not a valid flag" % match.group(1)) elif msg: send("Invalid Command.") else: cmd = choice(list(totals.keys())) send("%s has been used %s times." % (cmd, totals[cmd]))
def cmd(send, msg, args): """Gives help. Syntax: {command} [command] """ cmdchar = args['config']['core']['cmdchar'] if msg: if msg.startswith(cmdchar): msg = msg[len(cmdchar):] if len(msg.split()) > 1: send("One argument only") elif not is_registered(msg): send("Not a module.") else: doc = get_command(msg).get_doc() if doc is None: send("No documentation found.") else: for line in doc.splitlines(): send(line.format(command=cmdchar + msg)) else: modules = sorted(get_commands()) cmdlist = (' %s' % cmdchar).join(modules) send('Commands: %s%s' % (cmdchar, cmdlist), target=args['nick'], ignore_length=True) send('%shelp <command> for more info on a command.' % cmdchar, target=args['nick'])
def handle_msg(self, msgtype, c, e): """The Heart and Soul of IrcBot.""" # actions don't have the nick attr for some reason if msgtype == 'action': nick = e.source.split('!')[0] else: nick = e.source.nick # modes have separate arguments, everything else just one if msgtype == 'mode' or msgtype == 'nick' or msgtype == 'join': msg = " ".join(e.arguments) else: msg = e.arguments[0].strip() # Send the response to private messages to the sending nick. if msgtype == 'privmsg' or msgtype == 'privnotice': target = nick else: target = e.target send = lambda msg, mtype='privmsg', target=target: self.send(target, self.config['core']['nick'], msg, mtype) if msgtype == 'privnotice': if not hasattr(self, 'connection'): return if nick == 'NickServ': admin.set_admin(msg, self) return if self.config['feature'].getboolean('hooks') and nick not in self.ignored: for hook in self.hooks: realargs = self.do_args(hook.args, send, nick, target, e.source, c, hook, msgtype) hook.run(send, msg, msgtype, self, target, realargs) if msgtype == 'nick': if e.target in self.admins: c.privmsg('NickServ', 'ACC %s' % e.target) if identity.handle_nick(self, e): for x in misc.get_channels(self.channels, e.target): self.do_kick(send, x, e.target, "identity crisis") return # must come after set_admin to prevent spam self.do_log(target, nick, msg, msgtype) if msgtype == 'mode': self.do_mode(target, msg, nick, send) return if msgtype == 'join': if nick == c.real_nickname: send("Joined channel %s" % target, target=self.config['core']['ctrlchan']) return # ignore empty messages if not msg: return if e.target == self.config['core']['ctrlchan']: control.handle_ctrlchan(self, msg, c, send) if not self.is_admin(send, nick, False) and nick in self.ignored: return msg = misc.get_cmdchar(self.config, c, msg, msgtype) cmd = msg.split()[0] cmdchar = self.config['core']['cmdchar'] admins = [x.strip() for x in self.config['auth']['admins'].split(',')] # handle !s cmdlen = len(cmd) + 1 if cmd.startswith('%ss' % cmdchar): match = re.match('%ss(\W)' % cmdchar, cmd) if match: cmd = cmd.split(match.group(1))[0] cmdlen = len(cmd) cmdargs = msg[cmdlen:] found = False if cmd.startswith(cmdchar): cmd_name = cmd[len(cmdchar):] if command.is_registered(cmd_name): found = True cmd_obj = command.get_command(cmd_name) if cmd_obj.is_limited() and self.abusecheck(send, nick, target, cmd_obj.limit, cmd[len(cmdchar):]): return # Duplicate command if command.check_command(self.db.get(), nick, msg, target): return args = self.do_args(cmd_obj.args, send, nick, target, e.source, c, cmd_name, msgtype) cmd_obj.run(send, cmdargs, args, cmd_name, nick, target, self) # special commands if cmd.startswith(cmdchar): if cmd[len(cmdchar):] == 'reload': if nick in admins: found = True self.do_reload() send("Aye Aye Capt'n") # everything below this point requires admin if not found and self.is_admin(send, nick): self.do_admin(c, cmd[len(cmdchar):], cmdargs, send, nick, msgtype, target)
def handle_msg(self, msgtype, c, e): """The Heart and Soul of IrcBot.""" # actions don't have the nick attr for some reason if msgtype == 'action': nick = e.source.split('!')[0] else: nick = e.source.nick # modes have separate arguments, everything else just one if msgtype == 'mode' or msgtype == 'nick' or msgtype == 'join': msg = " ".join(e.arguments) else: msg = e.arguments[0].strip() # Send the response to private messages to the sending nick. if msgtype == 'privmsg' or msgtype == 'privnotice': target = nick else: target = e.target send = lambda msg, mtype='privmsg', target=target: self.send(target, self.config['core']['nick'], msg, mtype) if msgtype == 'privnotice': # FIXME: come up with a better way to prevent admin abuse. if nick == 'NickServ': admin.set_admin(msg, self) return if msgtype == 'pubnotice': return if self.config['feature'].getboolean('hooks') and nick not in self.ignored: for h in self.hooks: realargs = self.do_args(h.args, send, nick, target, e.source, c, h, msgtype) try: h.run(send, msg, msgtype, self, target, realargs) except InternalError: self.db.rollback() if msgtype == 'nick': if e.target in self.admins: c.send_raw('NS ACC %s' % e.target) if identity.handle_nick(self, e): for x in misc.get_channels(self.channels, e.target): self.do_kick(send, x, e.target, "identity crisis") return # must come after set_admin to prevent spam self.do_log(target, nick, msg, msgtype) if msgtype == 'mode': self.do_mode(target, msg, nick, send) return if msgtype == 'join': if nick == c.real_nickname: send("Joined channel %s" % target, target=self.config['core']['ctrlchan']) return # ignore empty messages if not msg: return if e.target == self.config['core']['ctrlchan']: control.handle_ctrlchan(self, msg, c, send) if not self.is_admin(send, nick, False) and nick in self.ignored: return msg = misc.get_cmdchar(self.config, c, msg, msgtype) cmd = msg.split()[0] cmdchar = self.config['core']['cmdchar'] admins = [x.strip() for x in self.config['auth']['admins'].split(',')] # handle !s cmdlen = len(cmd) + 1 if cmd.startswith('%ss' % cmdchar): match = re.match('%ss(\W)' % cmdchar, cmd) if match: cmd = cmd.split(match.group(1))[0] cmdlen = len(cmd) cmdargs = msg[cmdlen:] found = False if cmd.startswith(cmdchar): cmd_name = cmd[len(cmdchar):] if command.is_registered(cmd_name): found = True cmd_obj = command.get_command(cmd_name) if cmd_obj.is_limited() and self.abusecheck(send, nick, target, cmd_obj.limit, cmd[len(cmdchar):]): return try: # Duplicate command if command.check_command(self.db.get(), nick, msg, target): return args = self.do_args(cmd_obj.args, send, nick, target, e.source, c, cmd_name, msgtype) cmd_obj.run(send, cmdargs, args, cmd_name, nick, target, self) except InternalError: self.db.rollback() # special commands if cmd.startswith(cmdchar): if cmd[len(cmdchar):] == 'reload': if nick in admins: found = True self.do_reload() send("Aye Aye Capt'n") # everything below this point requires admin if not found and self.is_admin(send, nick): self.do_admin(c, cmd[len(cmdchar):], cmdargs, send, nick, msgtype, target)
def handle_msg(self, c, e): """The Heart and Soul of IrcBot.""" if e.type not in ['authenticate', 'join', 'part', 'quit']: nick = e.source.nick else: nick = e.source # cap and kick have multiple arguments. if e.type in ['cap', 'kick']: msg = " ".join(e.arguments).strip() elif e.arguments: msg = e.arguments[0].strip() else: # join and nick don't have any arguments. msg = "" # Send the response to private messages to the sending nick. target = nick if e.type == 'privmsg' else e.target def send(msg, mtype='privmsg', target=target, ignore_length=False): self.send(target, self.connection.real_nickname, msg, mtype, ignore_length) if e.type in ['authenticate', 'bannedfromchan', 'cap', 'ctcpreply', 'error', 'nosuchnick', 'nick', 'nicknameinuse', 'privnotice', 'welcome']: self.handle_event(msg, send, c, e) return # ignore empty messages if not msg and e.type != 'join': return self.do_log(target, nick, msg, e.type) if e.type == 'mode': self.do_mode(target, msg, nick, send) return if e.type == 'join': if nick == c.real_nickname: send("Joined channel %s" % target, target=self.config['core']['ctrlchan']) return if e.type == 'part': if nick == c.real_nickname: send("Parted channel %s" % target, target=self.config['core']['ctrlchan']) return if e.type == 'kick': if nick == c.real_nickname: send("Kicked from channel %s" % target, target=self.config['core']['ctrlchan']) # Auto-rejoin after 5 seconds. self.workers.defer(5, False, self.connection.join, target) return if e.target == self.config['core']['ctrlchan'] and self.is_admin(None, nick): control.handle_ctrlchan(self, msg, send) if self.is_ignored(nick) and not self.is_admin(None, nick): return if self.config['feature'].getboolean('hooks'): for h in hook.get_hook_objects(): realargs = self.do_args(h.args, send, nick, target, e.source, h, e.type) h.run(send, msg, e.type, self, target, realargs) msg = misc.get_cmdchar(self.config, c, msg, e.type) cmd = msg.split()[0] cmdchar = self.config['core']['cmdchar'] admins = [x.strip() for x in self.config['auth']['admins'].split(',')] cmdlen = len(cmd) + 1 # FIXME: figure out a better way to handle !s if cmd.startswith('%ss' % cmdchar): # escape special regex chars raw_cmdchar = '\\' + cmdchar if re.match(r'[\[\].^$*+?]', cmdchar) else cmdchar match = re.match(r'%ss(\W)' % raw_cmdchar, cmd) if match: cmd = cmd.split(match.group(1))[0] cmdlen = len(cmd) cmdargs = msg[cmdlen:] cmd_name = cmd[len(cmdchar):] if cmd.startswith(cmdchar) else None cmdargs, filtersend = self.get_filtered_send(cmdargs, send, target) if filtersend is None: send(cmdargs) return if command.is_registered(cmd_name): cmd_obj = command.get_command(cmd_name) if cmd_obj.is_limited() and self.abusecheck(send, nick, target, cmd_obj.limit, cmd[len(cmdchar):]): return if cmd_obj.requires_admin() and not self.is_admin(send, nick): send("This command requires admin privileges.") return args = self.do_args(cmd_obj.args, send, nick, target, e.source, cmd_name, e.type) cmd_obj.run(filtersend, cmdargs, args, cmd_name, nick, target, self) # special commands elif cmd[len(cmdchar):] == 'reload' or cmd[len(cmdchar):] == 'r': if nick in admins: send("Aye Aye Capt'n")
def handle_msg(self, msgtype, c, e): """The Heart and Soul of IrcBot.""" nick = e.source.nick # modes have separate arguments, everything else just one if msgtype == 'mode' or msgtype == 'nick' or msgtype == 'join': msg = " ".join(e.arguments) else: msg = e.arguments[0].strip() # ignore empty messages if not msg: return # Send the response to private messages to the sending nick. if msgtype == 'privmsg' or msgtype == 'privnotice': target = nick else: target = e.target def send(msg, mtype='privmsg', target=target): self.send(target, self.connection.real_nickname, msg, mtype) if msgtype == 'privnotice': # FIXME: come up with a better way to prevent admin abuse. if nick == 'NickServ': admin.set_admin(msg, self) return if self.config['feature'].getboolean('hooks') and not self.is_ignored(nick): for h in self.hooks.values(): realargs = self.do_args(h.args, send, nick, target, e.source, h, msgtype) h.run(send, msg, msgtype, self, target, realargs) if msgtype == 'nick': if self.config['feature'].getboolean('nickserv') and e.target in self.admins: c.send_raw('NS ACC %s' % e.target) if identity.handle_nick(self, e): for x in misc.get_channels(self.channels, e.target): self.do_kick(send, x, e.target, "identity crisis") return # must come after set_admin to prevent spam from all the NickServ ACC responses # We log nick changes in bot.py so that they show up for all channels. self.do_log(target, nick, msg, msgtype) if msgtype == 'mode': self.do_mode(target, msg, nick, send) return if msgtype == 'join': if nick == c.real_nickname: send("Joined channel %s" % target, target=self.config['core']['ctrlchan']) return if e.target == self.config['core']['ctrlchan'] and self.is_admin(None, nick): control.handle_ctrlchan(self, msg, send) if self.is_ignored(nick) and not self.is_admin(None, nick): return msg = misc.get_cmdchar(self.config, c, msg, msgtype) cmd = msg.split()[0] cmdchar = self.config['core']['cmdchar'] admins = [x.strip() for x in self.config['auth']['admins'].split(',')] # handle !s cmdlen = len(cmd) + 1 if cmd.startswith('%ss' % cmdchar): # escape special regex chars raw_cmdchar = '\\' + cmdchar if re.match(r'[\[\].^$*+?]', cmdchar) else cmdchar match = re.match(r'%ss(\W)' % raw_cmdchar, cmd) if match: cmd = cmd.split(match.group(1))[0] cmdlen = len(cmd) cmdargs = msg[cmdlen:] if cmd.startswith(cmdchar) and not msgtype == 'action': cmd_name = cmd[len(cmdchar):] if command.is_registered(cmd_name): cmd_obj = command.get_command(cmd_name) if cmd_obj.is_limited() and self.abusecheck(send, nick, target, cmd_obj.limit, cmd[len(cmdchar):]): return if cmd_obj.requires_admin() and not self.is_admin(send, nick): send("This command requires admin privileges.") return args = self.do_args(cmd_obj.args, send, nick, target, e.source, cmd_name, msgtype) cmd_obj.run(send, cmdargs, args, cmd_name, nick, target, self) # special commands elif cmd[len(cmdchar):] == 'reload': if nick in admins: send("Aye Aye Capt'n")
def handle_msg(self, c, e): """The Heart and Soul of IrcBot.""" if e.type not in ['authenticate', 'join', 'part', 'quit']: nick = e.source.nick else: nick = e.source # cap and kick have multiple arguments. if e.type in ['cap', 'kick']: msg = " ".join(e.arguments).strip() elif e.arguments: msg = e.arguments[0].strip() else: # join and nick don't have any arguments. msg = "" # Send the response to private messages to the sending nick. target = nick if e.type == 'privmsg' else e.target def send(msg, mtype='privmsg', target=target, ignore_length=False): self.send(target, self.connection.real_nickname, msg, mtype, ignore_length) if e.type in ['903', 'authenticate', 'bannedfromchan', 'cap', 'ctcpreply', 'error', 'nosuchnick', 'nick', 'nicknameinuse', 'privnotice', 'welcome']: self.handle_event(msg, send, c, e) return # ignore empty messages if not msg and e.type != 'join': return self.do_log(target, nick, msg, e.type) if e.type == 'mode': self.do_mode(target, msg, nick, send) return if e.type == 'join': if nick == c.real_nickname: send("Joined channel %s" % target, target=self.config['core']['ctrlchan']) return if e.type == 'part': if nick == c.real_nickname: send("Parted channel %s" % target, target=self.config['core']['ctrlchan']) return if e.type == 'kick': if nick == c.real_nickname: send("Kicked from channel %s" % target, target=self.config['core']['ctrlchan']) # Auto-rejoin after 5 seconds. self.workers.defer(5, False, self.connection.join, target) return if e.target == self.config['core']['ctrlchan'] and self.is_admin(None, nick): control.handle_ctrlchan(self, msg, send) if self.is_ignored(nick) and not self.is_admin(None, nick): return if self.config['feature'].getboolean('hooks'): for h in hook.get_hook_objects(): realargs = self.do_args(h.args, send, nick, target, e.source, h, e.type) h.run(send, msg, e.type, self, target, realargs) msg = misc.get_cmdchar(self.config, c, msg, e.type) cmd = msg.split()[0] cmdchar = self.config['core']['cmdchar'] admins = [x.strip() for x in self.config['auth']['admins'].split(',')] cmdlen = len(cmd) + 1 # FIXME: figure out a better way to handle !s if cmd.startswith('%ss' % cmdchar): # escape special regex chars raw_cmdchar = '\\' + cmdchar if re.match(r'[\[\].^$*+?]', cmdchar) else cmdchar match = re.match(r'%ss(\W)' % raw_cmdchar, cmd) if match: cmd = cmd.split(match.group(1))[0] cmdlen = len(cmd) cmdargs = msg[cmdlen:] cmd_name = cmd[len(cmdchar):] if cmd.startswith(cmdchar) else None cmdargs, filtersend = self.get_filtered_send(cmdargs, send, target) if filtersend is None: send(cmdargs) return if command.is_registered(cmd_name): cmd_obj = command.get_command(cmd_name) if cmd_obj.is_limited() and self.abusecheck(send, nick, target, cmd_obj.limit, cmd[len(cmdchar):]): return if cmd_obj.requires_admin() and not self.is_admin(send, nick): send("This command requires admin privileges.") return args = self.do_args(cmd_obj.args, send, nick, target, e.source, cmd_name, e.type) cmd_obj.run(filtersend, cmdargs, args, cmd_name, nick, target, self) # special commands elif cmd[len(cmdchar):] == 'reload': if nick in admins: send("Aye Aye Capt'n")