1 # Erebus IRC bot - Author: Erebus Team
2 # vim: fileencoding=utf-8
4 # This file is released into the public domain; see http://unlicense.org/
8 'author': 'Erebus Team',
9 'license': 'public domain',
17 lib
= modlib
.modlib(__name__
)
18 def modstart(parent_arg
, *args
, **kwargs
):
22 return lib
.modstart(parent
, *args
, **kwargs
)
23 def modstop(*args
, **kwargs
):
25 return lib
.modstop(*args
, **kwargs
)
33 jsonfile
= parent
.cfg
.get('userinfo', 'jsonpath', default
="./modules/userinfo.json")
35 db
= json
.load(open(jsonfile
, "r"))
39 if json
is not None and json
.dump
is not None and db
!= {}:
40 json
.dump(db
, open(jsonfile
, "w"))#, indent=4, separators=(',', ': '))
44 if isinstance(thing
, parent
.User
):
45 if thing
.auth
is not None:
47 elif isinstance(thing
, basestring
):
48 if thing
.startswith("#"):
51 u
= parent
.user(thing
, create
=False)
52 if u
is not None and u
.auth
is not None:
53 return "#"+parent
.user(thing
).auth
57 return list(set(db
.get(_getauth(user
), {}).keys() + db.get(str(user).lower(), {}
).keys())) #list-to-set-to-list to remove duplicates
61 key
in db
.get(_getauth(user
), {}) or
62 key
in db
.get(str(user
).lower(), {})
64 def get(user
, key
, default
=None):
67 db
.get(_getauth(user
), {}). #try to get the auth
68 get(key
, #try to get the info-key by auth
69 db
.get(str(user
).lower(), {}). #fallback to using the nick
70 get(key
, #and try to get the info-key from that
71 default
#otherwise throw out whatever default
73 def set(user
, key
, value
):
75 if _getauth(user
) is not None:
76 db
.setdefault(_getauth(user
), {})[key
] = value
#use auth if we can
77 db
.setdefault(str(user
).lower(), {})[key
] = value
#but set nick too
78 def delete(user
, key
):
81 if auth
is not None and auth
in db
and key
in db
[auth
]:
83 target
= str(user
).lower()
84 if target
in db
and key
in db
[target
]:
88 @lib.hook(needchan
=False, wantchan
=True)
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")
90 def getitems(bot
, user
, chan
, realtarget
, *args
):
91 if chan
is not None: replyto
= chan
99 bot
.msg(replyto
, "%(user)s: %(target)s has the following info items: %(items)s" % {'user':user,'target':target,'items':(', '.join(keys(target)))}
)
101 @lib.hook(needchan
=False, wantchan
=True)
102 @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")
104 def getinfo(bot
, user
, chan
, realtarget
, *args
):
105 if chan
is not None: replyto
= chan
115 value
= get(target
, item
, None)
117 bot
.msg(replyto
, "%(user)s: %(item)s on %(target)s is not set." % {'user':user,'item':item,'target':target}
)
119 bot
.msg(replyto
, "%(user)s: %(item)s on %(target)s: %(value)s" % {'user':user,'item':item,'target':target,'value':value}
)
121 @lib.hook(needchan
=False)
122 @lib.help("<item> <value>", "sets an info item about you")
124 def setinfo(bot
, user
, chan
, realtarget
, *args
):
125 set(user
, args
[0], ' '.join(args
[1:]))
127 bot
.msg(user
, "Done.")
129 @lib.hook(needchan
=False)
130 @lib.help("<item>", "deletes an info item about you")
132 def delinfo(bot
, user
, chan
, realtarget
, *args
):
133 delete(user
, args
[0])
135 bot
.msg(user
, "Done.")
137 @lib.hook(glevel
=lib
.ADMIN
, needchan
=False)
138 @lib.help("<target> <item> <value>", "sets an info item about someone else", "<target> may be a nick, or an auth in format '#auth'")
140 def osetinfo(bot
, user
, chan
, realtarget
, *args
):
141 set(args
[0], args
[1], ' '.join(args
[2:]))
143 bot
.msg(user
, "Done.")
145 @lib.hook(glevel
=lib
.STAFF
, needchan
=False)
146 @lib.help("<target> <item>", "deletes an info item about someone else", "<target> may be a nick, or an auth in format '#auth'")
148 def odelinfo(bot
, user
, chan
, realtarget
, *args
):
149 delete(args
[0], args
[1])
151 bot
.msg(user
, "Done.")