-
Notifications
You must be signed in to change notification settings - Fork 5
/
karma.py
261 lines (211 loc) · 6.85 KB
/
karma.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python
# encoding: utf-8
"""
karma.py - A karma module for willie.
Copyright 2013, Timothy Lee <[email protected]>
Licensed under the MIT License.
"""
import string
import willie
###############################################################################
# Setup the module
###############################################################################
MODULE = 'karma'
WHO = 'who'
KARMA = 'karma'
REASON = 'reason'
DEBUG_LEVEL = 'verbose'
feedback = None
byself = None
def debug_(tag, text, level):
"""Mimic willie.debug function for pytest to use.
"""
print "[%s] %s" % (tag, text)
debug = debug_
def configure(config):
"""
| [karma] | example | purpose |
| ------- | ------- | ------- |
| feedback | True | Notify by bot |
| byself | False | Self (pro|de)mote |
"""
if config.option('Configure karma', False):
config.interactive_add('karma', 'feedback', 'Notify by bot', 'True')
config.interactive_add('karma', 'byself', 'Self (pro|de)mote', 'False')
def setup(bot):
"""Setup the database, get the settings.
:bot: willie.bot.Willie
"""
#. get debug function
global debug
debug = bot.debug if bot.debug else debug
#. get settings
feedback_, byself_, debug_ = True, False, False
try:
config = getattr(bot.config, MODULE)
feedback_ = is_true(config.feedback)
byself_ = is_true(config.byself)
except Exception, e:
pass
global feedback, byself
feedback = feedback_
byself = byself_
#. check database
if bot.db:
key, name = WHO, KARMA
columns = [key, KARMA, REASON]
if not getattr(bot.db, name):
try:
bot.db.add_table(name, columns, key)
except Exception, e:
debug(MODULE, 'Table init fail - %s' % (e), DEBUG_LEVEL)
raise e
else:
msg = "DB init fail, setup the DB first!"
debug(MODULE, msg, DEBUG_LEVEL)
raise Exception(msg)
###############################################################################
# Helper function
###############################################################################
def is_true(value):
"""Return True if value is true
:value: value
:returns: True or False
"""
try:
return True if value.lower() == 'true' else False
except Exception:
return value
def get_table(bot):
"""Return the table instance.
:bot: willie.bot.Willie
:returns: willie.db.Table
"""
try:
return getattr(bot.db, KARMA)
except Exception:
return None
def get_karma(table, who):
"""Get karma status from the table.
:table: willie.db.Table instance
:who: nickname of IRC user
:returns: (karma, reason)
"""
karma, reason = str(0), str(None)
try:
karma, reason = table.get(who, (KARMA, REASON))
except Exception, e:
debug(MODULE, "get karma fail - %s." % (e), DEBUG_LEVEL)
return karma, reason
def _update_karma(table, who, reason, method='+'):
"""Update karma for specify IRC user.
:table: willie.db.Table
:who: nickname of IRC user
:reason: reason
:method: '+' or '-'
"""
karma = get_karma(table, who)[0]
karma = int(karma) if karma else 0
try:
if method == '+':
table.update(who, dict(karma=str(karma + 1), reason=reason))
else:
table.update(who, dict(karma=str(karma - 1), reason=reason))
except Exception, e:
debug(MODULE, "update karma fail, e: %s" % (e), DEBUG_LEVEL)
def promote_karma(table, who, reason):
"""Promote karma for specify IRC user.
:table: willie.db.Table
:who: nickname of IRC user
:reason: reason
"""
return _update_karma(table, who, reason, '+')
def demote_karma(table, who, reason):
"""Demote karma for specify IRC user.
:table: willie.db.Table
:who: nickname of IRC user
:reason: reason
"""
return _update_karma(table, who, reason, '-')
def _parse_msg(msg, method='+'):
"""Parse the message.
:msg: message
:returns: (who, reason)
"""
try:
who = msg.split(method)[0].strip().split().pop()
reason = msg.split(method)[2].strip()
if '#' in reason:
reason = reason.split('#')[1].strip()
if len(reason) == 0:
reason = None
#. check if nickname only contain [a-Z_]
for s in who:
if s not in "%s_" % string.ascii_letters:
who = None
break
#. strip illegal chars
reason = reason.replace('"', '') if reason else reason
except Exception, e:
debug(MODULE, "parse message fail - %s." % (e), DEBUG_LEVEL)
return None, None
return who, reason
def parse_promote(msg):
"""Parse the message with '++'.
:msg: message
:returns: (who, reason)
"""
return _parse_msg(msg, method='+')
def parse_demote(msg):
"""Parse the message with '--'.
:msg: message
:returns: (who, reason)
"""
return _parse_msg(msg, method='-')
def meet_karma(bot, trigger, parse_fun, karma_fun):
"""Update karma status for specify IRC user
:bot: willie.bot.Willie
:trigger: willie.bot.Willie.Trigger
"""
table = get_table(bot)
if table:
msg = trigger.bytes
who, reason = parse_fun(msg)
if who:
#. not allow self (pro|de)mote
if not byself:
if who == trigger.nick:
return
#. update karma
reason = reason if reason else str(None)
karma_fun(table, who, reason)
karma, reason= get_karma(table, who)
if feedback:
bot.say("%s: %s, reason: %s" % (who, karma, reason))
###############################################################################
# Event & Command
###############################################################################
@willie.module.rule(r'^[\w][\S]+[\+\+]')
def meet_promote_karma(bot, trigger):
"""Update karma status for specify IRC user if get '++' message.
"""
return meet_karma(bot, trigger, parse_promote, promote_karma)
@willie.module.rule(r'^[\w][\S]+[\-\-]')
def meet_demote_karma(bot, trigger):
"""Update karma status for specify IRC user if get '--' message.
"""
return meet_karma(bot, trigger, parse_demote, demote_karma)
@willie.module.commands('karma')
def karma(bot, trigger):
"""Command to show the karma status for specify IRC user.
"""
table = get_table(bot)
if table:
if trigger.group(2):
who = trigger.group(2).strip().split()[0]
karma, reason= get_karma(table, who)
bot.say("%s: %s, reason: %s" % (who, karma, reason))
else:
bot.say(".karma <nick> - Reports karma status for <nick>.")
else:
bot.say("Setup the database first, contact your bot admin.")