module Network.IRC.Fun.Bot.Internal.History
( rememberMsg
, checkEvent
)
where
import Control.Monad (liftM, when)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.RWS (modify)
import Data.Sequence ((|>), Seq, ViewL (..))
import Network.IRC.Fun.Bot.Internal.State
import Network.IRC.Fun.Bot.Internal.Types
import qualified Data.HashMap.Lazy as M
import qualified Data.Sequence as Q
import qualified Data.Sequence.Util as QU
import qualified Network.IRC.Fun.Client.Events as C (Event (..))
rememberMsg :: String
-> String
-> String
-> Bool
-> Session e s ()
rememberMsg chan nick msg action = do
maxlines <- liftM (maybe 0 csHistoryLines . M.lookup chan) getChans
when (maxlines > 0) $ do
h <- getHistory
t <- askTimeGetter >>= liftIO . liftM snd
m <- getMinutes
let hl = HistoryLine
{ hlTime = t
, hlNick = nick
, hlMessage = msg
, hlAction = action
, hlMinute = m
}
shorten s = if Q.length s > maxlines then QU.tail s else s
hls' = case M.lookup chan h of
Just hls -> shorten $ hls |> hl
Nothing -> Q.singleton hl
modify $ \ s -> s { bsHistory = M.insert chan hls' h }
checkEvent :: C.Event -> Maybe (Msg a)
checkEvent (C.ChannelMessage chan nick msg _) =
Just $ MsgHistoryEvent nick chan msg False
checkEvent (C.ChannelAction chan nick msg) =
Just $ MsgHistoryEvent nick chan msg True
checkEvent _ = Nothing