From cae8905594e086b6e57ec521794e6765ec5812d0 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Mon, 22 Sep 2003 08:36:12 +0000 Subject: [PATCH] RFE #807340: OwnerCommands command for setting conf variables. --- src/OwnerCommands.py | 31 ++++++++++++++++++++ src/conf.py | 59 ++++++++++++++++++++++++++++++++++---- test/test_OwnerCommands.py | 59 +++++++++++++++++++++++++++++++++----- 3 files changed, 137 insertions(+), 12 deletions(-) diff --git a/src/OwnerCommands.py b/src/OwnerCommands.py index 9f779265b..ecba557e3 100644 --- a/src/OwnerCommands.py +++ b/src/OwnerCommands.py @@ -102,6 +102,37 @@ class OwnerCommands(privmsgs.CapabilityCheckingPrivmsg): else: irc.error(msg, conf.replyEvalNotAllowed) + def setconf(self, irc, msg, args): + """ + + Sets the value of the conf-module variable to . + """ + (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): """ diff --git a/src/conf.py b/src/conf.py index 26659494c..228623c5d 100644 --- a/src/conf.py +++ b/src/conf.py @@ -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: diff --git a/test/test_OwnerCommands.py b/test/test_OwnerCommands.py index ae21e0623..ace59545a 100644 --- a/test/test_OwnerCommands.py +++ b/test/test_OwnerCommands.py @@ -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