1
|
1 |
from twisted.words.protocols import irc
|
|
2 |
from twisted.internet import protocol
|
4
|
3 |
from twisted.python import log
|
1
|
4 |
|
|
5 |
import buffer
|
|
6 |
|
4
|
7 |
HOSTNAME = "irc.marttila.de"
|
1
|
8 |
PORT = 6667
|
|
9 |
NICKNAME = "FixBot"
|
|
10 |
USERNAME = "fixme"
|
4
|
11 |
CHANNEL = "#fixme-test"
|
1
|
12 |
|
4
|
13 |
class BotProtocol (irc.IRCClient, object) :
|
1
|
14 |
"""
|
|
15 |
Fixme IRC bot
|
|
16 |
"""
|
4
|
17 |
|
|
18 |
def __init__ (self) :
|
|
19 |
self.nickname = NICKNAME
|
|
20 |
self.username = USERNAME
|
1
|
21 |
|
|
22 |
# housekeeping
|
|
23 |
def connectionMade (self) :
|
|
24 |
log.msg("Connected")
|
4
|
25 |
super(BotProtocol, self).connectionMade()
|
1
|
26 |
|
|
27 |
def connectionLost (self, reason) :
|
|
28 |
log.msg("Connection lost: %s" % reason)
|
4
|
29 |
super(BotProtocol, self).connectionLost(reason)
|
1
|
30 |
|
|
31 |
def signedOn (self) :
|
4
|
32 |
log.msg("Signed on, joining channel %s" % CHANNEL)
|
1
|
33 |
self.join(CHANNEL)
|
|
34 |
|
|
35 |
def joined (self, channel) :
|
|
36 |
log.msg("Joined channel %s" % channel)
|
4
|
37 |
|
|
38 |
self.factory.connection = self
|
1
|
39 |
|
|
40 |
# our actual functionality
|
|
41 |
def send (self, msg) :
|
4
|
42 |
msg = str(msg)
|
|
43 |
|
|
44 |
if len(msg) > 480 :
|
|
45 |
log.msg("truncating: %s" % msg)
|
|
46 |
msg = msg[:480] + "..."
|
|
47 |
|
|
48 |
msg = msg.replace("\n", "\\n").replace("\r", "\\r").replace("\0", "\\0")
|
|
49 |
|
|
50 |
self.msg(CHANNEL, msg)
|
1
|
51 |
|
|
52 |
def sendEvent (self, event) :
|
4
|
53 |
self.send("[%s.%s] %s" % (event.module.name, event.type, event.msg))
|
1
|
54 |
|
|
55 |
def moduleConnected (self, module, addr) :
|
4
|
56 |
self.send("{modules.%s} connected from %s:%d, version %s" % (module.name, addr.host, addr.port, module.version))
|
1
|
57 |
|
4
|
58 |
def moduleDisconnected (self, module, reason) :
|
|
59 |
self.send("{modules.%s} disconnected: %s" % (module.name, reason))
|
1
|
60 |
|
|
61 |
|