2016-04-02 03:21:33 +02:00
|
|
|
#!/usr/bin/env python3
|
2016-06-24 07:51:40 +02:00
|
|
|
"""
|
|
|
|
PyLink IRC Services launcher.
|
|
|
|
"""
|
2015-03-19 20:55:18 +01:00
|
|
|
|
|
|
|
import os
|
2016-06-21 19:27:36 +02:00
|
|
|
import sys
|
|
|
|
try:
|
2016-07-20 02:45:43 +02:00
|
|
|
from pylinkirc import world
|
2016-06-21 19:27:36 +02:00
|
|
|
except ImportError:
|
2016-06-21 20:03:28 +02:00
|
|
|
sys.stderr.write("ERROR: Failed to import PyLink main module (pylinkirc.world).\n\nIf you are "
|
2016-06-21 19:27:36 +02:00
|
|
|
"running PyLink from source, please install PyLink first using 'python3 "
|
2016-07-21 09:02:37 +02:00
|
|
|
"setup.py install' (global install) or 'python3 setup.py install --user'"
|
|
|
|
" (local install)\n")
|
2016-06-21 19:27:36 +02:00
|
|
|
sys.exit(1)
|
2016-08-23 16:28:48 +02:00
|
|
|
from pylinkirc import conf, __version__, real_version
|
2015-03-19 20:55:18 +01:00
|
|
|
|
2015-05-31 21:20:09 +02:00
|
|
|
if __name__ == '__main__':
|
2016-06-21 19:55:42 +02:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Starts an instance of PyLink IRC Services.')
|
|
|
|
parser.add_argument('config', help='specifies the path to the config file (defaults to pylink.yml)', nargs='?', default='pylink.yml')
|
|
|
|
parser.add_argument("-v", "--version", help="displays the program version and exits", action='store_true')
|
2016-12-17 05:55:09 +01:00
|
|
|
parser.add_argument("-c", "--check-pid", help="enables PID file checking", action='store_true')
|
2016-06-21 19:55:42 +02:00
|
|
|
parser.add_argument("-n", "--no-pid", help="skips generating PID files", action='store_true')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2016-06-21 20:03:28 +02:00
|
|
|
if args.version: # Display version and exit
|
2016-08-23 16:28:48 +02:00
|
|
|
print('PyLink %s (in VCS: %s)' % (__version__, real_version))
|
2016-06-21 20:03:28 +02:00
|
|
|
sys.exit()
|
|
|
|
|
2016-12-06 07:43:01 +01:00
|
|
|
# FIXME: we can't pass logging on to conf until we set up the config...
|
2016-06-21 19:55:42 +02:00
|
|
|
conf.loadConf(args.config)
|
|
|
|
|
|
|
|
from pylinkirc.log import log
|
2016-06-24 07:51:40 +02:00
|
|
|
from pylinkirc import classes, utils, coremods
|
2016-07-03 09:26:03 +02:00
|
|
|
log.info('PyLink %s starting...', __version__)
|
2015-07-13 08:28:34 +02:00
|
|
|
|
2016-12-10 03:10:47 +01:00
|
|
|
# Set terminal window title. See https://bbs.archlinux.org/viewtopic.php?id=85567
|
|
|
|
# and https://stackoverflow.com/questions/7387276/
|
|
|
|
if os.name == 'nt':
|
|
|
|
import ctypes
|
|
|
|
ctypes.windll.kernel32.SetConsoleTitleW("PyLink %s" % __version__)
|
|
|
|
elif os.name == 'posix':
|
|
|
|
sys.stdout.write("\x1b]2;PyLink %s\x07" % __version__)
|
|
|
|
|
2016-12-17 05:55:09 +01:00
|
|
|
# Check for a pid file. This stops instance overlap and user mistakes, which are especially an
|
|
|
|
# issue for clientbot...
|
|
|
|
if args.check_pid:
|
2016-10-15 22:09:35 +02:00
|
|
|
config = conf.confname
|
|
|
|
if os.path.exists("%s.pid" % config):
|
2016-10-15 23:34:21 +02:00
|
|
|
log.error("PID file exists; aborting! If PyLink didn't shut down cleanly last time it "
|
|
|
|
"was run, or you're upgrading from PyLink < 1.1-dev, delete %s.pid and try "
|
|
|
|
"again." % config)
|
2016-10-15 23:23:29 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
2016-06-21 20:03:28 +02:00
|
|
|
# Write a PID file unless specifically told not to.
|
|
|
|
if not args.no_pid:
|
|
|
|
with open('%s.pid' % conf.confname, 'w') as f:
|
|
|
|
f.write(str(os.getpid()))
|
2015-07-26 01:57:21 +02:00
|
|
|
|
2015-07-13 08:28:34 +02:00
|
|
|
# Import plugins first globally, because they can listen for events
|
|
|
|
# that happen before the connection phase.
|
|
|
|
to_load = conf.conf['plugins']
|
2017-03-09 07:30:32 +01:00
|
|
|
utils.resetModuleDirs()
|
2015-09-27 21:11:59 +02:00
|
|
|
# Here, we override the module lookup and import the plugins
|
|
|
|
# dynamically depending on which were configured.
|
2015-07-13 08:28:34 +02:00
|
|
|
for plugin in to_load:
|
|
|
|
try:
|
2016-07-03 08:57:20 +02:00
|
|
|
world.plugins[plugin] = pl = utils.loadPlugin(plugin)
|
2017-03-28 00:09:27 +02:00
|
|
|
except Exception as e:
|
2016-01-04 06:05:45 +01:00
|
|
|
log.exception('Failed to load plugin %r: %s: %s', plugin, type(e).__name__, str(e))
|
2015-07-13 08:28:34 +02:00
|
|
|
else:
|
|
|
|
if hasattr(pl, 'main'):
|
|
|
|
log.debug('Calling main() function of plugin %r', pl)
|
|
|
|
pl.main()
|
|
|
|
|
2015-12-25 02:33:49 +01:00
|
|
|
# Initialize all the networks one by one
|
|
|
|
for network, sdata in conf.conf['servers'].items():
|
|
|
|
|
2016-07-29 06:33:13 +02:00
|
|
|
try:
|
|
|
|
protoname = sdata['protocol']
|
|
|
|
except (KeyError, TypeError):
|
|
|
|
log.error("(%s) Configuration error: No protocol module specified, aborting.", network)
|
|
|
|
else:
|
|
|
|
# Fetch the correct protocol module
|
|
|
|
proto = utils.getProtocolModule(protoname)
|
2017-06-25 08:27:24 +02:00
|
|
|
world.networkobjects[network] = proto.Class(network)
|
2015-12-25 02:33:49 +01:00
|
|
|
|
2015-08-29 18:39:33 +02:00
|
|
|
world.started.set()
|
2016-03-27 00:29:17 +01:00
|
|
|
log.info("Loaded plugins: %s", ', '.join(sorted(world.plugins.keys())))
|
2016-08-25 21:07:55 +02:00
|
|
|
|
|
|
|
from pylinkirc import coremods
|
2017-05-13 04:02:25 +02:00
|
|
|
coremods.permissions.resetPermissions() # XXX we should probably move this to run on import
|