3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-26 04:32:51 +01:00
PyLink/main.py

117 lines
4.2 KiB
Python
Raw Normal View History

2015-03-19 20:55:18 +01:00
#!/usr/bin/python3
import imp
import os
import socket
import time
import sys
from collections import defaultdict
from log import log
2015-07-06 04:19:49 +02:00
import conf
import classes
2015-03-19 20:55:18 +01:00
class Irc():
2015-07-06 04:19:49 +02:00
def __init__(self, proto, conf):
# Initialize some variables
self.connected = False
self.name = conf['server']['netname']
self.conf = conf
# Server, channel, and user indexes to be populated by our protocol module
self.servers = {}
self.users = {}
self.channels = defaultdict(classes.IrcChannel)
# Sets flags such as whether to use halfops, etc. The default RFC1459
# modes are implied.
self.cmodes = {'op': 'o', 'secret': 's', 'private': 'p',
'noextmsg': 'n', 'moderated': 'm', 'inviteonly': 'i',
'topiclock': 't', 'limit': 'l', 'ban': 'b',
2015-07-06 04:19:49 +02:00
'voice': 'v', 'key': 'k',
# Type A, B, and C modes
'*A': 'b',
'*B': 'k',
'*C': 'l',
'*D': 'imnpstr'}
self.umodes = {'invisible': 'i', 'snomask': 's', 'wallops': 'w',
2015-07-06 04:19:49 +02:00
'oper': 'o',
'*A': '', '*B': '', '*C': 's', '*D': 'iow'}
self.maxnicklen = 30
2015-07-06 04:19:49 +02:00
self.prefixmodes = 'ov'
self.serverdata = conf['server']
2015-07-06 04:19:49 +02:00
self.sid = self.serverdata["sid"]
self.botdata = conf['bot']
2015-07-06 04:19:49 +02:00
self.proto = proto
self.connect()
def connect(self):
ip = self.serverdata["ip"]
port = self.serverdata["port"]
log.info("Connecting to network %r on %s:%s", self.name, ip, port)
self.socket = socket.socket()
self.socket.connect((ip, port))
2015-07-06 04:19:49 +02:00
self.proto.connect(self)
self.loaded = []
self.load_plugins()
self.connected = True
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)
log.debug("<- %s", line)
proto.handle_events(self, line)
except socket.error as e:
log.error('Received socket.error: %s, exiting.', str(e))
break
sys.exit(1)
def send(self, data):
data = data.encode("utf-8") + b"\n"
log.debug("-> %s", data.decode("utf-8").strip("\n"))
self.socket.send(data)
2015-03-19 20:55:18 +01:00
def load_plugins(self):
2015-07-06 04:19:49 +02:00
to_load = conf.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:
2015-06-08 02:04:23 +02:00
try:
moduleinfo = imp.find_module(plugin, plugins_folder)
self.loaded.append(imp.load_source(plugin, moduleinfo[1]))
except ImportError as e:
if str(e).startswith('No module named'):
log.error('Failed to load plugin %r: the plugin could not be found.', plugin)
2015-06-08 02:04:23 +02:00
else:
log.error('Failed to load plugin %r: import error %s', plugin, str(e))
2015-07-05 22:44:48 +02:00
log.info("loaded plugins: %s", self.loaded)
if __name__ == '__main__':
2015-07-05 22:44:48 +02:00
log.info('PyLink starting...')
2015-07-06 04:19:49 +02:00
if conf.conf['login']['password'] == 'changeme':
2015-07-05 22:44:48 +02:00
log.critical("You have not set the login details correctly! Exiting...")
sys.exit(2)
2015-07-06 04:19:49 +02:00
protoname = conf.conf['server']['protocol']
protocols_folder = [os.path.join(os.getcwd(), 'protocols')]
try:
moduleinfo = imp.find_module(protoname, protocols_folder)
proto = imp.load_source(protoname, moduleinfo[1])
except ImportError as e:
if str(e).startswith('No module named'):
log.critical('Failed to load protocol module %r: the file could not be found.', protoname)
else:
log.critical('Failed to load protocol module: import error %s', protoname, str(e))
sys.exit(2)
else:
2015-07-06 04:19:49 +02:00
irc_obj = Irc(proto, conf.conf)