]> jfr.im git - erebus.git/blame - modules/userinfo.py
modlib - fix typo in help() docstring
[erebus.git] / modules / userinfo.py
CommitLineData
36f91d21 1# Erebus IRC bot - Author: Erebus Team
2# trivia 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], # compatible module API versions
10 'depends': [], # other modules required to work properly?
11}
12
13# preamble
14import modlib
15lib = modlib.modlib(__name__)
16def modstart(parent_arg, *args, **kwargs):
17 global parent
18 parent = parent_arg
19 gotParent()
20 return lib.modstart(parent, *args, **kwargs)
21def modstop(*args, **kwargs):
22 closeshop()
23 return lib.modstop(*args, **kwargs)
24
25# module code
26import json
27
28#setup
29def gotParent():
30 global jsonfile, db
31 jsonfile = parent.cfg.get('userinfo', 'jsonpath', default="./modules/userinfo.json")
983cd789 32 try:
33 db = json.load(open(jsonfile, "r"))
34 except:
35 db = {}
36f91d21 36def closeshop():
37 if json is not None and json.dump is not None:
38 json.dump(db, open(jsonfile, "w"))#, indent=4, separators=(',', ': '))
39
40#functions
41def getauth(thing):
42 if isinstance(thing, parent.User):
43 if thing.auth is not None:
44 return "#"+thing.auth
45 elif isinstance(thing, basestring):
46 if thing[0] == "#":
47 return thing
48 else:
49 if parent.user(thing).auth is not None:
50 return "#"+parent.user(thing).auth
51 return None
52
fb20be7c 53def _has(user, key):
36f91d21 54 return (
55 key in db.get(getauth(user), {}) or
56 key in db.get(str(user).lower(), {})
57 )
fb20be7c 58def _get(user, key, default=None):
36f91d21 59 return (
fb20be7c 60 db.get(getauth(user), {}). #try to get the auth
61 get(key, #try to get the info-key by auth
62 db.get(str(user).lower(), {}). #fallback to using the nick
63 get(key, #and try to get the info-key from that
64 default #otherwise throw out whatever default
36f91d21 65 )))
fb20be7c 66def _set(user, key, value):
67 if getauth(user) is not None:
68 db.setdefault(getauth(user), {})[key] = value #use auth if we can
69 db.setdefault(str(user).lower(), {})[key] = value #but set nick too
36f91d21 70
71#commands
fb20be7c 72@lib.hook(needchan=False)
73def getinfo(bot, user, chan, realtarget, *args):
b79721ee 74 if chan is not None and realtarget == chan.name: replyto = chan
36f91d21 75 else: replyto = user
76
77 if len(args) > 1:
78 target = args[0]
79 item = args[1]
80 else:
81 target = user
82 item = args[0]
83
fb20be7c 84 value = _get(target, item, None)
36f91d21 85 if value is None:
86 bot.msg(replyto, "%(user)s: %(item)s on %(target)s is not set." % {'user':user,'item':item,'target':target})
87 else:
88 bot.msg(replyto, "%(user)s: %(item)s on %(target)s: %(value)s" % {'user':user,'item':item,'target':target,'value':value})
89
fb20be7c 90@lib.hook(needchan=False)
36f91d21 91@lib.argsGE(2)
fb20be7c 92def setinfo(bot, user, chan, realtarget, *args):
93 _set(user, args[0], ' '.join(args[1:]))
36f91d21 94 bot.msg(user, "Done.")
95
fb20be7c 96@lib.hook(glevel=lib.STAFF, needchan=False)
36f91d21 97@lib.argsGE(3)
fb20be7c 98def osetinfo(bot, user, chan, realtarget, *args):
99 _set(args[0], args[1], ' '.join(args[2:]))
36f91d21 100 bot.msg(user, "Done.")