-
Notifications
You must be signed in to change notification settings - Fork 4
/
ircbot.py
executable file
·318 lines (281 loc) · 11.1 KB
/
ircbot.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
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
#!/usr/bin/env python
import logging
from contextlib import closing
import socket
import argparse
import select
import sys
from textwrap import wrap
import os
import subprocess
import irclib
from time import sleep
class Socket(object):
"""
Socket is a struct containing a socket and two handlers :
on_write and on_read.
This class is used by Network to store each sockets with its
handlers.
"""
__slots__ = ("socket", "on_read", "on_write", "sockets")
def __init__(self, sock, on_read=None, on_write=None):
self.socket = sock
if on_read:
self.on_read = on_read
if on_write:
self.on_write = on_write
class Network(object):
"""
The Network class provides an easy way to listen on a TCP port.
It internally use select to block waiting for data, and permits
you to manually add and remove sockets to be monitored.
Basic usage is :
net = Network()
net.listen('127.0.0.1', 4242, sys.stdout.write)
net.run_forever()
public methods add_socket and remove_socket can be used if
another part or your code have opened sockets, and you want
Network to select on them.
"""
__slots__ = ('sockets', 'filenos')
def __init__(self):
self.sockets = []
self.filenos = {}
def listen(self, addr, port, on_read):
"""
Listen for TCP connections on addr:port.
The callback on_read is called, with the socket as single parameter,
when data is available.
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.add_socket(sock, lambda s: self.accept(s, on_read))
sock.bind((addr, port))
sock.listen(1)
def accept(self, sock, on_read):
"""
Used internally to accept a connection on a socket.
But if you need, you can use it, it will do a soc.accept()
then add the accepted sock to the list of monitored ones, so
when data is available, the callback on_read will be called.
"""
logging.debug("Accepting a connection")
conn, _ = sock.accept()
self.add_socket(conn, on_read)
def add_socket(self, sock, on_read=None, on_write=None):
"""
Manually add a socket and its on_read / on_write handlers
to this Network.
"""
logging.debug("Adding socket %d", sock.fileno())
self.sockets.append(sock)
self.filenos[sock.fileno()] = Socket(sock, on_read, on_write)
def remove_socket(self, sock):
"""
Manually remove a socket from the list of monitored ones.
"""
logging.debug("Removing socket %d", sock.fileno())
self.sockets.remove(sock)
del self.filenos[sock.fileno()]
def run_forever(self):
"""
Start the infinite loop listening on network.
"""
while True:
logging.debug("Entering select...")
(can_read, _, _) = select.select(self.sockets, [], [])
[self.filenos[sock.fileno()].on_read(sock) for sock in can_read]
class IRCBot:
"""
IRCBot is a basic bot that connect to a given serveur and channel with
a given nickname, and then, only tries to execute 'executables' in the
hooks directory named after the kinds of event it receive that
can be :
* error
* join
* kick
* mode
* part
* ping
* privmsg
* privnotice
* pubmsg <- Most usefull, it receive messages of the channel
* pubnotice
* quit
* invite
* pong
and a generic hook called for EVERY event, even if a hook exists for the
named event :
* all_raw_messages
"""
def __init__(self, server, chan, key, nickname, local_port):
self.network = Network()
self.chan = chan
self.key = key
self.ircobj = irclib.IRC(self.add_socket, self.rm_socket)
self.connection = self.ircobj.server()
self.ircobj.add_global_handler("all_events", self._dispatcher, -10)
self.ircobj.add_global_handler("welcome", self.on_connect)
self.ircobj.add_global_handler("nicknameinuse", self.on_nicknameinuse)
self.connection.connect(server, 6667, nickname)
self.network.listen("127.0.0.1", local_port, self.read_message)
self.network.run_forever()
def on_nicknameinuse(self, c, _):
c.nick(c.get_nickname() + "_")
def on_connect(self, connection, _):
connection.join(self.chan, self.key)
def read_message(self, sock):
"""
Read a message on the local socket and write it to the channel
"""
data = sock.recv(1024)
if not data:
self.network.remove_socket(sock)
else:
self.privmsg(self.chan, data)
def privmsg(self, to, message):
"""
Write a message to the channel
"""
if len(message) > 0:
for line in message.split('\n'):
wrapped = wrap(line, 512 - len("\r\n") -
len("PRIVMSG %s :" % self.chan))
for wrapped_line in wrapped:
if len(wrapped_line.strip()) > 0:
self.connection.privmsg(to, wrapped_line)
sleep(1)
def add_socket(self, sock):
self.network.add_socket(sock, lambda s: self.ircobj.process_data([s]))
def rm_socket(self, sock):
self.network.remove_socket(sock)
@staticmethod
def event_info(event):
source = event.source().split('!', 1) \
if event.source() is not None else []
s_login = source[0] if len(source) > 0 else ""
s_host = source[1] if len(source) > 1 else ""
target = event.target().split('!', 1) \
if event.target() is not None else []
t_login = target[0] if len(target) > 0 else ""
t_host = target[1] if len(target) > 1 else ""
return event.eventtype(), s_login, s_host, t_login, t_host
def _dispatcher(self, _, event):
etype, s_login, s_host, t_login, t_host = IRCBot.event_info(event)
try:
call = ['hooks-enabled/%s' % etype, s_login, s_host, t_login,
t_host] + event.arguments()
logging.info("calling: %s", str(call))
if os.path.isfile(call[0]):
message, debug = subprocess.Popen(call,
stdout=subprocess.PIPE)\
.communicate()
if len(message) == 0:
return
if debug is not None:
logging.info(" \_ debug '%s'", debug)
logging.info(" \_ response '%s'", message)
message_type, message = message.split(' ', 1)
if message_type == 'RAW':
for line in message.split("\n"):
self.connection.send_raw(line)
else:
self.privmsg(*message.split(' ', 1))
except Exception, ex:
logging.critical(ex)
logging.critical("while receiving %s of type %s from %s to %s",
str(event.arguments), etype, s_login, t_login)
def connect_to_irc(conf):
if conf.daemonize:
import daemon
with daemon.DaemonContext(working_directory=os.getcwd()):
IRCBot(conf.server, conf.chan, conf.key, conf.nickname,
conf.local_port)
else:
IRCBot(conf.server, conf.chan, conf.key, conf.nickname,
conf.local_port)
def netwrite(to_send, host, port):
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
sock.connect((host, port))
[sock.send(data) for data in to_send]
def say_something(conf):
if len(conf.message) == 1 and conf.message[0] == '-':
netwrite(sys.stdin, '127.0.0.1', conf.local_port)
else:
netwrite([' '.join(conf.message)], '127.0.0.1', conf.local_port)
HELP = {
"server": "IRC server address (ip or dns name)",
"chan": "Chan to join",
"nickname": "Nickname to use",
"local_port": "Local port to listen to messages",
"say_local_port": "Local listening port of the bot",
"verbose": "Be verbose (Show warning messazges)",
"vverbose": "Be very verbose (Show info messazges)",
"vvverbose": "Be very very verbose (Show debug messages)",
"quiet": "Be quiet",
"ircbot": "IRC Bot",
"connect": "Connect to a server",
"say": "Say something (once connected)",
"message": "What to say, '-' means stdin",
"daemonize": "Demonize the client process",
"key": "Channel key"}
def main():
parser = argparse.ArgumentParser(description=HELP['ircbot'])
parser.add_argument('-v', '--show-warnings',
dest='logging_level',
action='store_const',
const=logging.WARNING,
help=HELP['verbose'])
parser.add_argument('-vv', '--show-infos',
dest='logging_level',
action='store_const',
const=logging.INFO,
help=HELP['vverbose'])
parser.add_argument('-vvv', '--show-debugs',
dest='logging_level',
action='store_const',
const=logging.DEBUG,
help=HELP['vvverbose'])
parser.add_argument('-q', '--quiet',
dest='logging_level',
action='store_const',
const=logging.CRITICAL,
help=HELP['quiet'])
subparsers = parser.add_subparsers()
parser_client = subparsers.add_parser('connect', help=HELP['connect'])
parser_client.add_argument('server', help=HELP['server'])
parser_client.add_argument('chan', help=HELP['chan'])
parser_client.add_argument('nickname', help=HELP['nickname'])
parser_client.add_argument('-p', '--port',
dest="local_port",
help=HELP['local_port'],
type=int,
default=6668)
parser_client.add_argument('-d', '--daemonize',
action='store_true',
dest='daemonize',
help=HELP['daemonize'])
parser_client.add_argument('-k', '--key',
dest='key',
help=HELP['key'],
default='')
parser_client.set_defaults(func=connect_to_irc)
parser_say = subparsers.add_parser('say', help=HELP['say'])
parser_say.add_argument('message',
nargs='+',
help=HELP['message'])
parser_say.add_argument('-p', '--port',
dest="local_port",
help=HELP['say_local_port'],
type=int,
default=6668)
parser_say.set_defaults(func=say_something)
conf = parser.parse_args()
logging.basicConfig(level=conf.logging_level,
format='%(asctime)s %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
conf.func(conf)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
sys.exit(0)