2016-03-29 11:56:58 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2019-05-30 16:15:40 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
if sys.version_info < (3, 6):
|
|
|
|
sys.stderr.write("BitBot requires python 3.6.0 or later\n")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2019-10-10 11:10:45 +00:00
|
|
|
import atexit, argparse, faulthandler, os, platform, time
|
2019-10-11 13:00:26 +00:00
|
|
|
from src import Cache, Config, Control, Database, EventManager, Exports, IRCBot
|
2019-10-10 11:10:45 +00:00
|
|
|
from src import LockFile, Logging, ModuleManager, Timers, utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2019-02-11 11:43:14 +00:00
|
|
|
faulthandler.enable()
|
|
|
|
|
2018-09-27 10:07:29 +00:00
|
|
|
directory = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
|
2016-03-31 12:20:56 +00:00
|
|
|
arg_parser = argparse.ArgumentParser(
|
2018-09-27 10:07:29 +00:00
|
|
|
description="Python3 event-driven modular IRC bot")
|
|
|
|
|
2019-02-24 10:45:54 +00:00
|
|
|
arg_parser.add_argument("--version", "-v", action="store_true")
|
|
|
|
|
2018-09-27 10:07:29 +00:00
|
|
|
arg_parser.add_argument("--config", "-c",
|
|
|
|
help="Location of the JSON config file",
|
|
|
|
default=os.path.join(directory, "bot.conf"))
|
|
|
|
|
|
|
|
arg_parser.add_argument("--database", "-d",
|
|
|
|
help="Location of the sqlite3 database file",
|
|
|
|
default=os.path.join(directory, "databases", "bot.db"))
|
|
|
|
|
2018-11-26 11:09:43 +00:00
|
|
|
arg_parser.add_argument("--log-dir", "-l",
|
|
|
|
help="Location of the log directory",
|
|
|
|
default=os.path.join(directory, "logs"))
|
2018-09-27 10:07:29 +00:00
|
|
|
|
2018-11-13 10:40:49 +00:00
|
|
|
arg_parser.add_argument("--add-server", "-a",
|
|
|
|
help="Add a new server", action="store_true")
|
|
|
|
|
2019-02-24 08:37:42 +00:00
|
|
|
arg_parser.add_argument("--verbose", "-V", action="store_true")
|
2019-02-09 16:40:23 +00:00
|
|
|
arg_parser.add_argument("--log-level", "-L")
|
2019-03-04 19:35:18 +00:00
|
|
|
arg_parser.add_argument("--no-logging", "-N", action="store_true")
|
2017-01-27 21:41:47 +00:00
|
|
|
|
2019-02-24 10:45:54 +00:00
|
|
|
arg_parser.add_argument("--module", "-m",
|
|
|
|
help="Execute an action against a specific module")
|
|
|
|
arg_parser.add_argument("--module-args", "-M",
|
|
|
|
help="Arguments to give in action against a specific module")
|
2019-09-27 15:18:12 +00:00
|
|
|
arg_parser.add_argument("--external", "-e", help="External modules directory")
|
2019-02-24 10:43:46 +00:00
|
|
|
|
2019-07-04 12:32:08 +00:00
|
|
|
arg_parser.add_argument("--startup-disconnects", "-D",
|
|
|
|
help="Tolerate failed connections on startup", action="store_true")
|
|
|
|
|
2019-07-12 08:30:22 +00:00
|
|
|
arg_parser.add_argument("--remove-server", "-R",
|
|
|
|
help="Remove a server by it's alias")
|
|
|
|
|
2016-03-31 12:20:56 +00:00
|
|
|
args = arg_parser.parse_args()
|
|
|
|
|
2019-02-24 08:37:42 +00:00
|
|
|
if args.version:
|
|
|
|
print("BitBot %s" % IRCBot.VERSION)
|
|
|
|
sys.exit(0)
|
|
|
|
|
2019-02-09 16:40:23 +00:00
|
|
|
log_level = args.log_level
|
|
|
|
if not log_level:
|
2019-10-09 16:54:12 +00:00
|
|
|
log_level = "debug" if args.verbose else "warn"
|
2019-02-09 16:40:23 +00:00
|
|
|
|
2019-03-04 19:35:18 +00:00
|
|
|
log = Logging.Log(not args.no_logging, log_level, args.log_dir)
|
2019-05-30 16:05:46 +00:00
|
|
|
|
|
|
|
log.info("Starting BitBot %s (Python v%s)",
|
|
|
|
[IRCBot.VERSION, platform.python_version()])
|
|
|
|
|
2019-10-10 11:10:45 +00:00
|
|
|
lock_file = LockFile.LockFile(args.database)
|
|
|
|
if not lock_file.available():
|
|
|
|
log.critical("Database is locked. Is BitBot already running?")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
atexit.register(lock_file.unlock)
|
|
|
|
lock_file.lock()
|
|
|
|
|
2018-11-13 10:40:49 +00:00
|
|
|
database = Database.Database(log, args.database)
|
|
|
|
|
2019-07-12 08:30:22 +00:00
|
|
|
if args.remove_server:
|
|
|
|
alias = args.remove_server
|
|
|
|
id = database.servers.by_alias(alias)
|
|
|
|
if not id == None:
|
|
|
|
database.servers.delete(id)
|
|
|
|
print("Deleted server '%s'" % alias)
|
|
|
|
else:
|
|
|
|
sys.stderr.write("Unknown server '%s'\n" % alias)
|
|
|
|
sys.exit(0)
|
|
|
|
|
2018-11-13 10:40:49 +00:00
|
|
|
if args.add_server:
|
|
|
|
print("Adding a new server")
|
|
|
|
utils.cli.add_server(database)
|
|
|
|
sys.exit(0)
|
|
|
|
|
2018-09-29 11:53:39 +00:00
|
|
|
cache = Cache.Cache()
|
2018-09-28 15:51:36 +00:00
|
|
|
config = Config.Config(args.config)
|
2019-06-26 10:01:09 +00:00
|
|
|
events = EventManager.EventRoot(log).wrap()
|
|
|
|
exports = Exports.Exports()
|
2018-09-28 16:01:20 +00:00
|
|
|
timers = Timers.Timers(database, events, log)
|
2019-09-27 15:03:06 +00:00
|
|
|
|
2019-09-27 15:18:12 +00:00
|
|
|
module_directories = [os.path.join(directory, "modules")]
|
|
|
|
if args.external:
|
|
|
|
module_directories.append(os.path.abspath(args.external))
|
|
|
|
if "external-modules" in config:
|
|
|
|
module_directories.append(os.path.abspath(config["external-modules"]))
|
|
|
|
|
2019-06-26 10:01:09 +00:00
|
|
|
modules = ModuleManager.ModuleManager(events, exports, timers, config, log,
|
2019-09-27 15:03:06 +00:00
|
|
|
module_directories)
|
2018-09-27 10:07:29 +00:00
|
|
|
|
2018-09-29 11:53:39 +00:00
|
|
|
bot = IRCBot.Bot(directory, args, cache, config, database, events,
|
2019-10-10 12:58:45 +00:00
|
|
|
exports, log, modules, timers)
|
|
|
|
bot.add_poll_hook(cache)
|
|
|
|
bot.add_poll_hook(lock_file)
|
|
|
|
bot.add_poll_hook(timers)
|
2018-09-24 14:13:27 +00:00
|
|
|
|
2019-10-11 14:12:26 +00:00
|
|
|
control = Control.Control(bot, args.database)
|
|
|
|
control.bind()
|
2019-10-11 13:00:26 +00:00
|
|
|
bot.add_poll_source(control)
|
|
|
|
|
2019-02-24 10:43:46 +00:00
|
|
|
if args.module:
|
2019-05-30 16:28:07 +00:00
|
|
|
definition = modules.find_module(args.module)
|
|
|
|
module = modules.load_module(bot, definition)
|
2019-02-24 10:43:46 +00:00
|
|
|
module.module.command_line(args.module_args)
|
|
|
|
sys.exit(0)
|
|
|
|
|
2018-11-05 21:39:30 +00:00
|
|
|
|
|
|
|
server_configs = bot.database.servers.get_all()
|
2019-05-12 13:10:59 +00:00
|
|
|
|
2018-11-05 21:39:30 +00:00
|
|
|
if len(server_configs):
|
2019-05-12 13:10:59 +00:00
|
|
|
bot.load_modules()
|
2018-11-05 21:39:30 +00:00
|
|
|
|
|
|
|
servers = []
|
|
|
|
for server_id, alias in server_configs:
|
|
|
|
server = bot.add_server(server_id, connect=False)
|
2018-11-24 12:14:36 +00:00
|
|
|
if not server == None and server.get_setting("connect", True):
|
2019-06-17 13:20:11 +00:00
|
|
|
server.from_init = True
|
2018-11-05 21:39:30 +00:00
|
|
|
servers.append(server)
|
|
|
|
|
2018-09-19 12:25:12 +00:00
|
|
|
bot._events.on("boot.done").call()
|
2018-09-28 15:51:36 +00:00
|
|
|
|
2019-10-07 11:52:57 +00:00
|
|
|
timers.setup(bot.find_settings(prefix="timer-"))
|
2018-09-28 15:51:36 +00:00
|
|
|
|
2018-07-02 11:08:26 +00:00
|
|
|
for server in servers:
|
|
|
|
if not bot.connect(server):
|
2019-07-04 12:32:08 +00:00
|
|
|
log.error("Failed to connect to '%s'" % str(server))
|
|
|
|
if not args.startup_disconnects:
|
|
|
|
sys.exit(1)
|
2018-11-26 14:06:41 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
bot.run()
|
|
|
|
except Exception as e:
|
|
|
|
log.critical("Unhandled exception: %s", [str(e)], exc_info=True)
|
|
|
|
sys.exit(1)
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
|
|
|
try:
|
2018-11-14 10:27:15 +00:00
|
|
|
if utils.cli.bool_input("no servers found, add one?"):
|
2018-11-13 10:40:49 +00:00
|
|
|
utils.cli.add_server(database)
|
2016-03-29 11:56:58 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
print()
|
|
|
|
pass
|