RFE #807340: OwnerCommands command for setting conf variables.

This commit is contained in:
Jeremy Fincher 2003-09-22 08:36:12 +00:00
parent f137b21dcf
commit cae8905594
3 changed files with 137 additions and 12 deletions

View File

@ -102,6 +102,37 @@ class OwnerCommands(privmsgs.CapabilityCheckingPrivmsg):
else:
irc.error(msg, conf.replyEvalNotAllowed)
def setconf(self, irc, msg, args):
"""<name> <value>
Sets the value of the conf-module variable <name> to <value>.
"""
(name, value) = privmsgs.getArgs(args, needed=2)
if conf.allowEval:
try:
value = eval(value)
except Exception, e:
irc.error(msg, debug.exnToString(e))
return
setattr(conf, name, value)
irc.reply(msg, conf.replySuccess)
else:
if name == 'allowEval':
irc.error(msg, 'You can\'t set the value of allowEval.')
return
elif name not in conf.types:
irc.error(msg, 'I can\'t set that conf variable.')
return
else:
converter = conf.types[name]
try:
value = converter(value)
except ValueError, e:
irc.error(msg, str(e))
return
setattr(conf, name, value)
irc.reply(msg, conf.replySuccess)
def setdefaultcapability(self, irc, msg, args):
"""<capability>

View File

@ -178,11 +178,6 @@ nickmods = ['%s^', '^%s^', '__%s__', '%s_', '%s__', '__%s', '^^%s^^', '{%s}',
###
defaultAllow = True
###
# defaultChannelAllow: does an IrcChannel allow a command by by default?
###
defaultChannelAllow = True
###
# defaultIgnore: True if users should be ignored by default.
# It's a really easy way to make sure that people who want to
@ -227,4 +222,58 @@ version ='0.72.0'
commandsOnStart = []
# This is a dictionary mapping names to converter functions for use in the
# OwnerCommands.setconf command.
def mybool(s):
if s.capitalize() == 'False' or s == '0':
return False
elif s.capitalize() == 'True' or s == '1':
return True
else:
raise ValueError, 'invalid literal for mybool()'
def mystr(s):
while s and s[0] in "'\"" and s[0] == s[-1]:
s = s[1:-1]
return s
types = {
'logDir': mystr,
'confDir': mystr,
'dataDir': mystr,
#'pluginDirs': (list, str),
'userfile': mystr,
'channelfile': mystr,
'logTimestampFormat': mystr,
'humanTimestampFormat': mystr,
'throttleTime': float,
#'allowEval': mybool,
'replyWhenNotCommand': mybool,
'requireRegistration': mybool,
'enablePipeSyntax': mybool,
'replyError': mystr,
'replyNoCapability': mystr,
'replySuccess': mystr,
'replyIncorrectAuth': mystr,
'replyNoUser': mystr,
'replyNotRegistered': mystr,
'replyInvalidArgument': mystr,
'replyRequiresPrivacy': mystr,
'replyEvalNotAllowed': mystr,
'errorReplyPrivate': mybool,
#'telnetEnable': mybool,
#'telnetPort': int,
'poll': float,
#'maxHistory': int,
'pingInterval': float,
#'nickmods': (list, str),
'defaultAllow': mybool,
'defaultIgnore': mybool,
#'ignores': (list, str),
'prefixChars': mystr,
'detailedTracebacks': mybool,
'driverModule': mystr,
}
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

View File

@ -36,15 +36,28 @@ import conf
class OwnerCommandsTestCase(PluginTestCase, PluginDocumentation):
plugins = ('OwnerCommands',)
def testEval(self):
conf.allowEval = True
s = "[irc.__class__ for irc in " \
"irc.getCallback('Relay').ircstates.keys()]"
self.assertNotRegexp('eval ' + s, '^SyntaxError')
try:
originalConfAllowEval = conf.allowEval
conf.allowEval = True
s = "[irc.__class__ for irc in " \
"irc.getCallback('Relay').ircstates.keys()]"
self.assertNotRegexp('eval ' + s, '^SyntaxError')
conf.allowEval = False
self.assertError('eval 100')
finally:
conf.allowEval = originalConfAllowEval
def testExec(self):
self.assertNotError('exec conf.foo = True')
self.failUnless(conf.foo)
del conf.foo
try:
originalConfAllowEval = conf.allowEval
conf.allowEval = True
self.assertNotError('exec conf.foo = True')
self.failUnless(conf.foo)
del conf.foo
conf.allowEval = False
self.assertError('exec conf.foo = True')
finally:
conf.allowEval = originalConfAllowEval
def testSettrace(self):
self.assertNotError('settrace')
@ -84,6 +97,38 @@ class OwnerCommandsTestCase(PluginTestCase, PluginDocumentation):
def testSay(self):
self.assertResponse('say %s foo' % self.irc.nick, 'foo')
def testSetconf(self):
try:
originalConfAllowEval = conf.allowEval
conf.allowEval = False
self.assertError('setconf alsdkfj 100')
self.assertError('setconf poll "foo"')
try:
originalReplySuccess = conf.replySuccess
self.assertResponse('setconf replySuccess foo', 'foo')
self.assertResponse('setconf replySuccess "foo"', 'foo')
self.assertResponse('setconf replySuccess \'foo\'', 'foo')
finally:
conf.replySuccess = originalReplySuccess
try:
originalReplyWhenNotCommand = conf.replyWhenNotCommand
self.assertNotError('setconf replyWhenNotCommand True')
self.failUnless(conf.replyWhenNotCommand)
self.assertNotError('setconf replyWhenNotCommand False')
self.failIf(conf.replyWhenNotCommand)
self.assertNotError('setconf replyWhenNotCommand true')
self.failUnless(conf.replyWhenNotCommand)
self.assertNotError('setconf replyWhenNotCommand false')
self.failIf(conf.replyWhenNotCommand)
self.assertNotError('setconf replyWhenNotCommand 1')
self.failUnless(conf.replyWhenNotCommand)
self.assertNotError('setconf replyWhenNotCommand 0')
self.failIf(conf.replyWhenNotCommand)
finally:
conf.replyWhenNotCommand = originalReplyWhenNotCommand
finally:
conf.allowEval = originalConfAllowEval