-
Notifications
You must be signed in to change notification settings - Fork 1
/
Connector.py
140 lines (109 loc) · 3.92 KB
/
Connector.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
import sys
import traceback
from Configuration import ConfigFile
from Logging import LogFile
from twisted.words.protocols import irc
from twisted.internet import protocol, reactor
#these events get given to plugins to give back to the the ircconnector
log = LogFile("IRCConnector")
log.debug("Connector imported")
class IRCConnectorEvents:
def __init__(self):
pass
def join(self, channel, key=None):
return "join", channel, key
def part(self, channel, message=None):
return "part", channel, messages
def kick(self, channel, user, message=None):
return "kick", channel, user, message
def topic(self, channel, message=None):
return "topic", channel, message
def say(self, channel, message, length=None):
return "say", channel, message, 1024
def msg(self, user, message, length=None):
return "msg", user, message, 1024
def notice(self, user, message):
return "notice", user, message
def away(self, message=""):
return "away", message
def back(self):
return "back"
def setNick(self, nickname):
return "setNick", nickname
def quit(self, message=""):
return "quit", message
def me(self, channel, message):
return "action", channel, message
def ping(self, user, message=""):
return "ping", user, message
def stop(self):
return "stop"
class Connector(protocol.ClientFactory, irc.IRCClient, object):
EventHandler = None
reactor = None
def HandleResponse(self, eventInfo):
log.debug("Handle Response",eventInfo)
if eventInfo == "stop":
log.debug("Stopping reactor")
self.reactor.stop()
self.transport.doWrite()
return
f = getattr(self, eventInfo[0], None)
if f:
try:
log.debug("Calling", f, eventInfo[1:])
f(*eventInfo[1:])
#Coming from a different thread, we have to do this apparently
self.transport.doWrite()
except:
log.exception("Failed to send event.")
traceback.print_exc(file=sys.stdout)
def SetEventHandler(self, func):
log.debug("Event handler set to", func)
self.EventHandler = func
def buildProtocol(self, addr):
return self
def handleCommand(self, cmd, prefix, params):
super(Connector, self).handleCommand(cmd, prefix, params)
if self.EventHandler:
event = {}
event["nickname"] = self.nickname
event["command"] = cmd
event["prefix"] = prefix
event["target"] = params[0]
if len(params) == 2:
event["message"] = params[1]
log.debug("Producing Event", event)
self.EventHandler(event)
def __init__(self):
log.note("Constructing.")
self.eventObj = IRCConnectorEvents()
self.protocol = self
self.config = ConfigFile("SuperBot2IRC")
def Start(self):
server = self.config["Connection", "Server"]
port = int(self.config["Connection", "Port"])
nick = self.config["Connection", "Nick"]
log.note("Connecting", server, port, nick)
if not server:
log.fatal("No 'Connection:Server' specified.")
return
if not port:
log.fatal("No 'Connection:Port' specified.")
return
if not nick:
log.fatal("No 'Connection:Nick' specified.")
return
self.nickname = nick
reactor.connectTCP(server, port, self)
log.note("Running reactor")
self.reactor = reactor
try:
reactor.run()
except:
log.exception("Exception in reactor. Connector exiting.")
low.note("Exiting Connector")
def Stop(self):
log.note("Nothing to do for stopping")
def GetResponseObject(self):
return self.eventObj