recaptchaChallengeKey :: String -> Blast String
recaptchaChallengeKey key = do
    rawjsstr <- T.unpack <$> httpGetStr recaptchaUrl
    return $ fromMaybe (error $ fatalErrorMsg ++ ": " ++ rawjsstr) $
            findMap getChallenge $ lines rawjsstr
  where getChallenge s =
            takeUntil (=='\'') <$>
                stripPrefix "challenge : \'" (dropWhile isSpace s)
        fatalErrorMsg =
            "FATAL ERROR: getNewCaptcha: Recaptcha changed their "
            ++ "JSON formatting, update code"
        recaptchaUrl =
            "http://api.recaptcha.net/challenge?k=" ++ key ++ "&lang=en"

{-
getChallengeKey :: String -> Blast String
getChallengeKey key = do
    rawjsstr <- T.unpack <$> httpGetStr ("http://api.recaptcha.net/challenge?k=" ++ key ++ "&lang=en")
    return $ headNote (fatalErrorMsg ++ ": " ++ rawjsstr) $
        mapMaybe getChallenge $ lines rawjsstr
  where getChallenge s =
            takeUntil (=='\'') <$> stripPrefix "challenge : \'" (dropWhile isSpace s)
        fatalErrorMsg = "FATAL ERROR: getChallengeKey: Recaptcha changed their JSON formatting, update code"

reloadCaptcha :: String -> String -> Blast ()
reloadCaptcha key chKey = void $
    httpGet $ "http://www.google.com/recaptcha/api/reload?c="
                    ++ chKey ++ "&k=" ++ key ++ "&reason=r&type=image&lang=en"

getCaptchaImage :: String -> Blast LByteString
getCaptchaImage chKey =
    httpGetLbs $ "http://www.google.com/recaptcha/api/image?c=" ++ chKey

ssachGetCaptcha :: Board -> Maybe Int -> String -> String -> Blast (Maybe LByteString)
ssachGetCaptcha board thread key chKey =
    ifM (doWeNeedCaptcha board thread "")
        (do reloadCaptcha key chKey
            Just <$> getCaptchaImage chKey)
        (return Nothing)
-}

{-

data SolveMedia = SolveMedia
    {solveMediaKey :: String
    ,solveMediaMagic :: String
    ,solveMediaTStamp :: !Int
    ,solveMediaChalStamp :: !Int
    ,solveMediaCallbacks :: !(IORef Int) -- ^ How many times we reloaded captcha
    ,solveMediaFwv :: String
    ,solveMediaChid :: !(IORef String)
    }

instance Captcha SolveMedia where
    getNewCaptcha board thread usercode = do
        res <- makabaCaptcha board thread usercode
        if T.isInfixOf "OK" res || T.isInfixOf "VIP" res
          then return $ Left $ CAnswer True []
          else do
            let ckey = fromMaybe ssachSolveMediaKey $ atMay $ lines res

            chScriptLines <- fmap (lines . T.unpack) $ accept $ httpGetLbs $
                "http://api.solvemedia.com/papi/challenge.script?k=" ++ ckey

            let
              !magic = fromMaybe (magicerr $ unlines chScriptLines) $
                flip findMap chScriptLines $ \x ->
                  takeUntil (=='\'') . tail . dropUntil (=='\'') <$>
                    stripPrefix "magic" (dropWhile isSpace x)

              !chalstamp = fromMaybe (chalstamperr $ unlines chScriptLines) $
                flip findMap chScriptLines $ \x ->
                  readMay . takeUntil (==',') . dropUntil isNumber =<<
                    stripPrefix "chalstamp" (dropWhile isSpace x)

            _puzzle <- accept $ httpGetLbs "http://api.solvemedia.com/papi/_puzzle.js"

            fwid <- take 4 <$> getRandomRs ('a', 'z')
            ctx_bN :: Int <- getRandomR (10, 99)
      where
        accept = withOverrideHeader (hAccept, "*/*")
        magicerr = error . ("SolveMedia: Couldn't read magic from " ++)
        chalstamperr = error . ("SolveMedia: Couldn't read chalstamp from " ++)

    unsafeGenNewCaptcha = Just $

-}
recaptchaChallengeKey :: String -> Blast String
recaptchaChallengeKey key = do
    rawjsstr <- T.unpack <$> httpGetStr recaptchaUrl
    return $ fromMaybe (error $ fatalErrorMsg ++ ": " ++ rawjsstr) $
            findMap getChallenge $ lines rawjsstr
  where getChallenge s =
            takeUntil (=='\'') <$>
                stripPrefix "challenge : \'" (dropWhile isSpace s)
        fatalErrorMsg =
            "FATAL ERROR: getNewCaptcha: Recaptcha changed their "
            ++ "JSON formatting, update code"
        recaptchaUrl =
            "http://api.recaptcha.net/challenge?k=" ++ key ++ "&lang=en"

