add basic autoreconnect functionality -- reconnects if no messages in 5 minutes
This commit is contained in:
parent
9c44d8ce69
commit
bb29b225c6
2
bot.py
2
bot.py
|
@ -6,7 +6,7 @@ import Queue
|
||||||
|
|
||||||
sys.path += ['plugins'] # so 'import hook' works without duplication
|
sys.path += ['plugins'] # so 'import hook' works without duplication
|
||||||
sys.path += ['lib']
|
sys.path += ['lib']
|
||||||
os.chdir(sys.path[0]) # do stuff relative to the installation directory
|
os.chdir(sys.path[0] or '.') # do stuff relative to the installation directory
|
||||||
|
|
||||||
|
|
||||||
class Bot(object):
|
class Bot(object):
|
||||||
|
|
44
core/irc.py
44
core/irc.py
|
@ -1,10 +1,10 @@
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
import socket
|
import socket
|
||||||
|
import time
|
||||||
import thread
|
import thread
|
||||||
import Queue
|
import Queue
|
||||||
|
|
||||||
|
|
||||||
def decode(txt):
|
def decode(txt):
|
||||||
for codec in ('utf-8', 'iso-8859-1', 'shift_jis', 'cp1252'):
|
for codec in ('utf-8', 'iso-8859-1', 'shift_jis', 'cp1252'):
|
||||||
try:
|
try:
|
||||||
|
@ -17,7 +17,7 @@ def decode(txt):
|
||||||
class crlf_tcp(object):
|
class crlf_tcp(object):
|
||||||
"Handles tcp connections that consist of utf-8 lines ending with crlf"
|
"Handles tcp connections that consist of utf-8 lines ending with crlf"
|
||||||
|
|
||||||
def __init__(self, host, port):
|
def __init__(self, host, port, timeout=300):
|
||||||
self.ibuffer = ""
|
self.ibuffer = ""
|
||||||
self.obuffer = ""
|
self.obuffer = ""
|
||||||
self.oqueue = Queue.Queue() # lines to be sent out
|
self.oqueue = Queue.Queue() # lines to be sent out
|
||||||
|
@ -25,6 +25,7 @@ class crlf_tcp(object):
|
||||||
self.socket = socket.socket(socket.AF_INET, socket.TCP_NODELAY)
|
self.socket = socket.socket(socket.AF_INET, socket.TCP_NODELAY)
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
|
self.timeout = timeout
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.socket.connect((self.host, self.port))
|
self.socket.connect((self.host, self.port))
|
||||||
|
@ -32,11 +33,26 @@ class crlf_tcp(object):
|
||||||
thread.start_new_thread(self.send_loop, ())
|
thread.start_new_thread(self.send_loop, ())
|
||||||
|
|
||||||
def recv_loop(self):
|
def recv_loop(self):
|
||||||
|
last_timestamp = time.time()
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
self.ibuffer += self.socket.recv(4096)
|
data = self.socket.recv(4096)
|
||||||
except socket.timeout:
|
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 socket.timeout, e:
|
||||||
|
if time.time() - last_timestamp > self.timeout:
|
||||||
|
self.iqueue.put(StopIteration)
|
||||||
|
self.socket.close()
|
||||||
|
return
|
||||||
continue
|
continue
|
||||||
|
|
||||||
while '\r\n' in self.ibuffer:
|
while '\r\n' in self.ibuffer:
|
||||||
line, self.ibuffer = self.ibuffer.split('\r\n', 1)
|
line, self.ibuffer = self.ibuffer.split('\r\n', 1)
|
||||||
self.iqueue.put(decode(line))
|
self.iqueue.put(decode(line))
|
||||||
|
@ -63,21 +79,31 @@ class irc(object):
|
||||||
self.channels = channels
|
self.channels = channels
|
||||||
self.conf = conf
|
self.conf = conf
|
||||||
self.server = server
|
self.server = server
|
||||||
|
self.port = port
|
||||||
|
self.nick = nick
|
||||||
|
|
||||||
self.conn = crlf_tcp(server, port)
|
|
||||||
thread.start_new_thread(self.conn.run, ())
|
|
||||||
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,
|
||||||
# nick, user, host, paramlist, msg]
|
# nick, user, host, paramlist, msg]
|
||||||
self.nick = nick
|
self.connect()
|
||||||
self.set_nick(nick)
|
|
||||||
|
thread.start_new_thread(self.parse_loop, ())
|
||||||
|
|
||||||
|
def connect(self):
|
||||||
|
self.conn = crlf_tcp(self.server, self.port)
|
||||||
|
thread.start_new_thread(self.conn.run, ())
|
||||||
|
self.set_nick(self.nick)
|
||||||
self.cmd("USER", ["skybot", "3", "*",
|
self.cmd("USER", ["skybot", "3", "*",
|
||||||
":Python bot - http://bitbucket.org/Scaevolus/skybot/"])
|
":Python bot - http://bitbucket.org/Scaevolus/skybot/"])
|
||||||
thread.start_new_thread(self.parse_loop, ())
|
|
||||||
|
|
||||||
def parse_loop(self):
|
def parse_loop(self):
|
||||||
while True:
|
while True:
|
||||||
msg = self.conn.iqueue.get()
|
msg = self.conn.iqueue.get()
|
||||||
|
|
||||||
|
if msg == StopIteration:
|
||||||
|
self.connect()
|
||||||
|
continue
|
||||||
|
|
||||||
if msg.startswith(":"): #has a prefix
|
if msg.startswith(":"): #has a prefix
|
||||||
prefix, command, params = irc_prefix_rem(msg).groups()
|
prefix, command, params = irc_prefix_rem(msg).groups()
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import glob
|
import glob
|
||||||
import collections
|
import collections
|
||||||
import traceback
|
import traceback
|
||||||
|
import sys
|
||||||
|
|
||||||
if 'mtimes' not in globals():
|
if 'mtimes' not in globals():
|
||||||
mtimes = {}
|
mtimes = {}
|
||||||
|
@ -27,6 +28,8 @@ def reload(init=False):
|
||||||
globals())
|
globals())
|
||||||
except Exception:
|
except Exception:
|
||||||
traceback.print_exc(Exception)
|
traceback.print_exc(Exception)
|
||||||
|
if init: # stop if there's a syntax error in a core
|
||||||
|
sys.exit() # script on startup
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if filename == 'core/reload.py':
|
if filename == 'core/reload.py':
|
||||||
|
|
Loading…
Reference in New Issue