{- This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. -} module TeaCleaner.Configuration ( ProgramOptions(..) , Settings(..) , decodeSettingsFile , commandLineInfo , execParser ) where import GHC.Records (HasField(..)) import Data.Text (StrictText) import qualified Toml import Toml ((.=)) import Options.Applicative ( Parser , ParserInfo , (<**>) , execParser , fullDesc , help , helper , info , long , progDesc , switch ) 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 , spamWords :: [StrictText] , mailDomains :: [StrictText] , statistics :: IORef Int } deriving Eq decodeSettingsFile :: FilePath -> IO 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 { liveRun :: Bool } deriving (Eq, Show) commandLineInfo :: ParserInfo ProgramOptions commandLineInfo = info (commandLine <**> helper) $ fullDesc <> progDesc "Helps to detect some spam gitea accounts" commandLine :: Parser ProgramOptions commandLine = ProgramOptions <$> liveRunOption where liveRunOption = switch $ long "live-run" <> help "Purge suspicious users"