Skip to content
Snippets Groups Projects
Commit 25134500 authored by Kasalehlia's avatar Kasalehlia
Browse files

irc: use own simple implementation

parent beafd475
No related branches found
No related tags found
No related merge requests found
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('<','&lt;').replace('>','&gt;')
def _clean_str(s):
return s.replace('<','&lt;').replace('>','&gt;')
def run(emit, log_err):
#TODO log errors
emit('<h3>&nbsp;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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment