]> jfr.im git - erebus.git/blame - modules/userinfo.py
dont initialize the recvbuffer with 8KB of NUL
[erebus.git] / modules / userinfo.py
CommitLineData
36f91d21 1# Erebus IRC bot - Author: Erebus Team
4477123d 2# vim: fileencoding=utf-8
a62d0d18 3# userinfo module
36f91d21 4# This file is released into the public domain; see http://unlicense.org/
5
6# module info
7modinfo = {
8 'author': 'Erebus Team',
9 'license': 'public domain',
fa93b933 10 'compatible': [0],
a62d0d18 11 'depends': [],
12 'softdeps': ['help'],
36f91d21 13}
14
15# preamble
16import modlib
17lib = modlib.modlib(__name__)
18def modstart(parent_arg, *args, **kwargs):
19 global parent
20 parent = parent_arg
21 gotParent()
22 return lib.modstart(parent, *args, **kwargs)
23def modstop(*args, **kwargs):
54c084bc 24 savedb()
36f91d21 25 return lib.modstop(*args, **kwargs)
26
27# module code
c62b669b 28import json, __builtin__
36f91d21 29
30#setup
31def gotParent():
32 global jsonfile, db
33 jsonfile = parent.cfg.get('userinfo', 'jsonpath', default="./modules/userinfo.json")
983cd789 34 try:
35 db = json.load(open(jsonfile, "r"))
36 except:
37 db = {}
54c084bc 38def savedb():
230c1654 39 if json is not None and json.dump is not None and db != {}:
36f91d21 40 json.dump(db, open(jsonfile, "w"))#, indent=4, separators=(',', ': '))
41
42#functions
54c084bc 43def _getauth(thing):
36f91d21 44 if isinstance(thing, parent.User):
45 if thing.auth is not None:
46 return "#"+thing.auth
47 elif isinstance(thing, basestring):
fd07173d 48 if thing.startswith("#"):
36f91d21 49 return thing
50 else:
96dfdcd3 51 u = parent.user(thing, create=False)
52 if u is not None and u.auth is not None:
36f91d21 53 return "#"+parent.user(thing).auth
54 return None
55
aa582dd7 56def keys(user):
c62b669b 57 return list(__builtin__.set(db.get(_getauth(user), {}).keys() + db.get(str(user).lower(), {}).keys())) #list-to-set-to-list to remove duplicates
aa582dd7 58def has(user, key):
4b5f28dd 59 key = key.lower()
36f91d21 60 return (
54c084bc 61 key in db.get(_getauth(user), {}) or
36f91d21 62 key in db.get(str(user).lower(), {})
63 )
aa582dd7 64def get(user, key, default=None):
4b5f28dd 65 key = key.lower()
36f91d21 66 return (
54c084bc 67 db.get(_getauth(user), {}). #try to get the auth
fb20be7c 68 get(key, #try to get the info-key by auth
69 db.get(str(user).lower(), {}). #fallback to using the nick
230c1654 70 get(key, #and try to get the info-key from that
71 default #otherwise throw out whatever default
36f91d21 72 )))
aa582dd7 73def set(user, key, value):
4b5f28dd 74 key = key.lower()
54c084bc 75 if _getauth(user) is not None:
76 db.setdefault(_getauth(user), {})[key] = value #use auth if we can
fb20be7c 77 db.setdefault(str(user).lower(), {})[key] = value #but set nick too
aa582dd7 78def delete(user, key):
a0bd416f 79 key = key.lower()
54c084bc 80 auth = _getauth(user)
a0bd416f 81 if auth is not None and auth in db and key in db[auth]:
82 del db[auth][key]
83 target = str(user).lower()
84 if target in db and key in db[target]:
85 del db[target][key]
36f91d21 86
87#commands
f5aec865 88@lib.hook(needchan=False, wantchan=True)
230c1654 89@lib.help("[<target>]", "lists info items known about someone", "<target> may be a nick, or an auth in format '#auth'", "it defaults to yourself")
90def getitems(bot, user, chan, realtarget, *args):
230c1654 91 if len(args) > 0:
92 target = args[0]
93 else:
94 target = user
95
c62b669b 96 return "%(target)s has the following info items: %(items)s" % {'target':target,'items':(', '.join(keys(target)))}
230c1654 97
f5aec865 98@lib.hook(needchan=False, wantchan=True)
230c1654 99@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 100@lib.argsGE(1)
fb20be7c 101def getinfo(bot, user, chan, realtarget, *args):
36f91d21 102 if len(args) > 1:
103 target = args[0]
104 item = args[1]
105 else:
106 target = user
107 item = args[0]
108
aa582dd7 109 value = get(target, item, None)
36f91d21 110 if value is None:
c62b669b 111 return "%(item)s on %(target)s is not set." % {'item':item,'target':target}
36f91d21 112 else:
c62b669b 113 return "%(item)s on %(target)s: %(value)s" % {'item':item,'target':target,'value':value}
36f91d21 114
fb20be7c 115@lib.hook(needchan=False)
5f03d045 116@lib.help("<item> <value>", "sets an info item about you")
36f91d21 117@lib.argsGE(2)
fb20be7c 118def setinfo(bot, user, chan, realtarget, *args):
aa582dd7 119 set(user, args[0], ' '.join(args[1:]))
54c084bc 120 savedb()
36f91d21 121 bot.msg(user, "Done.")
122
a0bd416f 123@lib.hook(needchan=False)
124@lib.help("<item>", "deletes an info item about you")
125@lib.argsEQ(1)
126def delinfo(bot, user, chan, realtarget, *args):
aa582dd7 127 delete(user, args[0])
54c084bc 128 savedb()
a0bd416f 129 bot.msg(user, "Done.")
130
131@lib.hook(glevel=lib.ADMIN, needchan=False)
230c1654 132@lib.help("<target> <item> <value>", "sets an info item about someone else", "<target> may be a nick, or an auth in format '#auth'")
36f91d21 133@lib.argsGE(3)
fb20be7c 134def osetinfo(bot, user, chan, realtarget, *args):
aa582dd7 135 set(args[0], args[1], ' '.join(args[2:]))
54c084bc 136 savedb()
36f91d21 137 bot.msg(user, "Done.")
a0bd416f 138
139@lib.hook(glevel=lib.STAFF, needchan=False)
140@lib.help("<target> <item>", "deletes an info item about someone else", "<target> may be a nick, or an auth in format '#auth'")
141@lib.argsEQ(2)
142def odelinfo(bot, user, chan, realtarget, *args):
aa582dd7 143 delete(args[0], args[1])
54c084bc 144 savedb()
a0bd416f 145 bot.msg(user, "Done.")