fix slowness when connected to multiple networks & misc. stylistic changes
This commit is contained in:
parent
39a50f53f0
commit
923fdead1c
28
bot.py
28
bot.py
|
@ -1,8 +1,9 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
import sys
|
|
||||||
import os
|
import os
|
||||||
import Queue
|
import Queue
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
sys.path += ['plugins'] # so 'import hook' works without duplication
|
sys.path += ['plugins'] # so 'import hook' works without duplication
|
||||||
sys.path += ['lib']
|
sys.path += ['lib']
|
||||||
|
@ -33,16 +34,13 @@ try:
|
||||||
print 'ERROR: more than one connection named "%s"' % name
|
print 'ERROR: more than one connection named "%s"' % name
|
||||||
raise ValueError
|
raise ValueError
|
||||||
ssl = conf.get('ssl', False)
|
ssl = conf.get('ssl', False)
|
||||||
password = conf.get('password', None)
|
|
||||||
if ssl:
|
if ssl:
|
||||||
bot.conns[name] = SSLIRC(conf['server'], conf['nick'],
|
bot.conns[name] = SSLIRC(conf['server'], conf['nick'],
|
||||||
port=conf.get('port', 6667), channels=conf['channels'], conf=conf,
|
port=conf.get('port', 6667), channels=conf['channels'], conf=conf,
|
||||||
password=password,
|
ignore_certificate_errors=conf.get('ignore_cert', True))
|
||||||
ignoreCertificateErrors=conf.get('ignore_cert', True))
|
|
||||||
else:
|
else:
|
||||||
bot.conns[name] = IRC(conf['server'], conf['nick'],
|
bot.conns[name] = IRC(conf['server'], conf['nick'],
|
||||||
port=conf.get('port', 6667), channels=conf['channels'], conf=conf,
|
port=conf.get('port', 6667), channels=conf['channels'], conf=conf)
|
||||||
password=password)
|
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
print 'ERROR: malformed config file', Exception, e
|
print 'ERROR: malformed config file', Exception, e
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
@ -54,12 +52,14 @@ if not os.path.exists(bot.persist_dir):
|
||||||
print 'Running main loop'
|
print 'Running main loop'
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
reload() # these functions only do things
|
||||||
reload() # these functions only do things
|
config() # if changes have occured
|
||||||
config() # if changes have occured
|
|
||||||
|
|
||||||
for conn in bot.conns.itervalues():
|
for conn in bot.conns.itervalues():
|
||||||
out = conn.out.get(timeout=1)
|
try:
|
||||||
main(conn, out)
|
out = conn.out.get_nowait()
|
||||||
except Queue.Empty:
|
except Queue.Empty:
|
||||||
pass
|
pass
|
||||||
|
main(conn, out)
|
||||||
|
while all(conn.out.empty() for conn in bot.conns.itervalues()):
|
||||||
|
time.sleep(.3)
|
||||||
|
|
46
core/irc.py
46
core/irc.py
|
@ -4,6 +4,7 @@ import socket
|
||||||
import time
|
import time
|
||||||
import thread
|
import thread
|
||||||
import Queue
|
import Queue
|
||||||
|
|
||||||
from ssl import wrap_socket, CERT_NONE, CERT_REQUIRED, SSLError
|
from ssl import wrap_socket, CERT_NONE, CERT_REQUIRED, SSLError
|
||||||
|
|
||||||
def decode(txt):
|
def decode(txt):
|
||||||
|
@ -82,12 +83,13 @@ class crlf_tcp(object):
|
||||||
|
|
||||||
class crlf_ssl_tcp(crlf_tcp):
|
class crlf_ssl_tcp(crlf_tcp):
|
||||||
"Handles ssl tcp connetions that consist of utf-8 lines ending with crlf"
|
"Handles ssl tcp connetions that consist of utf-8 lines ending with crlf"
|
||||||
def __init__(self, host, port, ignoreCertErrors, timeout=300):
|
def __init__(self, host, port, ignore_cert_errors, timeout=300):
|
||||||
self.ignoreCertErrors = ignoreCertErrors
|
self.ignore_cert_errors = ignore_cert_errors
|
||||||
crlf_tcp.__init__(self, host, port, timeout)
|
crlf_tcp.__init__(self, host, port, timeout)
|
||||||
|
|
||||||
def create_socket(self):
|
def create_socket(self):
|
||||||
return wrap_socket(crlf_tcp.create_socket(self), server_side=False, cert_reqs = CERT_NONE if self.ignoreCertErrors else CERT_REQUIRED )
|
return wrap_socket(crlf_tcp.create_socket(self), server_side=False,
|
||||||
|
cert_reqs = [CERT_NONE, CERT_REQUIRED][self.ignore_cert_errors])
|
||||||
|
|
||||||
def recv_from_socket(self, nbytes):
|
def recv_from_socket(self, nbytes):
|
||||||
return self.socket.read(nbytes)
|
return self.socket.read(nbytes)
|
||||||
|
@ -110,13 +112,12 @@ irc_param_ref = re.compile(r'(?:^|(?<= ))(:.*|[^ ]+)').findall
|
||||||
class IRC(object):
|
class IRC(object):
|
||||||
"handles the IRC protocol"
|
"handles the IRC protocol"
|
||||||
#see the docs/ folder for more information on the protocol
|
#see the docs/ folder for more information on the protocol
|
||||||
def __init__(self, server, nick, port=6667, channels=[], conf={}, password=None):
|
def __init__(self, server, nick, port=6667, channels=[], conf={}):
|
||||||
self.channels = channels
|
self.channels = channels
|
||||||
self.conf = conf
|
self.conf = conf
|
||||||
self.server = server
|
self.server = server
|
||||||
self.port = port
|
self.port = port
|
||||||
self.nick = nick
|
self.nick = nick
|
||||||
self.password = password
|
|
||||||
|
|
||||||
self.out = Queue.Queue() #responses from the server are placed here
|
self.out = Queue.Queue() #responses from the server are placed here
|
||||||
# format: [rawline, prefix, command, params,
|
# format: [rawline, prefix, command, params,
|
||||||
|
@ -131,10 +132,10 @@ class IRC(object):
|
||||||
def connect(self):
|
def connect(self):
|
||||||
self.conn = self.create_connection()
|
self.conn = self.create_connection()
|
||||||
thread.start_new_thread(self.conn.run, ())
|
thread.start_new_thread(self.conn.run, ())
|
||||||
self.set_pass(self.password)
|
self.set_pass(self.conf.get('server_password'))
|
||||||
self.set_nick(self.nick)
|
self.set_nick(self.nick)
|
||||||
self.cmd("USER",
|
self.cmd("USER",
|
||||||
[conf.get('user', 'skybot'), "3", "*", ':' + conf.get('realname',
|
[conf.get('user', 'skybot'), "3", "*", conf.get('realname',
|
||||||
'Python bot - http://bitbucket.org/Scaevolus/skybot/')])
|
'Python bot - http://bitbucket.org/Scaevolus/skybot/')])
|
||||||
|
|
||||||
def parse_loop(self):
|
def parse_loop(self):
|
||||||
|
@ -152,8 +153,10 @@ class IRC(object):
|
||||||
nick, user, host = irc_netmask_rem(prefix).groups()
|
nick, user, host = irc_netmask_rem(prefix).groups()
|
||||||
paramlist = irc_param_ref(params)
|
paramlist = irc_param_ref(params)
|
||||||
lastparam = ""
|
lastparam = ""
|
||||||
if paramlist and paramlist[-1].startswith(':'):
|
if paramlist:
|
||||||
lastparam = paramlist[-1][1:]
|
if paramlist[-1].startswith(':'):
|
||||||
|
paramlist[-1] = paramlist[-1][1:]
|
||||||
|
lastparam = paramlist[-1]
|
||||||
self.out.put([msg, prefix, command, params, nick, user, host,
|
self.out.put([msg, prefix, command, params, nick, user, host,
|
||||||
paramlist, lastparam])
|
paramlist, lastparam])
|
||||||
if command == "PING":
|
if command == "PING":
|
||||||
|
@ -167,13 +170,14 @@ class IRC(object):
|
||||||
self.cmd("NICK", [nick])
|
self.cmd("NICK", [nick])
|
||||||
|
|
||||||
def join(self, channel):
|
def join(self, channel):
|
||||||
self.cmd("JOIN", [":"+channel])
|
self.cmd("JOIN", [channel])
|
||||||
|
|
||||||
def msg(self, target, text):
|
def msg(self, target, text):
|
||||||
self.cmd("PRIVMSG", [target, ":"+text])
|
self.cmd("PRIVMSG", [target, text])
|
||||||
|
|
||||||
def cmd(self, command, params=None):
|
def cmd(self, command, params=None):
|
||||||
if params:
|
if params:
|
||||||
|
params[-1] = ':' + params[-1]
|
||||||
self.send(command+' '+' '.join(params))
|
self.send(command+' '+' '.join(params))
|
||||||
else:
|
else:
|
||||||
self.send(command)
|
self.send(command)
|
||||||
|
@ -182,8 +186,6 @@ class IRC(object):
|
||||||
self.conn.oqueue.put(str)
|
self.conn.oqueue.put(str)
|
||||||
|
|
||||||
class FakeIRC(IRC):
|
class FakeIRC(IRC):
|
||||||
"handles the IRC protocol"
|
|
||||||
#see the docs/ folder for more information on the protocol
|
|
||||||
def __init__(self, server, nick, port=6667, channels=[], conf={}, fn=""):
|
def __init__(self, server, nick, port=6667, channels=[], conf={}, fn=""):
|
||||||
self.channels = channels
|
self.channels = channels
|
||||||
self.conf = conf
|
self.conf = conf
|
||||||
|
@ -192,8 +194,6 @@ class FakeIRC(IRC):
|
||||||
self.nick = nick
|
self.nick = nick
|
||||||
|
|
||||||
self.out = Queue.Queue() #responses from the server are placed here
|
self.out = Queue.Queue() #responses from the server are placed here
|
||||||
# format: [rawline, prefix, command, params,
|
|
||||||
# nick, user, host, paramlist, msg]
|
|
||||||
|
|
||||||
self.f = open(fn, 'rb')
|
self.f = open(fn, 'rb')
|
||||||
|
|
||||||
|
@ -214,11 +214,12 @@ class FakeIRC(IRC):
|
||||||
nick, user, host = irc_netmask_rem(prefix).groups()
|
nick, user, host = irc_netmask_rem(prefix).groups()
|
||||||
paramlist = irc_param_ref(params)
|
paramlist = irc_param_ref(params)
|
||||||
lastparam = ""
|
lastparam = ""
|
||||||
if paramlist and paramlist[-1].startswith(':'):
|
if paramlist:
|
||||||
lastparam = paramlist[-1][1:]
|
if paramlist[-1].startswith(':'):
|
||||||
|
paramlist[-1] = paramlist[-1][1:]
|
||||||
|
lastparam = paramlist[-1]
|
||||||
self.out.put([msg, prefix, command, params, nick, user, host,
|
self.out.put([msg, prefix, command, params, nick, user, host,
|
||||||
paramlist, lastparam])
|
paramlist, lastparam])
|
||||||
while self.out.qsize() > 5: time.sleep(.1)
|
|
||||||
if command == "PING":
|
if command == "PING":
|
||||||
self.cmd("PONG", [params])
|
self.cmd("PONG", [params])
|
||||||
|
|
||||||
|
@ -226,9 +227,10 @@ class FakeIRC(IRC):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class SSLIRC(IRC):
|
class SSLIRC(IRC):
|
||||||
def __init__(self, server, nick, port=6667, channels=[], conf={}, password=None, ignoreCertificateErrors=True):
|
def __init__(self, server, nick, port=6667, channels=[], conf={},
|
||||||
self.ignoreCertErrors = ignoreCertificateErrors
|
ignore_certificate_errors=True):
|
||||||
IRC.__init__(self, server, nick, port, channels, conf, password)
|
self.ignore_cert_errors = ignore_cert_errors
|
||||||
|
IRC.__init__(self, server, nick, port, channels, conf)
|
||||||
|
|
||||||
def create_connection(self):
|
def create_connection(self):
|
||||||
return crlf_ssl_tcp(self.server, self.port, self.ignoreCertErrors)
|
return crlf_ssl_tcp(self.server, self.port, self.ignore_cert_errors)
|
||||||
|
|
|
@ -33,8 +33,6 @@ irc_color_re = re.compile(r'(\x03(\d+,\d+|\d)|[\x0f\x02\x16\x1f])')
|
||||||
|
|
||||||
|
|
||||||
def get_log_filename(dir, server, chan):
|
def get_log_filename(dir, server, chan):
|
||||||
if chan.startswith(':'):
|
|
||||||
chan = chan[1:]
|
|
||||||
return os.path.join(dir, 'log', gmtime('%Y'), server,
|
return os.path.join(dir, 'log', gmtime('%Y'), server,
|
||||||
gmtime('%%s.%m-%d.log') % chan).lower()
|
gmtime('%%s.%m-%d.log') % chan).lower()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue