Made configuration commands persistent, yay!

This commit is contained in:
Jeremy Fincher 2003-08-22 23:15:29 +00:00
parent a11873a260
commit 8e068ce8c0
5 changed files with 75 additions and 59 deletions

View File

@ -38,42 +38,20 @@ from fix import *
import sys import sys
import time import time
import getopt import getopt
#import pprint
import conf import conf
import debug
import world import world
import irclib
import drivers
import ircmsgs
import privmsgs
import schedule
sys.path.append(conf.pluginDir) sys.path.append(conf.pluginDir)
world.startedAt = time.time() world.startedAt = time.time()
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())
def handleConfigFile(filename):
import debug
import irclib
import ircmsgs
import drivers
import ircutils
import privmsgs
class ConfigAfter376(irclib.IrcCallback): class ConfigAfter376(irclib.IrcCallback):
public = False public = False
def __init__(self, commands): def __init__(self, commands):
@ -88,24 +66,29 @@ def handleConfigFile(filename):
irc.queueMsg(msg) irc.queueMsg(msg)
do377 = do376 do377 = do376
def handleConfigFile():
nick = conf.config['nick'] nick = conf.config['nick']
user = conf.config['user'] user = conf.config['user']
ident = conf.config['ident'] ident = conf.config['ident']
prefix = ircutils.joinHostmask(nick, ident, 'host')
password = conf.config['password'] password = conf.config['password']
irc = irclib.Irc(nick, user, ident, password) irc = irclib.Irc(nick, user, ident, password)
for Class in privmsgs.standardPrivmsgModules: for Class in privmsgs.standardPrivmsgModules:
irc.addCallback(Class()) callback = Class()
world.startup = True if hasattr(callback, 'configure'):
for line in conf.config['onStart']: fakeIrc = callback.configure()
irc.feedMsg(ircmsgs.privmsg(irc.nick, line, prefix=prefix)) # This is mostly a hack to make sure the load command works.
irc.reset() for cb in fakeIrc.callbacks: # Should most always be empty.
world.startup = False irc.addCallback(cb)
irc.addCallback(callback)
irc.addCallback(ConfigAfter376(conf.config['afterConnect'])) irc.addCallback(ConfigAfter376(conf.config['afterConnect']))
driver = drivers.newDriver(conf.config['server'], irc) drivers.newDriver(conf.config['server'], irc)
def main(): def main():
(optlist, filenames) = getopt.getopt(sys.argv[1:], 'Opc:') (optlist, filenames) = getopt.getopt(sys.argv[1:], 'Opc:')
if len(filenames) != 1:
conf.reportConfigError('Command line', 'No configuration file given.')
for (option, argument) in optlist: for (option, argument) in optlist:
if option == '-c': if option == '-c':
myLocals = {} myLocals = {}
@ -117,12 +100,9 @@ def main():
setattr(conf, key, value) setattr(conf, key, value)
else: else:
print 'Unexpected argument %s; ignoring.' % option print 'Unexpected argument %s; ignoring.' % option
import debug filename = filenames[0]
import drivers
import schedule
for filename in filenames:
conf.processConfig(filename) conf.processConfig(filename)
handleConfigFile(filename) handleConfigFile()
schedule.addPeriodicEvent(world.upkeep, 300) schedule.addPeriodicEvent(world.upkeep, 300)
try: try:
while world.ircs: while world.ircs:

View File

