gale client

root / GinsuConfig.hs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
{-# LANGUAGE ScopedTypeVariables #-}
module GinsuConfig(
    checkConfigIsGood,
    configureGinsu,
    doCheckConfig,
    getGaleProxy,
    getGaleId,
    getGaleDomain,
    getGaleAliases,
    galeFile,
    getEditor
    ) where

import ConfigFile
import Control.Exception
import Data.Monoid
import System.Directory
import ErrorLog
import ExampleConf
import Gale.Gale
import Gale.Puff
import GenUtil
import Control.Monad
import System.Environment
import qualified System.Posix as Posix

checkConfigIsGood = do
    gd <- configLookup "GALE_DOMAIN"
    case gd of
        Just _ -> return ()
        Nothing -> putStrLn "GALE_DOMAIN is not set! either set $GALE_DOMAIN in the enviornment or set it in ~/.gale/conf or ~/.gale/ginsu.config\n" >> doCheckConfig

configFileG xs = mapConfig ("GINSU_" ++) (configFile xs) `mappend` configFile xs

configureGinsu = do
    galeDir <- getGaleDir
    configSetup $ mconcat
            [mapConfig ("GINSU_" ++) configEnv,
            configFileG (galeDir  ++ "ginsu.config"),
            configEnv,
            configFileG (galeDir ++ "conf"),
            configDefault (parseConfigFile exampleConf),
            configBuilder]

doCheckConfig = do
    galeDir <- getGaleDir


    gp <- getGaleProxy
    gid <- configLookupElse "GALE_ID" "* Not Found *"
    galeDomain <- configLookupElse "GALE_DOMAIN" "* Not Found *"
    galeAliases <- getGaleAliases
    galeSubscribe <- configLookupList "GALE_SUBSCRIBE"


    mapM_ putStrLn $ buildTableLL [
        ( "GALE_DIR ",  galeDir),
        ( "GALE_DOMAIN ", galeDomain),
        ( "GALE_ID ", gid),
        ( "GALE_PROXY ", simpleQuote gp),
        ( "GALE_SUBSCRIBE ", unwords (snub galeSubscribe)) ]
    when (galeAliases /= []) $ putStrLn $ "GALE_ALIASES \n" ++
        unlines ( map  (\ (l,cat) -> "  " ++ l ++ " -> " ++ show cat)  galeAliases)

    let p = (galeDir ++ "/auth/private/")
        knames = [p ++ gid, p ++ gid ++ ".gpri"]
    gn <- (mapM (\fn -> (doesFileExist fn >>= \b -> if b then return (Just fn) else return Nothing)) knames)
    case msum gn of
        Just fn -> putStrLn $ "Private key found in: " ++ fn
        Nothing -> putStrLn $ "Private key for '" ++ gid ++ "' not found in " ++ p
    putStrLn ""
    --putStrLn showKeyInfo
    --kt <- buildKeyTable
    --print kt
    exitSuccess

configBuilder = toConfig cb where
    cb "GALE_ID" = do
        n <- firstIO [getEnv "LOGNAME", getEnv "USER", Posix.getLoginName]
        d <- configLookup "GALE_DOMAIN"
        case d of
            Just d -> return [("<LOGIN>@$GALE_DOMAIN",("GALE_ID", n ++ "@" ++ d))]
            Nothing -> return []
    cb _ = return []

getGaleProxy :: IO [String]
getGaleProxy = do
    gps <- configLookup "GALE_PROXY"
    case gps of
        Nothing -> do
            v <- configLookup "GALE_DOMAIN"
            case v of
                Just v -> return $ hostStrings v
                Nothing -> return []
        (Just x) -> return (words x)


getGaleId :: IO String
getGaleId = do
    v <- configLookup "GALE_ID"
    case v of
        Just x -> return x
        Nothing -> return ""

getGaleDomain :: IO String
getGaleDomain = do
    v <- configLookup "GALE_DOMAIN"
    case v of
        Just x -> return x
        Nothing -> do
            gid <- getGaleId
            return $ drop 1 (dropWhile (/= '@') gid)


getGaleAliases :: IO [(String,Category)]
getGaleAliases = do
    v <- galeFile "aliases/"
    fs <- handle (\(_ :: IOException) -> return []) $ getDirectoryContents v
    --putLog LogDebug $ "aliases/ " ++ show fs
    let f fn = do
        --fc <- first [readFile (v ++ fn), readSymbolicLink (v ++ fn)]
        fc <- readAlias (v ++ fn)
        --putLog LogDebug $ "readAlias " ++ (v ++ fn) ++ " " ++ fc
        return (fn, catParseNew fc)
    tryMapM f fs


readAlias :: String -> IO String
readAlias fn = Posix.getSymbolicLinkStatus fn >>= \s -> if Posix.isRegularFile s then readFile fn else
    (if Posix.isSymbolicLink s then  Posix.readSymbolicLink fn else fail "bad alias")



varsLookupElse :: [String] -> IO String -> IO String
varsLookupElse ss action = do
    v <- fmap concat $ mapM configLookupList ss
    case v of
        (x:_) -> return x
        _ -> action

galeFile :: String -> IO String
galeFile fn = do
    gd <- getGaleDir
    return (gd ++ fn)

getEditor = varsLookupElse ["VISUAL", "EDITOR"] (return "vi")