diff --git a/modules/irc.py b/modules/irc.py index 6117a8baec84459e0ff4da276606396078706c6b..c243c1da312f32d09b0dc965b1515371d6219661 100644 --- a/modules/irc.py +++ b/modules/irc.py @@ -1,41 +1,45 @@ +import socket import ssl -import irc.bot -LIMIT = 35 +CACHE_LIMIT = 35 +HOST = 'bouncer.ksal.de' +PORT = 28921 +AUTH_USER = 'infodisplay/Freenode' +AUTH_PW = 'Fagee9ie' +NICK = 'infodisplay' +CHANNEL = '#stratum0' -class InfodisplayClient(irc.bot.SingleServerIRCBot): - def __init__(self, emit, channel, nickname, server, port=6667): - pw = 'Fagee9ie' - ssl_factory = irc.connection.Factory(wrapper=ssl.wrap_socket) - irc.bot.SingleServerIRCBot.__init__(self, [(server, port, pw)], nickname, nickname, connect_factory=ssl_factory, username='infodisplay/Freenode') - self.channel = channel - self.emit = emit - self.cache = [] - - def on_nicknameinuse(self, c, e): - c.nick(c.get_nickname() + "_") - - def on_welcome(self, c, e): - c.join(self.channel) - - def on_pubmsg(self, c, e): - user = self._clean_str(e.source.nick) - if user == '***': - return - msg = self._clean_str(e.arguments[0]) - line = '<p><span>{}</span> {}</p>'.format(user, msg) - self.cache.append(line) - if len(self.cache) > LIMIT: - self.cache.pop(0) - self.emit(''.join(self.cache), outlet='inner') - - def _clean_str(self, s): - return s.replace('<','<').replace('>','>') +def _clean_str(s): + return s.replace('<','<').replace('>','>') def run(emit, log_err): - #TODO log errors emit('<h3> IRC #stratum0</h3><div class="chat" data-infodisplay-outlet="inner"></div>') - bot = InfodisplayClient(emit, '#stratum0', 'infodisplay', 'bouncer.ksal.de', port=28921) - bot.start() - + ctx = ssl.SSLContext(protocol=ssl.PROTOCOL_TLSv1) + while 1: + cache = [] + sock = socket.socket() + conn = ctx.wrap_socket(sock) + conn.connect((HOST,PORT)) + fp = conn.makefile('rb') + + conn.write('PASS {}:{}\r\n'.format(AUTH_USER, AUTH_PW).encode()) + conn.write('NICK {}\r\nUSER {} 8 * :{}\r\n'.format(NICK, AUTH_USER, NICK).encode()) + conn.write('JOIN {}\r\n'.format(CHANNEL).encode()) + try: + while 1: + line = fp.readline().decode() + source, command, args = line.split(' ', 2) + if command == 'PRIVMSG' and 'znc@znc.in' not in source: + channel, message = args.split(' :', 1) + sender = source.split('!', 1)[0][1:] + if channel == CHANNEL: + out = '<p><span>{}</span> {}</p>'.format(sender, _clean_str(message)) + cache.append(out) + if len(cache) > CACHE_LIMIT: + cache.pop(0) + emit(''.join(cache), outlet='inner') + finally: + log_err('Connection cycled') + fp.close() + conn.close()