@ -15,12 +15,14 @@ import qualified GHC.IO.Exception as GIE
import Control.Monad.State.Strict
import Control.Monad.State.Strict
import Data.ByteString as BS
import Data.ByteString as BS
import Data.ByteString.Char8 as BSC8
import Data.Conduit
import Data.Conduit
import Data.Conduit.Binary as CB
import Data.Conduit.Binary as CB
import Data.Conduit.Internal as DCI
import Data.Conduit.Internal as DCI
import qualified Data.Conduit.List as CL
import qualified Data.Conduit.List as CL
import Data.IORef
import Data.IORef
import Data.Text ( Text )
import Data.Text ( Text )
import qualified Data.Text as T
import Data.XML.Pickle
import Data.XML.Pickle
import Data.XML.Types
import Data.XML.Types
@ -35,6 +37,9 @@ import Text.XML.Stream.Elements
import Text.XML.Stream.Parse as XP
import Text.XML.Stream.Parse as XP
import Text.XML.Unresolved ( InvalidEventStream ( .. ) )
import Text.XML.Unresolved ( InvalidEventStream ( .. ) )
import System.Log.Logger
import Data.ByteString.Base64
-- Enable/disable debug output
-- Enable/disable debug output
-- This will dump all incoming and outgoing network taffic to the console,
-- This will dump all incoming and outgoing network taffic to the console,
-- prefixed with "in: " and "out: " respectively
-- prefixed with "in: " and "out: " respectively
@ -145,21 +150,25 @@ xmppNoConnection = Connection_
-- updates the XmppConMonad Connection_ state.
-- updates the XmppConMonad Connection_ state.
connectTcpRaw :: HostName -> PortID -> Text -> IO Connection
connectTcpRaw :: HostName -> PortID -> Text -> IO Connection
connectTcpRaw host port hostname = do
connectTcpRaw host port hostname = do
let PortNumber portNumber = port
debugM " Pontarius.Xmpp " $ " Connecting to " ++ host ++ " on port " ++
( show portNumber ) ++ " through the realm " ++ ( T . unpack hostname ) ++ " . "
h <- connectTo host port
h <- connectTo host port
debugM " Pontarius.Xmpp " " Setting NoBuffering mode on handle. "
hSetBuffering h NoBuffering
hSetBuffering h NoBuffering
let eSource = DCI . ResumableSource ( sourceHandle h $= XP . parseBytes def )
let eSource = DCI . ResumableSource ( ( sourceHandle h $= logConduit ) $= XP . parseBytes def )
( return () )
( return () )
let hand = Hand { cSend = if debug
let hand = Hand { cSend = \ d -> do
then \ d -> do
let d64 = encode d
BS . putStrLn ( BS . append " out: " d )
debugM " Pontarius.Xmpp " $ " Sending TCP data: " ++
catchPush $ BS . hPut h d
( BSC8 . unpack d64 ) ++ " . "
else catchPush . BS . hPut h
catchPush $ BS . hPut h d
, cRecv = if debug then
, cRecv = \ n -> do
\ n -> do
d <- BS . hGetSome h n
bs <- BS . hGetSome h n
let d64 = encode d
BS . putStrLn bs
debugM " Pontarius.Xmpp " $ " Received TCP data: " ++
return bs
( BSC8 . unpack d64 ) ++ " . "
else BS . hGetSome h
return d
, cFlush = hFlush h
, cFlush = hFlush h
, cClose = hClose h
, cClose = hClose h
}
}
@ -178,6 +187,13 @@ connectTcpRaw host port hostname = do
, sFrom = Nothing
, sFrom = Nothing
}
}
mkConnection con
mkConnection con
where
logConduit :: Conduit ByteString IO ByteString
logConduit = CL . mapM $ \ d -> do
let d64 = encode d
debugM " Pontarius.Xmpp " $ " Received TCP data: " ++ ( BSC8 . unpack d64 ) ++
" . "
return d
-- Closes the connection and updates the XmppConMonad Connection_ state.
-- Closes the connection and updates the XmppConMonad Connection_ state.