2009-11-13 01:59:41 +00:00
|
|
|
import re
|
|
|
|
import socket
|
2009-12-12 19:11:04 +00:00
|
|
|
import time
|
2009-11-13 01:59:41 +00:00
|
|
|
import thread
|
|
|
|
import Queue
|
2010-02-22 23:24:01 +00:00
|
|
|
|
2010-02-22 19:33:03 +00:00
|
|
|
from ssl import wrap_socket, CERT_NONE, CERT_REQUIRED, SSLError
|
2009-11-13 01:59:41 +00:00
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2009-11-13 01:59:41 +00:00
|
|
|
def decode(txt):
|
|
|
|
for codec in ('utf-8', 'iso-8859-1', 'shift_jis', 'cp1252'):
|
|
|
|
try:
|
|
|
|
return txt.decode(codec)
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
continue
|
|
|
|
return txt.decode('utf-8', 'ignore')
|
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2010-12-31 19:01:54 +00:00
|
|
|
def censor(text):
|
|
|
|
replacement = '[censored]'
|
|
|
|
if 'censored_strings' in bot.config:
|
|
|
|
words = map(re.escape, bot.config['censored_strings'])
|
|
|
|
regex = re.compile('(%s)' % "|".join(words))
|
|
|
|
text = regex.sub(replacement, text)
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
2009-11-20 23:46:48 +00:00
|
|
|
class crlf_tcp(object):
|
2009-11-13 01:59:41 +00:00
|
|
|
"Handles tcp connections that consist of utf-8 lines ending with crlf"
|
|
|
|
|
2009-12-12 19:11:04 +00:00
|
|
|
def __init__(self, host, port, timeout=300):
|
2009-11-20 23:46:48 +00:00
|
|
|
self.ibuffer = ""
|
2009-11-13 01:59:41 +00:00
|
|
|
self.obuffer = ""
|
2010-03-01 02:32:41 +00:00
|
|
|
self.oqueue = Queue.Queue() # lines to be sent out
|
|
|
|
self.iqueue = Queue.Queue() # lines that were received
|
2010-02-22 19:33:03 +00:00
|
|
|
self.socket = self.create_socket()
|
2009-11-13 01:59:41 +00:00
|
|
|
self.host = host
|
|
|
|
self.port = port
|
2009-12-12 19:11:04 +00:00
|
|
|
self.timeout = timeout
|
2009-11-13 01:59:41 +00:00
|
|
|
|
2010-02-22 19:33:03 +00:00
|
|
|
def create_socket(self):
|
|
|
|
return socket.socket(socket.AF_INET, socket.TCP_NODELAY)
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2009-11-13 01:59:41 +00:00
|
|
|
def run(self):
|
2009-11-20 23:46:48 +00:00
|
|
|
self.socket.connect((self.host, self.port))
|
|
|
|
thread.start_new_thread(self.recv_loop, ())
|
|
|
|
thread.start_new_thread(self.send_loop, ())
|
2009-11-13 01:59:41 +00:00
|
|
|
|
2010-02-22 19:33:03 +00:00
|
|
|
def recv_from_socket(self, nbytes):
|
|
|
|
return self.socket.recv(nbytes)
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2010-02-22 19:33:03 +00:00
|
|
|
def get_timeout_exception_type(self):
|
|
|
|
return socket.timeout
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2010-02-22 19:33:03 +00:00
|
|
|
def handle_receive_exception(self, error, last_timestamp):
|
|
|
|
if time.time() - last_timestamp > self.timeout:
|
|
|
|
self.iqueue.put(StopIteration)
|
|
|
|
self.socket.close()
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2009-11-20 23:46:48 +00:00
|
|
|
def recv_loop(self):
|
2009-12-12 19:11:04 +00:00
|
|
|
last_timestamp = time.time()
|
2009-11-20 23:46:48 +00:00
|
|
|
while True:
|
2009-11-21 22:55:52 +00:00
|
|
|
try:
|
2010-02-22 19:33:03 +00:00
|
|
|
data = self.recv_from_socket(4096)
|
2009-12-12 19:11:04 +00:00
|
|
|
self.ibuffer += data
|
|
|
|
if data:
|
|
|
|
last_timestamp = time.time()
|
|
|
|
else:
|
|
|
|
if time.time() - last_timestamp > self.timeout:
|
|
|
|
self.iqueue.put(StopIteration)
|
|
|
|
self.socket.close()
|
|
|
|
return
|
|
|
|
time.sleep(1)
|
2011-05-11 20:40:04 +00:00
|
|
|
except (self.get_timeout_exception_type(), socket.error) as e:
|
2010-02-22 19:33:03 +00:00
|
|
|
if self.handle_receive_exception(e, last_timestamp):
|
2009-12-12 19:11:04 +00:00
|
|
|
return
|
2009-11-21 22:55:52 +00:00
|
|
|
continue
|
2010-02-11 23:29:45 +00:00
|
|
|
|
2009-11-20 23:46:48 +00:00
|
|
|
while '\r\n' in self.ibuffer:
|
|
|
|
line, self.ibuffer = self.ibuffer.split('\r\n', 1)
|
|
|
|
self.iqueue.put(decode(line))
|
2009-11-13 01:59:41 +00:00
|
|
|
|
2009-11-20 23:46:48 +00:00
|
|
|
def send_loop(self):
|
2009-11-13 01:59:41 +00:00
|
|
|
while True:
|
|
|
|
line = self.oqueue.get().splitlines()[0][:500]
|
|
|
|
print ">>> %r" % line
|
2009-11-20 23:46:48 +00:00
|
|
|
self.obuffer += line.encode('utf-8', 'replace') + '\r\n'
|
|
|
|
while self.obuffer:
|
|
|
|
sent = self.socket.send(self.obuffer)
|
|
|
|
self.obuffer = self.obuffer[sent:]
|
2009-11-13 01:59:41 +00:00
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2010-02-22 19:33:03 +00:00
|
|
|
class crlf_ssl_tcp(crlf_tcp):
|
|
|
|
"Handles ssl tcp connetions that consist of utf-8 lines ending with crlf"
|
2010-02-22 23:24:01 +00:00
|
|
|
def __init__(self, host, port, ignore_cert_errors, timeout=300):
|
|
|
|
self.ignore_cert_errors = ignore_cert_errors
|
2010-02-22 19:33:03 +00:00
|
|
|
crlf_tcp.__init__(self, host, port, timeout)
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2010-02-22 19:33:03 +00:00
|
|
|
def create_socket(self):
|
2010-03-01 02:32:41 +00:00
|
|
|
return wrap_socket(crlf_tcp.create_socket(self), server_side=False,
|
|
|
|
cert_reqs=CERT_NONE if self.ignore_cert_errors else
|
|
|
|
CERT_REQUIRED)
|
|
|
|
|
2010-02-22 19:33:03 +00:00
|
|
|
def recv_from_socket(self, nbytes):
|
|
|
|
return self.socket.read(nbytes)
|
|
|
|
|
|
|
|
def get_timeout_exception_type(self):
|
|
|
|
return SSLError
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2010-02-22 19:33:03 +00:00
|
|
|
def handle_receive_exception(self, error, last_timestamp):
|
|
|
|
# this is terrible
|
|
|
|
if not "timed out" in error.args[0]:
|
|
|
|
raise
|
|
|
|
return crlf_tcp.handle_receive_exception(self, error, last_timestamp)
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2009-11-13 01:59:41 +00:00
|
|
|
irc_prefix_rem = re.compile(r'(.*?) (.*?) (.*)').match
|
|
|
|
irc_noprefix_rem = re.compile(r'()(.*?) (.*)').match
|
|
|
|
irc_netmask_rem = re.compile(r':?([^!@]*)!?([^@]*)@?(.*)').match
|
|
|
|
irc_param_ref = re.compile(r'(?:^|(?<= ))(:.*|[^ ]+)').findall
|
|
|
|
|
|
|
|
|
2010-02-11 23:29:45 +00:00
|
|
|
class IRC(object):
|
2009-11-13 01:59:41 +00:00
|
|
|
"handles the IRC protocol"
|
|
|
|
#see the docs/ folder for more information on the protocol
|
2010-02-22 23:24:01 +00:00
|
|
|
def __init__(self, server, nick, port=6667, channels=[], conf={}):
|
2009-11-19 04:32:28 +00:00
|
|
|
self.channels = channels
|
|
|
|
self.conf = conf
|
2009-11-18 00:19:26 +00:00
|
|
|
self.server = server
|
2009-12-12 19:11:04 +00:00
|
|
|
self.port = port
|
|
|
|
self.nick = nick
|
2009-11-19 04:32:28 +00:00
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
self.out = Queue.Queue() # responses from the server are placed here
|
2009-11-13 01:59:41 +00:00
|
|
|
# format: [rawline, prefix, command, params,
|
|
|
|
# nick, user, host, paramlist, msg]
|
2009-12-12 19:11:04 +00:00
|
|
|
self.connect()
|
|
|
|
|
|
|
|
thread.start_new_thread(self.parse_loop, ())
|
|
|
|
|
2010-02-22 19:33:03 +00:00
|
|
|
def create_connection(self):
|
|
|
|
return crlf_tcp(self.server, self.port)
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2009-12-12 19:11:04 +00:00
|
|
|
def connect(self):
|
2010-02-22 19:33:03 +00:00
|
|
|
self.conn = self.create_connection()
|
2009-12-12 19:11:04 +00:00
|
|
|
thread.start_new_thread(self.conn.run, ())
|
2010-02-22 23:24:01 +00:00
|
|
|
self.set_pass(self.conf.get('server_password'))
|
2009-12-12 19:11:04 +00:00
|
|
|
self.set_nick(self.nick)
|
2010-01-17 20:20:11 +00:00
|
|
|
self.cmd("USER",
|
2010-02-22 23:24:01 +00:00
|
|
|
[conf.get('user', 'skybot'), "3", "*", conf.get('realname',
|
2010-08-24 21:59:47 +00:00
|
|
|
'Python bot - http://github.com/rmmh/skybot')])
|
2009-11-13 01:59:41 +00:00
|
|
|
|
|
|
|
def parse_loop(self):
|
|
|
|
while True:
|
|
|
|
msg = self.conn.iqueue.get()
|
2009-12-12 19:11:04 +00:00
|
|
|
|
|
|
|
if msg == StopIteration:
|
|
|
|
self.connect()
|
|
|
|
continue
|
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
if msg.startswith(":"): # has a prefix
|
2009-11-13 01:59:41 +00:00
|
|
|
prefix, command, params = irc_prefix_rem(msg).groups()
|
|
|
|
else:
|
|
|
|
prefix, command, params = irc_noprefix_rem(msg).groups()
|
|
|
|
nick, user, host = irc_netmask_rem(prefix).groups()
|
|
|
|
paramlist = irc_param_ref(params)
|
|
|
|
lastparam = ""
|
2010-02-22 23:24:01 +00:00
|
|
|
if paramlist:
|
|
|
|
if paramlist[-1].startswith(':'):
|
|
|
|
paramlist[-1] = paramlist[-1][1:]
|
|
|
|
lastparam = paramlist[-1]
|
2009-11-13 01:59:41 +00:00
|
|
|
self.out.put([msg, prefix, command, params, nick, user, host,
|
|
|
|
paramlist, lastparam])
|
|
|
|
if command == "PING":
|
2010-02-23 00:07:08 +00:00
|
|
|
self.cmd("PONG", paramlist)
|
2009-11-13 01:59:41 +00:00
|
|
|
|
2010-02-22 19:33:03 +00:00
|
|
|
def set_pass(self, password):
|
|
|
|
if password:
|
|
|
|
self.cmd("PASS", [password])
|
|
|
|
|
2009-11-18 00:19:26 +00:00
|
|
|
def set_nick(self, nick):
|
2009-11-13 01:59:41 +00:00
|
|
|
self.cmd("NICK", [nick])
|
|
|
|
|
|
|
|
def join(self, channel):
|
2010-02-22 23:24:01 +00:00
|
|
|
self.cmd("JOIN", [channel])
|
2009-11-13 01:59:41 +00:00
|
|
|
|
|
|
|
def msg(self, target, text):
|
2010-02-22 23:24:01 +00:00
|
|
|
self.cmd("PRIVMSG", [target, text])
|
2009-11-13 01:59:41 +00:00
|
|
|
|
|
|
|
def cmd(self, command, params=None):
|
|
|
|
if params:
|
2010-02-22 23:24:01 +00:00
|
|
|
params[-1] = ':' + params[-1]
|
2010-12-31 19:01:54 +00:00
|
|
|
self.send(command + ' ' + ' '.join(map(censor, params)))
|
2009-11-13 01:59:41 +00:00
|
|
|
else:
|
|
|
|
self.send(command)
|
|
|
|
|
|
|
|
def send(self, str):
|
|
|
|
self.conn.oqueue.put(str)
|
2010-02-11 23:29:45 +00:00
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2010-02-11 23:29:45 +00:00
|
|
|
class FakeIRC(IRC):
|
|
|
|
def __init__(self, server, nick, port=6667, channels=[], conf={}, fn=""):
|
|
|
|
self.channels = channels
|
|
|
|
self.conf = conf
|
|
|
|
self.server = server
|
|
|
|
self.port = port
|
|
|
|
self.nick = nick
|
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
self.out = Queue.Queue() # responses from the server are placed here
|
2010-02-11 23:29:45 +00:00
|
|
|
|
|
|
|
self.f = open(fn, 'rb')
|
|
|
|
|
|
|
|
thread.start_new_thread(self.parse_loop, ())
|
|
|
|
|
|
|
|
def parse_loop(self):
|
|
|
|
while True:
|
|
|
|
msg = decode(self.f.readline()[9:])
|
|
|
|
|
|
|
|
if msg == '':
|
|
|
|
print "!!!!DONE READING FILE!!!!"
|
|
|
|
return
|
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
if msg.startswith(":"): # has a prefix
|
2010-02-11 23:29:45 +00:00
|
|
|
prefix, command, params = irc_prefix_rem(msg).groups()
|
|
|
|
else:
|
|
|
|
prefix, command, params = irc_noprefix_rem(msg).groups()
|
|
|
|
nick, user, host = irc_netmask_rem(prefix).groups()
|
|
|
|
paramlist = irc_param_ref(params)
|
|
|
|
lastparam = ""
|
2010-02-22 23:24:01 +00:00
|
|
|
if paramlist:
|
|
|
|
if paramlist[-1].startswith(':'):
|
|
|
|
paramlist[-1] = paramlist[-1][1:]
|
|
|
|
lastparam = paramlist[-1]
|
2010-02-11 23:29:45 +00:00
|
|
|
self.out.put([msg, prefix, command, params, nick, user, host,
|
|
|
|
paramlist, lastparam])
|
|
|
|
if command == "PING":
|
|
|
|
self.cmd("PONG", [params])
|
|
|
|
|
|
|
|
def cmd(self, command, params=None):
|
2010-02-22 19:33:03 +00:00
|
|
|
pass
|
2010-03-01 02:32:41 +00:00
|
|
|
|
|
|
|
|
2010-02-22 19:33:03 +00:00
|
|
|
class SSLIRC(IRC):
|
2010-03-01 02:32:41 +00:00
|
|
|
def __init__(self, server, nick, port=6667, channels=[], conf={},
|
2010-02-22 23:24:01 +00:00
|
|
|
ignore_certificate_errors=True):
|
2010-02-22 23:49:30 +00:00
|
|
|
self.ignore_cert_errors = ignore_certificate_errors
|
2010-02-22 23:24:01 +00:00
|
|
|
IRC.__init__(self, server, nick, port, channels, conf)
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2010-02-22 19:33:03 +00:00
|
|
|
def create_connection(self):
|
2010-02-22 23:24:01 +00:00
|
|
|
return crlf_ssl_tcp(self.server, self.port, self.ignore_cert_errors)
|