]> jfr.im git - erebus.git/blob - modules/userinfo.py
adjust logging
[erebus.git] / modules / userinfo.py
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
6 modinfo = {
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
14 import modlib
15 lib = modlib.modlib(__name__)
16 def modstart(parent_arg, *args, **kwargs):
17 global parent
18 parent = parent_arg
19 gotParent()
20 return lib.modstart(parent, *args, **kwargs)
21 def modstop(*args, **kwargs):
22 closeshop()
23 return lib.modstop(*args, **kwargs)
24
25 # module code
26 import json
27
28 #setup
29 def gotParent():
30 global jsonfile, db
31 jsonfile = parent.cfg.get('userinfo', 'jsonpath', default="./modules/userinfo.json")
32 try:
33 db = json.load(open(jsonfile, "r"))
34 except:
35 db = {}
36 def closeshop():
37 if json is not None and json.dump is not None and db != {}:
38 json.dump(db, open(jsonfile, "w"))#, indent=4, separators=(',', ': '))
39
40 #functions
41 def 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
53 def _keys(user):
54 return list(set(db.get(getauth(user), {}).keys() + db.get(str(user).lower(), {}).keys())) #list-to-set-to-list to remove duplicates
55 def _has(user, key):
56 return (
57 key in db.get(getauth(user), {}) or
58 key in db.get(str(user).lower(), {})
59 )
60 def _get(user, key, default=None):
61 return (
62 db.get(getauth(user), {}). #try to get the auth
63 get(key, #try to get the info-key by auth
64 db.get(str(user).lower(), {}). #fallback to using the nick
65 get(key, #and try to get the info-key from that
66 default #otherwise throw out whatever default
67 )))
68 def _set(user, key, value):
69 if getauth(user) is not None:
70 db.setdefault(getauth(user), {})[key] = value #use auth if we can
71 db.setdefault(str(user).lower(), {})[key] = value #but set nick too
72
73 #commands
74 @lib.hook(needchan=False)
75 @lib.help("[<target>]", "lists info items known about someone", "<target> may be a nick, or an auth in format '#auth'", "it defaults to yourself")
76 def getitems(bot, user, chan, realtarget, *args):
77 if chan is not None and realtarget == chan.name: replyto = chan
78 else: replyto = user
79
80 if len(args) > 0:
81 target = args[0]
82 else:
83 target = user
84
85 bot.msg(replyto, "%(user)s: %(target)s has the following info items: %(items)s" % {'user':user,'target':target,'items':(', '.join(_keys(target)))})
86
87 @lib.hook(needchan=False)
88 @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")
89 @lib.argsGE(1)
90 def getinfo(bot, user, chan, realtarget, *args):
91 if chan is not None and realtarget == chan.name: replyto = chan
92 else: replyto = user
93
94 if len(args) > 1:
95 target = args[0]
96 item = args[1]
97 else:
98 target = user
99 item = args[0]
100
101 value = _get(target, item, None)
102 if value is None:
103 bot.msg(replyto, "%(user)s: %(item)s on %(target)s is not set." % {'user':user,'item':item,'target':target})
104 else:
105 bot.msg(replyto, "%(user)s: %(item)s on %(target)s: %(value)s" % {'user':user,'item':item,'target':target,'value':value})
106
107 @lib.hook(needchan=False)
108 @lib.help("<item> <value>", "sets an info item about you")
109 @lib.argsGE(2)
110 def setinfo(bot, user, chan, realtarget, *args):
111 _set(user, args[0], ' '.join(args[1:]))
112 closeshop()
113 bot.msg(user, "Done.")
114
115 @lib.hook(glevel=lib.STAFF, needchan=False)
116 @lib.help("<target> <item> <value>", "sets an info item about someone else", "<target> may be a nick, or an auth in format '#auth'")
117 @lib.argsGE(3)
118 def osetinfo(bot, user, chan, realtarget, *args):
119 _set(args[0], args[1], ' '.join(args[2:]))
120 closeshop()
121 bot.msg(user, "Done.")