h/core/config.py

62 lines
1.5 KiB
Python
Raw Normal View History

import inspect
import json
import os
2010-03-01 02:32:41 +00:00
def save(conf):
json.dump(conf, open('config', 'w'), sort_keys=True, indent=2)
if not os.path.exists('config'):
open('config', 'w').write(inspect.cleandoc(
r'''
{
"connections":
{
"local irc":
{
"server": "localhost",
"nick": "skybot",
"channels": ["#test"]
}
},
2014-05-09 03:09:58 +00:00
"prefix": ".",
"disabled_plugins": [],
"disabled_commands": [],
"acls": {},
2011-04-25 18:17:55 +00:00
"api_keys": {},
"censored_strings":
[
"DCC SEND",
"1nj3ct",
"thewrestlinggame",
"startkeylogger",
"hybux",
"\\0",
"\\x01",
"!coz",
"!tell /x"
]
}''') + '\n')
2010-03-01 02:32:41 +00:00
def config():
# reload config from file if file has changed
config_mtime = os.stat('config').st_mtime
if bot._config_mtime != config_mtime:
try:
bot.config = json.load(open('config'))
bot._config_mtime = config_mtime
2015-11-15 16:36:35 +00:00
for name, conf in bot.config['connections'].items():
if name in bot.conns:
bot.conns[name].set_conf(conf)
else:
if conf.get('ssl'):
bot.conns[name] = SSLIRC(conf)
else:
bot.conns[name] = IRC(conf)
2015-11-15 16:36:35 +00:00
except ValueError as e:
print('ERROR: malformed config!', e)
bot._config_mtime = 0