forked from FOSSRIT/kettlefish
/
kettlebot.py
79 lines (61 loc) · 2.59 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
# 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 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/oddshocks/kettlefish"
lineRate = 1
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 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]
if user == 'decause':
translated = translate_remyspeak(msg)
display = re.sub(r'(\+\+)|(--)', '', translated)
if not msg.lower() == translated.lower():
self.msg(channel, 'What {} meant to say was: {}'.format(
user, display))
if channel == self.nickname:
self.msg(user, translate_remyspeak(msg))
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()