{-
getChallengeKey :: String -> Blast String
getChallengeKey key = do
    rawjsstr <- T.unpack <$> httpGetStr ("http://api.recaptcha.net/challenge?k=" ++ key ++ "&lang=en")
    return $ headNote (fatalErrorMsg ++ ": " ++ rawjsstr) $
        mapMaybe getChallenge $ lines rawjsstr
  where getChallenge s =
            takeUntil (=='\'') <$> stripPrefix "challenge : \'" (dropWhile isSpace s)
        fatalErrorMsg = "FATAL ERROR: getChallengeKey: Recaptcha changed their JSON formatting, update code"

reloadCaptcha :: String -> String -> Blast ()
reloadCaptcha key chKey = void $
    httpGet $ "http://www.google.com/recaptcha/api/reload?c="
                    ++ chKey ++ "&k=" ++ key ++ "&reason=r&type=image&lang=en"

getCaptchaImage :: String -> Blast LByteString
getCaptchaImage chKey =
    httpGetLbs $ "http://www.google.com/recaptcha/api/image?c=" ++ chKey

ssachGetCaptcha :: Board -> Maybe Int -> String -> String -> Blast (Maybe LByteString)
ssachGetCaptcha board thread key chKey =
    ifM (doWeNeedCaptcha board thread "")
        (do reloadCaptcha key chKey
            Just <$> getCaptchaImage chKey)
        (return Nothing)
-}

{-

data SolveMedia = SolveMedia
    {solveMediaKey :: String
    ,solveMediaMagic :: String
    ,solveMediaTStamp :: !Int
    ,solveMediaChalStamp :: !Int
    ,solveMediaCallbacks :: !(IORef Int) -- ^ How many times we reloaded captcha
    ,solveMediaFwv :: String
    ,solveMediaChid :: !(IORef String)
    }

instance Captcha SolveMedia where
    getNewCaptcha board thread usercode = do
        res <- makabaCaptcha board thread usercode
        if T.isInfixOf "OK" res || T.isInfixOf "VIP" res
          then return $ Left $ CAnswer True []
          else do
            let ckey = fromMaybe ssachSolveMediaKey $ atMay $ lines res

            chScriptLines <- fmap (lines . T.unpack) $ accept $ httpGetLbs $
                "http://api.solvemedia.com/papi/challenge.script?k=" ++ ckey

            let
              !magic = fromMaybe (magicerr $ unlines chScriptLines) $
                flip findMap chScriptLines $ \x ->
                  takeUntil (=='\'') . tail . dropUntil (=='\'') <$>
                    stripPrefix "magic" (dropWhile isSpace x)

              !chalstamp = fromMaybe (chalstamperr $ unlines chScriptLines) $
                flip findMap chScriptLines $ \x ->
                  readMay . takeUntil (==',') . dropUntil isNumber =<<
                    stripPrefix "chalstamp" (dropWhile isSpace x)

            _puzzle <- accept $ httpGetLbs "http://api.solvemedia.com/papi/_puzzle.js"

            fwid <- take 4 <$> getRandomRs ('a', 'z')
            ctx_bN :: Int <- getRandomR (10, 99)
      where
        accept = withOverrideHeader (hAccept, "*/*")
        magicerr = error . ("SolveMedia: Couldn't read magic from " ++)
        chalstamperr = error . ("SolveMedia: Couldn't read chalstamp from " ++)

    unsafeGenNewCaptcha = Just $

-}
recaptchaChallengeKey key = do
    rawjsstr <- T.unpack <$> httpGetStr recaptchaUrl
    return $ fromMaybe (error $ fatalErrorMsg ++ ": " ++ rawjsstr) $
            findMap getChallenge $ lines rawjsstr
  where getChallenge s =
            takeUntil (=='\'') <$>
                stripPrefix "challenge : \'" (dropWhile isSpace s)
        fatalErrorMsg =
            "FATAL ERROR: getNewCaptcha: Recaptcha changed their "
            ++ "JSON formatting, update code"
        recaptchaUrl =
            "http://api.recaptcha.net/challenge?k=" ++ key ++ "&lang=en"

