2016-03-29 11:56:58 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2018-09-24 14:13:27 +00:00
|
|
|
import argparse, os, sys, time
|
2018-09-29 11:53:39 +00:00
|
|
|
from src import Cache, Config, Database, EventManager, Exports, IRCBot
|
2018-11-13 10:40:49 +00:00
|
|
|
from src import Logging, ModuleManager, Timers, utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
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")
|
|
|
|
|
|
|
|
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"))
|
|
|
|
|
|
|
|
arg_parser.add_argument("--log", "-l",
|
|
|
|
help="Location of the main log file",
|
|
|
|
default=os.path.join(directory, "logs", "bot.log"))
|
|
|
|
|
2018-11-13 10:40:49 +00:00
|
|
|
arg_parser.add_argument("--add-server", "-a",
|
|
|
|
help="Add a new server", action="store_true")
|
|
|
|
|
2017-01-27 21:41:47 +00:00
|
|
|
arg_parser.add_argument("--verbose", "-v", action="store_true")
|
|
|
|
|
2016-03-31 12:20:56 +00:00
|
|
|
args = arg_parser.parse_args()
|
|
|
|
|
2018-10-01 12:48:22 +00:00
|
|
|
log_level = "debug" if args.verbose else "info"
|
|
|
|
log = Logging.Log(log_level, args.log)
|
2018-11-13 10:40:49 +00:00
|
|
|
database = Database.Database(log, args.database)
|
|
|
|
|
|
|
|
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)
|
2018-09-27 10:07:29 +00:00
|
|
|
events = events = EventManager.EventHook(log)
|
|
|
|
exports = exports = Exports.Exports()
|
2018-09-28 16:01:20 +00:00
|
|
|
timers = Timers.Timers(database, events, log)
|
2018-10-12 17:13:48 +00:00
|
|
|
modules = modules = ModuleManager.ModuleManager(events, exports, timers, config,
|
|
|
|
log, os.path.join(directory, "modules"))
|
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,
|
2018-09-30 18:43:20 +00:00
|
|
|
exports, log, modules, timers)
|
2018-09-24 14:13:27 +00:00
|
|
|
|
2018-09-13 12:35:05 +00:00
|
|
|
whitelist = bot.get_setting("module-whitelist", [])
|
|
|
|
blacklist = bot.get_setting("module-blacklist", [])
|
2018-11-05 21:39:30 +00:00
|
|
|
|
|
|
|
server_configs = bot.database.servers.get_all()
|
|
|
|
if len(server_configs):
|
|
|
|
modules.load_modules(bot, whitelist=whitelist, blacklist=blacklist)
|
|
|
|
|
|
|
|
servers = []
|
|
|
|
for server_id, alias in server_configs:
|
|
|
|
server = bot.add_server(server_id, connect=False)
|
|
|
|
if not server == None:
|
|
|
|
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
|
|
|
|
2018-10-12 17:07:23 +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):
|
|
|
|
sys.stderr.write("failed to connect to '%s', exiting\r\n" % (
|
|
|
|
str(server)))
|
|
|
|
sys.exit(1)
|
|
|
|
bot.run()
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
if 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
|