diff --git a/plugins/FunDB.py b/plugins/FunDB.py index 3e2b92f83..87b15ea86 100755 --- a/plugins/FunDB.py +++ b/plugins/FunDB.py @@ -302,7 +302,7 @@ class FunDB(callbacks.Privmsg): try: id = int(id) except ValueError: - irc.error(msg, 'You must give a numeric id.') + irc.error(msg, 'The argument must be an integer.') return if table not in self._tables: irc.error(msg, '"%s" is not valid. Valid values include %s' % \ @@ -314,6 +314,50 @@ class FunDB(callbacks.Privmsg): self.db.commit() irc.reply(msg, conf.replySuccess) + def dbchange(self, irc, msg, args): + """ + + Changes the data, referred to in the first argument, with the id + number according to the regular expression . is the + zero-based index into the db; is a regular expression of the + form s/regexp/replacement/flags. + """ + (table, id, regexp) = privmsgs.getArgs(args, needed=3) + table = table.lower() + try: + ircdb.users.getUser(msg.prefix).name + except KeyError: + irc.error(msg, conf.replyNotRegistered) + return + try: + id = int(id) + except ValueError: + irc.error(msg, 'The argument must be an integer.') + return + if table not in self._tables: + irc.error(msg, '"%s" is not valid. Valid values include %s' % \ + (table, utils.commaAndify(self._tables))) + return + try: + replacer = utils.perlReToReplacer(regexp) + except ValueError, e: + irc.error(msg, 'The regexp wasn\'t valid: %s' % e.args[0]) + except re.error, e: + irc.error(msg, debug.exnToString(e)) + return + cursor = self.db.cursor() + sql = """SELECT %s FROM %ss WHERE id=%%s""" % (table, table) + cursor.execute(sql, id) + if cursor.rowcount == 0: + irc.error(msg, 'There is no such %s.' % table) + else: + old_entry = cursor.fetchone()[0] + new_entry = replacer(old_entry) + sql = """UPDATE %ss SET %s=%%s WHERE id=%%s""" % (table, table) + cursor.execute(sql, new_entry, id) + self.db.commit() + irc.reply(msg, conf.replySuccess) + def dbnum(self, irc, msg, args): """ @@ -346,7 +390,7 @@ class FunDB(callbacks.Privmsg): try: id = int(id) except ValueError: - irc.error(msg, ' must be an integer.') + irc.error(msg, 'The argument must be an integer.') return if table not in self._tables: irc.error(msg, '"%s" is not valid. Valid values include %s' % \ diff --git a/test/test_FunDB.py b/test/test_FunDB.py index 0c7064a42..ba68e0e85 100644 --- a/test/test_FunDB.py +++ b/test/test_FunDB.py @@ -104,5 +104,12 @@ class TestFunDB(PluginTestCase, PluginDocumentation): self.assertNotError('dbnum excuse') self.assertNotError('dbnum insult') + def testDbChange(self): + self.assertNotError('dbadd praise teaches $who perl') + self.assertNotError('dbchange praise 1 s/perl/python/') + self.assertResponse('praise #foo jemfinch', '\x01ACTION teaches'\ + ' jemfinch python (#1)\x01') + self.assertNotError('dbremove praise 1') + # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: