{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE MultiWayIf #-}
module Matterhorn.Config
  ( Config(..)
  , PasswordSource(..)
  , findConfig
  , defaultConfig
  , configConnectionType
  )
where

import           Prelude ()
import           Matterhorn.Prelude

import           Control.Monad.Trans.Except
import           Control.Monad.Trans.Class ( lift )
import           Data.Char ( isDigit, isAlpha )
import           Data.List ( isPrefixOf )
import           Data.List.Split ( splitOn )
import qualified Data.Map.Strict as M
import qualified Data.Text as T
import qualified Data.Text.IO as T
import           System.Directory ( makeAbsolute, getHomeDirectory )
import           System.Environment ( getExecutablePath )
import           System.FilePath ( (</>), takeDirectory, splitPath, joinPath )
import           System.Process ( readProcess )
import           Network.Mattermost.Types (ConnectionType(..))
import           Network.URI ( isIPv4address, isIPv6address )

import           Matterhorn.Config.Schema
import           Matterhorn.FilePaths
import           Matterhorn.IOUtil
import           Matterhorn.Types
import           Matterhorn.Types.KeyEvents


defaultPort :: Int
defaultPort :: Int
defaultPort = Int
443

bundledSyntaxPlaceholderName :: String
bundledSyntaxPlaceholderName :: String
bundledSyntaxPlaceholderName = String
"BUNDLED_SYNTAX"

userSyntaxPlaceholderName :: String
userSyntaxPlaceholderName :: String
userSyntaxPlaceholderName = String
"USER_SYNTAX"

defaultSkylightingPaths :: IO [FilePath]
defaultSkylightingPaths :: IO [String]
defaultSkylightingPaths = do
    String
xdg <- IO String
xdgSyntaxDir
    String
adjacent <- IO String
getBundledSyntaxPath
    [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return [String
xdg, String
adjacent]

getBundledSyntaxPath :: IO FilePath
getBundledSyntaxPath :: IO String
getBundledSyntaxPath = do
    String
selfPath <- IO String
getExecutablePath
    let distDir :: String
distDir = String
"dist-newstyle/"
        pathBits :: [String]
pathBits = String -> [String]
splitPath String
selfPath

    String -> IO String
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> IO String) -> String -> IO String
forall a b. (a -> b) -> a -> b
$ if String
distDir String -> [String] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
pathBits
             then
                 -- We're in development, so use the development
                 -- executable path to locate the XML path in the
                 -- development tree.
                 ([String] -> String
joinPath ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (String -> String -> Bool
forall a. Eq a => a -> a -> Bool
/= String
distDir) [String]
pathBits) String -> String -> String
</> String
syntaxDirName
             else
                 -- In this case we assume the binary is being run from
                 -- a release, in which case the syntax directory is a
                 -- sibling of the executable path.
                 String -> String
takeDirectory String
selfPath String -> String -> String
</> String
syntaxDirName

fromIni :: IniParser Config
fromIni :: IniParser Config
fromIni = do
  Config
conf <- Text -> SectionParser Config -> IniParser Config
forall a. Text -> SectionParser a -> IniParser a
section Text
"mattermost" (SectionParser Config -> IniParser Config)
-> SectionParser Config -> IniParser Config
forall a b. (a -> b) -> a -> b
$ do
    Maybe Text
configUser           <- Text -> (Text -> Either String Text) -> SectionParser (Maybe Text)
forall a.
Text -> (Text -> Either String a) -> SectionParser (Maybe a)
fieldMbOf Text
"user" Text -> Either String Text
forall e. Text -> Either e Text
stringField
    Maybe Text
configHost           <- Text -> (Text -> Either String Text) -> SectionParser (Maybe Text)
forall a.
Text -> (Text -> Either String a) -> SectionParser (Maybe a)
fieldMbOf Text
"host" Text -> Either String Text
hostField
    Maybe Text
configTeam           <- Text -> (Text -> Either String Text) -> SectionParser (Maybe Text)
forall a.
Text -> (Text -> Either String a) -> SectionParser (Maybe a)
fieldMbOf Text
"team" Text -> Either String Text
forall e. Text -> Either e Text
stringField
    Int
configPort           <- Text -> (Text -> Either String Int) -> Int -> SectionParser Int
forall a. Text -> (Text -> Either String a) -> a -> SectionParser a
fieldDefOf Text
"port" Text -> Either String Int
forall a. (Num a, Read a, Typeable a) => Text -> Either String a
number (Config -> Int
configPort Config
defaultConfig)
    Maybe Text
configUrlPath        <- Text -> (Text -> Either String Text) -> SectionParser (Maybe Text)
forall a.
Text -> (Text -> Either String a) -> SectionParser (Maybe a)
fieldMbOf Text
"urlPath" Text -> Either String Text
forall e. Text -> Either e Text
stringField
    Int
configChannelListWidth <- Text -> (Text -> Either String Int) -> Int -> SectionParser Int
forall a. Text -> (Text -> Either String a) -> a -> SectionParser a
fieldDefOf Text
"channelListWidth" Text -> Either String Int
forall a. (Num a, Read a, Typeable a) => Text -> Either String a
number
                              (Config -> Int
configChannelListWidth Config
defaultConfig)
    CPUUsagePolicy
configCpuUsagePolicy <- Text
-> (Text -> Either String CPUUsagePolicy)
-> CPUUsagePolicy
-> SectionParser CPUUsagePolicy
forall a. Text -> (Text -> Either String a) -> a -> SectionParser a
fieldDefOf Text
"cpuUsagePolicy" Text -> Either String CPUUsagePolicy
cpuUsagePolicy
                            (Config -> CPUUsagePolicy
configCpuUsagePolicy Config
defaultConfig)
    Int
configLogMaxBufferSize <- Text -> (Text -> Either String Int) -> Int -> SectionParser Int
forall a. Text -> (Text -> Either String a) -> a -> SectionParser a
fieldDefOf Text
"logMaxBufferSize" Text -> Either String Int
forall a. (Num a, Read a, Typeable a) => Text -> Either String a
number
                              (Config -> Int
configLogMaxBufferSize Config
defaultConfig)
    Maybe Text
configTimeFormat     <- Text -> (Text -> Either String Text) -> SectionParser (Maybe Text)
forall a.
Text -> (Text -> Either String a) -> SectionParser (Maybe a)
fieldMbOf Text
"timeFormat" Text -> Either String Text
forall e. Text -> Either e Text
stringField
    Maybe Text
configDateFormat     <- Text -> (Text -> Either String Text) -> SectionParser (Maybe Text)
forall a.
Text -> (Text -> Either String a) -> SectionParser (Maybe a)
fieldMbOf Text
"dateFormat" Text -> Either String Text
forall e. Text -> Either e Text
stringField
    Maybe Text
configTheme          <- Text -> (Text -> Either String Text) -> SectionParser (Maybe Text)
forall a.
Text -> (Text -> Either String a) -> SectionParser (Maybe a)
fieldMbOf Text
"theme" Text -> Either String Text
forall e. Text -> Either e Text
stringField
    Maybe Text
configThemeCustomizationFile <- Text -> (Text -> Either String Text) -> SectionParser (Maybe Text)
forall a.
Text -> (Text -> Either String a) -> SectionParser (Maybe a)
fieldMbOf Text
"themeCustomizationFile" Text -> Either String Text
forall e. Text -> Either e Text
stringField
    Maybe Text
configAspellDictionary <- Text -> (Text -> Either String Text) -> SectionParser (Maybe Text)
forall a.
Text -> (Text -> Either String a) -> SectionParser (Maybe a)
fieldMbOf Text
"aspellDictionary" Text -> Either String Text
forall e. Text -> Either e Text
stringField
    Maybe Text
configURLOpenCommand <- Text -> (Text -> Either String Text) -> SectionParser (Maybe Text)
forall a.
Text -> (Text -> Either String a) -> SectionParser (Maybe a)
fieldMbOf Text
"urlOpenCommand" Text -> Either String Text
forall e. Text -> Either e Text
stringField
    Bool
configURLOpenCommandInteractive <- Text -> Bool -> SectionParser Bool
fieldFlagDef Text
"urlOpenCommandIsInteractive" Bool
False
    Bool
configSmartBacktick  <- Text -> Bool -> SectionParser Bool
fieldFlagDef Text
"smartbacktick"
      (Config -> Bool
configSmartBacktick Config
defaultConfig)
    Bool
configSmartEditing <- Text -> Bool -> SectionParser Bool
fieldFlagDef Text
"smartediting"
      (Config -> Bool
configSmartEditing Config
defaultConfig)
    Bool
configShowOlderEdits <- Text -> Bool -> SectionParser Bool
fieldFlagDef Text
"showOlderEdits"
      (Config -> Bool
configShowOlderEdits Config
defaultConfig)
    BackgroundInfo
configShowBackground <- Text
-> (Text -> Either String BackgroundInfo)
-> BackgroundInfo
-> SectionParser BackgroundInfo
forall a. Text -> (Text -> Either String a) -> a -> SectionParser a
fieldDefOf Text
"showBackgroundActivity" Text -> Either String BackgroundInfo
backgroundField
      (Config -> BackgroundInfo
configShowBackground Config
defaultConfig)
    Bool
configShowMessagePreview <- Text -> Bool -> SectionParser Bool
fieldFlagDef Text
"showMessagePreview"
      (Config -> Bool
configShowMessagePreview Config
defaultConfig)
    Bool
configShowChannelList <- Text -> Bool -> SectionParser Bool
fieldFlagDef Text
"showChannelList"
      (Config -> Bool
configShowChannelList Config
defaultConfig)
    Bool
configShowExpandedChannelTopics <- Text -> Bool -> SectionParser Bool
fieldFlagDef Text
"showExpandedChannelTopics"
      (Config -> Bool
configShowExpandedChannelTopics Config
defaultConfig)
    Bool
configShowTypingIndicator <- Text -> Bool -> SectionParser Bool
fieldFlagDef Text
"showTypingIndicator"
      (Config -> Bool
configShowTypingIndicator Config
defaultConfig)
    Bool
configEnableAspell <- Text -> Bool -> SectionParser Bool
fieldFlagDef Text
"enableAspell"
      (Config -> Bool
configEnableAspell Config
defaultConfig)
    [String]
configSyntaxDirs <- Text
-> (Text -> Either String [String])
-> [String]
-> SectionParser [String]
forall a. Text -> (Text -> Either String a) -> a -> SectionParser a
fieldDefOf Text
"syntaxDirectories" Text -> Either String [String]
syntaxDirsField []
    Maybe Text
configActivityNotifyCommand <- Text -> SectionParser (Maybe Text)
fieldMb Text
"activityNotifyCommand"
    Bool
configShowMessageTimestamps <- Text -> Bool -> SectionParser Bool
fieldFlagDef Text
"showMessageTimestamps"
      (Config -> Bool
configShowMessageTimestamps Config
defaultConfig)
    Bool
configActivityBell <- Text -> Bool -> SectionParser Bool
fieldFlagDef Text
"activityBell"
      (Config -> Bool
configActivityBell Config
defaultConfig)
    Bool
configHyperlinkingMode <- Text -> Bool -> SectionParser Bool
fieldFlagDef Text
"hyperlinkURLs"
      (Config -> Bool
configHyperlinkingMode Config
defaultConfig)
    Maybe PasswordSource
configPass <- (PasswordSource -> Maybe PasswordSource
forall a. a -> Maybe a
Just (PasswordSource -> Maybe PasswordSource)
-> (Text -> PasswordSource) -> Text -> Maybe PasswordSource
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> PasswordSource
PasswordCommand (Text -> Maybe PasswordSource)
-> Parser IniSection IniValue Text
-> Parser IniSection IniValue (Maybe PasswordSource)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Parser IniSection IniValue Text
field Text
"passcmd") Parser IniSection IniValue (Maybe PasswordSource)
-> Parser IniSection IniValue (Maybe PasswordSource)
-> Parser IniSection IniValue (Maybe PasswordSource)
forall e t a. Parser e t a -> Parser e t a -> Parser e t a
<!>
                  (PasswordSource -> Maybe PasswordSource
forall a. a -> Maybe a
Just (PasswordSource -> Maybe PasswordSource)
-> (Text -> PasswordSource) -> Text -> Maybe PasswordSource
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> PasswordSource
PasswordString  (Text -> Maybe PasswordSource)
-> Parser IniSection IniValue Text
-> Parser IniSection IniValue (Maybe PasswordSource)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Parser IniSection IniValue Text
field Text
"pass") Parser IniSection IniValue (Maybe PasswordSource)
-> Parser IniSection IniValue (Maybe PasswordSource)
-> Parser IniSection IniValue (Maybe PasswordSource)
forall e t a. Parser e t a -> Parser e t a -> Parser e t a
<!>
                  Maybe PasswordSource
-> Parser IniSection IniValue (Maybe PasswordSource)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe PasswordSource
forall a. Maybe a
Nothing
    ChannelListOrientation
configChannelListOrientation <- Text
-> (Text -> Either String ChannelListOrientation)
-> ChannelListOrientation
-> SectionParser ChannelListOrientation
forall a. Text -> (Text -> Either String a) -> a -> SectionParser a
fieldDefOf Text
"channelListOrientation"
        Text -> Either String ChannelListOrientation
channelListOrientationField
        (Config -> ChannelListOrientation
configChannelListOrientation Config
defaultConfig)
    Maybe TokenSource
configToken <- (TokenSource -> Maybe TokenSource
forall a. a -> Maybe a
Just (TokenSource -> Maybe TokenSource)
-> (Text -> TokenSource) -> Text -> Maybe TokenSource
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> TokenSource
TokenCommand  (Text -> Maybe TokenSource)
-> Parser IniSection IniValue Text
-> Parser IniSection IniValue (Maybe TokenSource)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Parser IniSection IniValue Text
field Text
"tokencmd") Parser IniSection IniValue (Maybe TokenSource)
-> Parser IniSection IniValue (Maybe TokenSource)
-> Parser IniSection IniValue (Maybe TokenSource)
forall e t a. Parser e t a -> Parser e t a -> Parser e t a
<!>
                  Maybe TokenSource -> Parser IniSection IniValue (Maybe TokenSource)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe TokenSource
forall a. Maybe a
Nothing
    Bool
configUnsafeUseHTTP <-
      Text -> Bool -> SectionParser Bool
fieldFlagDef Text
"unsafeUseUnauthenticatedConnection" Bool
False
    Bool
configValidateServerCertificate <-
      Text -> Bool -> SectionParser Bool
fieldFlagDef Text
"validateServerCertificate" Bool
True
    Int
configDirectChannelExpirationDays <- Text -> (Text -> Either String Int) -> Int -> SectionParser Int
forall a. Text -> (Text -> Either String a) -> a -> SectionParser a
fieldDefOf Text
"directChannelExpirationDays" Text -> Either String Int
forall a. (Num a, Read a, Typeable a) => Text -> Either String a
number
      (Config -> Int
configDirectChannelExpirationDays Config
defaultConfig)
    Maybe String
configDefaultAttachmentPath <- Text
-> (Text -> Either String String) -> SectionParser (Maybe String)
forall a.
Text -> (Text -> Either String a) -> SectionParser (Maybe a)
fieldMbOf Text
"defaultAttachmentPath" Text -> Either String String
forall e. Text -> Either e String
filePathField

    let configAbsPath :: Maybe a
configAbsPath = Maybe a
forall a. Maybe a
Nothing
        configUserKeys :: KeyConfig
configUserKeys = KeyConfig
forall a. Monoid a => a
mempty
    Config -> SectionParser Config
forall (m :: * -> *) a. Monad m => a -> m a
return Config :: Maybe Text
-> Maybe Text
-> Maybe Text
-> Int
-> Maybe Text
-> Maybe PasswordSource
-> Maybe TokenSource
-> Maybe Text
-> Maybe Text
-> Maybe Text
-> Maybe Text
-> Bool
-> Bool
-> Maybe Text
-> Bool
-> Maybe Text
-> Bool
-> Bool
-> BackgroundInfo
-> Bool
-> Bool
-> Bool
-> Bool
-> Maybe Text
-> Bool
-> Bool
-> Int
-> Int
-> Bool
-> Bool
-> Maybe String
-> KeyConfig
-> Bool
-> [String]
-> Int
-> CPUUsagePolicy
-> Maybe String
-> ChannelListOrientation
-> Config
Config { Bool
Int
[String]
Maybe String
Maybe Text
Maybe TokenSource
Maybe PasswordSource
KeyConfig
ChannelListOrientation
BackgroundInfo
CPUUsagePolicy
forall a. Maybe a
configDefaultAttachmentPath :: Maybe String
configSyntaxDirs :: [String]
configUserKeys :: KeyConfig
configAbsPath :: Maybe String
configValidateServerCertificate :: Bool
configUnsafeUseHTTP :: Bool
configAspellDictionary :: Maybe Text
configActivityNotifyCommand :: Maybe Text
configURLOpenCommandInteractive :: Bool
configURLOpenCommand :: Maybe Text
configThemeCustomizationFile :: Maybe Text
configTheme :: Maybe Text
configDateFormat :: Maybe Text
configTimeFormat :: Maybe Text
configToken :: Maybe TokenSource
configPass :: Maybe PasswordSource
configUrlPath :: Maybe Text
configTeam :: Maybe Text
configHost :: Maybe Text
configUser :: Maybe Text
configUserKeys :: KeyConfig
configAbsPath :: forall a. Maybe a
configDefaultAttachmentPath :: Maybe String
configDirectChannelExpirationDays :: Int
configDirectChannelExpirationDays :: Int
configValidateServerCertificate :: Bool
configUnsafeUseHTTP :: Bool
configToken :: Maybe TokenSource
configChannelListOrientation :: ChannelListOrientation
configChannelListOrientation :: ChannelListOrientation
configPass :: Maybe PasswordSource
configHyperlinkingMode :: Bool
configHyperlinkingMode :: Bool
configActivityBell :: Bool
configActivityBell :: Bool
configShowMessageTimestamps :: Bool
configShowMessageTimestamps :: Bool
configActivityNotifyCommand :: Maybe Text
configSyntaxDirs :: [String]
configEnableAspell :: Bool
configEnableAspell :: Bool
configShowTypingIndicator :: Bool
configShowTypingIndicator :: Bool
configShowExpandedChannelTopics :: Bool
configShowExpandedChannelTopics :: Bool
configShowChannelList :: Bool
configShowChannelList :: Bool
configShowMessagePreview :: Bool
configShowMessagePreview :: Bool
configShowBackground :: BackgroundInfo
configShowBackground :: BackgroundInfo
configShowOlderEdits :: Bool
configShowOlderEdits :: Bool
configSmartEditing :: Bool
configSmartEditing :: Bool
configSmartBacktick :: Bool
configSmartBacktick :: Bool
configURLOpenCommandInteractive :: Bool
configURLOpenCommand :: Maybe Text
configAspellDictionary :: Maybe Text
configThemeCustomizationFile :: Maybe Text
configTheme :: Maybe Text
configDateFormat :: Maybe Text
configTimeFormat :: Maybe Text
configLogMaxBufferSize :: Int
configLogMaxBufferSize :: Int
configCpuUsagePolicy :: CPUUsagePolicy
configCpuUsagePolicy :: CPUUsagePolicy
configChannelListWidth :: Int
configChannelListWidth :: Int
configUrlPath :: Maybe Text
configPort :: Int
configPort :: Int
configTeam :: Maybe Text
configHost :: Maybe Text
configUser :: Maybe Text
.. }
  Maybe KeyConfig
keys <- Text -> SectionParser KeyConfig -> IniParser (Maybe KeyConfig)
forall a. Text -> SectionParser a -> IniParser (Maybe a)
sectionMb Text
"keybindings" (SectionParser KeyConfig -> IniParser (Maybe KeyConfig))
-> SectionParser KeyConfig -> IniParser (Maybe KeyConfig)
forall a b. (a -> b) -> a -> b
$ do
      ([Maybe (KeyEvent, BindingState)] -> KeyConfig)
-> Parser IniSection IniValue [Maybe (KeyEvent, BindingState)]
-> SectionParser KeyConfig
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([(KeyEvent, BindingState)] -> KeyConfig
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([(KeyEvent, BindingState)] -> KeyConfig)
-> ([Maybe (KeyEvent, BindingState)] -> [(KeyEvent, BindingState)])
-> [Maybe (KeyEvent, BindingState)]
-> KeyConfig
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe (KeyEvent, BindingState)] -> [(KeyEvent, BindingState)]
forall a. [Maybe a] -> [a]
catMaybes) (Parser IniSection IniValue [Maybe (KeyEvent, BindingState)]
 -> SectionParser KeyConfig)
