X-Git-Url: https://jfr.im/git/erebus.git/blobdiff_plain/36f91d215514e5f06e1d811e2a24b6ae5a9271f1..c3ff1486929923f394c219e6999df301a5a5bf04:/modules/userinfo.py diff --git a/modules/userinfo.py b/modules/userinfo.py index 29eb59f..9604b8b 100644 --- a/modules/userinfo.py +++ b/modules/userinfo.py @@ -1,13 +1,15 @@ # Erebus IRC bot - Author: Erebus Team -# trivia module +# vim: fileencoding=utf-8 +# userinfo module # This file is released into the public domain; see http://unlicense.org/ # module info modinfo = { 'author': 'Erebus Team', 'license': 'public domain', - 'compatible': [1], # compatible module API versions - 'depends': [], # other modules required to work properly? + 'compatible': [0], + 'depends': [], + 'softdeps': ['help'], } # preamble @@ -19,55 +21,90 @@ def modstart(parent_arg, *args, **kwargs): gotParent() return lib.modstart(parent, *args, **kwargs) def modstop(*args, **kwargs): - closeshop() + savedb() return lib.modstop(*args, **kwargs) # module code -import json +import json, sys +if sys.version_info.major >= 3: + import builtins as __builtin__ + stringbase = str +else: + import __builtin__ + stringbase = basestring #setup def gotParent(): global jsonfile, db jsonfile = parent.cfg.get('userinfo', 'jsonpath', default="./modules/userinfo.json") - db = json.load(open(jsonfile, "r")) -def closeshop(): - if json is not None and json.dump is not None: + try: + db = json.load(open(jsonfile, "r")) + except: + db = {} +def savedb(): + if json is not None and json.dump is not None and db != {}: json.dump(db, open(jsonfile, "w"))#, indent=4, separators=(',', ': ')) #functions -def getauth(thing): +def _getauth(thing): if isinstance(thing, parent.User): if thing.auth is not None: return "#"+thing.auth - elif isinstance(thing, basestring): - if thing[0] == "#": + elif isinstance(thing, stringbase): + if thing.startswith("#"): return thing else: - if parent.user(thing).auth is not None: + u = parent.user(thing, create=False) + if u is not None and u.auth is not None: return "#"+parent.user(thing).auth return None +def keys(user): + return list(__builtin__.set(list(db.get(_getauth(user), {}).keys()) + list(db.get(str(user).lower(), {}).keys()))) #list-to-set-to-list to remove duplicates def has(user, key): + key = key.lower() return ( - key in db.get(getauth(user), {}) or + key in db.get(_getauth(user), {}) or key in db.get(str(user).lower(), {}) ) def get(user, key, default=None): + key = key.lower() return ( - db.get(getauth(user), {}).get(key, - db.get(str(user).lower(), {}).get(key, - default + db.get(_getauth(user), {}). #try to get the auth + get(key, #try to get the info-key by auth + db.get(str(user).lower(), {}). #fallback to using the nick + get(key, #and try to get the info-key from that + default #otherwise throw out whatever default ))) def set(user, key, value): - if getauth(user) is not None: db.setdefault(getauth(user), {})[key] = value - db.setdefault(str(user).lower(), {})[key] = value + key = key.lower() + if _getauth(user) is not None: + db.setdefault(_getauth(user), {})[key] = value #use auth if we can + db.setdefault(str(user).lower(), {})[key] = value #but set nick too +def delete(user, key): + key = key.lower() + auth = _getauth(user) + if auth is not None and auth in db and key in db[auth]: + del db[auth][key] + target = str(user).lower() + if target in db and key in db[target]: + del db[target][key] #commands -@lib.hook('get', needchan=False) -def cmd_get(bot, user, chan, realtarget, *args): - if realtarget == chan.name: replyto = chan - else: replyto = user +@lib.hook(needchan=False, wantchan=True) +@lib.help("[]", "lists info items known about someone", " may be a nick, or an auth in format '#auth'", "it defaults to yourself") +def getitems(bot, user, chan, realtarget, *args): + if len(args) > 0: + target = args[0] + else: + target = user + + return "%(target)s has the following info items: %(items)s" % {'target':target,'items':(', '.join(keys(target)))} +@lib.hook(needchan=False, wantchan=True) +@lib.help("[] ", "gets an info item about someone", " may be a nick, or an auth in format '#auth'", "it defaults to yourself") +@lib.argsGE(1) +def getinfo(bot, user, chan, realtarget, *args): if len(args) > 1: target = args[0] item = args[1] @@ -77,18 +114,38 @@ def cmd_get(bot, user, chan, realtarget, *args): value = get(target, item, None) if value is None: - bot.msg(replyto, "%(user)s: %(item)s on %(target)s is not set." % {'user':user,'item':item,'target':target}) + return "%(item)s on %(target)s is not set." % {'item':item,'target':target} else: - bot.msg(replyto, "%(user)s: %(item)s on %(target)s: %(value)s" % {'user':user,'item':item,'target':target,'value':value}) + return "%(item)s on %(target)s: %(value)s" % {'item':item,'target':target,'value':value} -@lib.hook('set', needchan=False) +@lib.hook(needchan=False) +@lib.help(" ", "sets an info item about you") @lib.argsGE(2) -def cmd_set(bot, user, chan, realtarget, *args): +def setinfo(bot, user, chan, realtarget, *args): set(user, args[0], ' '.join(args[1:])) + savedb() bot.msg(user, "Done.") -@lib.hook('oset', glevel=lib.STAFF, needchan=False) +@lib.hook(needchan=False) +@lib.help("", "deletes an info item about you") +@lib.argsEQ(1) +def delinfo(bot, user, chan, realtarget, *args): + delete(user, args[0]) + savedb() + bot.msg(user, "Done.") + +@lib.hook(glevel=lib.ADMIN, needchan=False) +@lib.help(" ", "sets an info item about someone else", " may be a nick, or an auth in format '#auth'") @lib.argsGE(3) -def cmd_oset(bot, user, chan, realtarget, *args): +def osetinfo(bot, user, chan, realtarget, *args): set(args[0], args[1], ' '.join(args[2:])) + savedb() + bot.msg(user, "Done.") + +@lib.hook(glevel=lib.STAFF, needchan=False) +@lib.help(" ", "deletes an info item about someone else", " may be a nick, or an auth in format '#auth'") +@lib.argsEQ(2) +def odelinfo(bot, user, chan, realtarget, *args): + delete(args[0], args[1]) + savedb() bot.msg(user, "Done.")