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

add a make-next-uid function and rename pylink-main.py -> main.py

The "I really hate Python imports" update.
This commit is contained in:
James Lu 2015-04-03 18:45:18 -07:00
parent 7a62a655a3
commit b1a989c971
2 changed files with 39 additions and 15 deletions

View File

@ -19,13 +19,20 @@ with open("config.yml", 'r') as f:
global networkobjects
networkobjects = {}
class irc(multiprocessing.Process):
class IrcUser():
def __init__(self, nick, timestamp, data={'uid': None}):
self.nick = nick
self.data = data
self.timestamp = timestamp
class Irc(multiprocessing.Process):
def __init__(self, network):
multiprocessing.Process.__init__(self)
self.authenticated = False
self.connected = False
# Initialize some variables
self.socket = socket.socket()
self.kill_received = False
self.users = {}
self.name = network
self.serverdata = conf['networks'][network]
ip = self.serverdata["ip"]
@ -33,7 +40,7 @@ class irc(multiprocessing.Process):
self.sid = self.serverdata["sid"]
print("[+] New thread started for %s:%s" % (ip, port))
self.name = network
protoname = self.serverdata['protocol']
# With the introduction of Python 3, relative imports are no longer
# allowed from normal applications ran from the command line. Instead,
@ -73,7 +80,7 @@ class irc(multiprocessing.Process):
print('Disconnected... Restarting IRC Object for: %s' % network)
time.sleep(1)
del networkobjects[network]
networkobjects[network] = irc(network)
networkobjects[network] = Irc(network)
def relay(self, line):
for network in networkobjects.values():
@ -81,5 +88,5 @@ class irc(multiprocessing.Process):
for network in conf['networks']:
print('Creating IRC Object for: %s' % network)
networkobjects[network] = irc(network)
networkobjects[network] = Irc(network)
networkobjects[network].start()

View File

@ -2,21 +2,41 @@ import threading
import socket
import time
import re
import string
class AuthenticationError:
pass
# Ugh... damn you, Python imports!
from os import sys, path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
from main import IrcUser
def authenticate(irc):
# From http://www.inspircd.org/wiki/Modules/spanningtree/UUIDs.html
chars = string.digits + string.ascii_uppercase
iters = [iter(chars) for _ in range(6)]
a = [next(i) for i in iters]
def next_uid(sid, level=-1):
try:
a[level] = next(iters[level])
return sid + ''.join(a)
except StopIteration:
return UID(level-1)
def connect(irc):
ts = int(time.time())
u = IrcUser('PyLink', ts)
u.data['uid'] = our_uid = next_uid(irc.sid)
irc.users['PyLink'] = u
f = irc.send
f('CAPAB START 1202')
f('CAPAB CAPABILITIES :NICKMAX=32 HALFOP=0 CHANMAX=65 MAXMODES=20 IDENTMAX=12 MAXQUIT=255 PROTOCOL=1203')
f('CAPAB END')
f('SERVER %s %s 0 %s :PyLink Service' % (irc.serverdata["hostname"],
irc.serverdata["sendpass"], irc.sid))
f(':%s BURST %s' % (irc.sid, int(time.time())))
f(':%s BURST %s' % (irc.sid, ts))
# :751 UID 751AAAAAA 1220196319 Brain brainwave.brainbox.cc netadmin.chatspike.net brain 192.168.1.10 1220196324 +Siosw +ACKNOQcdfgklnoqtx :Craig Edwards
f(":{sid} UID {sid}AAAAAA {ts} PyLink {host} {host} pylink 127.0.0.1 {ts} +o + :PyLink Client".format(sid=irc.sid,
ts=int(time.time()), host=irc.serverdata["hostname"]))
f(":{sid} UID {uid} {ts} PyLink {host} {host} pylink 127.0.0.1 {ts} +o + :PyLink Client".format(sid=irc.sid,
ts=ts, host=irc.serverdata["hostname"], uid=our_uid))
f(':%s ENDBURST' % (irc.sid))
# :7NU PING 7NU 0AL
@ -59,6 +79,3 @@ def handle_events(irc, data):
func(irc, numeric, command, args)
except KeyError: # unhandled event
pass
def connect(irc):
authenticate(irc)