+ args = list(args)
+ if args[0].startswith("@"):
+ cat = args.pop(0)[1:].lower()
+ questions = state.db['questions'][cat]
+ else:
+ questions = state.questions
+
+ pattern = re.escape(' '.join(args))
+ return _findq(questions, pattern)
+
+@lib.hook(glevel=1, needchan=False)
+@lib.help("[@<category>] <regex>", "finds a question (qid) given a regex")
+@lib.argsGE(1)
+def findqre(bot, user, chan, realtarget, *args):
+ args = list(args)
+ if args[0].startswith("@"):
+ cat = args.pop(0)[1:].lower()
+ questions = state.db['questions'][cat]
+ else:
+ questions = state.questions
+ pattern = ' '.join(args)
+ return _findq(questions, pattern)
+
+@lib.hook(glevel=1, needchan=False)
+@lib.help("[@<category>] <phrase>", "finds a question (qid) given a (partial) question or answer")
+@lib.argsGE(1)
+def findqa(bot, user, chan, realtarget, *args):
+ args = list(args)
+ if args[0].startswith("@"):
+ cat = args.pop(0)[1:].lower()
+ questions = state.db['questions'][cat]
+ else:
+ questions = state.questions
+ pattern = ' '.join(args)
+ return _findq(questions, pattern, True)
+
+def _findq(questions, pattern, check_answers=False):
+ searcher = re.compile(pattern, re.IGNORECASE)
+ matches = [i for i in range(len(questions)) if searcher.search(questions[i][0]) is not None or (check_answers and searcher.search(questions[i][1]) is not None)]
+ if len(matches) > 25:
+ return "Too many matches! (>25)"
+ elif len(matches) > 1:
+ return "Multiple matches: %s" % (', '.join(str(x) for x in matches))