2015-03-19 20:55:18 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import imp
|
|
|
|
import os
|
2015-04-03 09:17:03 +02:00
|
|
|
import socket
|
2015-04-03 21:35:55 +02:00
|
|
|
import time
|
2015-04-25 07:37:07 +02:00
|
|
|
import sys
|
2015-06-24 04:29:53 +02:00
|
|
|
from collections import defaultdict
|
2015-04-25 07:37:07 +02:00
|
|
|
|
2015-07-05 22:22:17 +02:00
|
|
|
from log import log
|
2015-05-31 21:20:09 +02:00
|
|
|
from conf import conf
|
2015-06-24 04:29:53 +02:00
|
|
|
import classes
|
2015-03-19 20:55:18 +01:00
|
|
|
|
2015-04-18 04:55:48 +02:00
|
|
|
class Irc():
|
2015-06-17 05:05:41 +02:00
|
|
|
def __init__(self, proto):
|
2015-04-04 03:45:18 +02:00
|
|
|
# Initialize some variables
|
2015-04-18 04:55:48 +02:00
|
|
|
self.connected = False
|
|
|
|
self.name = conf['server']['netname']
|
2015-04-25 07:37:07 +02:00
|
|
|
self.conf = conf
|
2015-07-05 08:12:00 +02:00
|
|
|
# Server, channel, and user indexes to be populated by our protocol module
|
2015-05-31 07:15:19 +02:00
|
|
|
self.servers = {}
|
2015-07-05 08:12:00 +02:00
|
|
|
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',
|
|
|
|
'voice': 'v', 'key': 'k'}
|
|
|
|
self.umodes = {'invisible': 'i', 'snomask': 's', 'wallops': 'w',
|
|
|
|
'oper': 'o'}
|
|
|
|
self.maxnicklen = 30
|
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-07-05 22:22:17 +02:00
|
|
|
log.info("Connecting to network %r on %s:%s" % (self.name, ip, port))
|
2015-04-03 09:17:03 +02:00
|
|
|
|
|
|
|
self.socket = socket.socket()
|
|
|
|
self.socket.connect((ip, port))
|
2015-06-17 05:05:41 +02:00
|
|
|
self.proto = proto
|
2015-04-25 07:37:07 +02:00
|
|
|
proto.connect(self)
|
|
|
|
self.loaded = []
|
|
|
|
self.load_plugins()
|
2015-06-03 01:55:04 +02:00
|
|
|
self.connected = True
|
2015-04-18 04:55:48 +02:00
|
|
|
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-05-31 07:15:19 +02:00
|
|
|
data = self.socket.recv(2048).decode("utf-8")
|
2015-04-25 07:37:07 +02:00
|
|
|
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-07-05 22:22:17 +02:00
|
|
|
log.debug("<- {}".format(line))
|
2015-04-25 07:37:07 +02:00
|
|
|
proto.handle_events(self, line)
|
|
|
|
except socket.error as e:
|
2015-07-05 22:22:17 +02:00
|
|
|
log.error('Received socket.error: %s, exiting.' % str(e))
|
2015-04-25 07:37:07 +02:00
|
|
|
break
|
|
|
|
sys.exit(1)
|
2015-04-03 09:17:03 +02:00
|
|
|
|
|
|
|
def send(self, data):
|
|
|
|
data = data.encode("utf-8") + b"\n"
|
2015-07-05 22:22:17 +02:00
|
|
|
log.debug("-> {}".format(data.decode("utf-8").strip("\n")))
|
2015-04-03 09:17:03 +02:00
|
|
|
self.socket.send(data)
|
2015-03-19 20:55:18 +01:00
|
|
|
|
2015-04-25 07:37:07 +02: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:
|
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'):
|
2015-07-05 22:22:17 +02:00
|
|
|
log.error('Failed to load plugin %r: the plugin could not be found.' % plugin)
|
2015-06-08 02:04:23 +02:00
|
|
|
else:
|
2015-07-05 22:22:17 +02:00
|
|
|
log.error('Failed to load plugin %r: import error %s' % (plugin, str(e)))
|
2015-04-25 07:37:07 +02:00
|
|
|
print("loaded plugins: %s" % self.loaded)
|
|
|
|
|
2015-05-31 21:20:09 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
print('PyLink starting...')
|
2015-06-17 05:05:41 +02:00
|
|
|
if conf['login']['password'] == 'changeme':
|
|
|
|
print("You have not set the login details correctly! Exiting...")
|
|
|
|
sys.exit(2)
|
2015-04-25 07:37:07 +02:00
|
|
|
|
2015-06-17 05:05:41 +02:00
|
|
|
protoname = 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'):
|
2015-07-05 22:22:17 +02:00
|
|
|
log.critical('Failed to load protocol module %r: the file could not be found.' % protoname)
|
2015-06-17 05:05:41 +02:00
|
|
|
else:
|
2015-07-05 22:22:17 +02:00
|
|
|
log.critical('Failed to load protocol module: import error %s' % (protoname, str(e)))
|
|
|
|
sys.exit(2)
|
2015-06-17 05:05:41 +02:00
|
|
|
else:
|
|
|
|
irc_obj = Irc(proto)
|