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