Converted searchfactoids to accept a regular expression.

This commit is contained in:
Jeremy Fincher 2003-09-05 08:15:26 +00:00
parent 7b3a62eacf
commit 5fbba15857

View File

@ -333,20 +333,23 @@ class Factoids(ChannelDBHandler, callbacks.Privmsg):
_sqlTrans = string.maketrans('*?', '%_') _sqlTrans = string.maketrans('*?', '%_')
def searchfactoids(self, irc, msg, args): def searchfactoids(self, irc, msg, args):
"""[<channel>] <text> """[<channel>] <regexp>
Searches the keyspace for keys containing <text> in them. Can use Searches the keyspace for keys matching <regexp>.
arbitrary SQL LIKE wildcards or shell glob wildcards.
""" """
channel = privmsgs.getChannel(msg, args) channel = privmsgs.getChannel(msg, args)
keySearch = privmsgs.getArgs(args) regexp = privmsgs.getArgs(args)
keySearch = keySearch.translate(self._sqlTrans) try:
if keySearch.translate(string.ascii, '%_*?') == '': r = utils.perlReToPythonRe(regexp)
irc.error(msg, 'You can\'t search for just wildcards.') def p(s):
return int(bool(r.search(s)))
except ValueError, e:
irc.error(msg, 'Invalid regular expression.')
return return
db = self.getDb(channel) db = self.getDb(channel)
db.create_function('p', 1, p)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT key FROM keys WHERE key LIKE %s""", keySearch) cursor.execute("""SELECT key FROM keys WHERE p(key)""")
if cursor.rowcount == 0: if cursor.rowcount == 0:
irc.reply(msg, 'No keys matched that query.') irc.reply(msg, 'No keys matched that query.')
elif cursor.rowcount > 100: elif cursor.rowcount > 100: