]>
Commit | Line | Data |
---|---|---|
36f91d21 | 1 | # Erebus IRC bot - Author: Erebus Team |
4477123d | 2 | # vim: fileencoding=utf-8 |
a62d0d18 | 3 | # userinfo module |
36f91d21 | 4 | # This file is released into the public domain; see http://unlicense.org/ |
5 | ||
6 | # module info | |
7 | modinfo = { | |
8 | 'author': 'Erebus Team', | |
9 | 'license': 'public domain', | |
fa93b933 | 10 | 'compatible': [0], |
a62d0d18 | 11 | 'depends': [], |
12 | 'softdeps': ['help'], | |
36f91d21 | 13 | } |
14 | ||
15 | # preamble | |
16 | import modlib | |
17 | lib = modlib.modlib(__name__) | |
18 | def modstart(parent_arg, *args, **kwargs): | |
19 | global parent | |
20 | parent = parent_arg | |
21 | gotParent() | |
22 | return lib.modstart(parent, *args, **kwargs) | |
23 | def modstop(*args, **kwargs): | |
54c084bc | 24 | savedb() |
36f91d21 | 25 | return lib.modstop(*args, **kwargs) |
26 | ||
27 | # module code | |
d85e99b6 | 28 | import json, sys |
29 | if sys.version_info.major >= 3: | |
30 | import builtins as __builtin__ | |
31 | stringbase = str | |
32 | else: | |
33 | import __builtin__ | |
34 | stringbase = basestring | |
36f91d21 | 35 | |
36 | #setup | |
37 | def gotParent(): | |
38 | global jsonfile, db | |
39 | jsonfile = parent.cfg.get('userinfo', 'jsonpath', default="./modules/userinfo.json") | |
983cd789 | 40 | try: |
41 | db = json.load(open(jsonfile, "r")) | |
42 | except: | |
43 | db = {} | |
54c084bc | 44 | def savedb(): |
230c1654 | 45 | if json is not None and json.dump is not None and db != {}: |
36f91d21 | 46 | json.dump(db, open(jsonfile, "w"))#, indent=4, separators=(',', ': ')) |
47 | ||
48 | #functions | |
54c084bc | 49 | def _getauth(thing): |
36f91d21 | 50 | if isinstance(thing, parent.User): |
51 | if thing.auth is not None: | |
52 | return "#"+thing.auth | |
d85e99b6 | 53 | elif isinstance(thing, stringbase): |
fd07173d | 54 | if thing.startswith("#"): |
36f91d21 | 55 | return thing |
56 | else: | |
96dfdcd3 | 57 | u = parent.user(thing, create=False) |
58 | if u is not None and u.auth is not None: | |
36f91d21 | 59 | return "#"+parent.user(thing).auth |
60 | return None | |
61 | ||
aa582dd7 | 62 | def keys(user): |
c4e75347 | 63 | 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 |
aa582dd7 | 64 | def has(user, key): |
4b5f28dd | 65 | key = key.lower() |
36f91d21 | 66 | return ( |
54c084bc | 67 | key in db.get(_getauth(user), {}) or |
36f91d21 | 68 | key in db.get(str(user).lower(), {}) |
69 | ) | |
aa582dd7 | 70 | def get(user, key, default=None): |
4b5f28dd | 71 | key = key.lower() |
36f91d21 | 72 | return ( |
54c084bc | 73 | db.get(_getauth(user), {}). #try to get the auth |
fb20be7c | 74 | get(key, #try to get the info-key by auth |
75 | db.get(str(user).lower(), {}). #fallback to using the nick | |
230c1654 | 76 | get(key, #and try to get the info-key from that |
77 | default #otherwise throw out whatever default | |
36f91d21 | 78 | ))) |
aa582dd7 | 79 | def set(user, key, value): |
4b5f28dd | 80 | key = key.lower() |
54c084bc | 81 | if _getauth(user) is not None: |
82 | db.setdefault(_getauth(user), {})[key] = value #use auth if we can | |
fb20be7c | 83 | db.setdefault(str(user).lower(), {})[key] = value #but set nick too |
aa582dd7 | 84 | def delete(user, key): |
a0bd416f | 85 | key = key.lower() |
54c084bc | 86 | auth = _getauth(user) |
a0bd416f | 87 | if auth is not None and auth in db and key in db[auth]: |
88 | del db[auth][key] | |
89 | target = str(user).lower() | |
90 | if target in db and key in db[target]: | |
91 | del db[target][key] | |
36f91d21 | 92 | |
93 | #commands | |
f5aec865 | 94 | @lib.hook(needchan=False, wantchan=True) |
230c1654 | 95 | @lib.help("[<target>]", "lists info items known about someone", "<target> may be a nick, or an auth in format '#auth'", "it defaults to yourself") |
96 | def getitems(bot, user, chan, realtarget, *args): | |
230c1654 | 97 | if len(args) > 0: |
98 | target = args[0] | |
99 | else: | |
100 | target = user | |
101 | ||
c62b669b | 102 | return "%(target)s has the following info items: %(items)s" % {'target':target,'items':(', '.join(keys(target)))} |
230c1654 | 103 | |
f5aec865 | 104 | @lib.hook(needchan=False, wantchan=True) |
230c1654 | 105 | @lib.help("[<target>] <item>", "gets an info item about someone", "<target> may be a nick, or an auth in format '#auth'", "it defaults to yourself") |
5f03d045 | 106 | @lib.argsGE(1) |
fb20be7c | 107 | def getinfo(bot, user, chan, realtarget, *args): |
36f91d21 | 108 | if len(args) > 1: |
109 | target = args[0] | |
110 | item = args[1] | |
111 | else: | |
112 | target = user | |
113 | item = args[0] | |
114 | ||
aa582dd7 | 115 | value = get(target, item, None) |
36f91d21 | 116 | if value is None: |
c62b669b | 117 | return "%(item)s on %(target)s is not set." % {'item':item,'target':target} |
36f91d21 | 118 | else: |
c62b669b | 119 | return "%(item)s on %(target)s: %(value)s" % {'item':item,'target':target,'value':value} |
36f91d21 | 120 | |
fb20be7c | 121 | @lib.hook(needchan=False) |
5f03d045 | 122 | @lib.help("<item> <value>", "sets an info item about you") |
36f91d21 | 123 | @lib.argsGE(2) |
fb20be7c | 124 | def setinfo(bot, user, chan, realtarget, *args): |
aa582dd7 | 125 | set(user, args[0], ' '.join(args[1:])) |
54c084bc | 126 | savedb() |
36f91d21 | 127 | bot.msg(user, "Done.") |
128 | ||
a0bd416f | 129 | @lib.hook(needchan=False) |
130 | @lib.help("<item>", "deletes an info item about you") | |
131 | @lib.argsEQ(1) | |
132 | def delinfo(bot, user, chan, realtarget, *args): | |
aa582dd7 | 133 | delete(user, args[0]) |
54c084bc | 134 | savedb() |
a0bd416f | 135 | bot.msg(user, "Done.") |
136 | ||
137 | @lib.hook(glevel=lib.ADMIN, needchan=False) | |
230c1654 | 138 | @lib.help("<target> <item> <value>", "sets an info item about someone else", "<target> may be a nick, or an auth in format '#auth'") |
36f91d21 | 139 | @lib.argsGE(3) |
fb20be7c | 140 | def osetinfo(bot, user, chan, realtarget, *args): |
aa582dd7 | 141 | set(args[0], args[1], ' '.join(args[2:])) |
54c084bc | 142 | savedb() |
36f91d21 | 143 | bot.msg(user, "Done.") |
a0bd416f | 144 | |
145 | @lib.hook(glevel=lib.STAFF, needchan=False) | |
146 | @lib.help("<target> <item>", "deletes an info item about someone else", "<target> may be a nick, or an auth in format '#auth'") | |
147 | @lib.argsEQ(2) | |
148 | def odelinfo(bot, user, chan, realtarget, *args): | |
aa582dd7 | 149 | delete(args[0], args[1]) |
54c084bc | 150 | savedb() |
a0bd416f | 151 | bot.msg(user, "Done.") |