]> jfr.im git - erebus.git/blame_incremental - modules/userinfo.py
control - use new bot.reply return shortcut
[erebus.git] / modules / userinfo.py
... / ...
CommitLineData
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
7modinfo = {
8 'author': 'Erebus Team',
9 'license': 'public domain',
10 'compatible': [0],
11 'depends': [],
12 'softdeps': ['help'],
13}
14
15# preamble
16import modlib
17lib = modlib.modlib(__name__)
18def modstart(parent_arg, *args, **kwargs):
19 global parent
20 parent = parent_arg
21 gotParent()
22 return lib.modstart(parent, *args, **kwargs)
23def modstop(*args, **kwargs):
24 savedb()
25 return lib.modstop(*args, **kwargs)
26
27# module code
28import json
29
30#setup
31def 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 = {}
38def 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
43def _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
55def _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
57def _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 )
63def _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 )))
72def _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
77def _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")
89def 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)
103def 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)
123def 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)
131def 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)
139def 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)
147def odelinfo(bot, user, chan, realtarget, *args):
148 _del(args[0], args[1])
149 savedb()
150 bot.msg(user, "Done.")