]>
Commit | Line | Data |
---|---|---|
b25d4368 | 1 | #!/usr/bin/python |
2 | ||
931c88a4 | 3 | # Erebus IRC bot - Author: John Runyon |
4 | # "Bot" and "BotConnection" classes (handling a specific "arm") | |
5 | ||
b25d4368 | 6 | #TODO: error checking |
7 | ||
49a455aa | 8 | import socket, sys |
b25d4368 | 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 | |
a12f7519 | 15 | self.user = user |
16 | self.realname = realname | |
b25d4368 | 17 | self.chans = chans |
18 | ||
a12f7519 | 19 | self.conn = BotConnection(self, bind, server, port) |
b25d4368 | 20 | def connect(self): |
b2a896c8 | 21 | if self.conn.connect(): |
49a455aa | 22 | self.parent.newfd(self, self.conn.socket.fileno()) |
23 | ||
b25d4368 | 24 | def getdata(self): |
25 | for line in self.conn.read(): | |
49a455aa | 26 | print self.nick, '[I]', line |
a4eacae2 | 27 | |
49a455aa | 28 | if not self.conn.registered(): |
29 | pieces = line.split() | |
a4eacae2 | 30 | |
49a455aa | 31 | if pieces[0] == "PING": |
32 | self.conn.send("PONG %s" % (pieces[1])) | |
a4eacae2 | 33 | |
49a455aa | 34 | elif pieces[1] == "001": |
35 | self.conn.registered(True) | |
36 | for c in self.chans: | |
37 | self.join(c) | |
43b98e4e | 38 | |
49a455aa | 39 | else: |
40 | self.parse(line) | |
b25d4368 | 41 | def parse(self, line): |
42 | pieces = line.split() | |
a4eacae2 | 43 | |
49a455aa | 44 | if pieces[1] == "PRIVMSG": |
45 | nick = pieces[0].split('!')[0][1:] | |
46 | user = self.parent.user(nick) | |
839d2b35 | 47 | target = pieces[2] |
49a455aa | 48 | msg = ' '.join(pieces[3:])[1:] |
839d2b35 | 49 | self.parsemsg(user, target, msg) |
a4eacae2 | 50 | |
49a455aa | 51 | elif pieces[0] == "PING": |
52 | self.conn.send("PONG %s" % (pieces[1])) | |
a4eacae2 | 53 | |
b2a896c8 | 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 | ||
49a455aa | 61 | elif pieces[1] == "JOIN": |
62 | nick = pieces[0].split('!')[0][1:] | |
63 | user = self.parent.user(nick) | |
b2a896c8 | 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! | |
49a455aa | 70 | |
839d2b35 | 71 | def parsemsg(self, user, target, msg): |
72 | chan = None | |
73 | triggerused = msg[0] == self.parent.trigger | |
74 | if triggerused: msg = msg[1:] | |
49a455aa | 75 | pieces = msg.split() |
839d2b35 | 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 | ||
db50981b | 92 | cmd = pieces[0].lower() |
a4eacae2 | 93 | |
839d2b35 | 94 | print "%r %r %r %r" % (cmd, user, target, msg) |
95 | ||
db50981b | 96 | if self.parent.hashook(cmd): |
b2a896c8 | 97 | callback = self.parent.gethook(cmd) |
839d2b35 | 98 | if chan is None and callback.needchan: |
99 | self.msg(user, "You need to specify a channel for that command.") | |
100 | return | |
861f83ef | 101 | if user.glevel >= callback.reqglevel: |
102 | #TODO TODO TODO check reqclevel | |
103 | callback(self, user, chan, target, *pieces[1:]) | |
b2a896c8 | 104 | return |
105 | ||
106 | self.msg(user, "No such command, or you don't have access.") | |
49a455aa | 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') | |
a4eacae2 | 115 | |
49a455aa | 116 | def join(self, chan): |
117 | self.conn.send("JOIN %s" % (chan)) | |
a4eacae2 | 118 | |
49a455aa | 119 | def part(self, chan): |
120 | self.conn.send("PART %s" % (chan)) | |
a4eacae2 | 121 | |
49a455aa | 122 | def quit(self, reason="Shutdown"): |
123 | self.conn.send("QUIT :%s" % (reason)) | |
b25d4368 | 124 | |
a12f7519 | 125 | def __str__(self): return self.nick |
126 | def __repr__(self): return "<Bot %r>" % (self.nick) | |
127 | ||
b25d4368 | 128 | class BotConnection(object): |
129 | state = 0 # 0=disconnected, 1=registering, 2=connected | |
130 | ||
a12f7519 | 131 | def __init__(self, parent, bind, server, port): |
b25d4368 | 132 | self.parent = parent |
133 | self.buffer = '' | |
134 | self.socket = None | |
135 | ||
b25d4368 | 136 | self.bind = bind |
137 | self.server = server | |
138 | self.port = int(port) | |
b25d4368 | 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)) | |
a12f7519 | 144 | self.send("NICK %s" % (self.parent.nick)) |
145 | self.send("USER %s 0 * :%s" % (self.parent.user, self.parent.realname)) | |
b25d4368 | 146 | self.state = 1 |
49a455aa | 147 | return True |
b25d4368 | 148 | |
149 | def registered(self, done=False): | |
150 | if done: self.state = 2 | |
151 | return self.state == 2 | |
152 | ||
b25d4368 | 153 | #TODO: rewrite send() to queue |
154 | def send(self, line): | |
a12f7519 | 155 | print self.parent.nick, '[O]', line |
b25d4368 | 156 | self.write(line) |
a4eacae2 | 157 | |
b25d4368 | 158 | def write(self, line): |
b25d4368 | 159 | self.socket.sendall(line+"\r\n") |
a4eacae2 | 160 | |
b25d4368 | 161 | def read(self): |
162 | self.buffer += self.socket.recv(8192) | |
163 | lines = [] | |
a4eacae2 | 164 | |
b25d4368 | 165 | while '\r\n' in self.buffer: |
166 | pieces = self.buffer.split('\r\n', 1) | |
b25d4368 | 167 | lines.append(pieces[0]) |
168 | self.buffer = pieces[1] | |
a4eacae2 | 169 | |
b25d4368 | 170 | return lines |
a12f7519 | 171 | |
172 | def __str__(self): return self.nick | |
173 | def __repr__(self): return "<BotConnection %r (%r)>" % (self.socket.fileno(), self.parent.nick) |