]> jfr.im git - erebus.git/blame_incremental - modules/userinfo.py
stafflist - add online nicks
[erebus.git] / modules / userinfo.py
... / ...
CommitLineData
1# Erebus IRC bot - Author: Erebus Team
2# userinfo module
3# This file is released into the public domain; see http://unlicense.org/
4
5# module info
6modinfo = {
7 'author': 'Erebus Team',
8 'license': 'public domain',
9 'compatible': [0],
10 'depends': [],
11 'softdeps': ['help'],
12}
13
14# preamble
15import modlib
16lib = modlib.modlib(__name__)
17def modstart(parent_arg, *args, **kwargs):
18 global parent
19 parent = parent_arg
20 gotParent()
21 return lib.modstart(parent, *args, **kwargs)
22def modstop(*args, **kwargs):
23 savedb()
24 return lib.modstop(*args, **kwargs)
25
26# module code
27import json
28
29#setup
30def gotParent():
31 global jsonfile, db
32 jsonfile = parent.cfg.get('userinfo', 'jsonpath', default="./modules/userinfo.json")
33 try:
34 db = json.load(open(jsonfile, "r"))
35 except:
36 db = {}
37def savedb():
38 if json is not None and json.dump is not None and db != {}:
39 json.dump(db, open(jsonfile, "w"))#, indent=4, separators=(',', ': '))
40
41#functions
42def _getauth(thing):
43 if isinstance(thing, parent.User):
44 if thing.auth is not None:
45 return "#"+thing.auth
46 elif isinstance(thing, basestring):
47 if thing.startswith("#"):
48 return thing
49 else:
50 if parent.user(thing).auth is not None:
51 return "#"+parent.user(thing).auth
52 return None
53
54def _keys(user):
55 return list(set(db.get(_getauth(user), {}).keys() + db.get(str(user).lower(), {}).keys())) #list-to-set-to-list to remove duplicates
56def _has(user, key):
57 key = key.lower()
58 return (
59 key in db.get(_getauth(user), {}) or
60 key in db.get(str(user).lower(), {})
61 )
62def _get(user, key, default=None):
63 key = key.lower()
64 return (
65 db.get(_getauth(user), {}). #try to get the auth
66 get(key, #try to get the info-key by auth
67 db.get(str(user).lower(), {}). #fallback to using the nick
68 get(key, #and try to get the info-key from that
69 default #otherwise throw out whatever default
70 )))
71def _set(user, key, value):
72 key = key.lower()
73 if _getauth(user) is not None:
74 db.setdefault(_getauth(user), {})[key] = value #use auth if we can
75 db.setdefault(str(user).lower(), {})[key] = value #but set nick too
76def _del(user, key):
77 key = key.lower()
78 auth = _getauth(user)
79 if auth is not None and auth in db and key in db[auth]:
80 del db[auth][key]
81 target = str(user).lower()
82 if target in db and key in db[target]:
83 del db[target][key]
84
85#commands
86@lib.hook(needchan=False, wantchan=True)
87@lib.help("[<target>]", "lists info items known about someone", "<target> may be a nick, or an auth in format '#auth'", "it defaults to yourself")
88def getitems(bot, user, chan, realtarget, *args):
89 if chan is not None: replyto = chan
90 else: replyto = user
91
92 if len(args) > 0:
93 target = args[0]
94 else:
95 target = user
96
97 bot.msg(replyto, "%(user)s: %(target)s has the following info items: %(items)s" % {'user':user,'target':target,'items':(', '.join(_keys(target)))})
98
99@lib.hook(needchan=False, wantchan=True)
100@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")
101@lib.argsGE(1)
102def getinfo(bot, user, chan, realtarget, *args):
103 if chan is not None: replyto = chan
104 else: replyto = user
105
106 if len(args) > 1:
107 target = args[0]
108 item = args[1]
109 else:
110 target = user
111 item = args[0]
112
113 value = _get(target, item, None)
114 if value is None:
115 bot.msg(replyto, "%(user)s: %(item)s on %(target)s is not set." % {'user':user,'item':item,'target':target})
116 else:
117 bot.msg(replyto, "%(user)s: %(item)s on %(target)s: %(value)s" % {'user':user,'item':item,'target':target,'value':value})
118
119@lib.hook(needchan=False)
120@lib.help("<item> <value>", "sets an info item about you")
121@lib.argsGE(2)
122def setinfo(bot, user, chan, realtarget, *args):
123 _set(user, args[0], ' '.join(args[1:]))
124 savedb()
125 bot.msg(user, "Done.")
126
127@lib.hook(needchan=False)
128@lib.help("<item>", "deletes an info item about you")
129@lib.argsEQ(1)
130def delinfo(bot, user, chan, realtarget, *args):
131 _del(user, args[0])
132 savedb()
133 bot.msg(user, "Done.")
134
135@lib.hook(glevel=lib.ADMIN, needchan=False)
136@lib.help("<target> <item> <value>", "sets an info item about someone else", "<target> may be a nick, or an auth in format '#auth'")
137@lib.argsGE(3)
138def osetinfo(bot, user, chan, realtarget, *args):
139 _set(args[0], args[1], ' '.join(args[2:]))
140 savedb()
141 bot.msg(user, "Done.")
142
143@lib.hook(glevel=lib.STAFF, needchan=False)
144@lib.help("<target> <item>", "deletes an info item about someone else", "<target> may be a nick, or an auth in format '#auth'")
145@lib.argsEQ(2)
146def odelinfo(bot, user, chan, realtarget, *args):
147 _del(args[0], args[1])
148 savedb()
149 bot.msg(user, "Done.")