forked from boamaod/skype2irc
-
Notifications
You must be signed in to change notification settings - Fork 4
/
karma.py
123 lines (110 loc) · 3.72 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
import re
import sqlite3
import datetime
with sqlite3.connect('karma.db') as conn:
cur = conn.cursor()
try:
cur.execute('''CREATE TABLE "karma" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL COLLATE NOCASE,
"delta" INTEGER,
"comment" TEXT,
"add_time" TEXT NOT NULL
);''')
conn.commit()
except sqlite3.OperationalError:
pass
config = None
usemap = None
ircbot = None
exp = re.compile("^(\([^)]+\)|[^ ]+)([+]{2}|[-]{2})(.*)$")
def getsubject(msg):
msg = msg.strip().lower()
if msg[0] == '(' and msg[len(msg) - 1] == ')':
msg = msg[0:len(msg)-1].strip()
return msg
def getreason(msg, sender):
msg = msg.strip()
if msg.startswith('#'):
return msg[1:].strip()
elif msg.startswith('//'):
return msg[2:].strip()
else:
return ''
def fetch_karma(ktgt):
with sqlite3.connect('karma.db') as conn:
cur = conn.cursor()
cur.execute('''SELECT SUM(`delta`) FROM karma WHERE `name` LIKE ?;''', (ktgt,))
ksum = cur.fetchone()[0]
return ksum
def karmaparse(source, msg):
msg = msg.strip()
ismatch = exp.match(msg)
if ismatch:
subject = getsubject(ismatch.group(1))
amount = ismatch.group(2)
if amount == '--':
amount = -1
ktype = 'negative'
else:
amount = 1
ktype = 'positive'
reason = getreason(ismatch.group(3), source)
if reason:
reason = ' for "%s"' % reason
if subject == source.lower() and amount > 0:
return "You can't give karma to yourself... loser."
else:
with sqlite3.connect('karma.db') as conn:
cur = conn.cursor()
cur.execute('''INSERT INTO karma (`name`,`delta`,`comment`,`add_time`) VALUES (?,?,?,?)''', (subject, amount, reason, datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')))
conn.commit()
if subject == config['nick'].lower() and amount > 0:
return "Karma for %s is now %s. Thanks, %s!" % (subject, fetch_karma(subject), source)
elif subject == config['nick'].lower() and amount < 0:
return "Karma for %s is now %s. Pfft." % (subject, fetch_karma(subject))
else:
return "Karma for %s is now %s." % (subject, fetch_karma(subject))
else:
return None
def get_karma(msg):
m = re.match('!karma (.+)', msg)
if not m:
return
ktgt = m.group(1).strip()
ksum = fetch_karma(ktgt)
if ksum is None:
kstr = 'No karma has ever been assigned to %s.' % ktgt
else:
kstr = '%s has %s karma.' % (ktgt, ksum)
return kstr
"""
def explain(msg):
m = re.match('!explain (.+)', msg)
if not m:
return
ktgt = m.group(1).strip()
cur.execute('''SELECT comment FROM karma where `name` LIKE ?;''', (ktgt,))
"""
def irc_msg(source, target, msg):
if not target in config['channels']:
return
if msg.startswith('!karma'):
kstr = get_karma(msg)
ircbot.say(target, kstr)
usemap[target].SendMessage(kstr)
else:
karma = karmaparse(source, msg)
if karma and target in config['channels']:
ircbot.say(target, karma)
usemap[target].SendMessage(karma)
def skype_msg(sourceDisplay, sourceHandle, target, msg):
if msg.startswith('!karma'):
kstr = get_karma(msg)
ircbot.say(usemap[target], kstr)
target.SendMessage(kstr)
else:
karma = karmaparse(sourceDisplay, msg)
if karma and usemap[target] in config['channels']:
ircbot.say(usemap[target], karma)
target.SendMessage(karma)