tea-cleaner: Configure word lists

This commit is contained in:
2025-02-22 21:30:29 +01:00
parent a4c56fb432
commit 2dd3856389
4 changed files with 78 additions and 36 deletions

View File

@ -26,35 +26,44 @@ import Options.Applicative
import Text.URI (URI)
import qualified Text.URI as URI
import Data.Time (UTCTime(..), getCurrentTime)
import Data.IORef (IORef, newIORef)
data ConfigFile = ConfigFile
{ token :: StrictText
, server :: StrictText
, spamWords :: [StrictText]
, mailDomains :: [StrictText]
} deriving (Eq, Show)
configFileCodec :: Toml.TomlCodec ConfigFile
configFileCodec = ConfigFile
<$> Toml.text "token" .= getField @"token"
<*> Toml.text "server" .= getField @"server"
<*> Toml.arrayOf Toml._Text "spam_words" .= getField @"spamWords"
<*> Toml.arrayOf Toml._Text "mail_domains" .= getField @"mailDomains"
data Settings = Settings
{ token :: StrictText
, server :: URI
, now :: UTCTime
} deriving (Eq, Show)
, spamWords :: [StrictText]
, mailDomains :: [StrictText]
, statistics :: IORef Int
} deriving Eq
decodeSettingsFile :: FilePath -> IO Settings
decodeSettingsFile configPath = Toml.decodeFile configFileCodec configPath
>>= withConfiguration
where
withConfiguration configFile@ConfigFile{ server } = URI.mkURI server
>>= withServer configFile
withServer configFile parsedServer = getCurrentTime
>>= withTime configFile parsedServer
withTime ConfigFile{..} parsedServer now = pure $ Settings
decodeSettingsFile configPath = do
ConfigFile{..} <- Toml.decodeFile configFileCodec configPath
parsedServer <- URI.mkURI server
now <- getCurrentTime
ioRef <- newIORef 0
pure $ Settings
{ token = token
, server = parsedServer
, now = now
, spamWords = spamWords
, mailDomains = mailDomains
, statistics = ioRef
}
newtype ProgramOptions = ProgramOptions