from tehbot.plugins import *
import tehbot.plugins as plugins
import wolframalpha
import re
import pipes
import random

class AiHandler(ChannelHandler):
    @staticmethod
    def decision_answers():
        return [
                "Yes",
                "No",
                "Sure",
                "Absolutely",
                "For sure",
                "No, you are!",
                "Will you ever get sick of this?",
                "Certainly not",
                "I haven't made up my mind yet."
                ]

    def initialize(self, dbconn):
        ChannelHandler.initialize(self, dbconn)
        try:
            self.client = wolframalpha.Client(self.settings["wolframalpha_app_id"])
            self.ai_enabled = True
        except:
            self.ai_enabled = False

    def execute(self, connection, event, extra, dbconn):
        botname = self.tehbot.settings.value("botname", connection)
        decide_regex = [
                re.compile(r'''^(?:ok(?:ay)?|hey|hay)\s+%s\s*,?\s*(?:is|are|can|has|was|were|should|do|does)\s+\S[\s\S]*\?''' % botname, re.I),
                ]
        regex = [
                re.compile(r'''^(?:ok(?:ay)?|hey|hay)\s+%s\s*,?\s*(?P<what>.*?)\s*\??$''' % botname, re.I),
                ]
        solved_regex = [
                re.compile(r'''^(?:ok(?:ay)?|hey|hay)\s+%s\s*,?\s*has\s+(?P<who>\S+)\s+solved\s+(?P<chall>\S[\s\S]*?|"[^"]+"|'[^']+')(?:\s+on\s+(?P<site>\S[\s\S]*?|"[^"]+"|'[^']+'))?\s*\??$''' % botname, re.I),
                re.compile(r'''^(?:ok(?:ay)?|hey|hay)\s+%s\s*,?\s*did\s+(?P<who>\S+)\s+solve\s+(?P<chall>\S[\s\S]*?|"[^"]+"|'[^']+')(?:\s+on\s+(?P<site>\S[\s\S]*?|"[^"]+"|'[^']+'))?\s*\??$''' % botname, re.I)
                ]

        for r in solved_regex:
            match = r.search(extra["msg"])
            if match is not None:
                user = match.group(1)
                chall = match.group(2)
                site = match.group(3)

                plugin = self.tehbot.cmd_handlers["solvers"]
                chall = " ".join(plugins.mysplit(chall))
                args = '-u %s %s' % (user, pipes.quote(chall))
                if site is not None:
                    site = " ".join(plugins.mysplit(site))
                    args = args + " -s %s" % pipes.quote(site)
                plugin.handle(connection, event, {"args":args}, dbconn)
                return

        if not self.ai_enabled:
            return

        for r in decide_regex:
            match = r.search(extra["msg"])
            if match is not None:
                return random.choice(AiHandler.decision_answers())

        for r in regex:
            match = r.search(extra["msg"])
            if match is not None:
                what = match.group(1)

                try:
                    for p in self.client.query(what).pods:
                        if p.id == "Result" and p.text:
                            txt = " ".join(p.text.splitlines())
                            return plugins.shorten("%s: %s" % (event.source.nick, txt), 350)

                    for p in self.client.query(what).pods:
                        if p.id == "Misc" and p.text:
                            txt = " ".join(p.text.splitlines())
                            print txt

                    raise Exception("hm")
                except:
                    return [("me", "shrugs")]

register_channel_handler(AiHandler())

@@ -55,16 +55,64 @@
 plugins%0A
+from tehbot.plugins.duckduckgo import ddgsearch%0A
 import w
@@ -759,86 +759,620 @@
     
-    self.ai_enabled = True%0A        except:%0A            self.ai_enabled = False
+except:%0A            self.client = None%0A%0A    def ddg_handler(self, what):%0A        return ddgsearch(what)%0A%0A    def wolfram_handler(self, what):%0A        if not self.client:%0A            return%0A%0A        for p in self.client.query(what).pods:%0A            if p.id == %22Result%22 and p.text:%0A                txt = %22 %22.join(p.text.splitlines())%0A                return plugins.shorten(%22%25s: %25s%22 %25 (event.source.nick, txt), 350)%0A%0A        for p in self.client.query(what).pods:%0A            if p.id == %22Misc%22 and p.text:%0A                txt = %22 %22.join(p.text.splitlines())%0A                print txt%0A%0A        raise Exception(%22hm%22)
 %0A%0A  
@@ -2285,24 +2285,137 @@
          %5D%0A%0A
+        backend = self.settings.get(%22backend%22, %22ddg%22)%0A        aihandler = getattr(self, %22%25s_handler%22 %25 backend)%0A%0A
         for 
@@ -3066,60 +3066,8 @@
 rn%0A%0A
-        if not self.ai_enabled:%0A            return%0A%0A
     
@@ -3423,518 +3423,30 @@
     
-for p in self.client.query(what).pods:%0A                        if p.id == %22Result%22 and p.text:%0A                            txt = %22 %22.join(p.text.splitlines())%0A                            return plugins.shorten(%22%25s: %25s%22 %25 (event.source.nick, txt), 350)%0A%0A                    for p in self.client.qu
+return aihandl
 er
-y
 (what)
-.pods:%0A                        if p.id == %22Misc%22 and p.text:%0A                            txt = %22 %22.join(p.text.splitlines())%0A                            print txt%0A%0A                    raise Exception(%22hm%22)
 %0A   

switch from WolframAlpha to DuckDuckGo