summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlassulus <lassulus@googlemail.com>2014-01-13 22:54:03 +0100
committerlassulus <lassulus@googlemail.com>2014-01-13 22:54:03 +0100
commit263a63168a61d7136a6c4eeea0cd175d40093300 (patch)
treeb88d57d922c0b7b23704647099ba8f21d5e9da08
parent6373abda0862359ddad91336040cfde5a252a64c (diff)
reaktor: refactor ircasy for more encapsulation
-rw-r--r--[-rwxr-xr-x]Reaktor/IRC/ircasy.py72
1 files changed, 49 insertions, 23 deletions
diff --git a/Reaktor/IRC/ircasy.py b/Reaktor/IRC/ircasy.py
index 0a9465fd..3d211ac5 100755..100644
--- a/Reaktor/IRC/ircasy.py
+++ b/Reaktor/IRC/ircasy.py
@@ -13,27 +13,42 @@ from re import split, search, match
from textwrap import TextWrapper
import logging,logging.handlers
-log = logging.getLogger('asybot')
-hdlr = logging.handlers.SysLogHandler(facility=logging.handlers.SysLogHandler.LOG_DAEMON)
-formatter = logging.Formatter( '%(filename)s: %(levelname)s: %(message)s')
-hdlr.setFormatter(formatter)
-log.addHandler(hdlr)
-logging.basicConfig(level = logging.INFO)
# s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g -- removes color codes
class asybot(asychat):
- def __init__(self, server, port, channels, realname, nickname, username, hostname, ircname, hammer_interval, alarm_timeout, kill_timeout):
+ def __init__(self, server, port, channels, nickname, realname=False, username=False, hostname=False, hammer_interval=10, alarm_timeout=300, kill_timeout=360):
asychat.__init__(self)
+ #logger magic
+ self.log = logging.getLogger('asybot')
+ hdlr = logging.handlers.SysLogHandler(facility=logging.handlers.SysLogHandler.LOG_DAEMON)
+ formatter = logging.Formatter( '%(filename)s: %(levelname)s: %(message)s')
+ hdlr.setFormatter(formatter)
+ self.log.addHandler(hdlr)
+ logging.basicConfig(level = logging.DEBUG)
+
+ self.nickname = nickname
+
+ if realname:
+ self.realname = realname
+ else:
+ self.realname = nickname
+
+ if username:
+ self.username = username
+ else:
+ self.username = nickname
+
+ if hostname:
+ self.hostname = hostname
+ else:
+ self.hostname = nickname
+
+ self.retry = False
self.server = server
self.port = port
self.channels = channels
- self.realname = realname
- self.nickname = nickname
- self.username = username
- self.hostname = hostname
- self.ircname = ircname
self.data = ''
self.myterminator = '\r\n'
self.set_terminator(self.myterminator.encode())
@@ -41,7 +56,7 @@ class asybot(asychat):
self.connect((self.server, self.port))
self.wrapper = TextWrapper(subsequent_indent=" ",width=400)
- log.info('=> irc://%s@%s:%s/%s' % (self.nickname, self.server, self.port, self.channels))
+ self.log.info('=> irc://%s@%s:%s/%s' % (self.nickname, self.server, self.port, self.channels))
# When we don't receive data for alarm_timeout seconds then issue a
# PING every hammer_interval seconds until kill_timeout seconds have
@@ -52,7 +67,6 @@ class asybot(asychat):
signal(SIGALRM, lambda signum, frame: self.alarm_handler())
self.reset_alarm()
-
def reset_alarm(self):
self.last_activity = date.now()
alarm(self.alarm_timeout)
@@ -60,18 +74,18 @@ class asybot(asychat):
def alarm_handler(self):
delta = date.now() - self.last_activity
if delta > timedelta(seconds=self.kill_timeout):
- log.error('No data for %s. Giving up...' % delta)
- exit(2)
+ self.log.error('No data for %s. Giving up...' % delta)
+ self.handle_disconnect()
else:
- log.error('No data for %s. PINGing server...' % delta)
+ self.log.error('No data for %s. PINGing server...' % delta)
self.push('PING :%s' % self.nickname)
alarm(self.hammer_interval)
def collect_incoming_data(self, data):
- self.data += data.decode(encoding='UTF-8')
+ self.data += data.decode()
def found_terminator(self):
- log.debug('<< %s' % self.data)
+ self.log.debug('<< %s' % self.data)
message = self.data
self.data = ''
@@ -82,7 +96,7 @@ class asybot(asychat):
if command == 'PING':
self.push('PONG :%s' % rest)
- log.debug("Replying to servers PING with PONG :%s" %rest)
+ self.log.debug("Replying to servers PING with PONG :%s" %rest)
elif command == 'PRIVMSG':
self.on_privmsg(prefix, command, params, rest)
@@ -98,24 +112,36 @@ class asybot(asychat):
self.reset_alarm()
def push(self, message):
- log.debug('>> %s' % message)
+ self.log.debug('>> %s' % message)
msg = (message + self.myterminator).encode()
- log.debug('>> %s' % msg)
+ self.log.debug('>> %s' % msg)
asychat.push(self, msg)
+ def reconnect(self):
+ self.push('QUIT')
+ self.close()
+ self.create_socket(AF_INET, SOCK_STREAM)
+ self.connect((self.server, self.port))
+
def handle_connect(self):
self.push('NICK %s' % self.nickname)
self.push('USER %s %s %s :%s' %
(self.username, self.hostname, self.server, self.realname))
self.push('JOIN %s' % ','.join(self.channels))
+ def handle_disconnect(self):
+ if self.retry:
+ self.reconnect()
+ else:
+ self.log.error('No retry set, giving up')
+
def on_privmsg(self, prefix, command, params, rest):
pass
def PRIVMSG(self, target, text):
for line in self.wrapper.wrap(text):
msg = 'PRIVMSG %s :%s' % (','.join(target), line)
- log.info(msg)
+ self.log.info(msg)
self.push(msg)
sleep(1)