bitbot-3.11-fork/bitbotd

176 lines
5.3 KiB
Text
Raw Normal View History

2016-03-29 11:56:58 +00:00
#!/usr/bin/env python3
import sys
if sys.version_info < (3, 6):
sys.stderr.write("BitBot requires python 3.6.0 or later\n")
sys.exit(1)
import atexit, argparse, faulthandler, os, platform, time
from src import Cache, Config, Control, Database, EventManager, Exports, IRCBot
from src import LockFile, Logging, ModuleManager, Timers, utils
2016-03-29 11:56:58 +00:00
faulthandler.enable()
directory = os.path.dirname(os.path.realpath(__file__))
home = os.path.expanduser("~")
default_data = os.path.join(home, ".bitbot")
arg_parser = argparse.ArgumentParser(
description="Python3 event-driven modular IRC bot")
arg_parser.add_argument("--version", "-v", action="store_true")
arg_parser.add_argument("--config", "-c", help="Location of config file",
default=os.path.join(default_data, "bot.conf"))
arg_parser.add_argument("--data-dir", "-x",
help="Location of data files (database, lock, socket)",
default=default_data)
arg_parser.add_argument("--database", "-d",
help="Location of the sqlite3 database file")
arg_parser.add_argument("--log-dir", "-l",
help="Location of the log directory",
default=os.path.join(directory, "logs"))
arg_parser.add_argument("--add-server", "-a",
help="Add a new server", action="store_true")
arg_parser.add_argument("--verbose", "-V", action="store_true")
arg_parser.add_argument("--log-level", "-L")
arg_parser.add_argument("--no-logging", "-N", action="store_true")
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")
arg_parser.add_argument("--external", "-e", help="External modules directory")
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")
args = arg_parser.parse_args()
if args.version:
print("BitBot %s" % IRCBot.VERSION)
sys.exit(0)
database_location = None
lock_location = None
sock_locaiton = None
if not args.database == None:
database_location = args.database
lock_location = "%s.lock" % args.database
sock_location = "%s.sock" % args.database
else:
if not os.path.isdir(args.data_dir):
os.mkdir(args.data_dir)
database_location = os.path.join(args.data_dir, "bot.db")
lock_location = os.path.join(args.data_dir, "bot.lock")
sock_location = os.path.join(args.data_dir, "bot.sock")
log_level = args.log_level
if not log_level:
log_level = "debug" if args.verbose else "warn"
log = Logging.Log(not args.no_logging, log_level, args.log_dir)
2019-10-17 12:42:00 +00:00
log.info("Starting BitBot %s (Python v%s, db %s)",
[IRCBot.VERSION, platform.python_version(), database_location])
lock_file = LockFile.LockFile(lock_location)
if not lock_file.available():
log.critical("Database is locked. Is BitBot already running?")
sys.exit(2)
atexit.register(lock_file.unlock)
lock_file.lock()
database = Database.Database(log, database_location)
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)
if args.add_server:
print("Adding a new server")
utils.cli.add_server(database)
sys.exit(0)
cache = Cache.Cache()
2018-09-28 15:51:36 +00:00
config = Config.Config(args.config)
events = EventManager.EventRoot(log).wrap()
exports = Exports.Exports()
timers = Timers.Timers(database, events, log)
2019-09-27 15:03:06 +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"]))
modules = ModuleManager.ModuleManager(events, exports, timers, config, log,
2019-09-27 15:03:06 +00:00
module_directories)
bot = IRCBot.Bot(directory, args, cache, config, database, events,
exports, log, modules, timers)
bot.add_poll_hook(cache)
bot.add_poll_hook(lock_file)
bot.add_poll_hook(timers)
control = Control.Control(bot, sock_location)
control.bind()
bot.add_poll_source(control)
if args.module:
definition = modules.find_module(args.module)
module = modules.load_module(bot, definition)
module.module.command_line(args.module_args)
sys.exit(0)
server_configs = bot.database.servers.get_all()
if len(server_configs):
bot.load_modules()
servers = []
for server_id, alias in server_configs:
server = bot.add_server(server_id, connect=False)
if not server == None and server.get_setting("connect", True):
server.from_init = True
servers.append(server)
bot._events.on("boot.done").call()
2018-09-28 15:51:36 +00:00
timers.setup(bot.find_settings(prefix="timer-"))
2018-09-28 15:51:36 +00:00
for server in servers:
if not bot.connect(server):
log.error("Failed to connect to '%s'" % str(server))
if not args.startup_disconnects:
sys.exit(3)
try:
bot.run()
except Exception as e:
log.critical("Unhandled exception: %s", [str(e)], exc_info=True)
sys.exit(4)
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?"):
utils.cli.add_server(database)
2016-03-29 11:56:58 +00:00
except KeyboardInterrupt:
print()
pass