]>
Commit | Line | Data |
---|---|---|
1 | # Erebus IRC bot - Author: Erebus Team | |
2 | # vim: fileencoding=utf-8 | |
3 | # userinfo module | |
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', | |
10 | 'compatible': [0], | |
11 | 'depends': [], | |
12 | 'softdeps': ['help'], | |
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): | |
24 | savedb() | |
25 | return lib.modstop(*args, **kwargs) | |
26 | ||
27 | # module code | |
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 | |
35 | ||
36 | #setup | |
37 | def gotParent(): | |
38 | global jsonfile, db | |
39 | jsonfile = parent.cfg.get('userinfo', 'jsonpath', default="./modules/userinfo.json") | |
40 | try: | |
41 | db = json.load(open(jsonfile, "r")) | |
42 | except: | |
43 | db = {} | |
44 | def savedb(): | |
45 | if json is not None and json.dump is not None and db != {}: | |
46 | json.dump(db, open(jsonfile, "w"))#, indent=4, separators=(',', ': ')) | |
47 | ||
48 | #functions | |
49 | def _getauth(thing): | |
50 | if isinstance(thing, parent.User): | |
51 | if thing.auth is not None: | |
52 | return "#"+thing.auth | |
53 | elif isinstance(thing, stringbase): | |
54 | if thing.startswith("#"): | |
55 | return thing | |
56 | else: | |
57 | u = parent.user(thing, create=False) | |
58 | if u is not None and u.auth is not None: | |
59 | return "#"+parent.user(thing).auth | |
60 | return None | |
61 | ||
62 | def keys(user): | |
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 | |
64 | def has(user, key): | |
65 | key = key.lower() | |
66 | return ( | |
67 | key in db.get(_getauth(user), {}) or | |
68 | key in db.get(str(user).lower(), {}) | |
69 | ) | |
70 | def get(user, key, default=None): | |
71 | key = key.lower() | |
72 | return ( | |
73 | db.get(_getauth(user), {}). #try to get the auth | |
74 | get(key, #try to get the info-key by auth | |
75 | db.get(str(user).lower(), {}). #fallback to using the nick | |
76 | get(key, #and try to get the info-key from that | |
77 | default #otherwise throw out whatever default | |
78 | ))) | |
79 | def set(user, key, value): | |
80 | key = key.lower() | |
81 | if _getauth(user) is not None: | |
82 | db.setdefault(_getauth(user), {})[key] = value #use auth if we can | |
83 | db.setdefault(str(user).lower(), {})[key] = value #but set nick too | |
84 | def delete(user, key): | |
85 | key = key.lower() | |
86 | auth = _getauth(user) | |
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] | |
92 | ||
93 | #commands | |
94 | @lib.hook(needchan=False, wantchan=True) | |
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): | |
97 | if len(args) > 0: | |
98 | target = args[0] | |
99 | else: | |
100 | target = user | |
101 | ||
102 | return "%(target)s has the following info items: %(items)s" % {'target':target,'items':(', '.join(keys(target)))} | |
103 | ||
104 | @lib.hook(needchan=False, wantchan=True) | |
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") | |
106 | @lib.argsGE(1) | |
107 | def getinfo(bot, user, chan, realtarget, *args): | |
108 | if len(args) > 1: | |
109 | target = args[0] | |
110 | item = args[1] | |
111 | else: | |
112 | target = user | |
113 | item = args[0] | |
114 | ||
115 | value = get(target, item, None) | |
116 | if value is None: | |
117 | return "%(item)s on %(target)s is not set." % {'item':item,'target':target} | |
118 | else: | |
119 | return "%(item)s on %(target)s: %(value)s" % {'item':item,'target':target,'value':value} | |
120 | ||
121 | @lib.hook(needchan=False) | |
122 | @lib.help("<item> <value>", "sets an info item about you") | |
123 | @lib.argsGE(2) | |
124 | def setinfo(bot, user, chan, realtarget, *args): | |
125 | set(user, args[0], ' '.join(args[1:])) | |
126 | savedb() | |
127 | bot.msg(user, "Done.") | |
128 | ||
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): | |
133 | delete(user, args[0]) | |
134 | savedb() | |
135 | bot.msg(user, "Done.") | |
136 | ||
137 | @lib.hook(glevel=lib.ADMIN, needchan=False) | |
138 | @lib.help("<target> <item> <value>", "sets an info item about someone else", "<target> may be a nick, or an auth in format '#auth'") | |
139 | @lib.argsGE(3) | |
140 | def osetinfo(bot, user, chan, realtarget, *args): | |
141 | set(args[0], args[1], ' '.join(args[2:])) | |
142 | savedb() | |
143 | bot.msg(user, "Done.") | |
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): | |
149 | delete(args[0], args[1]) | |
150 | savedb() | |
151 | bot.msg(user, "Done.") |