forked from Thynix/RelayBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relaybot.py
366 lines (294 loc) · 13.9 KB
/
relaybot.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol, ssl
from twisted.internet.protocol import ReconnectingClientFactory
from twisted.python import log, reflect, util
from twisted.internet.endpoints import clientFromString
from twisted.internet.error import VerifyError, CertificateError
from twisted.internet.defer import Deferred
from twisted.internet.task import LoopingCall
from twisted.application import service
from twisted.python.hashlib import md5
from OpenSSL import SSL, crypto
from signal import signal, SIGINT
from ConfigParser import SafeConfigParser
import re, sys, itertools
#
# RelayBot is a derivative of http://code.google.com/p/relaybot/
#
log.startLogging(sys.stdout)
__version__ = "0.1"
application = service.Application("RelayBot")
_sessionCounter = itertools.count().next
def main():
config = SafeConfigParser()
config.read("relaybot.config")
defaults = config.defaults()
for section in config.sections():
def get(option):
if option in defaults or config.has_option(section, option):
return config.get(section, option) or defaults[option]
else:
return None
options = {}
for option in [ "timeout", "host", "port", "nick", "channel", "heartbeat", "password", "username", "realname", "mode", "ssl", "fingerprint", "nickcolor", "topicsync" ]:
options[option] = get(option)
mode = get("mode")
#Not using endpoints pending http://twistedmatrix.com/trac/ticket/4735
#(ReconnectingClientFactory equivalent for endpoints.)
factory = None
if mode == "Default":
factory = RelayFactory
elif mode == "FLIP":
factory = FLIPFactory
elif mode == "NickServ":
factory = NickServFactory
options["nickServPassword"] = get("nickServPassword")
elif mode == "ReadOnly":
factory = ReadOnlyFactory
options["nickServPassword"] = get("nickServPassword")
# RelayByCommand: only messages with <nickname>: will be relayed.
elif mode == "RelayByCommand":
factory = CommandFactory
factory = factory(options)
if options['ssl'] == "True":
if options['fingerprint']:
ctx = certoptions(fingerprint=options['fingerprint'], verifyDepth=0)
reactor.connectSSL(options['host'], int(options['port']), factory, ctx, int(options['timeout']))
else:
reactor.connectSSL(options['host'], int(options['port']), factory, ssl.ClientContextFactory(), int(options['timeout']))
else:
reactor.connectTCP(options['host'], int(options['port']), factory, int(options['timeout']))
reactor.callWhenRunning(signal, SIGINT, handler)
class certoptions(object):
_context = None
_OP_ALL = getattr(SSL, 'OP_ALL', 0x0000FFFF)
_OP_NO_TICKET = 0x00004000
method = SSL.TLSv1_METHOD
def __init__(self, privateKey=None, certificate=None, method=None, verify=False, caCerts=None, verifyDepth=9, requireCertificate=True, verifyOnce=True, enableSingleUseKeys=True, enableSessions=True, fixBrokenPeers=False, enableSessionTickets=False, fingerprint=True):
assert (privateKey is None) == (certificate is None), "Specify neither or both of privateKey and certificate"
self.privateKey = privateKey
self.certificate = certificate
if method is not None:
self.method = method
self.verify = verify
assert ((verify and caCerts) or
(not verify)), "Specify client CA certificate information if and only if enabling certificate verification"
self.caCerts = caCerts
self.verifyDepth = verifyDepth
self.requireCertificate = requireCertificate
self.verifyOnce = verifyOnce
self.enableSingleUseKeys = enableSingleUseKeys
self.enableSessions = enableSessions
self.fixBrokenPeers = fixBrokenPeers
self.enableSessionTickets = enableSessionTickets
self.fingerprint = fingerprint
def __getstate__(self):
d = self.__dict__.copy()
try:
del d['_context']
except KeyError:
pass
return d
def __setstate__(self, state):
self.__dict__ = state
def getContext(self):
if self._context is None:
self._context = self._makeContext()
return self._context
def _makeContext(self):
ctx = SSL.Context(self.method)
if self.certificate is not None and self.privateKey is not None:
ctx.use_certificate(self.certificate)
ctx.use_privatekey(self.privateKey)
ctx.check_privatekey()
verifyFlags = SSL.VERIFY_NONE
if self.verify or self.fingerprint:
verifyFlags = SSL.VERIFY_PEER
if self.requireCertificate:
verifyFlags |= SSL.VERIFY_FAIL_IF_NO_PEER_CERT
if self.verifyOnce:
verifyFlags |= SSL.VERIFY_CLIENT_ONCE
if self.caCerts:
store = ctx.get_cert_store()
for cert in self.caCerts:
store.add_cert(cert)
def _verifyCallback(conn, cert, errno, depth, preverify_ok):
if self.fingerprint:
digest = cert.digest("sha1")
if digest != self.fingerprint:
log.msg("Remote server fingerprint mismatch. Got: %s Expect: %s" % (digest, self.fingerprint))
return False
else:
log.msg("Remote server fingerprint match: %s " % (digest))
return True
return preverify_ok
ctx.set_verify(verifyFlags, _verifyCallback)
if self.verifyDepth is not None:
ctx.set_verify_depth(self.verifyDepth)
if self.enableSingleUseKeys:
ctx.set_options(SSL.OP_SINGLE_DH_USE)
if self.fixBrokenPeers:
ctx.set_options(self._OP_ALL)
if self.enableSessions:
sessionName = md5("%s-%d" % (reflect.qual(self.__class__), _sessionCounter())).hexdigest()
ctx.set_session_id(sessionName)
if not self.enableSessionTickets:
ctx.set_options(self._OP_NO_TICKET)
return ctx
class Communicator:
def __init__(self):
self.protocolInstances = {}
def register(self, protocol):
self.protocolInstances[protocol.identifier] = protocol
def isRegistered(self, protocol):
return protocol.identifier in self.protocolInstances
def unregister(self, protocol):
if protocol.identifier not in self.protocolInstances:
log.msg("No protocol instance with identifier %s."%protocol.identifier)
return
del self.protocolInstances[protocol.identifier]
def relay(self, protocol, message):
for identifier in self.protocolInstances.keys():
if identifier == protocol.identifier:
continue
instance = self.protocolInstances[identifier]
instance.sendLine(message)
#Global scope: all protocol instances will need this.
communicator = Communicator()
class IRCRelayer(irc.IRCClient):
def __init__(self, config):
self.network = config['host']
self.password = config['password']
self.channel = config['channel']
self.nickname = config['nick']
self.identifier = config['identifier']
self.heartbeatInterval = float(config['heartbeat'])
self.username = config['username']
self.realname = config['realname']
self.mode = config['mode']
self.nickcolor = config['nickcolor']
self.topicsync = config['topicsync']
log.msg("IRC Relay created. Name: %s | Host: %s | Channel: %s"%(self.nickname, self.network, self.channel))
# IRC RFC: https://tools.ietf.org/html/rfc2812#page-4
if len(self.nickname) > 9:
log.msg("Nickname %s is %d characters long, which exceeds the RFC maximum of 9 characters. This may cause connection problems."%(self.nickname, len(self.nickname)))
def formatUsername(self, username):
return username.split("!")[0]
def relay(self, message):
communicator.relay(self, message)
def signedOn(self):
log.msg("[%s] Connected to network."%self.network)
self.startHeartbeat()
self.join(self.channel, "")
def connectionLost(self, reason):
log.msg("[%s] Connection lost, unregistering."%self.network)
communicator.unregister(self)
def joined(self, channel):
log.msg("Joined channel %s, registering."%channel)
communicator.register(self)
def formatMessage(self, message):
return message.replace(self.nickname + ": ", "", 1)
def formatNick(self, user):
nick = "[" + self.formatUsername(user) + "]"
if self.nickcolor == "True":
nick = "[\x0303" + self.formatUsername(user) + "\x03]"
return nick
def privmsg(self, user, channel, message):
if self.mode != "RelayByCommand":
self.relay(":%s PRIVMSG %s :%s %s"%(user, channel, self.formatNick(user), message))
elif message.startswith(self.nickname + ':'):
self.relay(":%s PRIVMSG %s :%s %s"%(user, channel, self.formatNick(user), self.formatMessage(message)))
def kickedFrom(self, channel, kicker, message):
log.msg("Kicked by %s. Message \"%s\""%(kicker, message))
communicator.unregister(self)
def action(self, user, channel, message):
if self.mode != "RelayByCommand":
self.relay(":%s PRIVMSG %s :%s %s"%(user, channel, self.formatNick(user), message))
def topicUpdated(self, user, channel, newTopic):
if self.mode != "RelayByCommand":
self.topic(user, channel, newTopic)
def topic(self, user, channel, newTopic):
if self.topicsync == "True":
self.relay(":%s TOPIC %s :%s" %(user, channel, newTopic))
class RelayFactory(ReconnectingClientFactory):
protocol = IRCRelayer
#Log information which includes reconnection status.
noisy = True
def __init__(self, config):
config["identifier"] = "{0}{1}{2}".format(config["host"], config["port"], config["channel"])
self.config = config
def buildProtocol(self, addr):
#Connected - reset reconnect attempt delay.
self.maxDelay = 900
x = self.protocol(self.config)
x.factory = self
return x
#Remove the _<numbers> that FLIP puts on the end of usernames.
class FLIPRelayer(IRCRelayer):
def formatUsername(self, username):
return re.sub("_\d+$", "", IRCRelayer.formatUsername(self, username))
class FLIPFactory(RelayFactory):
protocol = FLIPRelayer
class NickServRelayer(IRCRelayer):
NickServ = "nickserv"
NickPollInterval = 30
def signedOn(self):
log.msg("[%s] Connected to network."%self.network)
self.startHeartbeat()
self.join(self.channel, "")
self.checkDesiredNick()
def checkDesiredNick(self):
"""
Checks that the nick is as desired, and if not attempts to retrieve it with
NickServ GHOST and trying again to change it after a polling interval.
"""
if self.nickname != self.desiredNick:
log.msg("[%s] Using GHOST to reclaim nick %s."%(self.network, self.desiredNick))
self.msg(NickServRelayer.NickServ, "GHOST %s %s"%(self.desiredNick, self.password))
# If NickServ does not respond try to regain nick anyway.
self.nickPoll.start(self.NickPollInterval)
def regainNickPoll(self):
if self.nickname != self.desiredNick:
log.msg("[%s] Reclaiming desired nick in polling."%(self.network))
self.setNick(self.desiredNick)
else:
log.msg("[%s] Have desired nick."%(self.network))
self.nickPoll.stop()
def nickChanged(self, nick):
log.msg("[%s] Nick changed from %s to %s."%(self.network, self.nickname, nick))
self.nickname = nick
self.checkDesiredNick()
def noticed(self, user, channel, message):
log.msg("[%s] Recieved notice \"%s\" from %s."%(self.network, message, user))
#Identify with nickserv if requested
if IRCRelayer.formatUsername(self, user).lower() == NickServRelayer.NickServ:
msg = message.lower()
if msg.startswith("this nickname is registered and protected"):
log.msg("[%s] Password requested; identifying with %s."%(self.network, NickServRelayer.NickServ))
self.msg(NickServRelayer.NickServ, "IDENTIFY %s"%self.password)
elif msg == "ghost with your nickname has been killed." or msg == "ghost with your nick has been killed.":
log.msg("[%s] GHOST successful, reclaiming nick %s."%(self.network,self.desiredNick))
self.setNick(self.desiredNick)
elif msg.endswith("isn't currently in use."):
log.msg("[%s] GHOST not needed, reclaiming nick %s."%(self.network,self.desiredNick))
self.setNick(self.desiredNick)
def __init__(self, config):
IRCRelayer.__init__(self, config)
self.password = config['nickServPassword']
self.desiredNick = config['nick']
self.nickPoll = LoopingCall(self.regainNickPoll)
class ReadOnlyRelayer(NickServRelayer):
pass
class CommandRelayer(IRCRelayer):
pass
class ReadOnlyFactory(RelayFactory):
protocol = ReadOnlyRelayer
class NickServFactory(RelayFactory):
protocol = NickServRelayer
class CommandFactory(RelayFactory):
protocol = CommandRelayer
def handler(signum, frame):
reactor.stop()
#Main if run as script, builtin for twistd.
if __name__ in ["__main__", "__builtin__"]:
main()