mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-01 01:09:22 +01:00
never mind - use multiprocessing, add auto-reconnect, and some more handlers
This commit is contained in:
parent
324551e185
commit
7a62a655a3
@ -20,13 +20,45 @@ def authenticate(irc):
|
||||
f(':%s ENDBURST' % (irc.sid))
|
||||
|
||||
# :7NU PING 7NU 0AL
|
||||
def handle_ping(irc, data):
|
||||
m = re.search('\:(\d[A-Z0-9]{1,2}) PING (\d[A-Z0-9]{1,2}) %s' % irc.sid, data)
|
||||
if m:
|
||||
irc.send(':%s PONG %s' % (irc.sid, m.group(0)))
|
||||
def handle_ping(irc, servernumeric, command, args):
|
||||
if args[3] == irc.sid:
|
||||
irc.send(':%s PONG %s' % (irc.sid, args[2]))
|
||||
|
||||
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):
|
||||
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):
|
||||
authenticate(irc)
|
||||
|
@ -5,7 +5,8 @@ import imp
|
||||
import os
|
||||
import threading
|
||||
import socket
|
||||
import asyncio
|
||||
import multiprocessing
|
||||
import time
|
||||
|
||||
print('PyLink starting...')
|
||||
|
||||
@ -18,13 +19,13 @@ with open("config.yml", 'r') as f:
|
||||
global networkobjects
|
||||
networkobjects = {}
|
||||
|
||||
class irc(asyncio.Protocol):
|
||||
def __init__(self, network, loop):
|
||||
asyncio.Protocol.__init__(self)
|
||||
class irc(multiprocessing.Process):
|
||||
def __init__(self, network):
|
||||
multiprocessing.Process.__init__(self)
|
||||
self.authenticated = False
|
||||
self.connected = False
|
||||
self.socket = socket.socket()
|
||||
self.loop = loop
|
||||
self.kill_received = False
|
||||
|
||||
self.serverdata = conf['networks'][network]
|
||||
ip = self.serverdata["ip"]
|
||||
@ -50,23 +51,35 @@ class irc(asyncio.Protocol):
|
||||
self.socket.connect((ip, port))
|
||||
self.proto.connect(self)
|
||||
|
||||
# def collect_incoming_data(self, data):
|
||||
|
||||
@asyncio.coroutine
|
||||
def handle_read(self):
|
||||
data = self.socket.recv(2048)
|
||||
buf = data.decode("utf-8")
|
||||
for line in buf.split("\n"):
|
||||
print("<- {}".format(line))
|
||||
self.proto.handle_events(self, line)
|
||||
def run(self):
|
||||
while not self.kill_received:
|
||||
try:
|
||||
data = self.socket.recv(1024)
|
||||
if data:
|
||||
buf = data.decode("utf-8")
|
||||
for line in buf.split("\n"):
|
||||
print("<- {}".format(line))
|
||||
self.proto.handle_events(self, line)
|
||||
except socket.error:
|
||||
self.restart()
|
||||
break
|
||||
|
||||
def send(self, data):
|
||||
data = data.encode("utf-8") + b"\n"
|
||||
print("-> {}".format(data.decode("utf-8").strip("\n")))
|
||||
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']:
|
||||
print('Creating IRC Object for: %s' % network)
|
||||
networkobjects[network] = irc(network)
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_forever(networkobjects[network].handle_read())
|
||||
networkobjects[network].start()
|
||||
|
Loading…
Reference in New Issue
Block a user