]>
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 | |
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 | if parent.user(thing).auth is not None: | |
52 | return "#"+parent.user(thing).auth | |
53 | return None | |
54 | ||
55 | def _keys(user): | |
56 | return list(set(db.get(_getauth(user), {}).keys() + db.get(str(user).lower(), {}).keys())) #list-to-set-to-list to remove duplicates | |
57 | def _has(user, key): | |
58 | key = key.lower() | |
59 | return ( | |
60 | key in db.get(_getauth(user), {}) or | |
61 | key in db.get(str(user).lower(), {}) | |
62 | ) | |
63 | def _get(user, key, default=None): | |
64 | key = key.lower() | |
65 | return ( | |
66 | db.get(_getauth(user), {}). #try to get the auth | |
67 | get(key, #try to get the info-key by auth | |
68 | db.get(str(user).lower(), {}). #fallback to using the nick | |
69 | get(key, #and try to get the info-key from that | |
70 | default #otherwise throw out whatever default | |
71 | ))) | |
72 | def _set(user, key, value): | |
73 | key = key.lower() | |
74 | if _getauth(user) is not None: | |
75 | db.setdefault(_getauth(user), {})[key] = value #use auth if we can | |
76 | db.setdefault(str(user).lower(), {})[key] = value #but set nick too | |
77 | def _del(user, key): | |
78 | key = key.lower() | |
79 | auth = _getauth(user) | |
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] | |
85 | ||
86 | #commands | |
87 | @lib.hook(needchan=False, wantchan=True) | |
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): | |
90 | if chan is not None: replyto = chan | |
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 | ||
100 | @lib.hook(needchan=False, wantchan=True) | |
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") | |
102 | @lib.argsGE(1) | |
103 | def getinfo(bot, user, chan, realtarget, *args): | |
104 | if chan is not None: replyto = chan | |
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 | ||
114 | value = _get(target, item, None) | |
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 | ||
120 | @lib.hook(needchan=False) | |
121 | @lib.help("<item> <value>", "sets an info item about you") | |
122 | @lib.argsGE(2) | |
123 | def setinfo(bot, user, chan, realtarget, *args): | |
124 | _set(user, args[0], ' '.join(args[1:])) | |
125 | savedb() | |
126 | bot.msg(user, "Done.") | |
127 | ||
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]) | |
133 | savedb() | |
134 | bot.msg(user, "Done.") | |
135 | ||
136 | @lib.hook(glevel=lib.ADMIN, needchan=False) | |
137 | @lib.help("<target> <item> <value>", "sets an info item about someone else", "<target> may be a nick, or an auth in format '#auth'") | |
138 | @lib.argsGE(3) | |
139 | def osetinfo(bot, user, chan, realtarget, *args): | |
140 | _set(args[0], args[1], ' '.join(args[2:])) | |
141 | savedb() | |
142 | bot.msg(user, "Done.") | |
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]) | |
149 | savedb() | |
150 | bot.msg(user, "Done.") |