{-# LANGUAGE OverloadedStrings #-}
module Network.Xmpp.Extras.MUC
( MUCHistoryReq(..)
, joinMUC
, sendMUC
) where
import Data.Default
import Data.Maybe
import Data.Text as T
import Data.Time
import Data.XML.Types
import Network.Xmpp.Extras.DateTime
import Network.Xmpp.Internal hiding (priority, status)
data MUCHistoryReq = MUCHistoryReq
{ mhrMaxChars :: Maybe Integer
, mhrMaxStanzas :: Maybe Integer
, mhrSeconds :: Maybe Integer
, mhrSince :: Maybe UTCTime
}
instance Default MUCHistoryReq where
def = MUCHistoryReq Nothing Nothing Nothing Nothing
joinMUC :: Jid -> Maybe MUCHistoryReq -> Session -> IO (Either XmppFailure ())
joinMUC jid mhr = sendPresence ((presTo presence jid) { presencePayload = [Element "x" [("xmlns", [ContentText "http://jabber.org/protocol/muc"])] $ maybe [] (\hr -> [
NodeElement $ Element "history" (
(elementify "maxchars" show $ mhrSeconds hr) ++
(elementify "maxstanzas" show $ mhrSeconds hr) ++
(elementify "seconds" show $ mhrSeconds hr) ++
(elementify "since" toDateTime $ mhrSince hr)
) []]) mhr
] } )
where elementify name show content = fmap (\s -> ("seconds", [ContentText $ T.pack $ show s])) $ maybeToList content
sendMUC :: Jid -> Text -> Session -> IO (Either XmppFailure ())
sendMUC jid text = sendMessage ((simpleIM jid text) { messageType = GroupChat })