module Utils.PastePipe where
import Control.Monad (when)
import Data.Maybe
import Network.Browser
import Network.HTTP.Base
import Network.URI
import System.Console.CmdArgs
import System.Environment (getEnv)
data Config = Config { userName :: String
, language :: String
, title :: String
, uri :: String
, test :: Bool }
deriving (Show, Data, Typeable)
config :: String -> Config
config realUser = Config { userName = realUser
&= help "Your user name"
&= typ "USER"
&= explicit
&= name "user"
, language = "haskell"
&= help "The language used for syntax highlighting"
&= typ "LANGUAGE"
, title = ""
&= help "The title of the snippet"
&= typ "TITLE"
&= explicit
&= name "title"
&= name "t"
, uri = defaultUri
&= help "The URI of the lpaste instance to post to"
&= typ "URL"
, test = False
&= help "Prevents PastePipe from actually posting content, just echos the configuration and input"
}
&= summary "PastePipe v1.3, (C) Rogan Creswick 2009"
&= program "pastepipe"
postWithDefaults :: String -> IO URI
postWithDefaults s = getEnv "USER" >>= \u -> post (config u) s
outHandler :: String -> IO ()
outHandler str = do
loud <- isLoud
when loud $ putStr str
defaultUri :: String
defaultUri = "http://lpaste.net/"
saveUri :: String -> URI
saveUri coreUri = buildURI coreUri "new"
buildURI :: String -> String -> URI
buildURI coreUri str = fromJust $ parseURI $ coreUri ++ str
post :: Config -> String -> IO URI
post conf str = do
(url, _) <- Network.Browser.browse $ do
setOutHandler outHandler
setAllowRedirects True
request $ buildRequest conf str
return url
buildRequest :: Config -> String -> Request String
buildRequest conf str = formToRequest $ Form POST (saveUri $ uri conf)
[ ("title", title conf)
, ("author", userName conf)
, ("paste", str)
, ("language", language conf)
, ("channel", "")
, ("email", "")
]
fakePost :: Config -> String -> IO URI
fakePost conf str = do
putStrLn $ "uri: "++uri conf
putStrLn $ "user: "++userName conf
putStrLn $ "lang: "++language conf
putStrLn $ "title: "++title conf
putStrLn $ "content: "++str
return $ fromJust $ parseURI $ uri conf