]> jfr.im git - erebus.git/blob - modules/userinfo.py
74a70e2909931e82805f889126742a57cab7f00d
[erebus.git] / modules / userinfo.py
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
6 modinfo = {
7 'author': 'Erebus Team',
8 'license': 'public domain',
9 'compatible': [2],
10 'depends': [],
11 'softdeps': ['help'],
12 }
13
14 # preamble
15 import modlib
16 lib = modlib.modlib(__name__)
17 def modstart(parent_arg, *args, **kwargs):
18 global parent
19 parent = parent_arg
20 gotParent()
21 return lib.modstart(parent, *args, **kwargs)
22 def modstop(*args, **kwargs):
23 closeshop()
24 return lib.modstop(*args, **kwargs)
25
26 # module code
27 import json
28
29 #setup
30 def 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 = {}
37 def 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
42 def 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
54 def _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
56 def _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 )
62 def _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 )))
71 def _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
76
77 #commands
78 @lib.hook(needchan=False, wantchan=True)
79 @lib.help("[<target>]", "lists info items known about someone", "<target> may be a nick, or an auth in format '#auth'", "it defaults to yourself")
80 def getitems(bot, user, chan, realtarget, *args):
81 if chan is not None: replyto = chan
82 else: replyto = user
83
84 if len(args) > 0:
85 target = args[0]
86 else:
87 target = user
88
89 bot.msg(replyto, "%(user)s: %(target)s has the following info items: %(items)s" % {'user':user,'target':target,'items':(', '.join(_keys(target)))})
90
91 @lib.hook(needchan=False, wantchan=True)
92 @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")
93 @lib.argsGE(1)
94 def getinfo(bot, user, chan, realtarget, *args):
95 if chan is not None: replyto = chan
96 else: replyto = user
97
98 if len(args) > 1:
99 target = args[0]
100 item = args[1]
101 else:
102 target = user
103 item = args[0]
104
105 value = _get(target, item, None)
106 if value is None:
107 bot.msg(replyto, "%(user)s: %(item)s on %(target)s is not set." % {'user':user,'item':item,'target':target})
108 else:
109 bot.msg(replyto, "%(user)s: %(item)s on %(target)s: %(value)s" % {'user':user,'item':item,'target':target,'value':value})
110
111 @lib.hook(needchan=False)
112 @lib.help("<item> <value>", "sets an info item about you")
113 @lib.argsGE(2)
114 def setinfo(bot, user, chan, realtarget, *args):
115 _set(user, args[0], ' '.join(args[1:]))
116 closeshop()
117 bot.msg(user, "Done.")
118
119 @lib.hook(glevel=lib.STAFF, needchan=False)
120 @lib.help("<target> <item> <value>", "sets an info item about someone else", "<target> may be a nick, or an auth in format '#auth'")
121 @lib.argsGE(3)
122 def osetinfo(bot, user, chan, realtarget, *args):
123 _set(args[0], args[1], ' '.join(args[2:]))
124 closeshop()
125 bot.msg(user, "Done.")