{-# Language OverloadedStrings, TemplateHaskell #-}
{-|
Module      : Client.Message
Description : Messages to be added to buffers
Copyright   : (c) Eric Mertens, 2016
License     : ISC
Maintainer  : emertens@gmail.com

This module provides the type used to track messages just before they
are added to a window.

-}
module Client.Message
  (
  -- * Client message type
    ClientMessage(..)
  , msgNetwork
  , msgBody
  , msgTime

  -- * Message body type
  , MessageBody(..)
  , _IrcBody
  , _ErrorBody
  , _NormalBody

  -- * Client message operations
  , IrcSummary(..)
  , msgSummary
  , summaryActor

  -- * Quit message details
  , QuitKind(..)

  -- * Client message operations
  , msgText
  ) where

import           Control.Lens
import           Data.Text (Text)
import           Data.Time (ZonedTime)
import           Irc.Message
import           Irc.Identifier
import           Irc.UserInfo
import           Irc.Codes

data MessageBody
  = IrcBody    !IrcMsg
  | ErrorBody  {-# UNPACK #-} !Text
  | NormalBody {-# UNPACK #-} !Text

makePrisms ''MessageBody

data ClientMessage = ClientMessage
  { ClientMessage -> Text
_msgNetwork :: !Text
  , ClientMessage -> MessageBody
_msgBody    :: !MessageBody
  , ClientMessage -> ZonedTime
_msgTime    :: !ZonedTime
  }

makeLenses ''ClientMessage

data QuitKind
  = NormalQuit -- ^ User quit
  | MassQuit   -- ^ Mass event like a netsplit
  deriving (QuitKind -> QuitKind -> Bool
(QuitKind -> QuitKind -> Bool)
-> (QuitKind -> QuitKind -> Bool) -> Eq QuitKind
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: QuitKind -> QuitKind -> Bool
$c/= :: QuitKind -> QuitKind -> Bool
== :: QuitKind -> QuitKind -> Bool
$c== :: QuitKind -> QuitKind -> Bool
Eq, Int -> QuitKind -> ShowS
[QuitKind] -> ShowS
QuitKind -> String
(Int -> QuitKind -> ShowS)
-> (QuitKind -> String) -> ([QuitKind] -> ShowS) -> Show QuitKind
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [QuitKind] -> ShowS
$cshowList :: [QuitKind] -> ShowS
show :: QuitKind -> String
$cshow :: QuitKind -> String
showsPrec :: Int -> QuitKind -> ShowS
$cshowsPrec :: Int -> QuitKind -> ShowS
Show)

data IrcSummary
  = JoinSummary {-# UNPACK #-} !Identifier
  | QuitSummary {-# UNPACK #-} !Identifier !QuitKind
  | PartSummary {-# UNPACK #-} !Identifier
  | NickSummary {-# UNPACK #-} !Identifier {-# UNPACK #-} !Identifier
  | ReplySummary {-# UNPACK #-} !ReplyCode
  | ChatSummary {-# UNPACK #-} !UserInfo
  | CtcpSummary {-# UNPACK #-} !Identifier
  | ChngSummary {-# UNPACK #-} !Identifier -- ^ Chghost command
  | AcctSummary {-# UNPACK #-} !Identifier -- ^ Account command
  | NoSummary
  deriving (IrcSummary -> IrcSummary -> Bool
(IrcSummary -> IrcSummary -> Bool)
-> (IrcSummary -> IrcSummary -> Bool) -> Eq IrcSummary
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: IrcSummary -> IrcSummary -> Bool
$c/= :: IrcSummary -> IrcSummary -> Bool
== :: IrcSummary -> IrcSummary -> Bool
$c== :: IrcSummary -> IrcSummary -> Bool
Eq, Int -> IrcSummary -> ShowS
[IrcSummary] -> ShowS
IrcSummary -> String
(Int -> IrcSummary -> ShowS)
-> (IrcSummary -> String)
-> ([IrcSummary] -> ShowS)
-> Show IrcSummary
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [IrcSummary] -> ShowS
$cshowList :: [IrcSummary] -> ShowS
show :: IrcSummary -> String
$cshow :: IrcSummary -> String
showsPrec :: Int -> IrcSummary -> ShowS
$cshowsPrec :: Int -> IrcSummary -> ShowS
Show)


-- | Compute a searchable text representation of the message
msgText :: MessageBody -> Text
msgText :: MessageBody -> Text
msgText (IrcBody    IrcMsg
irc) = IrcMsg -> Text
ircMsgText IrcMsg
irc
msgText (ErrorBody  Text
txt) = Text
txt
msgText (NormalBody Text
txt) = Text
txt


msgSummary :: MessageBody -> IrcSummary
msgSummary :: MessageBody -> IrcSummary
msgSummary (IrcBody    IrcMsg
irc) = IrcMsg -> IrcSummary
ircSummary IrcMsg
irc
msgSummary (ErrorBody  Text
_  ) = IrcSummary
NoSummary
msgSummary (NormalBody Text
_  ) = IrcSummary
NoSummary


ircSummary :: IrcMsg -> IrcSummary
ircSummary :: IrcMsg -> IrcSummary
ircSummary IrcMsg
msg =
  case IrcMsg
msg of
    Join Source
who Identifier
_ Text
_ Text
_  -> Identifier -> IrcSummary
JoinSummary (UserInfo -> Identifier
userNick (Source -> UserInfo
srcUser Source
who))
    Part Source
who Identifier
_ Maybe Text
_    -> Identifier -> IrcSummary
PartSummary (UserInfo -> Identifier
userNick (Source -> UserInfo
srcUser Source
who))
    Quit Source
who Maybe Text
mbTxt  -> Identifier -> QuitKind -> IrcSummary
QuitSummary (UserInfo -> Identifier
userNick (Source -> UserInfo
srcUser Source
who)) (Maybe Text -> QuitKind
quitKind Maybe Text
mbTxt)
    Nick Source
who Identifier
who'   -> Identifier -> Identifier -> IrcSummary
NickSummary (UserInfo -> Identifier
userNick (Source -> UserInfo
srcUser Source
who)) Identifier
who'
    Privmsg Source
who Identifier
_ Text
_ -> UserInfo -> IrcSummary
ChatSummary (Source -> UserInfo
srcUser Source
who)
    Notice Source
who Identifier
_ Text
_  -> UserInfo -> IrcSummary
ChatSummary (Source -> UserInfo
srcUser Source
who)
    Ctcp Source
who Identifier
_ Text
"ACTION" Text
_ -> UserInfo -> IrcSummary
ChatSummary (Source -> UserInfo
srcUser Source
who)
    Ctcp Source
who Identifier
_ Text
_ Text
_ -> Identifier -> IrcSummary
CtcpSummary (UserInfo -> Identifier
userNick (Source -> UserInfo
srcUser Source
who))
    CtcpNotice Source
who Identifier
_ Text
_ Text
_ -> UserInfo -> IrcSummary
ChatSummary (Source -> UserInfo
srcUser Source
who)
    Reply Text
_ ReplyCode
code [Text]
_  -> ReplyCode -> IrcSummary
ReplySummary ReplyCode
code
    Account Source
who Text
_   -> Identifier -> IrcSummary
AcctSummary (UserInfo -> Identifier
userNick (Source -> UserInfo
srcUser Source
who))
    Chghost Source
who Text
_ Text
_ -> Identifier -> IrcSummary
ChngSummary (UserInfo -> Identifier
userNick (Source -> UserInfo
srcUser Source
who))
    IrcMsg
_               -> IrcSummary
NoSummary

quitKind :: Maybe Text -> QuitKind
quitKind :: Maybe Text -> QuitKind
quitKind Maybe Text
mbReason =
  case Maybe Text
mbReason of
    Just Text
"*.net *.split"        -> QuitKind
MassQuit
    Maybe Text
_                           -> QuitKind
NormalQuit

summaryActor :: IrcSummary -> Maybe Identifier
summaryActor :: IrcSummary -> Maybe Identifier
summaryActor IrcSummary
s =
  case IrcSummary
s of
    JoinSummary Identifier
who   -> Identifier -> Maybe Identifier
forall a. a -> Maybe a
Just Identifier
who
    QuitSummary Identifier
who QuitKind
_ -> Identifier -> Maybe Identifier
forall a. a -> Maybe a
Just Identifier
who
    PartSummary Identifier
who   -> Identifier -> Maybe Identifier
forall a. a -> Maybe a
Just Identifier
who
    NickSummary Identifier
who Identifier
_ -> Identifier -> Maybe Identifier
forall a. a -> Maybe a
Just Identifier
who
    ChatSummary UserInfo
who   -> Identifier -> Maybe Identifier
forall a. a -> Maybe a
Just (UserInfo -> Identifier
userNick UserInfo
who)
    CtcpSummary Identifier
who   -> Identifier -> Maybe Identifier
forall a. a -> Maybe a
Just Identifier
who
    AcctSummary Identifier
who   -> Identifier -> Maybe Identifier
forall a. a -> Maybe a
Just Identifier
who
    ChngSummary Identifier
who   -> Identifier -> Maybe Identifier
forall a. a -> Maybe a
Just Identifier
who
    ReplySummary {}   -> Maybe Identifier
forall a. Maybe a
Nothing
    IrcSummary
NoSummary         -> Maybe Identifier
forall a. Maybe a
Nothing