/
main.py
executable file
·59 lines (45 loc) · 1.6 KB
/
main.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
#!/usr/bin/env python
import sys
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol
from twisted.python import log
import re
from twisted.internet.defer import setDebugging
setDebugging(True)
class SedBot(irc.IRCClient):
nickname = "sedbot"
def __init__(self):
self.last = {}
def signedOn(self):
self.join("#pumpingstationone")
def joined(self, channel):
pass
def privmsg(self, user, channel, msg):
nick = user.split("!")[0]
if nick == self.nickname:
return
elif channel == self.nickname:
return
m = re.search("s/(.*)/(.*)/[giI]*", msg)
if(m):
search_string = m.group(1)
replace_string = m.group(2)
original_msg = self.last[user]
log.msg("replacing {0} with {1} in {2}".format(search_string, replace_string, original_msg))
new_msg = re.sub(search_string, replace_string, original_msg, 0, re.IGNORECASE)
full_msg = "What {0} meant to say was \"{1}\".".format(nick, new_msg)
self.msg(channel, full_msg)
else:
self.last[user] = msg
class SedBotFactory(protocol.ClientFactory):
protocol = SedBot
def clientConnectionLost(self, connector, reason):
connector.connect()
def clientConnectionFailed(self, connector, reason):
print("connection failed: {0}".format(reason))
reactor.stop()
if __name__ == '__main__':
log.startLogging(sys.stdout)
f = SedBotFactory()
reactor.connectTCP("irc.freenode.net", 6667, f)
reactor.run()