2003-03-12 07:26:59 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002, Jeremiah Fincher
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
|
|
|
Main program file for running the bot.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from fix import *
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import email
|
|
|
|
|
|
|
|
import conf
|
|
|
|
import world
|
|
|
|
import debug
|
|
|
|
import irclib
|
|
|
|
import ircmsgs
|
|
|
|
import drivers
|
|
|
|
import schedule
|
|
|
|
import privmsgs
|
|
|
|
import asyncoreDrivers
|
|
|
|
|
|
|
|
sys.path.append(conf.pluginDir)
|
|
|
|
|
|
|
|
class ConfigurationDict(dict):
|
|
|
|
def __init__(self, L=None):
|
|
|
|
if L is not None:
|
|
|
|
L = [(key.lower(), value) for (key, value) in L]
|
|
|
|
dict.__init__(self, L)
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
dict.__setitem__(self, key.lower(), value)
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
try:
|
|
|
|
return dict.__getitem__(self, key.lower())
|
|
|
|
except KeyError:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def __contains__(self, key):
|
|
|
|
return dict.__contains__(self, key.lower())
|
|
|
|
|
|
|
|
|
|
|
|
class ConfigAfter376(irclib.IrcCallback):
|
|
|
|
public = False
|
|
|
|
def __init__(self, msgs):
|
|
|
|
self.msgs = msgs
|
|
|
|
|
|
|
|
def do376(self, irc, msg):
|
2003-03-25 20:20:30 +01:00
|
|
|
#debug.printf('Firing ConfigAfter376 messages')
|
2003-03-12 07:26:59 +01:00
|
|
|
for msg in self.msgs:
|
|
|
|
irc.queueMsg(msg)
|
|
|
|
|
|
|
|
do377 = do376
|
|
|
|
|
|
|
|
def reportConfigError(filename, msg):
|
|
|
|
debug.recoverableError('%s: %s' % (filename, msg))
|
|
|
|
|
|
|
|
def processConfigFile(filename):
|
|
|
|
try:
|
|
|
|
fd = file(filename)
|
|
|
|
m = email.message_from_file(fd)
|
|
|
|
d = ConfigurationDict(m.items())
|
|
|
|
if 'nick' not in d:
|
|
|
|
reportConfigError(filename, 'No nick defined.')
|
|
|
|
return
|
|
|
|
if 'server' not in d:
|
|
|
|
reportConfigError(filename, 'No server defined.')
|
|
|
|
return
|
|
|
|
nick = d['nick']
|
|
|
|
server = d['server']
|
|
|
|
if server.find(':') != -1:
|
|
|
|
(server, port) = server.split(':', 1)
|
|
|
|
try:
|
|
|
|
server = (server, int(port))
|
|
|
|
except ValueError:
|
|
|
|
reportConfigError(filename, 'Server has invalid port.')
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
server = (server, 6667)
|
|
|
|
user = d['user'] or nick
|
|
|
|
ident = d['ident'] or nick
|
|
|
|
irc = irclib.Irc(nick, user, ident)
|
2003-03-25 20:20:30 +01:00
|
|
|
for Class in privmsgs.standardPrivmsgModules:
|
|
|
|
irc.addCallback(Class())
|
2003-03-26 00:42:37 +01:00
|
|
|
world.startup = True
|
2003-03-25 20:20:30 +01:00
|
|
|
lines = m.get_payload().splitlines()
|
|
|
|
(startup, after376) = tuple(itersplit(lines, lambda s: not s))
|
2003-03-26 00:42:37 +01:00
|
|
|
#debug.printf('startup: %r' % startup)
|
|
|
|
#debug.printf('after376: %r' % after376)
|
2003-03-25 20:20:30 +01:00
|
|
|
for line in filter(None, startup):
|
2003-03-12 07:26:59 +01:00
|
|
|
if not line.startswith('#'):
|
|
|
|
irc.feedMsg(ircmsgs.privmsg(irc.nick, line))
|
|
|
|
irc.reset()
|
2003-03-26 00:42:37 +01:00
|
|
|
world.startup = False
|
2003-03-25 20:20:30 +01:00
|
|
|
msgs = [ircmsgs.privmsg(irc.nick, line) for line in after376]
|
|
|
|
irc.addCallback(ConfigAfter376(msgs))
|
2003-03-12 07:26:59 +01:00
|
|
|
driver = asyncoreDrivers.AsyncoreDriver(server)
|
|
|
|
driver.irc = irc
|
|
|
|
except IOError, e:
|
|
|
|
reportConfigError(filename, e)
|
|
|
|
except email.Errors.HeaderParseError, e:
|
|
|
|
s = str(e)
|
|
|
|
problem = s[s.rfind('`')+1:-2]
|
|
|
|
msg = 'Invalid configuration format: %s' % problem
|
|
|
|
reportConfigError(filename, msg)
|
|
|
|
|
|
|
|
def main():
|
|
|
|
for filename in sys.argv[1:]:
|
|
|
|
processConfigFile(filename)
|
|
|
|
schedule.addPeriodicEvent(world.upkeep, 300)
|
|
|
|
try:
|
|
|
|
while world.ircs:
|
|
|
|
drivers.run()
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
debug.recoverableException()
|
|
|
|
except: # It must have been deadly on purpose.
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
2003-03-24 09:41:19 +01:00
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|