mirror of
https://github.com/jlu5/PyLink.git
synced 2025-02-02 15:44:06 +01:00
more things
This commit is contained in:
parent
c074d58052
commit
80a2ce1d0a
32
main.py
32
main.py
@ -7,7 +7,6 @@ import threading
|
|||||||
import socket
|
import socket
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
import time
|
import time
|
||||||
|
|
||||||
print('PyLink starting...')
|
print('PyLink starting...')
|
||||||
|
|
||||||
with open("config.yml", 'r') as f:
|
with open("config.yml", 'r') as f:
|
||||||
@ -17,10 +16,17 @@ with open("config.yml", 'r') as f:
|
|||||||
# print("You have not set the login details correctly! Exiting...")
|
# print("You have not set the login details correctly! Exiting...")
|
||||||
|
|
||||||
class IrcUser():
|
class IrcUser():
|
||||||
def __init__(self, nick, timestamp, data={'uid': None}):
|
def __init__(self, nick, ts, uid, ident='null', host='null',
|
||||||
|
realname='PyLink dummy client', realhost='null',
|
||||||
|
ip='0.0.0.0'):
|
||||||
self.nick = nick
|
self.nick = nick
|
||||||
self.data = data
|
self.ts = ts
|
||||||
self.timestamp = timestamp
|
self.uid = uid
|
||||||
|
self.ident = ident
|
||||||
|
self.host = host
|
||||||
|
self.realhost = realhost
|
||||||
|
self.ip = ip
|
||||||
|
self.realname = realname
|
||||||
|
|
||||||
class Irc():
|
class Irc():
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -28,6 +34,7 @@ class Irc():
|
|||||||
self.socket = socket.socket()
|
self.socket = socket.socket()
|
||||||
self.connected = False
|
self.connected = False
|
||||||
self.users = {}
|
self.users = {}
|
||||||
|
self.channels = {}
|
||||||
self.name = conf['server']['netname']
|
self.name = conf['server']['netname']
|
||||||
|
|
||||||
self.serverdata = conf['server']
|
self.serverdata = conf['server']
|
||||||
@ -56,17 +63,22 @@ class Irc():
|
|||||||
self.run()
|
self.run()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
buf = ""
|
||||||
|
data = ""
|
||||||
while self.connected:
|
while self.connected:
|
||||||
try:
|
try:
|
||||||
data = self.socket.recv(1024)
|
data += self.socket.recv(1024).decode("utf-8")
|
||||||
if data:
|
if not data:
|
||||||
buf = data.decode("utf-8")
|
break
|
||||||
for line in buf.split("\n"):
|
buf += data
|
||||||
print("<- {}".format(line))
|
while '\n' in buf:
|
||||||
self.proto.handle_events(self, line)
|
line, buf = buf.split('\n', 1)
|
||||||
|
print("<- {}".format(line))
|
||||||
|
self.proto.handle_events(self, line)
|
||||||
except socket.error:
|
except socket.error:
|
||||||
print('Received socket.error: %s, exiting.' % str(e))
|
print('Received socket.error: %s, exiting.' % str(e))
|
||||||
self.connected = False
|
self.connected = False
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
def send(self, data):
|
def send(self, data):
|
||||||
data = data.encode("utf-8") + b"\n"
|
data = data.encode("utf-8") + b"\n"
|
||||||
|
@ -3,8 +3,10 @@ import socket
|
|||||||
import time
|
import time
|
||||||
import re
|
import re
|
||||||
import string
|
import string
|
||||||
|
import sys
|
||||||
|
|
||||||
# Ugh... damn you, Python imports!
|
# TODO: make PyLink a package so I don't have to hack the import system
|
||||||
|
# like this.
|
||||||
from os import sys, path
|
from os import sys, path
|
||||||
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
|
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
|
||||||
from main import IrcUser
|
from main import IrcUser
|
||||||
@ -21,58 +23,133 @@ def next_uid(sid, level=-1):
|
|||||||
except StopIteration:
|
except StopIteration:
|
||||||
return UID(level-1)
|
return UID(level-1)
|
||||||
|
|
||||||
|
def _sendFromServer(irc, msg):
|
||||||
|
irc.send(':%s %s' % (irc.sid, msg))
|
||||||
|
|
||||||
|
def _sendFromUser(irc, msg, user=None):
|
||||||
|
if user is None:
|
||||||
|
user = irc.pseudoclient.uid
|
||||||
|
irc.send(':%s %s' % (user, msg))
|
||||||
|
|
||||||
|
def _join(irc, channel):
|
||||||
|
_sendFromUser(irc, "FJOIN {channel} {ts} +nt :,{uid}".format(sid=irc.sid,
|
||||||
|
ts=int(time.time()), uid=irc.pseudoclient.uid, channel=channel))
|
||||||
|
|
||||||
|
def _uidToNick(irc, uid):
|
||||||
|
for k, v in irc.users.items():
|
||||||
|
if v.uid == uid:
|
||||||
|
return k
|
||||||
|
|
||||||
def connect(irc):
|
def connect(irc):
|
||||||
ts = int(time.time())
|
ts = int(time.time())
|
||||||
u = IrcUser('PyLink', ts)
|
host = irc.serverdata["hostname"]
|
||||||
u.data['uid'] = our_uid = next_uid(irc.sid)
|
uid = next_uid(irc.sid)
|
||||||
irc.users['PyLink'] = u
|
irc.pseudoclient = IrcUser('PyLink', ts, uid, 'pylink', host,
|
||||||
|
'PyLink Client')
|
||||||
|
irc.users['PyLink'] = irc.pseudoclient
|
||||||
|
|
||||||
f = irc.send
|
f = irc.send
|
||||||
f('CAPAB START 1202')
|
f('CAPAB START 1203')
|
||||||
f('CAPAB CAPABILITIES :NICKMAX=32 HALFOP=0 CHANMAX=65 MAXMODES=20 IDENTMAX=12 MAXQUIT=255 PROTOCOL=1203')
|
# This is hard coded atm... We should fix it eventually...
|
||||||
|
f('CAPAB CAPABILITIES :NICKMAX=32 HALFOP=0 CHANMAX=65 MAXMODES=20'
|
||||||
|
' IDENTMAX=12 MAXQUIT=255 PROTOCOL=1203')
|
||||||
f('CAPAB END')
|
f('CAPAB END')
|
||||||
f('SERVER %s %s 0 %s :PyLink Service' % (irc.serverdata["hostname"],
|
# TODO: check recvpass here
|
||||||
irc.serverdata["sendpass"], irc.sid))
|
f('SERVER {host} {Pass} 0 {sid} :PyLink Service'.format(host=host,
|
||||||
|
Pass=irc.serverdata["sendpass"], sid=irc.sid))
|
||||||
f(':%s BURST %s' % (irc.sid, ts))
|
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
|
# InspIRCd documentation:
|
||||||
f(":{sid} UID {uid} {ts} PyLink {host} {host} pylink 127.0.0.1 {ts} +o + :PyLink Client".format(sid=irc.sid,
|
# :751 UID 751AAAAAA 1220196319 Brain brainwave.brainbox.cc
|
||||||
ts=ts, host=irc.serverdata["hostname"], uid=our_uid))
|
# netadmin.chatspike.net brain 192.168.1.10 1220196324 +Siosw
|
||||||
|
# +ACKNOQcdfgklnoqtx :Craig Edwards
|
||||||
|
f(":{sid} UID {uid} {ts} PyLink {host} {host} pylink 127.0.0.1 {ts} +o +"
|
||||||
|
" :PyLink Client".format(sid=irc.sid, ts=ts,
|
||||||
|
host=host,
|
||||||
|
uid=uid))
|
||||||
f(':%s ENDBURST' % (irc.sid))
|
f(':%s ENDBURST' % (irc.sid))
|
||||||
|
_join(irc, irc.serverdata["channel"])
|
||||||
|
|
||||||
# :7NU PING 7NU 0AL
|
# :7NU PING 7NU 0AL
|
||||||
def handle_ping(irc, servernumeric, command, args):
|
def handle_ping(irc, servernumeric, command, args):
|
||||||
if args[3] == irc.sid:
|
if args[3] == irc.sid:
|
||||||
irc.send(':%s PONG %s' % (irc.sid, args[2]))
|
_sendFromServer(irc, 'PONG %s' % args[2])
|
||||||
|
|
||||||
def handle_privmsg(irc, numeric, command, args):
|
def handle_privmsg(irc, numeric, command, args):
|
||||||
irc.send(':0ALAAAAAA PRIVMSG %s :hello!' % numeric)
|
# _sendFromUser(irc, 'PRIVMSG %s :hello!' % numeric)
|
||||||
|
print(irc.users)
|
||||||
|
print(irc.channels)
|
||||||
|
|
||||||
def handle_error(irc, numeric, command, args):
|
def handle_error(irc, numeric, command, args):
|
||||||
print('Received an ERROR, killing!')
|
print('Received an ERROR, killing!')
|
||||||
irc.restart()
|
irc.connected = False
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
def handle_fjoin(irc, servernumeric, command, args):
|
||||||
|
# :70M FJOIN #chat 1423790411 +AFPfjnt 6:5 7:5 9:5 :o,1SRAABIT4 v,1IOAAF53R <...>
|
||||||
|
channel = args[0]
|
||||||
|
# tl;dr InspIRCd sends each user's channel data in the form of 'modeprefix(es),UID'
|
||||||
|
# We'll save each user in this format too, at least for now.
|
||||||
|
print(args)
|
||||||
|
users = args[-1].split()
|
||||||
|
users = [x.split(',') for x in users]
|
||||||
|
print(users)
|
||||||
|
|
||||||
|
'''
|
||||||
|
if channel not in irc.channels.keys():
|
||||||
|
irc.channels[channel]['users'] = users
|
||||||
|
else:
|
||||||
|
old_users = irc.channels[channel]['users'].copy()
|
||||||
|
old_users.update(users)
|
||||||
|
'''
|
||||||
|
|
||||||
|
def handle_uid(irc, numeric, command, args):
|
||||||
|
# :1SR UID 1SRAAAAAU 1428974823 synnero ow.my.eye.rs ow.my.eye.rs GLolol 2604:180:1::d34d:d87b 1425951245 +Wiosw +ACGKNOQXacfgklnoqvx :move along, nothing to see here!
|
||||||
|
uid, ts, nick, realhost, host, ident, ip = args[0:7]
|
||||||
|
realname = args[-1]
|
||||||
|
irc.users[nick] = IrcUser(nick, ts, uid, ident, host, realname, realhost, ip)
|
||||||
|
|
||||||
|
def handle_quit(irc, numeric, command, args):
|
||||||
|
# :1SRAAGB4T QUIT :Quit: quit message goes here
|
||||||
|
nick = _uidToNick(irc, numeric)
|
||||||
|
del irc.users[nick]
|
||||||
|
for k, v in irc.channels.items():
|
||||||
|
try:
|
||||||
|
del irc.channels[k][users][v]
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
def handle_events(irc, data):
|
def handle_events(irc, data):
|
||||||
|
# Each server message looks something like this:
|
||||||
|
# :70M FJOIN #chat 1423790411 +AFPfjnt 6:5 7:5 9:5 :v,1SRAAESWE
|
||||||
|
# :<sid> <command> <argument1> <argument2> ... :final multi word argument
|
||||||
try:
|
try:
|
||||||
args = data.split()
|
args = data.split()
|
||||||
real_args = []
|
real_args = []
|
||||||
for arg in args:
|
for arg in args:
|
||||||
real_args.append(arg)
|
real_args.append(arg)
|
||||||
|
# If the argument starts with ':' and ISN'T the first argument.
|
||||||
|
# The first argument is used for denoting the source UID/SID.
|
||||||
if arg.startswith(':') and args.index(arg) != 0:
|
if arg.startswith(':') and args.index(arg) != 0:
|
||||||
# : indicates that the argument has multiple words, and lasts until the remainder of the line
|
# : is used for multi-word arguments that last until the end
|
||||||
index = args.index(arg)
|
# of the message. We can use list splicing here to turn them all
|
||||||
|
# into one argument.
|
||||||
|
index = args.index(arg) # Get array index of arg
|
||||||
|
# Replace it with everything left on that line
|
||||||
arg = ' '.join(args[index:])[1:]
|
arg = ' '.join(args[index:])[1:]
|
||||||
|
# Cut the original argument list at before multi-line one, and
|
||||||
|
# merge them together.
|
||||||
real_args = args[:index]
|
real_args = args[:index]
|
||||||
real_args.append(arg)
|
real_args.append(arg)
|
||||||
break
|
break
|
||||||
real_args[0] = real_args[0].split(':', 1)[1]
|
real_args[0] = real_args[0].split(':', 1)[1]
|
||||||
args = real_args
|
args = real_args
|
||||||
# Strip leading :
|
|
||||||
|
|
||||||
numeric = args[0]
|
numeric = args[0]
|
||||||
command = args[1]
|
command = args[1]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# We will do wildcard event handling here. Unhandled events are just ignored, yay!
|
||||||
try:
|
try:
|
||||||
func = globals()['handle_'+command.lower()]
|
func = globals()['handle_'+command.lower()]
|
||||||
func(irc, numeric, command, args)
|
func(irc, numeric, command, args)
|
||||||
|
Loading…
Reference in New Issue
Block a user