-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathkettlebot.py
179 lines (148 loc) · 6.45 KB
/
kettlebot.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
# kettlefish is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# kettlefish is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with kettlefish. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import datetime
import json
import re
from twisted.words.protocols.irc import IRCClient
from twisted.internet.protocol import ReconnectingClientFactory
from twisted.internet import reactor
from kettlefish import translate_remyspeak
class KettleBot(IRCClient):
bot_name = "kettlefish"
channel = "#rit-foss"
versionNum = 1
sourceURL = "http://github.com/FOSSRIT/kettlefish"
lineRate = 1
def __init__(self, *args, **kwargs):
with open('victims.json') as jfile:
self.victims = set(json.load(jfile))
self.quiet = None
self.karma = re.compile(r'\+\+|--')
self.xml = re.compile(r'<([\w]+)(?:\s[\w\s=\'"]*)?>')
self.xml_close = re.compile(r'</([\w]+)(?:\s[\w\s=\'"]*)?>')
def signedOn(self):
"""Called when bot has succesfully signed on to server."""
self.join(self.factory.channel)
self.factory.add_bot(self)
def joined(self, channel):
"""This will get called when the bot joins the channel."""
print("Joined %s" % channel)
def can_talk(self, channel, message):
if self.quiet and self.quiet > datetime.datetime.now():
return None
else:
self.quiet = None
self.msg(channel, message)
def left(self, channel):
"""This will get called when the bot leaves the channel."""
print("Left %s" % channel)
def privmsg(self, user, channel, msg):
"""This will get called when the bot receives a message."""
user = user.split('!', 1)[0]
# Regexes to handle non-kettlefish actions
sauce = re.match('{}([:,])? s(our|au)ce'.format(self.nickname), msg)
halp = re.match('{}([:,])? h[ea]lp'.format(self.nickname), msg)
shushify = re.match('{}([:,])? (un)?(s)?hush(.*)'.format(self.nickname), msg)
optional = re.match('{}([:,])? opt (in|out)$'.format(self.nickname), msg)
thanks = re.search('({0}([:,])? thanks)|(thanks.*{0})'.format(self.nickname), msg)
tag_list = self.xml.findall(msg.lower())
untag_list = self.xml_close.findall(msg.lower())
love = re.match('{}: <3'.format(self.nickname), msg)
# Anything PM-ed to kettlefish will be translated
if channel == self.nickname:
self.msg(user, translate_remyspeak(msg))
elif sauce:
self.msg(channel, self.sourceURL)
elif halp:
self.can_talk(
channel,
'{}: opt <in|out> -- opt in or out of automatic {} '
'translation'.format(user, self.nickname)
)
self.can_talk(
channel,
'{}: shush [minutes] -- prevent {} from speaking for a time '
'(default 5 minutes)'.format(user, self.nickname)
)
self.can_talk(
channel,
"{}: source -- display {}'s source repository URL".format(user, self.nickname)
)
self.can_talk(
channel,
"{}: help -- display this message".format(user)
)
# Handle being told to shush or unshush
elif shushify:
args = shushify.groups()
# This tests the existence of the 'un-' prefix
if args[0] == 'un':
self.quiet = None
self.msg(channel, 'universal translation matrix re-enabled')
else:
m = 5
if args[1]:
try:
# Don't shush longer than 90 minutes
m = min(int(args[2]), 90)
except ValueError:
pass
self.quiet = datetime.datetime.now() + datetime.timedelta(minutes=m)
self.msg(channel, 'quiet time')
# I provide a valuable service to the community!
elif thanks:
self.can_talk(channel, "{}: You're welcome!".format(user))
# But not that valuable...
elif optional:
if optional.groups()[0] == 'out':
self.victims.discard(user)
self.describe(channel, 'recognizes that {} hates fun'.format(user))
elif optional.groups()[0] == 'in':
self.victims.add(user)
self.describe(channel, 'has noted that {} is a cool person'.format(user))
with open('victims.json', 'w') as jfile:
json.dump(list(self.victims), jfile)
elif tag_list and user in self.victims:
for tag in untag_list:
if tag in tag_list:
tag_list.remove(tag)
if tag_list:
response = ''.join('</' + tag + '>' for tag in tag_list[::-1])
self.can_talk(channel, response)
elif user in self.victims:
translated = translate_remyspeak(msg)
display = self.karma.sub('', translated)
if not msg.lower() == translated.lower():
self.can_talk(channel, 'What {} means is: {}'.format(user, display))
elif love:
self.can_talk(channel, 'I <3 you too, {}'.format(user))
def action(self, user, channel, msg):
if user == 'decause' and msg == '&':
self.can_talk(channel, '{} steps into the background'.format(user))
class KettleBotFactory(ReconnectingClientFactory):
active_bot = None
def __init__(self, protocol=KettleBot):
self.protocol = protocol
self.channel = protocol.channel
IRCClient.nickname = protocol.bot_name
IRCClient.realname = protocol.bot_name
def add_bot(self, bot):
self.active_bot = bot
if __name__ == '__main__':
# create factory protocol and application
f = KettleBotFactory()
# connect factory to this host and port
reactor.connectTCP("irc.freenode.net", 6667, f)
# run bot
reactor.run()