-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathirc.py
64 lines (50 loc) · 2.14 KB
/
irc.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
from twisted.words.protocols import irc
from twisted.internet import protocol
class IRCBot(irc.IRCClient):
def _get_nickname(self):
return self.factory.config['nick']
nickname = property(_get_nickname)
def _get_username(self):
return self.factory.config['user']
username = property(_get_username)
def _get_realname(self):
return self.factory.config['realname']
realname = property(_get_realname)
def _get_password(self):
return self.factory.config['pass']
password = property(_get_password)
def _get_lineRate(self):
return self.factory.config['lineRate']
lineRate = property(_get_lineRate)
versionName = "TwistedCat"
def signedOn(self):
if 'oper' in self.factory.config:
self.sendLine('oper '+self.factory.config['oper']['user']+' '+self.factory.config['oper']['pass'])
for channel in self.factory.config['channels']:
if self.factory.config['channels'][channel] and self.factory.config['channels'][channel].has_key('key'):
self.join(channel, self.factory.config['channels'][channel]['key'])
else:
self.join(channel)
print "Signed on as %s." % (self.nickname,)
self.factory.irc = self
def joined(self, channel):
print "Joined %s." % (channel,)
class IRCBotFactory(protocol.ClientFactory):
protocol = IRCBot
def __init__(self, config):
self.config = config
def clientConnectionLost(self, connector, reason):
print "Lost connection (%s), reconnecting." % (reason,)
connector.connect()
def clientConnectionFailed(self, connector, reason):
print "Could not connect: %s" % (reason,)
def msg(self, message):
split = message.split()
if split[0] in self.config['channels'] or split[0] in self.config['users']:
print "Sending message to", split[0]
self.irc.msg(split[0], message[message.find(' ')+1:])
else:
for dest in self.config['channels']:
self.irc.msg(dest, message)
for dest in self.config['users']:
self.irc.msg(dest, message)