-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
759 lines (560 loc) · 22.1 KB
/
bot.py
File metadata and controls
759 lines (560 loc) · 22.1 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: set sw=4 sts=4 et tw=120 :
# Copyright (c) 2011 Alexander Færøy <ahf@0x90.dk>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from __future__ import with_statement
import sys
import simplejson as json
from fnmatch import fnmatch
from collections import deque
from twisted.words.protocols import irc
from twisted.internet import protocol, reactor, ssl
class ChannelState(object):
def __init__(self, name):
self.name = name
self.opped = False
def getName(self):
return self.name
def isOpped(self):
return self.opped
def setOpped(self, v):
self.opped = v
class Hostmask(object):
def __init__(self, nickname, username, hostname):
self.nickname = nickname
self.username = username
self.hostname = hostname
def getNickname(self):
return self.nickname
def getUsername(self):
return self.username
def getHostname(self):
return self.hostname
def getHostmask(self):
return str(self)
def __str__(self):
return "%s!%s@%s" % (self.nickname, self.username, self.hostname)
@staticmethod
def parse(hostmask):
tmp = hostmask.split('@')
if len(tmp) != 2:
return None
hostname = tmp[1]
tmp = tmp[0].split('!')
if len(tmp) != 2:
return None
nickname = tmp[0]
username = tmp[1]
return Hostmask(nickname, username, hostname)
class Channel(object):
def __init__(self, name, operators):
self.name = name
self.operators = operators
def getName(self):
return self.name
def getOperators(self):
return self.operators
def __repr__(self):
return self.name
class User(object):
def __init__(self, name, mask, userclass):
self.name = name
self.mask = mask
self.userClass = userclass
def getName(self):
return self.name
def getUserClass(self):
return self.userClass
def getMask(self):
return self.mask
def match(self, other):
return fnmatch(other, self.mask)
def __repr__(self):
return self.name
class Server(object):
def __init__(self, hostname, port, ssl):
self.hostname = hostname
self.port = port
self.ssl = ssl
def getHostname(self):
return self.hostname
def getPort(self):
return self.port
def isSecure(self):
return self.ssl
def __repr__(self):
return self.hostname
class UserRegistry(object):
def __init__(self):
self.users = {}
def registerUser(self, user):
self.users[user.getName()] = user
def find(self, username):
return self.users.get(username)
def contains(self, username):
return username in self.users
def getUsers(self):
return self.users.values()
def findMatches(self, hostmask):
r = []
for user in self.users.values():
if user.match(hostmask.getHostmask()):
r.append(user)
return r
class Configuration(object):
def __init__(self):
self.nickname = ""
self.username = ""
self.realname = ""
self.servers = deque()
self.channels = {}
self.userRegistry = UserRegistry()
self.currentServer = None
# Errors and warnings.
self.valid = True
self.errorMessages = []
self.warningMessages = []
def invalidate(self):
self.valid = False
def isValid(self):
return self.valid
def hasUser(self, username):
return self.userRegistry.contains(username)
def findUser(self, username):
return self.userRegistry.find(username)
def setNickname(self, nickname):
self.nickname = nickname
def getNickname(self):
return self.nickname
def setUsername(self, username):
self.username = username
def getUsername(self):
return self.username
def setRealname(self, realname):
self.realname = realname
def getRealname(self):
return self.realname
def appendErrorMessage(self, errorMessage):
self.errorMessages.append(errorMessage)
def getErrorMessages(self):
return self.errorMessages
def appendWarningMessage(self, warningMessage):
self.warningMessages.append(warningMessage)
def getWarningMessages(self):
return self.warningMessages
def addUser(self, user):
self.userRegistry.registerUser(user)
def getUsers(self):
return self.userRegistry.getUsers()
def addServer(self, server):
self.servers.append(server)
def getServers(self):
return self.servers
def addChannel(self, channel):
self.channels[channel.getName()] = channel
def getChannels(self):
return self.channels.values()
def getChannel(self, channel):
return self.channels.get(channel)
class ConfigurationEncoder(json.JSONEncoder):
def default(self, config):
if isinstance(config, Configuration):
data = {
"bot": {},
"servers": [],
"users": [],
"channels": []
}
data["bot"]["nickname"] = config.getNickname()
data["bot"]["username"] = config.getUsername()
data["bot"]["realname"] = config.getRealname()
for s in config.getServers():
data["servers"].append({
"hostname": s.getHostname(),
"port": s.getPort(),
"ssl": s.isSecure()
})
for u in config.getUsers():
data["users"].append({
"name": u.getName(),
"mask": u.getMask(),
"class": u.getUserClass()
})
for c in config.getChannels():
data["channels"].append({
"name": c.getName(),
"operators": [o.getName() for o in c.getOperators()]
})
return data
return json.JSONEncoder(self, obj)
class ConfigurationDecoder(object):
required_keys = set(["bot", "servers", "users", "channels"])
valid_user_classes = set(["admin", "user"])
invalid_user_masks = set(["*", "*@*", "*!*", "*!*@*"])
def decode(self, obj):
config = Configuration()
# Sanity check.
for key in self.required_keys:
if key not in obj:
config.appendErrorMessage("Configuration file lacks '%s' key." % key)
config.invalidate()
# If our configuration file is invalid at this point, there is no need
# for us to continue inspecting the configuration file, so we simply
# return an invalid Configuration object to the caller which then can
# notify the user about what is wrong and revert to its current
# configuration file or exit if there is no configuration file to revert
# back to.
if not config.isValid():
return config
if "nickname" not in obj["bot"]:
config.appendErrorMessage("Missing nickname.")
config.invalidate()
if "username" not in obj["bot"]:
config.appendErrorMessage("Missing username.")
config.invalidate()
if "realname" not in obj["bot"]:
config.appendErrorMessage("Missing realname")
config.invalidate()
# Again, if our configuration file is invalid at this point, we might as
# well return an invalid configuration now.
if not config.isValid():
return config
# We have already checked for these:
config.setNickname(obj["bot"]["nickname"])
config.setUsername(obj["bot"]["username"])
config.setRealname(obj["bot"]["realname"])
# The only required key for a server is the port. We default to port
# 6667 and non-SSL if none of those keys are present.
for s in obj["servers"]:
if "hostname" not in s:
config.appendWarningMessage("Ignored server-entry due to lack of hostname.")
continue
hostname = s["hostname"]
port = 6667
ssl = False
if "port" in s:
port = s["port"]
if "ssl" in s:
if s["ssl"]:
ssl = True
config.addServer(Server(hostname, port, ssl))
# The only required keys for a user is the name and mask;
for u in obj["users"]:
if "name" not in u:
config.appendWarningMessage("Ignored user-entry due to lack of name.")
continue
if "mask" not in u:
config.appendWarningMessage("Ignored user-entry due to lack of mask.")
continue
if u["mask"] in self.invalid_user_masks:
config.appendWarningMessage("Insecure mask for user '%s'" % u["name"])
continue
name = u["name"]
mask = u["mask"]
userClass = "user"
if "class" in u:
if u["class"] not in self.valid_user_classes:
config.appendWarningMessage("Ignoring invalid class '%s'." % u["class"])
else:
userClass = u["class"]
config.addUser(User(name, mask, userClass))
# The only required key for a channel is the name itself.
for c in obj["channels"]:
if "name" not in c:
config.appendWarningMessage("Ignored channel-entry due to lack of name.")
continue
name = c["name"]
operators = set([])
# We are validating usernames here by checking with the
# configuration object if the user has already been added.
# Otherwise, we ignore it and emits a warning.
if "operators" in c:
for operator in c["operators"]:
if not config.hasUser(operator):
config.appendWarningMessage("Unknown operator '%s'" % operator)
continue
# Append the operator to the list of users. Remember the
# fact that objects in Python are passed as references.
operators.add(config.findUser(operator))
config.addChannel(Channel(name, operators))
return config
class ConfigurationLoader(object):
def load(self, filename):
decoder = ConfigurationDecoder()
content = {}
with open(filename) as f:
content = json.load(f, encoding = "ASCII")
return decoder.decode(content)
class ConfigurationSaver(object):
def save(self, filename, configuration):
content = json.dumps(configuration, cls = ConfigurationEncoder, indent = 4, sort_keys = True)
with open(filename, 'w') as f:
f.write(content + '\n')
class ConfigurationService(object):
def __init__(self, filename):
# This should only happen when the bot is initially run. In case there
# is an error here, we must shut down since there is no configuration
# file to revert back to.
loader = ConfigurationLoader()
self.filename = filename
self.config = loader.load(filename)
self.currentServer = None
self.channelStates = {}
for warning in self.config.getWarningMessages():
print " * %s (Warning)" % warning
if not self.config.isValid():
print "Errors and/or warnings was found whilst trying to load the configuration file:"
for error in self.config.getErrorMessages():
print " * %s (Error)" % error
sys.exit(1)
def reload(self):
loader = ConfigurationLoader()
config = loader.load(self.filename)
# FIXME: Refactor this code into a sanityCheck(self, config) method.
if not config.isValid():
# FIXME: Warn the user who is trying to reload that the reload failed.
return
# FIXME: Make sure that if, for example, the nickname has changed that
# we sent a NICK newnick to the server.
# merge()
self.config = config
def save(self):
saver + ConfigurationSaver()
saver.save(self.filename, self.config)
def getChannels(self):
return self.config.getChannels()
def getNickname(self):
return self.config.getNickname()
def getUsername(self):
return self.config.getUsername()
def getRealname(self):
return self.config.getRealname()
def findMatches(self, hostmask):
return self.config.userRegistry.findMatches(hostmask)
def nextServer(self):
servers = self.config.getServers()
self.currentServer = servers.popleft()
servers.append(self.currentServer)
return self.currentServer
def getChannelState(self, channel):
return self.channelStates.get(channel)
def joinedChannel(self, channel):
self.channelStates[channel] = ChannelState(channel)
def partedChannel(self, channel):
del self.channelStates[channel]
def findOperatorCandidates(self, hostmask, channel):
c = self.config.getChannel(channel)
# In this case, we are currently in a channel that is not listed in our configuration file. This could possibly
# be due to forced channel join by an operator. This is only possible on certain dodgy IRCd's, like unreal and
# friends, where operators have more power than God himself.
if c == None:
return []
r = []
for operator in c.getOperators():
if operator.match(hostmask.getHostmask()):
r.append(operator)
return r
class Client(irc.IRCClient):
def _getConfig(self):
return self.factory.config
def _getNickname(self):
return self.config.getNickname()
def _getUsername(self):
return self.config.getUsername()
def _getRealname(self):
return self.config.getRealname()
config = property(_getConfig)
nickname = property(_getNickname)
username = property(_getUsername)
realname = property(_getRealname)
def alterCollidedNick(self, nickname):
return nickname + "_"
def signedOn(self):
print ">>> Signed On"
for channel in self.config.getChannels():
self.join(channel.getName())
def joined(self, channel):
print ">>> Joining: %s" % channel
self.config.joinedChannel(channel)
def userJoined(self, user, channel):
print ">>> %s has joined %s" % (user, channel)
def privmsg(self, user, target, message):
if self.nickname != target:
return
hostmask = Hostmask.parse(user)
if hostmask is None:
return
print ">>> Message from '%s': '%s'" % (hostmask.getHostmask(), message)
parameters = message.split(" ")
length = len(parameters)
if length >= 1:
method = getattr(self, "cmd_%s" % parameters[0], None)
if method:
print ">>> Executing handler for command '%s'" % parameters[0]
method(hostmask, parameters)
else:
self.notice(hostmask.getNickname(), "Unknown Command: %s" % message)
def irc_JOIN(self, prefix, params):
if len(params) != 1:
return
hostmask = Hostmask.parse(prefix)
channel = params[0]
nickname = hostmask.getNickname()
# Default implementation from Twisted:
if nickname == self.nickname:
self.joined(channel)
else:
self.userJoined(nickname, channel)
self.considerOpping(hostmask, channel)
def op(self, channel, nick):
self.mode(channel, True, "o", user = nick)
def who(self, target):
self.sendLine("WHO %s" % target)
def ctcpUnknownQuery(self, user, channel, tag, data):
# Ignore unknown CTCP messages.
pass
def irc_unknown(self, prefix, command, params):
method = getattr(self, "irc_protocol_%s" % command, None)
if method:
method(prefix, command, params)
def ctcpQuery_OP(self, user, target, message):
if target != self.nickname:
return
if message is None:
return
hostmask = Hostmask.parse(user)
if hostmask is None:
return
channels = message.split(" ")
message = ", ".join(channels)
print ">>> CTCP OP from '%s' for channels: %s" % (hostmask.getHostmask(), message)
for channel in channels:
self.considerOpping(hostmask, channel)
def considerOpping(self, hostmask, channel):
cs = self.config.getChannelState(channel)
if not cs:
return
if not cs.isOpped():
return
print ">>> Considering giving op to %s on %s" % (hostmask.getNickname(), channel)
matches = self.config.findOperatorCandidates(hostmask, channel)
nick = hostmask.getNickname()
op = False
for match in matches:
print ">>> Found match for %s: '%s' matched '%s' for bot user '%s'" % (nick, hostmask.getHostmask(), match.getMask(), match.getName())
op = True
if op:
self.op(channel, nick)
def userLeft(self, user, channel):
print ">>> %s has left %s" % (user, channel)
def userQuit(self, user, quitMessage):
print ">>> %s has quit: %s" % (user, quitMessage)
def userKicked(self, kickee, channel, kicker, message):
print ">>> %s got kicked by %s on %s: %s" % (kickee, kicker, channel, message)
def kickedFrom(self, channel, kicker, message):
print ">>> %s kicked us from %s: %s" % (kicker, channel, message)
self.config.partedChannel(channel)
self.join(channel)
def modeChanged(self, user, channel, added, modes, args):
hostmask = Hostmask.parse(user)
if hostmask == None:
modeChanger = user
else:
modeChanger = hostmask.getNickname()
if added:
c = '+'
else:
c = '-'
# Icky.
if not args or not args[0]:
print ">>> Mode/%s [%c%s] by %s" % (channel, c, modes, modeChanger)
else:
print ">>> Mode/%s [%c%s %s] by %s" % (channel, c, modes, ' '.join(args), modeChanger)
# Only check for operator mode change.
pairedModes = zip(modes, args)
for mode in pairedModes:
modeChar = mode[0]
target = mode[1]
if modeChar == 'o':
if added:
self.userOpped(user, target, channel)
else:
self.userDeopped(user, target, channel)
def userOpped(self, user, target, channel):
# We got opped.
if self.nickname == target:
cs = self.config.getChannelState(channel)
if not cs:
return
cs.setOpped(True)
def userDeopped(self, user, target, channel):
# We got deopped.
if self.nickname == target:
cs = self.config.getChannelState(channel)
if not cs:
return
cs.setOpped(False)
def cmd_whoami(self, hostmask, parameters):
users = self.config.findMatches(hostmask)
target = hostmask.getNickname()
if users:
for user in users:
self.notice(target, "Username: '%s'" % user.getName())
self.notice(target, "Hostmask: '%s'" % user.getMask())
self.notice(target, "Class: '%s'" % user.getUserClass())
else:
self.notice(target, "Unknown user")
class ClientFactory(protocol.ClientFactory):
protocol = Client
def __init__(self, config):
self.config = config
def startedConnecting(self, connector):
destination = connector.getDestination()
print ">>> Connecting to: %s:%d" % (destination.host, destination.port)
def clientConnectionLost(self, connector, reason):
print ">>> Connection Lost: %s" % reason.getErrorMessage()
print ">>> Reconnecting in 10 seconds"
# FIXME: Handle server switch here.
reactor.callLater(10, connector.connect)
def clientConnectionFailed(self, connector, reason):
print ">>> Connection Failed: %s" % reason.getErrorMessage()
reactor.stop()
class Bot(object):
def __init__(self, filename):
config = ConfigurationService(filename)
server = config.nextServer()
clientFactory = ClientFactory(config)
if server.isSecure():
reactor.connectSSL(server.getHostname(), server.getPort(), clientFactory, ssl.ClientContextFactory())
else:
reactor.connectTCP(server.getHostname(), server.getPort(), clientFactory)
def runForever(self):
reactor.run()
if __name__ == "__main__":
if len(sys.argv) < 2:
print "Usage: %s path/to/config.json" % sys.argv[0]
sys.exit(1)
bot = Bot(sys.argv[1])
bot.runForever()