{-
getChallengeKey :: String -> Blast String
getChallengeKey key = do
    rawjsstr <- T.unpack <$> httpGetStr ("http://api.recaptcha.net/challenge?k=" ++ key ++ "&lang=en")
    return $ headNote (fatalErrorMsg ++ ": " ++ rawjsstr) $
        mapMaybe getChallenge $ lines rawjsstr
  where getChallenge s =
            takeUntil (=='\'') <$> stripPrefix "challenge : \'" (dropWhile isSpace s)
        fatalErrorMsg = "FATAL ERROR: getChallengeKey: Recaptcha changed their JSON formatting, update code"

reloadCaptcha :: String -> String -> Blast ()
reloadCaptcha key chKey = void $
    httpGet $ "http://www.google.com/recaptcha/api/reload?c="
                    ++ chKey ++ "&k=" ++ key ++ "&reason=r&type=image&lang=en"

getCaptchaImage :: String -> Blast LByteString
getCaptchaImage chKey =
    httpGetLbs $ "http://www.google.com/recaptcha/api/image?c=" ++ chKey

ssachGetCaptcha :: Board -> Maybe Int -> String -> String -> Blast (Maybe LByteString)
ssachGetCaptcha board thread key chKey =
    ifM (doWeNeedCaptcha board thread "")
        (do reloadCaptcha key chKey
            Just <$> getCaptchaImage chKey)
        (return Nothing)
-}

{-

data SolveMedia = SolveMedia
    {solveMediaKey :: String
    ,solveMediaMagic :: String
    ,solveMediaTStamp :: !Int
    ,solveMediaChalStamp :: !Int
    ,solveMediaCallbacks :: !(IORef Int) -- ^ How many times we reloaded captcha
    ,solveMediaFwv :: String
    ,solveMediaChid :: !(IORef String)
    }

instance Captcha SolveMedia where
    getNewCaptcha board thread usercode = do
        res <- makabaCaptcha board thread usercode
        if T.isInfixOf "OK" res || T.isInfixOf "VIP" res
          then return $ Left $ CAnswer True []
          else do
            let ckey = fromMaybe ssachSolveMediaKey $ atMay $ lines res

            chScriptLines <- fmap (lines . T.unpack) $ accept $ httpGetLbs $
                "http://api.solvemedia.com/papi/challenge.script?k=" ++ ckey

            let
              !magic = fromMaybe (magicerr $ unlines chScriptLines) $
                flip findMap chScriptLines $ \x ->
                  takeUntil (=='\'') . tail . dropUntil (=='\'') <$>
                    stripPrefix "magic" (dropWhile isSpace x)

              !chalstamp = fromMaybe (chalstamperr $ unlines chScriptLines) $
                flip findMap chScriptLines $ \x ->
                  readMay . takeUntil (==',') . dropUntil isNumber =<<
                    stripPrefix "chalstamp" (dropWhile isSpace x)

            _puzzle <- accept $ httpGetLbs "http://api.solvemedia.com/papi/_puzzle.js"

            fwid <- take 4 <$> getRandomRs ('a', 'z')
            ctx_bN :: Int <- getRandomR (10, 99)
      where
        accept = withOverrideHeader (hAccept, "*/*")
        magicerr = error . ("SolveMedia: Couldn't read magic from " ++)
        chalstamperr = error . ("SolveMedia: Couldn't read chalstamp from " ++)

    unsafeGenNewCaptcha = Just $

-}