2015-03-19 20:55:18 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import yaml
|
|
|
|
import imp
|
|
|
|
import os
|
2015-04-03 09:17:03 +02:00
|
|
|
import threading
|
|
|
|
import socket
|
2015-04-03 21:35:55 +02:00
|
|
|
import multiprocessing
|
|
|
|
import time
|
2015-03-19 20:55:18 +01:00
|
|
|
print('PyLink starting...')
|
|
|
|
|
|
|
|
with open("config.yml", 'r') as f:
|
|
|
|
conf = yaml.load(f)
|
|
|
|
|
|
|
|
# if conf['login']['password'] == 'changeme':
|
|
|
|
# print("You have not set the login details correctly! Exiting...")
|
|
|
|
|
2015-04-04 03:45:18 +02:00
|
|
|
class IrcUser():
|
2015-04-18 07:11:49 +02:00
|
|
|
def __init__(self, nick, ts, uid, ident='null', host='null',
|
|
|
|
realname='PyLink dummy client', realhost='null',
|
|
|
|
ip='0.0.0.0'):
|
2015-04-04 03:45:18 +02:00
|
|
|
self.nick = nick
|
2015-04-18 07:11:49 +02:00
|
|
|
self.ts = ts
|
|
|
|
self.uid = uid
|
|
|
|
self.ident = ident
|
|
|
|
self.host = host
|
|
|
|
self.realhost = realhost
|
|
|
|
self.ip = ip
|
|
|
|
self.realname = realname
|
2015-04-04 03:45:18 +02:00
|
|
|
|
2015-04-18 04:55:48 +02:00
|
|
|
class Irc():
|
|
|
|
def __init__(self):
|
2015-04-04 03:45:18 +02:00
|
|
|
# Initialize some variables
|
2015-04-03 09:17:03 +02:00
|
|
|
self.socket = socket.socket()
|
2015-04-18 04:55:48 +02:00
|
|
|
self.connected = False
|
2015-04-04 03:45:18 +02:00
|
|
|
self.users = {}
|
2015-04-18 07:11:49 +02:00
|
|
|
self.channels = {}
|
2015-04-18 04:55:48 +02:00
|
|
|
self.name = conf['server']['netname']
|
2015-04-03 09:17:03 +02:00
|
|
|
|
2015-04-18 04:55:48 +02:00
|
|
|
self.serverdata = conf['server']
|
2015-04-03 09:17:03 +02:00
|
|
|
ip = self.serverdata["ip"]
|
|
|
|
port = self.serverdata["port"]
|
|
|
|
self.sid = self.serverdata["sid"]
|
2015-04-18 04:55:48 +02:00
|
|
|
print("Connecting to network %r on %s:%s" % (self.name, ip, port))
|
2015-04-03 09:17:03 +02:00
|
|
|
|
|
|
|
protoname = self.serverdata['protocol']
|
2015-03-19 20:55:18 +01:00
|
|
|
# With the introduction of Python 3, relative imports are no longer
|
|
|
|
# allowed from normal applications ran from the command line. Instead,
|
|
|
|
# these imported libraries must be installed as a package using distutils
|
|
|
|
# or something similar.
|
|
|
|
#
|
|
|
|
# But I don't want that! Where PyLink is at right now (a total WIP), it is
|
|
|
|
# a lot more convenient to run the program directly from the source folder.
|
|
|
|
protocols_folder = [os.path.join(os.getcwd(), 'protocols')]
|
|
|
|
# Here, we override the module lookup and import the protocol module
|
|
|
|
# dynamically depending on which module was configured.
|
|
|
|
moduleinfo = imp.find_module(protoname, protocols_folder)
|
|
|
|
self.proto = imp.load_source(protoname, moduleinfo[1])
|
2015-04-03 09:17:03 +02:00
|
|
|
self.socket = socket.socket()
|
|
|
|
self.socket.connect((ip, port))
|
|
|
|
self.proto.connect(self)
|
2015-04-18 04:55:48 +02:00
|
|
|
self.connected = True
|
|
|
|
self.run()
|
2015-04-03 09:17:03 +02:00
|
|
|
|
2015-04-03 21:35:55 +02:00
|
|
|
def run(self):
|
2015-04-18 07:11:49 +02:00
|
|
|
buf = ""
|
|
|
|
data = ""
|
2015-04-18 04:55:48 +02:00
|
|
|
while self.connected:
|
2015-04-03 21:35:55 +02:00
|
|
|
try:
|
2015-04-18 07:11:49 +02:00
|
|
|
data += self.socket.recv(1024).decode("utf-8")
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
buf += data
|
|
|
|
while '\n' in buf:
|
|
|
|
line, buf = buf.split('\n', 1)
|
|
|
|
print("<- {}".format(line))
|
|
|
|
self.proto.handle_events(self, line)
|
2015-04-03 21:35:55 +02:00
|
|
|
except socket.error:
|
2015-04-18 04:55:48 +02:00
|
|
|
print('Received socket.error: %s, exiting.' % str(e))
|
|
|
|
self.connected = False
|
2015-04-18 07:11:49 +02:00
|
|
|
sys.exit(1)
|
2015-04-03 09:17:03 +02:00
|
|
|
|
|
|
|
def send(self, data):
|
|
|
|
data = data.encode("utf-8") + b"\n"
|
|
|
|
print("-> {}".format(data.decode("utf-8").strip("\n")))
|
|
|
|
self.socket.send(data)
|
2015-03-19 20:55:18 +01:00
|
|
|
|
2015-04-18 04:55:48 +02:00
|
|
|
irc_obj = Irc()
|