X-Git-Url: https://jfr.im/git/erebus.git/blobdiff_plain/54c084bccc38b14298a744c348b43e92819b78a2..d85e99b6ef5372e26b9bd1aabf58e604afe2db53:/modules/userinfo.py diff --git a/modules/userinfo.py b/modules/userinfo.py index 6cb5149..c4281dd 100644 --- a/modules/userinfo.py +++ b/modules/userinfo.py @@ -1,4 +1,5 @@ # Erebus IRC bot - Author: Erebus Team +# vim: fileencoding=utf-8 # userinfo module # This file is released into the public domain; see http://unlicense.org/ @@ -6,7 +7,7 @@ modinfo = { 'author': 'Erebus Team', 'license': 'public domain', - 'compatible': [2], + 'compatible': [0], 'depends': [], 'softdeps': ['help'], } @@ -24,7 +25,13 @@ def modstop(*args, **kwargs): 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(): @@ -43,23 +50,24 @@ def _getauth(thing): if isinstance(thing, parent.User): if thing.auth is not None: return "#"+thing.auth - elif isinstance(thing, basestring): + 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(set(db.get(_getauth(user), {}).keys() + db.get(str(user).lower(), {}).keys())) #list-to-set-to-list to remove duplicates -def _has(user, key): +def keys(user): + return list(__builtin__.set(db.get(_getauth(user), {}).keys() + 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(str(user).lower(), {}) ) -def _get(user, key, default=None): +def get(user, key, default=None): key = key.lower() return ( db.get(_getauth(user), {}). #try to get the auth @@ -68,12 +76,12 @@ def _get(user, key, default=None): get(key, #and try to get the info-key from that default #otherwise throw out whatever default ))) -def _set(user, key, value): +def set(user, 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 _del(user, key): +def delete(user, key): key = key.lower() auth = _getauth(user) if auth is not None and auth in db and key in db[auth]: @@ -86,23 +94,17 @@ def _del(user, key): @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 chan is not None: replyto = chan - else: replyto = user - if len(args) > 0: target = args[0] else: target = user - bot.msg(replyto, "%(user)s: %(target)s has the following info items: %(items)s" % {'user':user,'target':target,'items':(', '.join(_keys(target)))}) + 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 chan is not None: replyto = chan - else: replyto = user - if len(args) > 1: target = args[0] item = args[1] @@ -110,17 +112,17 @@ def getinfo(bot, user, chan, realtarget, *args): target = user item = args[0] - value = _get(target, item, None) + 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(needchan=False) @lib.help(" ", "sets an info item about you") @lib.argsGE(2) def setinfo(bot, user, chan, realtarget, *args): - _set(user, args[0], ' '.join(args[1:])) + set(user, args[0], ' '.join(args[1:])) savedb() bot.msg(user, "Done.") @@ -128,7 +130,7 @@ def setinfo(bot, user, chan, realtarget, *args): @lib.help("", "deletes an info item about you") @lib.argsEQ(1) def delinfo(bot, user, chan, realtarget, *args): - _del(user, args[0]) + delete(user, args[0]) savedb() bot.msg(user, "Done.") @@ -136,7 +138,7 @@ def delinfo(bot, user, chan, realtarget, *args): @lib.help(" ", "sets an info item about someone else", " may be a nick, or an auth in format '#auth'") @lib.argsGE(3) def osetinfo(bot, user, chan, realtarget, *args): - _set(args[0], args[1], ' '.join(args[2:])) + set(args[0], args[1], ' '.join(args[2:])) savedb() bot.msg(user, "Done.") @@ -144,6 +146,6 @@ def osetinfo(bot, user, chan, realtarget, *args): @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): - _del(args[0], args[1]) + delete(args[0], args[1]) savedb() bot.msg(user, "Done.")