aboutsummaryrefslogtreecommitdiff
path: root/start.py
blob: ebe638adadb987f85c1324e2877370e39dd45d28 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3

import argparse, os, sys, time
from src import Cache, Config, Database, EventManager, Exports, IRCBot
from src import Logging, ModuleManager, Timers, utils

directory = os.path.dirname(os.path.realpath(__file__))

arg_parser = argparse.ArgumentParser(
    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"))

arg_parser.add_argument("--add-server", "-a",
    help="Add a new server", action="store_true")

arg_parser.add_argument("--verbose", "-v", action="store_true")

args = arg_parser.parse_args()

log_level = "debug" if args.verbose else "info"
log = Logging.Log(log_level, args.log)
database = Database.Database(log, args.database)

if args.add_server:
    print("Adding a new server")
    utils.cli.add_server(database)
    sys.exit(0)

cache = Cache.Cache()
config = Config.Config(args.config)
events = events = EventManager.EventHook(log)
exports = exports = Exports.Exports()
timers = Timers.Timers(database, events, log)
modules = modules = ModuleManager.ModuleManager(events, exports, timers, config,
    log, os.path.join(directory, "modules"))

bot = IRCBot.Bot(directory, args, cache, config, database, events,
    exports, log, modules, timers)

whitelist = bot.get_setting("module-whitelist", [])
blacklist = bot.get_setting("module-blacklist", [])

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)

    bot._events.on("boot.done").call()

    timers.setup(bot.find_settings_prefix("timer-"))

    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()
else:
    try:
        if bool_input("no servers found, add one?"):
            utils.cli.add_server(database)
    except KeyboardInterrupt:
        print()
        pass