]> jfr.im git - erebus.git/blame_incremental - modules/userinfo.py
control - add aliases
[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': [1,2],
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 closeshop()
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 closeshop():
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 return (
58 key in db.get(getauth(user), {}) or
59 key in db.get(str(user).lower(), {})
60 )
61def _get(user, key, default=None):
62 return (
63 db.get(getauth(user), {}). #try to get the auth
64 get(key, #try to get the info-key by auth
65 db.get(str(user).lower(), {}). #fallback to using the nick
66 get(key, #and try to get the info-key from that
67 default #otherwise throw out whatever default
68 )))
69def _set(user, key, value):
70 if getauth(user) is not None:
71 db.setdefault(getauth(user), {})[key] = value #use auth if we can
72 db.setdefault(str(user).lower(), {})[key] = value #but set nick too
73
74#commands
75@lib.hook(needchan=False)
76@lib.help("[<target>]", "lists info items known about someone", "<target> may be a nick, or an auth in format '#auth'", "it defaults to yourself")
77def getitems(bot, user, chan, realtarget, *args):
78 if chan is not None and realtarget == chan.name: replyto = chan
79 else: replyto = user
80
81 if len(args) > 0:
82 target = args[0]
83 else:
84 target = user
85
86 bot.msg(replyto, "%(user)s: %(target)s has the following info items: %(items)s" % {'user':user,'target':target,'items':(', '.join(_keys(target)))})
87
88@lib.hook(needchan=False)
89@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")
90@lib.argsGE(1)
91def getinfo(bot, user, chan, realtarget, *args):
92 if chan is not None and realtarget == chan.name: replyto = chan
93 else: replyto = user
94
95 if len(args) > 1:
96 target = args[0]
97 item = args[1]
98 else:
99 target = user
100 item = args[0]
101
102 value = _get(target, item, None)
103 if value is None:
104 bot.msg(replyto, "%(user)s: %(item)s on %(target)s is not set." % {'user':user,'item':item,'target':target})
105 else:
106 bot.msg(replyto, "%(user)s: %(item)s on %(target)s: %(value)s" % {'user':user,'item':item,'target':target,'value':value})
107
108@lib.hook(needchan=False)
109@lib.help("<item> <value>", "sets an info item about you")
110@lib.argsGE(2)
111def setinfo(bot, user, chan, realtarget, *args):
112 _set(user, args[0], ' '.join(args[1:]))
113 closeshop()
114 bot.msg(user, "Done.")
115
116@lib.hook(glevel=lib.STAFF, needchan=False)
117@lib.help("<target> <item> <value>", "sets an info item about someone else", "<target> may be a nick, or an auth in format '#auth'")
118@lib.argsGE(3)
119def osetinfo(bot, user, chan, realtarget, *args):
120 _set(args[0], args[1], ' '.join(args[2:]))
121 closeshop()
122 bot.msg(user, "Done.")