packages feed

funbot-0.3: src/FunBot/Types.hs

{- This file is part of funbot.
 -
 - Written in 2015 by fr33domlover <fr33domlover@rel4tion.org>.
 -
 - ♡ Copying is an act of love. Please copy, reuse and share.
 -
 - The author(s) have dedicated all copyright and related and neighboring
 - rights to this software to the public domain worldwide. This software is
 - distributed without any warranty.
 -
 - You should have received a copy of the CC0 Public Domain Dedication along
 - with this software. If not, see
 - <http://creativecommons.org/publicdomain/zero/1.0/>.
 -}

module FunBot.Types
    ( Filter (..)
    , BranchFilter
    , PushAnnSpec (..)
    , NewsItemFields (..)
    , NewsAnnSpec (..)
    , BotEnv (..)
    , Settings (..)
    , SettingsOption
    , SettingsTree
    , Memo (..)
    , HistoryLine (..)
    , HistoryDisplay (..)
    , UserOptions (..)
    , BotState (..)
    , BotSession
    , ExtEventSource
    , ExtEventHandler
    )
where

import Data.HashMap.Lazy (HashMap)
import Data.HashSet (HashSet)
import Data.Sequence (Seq)
import Data.Settings.Types (Section (..), Option (..))
import Data.Time.Clock (UTCTime)
import FunBot.ExtEvents (ExtEvent)
import Network.IRC.Fun.Bot.Types (Session, EventSource, EventHandler)

-- | Generic item filter
data Filter a = Accept [a] | Reject [a]

-- | Chooser for repo branches whose commits should be announced to IRC
type BranchFilter = Filter String

-- | Configuration for announcing a git repo's commits to a specific channel
data PushAnnSpec = PushAnnSpec
    { -- | IRC channel into which to announce
      pAnnChannel    :: String
      -- Branch filter to choose which branches to announce
    , pAnnBranches   :: BranchFilter
      -- Whether to report all commits in a push ('True') or shorten long
      -- pushes to avoid channel spam ('False').
    , pAnnAllCommits :: Bool
    }

-- | Pick news item fields to display
data NewsItemFields = NewsItemFields
    { dispFeedTitle :: Bool
    , dispAuthor    :: Bool
    , dispUrl       :: Bool
    }

-- | Configuration for announcing news items
data NewsAnnSpec = NewsAnnSpec
    { -- | IRC channels into which to announce
      nAnnChannels :: [String]
      -- | Filter for picking news item fields to display or hide
    , nAnnFields   :: NewsItemFields
    }

-- | Read-only custom bot environment
data BotEnv = BotEnv
    { -- | Port on which the web hook event source will run
      webHookSourcePort :: Int
      -- | An 'IO' action which schedules saving settings to disk. There is a
      -- wrapper in the 'Session' monad which uses this with the settings
      -- stored in bot state, so you probably don't need this field directly.
    , saveSettings      :: Settings -> IO ()
      -- | Similarly for memos.
    , saveMemos         :: HashMap String [Memo] -> IO ()
      -- | Similarly for user options.
    , saveUserOpts      :: HashMap String UserOptions -> IO ()
      -- | Filename for logging feed listener errors
    , feedErrorLogFile  :: FilePath
    }

-- | User-modifiable bot behavior settings
data Settings = Settings
    { -- | Maps a Git repo name+owner to annoucement details
      gitAnnChans  :: HashMap (String, String) [PushAnnSpec]
    , -- | Maps a feed label to its URL and announcement details
      watchedFeeds :: HashMap String (String, NewsAnnSpec)
    }

-- | Alias for the settings option type
type SettingsOption = Option BotSession

-- | Alias for the settings section type
type SettingsTree = Section BotSession

-- | A message left to an offline user, for them to read later.
data Memo = Memo
    { memoTime    :: String
    , memoSender  :: String
    , memoRecvIn  :: Maybe String
    , memoSendIn  :: Maybe String
    , memoContent :: String
    }

-- | A message sent previously by an IRC user into a channel.
data HistoryLine = HistoryLine
    { hlTime    :: String
    , hlNick    :: String
    , hlMessage :: String
    , hlAction  :: Bool
    }

-- | History display options per channel
data HistoryDisplay = HistoryDisplay
    { -- | Whether channel history should be displayed
      hdEnabled  :: Bool
      -- | Maximal number of messages to show
    , hdMaxLines :: Int
    }

-- | Per-user options, consider private user info
data UserOptions = UserOptions
    { -- | History display options per channel
      uoHistoryDisplay :: HashMap String HistoryDisplay
    }

-- | Read-write custom bot state
data BotState = BotState
    { -- | User-modifiable bot behavior settings
      bsSettings :: Settings
      -- | Settings tree and access definition for UI
    , bsSTree    :: SettingsTree
      -- | Memos waiting for users to connect.
    , bsMemos    :: HashMap String [Memo]
      -- | Per-channel last messages
    , bsHistory  :: HashMap String (Seq HistoryLine)
      -- | Per-user options
    , bsUserOptions :: HashMap String UserOptions
    }

-- | Shortcut alias for bot session monad
type BotSession = Session BotEnv BotState

-- | Shortcut alias for event source function type
type ExtEventSource = EventSource BotEnv BotState ExtEvent

-- | Shortcut alias for event handler function type
type ExtEventHandler = EventHandler BotEnv BotState ExtEvent