You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.0 KiB

14 years ago
module Network.XMPP.Monad where
import Control.Monad.Trans
import Control.Monad.Trans.State
import Data.Conduit
14 years ago
import Data.Conduit.Text as CT
import Data.Conduit.Binary as CB
14 years ago
import Data.Conduit.List as CL
import Data.XML.Types
import Data.Default
import Data.Text
import System.IO
import Text.XML.Stream.Elements
14 years ago
import Text.XML.Stream.Render as XR
import Text.XML.Stream.Parse
14 years ago
type XMPPMonad a = StateT XMPPState (ResourceT IO) a
data XMPPState = XMPPState
{ conSrc :: BufferedSource IO Event
, conSink :: Sink Event IO ()
, conHandle :: Maybe Handle
, sFeatures :: ServerFeatures
, haveTLS :: Bool
14 years ago
, sHostname :: Text
, username :: Text
, resource :: Text
14 years ago
}
data ServerFeatures = SF
{ stls :: Bool
, stlsRequired :: Bool
, saslMechanisms :: [Text]
, other :: [Element]
} deriving Show
instance Default ServerFeatures where
def = SF
{ stls = False
, stlsRequired = False
, saslMechanisms = []
, other = []
}
push :: Element -> XMPPMonad ()
push x = do
sink <- gets conSink
lift $ CL.sourceList (elementToEvents x) $$ sink
pushOpen :: Element -> XMPPMonad ()
pushOpen x = do
sink <- gets conSink
lift $ CL.sourceList (elementToEvents' x) $$ sink
pulls :: Sink Event IO a -> XMPPMonad a
pulls snk = do
source <- gets conSrc
lift $ source $$ snk
pull :: XMPPMonad Element
pull = do
source <- gets conSrc
pulls elementFromEvents
14 years ago
xmppFromHandle handle hostname username resource f = runResourceT $ do
liftIO $ hSetBuffering handle NoBuffering
src <- bufferSource $ CB.sourceHandle handle $= CT.decode CT.utf8 $= parseText def
let st = XMPPState
src
(XR.renderBytes def =$ CB.sinkHandle handle)
(Just handle)
def
False
hostname
username
resource
runStateT f st