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