diff --git a/admin.py b/admin.py index 7b14aec..58d4ad8 100755 --- a/admin.py +++ b/admin.py @@ -2,19 +2,19 @@ """ admin.py - Phenny Admin Module Copyright 2008-9, Sean B. Palmer, inamidst.com -Modified by Sfan5 2013 +Modified by sfan5 2013 Licensed under the Eiffel Forum License 2. http://inamidst.com/phenny/ """ -def join(phenny, input): +def join(phenny, input): """Join the specified channel. This is an admin-only command.""" # Can only be done in privmsg by an admin if input.sender.startswith('#'): return - if input.admin: + if input.admin: channel, key = input.group(1), input.group(2) - if not key: + if not key: phenny.write(['JOIN'], channel) else: phenny.write(['JOIN', channel, key]) join.rule = r'\!join (#\S+)(?: *(\S+))?' @@ -22,11 +22,11 @@ join.rule = r'\!join (#\S+)(?: *(\S+))?' join.priority = 'low' join.example = '.join #example or .join #example key' -def part(phenny, input): +def part(phenny, input): """Part the specified channel. This is an admin-only command.""" # Can only be done in privmsg by an admin if input.sender.startswith('#'): return - if input.admin: + if input.admin: if ' ' in input.group(2): arg = input.group(2).split(" ") arg2 = ' '.join(arg[1:]) @@ -38,11 +38,11 @@ part.commands = ['part'] part.priority = 'low' part.example = '.part #example' -def quit(phenny, input): +def quit(phenny, input): """Quit from the server. This is an owner-only command.""" # Can only be done in privmsg by the owner if input.sender.startswith('#'): return - if input.owner: + if input.owner: phenny.write(['QUIT']) __import__('os')._exit(0) quit.commands = ['quit'] @@ -54,20 +54,20 @@ def quit2(phenny, input): quit2.rule = ('$nick', 'quit') quit2.priority = 'low' -def msg(phenny, input): +def msg(phenny, input): # Can only be done in privmsg by an admin if input.sender.startswith('#'): return a, b = input.group(2), input.group(3) if (not a) or (not b): return - if input.admin: + if input.admin: phenny.msg(a, b) msg.rule = (['msg'], r'(#?\S+) (.+)') msg.priority = 'low' -def me(phenny, input): +def me(phenny, input): # Can only be done in privmsg by an admin if input.sender.startswith('#'): return - if input.admin: + if input.admin: msg = '\x01ACTION %s\x01' % input.group(3) phenny.msg(input.group(2) or input.sender, msg) me.rule = (['me'], r'(#?\S+) (.+)') @@ -79,5 +79,5 @@ def py(phenny, input): py.commands = ['py'] py.priority = 'high' -if __name__ == '__main__': +if __name__ == '__main__': print __doc__.strip() diff --git a/antiabuse.py b/antiabuse.py index c3c7a05..342a33b 100755 --- a/antiabuse.py +++ b/antiabuse.py @@ -10,14 +10,49 @@ antiabuse["ignorelist"] = [] antiabuse["cooldown_l"] = {} antiabuse["cooldown"] = 3 # seconds +def api_ignore(mask): + antiabuse["ignorelist"].append(mask) + db = sqlite3.connect("antiabuse.sqlite") + c = db.cursor() + c.execute("INSERT INTO ignore (nick) VALUES (?)", (mask,)) + c.close() + db.commit() + db.close() + +def api_unignore(mask): + if not mask in antiabuse["ignorelist"]: + return + antiabuse['ignorelist'].remove(mask) + db = sqlite3.connect("antiabuse.sqlite") + c = db.cursor() + c.execute("DELETE FROM ignore WHERE nick = ?", (mask,)) + c.close() + db.commit() + db.close() + +def api_get_ignorelist(): + return antiabuse["ignorelist"] + +class SomeObject(object): + pass + +antiabuse_api = SomeObject() +antiabuse_api.ignore = api_ignore +antiabuse_api.unignore = api_unignore +antiabuse_api.get_ignorelsit = api_get_ignorelist + +_export = { + 'antiabuse': antiabuse_api, +} + def aa_hook(phenny, input, func): if input.admin or input.owner: - return False + return True # Ignore list for entry in antiabuse["ignorelist"]: if phenny.match_hostmask(entry, input.hostmask): - return True # abort command + return False # abort command # Cooldown if input.nick in antiabuse["cooldown_l"]: @@ -26,10 +61,10 @@ def aa_hook(phenny, input, func): ot = 0 antiabuse["cooldown_l"][input.nick] = time.time() if antiabuse["cooldown_l"][input.nick] - antiabuse["cooldown"] < ot: - return True # abort command + return False # abort command pass - return False + return True aa_hook.hook = True @@ -49,13 +84,7 @@ def ignore(phenny, input): if not input.admin: return arg = hmasktrans(input.group(2).strip()) - antiabuse["ignorelist"].append(arg) - db = sqlite3.connect("antiabuse.sqlite") - c = db.cursor() - c.execute("INSERT INTO ignore (nick) VALUES (?)", (arg,)) - c.close() - db.commit() - db.close() + api_ignore(arg) phenny.reply("'%s' added to ignore list." % arg) ignore.commands = ['ignore'] @@ -65,15 +94,7 @@ def unignore(phenny, input): if not input.admin: return arg = hmasktrans(input.group(2).strip()) - if not arg in antiabuse["ignorelist"]: - return - antiabuse['ignorelist'].remove(arg) - db = sqlite3.connect("antiabuse.sqlite") - c = db.cursor() - c.execute("DELETE FROM ignore WHERE nick = ?", (arg,)) - c.close() - db.commit() - db.close() + api_unignore(arg) phenny.reply("'%s' removed from ignore list." % arg) unignore.commands = ['unignore'] @@ -82,7 +103,10 @@ unignore.priority = 'high' def listignore(phenny, input): if not input.admin: return - phenny.reply(', '.join(antiabuse['ignorelist'])) + s = ', '.join(antiabuse['ignorelist']) + if s == "": + s = "Ignore list empty." + phenny.reply(s) listignore.commands = ['listignore'] listignore.priority = 'high' diff --git a/calc.py b/calc.py index 3e7faca..bfcdbc1 100755 --- a/calc.py +++ b/calc.py @@ -8,6 +8,7 @@ Copyright 2014, sfan5 import math import random import struct +import multiprocessing class SomeObject(object): pass @@ -39,11 +40,28 @@ def c(phenny, input): q = input.group(2).encode('ascii', 'ignore') if '__' in q: return phenny.reply("Sorry, but no double underscores.") - print("[LOG]: %s calculated '%s'" % (input.nick, q)) - try: - phenny.say(repr(eval(q, {'__builtins__': env}, {}))) - except Exception as e: - phenny.say(type(e).__name__ + ": " + str(e)) + log.log("event", "%s calculated '%s'" % (log.fmt_user(input), q), phenny) + o = multiprocessing.Queue() + def get_result(o, q): + try: + o.put(repr(eval(q, {'__builtins__': env}, {}))) + except Exception as e: + o.put(type(e).__name__ + ": " + str(e)) + proc = multiprocessing.Process(target=get_result, args=(o,q)) + proc.start() + proc.join(2.0) + if proc.is_alive(): + proc.terminate() + if 'math.pow' in q or '**' in q: + phenny.reply("Kindly go fuck yourself!") + antiabuse.ignore("*!*" + input.hostmask[input.hostmask.find("@"):]) + log.log("action", "Auto-ignored %s for !c crash attempt" % log.fmt_user(input), phenny) + else: + phenny.reply("Took to long to calculate") + return + else: + phenny.say(o.get()) + c.commands = ['c'] c.example = '.c 5 + 3' diff --git a/devwiki.py b/devwiki.py index 541e9ea..419f2ca 100755 --- a/devwiki.py +++ b/devwiki.py @@ -131,12 +131,12 @@ def devwikipedia(term, language='en', last=False): term = term.decode('utf-8').encode('utf-8') return sentence + ' - ' + (devwikiuri % (term)) -def devwik(phenny, input): +def devwik(phenny, input): origterm = input.groups()[1] if not origterm: return phenny.say('Perhaps you meant "!devwik Zen"?') origterm = origterm.encode('utf-8') - print("[LOG]: %s queried Minetest Dev Wiki for '%s'" % (input.nick,origterm)) + log.log("event", "%s queried Devwiki for '%s'" % (log.fmt_user(input), origterm), phenny) term = urllib.unquote(origterm) language = 'en' diff --git a/search.py b/search.py index 105151b..81a5f70 100755 --- a/search.py +++ b/search.py @@ -10,8 +10,6 @@ http://inamidst.com/phenny/ import web, re -search_badwords = ["porn","p0rn","pr0n","pron","redtube","sex","pussy","weed","smoking","drug","penis","vagina"] # Thank KikaRz, LandMine and RagnarLaud for this - class Grab(web.urllib.URLopener): def __init__(self, *args): self.version = 'Mozilla/5.0 (MinetestBot)' @@ -61,12 +59,8 @@ def g(phenny, input): query = input.group(2) if not query: return phenny.reply('.g what?') - for bw in search_badwords: - if bw in query: - print("[LOG]: %s queried Google Result for '%s' | DENIED: Badword" % (input.nick,query)) - return phenny.reply("Gross!") query = query.encode('utf-8') - print("[LOG]: %s queried Google Result for '%s'" % (input.nick,query)) + log.log("%s searched Google for '%s'" % (log.fmt_user(input), query)) uri = google_search(query) if uri: phenny.reply(uri) @@ -81,12 +75,8 @@ def gc(phenny, input): if not input.group(2): return phenny.reply("No query term.") query = input.group(2).encode('utf-8') + log.log("%s searched Google for '%s'" % (log.fmt_user(input), query)) result = new_gc(query) - for bw in search_badwords: - if bw in query: - print("[LOG]: %s queried Google Result Number for '%s' | DENIED: Badword" % (input.nick,query)) - return phenny.reply("Gross!") - print("[LOG]: %s queried Google Result Number for '%s'" % (input.nick,query)) if result: phenny.say(query + ": " + result) else: phenny.reply("Sorry, couldn't get a result.") diff --git a/seen.py b/seen.py index 4186b15..e2c4d79 100755 --- a/seen.py +++ b/seen.py @@ -52,6 +52,27 @@ def pushupdate(sender, nick): updates.append((sender, ts, nick)) update_l.release() +def api_seen(nick): + dblock.acquire() + db = opendb() + c = db.cursor() + c.execute("SELECT channel, time FROM seen WHERE nick = ?", (nick,)) + r = c.fetchone() + c.close() + db.close() + dblock.release() + return r + +class SomeObject(object): + pass + +seen_api = SomeObject() +seen_api.seen = api_seen + +_export = { + 'seen': seen_api, +} + def seen(phenny, input): """seen - Reports when was last seen.""" nick = input.group(2) @@ -59,16 +80,9 @@ def seen(phenny, input): return phenny.reply("Need a nickname to search for...") nick = nick.lower() - print("[LOG]: %s queried Seen Result for %s" % (input.nick,nick)) + log.log("event", "%s queried Seen database for '%s'" % (log.fmt_user(input), nick), phenny) - dblock.acquire() - db = opendb() - c = db.cursor() - c.execute("SELECT channel, time FROM seen WHERE nick = ?", (nick,)) - r = c.fetchone() - c.close() - db.close() - dblock.release() + r = api_seen(nick) if r: channel, t = r[0], r[1] diff --git a/tell.py b/tell.py index c548335..13150b6 100755 --- a/tell.py +++ b/tell.py @@ -30,6 +30,22 @@ def tell_diskwr(): db.close() tell_pending = [] +def api_tell(teller, tellee, text): + d = (teller, tellee, text, int(time.mktime(time.gmtime()))) + tell_pending.append(("add", d)) + # We do not insert the entry into tell_list yet because we don't know the id it will have + tell_diskwr() # Write the change to disk + +class SomeObject(object): + pass + +tell_api = SomeObject() +tell_api.tell = api_tell + +_export = { + 'tell': tell_api, +} + def tell(phenny, input): arg = input.group(2) if not arg: @@ -46,10 +62,7 @@ def tell(phenny, input): elif target[-1] == ":": return phenny.reply("Do not put an : at the end of nickname") - d = (teller, target, text, int(time.mktime(time.gmtime()))) - tell_pending.append(("add", d)) - # We do not insert the entry into tell_list yet because we don't know the id it will have - tell_diskwr() # Write the change to disk + api_tell(teller, target, text) response = "I'll pass that on when %s is around" % target rand = random.random() diff --git a/twitter.py b/twitter.py index 8aa8370..fbe407b 100755 --- a/twitter.py +++ b/twitter.py @@ -76,7 +76,7 @@ def twitter(phenny, input): arg = arg.strip() if isinstance(arg, unicode): arg = arg.encode('utf-8') - print("[LOG]: %s queried Twitter for '%s'" % (input.nick,arg)) + log.log("%s queried Twitter for '%s'" % (log.fmt_user(input), arg)) if arg.isdigit(): phenny.say(id_tweet(arg)) elif r_username.match(arg): diff --git a/wiki.py b/wiki.py index bbe2a77..b0d9ced 100755 --- a/wiki.py +++ b/wiki.py @@ -136,7 +136,8 @@ def wik(phenny, input): if not origterm: return phenny.say('Perhaps you meant "!wik Zen"?') origterm = origterm.encode('utf-8') - print("[LOG]: %s queried Minetest Wiki for '%s'" % (input.nick,origterm)) + + log.log("event", "%s queried Wiki for '%s'" % (log.fmt_user(input), origterm), phenny) term = urllib.unquote(origterm) language = 'en'