hunk ./Network/Twitter.hs 17 -import Data.Map as M -import qualified Data.Map +import Data.Map (Map) +import qualified Data.Map as M hunk ./Network/Twitter.hs 21 -import Network.Browser -import Network.HTTP hiding (user) +import Network.Curl hunk ./Network/Twitter.hs 33 + , twCurl :: Curl hunk ./Network/Twitter.hs 45 - = do authInfo <- newTVarIO Nothing + = do curl <- initialize + authInfo <- newTVarIO Nothing hunk ./Network/Twitter.hs 51 + , twCurl = curl hunk ./Network/Twitter.hs 88 - ] (decodeString $ rspBody res) + ] (decodeString $ respBody res) hunk ./Network/Twitter.hs 122 -doReq :: Twitter -> FilePath -> RequestMethod -> [(String, String)] -> IO Response -doReq tw path method args - = do let uri = appendPath path $ twBaseURI tw - frm = Form method uri args - req = formToRequest frm - auth <- atomically $ readTVar (twAuthInfo tw) - (_, res) <- browse $ do setAllowRedirects True - setAllowBasicAuth True - setAuthorityGen (\ _ _ -> return auth) - setErrHandler (const $ return ()) - setOutHandler (const $ return ()) - request req - case rspCode res - of (2, _, _) -> return res - _ -> fail (show (rspCode res) ++ " " ++ rspReason res) - - -get :: Twitter -> FilePath -> [(String, String)] -> IO Response +get :: Twitter -> FilePath -> [(String, String)] -> IO CurlResponse hunk ./Network/Twitter.hs 124 - = doReq tw path GET args + = do let q = "?" ++ joinWith ";" (map zipTuple args) + uri = appendPath path $ twBaseURI tw + h = twCurl tw + setopt h $ CurlURL $ uriToString id (uri { uriQuery = q }) "" + setopt h $ CurlHttpGet True + perform_with_response h hunk ./Network/Twitter.hs 132 -post :: Twitter -> FilePath -> [(String, String)] -> IO Response +post :: Twitter -> FilePath -> [(String, String)] -> IO CurlResponse hunk ./Network/Twitter.hs 134 - = doReq tw path POST args + = do let uri = appendPath path $ twBaseURI tw + h = twCurl tw + setopt h $ CurlURL $ uriToString id uri "" + setopt h $ CurlPost True + setopt h $ CurlPostFields $ map zipTuple args + perform_with_response h + + +zipTuple :: (String, String) -> String +zipTuple (a, b) = a ++ "=" ++ b + +joinWith :: [a] -> [[a]] -> [a] +joinWith _ [] = [] +joinWith _ [x] = x +joinWith sep (x:xs) = x ++ sep ++ joinWith sep xs hunk ./Network/Twitter.hs 153 - = do let uri = appendPath "account/verify_credentials.xml" $ twBaseURI tw - req = defaultGETRequest uri - (_, res) <- browse $ do setAllowRedirects True - setAllowBasicAuth True - setAuthorityGen (\ _ _ -> return $ Just (user, pass)) - setErrHandler (const $ return ()) - setOutHandler (const $ return ()) - request req - case rspCode res - of (2,0,0) -> atomically $ writeTVar (twAuthInfo tw) $ Just (user, pass) - _ -> fail (show (rspCode res) ++ " " ++ rspReason res) + = do let h = twCurl tw + setopt h $ CurlUserPwd $ user ++ ":" ++ pass + res <- get tw "account/verify_credentials.xml" [] + case respCurlCode res of + CurlOK -> atomically $ writeTVar (twAuthInfo tw) $ Just (user, pass) + _ -> do setopt h $ CurlUserPwd "" + fail $ show $ respCurlCode res hunk ./Network/Twitter.hs 177 - ] (decodeString $ rspBody res) + ] (decodeString $ respBody res) hunk ./Twitprox.hs 16 +import Network.Curl hunk ./Twitprox.hs 25 -main = do ircd <- startIRCd "twitprox" (PortNumber 9090) +main = withCurlDo $ + do ircd <- startIRCd "twitprox" (PortNumber 9090) hunk ./twitprox.cabal 10 - HTTP, base, containers, filepath, hxt, irc, network, + base, containers, curl, filepath, hxt, irc, network, hunk ./twitprox.cabal 12 -