authinfo-hs (empty) → 0.1.0.0
raw patch · 4 files changed
+106/−0 lines, 4 filesdep +attoparsecdep +basedep +networksetup-changed
Dependencies added: attoparsec, base, network, text
Files
- LICENSE +20/−0
- Setup.hs +2/−0
- System/Authinfo.hs +55/−0
- authinfo-hs.cabal +29/−0
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2014 Robert Glossop++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ System/Authinfo.hs view
@@ -0,0 +1,55 @@+{-# LANGUAGE OverloadedStrings #-}+module System.Authinfo (readAuthinfo, getPassword) where+import System.Environment+import Data.Attoparsec.Text hiding (take)+import Data.List+import Control.Applicative+import qualified Data.Text as T+import qualified Data.Text.IO as T+import Control.Monad+import Network+import Data.Maybe+import Data.Char++type Line = (T.Text, T.Text, T.Text, Maybe PortNumber)+dropBack :: Int -> [a] -> [a]+dropBack n l = take (length l - n) l++quoted = char '"' *> takeWhile1 (not . (=='"')) <* char '"'+lstring s = lexeme $ string s+lexeme p = skipSpace *> p <* skipSpace+word = takeWhile1 $ not . isSpace+attempt p = fmap Just p <|> return Nothing+-- a possibly-quoted field+field = do p <- peekChar'+ if p == '"'+ then quoted+ else word++-- parse line+line :: Parser Line+line = fmap (\(Just h, Just l, Just p, r) -> (h,l,p,r)) + (line' (Nothing,Nothing,Nothing,Nothing))+ where line' s@(h,l,p,r) = + msum [ char '\n' >> return s+ , lstring "machine" >> field >>= (\h -> line' (Just h,l,p,r))+ , lstring "login" >> field >>= (\l -> line' (h,Just l,p,r))+ , lstring "password" >> field >>= (\p -> line' (h,l,Just p,r))+ , lstring "port" >> decimal >>= (\r -> line' (h,l,p,Just r))+ ] <?> "line"++file = many line++-- |Parses whole authinfo file+readAuthinfo :: IO [Line]+readAuthinfo = do+ home <- getEnv "HOME"+ Right res <- fmap (parseOnly file) $ T.readFile $ home ++ "/.authinfo"+ return res+ +-- |Gets the given user info out of AuthInfo+getPassword :: T.Text -> T.Text -> IO (Maybe (T.Text, Maybe PortNumber))+getPassword host user =+ fmap (fmap (\(_,_,pass,port) -> (pass,port)) . + find (\(h,u,_,_) -> h == host && u == user))+ readAuthinfo
+ authinfo-hs.cabal view
@@ -0,0 +1,29 @@+-- Initial authinfo-hs.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/++name: authinfo-hs+version: 0.1.0.0+synopsis: Password querying for .authinfo+description: A .authinfo querier+homepage: http://github.com/robgssp/authinfo-hs+license: MIT+license-file: LICENSE+author: Robert Glossop+maintainer: robgssp@gmail.com+-- copyright: +category: System+build-type: Simple+-- extra-source-files: +cabal-version: >=1.10++library+ exposed-modules: System.Authinfo+ -- other-modules: + other-extensions: OverloadedStrings+ build-depends: base >=4.7 && <5, attoparsec >=0.12, text >=1.1, network >=2.6+ -- hs-source-dirs: + default-language: Haskell2010++source-repository head+ type: git+ location: git@github.com:robgssp/authinfo-hs.git