2016-04-01 18:21:33 -07:00
|
|
|
#!/usr/bin/env python3
|
2016-06-23 22:51:40 -07:00
|
|
|
"""
|
|
|
|
PyLink IRC Services launcher.
|
|
|
|
"""
|
2015-03-19 12:55:18 -07:00
|
|
|
|
|
|
|
import os
|
2016-06-21 10:27:36 -07:00
|
|
|
import sys
|
|
|
|
try:
|
2016-07-19 17:45:43 -07:00
|
|
|
from pylinkirc import world
|
2016-06-21 10:27:36 -07:00
|
|
|
except ImportError:
|
2016-06-21 11:03:28 -07:00
|
|
|
sys.stderr.write("ERROR: Failed to import PyLink main module (pylinkirc.world).\n\nIf you are "
|
2016-06-21 10:27:36 -07:00
|
|
|
"running PyLink from source, please install PyLink first using 'python3 "
|
2016-07-21 00:02:37 -07:00
|
|
|
"setup.py install' (global install) or 'python3 setup.py install --user'"
|
|
|
|
" (local install)\n")
|
2016-06-21 10:27:36 -07:00
|
|
|
sys.exit(1)
|
2016-08-23 07:28:48 -07:00
|
|
|
from pylinkirc import conf, __version__, real_version
|
2015-03-19 12:55:18 -07:00
|
|
|
|
2015-05-31 12:20:09 -07:00
|
|
|
if __name__ == '__main__':
|
2016-06-21 10:55:42 -07: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')
|
|
|
|
parser.add_argument("-n", "--no-pid", help="skips generating PID files", action='store_true')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2016-06-21 11:03:28 -07:00
|
|
|
if args.version: # Display version and exit
|
2016-08-23 07:28:48 -07:00
|
|
|
print('PyLink %s (in VCS: %s)' % (__version__, real_version))
|
2016-06-21 11:03:28 -07:00
|
|
|
sys.exit()
|
|
|
|
|
2016-06-21 10:55:42 -07:00
|
|
|
# Load the config
|
|
|
|
conf.loadConf(args.config)
|
|
|
|
|
|
|
|
from pylinkirc.log import log
|
2016-06-23 22:51:40 -07:00
|
|
|
from pylinkirc import classes, utils, coremods
|
2016-07-03 00:26:03 -07:00
|
|
|
log.info('PyLink %s starting...', __version__)
|
2015-07-12 23:28:34 -07:00
|
|
|
|
2016-06-21 11:03:28 -07: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-25 16:57:21 -07:00
|
|
|
|
2015-07-12 23:28:34 -07:00
|
|
|
# Import plugins first globally, because they can listen for events
|
|
|
|
# that happen before the connection phase.
|
|
|
|
to_load = conf.conf['plugins']
|
2015-09-27 12:11:59 -07:00
|
|
|
# Here, we override the module lookup and import the plugins
|
|
|
|
# dynamically depending on which were configured.
|
2015-07-12 23:28:34 -07:00
|
|
|
for plugin in to_load:
|
|
|
|
try:
|
2016-07-02 23:57:20 -07:00
|
|
|
world.plugins[plugin] = pl = utils.loadPlugin(plugin)
|
2016-01-03 21:05:45 -08:00
|
|
|
except (OSError, ImportError) as e:
|
|
|
|
log.exception('Failed to load plugin %r: %s: %s', plugin, type(e).__name__, str(e))
|
2015-07-12 23:28:34 -07:00
|
|
|
else:
|
|
|
|
if hasattr(pl, 'main'):
|
|
|
|
log.debug('Calling main() function of plugin %r', pl)
|
|
|
|
pl.main()
|
|
|
|
|
2015-12-24 17:33:49 -08:00
|
|
|
# Initialize all the networks one by one
|
|
|
|
for network, sdata in conf.conf['servers'].items():
|
|
|
|
|
2016-07-28 21:33:13 -07: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)
|
|
|
|
world.networkobjects[network] = irc = classes.Irc(network, proto, conf.conf)
|
2015-12-24 17:33:49 -08:00
|
|
|
|
2015-08-29 09:39:33 -07:00
|
|
|
world.started.set()
|
2016-03-26 16:29:17 -07:00
|
|
|
log.info("Loaded plugins: %s", ', '.join(sorted(world.plugins.keys())))
|