@ -51,6 +51,7 @@ import sre_constants
from cStringIO import StringIO from cStringIO import StringIO
import conf import conf
import utils
import world import world
import ircdb import ircdb
import irclib import irclib
@ -366,6 +367,29 @@ class Privmsg(irclib.IrcCallback):
self.rateLimiter = RateLimiter() self.rateLimiter = RateLimiter()
self.Proxy = IrcObjectProxy self.Proxy = IrcObjectProxy
def configure(self):
nick = conf.config['nick']
user = conf.config['user']
ident = conf.config['ident']
fakeIrc = irclib.Irc(nick, user, ident)
fakeIrc.error = lambda _, s: None #debug.printf(s)
fakeIrc.reply = lambda _, s: None #debug.printf(s)
fakeIrc.findCallback = lambda *args: None
for args in conf.config['onStart']:
args = args[:]
command = args.pop(0)
if self.isCommand(command):
#debug.printf('%s: %r' % (command, args))
method = getattr(self, command)
line = ' '.join(map(utils.dqrepr, args))
msg = ircmsgs.privmsg(fakeIrc.nick, line, fakeIrc.prefix)
try:
world.startup = True
method(fakeIrc, msg, args)
finally:
world.startup = False
return fakeIrc
def __call__(self, irc, msg): def __call__(self, irc, msg):
irclib.IrcCallback.__call__(self, irc, msg) irclib.IrcCallback.__call__(self, irc, msg)
# Now, if there's anything in the rateLimiter... # Now, if there's anything in the rateLimiter...
@ -395,7 +419,7 @@ class Privmsg(irclib.IrcCallback):
if self.threaded: if self.threaded:
thread = CommandThread(f, irc, msg, args) thread = CommandThread(f, irc, msg, args)
thread.start() thread.start()
debug.printf('Spawned new thread: %s' % thread) #debug.printf('Spawned new thread: %s' % thread)
else: else:
# Exceptions aren't caught here because IrcObjectProxy.finalEval # Exceptions aren't caught here because IrcObjectProxy.finalEval
# catches them and does The Right Thing. # catches them and does The Right Thing.
@ -412,7 +436,7 @@ class Privmsg(irclib.IrcCallback):
if s: if s:
recipient = msg.args[0] recipient = msg.args[0]
if ircdb.checkIgnored(msg.prefix, recipient): if ircdb.checkIgnored(msg.prefix, recipient):
debug.printf('Privmsg.doPrivmsg: ignoringi %s.' % recipient) debug.printf('Privmsg.doPrivmsg: ignoring %s.' % recipient)
return return
m = self._r.match(s) m = self._r.match(s)
if m and self.isCommand(canonicalName(m.group(1))): if m and self.isCommand(canonicalName(m.group(1))):
@ -462,6 +486,7 @@ class PrivmsgRegexp(Privmsg):
""" """
threaded = False # Again, like Privmsg... threaded = False # Again, like Privmsg...
flags = re.I flags = re.I
onlyFirstMatch = False
def __init__(self): def __init__(self):
Privmsg.__init__(self) Privmsg.__init__(self)
self.Proxy = IrcObjectProxyRegexp self.Proxy = IrcObjectProxyRegexp
@ -479,6 +504,7 @@ class PrivmsgRegexp(Privmsg):
(self.__class__.__name__, name, (self.__class__.__name__, name,
value.__doc__, debug.exnToString(e)) value.__doc__, debug.exnToString(e))
debug.msg(s) debug.msg(s)
self.res.sort(lambda (r1, m1), (r2, m2): cmp(m1.__name__, m2.__name__))
def doPrivmsg(self, irc, msg): def doPrivmsg(self, irc, msg):
if ircdb.checkIgnored(msg.prefix, msg.args[0]): if ircdb.checkIgnored(msg.prefix, msg.args[0]):
@ -492,6 +518,8 @@ class PrivmsgRegexp(Privmsg):
if msg: if msg:
irc = IrcObjectProxyRegexp(irc) irc = IrcObjectProxyRegexp(irc)
self.callCommand(method, irc, msg, m) self.callCommand(method, irc, msg, m)
if self.onlyFirstMatch:
return
class PrivmsgCommandAndRegexp(Privmsg): class PrivmsgCommandAndRegexp(Privmsg):

View File

@ -187,6 +187,8 @@ driverModule = 'asyncoreDrivers'
############################### ###############################
############################### ###############################
############################### ###############################
import debug
class ConfigurationDict(dict): class ConfigurationDict(dict):
def __init__(self, L=()): def __init__(self, L=()):
L = [(key.lower(), value) for (key, value) in L] L = [(key.lower(), value) for (key, value) in L]
@ -206,11 +208,12 @@ class ConfigurationDict(dict):
config = ConfigurationDict() config = ConfigurationDict()
def processConfig(filename):
import debug
import email
def reportConfigError(filename, msg): def reportConfigError(filename, msg):
debug.unrecoverableError('%s: %s' % (filename, msg)) debug.unrecoverableError('%s: %s' % (filename, msg))
def processConfig(filename):
import email
from callbacks import tokenize
try: try:
fd = file(filename) fd = file(filename)
m = email.message_from_file(fd) m = email.message_from_file(fd)
@ -247,7 +250,7 @@ def processConfig(filename):
# from the commands to be run after connecting. This separates them # from the commands to be run after connecting. This separates them
# based on those newlines. # based on those newlines.
(onStart, afterConnect) = tuple(itersplit(lines,lambda s: not s,True)) (onStart, afterConnect) = tuple(itersplit(lines,lambda s: not s,True))
config['onStart'] = [s for s in onStart if s and s[0] != '#'] config['onStart'] = [tokenize(s) for s in onStart if s and s[0] != '#']
config['afterConnect'] = [s for s in afterConnect if s and s[0] != '#'] config['afterConnect'] = [s for s in afterConnect if s and s[0] != '#']
except IOError, e: except IOError, e:

View File

@ -519,4 +519,5 @@ class Irc(object):
def __eq__(self, other): def __eq__(self, other):
return id(self) == id(other) return id(self) == id(other)
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

View File

@ -266,6 +266,8 @@ class OwnerCommands(CapabilityCheckingPrivmsg):
module = imp.load_module(name, *moduleInfo) module = imp.load_module(name, *moduleInfo)
linecache.checkcache() linecache.checkcache()
callback = module.Class() callback = module.Class()
if hasattr(callback, 'configure'):
callback.configure()
irc.addCallback(callback) irc.addCallback(callback)
irc.reply(msg, conf.replySuccess) irc.reply(msg, conf.replySuccess)
@ -299,6 +301,8 @@ class OwnerCommands(CapabilityCheckingPrivmsg):
module = imp.load_module(name, *moduleInfo) module = imp.load_module(name, *moduleInfo)
linecache.checkcache() linecache.checkcache()
callback = module.Class() callback = module.Class()
if hasattr(callback, 'configure'):
callback.configure()
irc.addCallback(callback) irc.addCallback(callback)
irc.reply(msg, conf.replySuccess) irc.reply(msg, conf.replySuccess)
except ImportError: except ImportError: