3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-01 09:19:23 +01:00

never mind - use multiprocessing, add auto-reconnect, and some more handlers

This commit is contained in:
James Lu 2015-04-03 12:35:55 -07:00
parent 324551e185
commit 7a62a655a3
2 changed files with 66 additions and 21 deletions

View File

@ -20,13 +20,45 @@ def authenticate(irc):
f(':%s ENDBURST' % (irc.sid)) f(':%s ENDBURST' % (irc.sid))
# :7NU PING 7NU 0AL # :7NU PING 7NU 0AL
def handle_ping(irc, data): def handle_ping(irc, servernumeric, command, args):
m = re.search('\:(\d[A-Z0-9]{1,2}) PING (\d[A-Z0-9]{1,2}) %s' % irc.sid, data) if args[3] == irc.sid:
if m: irc.send(':%s PONG %s' % (irc.sid, args[2]))
irc.send(':%s PONG %s' % (irc.sid, m.group(0)))
def handle_privmsg(irc, numeric, command, args):
irc.send(':0ALAAAAAA PRIVMSG %s :hello!' % numeric)
def handle_error(irc, numeric, command, args):
print('Received an ERROR, killing!')
irc.restart()
def handle_events(irc, data): def handle_events(irc, data):
handle_ping(irc, data) try:
args = data.split()
real_args = []
for arg in args:
real_args.append(arg)
if arg.startswith(':') and args.index(arg) != 0:
# : indicates that the argument has multiple words, and lasts until the remainder of the line
index = args.index(arg)
arg = ' '.join(args[index:])[1:]
real_args = args[:index]
real_args.append(arg)
break
real_args[0] = real_args[0].split(':', 1)[1]
args = real_args
# Strip leading :
numeric = args[0]
command = args[1]
print(args)
except IndexError:
return
try:
func = globals()['handle_'+command.lower()]
func(irc, numeric, command, args)
except KeyError: # unhandled event
pass
def connect(irc): def connect(irc):
authenticate(irc) authenticate(irc)

View File

@ -5,7 +5,8 @@ import imp
import os import os
import threading import threading
import socket import socket
import asyncio import multiprocessing
import time
print('PyLink starting...') print('PyLink starting...')
@ -18,13 +19,13 @@ with open("config.yml", 'r') as f:
global networkobjects global networkobjects
networkobjects = {} networkobjects = {}
class irc(asyncio.Protocol): class irc(multiprocessing.Process):
def __init__(self, network, loop): def __init__(self, network):
asyncio.Protocol.__init__(self) multiprocessing.Process.__init__(self)
self.authenticated = False self.authenticated = False
self.connected = False self.connected = False
self.socket = socket.socket() self.socket = socket.socket()
self.loop = loop self.kill_received = False
self.serverdata = conf['networks'][network] self.serverdata = conf['networks'][network]
ip = self.serverdata["ip"] ip = self.serverdata["ip"]
@ -50,23 +51,35 @@ class irc(asyncio.Protocol):
self.socket.connect((ip, port)) self.socket.connect((ip, port))
self.proto.connect(self) self.proto.connect(self)
# def collect_incoming_data(self, data): def run(self):
while not self.kill_received:
@asyncio.coroutine try:
def handle_read(self): data = self.socket.recv(1024)
data = self.socket.recv(2048) if data:
buf = data.decode("utf-8") buf = data.decode("utf-8")
for line in buf.split("\n"): for line in buf.split("\n"):
print("<- {}".format(line)) print("<- {}".format(line))
self.proto.handle_events(self, line) self.proto.handle_events(self, line)
except socket.error:
self.restart()
break
def send(self, data): def send(self, data):
data = data.encode("utf-8") + b"\n" data = data.encode("utf-8") + b"\n"
print("-> {}".format(data.decode("utf-8").strip("\n"))) print("-> {}".format(data.decode("utf-8").strip("\n")))
self.socket.send(data) self.socket.send(data)
def restart(self):
print('Disconnected... Restarting IRC Object for: %s' % network)
time.sleep(1)
del networkobjects[network]
networkobjects[network] = irc(network)
def relay(self, line):
for network in networkobjects.values():
self.proto.handle_events(self, line)
for network in conf['networks']: for network in conf['networks']:
print('Creating IRC Object for: %s' % network) print('Creating IRC Object for: %s' % network)
networkobjects[network] = irc(network) networkobjects[network] = irc(network)
loop = asyncio.get_event_loop() networkobjects[network].start()
loop.run_forever(networkobjects[network].handle_read())