From eeb60821c73596fb3b1c2426bc26dbcd59eb7bb3 Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Sun, 1 Nov 2015 19:46:30 +0300 Subject: Support different bins --- src/ZeroBin.hs | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/ZeroBin.hs b/src/ZeroBin.hs index 13c229e..fac93a5 100644 --- a/src/ZeroBin.hs +++ b/src/ZeroBin.hs @@ -2,7 +2,6 @@ module ZeroBin ( Expiration(..), - pasteEc, share ) where @@ -17,9 +16,6 @@ import qualified Data.ByteString.Char8 as C import qualified Data.ByteString.Lazy as L import qualified Network.HTTP.Conduit as HTTP -pasteEc :: String -pasteEc = "https://paste.ec" - data Response = Response { status :: String , message :: Maybe String @@ -41,25 +37,28 @@ instance Show Expiration where show Month = "1_month" show Never = "never" -post :: Expiration -> Content -> IO Response -post ex ct = do - req' <- HTTP.parseUrl $ pasteEc ++ "/paste/create" +post :: String -> Expiration -> Content -> IO (Either String String) +post bin ex ct = do + req' <- HTTP.parseUrl $ bin ++ "/paste/create" let req = HTTP.urlEncodedBody [ (C.pack "expiration" , C.pack $ show ex) , (C.pack "content" , L.toStrict $ JSON.encode ct) ] req' manager <- HTTP.newManager HTTP.tlsManagerSettings response <- HTTP.httpLbs req manager - return . fromJust . JSON.decode $ HTTP.responseBody response - -share :: Expiration -> ByteString -> IO (Either String String) -share ex txt = do - pwd <- makePassword 33 - c <- encrypt pwd (encode txt) - resp <- post ex c + let resp = fromJust . JSON.decode $ HTTP.responseBody response case status resp of "ok" -> return . Right $ - pasteEc ++ "/paste/" ++ (fromJust . paste) resp ++ "#" ++ pwd + bin ++ "/paste/" ++ (fromJust . paste) resp _ -> return . Left $ (fromJust . message) resp +share :: String -> Expiration -> ByteString -> IO (Either String String) +share bin ex txt = do + pwd <- makePassword 33 + c <- encrypt pwd (encode txt) + append pwd `fmap` post bin ex c + where + append _ (Left e) = Left e + append p (Right u) = Right $ u ++ "#" ++ p + -- cgit v1.2.3