def greeting(phenny, input): with lock: users.add(input.nick) with DatabaseCursor(phenny.greeting_db) as cursor: cursor.execute('select nick from opted_out') rows = cursor.fetchall() opted_out_of_m = [] for row in rows: opted_out_of_m.append(row[0]) if "[m]" in input.nick and not input.nick in opted_out_of_m: hint = "Please consider removing [m] from your IRC nick. See http://wiki.apertium.org/wiki/IRC/Matrix#Remove_.5Bm.5D_from_your_IRC_nick for details. Reply .dismiss to prevent this message from appearing again." phenny.msg(input.nick, input.nick + ": " + hint) if input.sender.casefold() not in phenny.config.greetings.keys(): return time.sleep(phenny.config.greet_delay) if input.nick not in users: return greetingmessage = phenny.config.greetings[input.sender.casefold()] greetingmessage = greetingmessage.replace("%name", input.nick) greetingmessage = greetingmessage.replace("%channel", input.sender) nick = input.nick.casefold() with DatabaseCursor(phenny.greeting_db) as cursor: cursor.execute("SELECT * FROM special_nicks WHERE nick = ?", (nick,)) try: phenny.msg(input.nick, input.nick + ": " + str(cursor.fetchone()[0])) return except TypeError: pass with DatabaseCursor(phenny.logger_db) as cursor: cursor.execute("SELECT * FROM lines_by_nick WHERE nick = ?", (nick,)) if cursor.fetchone() is None: if nick != phenny.config.nick.casefold(): if nick not in phenny.greeting_count: phenny.greeting_count[nick] = 0 help_text = "You will need to register your nick with NickServ. Type /msg NickServ HELP for information on getting started with this." phenny.msg(input.nick, help_text) phenny.proto.notice(input.nick, help_text) phenny.greeting_count[nick] += 1 if math.log2(phenny.greeting_count[nick]) % 1 == 0: phenny.msg(input.nick, greetingmessage)
def delete(phenny, nick): sqlite_data = { 'nick': nick } with DatabaseCursor(phenny.gci_db) as cursor: cursor.execute("DELETE FROM gci_data WHERE nick=:nick", sqlite_data)
def greeting_add(phenny, input): if not input.admin: phenny.reply("You have insufficient privelleges to use this command.") return if input.group(2) is None: phenny.reply("You haven't specified a name and message.") return elif len(input.group(2).split(" ")) < 2: phenny.reply("You haven't specified a message.") return sqlite_data = { 'channel': input.sender, 'nick': input.group(2).split(" ")[0].casefold(), 'message': input.group(2).split(" ", 1)[1] } with DatabaseCursor(phenny.greeting_db) as cursor: cursor.execute('''insert or replace into special_nicks (channel, nick, message) values(:channel, :nick, :message);''', sqlite_data) cursor.execute('update special_nicks set message=:message where channel=:channel and nick=:nick', sqlite_data) phenny.reply("Successfully added " + input.group(2).split(" ", 1)[0] + " to the special greetings list.")
def logger(phenny, input): sqlite_data = { 'channel': input.sender, 'nick': input.nick.casefold(), 'msg': input.group(1), 'chars': len(input.group(1)), } # format action messages if sqlite_data['msg'][:8] == '\x01ACTION ': sqlite_data['msg'] = '* {0} {1}'.format(sqlite_data['nick'], sqlite_data['msg'][8:-1]) with DatabaseCursor(phenny.logger_db) as cursor: cursor.execute( '''insert or replace into lines_by_nick (channel, nick, lines, characters, last_time, quote) values(:channel, :nick, coalesce((select lines from lines_by_nick where channel=:channel and nick=:nick) + 1, 1), coalesce((select characters from lines_by_nick where channel=:channel and nick=:nick) + :chars, :chars), CURRENT_TIMESTAMP, :msg );''', sqlite_data) cursor.execute( 'update lines_by_nick set quote=:msg where channel=:channel and nick=:nick', sqlite_data)
def add_messages(phenny, target, messages, tag=None): if not type(messages) is list: messages = [messages] if not target.startswith('#'): messages = list(map(lambda message: target + ': ' + message, messages)) messages = sum(map(lambda message: break_up(message), messages), []) if not tag: tag = calling_module() if len(messages) <= 2: for message in messages: phenny.msg(target, message) return phenny.msg(target, messages.pop(0)) phenny.msg( target, 'Type ".more" to view remaining {num} messages.'.format( num=len(messages))) target = target.casefold() with DatabaseCursor(phenny.more_db) as cursor: values = [(target, message, tag) for message in messages] cursor.executemany( "INSERT INTO more (target, message, tag) VALUES (?, ?, ?)", values)
def insert(phenny, nick, mentor_nick, code, all_time=0): sqlite_data = (nick, mentor_nick, code, all_time) with DatabaseCursor(phenny.gci_db) as cursor: cursor.execute( "INSERT INTO gci_data (nick, mentor_nick, code, all_time) VALUES (?, ?, ?, ?)", sqlite_data)
def update(phenny, nick, all_time): sqlite_data = { 'nick': nick, 'all_time': all_time, } with DatabaseCursor(phenny.gci_db) as cursor: cursor.execute("UPDATE gci_data SET all_time=:all_time WHERE nick=:nick", sqlite_data)
def select(phenny, nick): sqlite_data = { 'nick': nick } with DatabaseCursor(phenny.gci_db) as cursor: cursor.execute("SELECT mentor_nick, code, all_time FROM gci_data WHERE nick=:nick", sqlite_data) return cursor.fetchone()
def delete_all(phenny, target=None): with DatabaseCursor(phenny.more_db) as cursor: if target: target = target.casefold() cursor.execute("DELETE FROM more WHERE target=?", (target, )) else: cursor.execute("DELETE FROM more")
def count_more(phenny, target, tag=None): target = target.casefold() with DatabaseCursor(phenny.more_db) as cursor: if tag: cursor.execute("SELECT COUNT(*) FROM more WHERE target=? AND tag=?", (target, tag)) else: cursor.execute("SELECT COUNT(*) FROM more WHERE target=?", (target,)) return cursor.fetchone()[0]
def dismiss(phenny, input): try: with DatabaseCursor(phenny.greeting_db) as cursor: cursor.execute( 'insert into opted_out (nick) values (?)', (input.nick,) ) phenny.say('I won\'t tell you again.') except Exception as e: phenny.say('Sorry, an error occurred.') raise e
def greeting(phenny, input): with lock: users.add(input.nick) if "[m]" in input.nick: hint = "Consider removing [m] from your IRC nick! See http://wiki.apertium.org/wiki/IRC/Matrix#Remove_.5Bm.5D_from_your_IRC_nick for details." phenny.msg(input.nick, input.nick + ": " + hint) if input.sender.casefold() not in phenny.config.greetings.keys(): return time.sleep(phenny.config.greet_delay) if input.nick not in users: return greetingmessage = phenny.config.greetings[input.sender.casefold()] greetingmessage = greetingmessage.replace("%name", input.nick) greetingmessage = greetingmessage.replace("%channel", input.sender) nick = input.nick.casefold() with DatabaseCursor(phenny.greeting_db) as cursor: cursor.execute("SELECT * FROM special_nicks WHERE nick = ?", (nick,)) try: phenny.say(input.nick + ": " + str(cursor.fetchone()[0])) return except TypeError: pass with DatabaseCursor(phenny.logger_db) as cursor: cursor.execute("SELECT * FROM lines_by_nick WHERE nick = ?", (nick,)) if cursor.fetchone() is None: if nick != phenny.config.nick.casefold(): if nick not in phenny.greeting_count: phenny.greeting_count[nick] = 0 phenny.greeting_count[nick] += 1 if math.log2(phenny.greeting_count[nick]) % 1 == 0: phenny.say(greetingmessage)
def greeting_del(phenny, input): if not input.admin: phenny.reply("You have insufficient privelleges to use this command.") return if input.group(2) == None: phenny.reply ("You haven't specified a name.") return with DatabaseCursor(phenny.greeting_db) as cursor: cursor.execute("DELETE FROM special_nicks WHERE nick = ? AND channel = ?", (input.group(2).split(" ")[0].casefold(), input.sender)) phenny.reply("Successfully deleted " + input.group(2).split(" ", 1)[0] + " from the special greetings list.")
def get_more(phenny, target, count, tag=None): target = target.casefold() with DatabaseCursor(phenny.more_db) as cursor: if tag: cursor.execute("SELECT id, message, tag FROM more WHERE target=? AND tag=? ORDER BY id ASC LIMIT ?", (target, tag, count)) else: cursor.execute("SELECT id, message, tag FROM more WHERE target=? ORDER BY id ASC LIMIT ?", (target, count)) rows = cursor.fetchall() cursor.executemany("DELETE FROM more WHERE id=?", [(row[0],) for row in rows]) return [row[1:3] for row in rows]
def whoisset(phenny, input): '''.whoisset <github> <wiki> <timezone> <realname> <location> - set whois info; multiword values go in quotes; all values are optional; use x to omit a value; gci/gsoc mentors append name on gci tasks/gsoc to end of command; gci/gsoc admins append 'admin' to end of command; gci/gsoc admins/mentors, pls remember to rerun this command without gci/gsoc info after gci/gsoc ends''' try: text = shlex.split(input.group()) if len(text) < 6: phenny.say( 'usage: .whoisset <github> <wiki> <timezone> <realname> <location>' ) return with DatabaseCursor(phenny.whois_db) as cursor: make_table(cursor) is_admin = False is_mentor = False try: gci_status = text[6] if gci_status == 'admin': is_admin = True else: is_mentor = True except Exception: pass cursor.execute('''delete from users where nick = ?;''', (input.nick, )) cursor.execute( '''insert into users values (?, ?, ?, ?, ?, ?, ?, ?, ?);''', (input.nick, text_or_none(text[1]), text_or_none( text[2]), text_or_none(text[3]), (is_admin or is_mentor), (text[6] if is_mentor else None), is_admin, text_or_none(text[4]), text_or_none(text[5]))) cursor.close() phenny.say('OK, I recorded all that. Type `.whois ' + input.nick + '` to verify it.') except Exception as e: phenny.say('Sorry, an error occurred.') phenny.say( 'Say `.help whoisset` in private chat with me to see usage.') raise e
def whoisdrop(phenny, input): '''.whoisdrop <nick> - drop a record from the whois database (must be <nick> or a phenny admin)''' try: try: text = input.group().split(' ')[1] except IndexError: phenny.say('usage: .whoisdrop <nick>') return if input.nick.casefold() in caseless_list(phenny.config.admins) or input.nick.lower() is text.lower(): with DatabaseCursor(phenny.whois_db) as cursor: make_table(cursor) cursor.execute('''delete from users where nick = ?;''', (text,)) phenny.say('{} has been removed from the database.'.format(text)) else: phenny.say('You must be an admin to use this command.') except Exception as e: phenny.say('Sorry, an error occurred.') raise e
def whois(phenny, input): '''.whois <nick> - get info for registered nick; set data with .whoisset (see .help whoisset); admins or nick owners can drop with .whoisdrop <nick> ''' bynick = True try: if len(input.group().split(' ', 1)) < 2: phenny.say('usage: .whois <query>') return 0 text = input.group().split(' ', 1)[1] if text.lower() == phenny.nick.lower(): phenny.say( '{} (Wondrous Guardian of {}) | IRC (all timezones) | now'. format(phenny.nick, all_of(phenny.channels))) return 0 with DatabaseCursor(phenny.whois_db) as cursor: make_table(cursor) cursor.execute('select * from users') data = [Record(datum) for datum in cursor.fetchall()] user = None for record in data: if record.nick.lower() == text.lower(): user = record break if user == None: aliasmodule.loadAliases(phenny) aliases = aliasmodule.aliasGroupFor(text) for alias in aliases: for record in data: if record.nick.lower() == alias.lower(): user = record user.nick = text break if user == None: #can't find by nick; try by other things text = input.group().split(' ', 1)[1] results = [] for entry in data: e = [ text.lower() in record.lower() for record in [ entry.nick, entry.github, entry.wiki, entry.gciname, entry.realname ] if record is not None ] if True in e: results.append(entry) if results: user = results[0] else: try: seen_string = ' They were last seen {}.'.format( seen.seen(text, phenny)['ago']) except seen.NotSeenError: seen_string = '' phenny.say( 'Hmm... {} is not in the database. Apparently, they have not registered.{}' .format(text, seen_string)) return 0 if user.timezone is None: if user.location is None: locstring = '' else: locstring = ' | {}'.format(user.location) else: if user.location is None: locstring = ' | {}'.format(user.timezone) else: locstring = ' | {} ({})'.format(user.location, user.timezone) try: seen_string = ' | {}'.format(seen.seen(user.nick, phenny)['ago']) except seen.NotSeenError: seen_string = '' realnamestring = (' ({})'.format(user.realname) if user.realname is not None else '') phenny.say('{}{}{}{}'.format(user.nick, realnamestring, locstring, seen_string)) if user.isgci and not user.isadmin: say_username('GCI/GSOC', user.wiki, None, phenny) if user.wiki is not None: say_username('Apertium Wiki', user.wiki, 'http://wiki.apertium.org/wiki/User:{}', phenny) if user.github is not None: say_username('GitHub', user.github, 'https://github.com/{}', phenny) except Exception as e: phenny.say('Sorry, an error occurred.') raise e