2010-07-30 13:34:51 -05:00
|
|
|
"""
|
|
|
|
dr.botzo - a pluggable IRC bot written in Python
|
|
|
|
Copyright (C) 2010 Brian S. Stephan
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
2010-07-26 21:51:03 -05:00
|
|
|
|
2010-07-24 10:39:58 -05:00
|
|
|
from ConfigParser import ConfigParser, NoSectionError, NoOptionError
|
2010-07-24 09:54:38 -05:00
|
|
|
import os
|
2010-07-25 08:52:48 -05:00
|
|
|
import re
|
2010-07-24 10:12:00 -05:00
|
|
|
import sys
|
2010-07-27 20:26:21 -05:00
|
|
|
import inspect
|
2010-10-24 11:46:49 -05:00
|
|
|
import sqlite3
|
2010-07-24 09:54:38 -05:00
|
|
|
|
2011-01-07 09:54:51 -06:00
|
|
|
import DrBotIRC
|
2010-07-30 18:34:10 -05:00
|
|
|
from extlib import irclib
|
|
|
|
|
2010-07-27 20:26:21 -05:00
|
|
|
modlist = []
|
2010-07-28 00:11:58 -05:00
|
|
|
moduleList = [ "Countdown", "Dice", "IrcAdmin", "GoogleTranslate", "Seen", "FactFile" ]
|
2010-07-27 20:26:21 -05:00
|
|
|
modObjs = []
|
2010-07-26 18:14:33 -05:00
|
|
|
|
2011-01-06 22:43:10 -06:00
|
|
|
config_file = 'dr.botzo.cfg'
|
2010-10-02 21:44:03 -05:00
|
|
|
# check argv
|
2011-01-06 22:43:10 -06:00
|
|
|
if len(sys.argv) == 2:
|
|
|
|
config_file = sys.argv[1]
|
2010-10-02 21:44:03 -05:00
|
|
|
|
2010-07-24 10:47:33 -05:00
|
|
|
# read config file
|
|
|
|
|
2010-07-24 11:34:19 -05:00
|
|
|
config = ConfigParser({'debug': 'false'})
|
2011-01-06 22:43:10 -06:00
|
|
|
config.read(os.path.expanduser(config_file))
|
2010-07-24 09:54:38 -05:00
|
|
|
|
2010-07-24 11:34:19 -05:00
|
|
|
# load necessary options
|
2010-07-24 10:12:00 -05:00
|
|
|
try:
|
|
|
|
# load connection info
|
2010-08-01 11:41:26 -05:00
|
|
|
botserver = config.get('dr.botzo', 'server')
|
|
|
|
botport = config.getint('dr.botzo', 'port')
|
|
|
|
botnick = config.get('dr.botzo', 'nick')
|
2010-09-08 20:32:43 -05:00
|
|
|
botpass = config.get('dr.botzo', 'pass')
|
|
|
|
botuser = config.get('dr.botzo', 'user')
|
2010-08-01 11:41:26 -05:00
|
|
|
botircname = config.get('dr.botzo', 'name')
|
2010-07-24 10:12:00 -05:00
|
|
|
except NoSectionError as e:
|
2010-07-24 10:14:47 -05:00
|
|
|
sys.exit("Aborted due to error with necessary configuration: " + str(e))
|
|
|
|
except NoOptionError as e:
|
|
|
|
sys.exit("Aborted due to error with necessary configuration: " + str(e))
|
2010-07-24 09:54:38 -05:00
|
|
|
|
2010-07-24 11:34:19 -05:00
|
|
|
# load additional options
|
2010-08-01 11:41:26 -05:00
|
|
|
irclib.DEBUG = config.getboolean('dr.botzo', 'debug')
|
2010-07-24 11:34:19 -05:00
|
|
|
|
2010-10-24 11:46:49 -05:00
|
|
|
try:
|
|
|
|
# make sure we can initialize the database, if such a thing is
|
|
|
|
# called for in the config file
|
|
|
|
dbfile = config.get('dr.botzo', 'database')
|
|
|
|
conn = sqlite3.connect(dbfile)
|
|
|
|
try:
|
|
|
|
query = """
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM sqlite_master
|
|
|
|
WHERE type = 'table' AND name = 'drbotzo_modules'
|
|
|
|
"""
|
|
|
|
row = conn.execute(query).fetchone()
|
|
|
|
if row[0] == 0:
|
|
|
|
# need to create the drbotzo_modules table
|
|
|
|
query = """
|
|
|
|
CREATE TABLE drbotzo_modules (
|
|
|
|
module TEXT,
|
|
|
|
version INTEGER
|
|
|
|
)
|
|
|
|
"""
|
|
|
|
conn.execute(query)
|
|
|
|
conn.commit()
|
|
|
|
finally: conn.close()
|
|
|
|
except NoOptionError: pass # if the config file has no db property, assume that
|
|
|
|
except NoSectionError: pass # the database doesn't need to exist
|
|
|
|
|
2010-07-24 10:47:33 -05:00
|
|
|
# start up the IRC bot
|
|
|
|
|
|
|
|
# create IRC and server objects and connect
|
2011-01-07 09:54:51 -06:00
|
|
|
irc = DrBotIRC.DrBotIRC()
|
2010-09-08 20:32:43 -05:00
|
|
|
server = irc.server().connect(botserver, botport, botnick, botpass, botuser, botircname)
|
2010-07-24 10:47:33 -05:00
|
|
|
|
2010-07-29 23:13:15 -05:00
|
|
|
# load features
|
|
|
|
try:
|
2010-08-01 11:41:26 -05:00
|
|
|
cfgmodlist = config.get('dr.botzo', 'module_list')
|
2010-07-29 23:13:15 -05:00
|
|
|
|
|
|
|
mods = cfgmodlist.split(',')
|
|
|
|
for mod in mods:
|
|
|
|
# try to load each module
|
2010-12-17 22:46:48 -06:00
|
|
|
mod = mod.strip()
|
2010-12-10 23:25:56 -06:00
|
|
|
modstr = 'modules.'+mod
|
2010-12-17 22:46:48 -06:00
|
|
|
print "DEBUG: attempting to load module %s" % (modstr)
|
2010-12-10 23:25:56 -06:00
|
|
|
__import__(modstr)
|
|
|
|
module = sys.modules[modstr]
|
|
|
|
botmod = eval('module.' + mod + '(config, server, modlist)')
|
2010-12-15 20:53:52 -06:00
|
|
|
botmod.register_handlers(server)
|
2010-07-29 23:13:15 -05:00
|
|
|
except NoSectionError as e:
|
|
|
|
print("You seem to be missing a modules config section, which you probably wanted.")
|
|
|
|
except NoOptionError as e:
|
|
|
|
print("You seem to be missing a modlist config option, which you probably wanted.")
|
2010-07-25 20:09:02 -05:00
|
|
|
|
2010-07-24 10:47:33 -05:00
|
|
|
# loop forever
|
|
|
|
irc.process_forever()
|
|
|
|
|
2010-07-24 09:54:38 -05:00
|
|
|
# vi:tabstop=4:expandtab:autoindent
|
2010-07-28 00:11:58 -05:00
|
|
|
# kate: indent-mode python;indent-width 4;replace-tabs on;
|