Made Aliases persistent.

This commit is contained in:
Jeremy Fincher 2003-11-11 12:18:25 +00:00
parent 225ee0ed6a
commit 0ca15118e7
2 changed files with 25 additions and 10 deletions

View File

@ -35,6 +35,7 @@ Allows 'aliases' for other commands.
import plugins import plugins
import os
import re import re
import sets import sets
import types import types
@ -44,6 +45,7 @@ import debug
import utils import utils
import privmsgs import privmsgs
import callbacks import callbacks
import structures
def configure(onStart, afterConnect, advanced): def configure(onStart, afterConnect, advanced):
# This will be called by setup.py to configure this module. onStart and # This will be called by setup.py to configure this module. onStart and
@ -153,7 +155,19 @@ def makeNewAlias(name, alias):
class Alias(callbacks.Privmsg): class Alias(callbacks.Privmsg):
def __init__(self): def __init__(self):
callbacks.Privmsg.__init__(self) callbacks.Privmsg.__init__(self)
self.frozen = sets.Set() filename = os.path.join(conf.dataDir, 'Aliases.db')
# Schema: {name: [alias, frozen]}
self.aliases = structures.PersistentDictionary(filename)
def __call__(self, irc, msg):
# Adding the aliases requires an Irc. So the first time we get called
# with an Irc, we add our aliases and then delete ourselves :)
for (name, (alias, frozen)) in self.aliases.iteritems():
self.addAlias(irc, name, alias, frozen)
del self.__class__.__call__
def die(self):
self.aliases.close()
def freeze(self, irc, msg, args): def freeze(self, irc, msg, args):
"""<alias> """<alias>
@ -163,7 +177,7 @@ class Alias(callbacks.Privmsg):
name = privmsgs.getArgs(args) name = privmsgs.getArgs(args)
name = callbacks.canonicalName(name) name = callbacks.canonicalName(name)
if hasattr(self, name) and self.isCommand(name): if hasattr(self, name) and self.isCommand(name):
self.frozen.add(name) self.aliases[name][1] = True
irc.reply(msg, conf.replySuccess) irc.reply(msg, conf.replySuccess)
else: else:
irc.error(msg, 'There is no such alias.') irc.error(msg, 'There is no such alias.')
@ -177,7 +191,7 @@ class Alias(callbacks.Privmsg):
name = privmsgs.getArgs(args) name = privmsgs.getArgs(args)
name = callbacks.canonicalName(name) name = callbacks.canonicalName(name)
if hasattr(self, name) and self.isCommand(name): if hasattr(self, name) and self.isCommand(name):
self.frozen.discard(name) self.aliases[name][1] = False
irc.reply(msg, conf.replySuccess) irc.reply(msg, conf.replySuccess)
else: else:
irc.error(msg, 'There is no such alias.') irc.error(msg, 'There is no such alias.')
@ -198,22 +212,23 @@ class Alias(callbacks.Privmsg):
if [cb for cb in cbs if cb != self]: if [cb for cb in cbs if cb != self]:
s = 'A command with the name %r already exists.' % name s = 'A command with the name %r already exists.' % name
raise AliasError, s raise AliasError, s
if name in self.frozen: if name in self.aliases:
raise AliasError, 'Alias %r is frozen.' % name (currentAlias, frozen) = self.aliases[name]
if frozen and currentAlias != alias:
raise AliasError, 'Alias %r is frozen.' % name
try: try:
f = makeNewAlias(name, alias) f = makeNewAlias(name, alias)
except RecursiveAlias: except RecursiveAlias:
raise AliasError, 'You can\'t define a recursive alias.' raise AliasError, 'You can\'t define a recursive alias.'
setattr(self.__class__, name, f) setattr(self.__class__, name, f)
if freeze: self.aliases[name] = [alias, freeze]
self.frozen.add(name)
def removeAlias(self, name, evenIfFrozen=False): def removeAlias(self, name, evenIfFrozen=False):
name = callbacks.canonicalName(name) name = callbacks.canonicalName(name)
if hasattr(self, name) and self.isCommand(name): if hasattr(self, name) and self.isCommand(name):
if evenIfFrozen or name not in self.frozen: if evenIfFrozen or not self.aliases[name][1]:
delattr(self.__class__, name) delattr(self.__class__, name)
self.frozen.discard(name) del self.aliases[name]
else: else:
raise AliasError, 'That alias is frozen.' raise AliasError, 'That alias is frozen.'
else: else:

View File

@ -126,7 +126,7 @@ class AliasTestCase(ChannelPluginTestCase, PluginDocumentation):
self.assertResponse('foobar', 'sbbone') self.assertResponse('foobar', 'sbbone')
self.assertRaises(Alias.AliasError, cb.removeAlias, 'foobar') self.assertRaises(Alias.AliasError, cb.removeAlias, 'foobar')
cb.removeAlias('foobar', evenIfFrozen=True) cb.removeAlias('foobar', evenIfFrozen=True)
self.failIf('foobar' in cb.frozen) self.failIf('foobar' in cb.aliases)
self.assertNoResponse('foobar', 2) self.assertNoResponse('foobar', 2)
def testOptionalArgs(self): def testOptionalArgs(self):