]>
Commit | Line | Data |
---|---|---|
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 | |
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") | |
983cd789 | 32 | try: |
33 | db = json.load(open(jsonfile, "r")) | |
34 | except: | |
35 | db = {} | |
36f91d21 | 36 | def closeshop(): |
230c1654 | 37 | if json is not None and json.dump is not None and db != {}: |
36f91d21 | 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 | ||
230c1654 | 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 | |
fb20be7c | 55 | def _has(user, key): |
36f91d21 | 56 | return ( |
57 | key in db.get(getauth(user), {}) or | |
58 | key in db.get(str(user).lower(), {}) | |
59 | ) | |
fb20be7c | 60 | def _get(user, key, default=None): |
36f91d21 | 61 | return ( |
fb20be7c | 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 | |
230c1654 | 65 | get(key, #and try to get the info-key from that |
66 | default #otherwise throw out whatever default | |
36f91d21 | 67 | ))) |
fb20be7c | 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 | |
36f91d21 | 72 | |
73 | #commands | |
fb20be7c | 74 | @lib.hook(needchan=False) |
230c1654 | 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") | |
5f03d045 | 89 | @lib.argsGE(1) |
fb20be7c | 90 | def getinfo(bot, user, chan, realtarget, *args): |
b79721ee | 91 | if chan is not None and realtarget == chan.name: replyto = chan |
36f91d21 | 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 | ||
fb20be7c | 101 | value = _get(target, item, None) |
36f91d21 | 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 | ||
fb20be7c | 107 | @lib.hook(needchan=False) |
5f03d045 | 108 | @lib.help("<item> <value>", "sets an info item about you") |
36f91d21 | 109 | @lib.argsGE(2) |
fb20be7c | 110 | def setinfo(bot, user, chan, realtarget, *args): |
111 | _set(user, args[0], ' '.join(args[1:])) | |
230c1654 | 112 | closeshop() |
36f91d21 | 113 | bot.msg(user, "Done.") |
114 | ||
fb20be7c | 115 | @lib.hook(glevel=lib.STAFF, needchan=False) |
230c1654 | 116 | @lib.help("<target> <item> <value>", "sets an info item about someone else", "<target> may be a nick, or an auth in format '#auth'") |
36f91d21 | 117 | @lib.argsGE(3) |
fb20be7c | 118 | def osetinfo(bot, user, chan, realtarget, *args): |
119 | _set(args[0], args[1], ' '.join(args[2:])) | |
230c1654 | 120 | closeshop() |
36f91d21 | 121 | bot.msg(user, "Done.") |