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-31 13:06:49 +00:00
|
|
|
import atexit, argparse, faulthandler, os, platform, time, typing
|
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__))
|
2019-10-17 11:39:56 +00:00
|
|
|
home = os.path.expanduser("~")
|
|
|
|
default_data = os.path.join(home, ".bitbot")
|
2018-09-27 10:07:29 +00:00
|
|
|
|
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")
|
|
|
|
|
2019-10-17 11:39:56 +00:00
|
|
|
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)
|
2018-09-27 10:07:29 +00:00
|
|
|
|
|
|
|
arg_parser.add_argument("--database", "-d",
|
2019-10-17 11:39:56 +00:00
|
|
|
help="Location of the sqlite3 database file")
|
2018-09-27 10:07:29 +00:00
|
|
|
|
2018-11-26 11:09:43 +00:00
|
|
|
arg_parser.add_argument("--log-dir", "-l",
|
2019-11-14 13:31:12 +00:00
|
|
|
help="Location of the log directory")
|
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-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-11-14 13:31:12 +00:00
|
|
|
if not os.path.isdir(args.data_dir):
|
|
|
|
os.mkdir(args.data_dir)
|
|
|
|
|
2019-10-17 11:39:56 +00:00
|
|
|
database_location = None
|
|
|
|
lock_location = None
|
|
|
|
sock_locaiton = None
|
2019-11-14 13:31:12 +00:00
|
|
|
log_directory = None
|
2019-10-17 11:39:56 +00:00
|
|
|
if not args.database == None:
|
|
|
|
database_location = args.database
|
|
|
|
lock_location = "%s.lock" % args.database
|
|
|
|
sock_location = "%s.sock" % args.database
|
|
|
|
else:
|
|
|
|
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")
|
|
|
|
|
2019-11-14 13:31:12 +00:00
|
|
|
log_directory = args.log_dir or os.path.join(args.data_dir, "logs")
|
|
|
|
if not os.path.isdir(log_directory):
|
|
|
|
os.mkdir(log_directory)
|
|
|
|
|
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-11-14 13:31:12 +00:00
|
|
|
log = Logging.Log(not args.no_logging, log_level, log_directory)
|
2019-05-30 16:05:46 +00:00
|
|
|
|
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])
|
2019-05-30 16:05:46 +00:00
|
|
|
|
2019-10-17 11:39:56 +00:00
|
|
|
lock_file = LockFile.LockFile(lock_location)
|
2019-10-10 11:10:45 +00:00
|
|
|
if not lock_file.available():
|
|
|
|
log.critical("Database is locked. Is BitBot already running?")
|
2019-10-25 13:13:48 +00:00
|
|
|
sys.exit(2)
|
2019-10-10 11:10:45 +00:00
|
|
|
|
|
|
|
atexit.register(lock_file.unlock)
|
|
|
|
lock_file.lock()
|
|
|
|
|
2019-10-17 11:39:56 +00:00
|
|
|
database = Database.Database(log, database_location)
|
2018-11-13 10:40:49 +00:00
|
|
|
|
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:
|
2019-10-31 13:06:49 +00:00
|
|
|
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)
|
|
|
|
|
2019-10-29 18:00:38 +00:00
|
|
|
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)
|
2018-11-13 10:40:49 +00:00
|
|
|
if args.add_server:
|
|
|
|
print("Adding a new server")
|
2019-10-29 18:00:38 +00:00
|
|
|
_add_server()
|
2018-11-13 10:40:49 +00:00
|
|
|
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-17 11:39:56 +00:00
|
|
|
control = Control.Control(bot, sock_location)
|
2019-10-11 14:12:26 +00:00
|
|
|
control.bind()
|
2019-10-11 13:00:26 +00:00
|
|
|
bot.add_poll_source(control)
|
|
|
|
|
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-10-25 13:18:22 +00:00
|
|
|
log.error("Failed to connect to '%s'", [str(server)], exc_info=True)
|
2019-07-04 12:32:08 +00:00
|
|
|
if not args.startup_disconnects:
|
2019-10-25 13:13:48 +00:00
|
|
|
sys.exit(3)
|
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)
|
2019-10-25 13:13:48 +00:00
|
|
|
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?"):
|
2019-10-29 18:00:38 +00:00
|
|
|
_add_server()
|
2016-03-29 11:56:58 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
print()
|
|
|
|
pass
|