bitbot-3.11-fork/bitbotd

168 lines
5.1 KiB
Text
Raw Permalink Normal View History

2016-03-29 11:56:58 +00:00
#!/usr/bin/env python3
import sys
import src.utils.consts
if sys.version_info < (3, 6):
sys.stderr.write("BitBot requires python 3.6.0 or later\n")
sys.exit(src.utils.consts.Exit.WRONGVERSION)
import atexit, argparse, faulthandler, os, platform, time, typing
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("--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("--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)
2020-01-30 11:50:40 +00:00
config = Config.Config("bot", args.config)
config.load()
DATA_DIR = ""
def _expand(s: str):
return os.path.expanduser(s).format(DATA=DATA_DIR)
DATA_DIR = _expand(config.get("data-directory", "~/.bitbot"))
LOG_DIR = _expand(config.get("log-directory", "{DATA}/logs/"))
DATABASE = _expand(config.get("database", "sqlite3:{DATA}/bot.db"))
LOCK_FILE = _expand(config.get("lock-file", "{DATA}/bot.lock"))
SOCK_FILE = _expand(config.get("sock-file", "{DATA}/bot.sock"))
if not os.path.isdir(LOG_DIR):
os.mkdir(LOG_DIR)
log_level = args.log_level
if not log_level:
log_level = "debug" if args.verbose else "warn"
log_level_files = config.get("log-levels", "TRACE,INFO,WARN"
).split(",")
log = Logging.Log(not args.no_logging, log_level, LOG_DIR, log_level_files)
2019-10-17 12:42:00 +00:00
log.info("Starting BitBot %s (Python v%s, db %s)",
[IRCBot.VERSION, platform.python_version(), DATABASE])
lock_file = LockFile.LockFile(LOCK_FILE)
if not lock_file.available():
log.critical("Database is locked. Is BitBot already running?")
sys.exit(utils.consts.Exit.LOCKED)
atexit.register(lock_file.unlock)
lock_file.lock()
database = Database.Database(log, 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(typing.cast(int, id))
2019-07-12 08:30:22 +00:00
print("Deleted server '%s'" % alias)
else:
sys.stderr.write("Unknown server '%s'\n" % alias)
sys.exit(0)
def _add_server():
details = utils.cli.add_server()
database.servers.add(details.alias, details.hostname, details.port,
details.password, details.tls, details.bindhost, details.nickname,
details.username, details.realname)
if args.add_server:
print("Adding a new server")
_add_server()
sys.exit(0)
cache = Cache.Cache()
events = EventManager.EventRoot(log).wrap()
exports = Exports.Exports()
timers = Timers.Timers(database, events, log)
2019-09-27 15:03:06 +00:00
core_modules = os.path.join(directory, "src", "core_modules")
extra_modules = [os.path.join(directory, "modules")]
if args.external:
extra_modules.append(os.path.abspath(args.external))
if "external-modules" in config:
conf_extra = os.path.abspath(config["external-modules"])
extra_modules.append(_expand(conf_extra))
modules = ModuleManager.ModuleManager(events, exports, timers, config, log,
core_modules, extra_modules)
2020-01-27 23:22:23 +00:00
bot = IRCBot.Bot(directory, DATA_DIR, 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_FILE)
control.bind()
bot.add_poll_source(control)
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)], exc_info=True)
if not args.startup_disconnects:
sys.exit(utils.consts.Exit.DISCONNECT)
try:
bot.run()
except Exception as e:
log.critical("Unhandled exception: %s", [str(e)], exc_info=True)
sys.exit(utils.consts.Exit.FATAL)
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?"):
_add_server()
2016-03-29 11:56:58 +00:00
except KeyboardInterrupt:
print()
pass