hackbot/runhackbot.py
2020-07-26 23:07:05 +02:00

82 lines
2.3 KiB
Python
Executable file

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import sys
import logging
from configparser import ConfigParser
from hackbot import HackBot
# Setup logging.
format_string = '%(levelname)-8s %(message)s'
logging.basicConfig(level=logging.WARNING, format=format_string)
def setup_config(default_config, config_file, config):
'''
param 1: dictionary
param 1: string
param 2: configparser object
returns: configparser object oder false
'''
config.read_dict(default_config)
if not config.read(config_file):
logging.error('Config file {} not found or not readable'.format\
(config_file))
return False
return config
def display_setup(config):
'''
param 1: configparser object
'''
for section in config.sections():
print('Section: {}'.format(section))
for key, value in config.items(section):
print('\t{}: {}'.format(key, value))
print('\n', end='')
def run_hackbot():
'''
Configure and starts logging and hackbot.
'''
log_levels = ('critical', 'error', 'warning', 'info', 'debug')
config_file = './hackbot.conf'
default_config = {
'settings': {
'loglevel': 'warning',
'plugindir': '.'
},
'jabber': {
'jid': '',
'password': '',
'room': '',
'nick': ''
}
}
config = ConfigParser()
config = setup_config(default_config, config_file, config)
if config is False:
sys.exit()
logger = logging.getLogger()
if not config['settings']['loglevel'] in log_levels:
logging.warning('Invalid loglevel given: {} Use default level: {}'.\
format(config['settings']['loglevel'],
default_config['settings']['loglevel']))
config.set('settings', 'loglevel', \
default_config['settings']['loglevel'])
logger.setLevel(config['settings']['loglevel'].upper())
xmpp = HackBot(config['jabber']['jid'],
config['jabber']['password'],
config['jabber']['room'],
config['jabber']['nick'],
config['settings']['plugindir'])
xmpp.run()
if __name__ == '__main__':
run_hackbot()