]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/python | |
2 | ||
3 | # Erebus IRC bot - Author: John Runyon | |
4 | # "Bot" and "BotConnection" classes (handling a specific "arm") | |
5 | ||
6 | #TODO: error checking | |
7 | ||
8 | import socket, sys | |
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 | |
15 | self.user = user | |
16 | self.realname = realname | |
17 | self.chans = chans | |
18 | ||
19 | self.conn = BotConnection(self, bind, server, port) | |
20 | def connect(self): | |
21 | if self.conn.connect(): | |
22 | self.parent.newfd(self, self.conn.socket.fileno()) | |
23 | ||
24 | def getdata(self): | |
25 | for line in self.conn.read(): | |
26 | print self.nick, '[I]', line | |
27 | ||
28 | if not self.conn.registered(): | |
29 | pieces = line.split() | |
30 | ||
31 | if pieces[0] == "PING": | |
32 | self.conn.send("PONG %s" % (pieces[1])) | |
33 | ||
34 | elif pieces[1] == "001": | |
35 | self.conn.registered(True) | |
36 | for c in self.chans: | |
37 | self.join(c) | |
38 | ||
39 | else: | |
40 | self.parse(line) | |
41 | def parse(self, line): | |
42 | pieces = line.split() | |
43 | ||
44 | if pieces[1] == "PRIVMSG": | |
45 | nick = pieces[0].split('!')[0][1:] | |
46 | user = self.parent.user(nick) | |
47 | target = pieces[2] | |
48 | msg = ' '.join(pieces[3:])[1:] | |
49 | self.parsemsg(user, target, msg) | |
50 | ||
51 | elif pieces[0] == "PING": | |
52 | self.conn.send("PONG %s" % (pieces[1])) | |
53 | ||
54 | elif pieces[1] == "354": # WHOX | |
55 | qt = pieces[3] | |
56 | nick = pieces[4] | |
57 | auth = pieces[5] | |
58 | if auth != '0': | |
59 | self.parent.user(nick).authed(auth) | |
60 | ||
61 | elif pieces[1] == "JOIN": | |
62 | nick = pieces[0].split('!')[0][1:] | |
63 | user = self.parent.user(nick) | |
64 | chan = self.parent.channel(pieces[2]) | |
65 | ||
66 | if nick == self.nick: | |
67 | self.conn.send("WHO %s %%ant,1" % (chan)) | |
68 | else: | |
69 | pass #TODO TODO TODO add to common chans! | |
70 | ||
71 | def parsemsg(self, user, target, msg): | |
72 | chan = None | |
73 | triggerused = msg[0] == self.parent.trigger | |
74 | if triggerused: msg = msg[1:] | |
75 | pieces = msg.split() | |
76 | ||
77 | if target == self.nick: | |
78 | chanword = pieces[1] | |
79 | if chanword[0] == '#': | |
80 | chan = self.parent.channel(chanword) | |
81 | pieces.pop(1) | |
82 | ||
83 | else: # message was sent to a channel | |
84 | chan = self.parent.channel(target) #TODO check if bot's on channel --- in Erebus.channel() maybe? | |
85 | if msg[0] == '*': # message may be addressed to bot by "*BOTNICK" trigger? | |
86 | if pieces[0][1:].lower() == self.nick.lower(): | |
87 | pieces.pop(0) # command actually starts with next word | |
88 | msg = ' '.join(pieces) # command actually starts with next word | |
89 | elif not triggerused: | |
90 | return # not to bot, don't process! | |
91 | ||
92 | cmd = pieces[0].lower() | |
93 | ||
94 | print "%r %r %r %r" % (cmd, user, target, msg) | |
95 | ||
96 | if self.parent.hashook(cmd): | |
97 | callback = self.parent.gethook(cmd) | |
98 | if chan is None and callback.needchan: | |
99 | self.msg(user, "You need to specify a channel for that command.") | |
100 | return | |
101 | if user.glevel >= callback.reqglevel: | |
102 | #TODO TODO TODO check reqclevel | |
103 | callback(self, user, chan, target, *pieces[1:]) | |
104 | return | |
105 | ||
106 | self.msg(user, "No such command, or you don't have access.") | |
107 | ||
108 | def msg(self, target, msg): | |
109 | if isinstance(target, self.parent.User): self.conn.send("NOTICE %s :%s" % (target.nick, msg)) | |
110 | elif isinstance(target, self.parent.Channel): self.conn.send("PRIVMSG %s :%s" % (target.name, msg)) | |
111 | elif isinstance(target, basestring): | |
112 | if target[0] == '#': self.conn.send("PRIVMSG %s :%s" % (target, msg)) | |
113 | else: self.conn.send("NOTICE %s :%s" % (target, msg)) | |
114 | else: raise TypeError('Bot.msg() "target" must be Erebus.User, Erebus.Channel, or string') | |
115 | ||
116 | def join(self, chan): | |
117 | self.conn.send("JOIN %s" % (chan)) | |
118 | ||
119 | def part(self, chan): | |
120 | self.conn.send("PART %s" % (chan)) | |
121 | ||
122 | def quit(self, reason="Shutdown"): | |
123 | self.conn.send("QUIT :%s" % (reason)) | |
124 | ||
125 | def __str__(self): return self.nick | |
126 | def __repr__(self): return "<Bot %r>" % (self.nick) | |
127 | ||
128 | class BotConnection(object): | |
129 | state = 0 # 0=disconnected, 1=registering, 2=connected | |
130 | ||
131 | def __init__(self, parent, bind, server, port): | |
132 | self.parent = parent | |
133 | self.buffer = '' | |
134 | self.socket = None | |
135 | ||
136 | self.bind = bind | |
137 | self.server = server | |
138 | self.port = int(port) | |
139 | ||
140 | def connect(self): | |
141 | self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
142 | self.socket.bind((self.bind, 0)) | |
143 | self.socket.connect((self.server, self.port)) | |
144 | self.send("NICK %s" % (self.parent.nick)) | |
145 | self.send("USER %s 0 * :%s" % (self.parent.user, self.parent.realname)) | |
146 | self.state = 1 | |
147 | return True | |
148 | ||
149 | def registered(self, done=False): | |
150 | if done: self.state = 2 | |
151 | return self.state == 2 | |
152 | ||
153 | #TODO: rewrite send() to queue | |
154 | def send(self, line): | |
155 | print self.parent.nick, '[O]', line | |
156 | self.write(line) | |
157 | ||
158 | def write(self, line): | |
159 | self.socket.sendall(line+"\r\n") | |
160 | ||
161 | def read(self): | |
162 | self.buffer += self.socket.recv(8192) | |
163 | lines = [] | |
164 | ||
165 | while '\r\n' in self.buffer: | |
166 | pieces = self.buffer.split('\r\n', 1) | |
167 | lines.append(pieces[0]) | |
168 | self.buffer = pieces[1] | |
169 | ||
170 | return lines | |
171 | ||
172 | def __str__(self): return self.nick | |
173 | def __repr__(self): return "<BotConnection %r (%r)>" % (self.socket.fileno(), self.parent.nick) |