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

83 lines
2.4 KiB
Python
Raw Normal View History

2015-03-19 20:55:18 +01:00
#!/usr/bin/python3
import yaml
import imp
import os
import socket
import time
import sys
2015-05-31 07:35:00 +02:00
import logging
import proto
2015-03-19 20:55:18 +01:00
with open("config.yml", 'r') as f:
conf = yaml.load(f)
2015-05-31 07:35:00 +02:00
logger = logging.getLogger('pylinklogger')
# logger.setLevel(getattr(logging, conf['bot']['loglevel']))
logger.info('PyLink starting...')
2015-03-19 20:55:18 +01:00
# if conf['login']['password'] == 'changeme':
# print("You have not set the login details correctly! Exiting...")
class Irc():
def __init__(self):
# Initialize some variables
self.socket = socket.socket()
self.connected = False
self.users = {}
2015-04-18 07:11:49 +02:00
self.channels = {}
self.name = conf['server']['netname']
self.conf = conf
self.servers = {}
self.serverdata = conf['server']
ip = self.serverdata["ip"]
port = self.serverdata["port"]
self.sid = self.serverdata["sid"]
2015-05-31 07:35:00 +02:00
logger.info("Connecting to network %r on %s:%s" % (self.name, ip, port))
self.socket = socket.socket()
self.socket.connect((ip, port))
proto.connect(self)
self.connected = True
self.loaded = []
self.load_plugins()
self.run()
def run(self):
2015-04-18 07:11:49 +02:00
buf = ""
data = ""
while self.connected:
try:
data = self.socket.recv(2048).decode("utf-8")
buf += data
2015-04-18 07:11:49 +02:00
if not data:
break
while '\n' in buf:
line, buf = buf.split('\n', 1)
2015-05-31 07:35:00 +02:00
logger.debug("<- {}".format(line))
proto.handle_events(self, line)
except socket.error as e:
2015-05-31 07:35:00 +02:00
logger.error('Received socket.error: %s, exiting.' % str(e))
break
sys.exit(1)
def send(self, data):
data = data.encode("utf-8") + b"\n"
2015-05-31 07:35:00 +02:00
logger.debug("-> {}".format(data.decode("utf-8").strip("\n")))
self.socket.send(data)
2015-03-19 20:55:18 +01:00
def load_plugins(self):
to_load = conf['plugins']
plugins_folder = [os.path.join(os.getcwd(), 'plugins')]
# Here, we override the module lookup and import the plugins
# dynamically depending on which were configured.
for plugin in to_load:
moduleinfo = imp.find_module(plugin, plugins_folder)
self.loaded.append(imp.load_source(plugin, moduleinfo[1]))
2015-05-31 07:35:00 +02:00
logger.info("loaded plugins: %s" % self.loaded)
irc_obj = Irc()