This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/core/irc.py

264 lines
8.7 KiB
Python
Raw Normal View History

2011-11-20 10:23:31 +01:00
import re
import socket
import time
import thread
import Queue
from ssl import wrap_socket, CERT_NONE, CERT_REQUIRED, SSLError
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')
def censor(text):
2013-08-01 07:48:18 +02:00
text = text.replace('\n', '').replace('\r', '')
2011-11-20 10:23:31 +01:00
replacement = '[censored]'
if 'censored_strings' in bot.config:
2013-08-01 14:12:48 +02:00
if bot.config['censored_strings']:
words = map(re.escape, bot.config['censored_strings'])
2013-09-05 04:41:44 +02:00
regex = re.compile('({})'.format("|".join(words)))
2013-08-01 14:12:48 +02:00
text = regex.sub(replacement, text)
2011-11-20 10:23:31 +01:00
return text
class crlf_tcp(object):
2013-09-04 12:30:04 +02:00
"""Handles tcp connections that consist of utf-8 lines ending with crlf"""
2011-11-20 10:23:31 +01:00
def __init__(self, host, port, timeout=300):
self.ibuffer = ""
self.obuffer = ""
self.oqueue = Queue.Queue() # lines to be sent out
self.iqueue = Queue.Queue() # lines that were received
self.socket = self.create_socket()
self.host = host
self.port = port
self.timeout = timeout
def create_socket(self):
return socket.socket(socket.AF_INET, socket.TCP_NODELAY)
def run(self):
2015-02-18 22:55:28 +01:00
noerror = 0
while 1:
try:
self.socket.connect((self.host, self.port))
break
except socket.gaierror as e:
time.sleep(5)
except socket.timeout as e:
time.sleep(5)
2011-11-20 10:23:31 +01:00
thread.start_new_thread(self.recv_loop, ())
thread.start_new_thread(self.send_loop, ())
def recv_from_socket(self, nbytes):
return self.socket.recv(nbytes)
def get_timeout_exception_type(self):
return socket.timeout
def handle_receive_exception(self, error, last_timestamp):
2015-02-18 22:55:28 +01:00
print("Receive exception: %s" % (error))
2011-11-20 10:23:31 +01:00
if time.time() - last_timestamp > self.timeout:
2015-02-18 22:55:28 +01:00
print("Receive timeout. Restart connection.")
2011-11-20 10:23:31 +01:00
self.iqueue.put(StopIteration)
self.socket.close()
return True
return False
2015-02-18 22:55:28 +01:00
def handle_send_exception(self, error):
print("Send exception: %s" % (error))
self.iqueue.put(StopIteration)
self.socket.close()
return True
2011-11-20 10:23:31 +01:00
def recv_loop(self):
last_timestamp = time.time()
while True:
try:
2015-02-18 22:55:28 +01:00
data = self.recv_from_socket(4096)
2011-11-20 10:23:31 +01: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)
except (self.get_timeout_exception_type(), socket.error) as e:
if self.handle_receive_exception(e, last_timestamp):
return
continue
2015-02-18 22:55:28 +01:00
except AttributeError:
return
2011-11-20 10:23:31 +01:00
while '\r\n' in self.ibuffer:
line, self.ibuffer = self.ibuffer.split('\r\n', 1)
self.iqueue.put(decode(line))
def send_loop(self):
while True:
2015-02-18 22:55:28 +01:00
try:
line = self.oqueue.get().splitlines()[0][:500]
if line == StopIteration:
return
print ">>> %r" % line
self.obuffer += line.encode('utf-8', 'replace') + '\r\n'
while self.obuffer:
sent = self.socket.send(self.obuffer)
self.obuffer = self.obuffer[sent:]
2011-11-20 10:23:31 +01:00
2015-02-18 22:55:28 +01:00
except socket.error as e:
self.handle_send_exception(e)
return
2011-11-20 10:23:31 +01:00
class crlf_ssl_tcp(crlf_tcp):
2013-09-04 12:30:04 +02:00
"""Handles ssl tcp connetions that consist of utf-8 lines ending with crlf"""
2011-11-20 10:23:31 +01:00
def __init__(self, host, port, ignore_cert_errors, timeout=300):
self.ignore_cert_errors = ignore_cert_errors
crlf_tcp.__init__(self, host, port, timeout)
def create_socket(self):
return wrap_socket(crlf_tcp.create_socket(self), server_side=False,
2013-09-04 12:30:04 +02:00
cert_reqs=CERT_NONE if self.ignore_cert_errors else
CERT_REQUIRED)
2011-11-20 10:23:31 +01:00
def recv_from_socket(self, nbytes):
return self.socket.read(nbytes)
def get_timeout_exception_type(self):
return SSLError
def handle_receive_exception(self, error, last_timestamp):
# this is terrible
2015-02-18 22:55:28 +01:00
#if not "timed out" in error.args[0]:
# raise
2011-11-20 10:23:31 +01:00
return crlf_tcp.handle_receive_exception(self, error, last_timestamp)
2015-02-18 22:55:28 +01:00
def handle_send_exception(self, error):
return crlf_tcp.handle_send_exception(self, error)
2013-09-04 12:30:04 +02:00
2011-11-20 10:23:31 +01: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
class IRC(object):
2013-09-04 12:30:04 +02:00
"""handles the IRC protocol"""
2012-05-07 00:55:35 +02:00
def __init__(self, name, server, nick, port=6667, channels=[], conf={}):
self.name = name
2011-11-20 10:23:31 +01:00
self.channels = channels
self.conf = conf
self.server = server
self.port = port
self.nick = nick
2014-03-01 06:40:36 +01:00
self.history = {}
self.vars = {}
2011-11-20 10:23:31 +01:00
self.out = Queue.Queue() # responses from the server are placed here
# format: [rawline, prefix, command, params,
# nick, user, host, paramlist, msg]
self.connect()
thread.start_new_thread(self.parse_loop, ())
def create_connection(self):
return crlf_tcp(self.server, self.port)
def connect(self):
self.conn = self.create_connection()
thread.start_new_thread(self.conn.run, ())
self.set_pass(self.conf.get('server_password'))
self.set_nick(self.nick)
self.cmd("USER",
2013-09-04 12:30:04 +02:00
[conf.get('user', 'cloudbot'), "3", "*", conf.get('realname',
'CloudBot - http://git.io/cloudbot')])
2011-11-20 10:23:31 +01:00
def parse_loop(self):
while True:
2012-05-07 00:55:35 +02:00
# get a message from the input queue
2011-11-20 10:23:31 +01:00
msg = self.conn.iqueue.get()
if msg == StopIteration:
self.connect()
continue
2012-05-07 00:55:35 +02:00
# parse the message
2011-11-20 10:23:31 +01:00
if msg.startswith(":"): # has a prefix
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()
2013-08-01 11:08:41 +02:00
mask = nick + "!" + user + "@" + host
2011-11-20 10:23:31 +01:00
paramlist = irc_param_ref(params)
lastparam = ""
if paramlist:
if paramlist[-1].startswith(':'):
paramlist[-1] = paramlist[-1][1:]
lastparam = paramlist[-1]
2013-09-04 15:31:36 +02:00
# put the parsed message in the response queue
2011-11-20 10:23:31 +01:00
self.out.put([msg, prefix, command, params, nick, user, host,
2013-09-04 12:30:04 +02:00
mask, paramlist, lastparam])
2012-05-07 00:55:35 +02:00
# if the server pings us, pong them back
2011-11-20 10:23:31 +01:00
if command == "PING":
self.cmd("PONG", paramlist)
def set_pass(self, password):
if password:
self.cmd("PASS", [password])
def set_nick(self, nick):
self.cmd("NICK", [nick])
def join(self, channel):
2012-05-07 00:55:35 +02:00
""" makes the bot join a channel """
2013-09-05 04:41:44 +02:00
self.send("JOIN {}".format(channel))
if channel not in self.channels:
self.channels.append(channel)
2012-03-27 23:21:07 +02:00
def part(self, channel):
2012-05-07 00:55:35 +02:00
""" makes the bot leave a channel """
2012-03-27 23:21:07 +02:00
self.cmd("PART", [channel])
if channel in self.channels:
self.channels.remove(channel)
2011-11-20 10:23:31 +01:00
def msg(self, target, text):
2013-10-01 05:57:20 +02:00
""" makes the bot send a PRIVMSG to a target """
2011-11-20 10:23:31 +01:00
self.cmd("PRIVMSG", [target, text])
2013-10-01 05:55:18 +02:00
def ctcp(self, target, ctcp_type, text):
2013-10-01 05:57:20 +02:00
""" makes the bot send a PRIVMSG CTCP to a target """
2013-10-01 13:09:57 +02:00
out = u"\x01{} {}\x01".format(ctcp_type, text)
self.cmd("PRIVMSG", [target, out])
2013-10-01 05:55:18 +02:00
2011-11-20 10:23:31 +01:00
def cmd(self, command, params=None):
if params:
2013-10-08 23:49:16 +02:00
params[-1] = u':' + params[-1]
self.send(u"{} {}".format(command, ' '.join(params)))
2011-11-20 10:23:31 +01:00
else:
self.send(command)
def send(self, str):
self.conn.oqueue.put(str)
class SSLIRC(IRC):
2012-05-07 00:55:35 +02:00
def __init__(self, name, server, nick, port=6667, channels=[], conf={},
2011-11-20 10:23:31 +01:00
ignore_certificate_errors=True):
self.ignore_cert_errors = ignore_certificate_errors
2012-05-07 00:55:35 +02:00
IRC.__init__(self, name, server, nick, port, channels, conf)
2011-11-20 10:23:31 +01:00
def create_connection(self):
2012-02-29 06:09:40 +01:00
return crlf_ssl_tcp(self.server, self.port, self.ignore_cert_errors)