-> Parser IniSection IniValue [Maybe (KeyEvent, BindingState)]
-> SectionParser KeyConfig
forall a b. (a -> b) -> a -> b
$ [KeyEvent]
-> (KeyEvent
    -> Parser IniSection IniValue (Maybe (KeyEvent, BindingState)))
-> Parser IniSection IniValue [Maybe (KeyEvent, BindingState)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [KeyEvent]
allEvents ((KeyEvent
  -> Parser IniSection IniValue (Maybe (KeyEvent, BindingState)))
 -> Parser IniSection IniValue [Maybe (KeyEvent, BindingState)])
-> (KeyEvent
    -> Parser IniSection IniValue (Maybe (KeyEvent, BindingState)))
-> Parser IniSection IniValue [Maybe (KeyEvent, BindingState)]
forall a b. (a -> b) -> a -> b
$ \ KeyEvent
ev -> do
          Maybe BindingState
kb <- Text
-> (Text -> Either String BindingState)
-> SectionParser (Maybe BindingState)
forall a.
Text -> (Text -> Either String a) -> SectionParser (Maybe a)
fieldMbOf (KeyEvent -> Text
keyEventName KeyEvent
ev) Text -> Either String BindingState
parseBindingList
          case Maybe BindingState
kb of
              Maybe BindingState
Nothing      -> Maybe (KeyEvent, BindingState)
-> Parser IniSection IniValue (Maybe (KeyEvent, BindingState))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (KeyEvent, BindingState)
forall a. Maybe a
Nothing
              Just BindingState
binding -> Maybe (KeyEvent, BindingState)
-> Parser IniSection IniValue (Maybe (KeyEvent, BindingState))
forall (m :: * -> *) a. Monad m => a -> m a
return ((KeyEvent, BindingState) -> Maybe (KeyEvent, BindingState)
forall a. a -> Maybe a
Just (KeyEvent
ev, BindingState
binding))
  Config -> IniParser Config
forall (m :: * -> *) a. Monad m => a -> m a
return Config
conf { configUserKeys :: KeyConfig
configUserKeys = KeyConfig -> Maybe KeyConfig -> KeyConfig
forall a. a -> Maybe a -> a
fromMaybe KeyConfig
forall a. Monoid a => a
mempty Maybe KeyConfig
keys }

channelListOrientationField :: Text -> Either String ChannelListOrientation
channelListOrientationField :: Text -> Either String ChannelListOrientation
channelListOrientationField Text
t =
    case Text -> Text
T.toLower Text
t of
        Text
"left" -> ChannelListOrientation -> Either String ChannelListOrientation
forall (m :: * -> *) a. Monad m => a -> m a
return ChannelListOrientation
ChannelListLeft
        Text
"right" -> ChannelListOrientation -> Either String ChannelListOrientation
forall (m :: * -> *) a. Monad m => a -> m a
return ChannelListOrientation
ChannelListRight
        Text
_ -> String -> Either String ChannelListOrientation
forall a b. a -> Either a b
Left (String -> Either String ChannelListOrientation)
-> String -> Either String ChannelListOrientation
forall a b. (a -> b) -> a -> b
$ String
"Invalid value for channelListOrientation: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall a. Show a => a -> String
show Text
t

syntaxDirsField :: Text -> Either String [FilePath]
syntaxDirsField :: Text -> Either String [String]
syntaxDirsField = Text
-> (Text -> Either String (Item [String]))
-> Text
-> Either String [String]
forall l.
IsList l =>
Text -> (Text -> Either String (Item l)) -> Text -> Either String l
listWithSeparator Text
":" Text -> Either String (Item [String])
forall a. IsString a => Text -> Either String a
string

validHostnameFragmentChar :: Char -> Bool
validHostnameFragmentChar :: Char -> Bool
validHostnameFragmentChar Char
c = Char -> Bool
isAlpha Char
c Bool -> Bool -> Bool
|| Char -> Bool
isDigit Char
c Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-'

isHostnameFragment :: String -> Bool
isHostnameFragment :: String -> Bool
isHostnameFragment String
"" = Bool
False
isHostnameFragment String
s = (Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
validHostnameFragmentChar String
s

isHostname :: String -> Bool
isHostname :: String -> Bool
isHostname String
"" = Bool
False
isHostname String
s =
    let parts :: [String]
parts@(String
h:[String]
_) = String -> String -> [String]
forall a. Eq a => [a] -> [a] -> [[a]]
splitOn String
"." String
s
    in (String -> Bool) -> [String] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all String -> Bool
isHostnameFragment [String]
parts Bool -> Bool -> Bool
&& Bool -> Bool
not (String
"-" String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` String
h)

hostField :: Text -> Either String Text
hostField :: Text -> Either String Text
hostField Text
t =
    let s :: String
s = Text -> String
T.unpack Text
t
        valid :: Bool
valid = [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or [ String -> Bool
isIPv4address String
s
                   , String -> Bool
isIPv6address String
s
                   , String -> Bool
isHostname String
s
                   ]
    in if Bool
valid
       then Text -> Either String Text
forall a b. b -> Either a b
Right Text
t
       else String -> Either String Text
forall a b. a -> Either a b
Left String
"Invalid 'host' value, must be a hostname or IPv4/IPv6 address"

expandTilde :: FilePath -> FilePath -> FilePath
expandTilde :: String -> String -> String
expandTilde String
homeDir String
p =
    let parts :: [String]
parts = String -> [String]
splitPath String
p
        f :: String -> String
f String
part | String
part String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"~/" = String
homeDir String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/"
               | Bool
otherwise    = String
part
    in [String] -> String
joinPath ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ String -> String
f (String -> String) -> [String] -> [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [String]
parts

backgroundField :: Text -> Either String BackgroundInfo
backgroundField :: Text -> Either String BackgroundInfo
backgroundField Text
t =
    case Text
t of
        Text
"Disabled" -> BackgroundInfo -> Either String BackgroundInfo
forall a b. b -> Either a b
Right BackgroundInfo
Disabled
        Text
"Active" -> BackgroundInfo -> Either String BackgroundInfo
forall a b. b -> Either a b
Right BackgroundInfo
Active
        Text
"ActiveCount" -> BackgroundInfo -> Either String BackgroundInfo
forall a b. b -> Either a b
Right BackgroundInfo
ActiveCount
        Text
_ -> String -> Either String BackgroundInfo
forall a b. a -> Either a b
Left (String
"Invalid value " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall a. Show a => a -> String
show Text
t
                  String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"; must be one of: Disabled, Active, ActiveCount")

cpuUsagePolicy :: Text -> Either String CPUUsagePolicy
cpuUsagePolicy :: Text -> Either String CPUUsagePolicy
cpuUsagePolicy Text
t =
    case Text -> Text
T.toLower Text
t of
        Text
"single" -> CPUUsagePolicy -> Either String CPUUsagePolicy
forall (m :: * -> *) a. Monad m => a -> m a
return CPUUsagePolicy
SingleCPU
        Text
"multiple" -> CPUUsagePolicy -> Either String CPUUsagePolicy
forall (m :: * -> *) a. Monad m => a -> m a
return CPUUsagePolicy
MultipleCPUs
        Text
_ -> String -> Either String CPUUsagePolicy
forall a b. a -> Either a b
Left (String -> Either String CPUUsagePolicy)
-> String -> Either String CPUUsagePolicy
forall a b. (a -> b) -> a -> b
$ String
"Invalid CPU usage policy value: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall a. Show a => a -> String
show Text
t

stringField :: Text -> Either e Text
stringField :: Text -> Either e Text
stringField Text
t =
    case Text -> Bool
isQuoted Text
t of
        Bool
True -> Text -> Either e Text
forall a b. b -> Either a b
Right (Text -> Either e Text) -> Text -> Either e Text
forall a b. (a -> b) -> a -> b
$ Text -> Text
parseQuotedString Text
t
        Bool
False -> Text -> Either e Text
forall a b. b -> Either a b
Right Text
t

filePathField :: Text -> Either e FilePath
filePathField :: Text -> Either e String
filePathField Text
t = let path :: String
path = Text -> String
T.unpack Text
t in String -> Either e String
forall a b. b -> Either a b
Right String
path

parseQuotedString :: Text -> Text
parseQuotedString :: Text -> Text
parseQuotedString Text
t =
    let body :: Text
body = Int -> Text -> Text
T.drop Int
1 (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Text
T.init Text
t
        unescapeQuotes :: Text -> Text
unescapeQuotes Text
s | Text -> Bool
T.null Text
s = Text
s
                         | Text
"\\\"" Text -> Text -> Bool
`T.isPrefixOf` Text
s = Text
"\"" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
unescapeQuotes (Int -> Text -> Text
T.drop Int
2 Text
s)
                         | Bool
otherwise = (Char -> Text
T.singleton (Char -> Text) -> Char -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Char
T.head Text
s) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
unescapeQuotes (Int -> Text -> Text
T.drop Int
1 Text
s)
    in Text -> Text
unescapeQuotes Text
body

isQuoted :: Text -> Bool
isQuoted :: Text -> Bool
isQuoted Text
t =
    let quote :: Text
quote = Text
"\""
    in (Text
quote Text -> Text -> Bool
`T.isPrefixOf` Text
t) Bool -> Bool -> Bool
&&
       (Text
quote Text -> Text -> Bool
`T.isSuffixOf` Text
t)

defaultConfig :: Config
defaultConfig :: Config
defaultConfig =
    Config :: Maybe Text
-> Maybe Text
-> Maybe Text
-> Int
-> Maybe Text
-> Maybe PasswordSource
-> Maybe TokenSource
-> Maybe Text
-> Maybe Text
-> Maybe Text
-> Maybe Text
-> Bool
-> Bool
-> Maybe Text
-> Bool
-> Maybe Text
-> Bool
-> Bool
-> BackgroundInfo
-> Bool
-> Bool
-> Bool
-> Bool
-> Maybe Text
-> Bool
-> Bool
-> Int
-> Int
-> Bool
-> Bool
-> Maybe String
-> KeyConfig
-> Bool
-> [String]
-> Int
-> CPUUsagePolicy
-> Maybe String
-> ChannelListOrientation
-> Config
Config { configAbsPath :: Maybe String
configAbsPath                     = Maybe String
forall a. Maybe a
Nothing
           , configUser :: Maybe Text
configUser                        = Maybe Text
forall a. Maybe a
Nothing
           , configHost :: Maybe Text
configHost                        = Maybe Text
forall a. Maybe a
Nothing
           , configTeam :: Maybe Text
configTeam                        = Maybe Text
forall a. Maybe a
Nothing
           , configPort :: Int
configPort                        = Int
defaultPort
           , configUrlPath :: Maybe Text
configUrlPath                     = Maybe Text
forall a. Maybe a
Nothing
           , configPass :: Maybe PasswordSource
configPass                        = Maybe PasswordSource
forall a. Maybe a
Nothing
           , configToken :: Maybe TokenSource
configToken                       = Maybe TokenSource
forall a. Maybe a
Nothing
           , configTimeFormat :: Maybe Text
configTimeFormat                  = Maybe Text
forall a. Maybe a
Nothing
           , configDateFormat :: Maybe Text
configDateFormat                  = Maybe Text
forall a. Maybe a
Nothing
           , configTheme :: Maybe Text
configTheme                       = Maybe Text
forall a. Maybe a
Nothing
           , configThemeCustomizationFile :: Maybe Text
configThemeCustomizationFile      = Maybe Text
forall a. Maybe a
Nothing
           , configSmartBacktick :: Bool
configSmartBacktick               = Bool
True
           , configSmartEditing :: Bool
configSmartEditing                = Bool
True
           , configURLOpenCommand :: Maybe Text
configURLOpenCommand              = Maybe Text
forall a. Maybe a
Nothing
           , configURLOpenCommandInteractive :: Bool
configURLOpenCommandInteractive   = Bool
False
           , configActivityNotifyCommand :: Maybe Text
configActivityNotifyCommand       = Maybe Text
forall a. Maybe a
Nothing
           , configActivityBell :: Bool
configActivityBell                = Bool
False
           , configShowMessageTimestamps :: Bool
configShowMessageTimestamps       = Bool
True
           , configShowBackground :: BackgroundInfo
configShowBackground              = BackgroundInfo
Disabled
           , configShowMessagePreview :: Bool
configShowMessagePreview          = Bool
False
           , configShowChannelList :: Bool
configShowChannelList             = Bool
True
           , configShowExpandedChannelTopics :: Bool
configShowExpandedChannelTopics   = Bool
True
           , configEnableAspell :: Bool
configEnableAspell                = Bool
False
           , configAspellDictionary :: Maybe Text
configAspellDictionary            = Maybe Text
forall a. Maybe a
Nothing
           , configUnsafeUseHTTP :: Bool
configUnsafeUseHTTP               = Bool
False
           , configValidateServerCertificate :: Bool
configValidateServerCertificate   = Bool
True
           , configChannelListWidth :: Int
configChannelListWidth            = Int
20
           , configLogMaxBufferSize :: Int
configLogMaxBufferSize            = Int
200
           , configShowOlderEdits :: Bool
configShowOlderEdits              = Bool
True
           , configUserKeys :: KeyConfig
configUserKeys                    = KeyConfig
forall a. Monoid a => a
mempty
           , configShowTypingIndicator :: Bool
configShowTypingIndicator         = Bool
False
           , configHyperlinkingMode :: Bool
configHyperlinkingMode            = Bool
True
           , configSyntaxDirs :: [String]
configSyntaxDirs                  = []
           , configDirectChannelExpirationDays :: Int
configDirectChannelExpirationDays = Int
7
           , configCpuUsagePolicy :: CPUUsagePolicy
configCpuUsagePolicy              = CPUUsagePolicy
MultipleCPUs
           , configDefaultAttachmentPath :: Maybe String
configDefaultAttachmentPath       = Maybe String
forall a. Maybe a
Nothing
           , configChannelListOrientation :: ChannelListOrientation
configChannelListOrientation      = ChannelListOrientation
ChannelListLeft
           }

findConfig :: Maybe FilePath -> IO (Either String ([String], Config))
findConfig :: Maybe String -> IO (Either String ([String], Config))
findConfig Maybe String
Nothing = ExceptT String IO ([String], Config)
-> IO (Either String ([String], Config))
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT String IO ([String], Config)
 -> IO (Either String ([String], Config)))
-> ExceptT String IO ([String], Config)
-> IO (Either String ([String], Config))
forall a b. (a -> b) -> a -> b
$ do
    Maybe String
cfg <- IO (Maybe String) -> ExceptT String IO (Maybe String)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IO (Maybe String) -> ExceptT String IO (Maybe String))
-> IO (Maybe String) -> ExceptT String IO (Maybe String)
forall a b. (a -> b) -> a -> b
$ String -> IO (Maybe String)
locateConfig String
configFileName
    ([String]
warns, Config
config) <-
      case Maybe String
cfg of
        Maybe String
Nothing -> ([String], Config) -> ExceptT String IO ([String], Config)
forall (m :: * -> *) a. Monad m => a -> m a
return ([], Config
defaultConfig)
        Just String
path -> String -> ExceptT String IO ([String], Config)
getConfig String
path
    Config
config' <- Config -> ExceptT String IO Config
fixupPaths Config
config
    ([String], Config) -> ExceptT String IO ([String], Config)
forall (m :: * -> *) a. Monad m => a -> m a
return ([String]
warns, Config
config')
findConfig (Just String
path) =
    ExceptT String IO ([String], Config)
-> IO (Either String ([String], Config))
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT String IO ([String], Config)
 -> IO (Either String ([String], Config)))
-> ExceptT String IO ([String], Config)
-> IO (Either String ([String], Config))
forall a b. (a -> b) -> a -> b
$ do ([String]
warns, Config
config) <- String -> ExceptT String IO ([String], Config)
getConfig String
path
                    Config
config' <- Config -> ExceptT String IO Config
fixupPaths Config
config
                    ([String], Config) -> ExceptT String IO ([String], Config)
forall (m :: * -> *) a. Monad m => a -> m a
return ([String]
warns, Config
config')

-- | Fix path references in the configuration:
--
-- * Rewrite the syntax directory path list with 'fixupSyntaxDirs'
-- * Expand "~" encountered in any setting that contains a path value
fixupPaths :: Config -> ExceptT String IO Config
fixupPaths :: Config -> ExceptT String IO Config
fixupPaths Config
initial = do
    Config
new <- Config -> ExceptT String IO Config
fixupSyntaxDirs Config
initial
    String
homeDir <- IO String -> ExceptT String IO String
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO String
getHomeDirectory
    let fixP :: String -> String
fixP = String -> String -> String
expandTilde String
homeDir
        fixPText :: Text -> Text
fixPText = String -> Text
T.pack (String -> Text) -> (Text -> String) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> String
expandTilde String
homeDir (String -> String) -> (Text -> String) -> Text -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack
    Config -> ExceptT String IO Config
forall (m :: * -> *) a. Monad m => a -> m a
return (Config -> ExceptT String IO Config)
-> Config -> ExceptT String IO Config
forall a b. (a -> b) -> a -> b
$ Config
new { configThemeCustomizationFile :: Maybe Text
configThemeCustomizationFile = Text -> Text
fixPText (Text -> Text) -> Maybe Text -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Config -> Maybe Text
configThemeCustomizationFile Config
new
                 , configSyntaxDirs :: [String]
configSyntaxDirs             = String -> String
fixP (String -> String) -> [String] -> [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Config -> [String]
configSyntaxDirs Config
new
                 , configURLOpenCommand :: Maybe Text
configURLOpenCommand         = Text -> Text
fixPText (Text -> Text) -> Maybe Text -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Config -> Maybe Text
configURLOpenCommand Config
new
                 , configActivityNotifyCommand :: Maybe Text
configActivityNotifyCommand  = Text -> Text
fixPText (Text -> Text) -> Maybe Text -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Config -> Maybe Text
configActivityNotifyCommand Config
new
                 , configDefaultAttachmentPath :: Maybe String
configDefaultAttachmentPath  = String -> String
fixP (String -> String) -> Maybe String -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Config -> Maybe String
configDefaultAttachmentPath Config
new
                 }

-- | If the configuration has no syntax directories specified (the
-- default if the user did not provide the setting), fill in the
-- list with the defaults. Otherwise replace any bundled directory
-- placeholders in the config's syntax path list.
fixupSyntaxDirs :: Config -> ExceptT String IO Config
fixupSyntaxDirs :: Config -> ExceptT String IO Config
fixupSyntaxDirs Config
c =
    if Config -> [String]
configSyntaxDirs Config
c [String] -> [String] -> Bool
forall a. Eq a => a -> a -> Bool
== []
    then do
        [String]
dirs <- IO [String] -> ExceptT String IO [String]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO [String]
defaultSkylightingPaths
        Config -> ExceptT String IO Config
forall (m :: * -> *) a. Monad m => a -> m a
return (Config -> ExceptT String IO Config)
-> Config -> ExceptT String IO Config
forall a b. (a -> b) -> a -> b
$ Config
c { configSyntaxDirs :: [String]
configSyntaxDirs = [String]
dirs }
    else do
        [String]
newDirs <- [String]
-> (String -> ExceptT String IO String)
-> ExceptT String IO [String]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM (Config -> [String]
configSyntaxDirs Config
c) ((String -> ExceptT String IO String)
 -> ExceptT String IO [String])
-> (String -> ExceptT String IO String)
-> ExceptT String IO [String]
forall a b. (a -> b) -> a -> b
$ \String
dir ->
            if | String
dir String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
bundledSyntaxPlaceholderName -> IO String -> ExceptT String IO String
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO String
getBundledSyntaxPath
               | String
dir String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
userSyntaxPlaceholderName    -> IO String -> ExceptT String IO String
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO String
xdgSyntaxDir
               | Bool
otherwise                           -> String -> ExceptT String IO String
forall (m :: * -> *) a. Monad m => a -> m a
return String
dir

        Config -> ExceptT String IO Config
forall (m :: * -> *) a. Monad m => a -> m a
return (Config -> ExceptT String IO Config)
-> Config -> ExceptT String IO Config
forall a b. (a -> b) -> a -> b
$ Config
c { configSyntaxDirs :: [String]
configSyntaxDirs = [String]
newDirs }

getConfig :: FilePath -> ExceptT String IO ([String], Config)
getConfig :: String -> ExceptT String IO ([String], Config)
getConfig String
fp = do
    String
absPath <- IO String -> ExceptT String IO String
forall a. IO a -> ExceptT String IO a
convertIOException (IO String -> ExceptT String IO String)
-> IO String -> ExceptT String IO String
forall a b. (a -> b) -> a -> b
$ String -> IO String
makeAbsolute String
fp
    Text
t <- (IO Text -> ExceptT String IO Text
forall a. IO a -> ExceptT String IO a
convertIOException (IO Text -> ExceptT String IO Text)
-> IO Text -> ExceptT String IO Text
forall a b. (a -> b) -> a -> b
$ String -> IO Text
T.readFile String
absPath) ExceptT String IO Text
-> (String -> ExceptT String IO Text) -> ExceptT String IO Text
forall (m :: * -> *) e a e'.
Monad m =>
ExceptT e m a -> (e -> ExceptT e' m a) -> ExceptT e' m a
`catchE`
         (\String
e -> String -> ExceptT String IO Text
forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
throwE (String -> ExceptT String IO Text)
-> String -> ExceptT String IO Text
forall a b. (a -> b) -> a -> b
$ String
"Could not read " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall a. Show a => a -> String
show String
absPath String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
": " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
e)

    -- HACK ALERT FIXME:
    --
    -- The config parser library we use, config-ini (as of 0.2.4.0)
    -- cannot handle configuration files without trailing newlines.
    -- Since that's not a really good reason for this function to raise
    -- an exception (and is fixable on the fly), we have the following
    -- check. This check is admittedly not a great thing to have to do,
    -- and we should definitely get rid of it when config-ini fixes this
    -- issue.
    let t' :: Text
t' = if Text
"\n" Text -> Text -> Bool
`T.isSuffixOf` Text
t then Text
t else Text
t Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n"

    case Text -> IniParser Config -> Either Fatal ([Warning], Config)
forall a. Text -> IniParser a -> Either Fatal ([Warning], a)
parseIniFile Text
t' IniParser Config
fromIni of
        Left Fatal
err -> do
            String -> ExceptT String IO ([String], Config)
forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
throwE (String -> ExceptT String IO ([String], Config))
-> String -> ExceptT String IO ([String], Config)
forall a b. (a -> b) -> a -> b
$ String
"Unable to parse " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
absPath String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
":" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Fatal -> String
fatalString Fatal
err
        Right ([Warning]
warns, Config
conf) -> do
            Maybe Text
actualPass <- case Config -> Maybe PasswordSource
configPass Config
conf of
                Just (PasswordCommand Text
cmdString) -> do
                    let (String
cmd:[String]
rest) = Text -> String
T.unpack (Text -> String) -> [Text] -> [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> [Text]
T.words Text
cmdString
                    String
output <- IO String -> ExceptT String IO String
forall a. IO a -> ExceptT String IO a
convertIOException (String -> [String] -> String -> IO String
readProcess String
cmd [String]
rest String
"") ExceptT String IO String
-> (String -> ExceptT String IO String) -> ExceptT String IO String
forall (m :: * -> *) e a e'.
Monad m =>
ExceptT e m a -> (e -> ExceptT e' m a) -> ExceptT e' m a
`catchE`
                              (\String
e -> String -> ExceptT String IO String
forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
throwE (String -> ExceptT String IO String)
-> String -> ExceptT String IO String
forall a b. (a -> b) -> a -> b
$ String
"Could not execute password command: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
e)
                    Maybe Text -> ExceptT String IO (Maybe Text)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Text -> ExceptT String IO (Maybe Text))
-> Maybe Text -> ExceptT String IO (Maybe Text)
forall a b. (a -> b) -> a -> b
$ Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text) -> Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack ((Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\n') String
output)
                Just (PasswordString Text
pass) -> Maybe Text -> ExceptT String IO (Maybe Text)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Text -> ExceptT String IO (Maybe Text))
-> Maybe Text -> ExceptT String IO (Maybe Text)
forall a b. (a -> b) -> a -> b
$ Text -> Maybe Text
forall a. a -> Maybe a
Just Text
pass
                Maybe PasswordSource
Nothing -> Maybe Text -> ExceptT String IO (Maybe Text)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Text
forall a. Maybe a
Nothing

            Maybe Text
actualToken <- case Config -> Maybe TokenSource
configToken Config
conf of
                Just (TokenCommand Text
cmdString) -> do
                    let (String
cmd:[String]
rest) = Text -> String
T.unpack (Text -> String) -> [Text] -> [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> [Text]
T.words Text
cmdString
                    String
output <- IO String -> ExceptT String IO String
forall a. IO a -> ExceptT String IO a
convertIOException (String -> [String] -> String -> IO String
readProcess String
cmd [String]
rest String
"") ExceptT String IO String
-> (String -> ExceptT String IO String) -> ExceptT String IO String
forall (m :: * -> *) e a e'.
Monad m =>
ExceptT e m a -> (e -> ExceptT e' m a) -> ExceptT e' m a
`catchE`
                              (\String
e -> String -> ExceptT String IO String
forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
throwE (String -> ExceptT String IO String)
-> String -> ExceptT String IO String
forall a b. (a -> b) -> a -> b
$ String
"Could not execute token command: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
e)
                    Maybe Text -> ExceptT String IO (Maybe Text)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Text -> ExceptT String IO (Maybe Text))
-> Maybe Text -> ExceptT String IO (Maybe Text)
forall a b. (a -> b) -> a -> b
$ Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text) -> Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack ((Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\n') String
output)
                Just (TokenString Text
_) -> String -> ExceptT String IO (Maybe Text)
forall a. HasCallStack => String -> a
error (String -> ExceptT String IO (Maybe Text))
-> String -> ExceptT String IO (Maybe Text)
forall a b. (a -> b) -> a -> b
$ String
"BUG: getConfig: token in the Config was already a TokenString"
                Maybe TokenSource
Nothing -> Maybe Text -> ExceptT String IO (Maybe Text)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Text
forall a. Maybe a
Nothing

            let conf' :: Config
conf' = Config
conf
                  { configPass :: Maybe PasswordSource
configPass = Text -> PasswordSource
PasswordString (Text -> PasswordSource) -> Maybe Text -> Maybe PasswordSource
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Text
actualPass
                  , configToken :: Maybe TokenSource
configToken = Text -> TokenSource
TokenString (Text -> TokenSource) -> Maybe Text -> Maybe TokenSource
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Text
actualToken
                  , configAbsPath :: Maybe String
configAbsPath = String -> Maybe String
forall a. a -> Maybe a
Just String
absPath
                  }
            ([String], Config) -> ExceptT String IO ([String], Config)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Warning -> String) -> [Warning] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Warning -> String
warningString [Warning]
warns, Config
conf')

configConnectionType :: Config -> ConnectionType
configConnectionType :: Config -> ConnectionType
configConnectionType Config
config
  | Config -> Bool
configUnsafeUseHTTP Config
config = ConnectionType
ConnectHTTP
  | Bool
otherwise = Bool -> ConnectionType
ConnectHTTPS (Config -> Bool
configValidateServerCertificate Config
config)