]>
Commit | Line | Data |
---|---|---|
f4f6442e JR |
1 | #!/usr/bin/python |
2 | ||
db75daab JR |
3 | # Erebus IRC bot - Author: John Runyon |
4 | # "Bot" and "BotConnection" classes (handling a specific "arm") | |
5 | ||
f4f6442e JR |
6 | #TODO: error checking |
7 | ||
9c935294 | 8 | import socket, sys |
f4f6442e JR |
9 | |
10 | #bots = {'erebus': bot.Bot(nick='Erebus', user='erebus', bind='', server='irc.quakenet.org', port=6667, realname='Erebus')} | |
11 | class Bot(object): | |
12 | def __init__(self, parent, nick, user, bind, server, port, realname, chans): | |
13 | self.parent = parent | |
14 | self.nick = nick | |
82696467 JR |
15 | self.user = user |
16 | self.realname = realname | |
f4f6442e JR |
17 | self.chans = chans |
18 | ||
82696467 | 19 | self.conn = BotConnection(self, bind, server, port) |
f4f6442e | 20 | def connect(self): |
db75daab JR |
21 | |
22 | if self.conn.connect(): | |
9c935294 JR |
23 | self.parent.newfd(self, self.conn.socket.fileno()) |
24 | ||
f4f6442e JR |
25 | def getdata(self): |
26 | for line in self.conn.read(): | |
9c935294 | 27 | print self.nick, '[I]', line |
ab7c0c0b | 28 | |
9c935294 JR |
29 | if not self.conn.registered(): |
30 | pieces = line.split() | |
ab7c0c0b | 31 | |
9c935294 JR |
32 | if pieces[0] == "PING": |
33 | self.conn.send("PONG %s" % (pieces[1])) | |
ab7c0c0b | 34 | |
9c935294 JR |
35 | elif pieces[1] == "001": |
36 | self.conn.registered(True) | |
37 | for c in self.chans: | |
38 | self.join(c) | |
f9767f79 | 39 | |
9c935294 JR |
40 | else: |
41 | self.parse(line) | |
f4f6442e JR |
42 | def parse(self, line): |
43 | pieces = line.split() | |
ab7c0c0b | 44 | |
9c935294 JR |
45 | if pieces[1] == "PRIVMSG": |
46 | nick = pieces[0].split('!')[0][1:] | |
47 | user = self.parent.user(nick) | |
48 | chan = self.parent.channel(pieces[2]) | |
49 | msg = ' '.join(pieces[3:])[1:] | |
50 | self.parsemsg(user, chan, msg) | |
ab7c0c0b | 51 | |
9c935294 JR |
52 | elif pieces[0] == "PING": |
53 | self.conn.send("PONG %s" % (pieces[1])) | |
ab7c0c0b | 54 | |
9c935294 JR |
55 | elif pieces[1] == "JOIN": |
56 | nick = pieces[0].split('!')[0][1:] | |
57 | user = self.parent.user(nick) | |
58 | chan = self.parent.channel(pieces[2]) #TODO TODO TODO | |
59 | ||
60 | def parsemsg(self, user, chan, msg): | |
61 | if msg[0] == '!': #TODO check config for trigger | |
62 | msg = msg[1:] | |
63 | else: | |
64 | return | |
ab7c0c0b | 65 | |
9c935294 | 66 | pieces = msg.split() |
d1ea05b0 | 67 | cmd = pieces[0].lower() |
ab7c0c0b | 68 | |
d1ea05b0 JR |
69 | if self.parent.hashook(cmd): |
70 | self.parent.gethook(cmd)(self, user, chan, *pieces[1:]) | |
9c935294 JR |
71 | |
72 | def msg(self, target, msg): | |
73 | if isinstance(target, self.parent.User): self.conn.send("NOTICE %s :%s" % (target.nick, msg)) | |
74 | elif isinstance(target, self.parent.Channel): self.conn.send("PRIVMSG %s :%s" % (target.name, msg)) | |
75 | elif isinstance(target, basestring): | |
76 | if target[0] == '#': self.conn.send("PRIVMSG %s :%s" % (target, msg)) | |
77 | else: self.conn.send("NOTICE %s :%s" % (target, msg)) | |
78 | else: raise TypeError('Bot.msg() "target" must be Erebus.User, Erebus.Channel, or string') | |
ab7c0c0b | 79 | |
9c935294 JR |
80 | def join(self, chan): |
81 | self.conn.send("JOIN %s" % (chan)) | |
ab7c0c0b | 82 | |
9c935294 JR |
83 | def part(self, chan): |
84 | self.conn.send("PART %s" % (chan)) | |
ab7c0c0b | 85 | |
9c935294 JR |
86 | def quit(self, reason="Shutdown"): |
87 | self.conn.send("QUIT :%s" % (reason)) | |
f4f6442e | 88 | |
82696467 JR |
89 | def __str__(self): return self.nick |
90 | def __repr__(self): return "<Bot %r>" % (self.nick) | |
91 | ||
f4f6442e JR |
92 | class BotConnection(object): |
93 | state = 0 # 0=disconnected, 1=registering, 2=connected | |
94 | ||
82696467 | 95 | def __init__(self, parent, bind, server, port): |
f4f6442e JR |
96 | self.parent = parent |
97 | self.buffer = '' | |
98 | self.socket = None | |
99 | ||
f4f6442e JR |
100 | self.bind = bind |
101 | self.server = server | |
102 | self.port = int(port) | |
f4f6442e JR |
103 | |
104 | def connect(self): | |
105 | self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
106 | self.socket.bind((self.bind, 0)) | |
107 | self.socket.connect((self.server, self.port)) | |
82696467 JR |
108 | self.send("NICK %s" % (self.parent.nick)) |
109 | self.send("USER %s 0 * :%s" % (self.parent.user, self.parent.realname)) | |
f4f6442e | 110 | self.state = 1 |
9c935294 | 111 | return True |
f4f6442e JR |
112 | |
113 | def registered(self, done=False): | |
114 | if done: self.state = 2 | |
115 | return self.state == 2 | |
116 | ||
f4f6442e JR |
117 | #TODO: rewrite send() to queue |
118 | def send(self, line): | |
82696467 | 119 | print self.parent.nick, '[O]', line |
f4f6442e | 120 | self.write(line) |
ab7c0c0b | 121 | |
f4f6442e | 122 | def write(self, line): |
f4f6442e | 123 | self.socket.sendall(line+"\r\n") |
ab7c0c0b | 124 | |
f4f6442e JR |
125 | def read(self): |
126 | self.buffer += self.socket.recv(8192) | |
127 | lines = [] | |
ab7c0c0b | 128 | |
f4f6442e JR |
129 | while '\r\n' in self.buffer: |
130 | pieces = self.buffer.split('\r\n', 1) | |
f4f6442e JR |
131 | lines.append(pieces[0]) |
132 | self.buffer = pieces[1] | |
ab7c0c0b | 133 | |
f4f6442e | 134 | return lines |
82696467 JR |
135 | |
136 | def __str__(self): return self.nick | |
137 | def __repr__(self): return "<BotConnection %r (%r)>" % (self.socket.fileno(), self.parent.nick) |