kazbek/tea-cleaner/TeaCleaner/Configuration.hs

86 lines
2.3 KiB
Haskell
Raw Permalink Normal View History

2025-02-24 21:09:23 +01:00
{- 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/. -}
2025-02-18 11:57:18 +01:00
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)
2025-02-22 21:30:29 +01:00
import Data.IORef (IORef, newIORef)
2025-02-18 11:57:18 +01:00
data ConfigFile = ConfigFile
{ token :: StrictText
, server :: StrictText
2025-02-22 21:30:29 +01:00
, spamWords :: [StrictText]
, mailDomains :: [StrictText]
2025-02-18 11:57:18 +01:00
} deriving (Eq, Show)
configFileCodec :: Toml.TomlCodec ConfigFile
configFileCodec = ConfigFile
<$> Toml.text "token" .= getField @"token"
<*> Toml.text "server" .= getField @"server"
2025-02-22 21:30:29 +01:00
<*> Toml.arrayOf Toml._Text "spam_words" .= getField @"spamWords"
<*> Toml.arrayOf Toml._Text "mail_domains" .= getField @"mailDomains"
2025-02-18 11:57:18 +01:00
data Settings = Settings
{ token :: StrictText
, server :: URI
, now :: UTCTime
2025-02-22 21:30:29 +01:00
, spamWords :: [StrictText]
, mailDomains :: [StrictText]
, statistics :: IORef Int
} deriving Eq
2025-02-18 11:57:18 +01:00
decodeSettingsFile :: FilePath -> IO Settings
2025-02-22 21:30:29 +01:00
decodeSettingsFile configPath = do
ConfigFile{..} <- Toml.decodeFile configFileCodec configPath
parsedServer <- URI.mkURI server
now <- getCurrentTime
ioRef <- newIORef 0
pure $ Settings
2025-02-18 11:57:18 +01:00
{ token = token
, server = parsedServer
, now = now
2025-02-22 21:30:29 +01:00
, spamWords = spamWords
, mailDomains = mailDomains
, statistics = ioRef
2025-02-18 11:57:18 +01:00
}
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"