]>
Commit | Line | Data |
---|---|---|
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 | |
6 | modinfo = { | |
7 | 'author': 'Erebus Team', | |
8 | 'license': 'public domain', | |
f5aec865 | 9 | 'compatible': [2], |
a62d0d18 | 10 | 'depends': [], |
11 | 'softdeps': ['help'], | |
36f91d21 | 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") | |
983cd789 | 33 | try: |
34 | db = json.load(open(jsonfile, "r")) | |
35 | except: | |
36 | db = {} | |
36f91d21 | 37 | def 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 | |
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): | |
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 | 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 | |
fb20be7c | 56 | def _has(user, key): |
4b5f28dd | 57 | key = key.lower() |
36f91d21 | 58 | return ( |
59 | key in db.get(getauth(user), {}) or | |
60 | key in db.get(str(user).lower(), {}) | |
61 | ) | |
fb20be7c | 62 | def _get(user, key, default=None): |
4b5f28dd | 63 | key = key.lower() |
36f91d21 | 64 | return ( |
fb20be7c | 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 | |
230c1654 | 68 | get(key, #and try to get the info-key from that |
69 | default #otherwise throw out whatever default | |
36f91d21 | 70 | ))) |
fb20be7c | 71 | def _set(user, key, value): |
4b5f28dd | 72 | key = key.lower() |
fb20be7c | 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 | |
36f91d21 | 76 | |
77 | #commands | |
f5aec865 | 78 | @lib.hook(needchan=False, wantchan=True) |
230c1654 | 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): | |
f5aec865 | 81 | if chan is not None: replyto = chan |
230c1654 | 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 | ||
f5aec865 | 91 | @lib.hook(needchan=False, wantchan=True) |
230c1654 | 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") |
5f03d045 | 93 | @lib.argsGE(1) |
fb20be7c | 94 | def getinfo(bot, user, chan, realtarget, *args): |
f5aec865 | 95 | if chan is not None: replyto = chan |
36f91d21 | 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 | ||
fb20be7c | 105 | value = _get(target, item, None) |
36f91d21 | 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 | ||
fb20be7c | 111 | @lib.hook(needchan=False) |
5f03d045 | 112 | @lib.help("<item> <value>", "sets an info item about you") |
36f91d21 | 113 | @lib.argsGE(2) |
fb20be7c | 114 | def setinfo(bot, user, chan, realtarget, *args): |
115 | _set(user, args[0], ' '.join(args[1:])) | |
230c1654 | 116 | closeshop() |
36f91d21 | 117 | bot.msg(user, "Done.") |
118 | ||
fb20be7c | 119 | @lib.hook(glevel=lib.STAFF, needchan=False) |
230c1654 | 120 | @lib.help("<target> <item> <value>", "sets an info item about someone else", "<target> may be a nick, or an auth in format '#auth'") |
36f91d21 | 121 | @lib.argsGE(3) |
fb20be7c | 122 | def osetinfo(bot, user, chan, realtarget, *args): |
123 | _set(args[0], args[1], ' '.join(args[2:])) | |
230c1654 | 124 | closeshop() |
36f91d21 | 125 | bot.msg(user, "Done.") |