def logsearch(event, bot): """ log [n] [searchterm]. Will search logs for searchterm. n is the number of results to display [1-99], default is 6 and anything over will be output to pastebin. """ iproxy = INDEX_PROXIES.get(bot.network) if iproxy: # parse input if not event.argument: return bot.say(functionHelp(logsearch)) n, q = argumentSplit(event.argument, 2) try: n = int(n) if n > 99: raise ValueError elif n < 0: raise ValueError if n == 0: n = None q = q except ValueError: q = event.argument n = 6 results = iproxy.search(event.target, q, n) if results is None: bot.say("Log search error happened. Check console.") else: #results.append((item["timestamp"], item["nick"], item["source"], item["content"])) if not results: return bot.say("No results.") if n > 6 or n is None: title = "Logsearch for (%s)" % q body = "%s: %%s" % title pastehelper(bot, body, items=(LOG_FORMAT % (x[1], x[3]) for x in results), title=title, altmsg="%s", force=True) else: bot.say("{0}", fcfs=True, strins=[LOG_FORMAT % (x[1], x[3]) for x in results], joinsep=u"\x02 | \x02")
def butts(event, bot): """ butts [~del] [input]. Add or delete an entry in the bestbutts quote database. If no input is supplied the entire database will be dumped. e.g. .butts <@bbm> butt is love """ if not bot.getOption("BUTTS", module="pbm_butt"): return if not event.argument: items = bot.dbQuery('''SELECT id, butt FROM butts;''') if items: pastehelper(bot, basemsg="butts: %s", force=True, altmsg="%s", items=("%s: %s" % (row[0], row[1]) for row in items)) else: bot.say("no butts.") else: cmd, arg = argumentSplit(event.argument, 2) if cmd == "~del": bot.dbQuery('''DELETE FROM butts WHERE id = ?;''', (arg, )) bot.say("OK.") else: # add a butts bot.dbQuery('''INSERT INTO butts (butt) VALUES(?);''', (event.argument, )) bot.say("OK.")
def listUsers(self, dest): users = self.channels.get(dest) if users: if len(users) > 2: msg = "Users listening to (%s): %%s" % dest title = "Users listening to (%s)" % dest items = [USER_ITEM % (u.id, u.getName()) for u in users] pastehelper(SteamIRCBotWrapper(Event(None, target=dest), self.container, self), msg, items=items, altmsg="%s", title=title) else: for u in users: self.ircSay(dest, USER_ITEM % (u.id, u.getName())) else: self.ircSay(dest, "No one listening in here.")
def listgroups(bot, user=None): # [[group, [aliases]],] nglist = [] if user: result = bot.dbQuery( '''SELECT grp from aliasgrp WHERE user = ? GROUP BY grp;''', (user, )) else: result = bot.dbQuery('''SELECT grp from aliasgrp GROUP BY grp;''') for group in result: group = group['grp'] aliases = [ a['alias'] for a in bot.dbQuery( '''SELECT alias FROM aliasgrpalias WHERE grp = ? AND alias != ?;''', (group, group)) ] if aliases: nglist.append("%s (%s)" % (group, ", ".join(aliases))) else: nglist.append(group) if not nglist: if user: return bot.say("No groups found for user (%s)." % user) else: return bot.say("No groups found.") if user: msg = "Groups for (%s): %%s" % user else: msg = "Groups: %s" return pastehelper(bot, msg, items=nglist, title=msg[:-3])
def deliver_tell(event, bot): # if alias module available use it user = None if bot.isModuleAvailable("pbm_alias"): # pretty convoluted but faster than fetching both modules every time # may return none if this event gets captured for a first time user before user module # also faster this way than making 2 db calls for USER_MODULE.get_username user = USERS_MODULE.ALIAS_MODULE.lookup_alias(bot.dbQuery, event.nick) if not user: user = event.nick toldtime = int(timegm(gmtime())) # This seems like it might be a bit of a waste. But it should stop the rare occurance of "double tell delivery" (I've only seen it happen once.) tells = bot.dbBatch( ( # Query1, get tells ('''SELECT id,source,telltime,origintime,remind,msg FROM tell WHERE user=? AND delivered=0 AND telltime<? ORDER BY telltime;''', (user,toldtime)), # Query2, update query ('''UPDATE tell SET delivered=1,toldtime=? WHERE user=? AND delivered=0 AND telltime<?;''',(toldtime, user, toldtime)), ) )[0] # 0 gets the results from the first query only if tells: collate = False lines = None if len(tells) > 3: collate = True lines = [] for tell in tells: if tell['remind']: source = tell['source'] if source: data = [event.nick, source, tell['msg'], distance_of_time_in_words(tell['origintime'], toldtime), distance_of_time_in_words(tell['telltime'], toldtime, suffix="late")] fmt = REMINDFORMAT else: data = [event.nick, tell['msg'], distance_of_time_in_words(tell['origintime'], toldtime), distance_of_time_in_words(tell['telltime'], toldtime, suffix="late")] fmt = SELFREMINDFORMAT if collate: lines.append(fmt.format(*data)) else: bot.say(fmt, strins=data, fcfs=True) else: data = [event.nick, tell['source'], tell['msg'], distance_of_time_in_words(tell['telltime'], toldtime)] if collate: lines.append(TELLFORMAT.format(*data)) else: bot.say(TELLFORMAT, strins=data, fcfs=True) if collate: msg = "Tells/reminds for (%s): %%s" % event.nick title = "Tells/reminds for (%s)" % event.nick pastehelper(bot, msg, items=lines, altmsg="%s", force=True, title=title)
def listgroupusers(bot, groupname): # querying a group members = group_list(bot.dbQuery, groupname) if members: msg = "Users in group (%s): %%s" % (groupname) return pastehelper(bot, msg, items=members, title="Members of (%s)" % groupname) else: return bot.say("Group not found: (%s)" % groupname)
def deliver_alerts(chan_or_user=None, alerts=None, bot=None): if not bot: return current_time = int(timegm(gmtime())) filtered_alerts = [] row_ids = [] for a in alerts: id = str(a['id']) if bot.dbQuery('''SELECT 1 FROM alert WHERE delivered=0 AND id=?''', (id,)): row_ids.append(id) filtered_alerts.append(a) alerts = filtered_alerts if not alerts: return collate = False lines = None if len(alerts) > 3: collate = True lines = [] for a in alerts: row_ids.append(id) receiving_user = a['target_user'] source_user = a['source_user'] if source_user: data = [a['target_user'], source_user, a['msg'], distance_of_time_in_words(a['created_time'], current_time)] fmt = ALERT_FORMAT else: data = [a['target_user'], a['msg'], distance_of_time_in_words(a['created_time'], current_time)] fmt = SELF_ALERT_FORMAT if collate: lines.append(fmt.format(*data)) else: bot.sendmsg(chan_or_user, fmt, strins=data, fcfs=True) if collate: msg = "Alerts for (%s): %%s" % receiving_user title = "Alerts for (%s)" % receiving_user pastehelper(bot, msg, items=lines, altmsg="%s", force=True, title=title) for row_id in row_ids: bot.dbQuery('''UPDATE alert SET delivered=1 WHERE id=?;''', (row_id, ))
def butts(event, bot): """ butts [~del] [input]. Add or delete an entry in the bestbutts quote database. If no input is supplied the entire database will be dumped. e.g. .butts <@bbm> butt is love """ if not bot.getOption("BUTTS", module="pbm_butt"): return if not event.argument: items = bot.dbQuery('''SELECT id, butt FROM butts;''') if items: pastehelper(bot, basemsg="butts: %s", force=True, altmsg="%s", items=("%s: %s" % (row[0], row[1]) for row in items)) else: bot.say("no butts.") else: cmd, arg = argumentSplit(event.argument, 2) if cmd == "~del": bot.dbQuery('''DELETE FROM butts WHERE id = ?;''', (arg, )) bot.say("OK.") else: # add a butts bot.dbQuery('''INSERT INTO butts (butt) VALUES(?);''', (event.argument,)) bot.say("OK.")
def alias(event, bot): """ alias [target] aliasname. If only aliasname is supplied, aliases for aliasname are retrieved. Otherwise if target is also supplied, aliasname will become an alias of target (can also be a group.) See addtional help for ~del. |alias ~del aliasname: will remove the alias aliasname. """ # API is bit different from olde bot, and then group things arg1, arg2 = argumentSplit(event.argument, 2) if arg1 == "~del": if arg2: return del_alias(bot, arg2) else: # show help for del return bot.say(functionHelp(alias, "~del")) elif arg1 and arg2: #binding a new alias if arg2.lower() == "me": return bot.say("But you are already yourself.") source = USERS_MODULE.get_username(bot, arg1, source=event.nick, _inalias=True) if not source: # ATTEMPT GROUP if aliasgroup(bot, arg1, arg2) is False: return bot.say("(%s) is not a group or a user I know." % arg1) else: return # else continue with normal user return aliasuser(bot, arg1, arg2, source) elif arg1: #querying an alias nick = lookup_alias(bot.dbQuery, arg1) if nick: aliases = alias_list(bot.dbQuery, nick) if aliases: msg = "Aliases for (%s): %%s" % arg1 title = "Aliases for (%s)" % arg1 return pastehelper(bot, msg, items=aliases, altmsg="%s", title=title) # unknown alias or no aliases: return bot.say("No aliases for (%s)" % arg1) # if none of the above, show help bot.say(functionHelp(alias)) return
def alias(event, bot): """ alias [target] aliasname. If only aliasname is supplied, aliases for aliasname are retrieved. Otherwise if target is also supplied, aliasname will become an alias of target (can also be a group.) See addtional help for ~del. |alias ~del aliasname: will remove the alias aliasname. """ # API is bit different from olde bot, and then group things arg1, arg2 = argumentSplit(event.argument, 2) if arg1 == "~del": if arg2: return del_alias(arg2) else: # show help for del return bot.say(functionHelp(alias, "~del")) elif arg1 and arg2: #binding a new alias if arg2.lower() == "me": return bot.say("But you are already yourself.") source = USERS_MODULE.get_username(bot, arg1, source=event.nick, _inalias=True) if not source: # ATTEMPT GROUP if aliasgroup(bot, arg1, arg2) is False: return bot.say("(%s) is not a group or a user I know." % arg1) else: return # else continue with normal user return aliasuser(bot, arg1, arg2, source) elif arg1: #querying an alias nick = lookup_alias(bot.dbQuery, arg1) if nick: aliases = alias_list(bot.dbQuery, nick) if aliases: msg = "Aliases for (%s): %%s" % arg1 title = "Aliases for (%s)" % arg1 return pastehelper(bot, msg, items=aliases, altmsg="%s", title=title) # unknown alias or no aliases: return bot.say("No aliases for (%s)" % arg1) # if none of the above, show help bot.say(functionHelp(alias)) return
def listgroups(bot, user=None): # [[group, [aliases]],] nglist = [] if user: result = bot.dbQuery('''SELECT grp from aliasgrp WHERE user = ? GROUP BY grp;''', (user,)) else: result = bot.dbQuery('''SELECT grp from aliasgrp GROUP BY grp;''') for group in result: group = group['grp'] aliases = [a['alias'] for a in bot.dbQuery('''SELECT alias FROM aliasgrpalias WHERE grp = ? AND alias != ?;''', (group, group))] if aliases: nglist.append("%s (%s)" % (group, ", ".join(aliases))) else: nglist.append(group) if not nglist: if user: return bot.say("No groups found for user (%s)." % user) else: return bot.say("No groups found.") if user: msg = "Groups for (%s): %%s" % user else: msg = "Groups: %s" return pastehelper(bot, msg, items=nglist, title=msg[:-3])
def alias(event, bot): """ alias [(source, ~group, ~del)] argument. If only argument is supplied, aliases for that argument are retrieved. Otherwise if source is supplied, argument will become an alias of source. See addtional help for ~group and ~del. |alias ~group [groupname] argument: will add argument to groupname is groupname is provided, else will return all users in groupname. |alias ~del [(~group, groupname)] argument: will remove the user alias argument if supplied on it's own. ~group will remove the entire group argument. If groupname is used will remove argument from group, in this case argument may be multiple entries. """ # API is similar to old bot arg1, arg2, arg3 = argumentSplit(event.argument, 3) if arg1 == "~group": if arg2 and arg3: # binding to a group nick = USERS_MODULE.get_username(bot, arg2, source=event.nick, _inalias=True) if not nick: return bot.say("User/alias (%s) not found or seen." % arg2) if group_check(bot.dbQuery, arg2, arg3): return bot.say("User/alias (%s) is already a member of (%s)." % (arg3, arg2)) group_add(bot.dbQuery, arg2, arg3) return bot.say("User/alias (%s) added to group (%s)." % (arg2, arg3)) elif arg2: # querying a group members = group_list(bot.dbQuery, arg2) if members: msg = "Users in group (%s): %%s" % (arg2) return pastehelper(bot, msg, items=members, title="Members of (%s)" % arg2) else: return bot.say("Group not found: (%s)" % arg2) else: #show help for group return bot.say(functionHelp(alias, "~group")) elif arg1 == "~del": if arg2 and arg3: # do a delete on group if arg2 == "~group": #remove entire group group = bot.dbQuery('''SELECT grp FROM aliasgrp WHERE grp = ?;''', (arg3,), fetchone) if group: bot.dbQuery('''DELETE FROM aliasgrp WHERE grp = ?;''', (arg3,)) return bot.say("Removed group (%s)" % arg3) else: return bot.say("Group (%s) not found." % arg3) else: # assume arg2 is a groupname to remove entry from group = bot.dbQuery('''SELECT grp FROM aliasgrp WHERE grp = ?;''', (arg2,), fetchone) if group: nick = USERS_MODULE.get_username(bot, arg3, source=event.nick, _inalias=True) if not nick: return bot.say("User/alias (%s) not found." % arg3) if bot.dbQuery('''SELECT 1 FROM aliasgrp WHERE grp = ? AND user = ?;''', (arg2,nick), fetchone): bot.dbQuery('''DELETE FROM aliasgrp WHERE grp = ? AND user = ?;''', (arg2, nick)) return bot.say("Removed (%s) from (%s)" % (nick, arg2)) else: return bot.say("User/alias (%s) not found in group (%s)" % (nick, arg2)) else: return bot.say("Group (%s) not found." % arg2) elif arg2: # single alias delete origin = lookup_alias(bot.dbQuery, arg2) if origin: bot.dbQuery('''DELETE FROM alias WHERE alias = ?;''', (arg2,)) return bot.say("Alias (%s) for (%s) removed." % (arg2, origin)) else: return bot.say("Alias (%s) not found." % arg2) else: # show help for del return bot.say(functionHelp(alias, "~del")) elif arg1 and arg2: if arg3: arg2 += arg3 #binding a new alias if arg2.lower() == "me": return bot.say("But you are already yourself.") source = USERS_MODULE.get_username(bot, arg1, source=event.nick, _inalias=True) if not source: return bot.say("(%s) not seen before." % arg1) # Query target_user first so we can display error messages in sane order. target_user = USERS_MODULE._get_username(bot.dbQuery, arg2) if source == target_user: return bot.say("But %s is already %s." % (arg1, arg2)) target = lookup_alias(bot.dbQuery, arg2) # check target if target: #alias already in use by nnick return bot.say("Alias already in use by (%s)" % target) # check if target is an existing/seen user. # If it is, it means we are probably applying a user as an alias (remove old user in that case) # in this case we are going to remove target user and execute all observers to user's rename plans. target = target_user if source == target: return bot.say("But %s is already %s." % (arg1, arg2)) # see comments just above if target: USERS_MODULE._rename_user(bot.network, target, source) # find all groups that alias is a part of, and change membership to use "user" (source) bot.dbQuery('''UPDATE aliasgrp SET user=? WHERE user = ?;''', (source, arg2)) # add origin mapping so that origins can't get aliased # this will get called everytime but it's more messy if you check alias independently of alias, no big deal if add_alias(bot.dbQuery, source, source) # REPLACEing everytime. add_alias(bot.dbQuery, source, arg2) return bot.say("Added (%s) to (%s)" % (arg2, source)) elif arg1: #querying an alias nick = lookup_alias(bot.dbQuery, arg1) if nick: aliases = alias_list(bot.dbQuery, nick) if aliases: msg = "Aliases for (%s): %%s" % arg1 title = "Aliases for (%s)" % arg1 return pastehelper(bot, msg, items=aliases, altmsg="%s", title=title) # unknown alias or no aliases: return bot.say("No aliases for (%s)" % arg1) # if none of the above, show help bot.say(functionHelp(alias)) return