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