]> jfr.im git - erebus.git/blob - modules/userinfo.py
65580f8690fa671b9dfaa08a85387e39a4f44d7e
[erebus.git] / modules / userinfo.py
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 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(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 chan is not None: replyto = chan
92 else: replyto = user
93
94 if len(args) > 0:
95 target = args[0]
96 else:
97 target = user
98
99 bot.msg(replyto, "%(user)s: %(target)s has the following info items: %(items)s" % {'user':user,'target':target,'items':(', '.join(keys(target)))})
100
101 @lib.hook(needchan=False, wantchan=True)
102 @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")
103 @lib.argsGE(1)
104 def getinfo(bot, user, chan, realtarget, *args):
105 if chan is not None: replyto = chan
106 else: replyto = user
107
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 bot.msg(replyto, "%(user)s: %(item)s on %(target)s is not set." % {'user':user,'item':item,'target':target})
118 else:
119 bot.msg(replyto, "%(user)s: %(item)s on %(target)s: %(value)s" % {'user':user,'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.")