]> jfr.im git - erebus.git/blame - modules/userinfo.py
control - add aliases
[erebus.git] / modules / userinfo.py
CommitLineData
36f91d21 1# Erebus IRC bot - Author: Erebus Team
a62d0d18 2# userinfo module
36f91d21 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',
a62d0d18 9 'compatible': [1,2],
10 'depends': [],
11 'softdeps': ['help'],
36f91d21 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")
983cd789 33 try:
34 db = json.load(open(jsonfile, "r"))
35 except:
36 db = {}
36f91d21 37def closeshop():
230c1654 38 if json is not None and json.dump is not None and db != {}:
36f91d21 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):
fd07173d 47 if thing.startswith("#"):
36f91d21 48 return thing
49 else:
50 if parent.user(thing).auth is not None:
51 return "#"+parent.user(thing).auth
52 return None
53
230c1654 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
fb20be7c 56def _has(user, key):
36f91d21 57 return (
58 key in db.get(getauth(user), {}) or
59 key in db.get(str(user).lower(), {})
60 )
fb20be7c 61def _get(user, key, default=None):
36f91d21 62 return (
fb20be7c 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
230c1654 66 get(key, #and try to get the info-key from that
67 default #otherwise throw out whatever default
36f91d21 68 )))
fb20be7c 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
36f91d21 73
74#commands
fb20be7c 75@lib.hook(needchan=False)
230c1654 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")
5f03d045 90@lib.argsGE(1)
fb20be7c 91def getinfo(bot, user, chan, realtarget, *args):
b79721ee 92 if chan is not None and realtarget == chan.name: replyto = chan
36f91d21 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
fb20be7c 102 value = _get(target, item, None)
36f91d21 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
fb20be7c 108@lib.hook(needchan=False)
5f03d045 109@lib.help("<item> <value>", "sets an info item about you")
36f91d21 110@lib.argsGE(2)
fb20be7c 111def setinfo(bot, user, chan, realtarget, *args):
112 _set(user, args[0], ' '.join(args[1:]))
230c1654 113 closeshop()
36f91d21 114 bot.msg(user, "Done.")
115
fb20be7c 116@lib.hook(glevel=lib.STAFF, needchan=False)
230c1654 117@lib.help("<target> <item> <value>", "sets an info item about someone else", "<target> may be a nick, or an auth in format '#auth'")
36f91d21 118@lib.argsGE(3)
fb20be7c 119def osetinfo(bot, user, chan, realtarget, *args):
120 _set(args[0], args[1], ' '.join(args[2:]))
230c1654 121 closeshop()
36f91d21 122 bot.msg(user, "Done.")