12 changed files with 290 additions and 1 deletions
@ -0,0 +1,9 @@ |
|||||||
|
The MIT License |
||||||
|
|
||||||
|
Copyright (c) 2012 Philipp Balzarek |
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
||||||
@ -0,0 +1,45 @@ |
|||||||
|
{-# LANGUAGE NoMonomorphismRestriction, OverloadedStrings #-} |
||||||
|
module Network.XMPP where |
||||||
|
|
||||||
|
import Control.Monad |
||||||
|
import Control.Monad.Trans.Class |
||||||
|
import Control.Monad.Trans.State |
||||||
|
|
||||||
|
import qualified Data.ByteString as BS |
||||||
|
import Data.Text as Text |
||||||
|
|
||||||
|
import Network |
||||||
|
import Network.XMPP.Concurrent |
||||||
|
import Network.XMPP.Monad |
||||||
|
import Network.XMPP.TLS |
||||||
|
import Network.XMPP.Stream |
||||||
|
import Network.XMPP.SASL |
||||||
|
import Network.XMPP.Types |
||||||
|
import Network.XMPP.Bind |
||||||
|
import Network.XMPP.Session |
||||||
|
|
||||||
|
|
||||||
|
import System.IO |
||||||
|
|
||||||
|
--fromHandle :: Handle -> Text -> Text -> Maybe Text -> Text -> IO ((), XMPPState) |
||||||
|
fromHandle :: Handle -> Text -> Text -> Maybe Text -> Text -> XMPPThread a |
||||||
|
-> IO ((), XMPPState) |
||||||
|
fromHandle handle hostname username resource password a = |
||||||
|
xmppFromHandle handle hostname username resource $ do |
||||||
|
xmppStartStream |
||||||
|
-- this will check whether the server supports tls |
||||||
|
-- on it's own |
||||||
|
xmppStartTLS exampleParams |
||||||
|
xmppSASL password |
||||||
|
xmppBind |
||||||
|
xmppSession |
||||||
|
runThreaded a |
||||||
|
return () |
||||||
|
|
||||||
|
connectXMPP :: HostName -> Text -> Text -> Maybe Text |
||||||
|
-> Text -> XMPPThread a -> IO ((), XMPPState) |
||||||
|
connectXMPP host hostname username resource passwd a = do |
||||||
|
con <- connectTo host (PortNumber 5222) |
||||||
|
hSetBuffering con NoBuffering |
||||||
|
fromHandle con hostname username resource passwd a |
||||||
|
|
||||||
@ -0,0 +1,154 @@ |
|||||||
|
{-# LANGUAGE OverloadedStrings #-} |
||||||
|
|
||||||
|
|
||||||
|
module Network.XMPP.Concurrent |
||||||
|
where |
||||||
|
|
||||||
|
-- import Network.XMPP.Stream |
||||||
|
import Network.XMPP.Types |
||||||
|
|
||||||
|
import Control.Concurrent |
||||||
|
import Control.Concurrent.STM |
||||||
|
import Control.Concurrent.STM.TChan |
||||||
|
import Control.Concurrent.STM.TMVar |
||||||
|
import Control.Monad.IO.Class |
||||||
|
import Control.Monad |
||||||
|
import Control.Monad.Trans.Class |
||||||
|
import Control.Monad.Trans.Reader |
||||||
|
import Control.Monad.Trans.Resource |
||||||
|
import Control.Monad.Trans.State |
||||||
|
|
||||||
|
|
||||||
|
import qualified Data.ByteString as BS |
||||||
|
import Data.Maybe |
||||||
|
import Data.IORef |
||||||
|
|
||||||
|
import Network.XMPP.Types |
||||||
|
import Network.XMPP.Monad |
||||||
|
import Network.XMPP.Marshal |
||||||
|
import Network.XMPP.Pickle |
||||||
|
|
||||||
|
|
||||||
|
import System.IO |
||||||
|
|
||||||
|
import Text.XML.Expat.Format |
||||||
|
import Text.XML.Expat.Pickle |
||||||
|
|
||||||
|
data Thread = Thread { messagesRef :: IORef (Maybe (TChan Message)) |
||||||
|
, presenceRef :: IORef (Maybe (TChan Presence)) |
||||||
|
, mShadow :: TChan Stanza -- the original chan |
||||||
|
, pShadow :: TChan Stanza -- the original chan |
||||||
|
, outCh :: TChan Stanza |
||||||
|
} |
||||||
|
|
||||||
|
type XMPPThread a = ReaderT Thread IO a |
||||||
|
|
||||||
|
-- Two streams: input and output. Threads read from input stream and write to output stream. |
||||||
|
-- | Runs thread in XmppState monad |
||||||
|
-- returns channel of incoming and outgoing stances, respectively |
||||||
|
-- and an Action to stop the Threads and close the connection |
||||||
|
startThreads :: XMPPMonad (TChan Stanza, TChan Stanza, IO ()) |
||||||
|
startThreads = do |
||||||
|
writeLock <- liftIO $ newTMVarIO () |
||||||
|
messagesC <- liftIO newTChanIO |
||||||
|
presenceC <- liftIO newTChanIO |
||||||
|
iqC <- liftIO newTChanIO |
||||||
|
outC <- liftIO newTChanIO |
||||||
|
iqHandlers <- liftIO newTVarIO |
||||||
|
pushBS <- gets sConPush |
||||||
|
lw <- liftIO . forkIO $ loopWrite writeLock pushBS outC |
||||||
|
cp <- liftIO . forkIO $ connPersist pushBS writeLock |
||||||
|
s <- get |
||||||
|
rd <- lift . resourceForkIO . void . flip runStateT s . forever $ do |
||||||
|
s <- pull |
||||||
|
case s of |
||||||
|
SMessage m -> liftIO . atomically $ writeTChan messageC m |
||||||
|
SPresence p -> liftIO . atomically $ writeTChan presenceC p |
||||||
|
SIQ i -> liftIO . atomically $ writeTChan presenceC i |
||||||
|
return (inC, outC, killConnection writeLock [lw, rd, cp]) |
||||||
|
where |
||||||
|
loopWrite writeLock pushBS out' = forever $ do |
||||||
|
next <- liftIO . atomically $ ( takeTMVar writeLock >> readTChan out') |
||||||
|
liftIO . pushBS . formatNode' $ pickleElem stanzaP next |
||||||
|
liftIO . atomically $ putTMVar writeLock () |
||||||
|
iqHandler handlers iqC = forever $ do |
||||||
|
iq <- liftIO . atomically $ readTChan iqC |
||||||
|
|
||||||
|
|
||||||
|
killConnection writeLock threads = liftIO $ do |
||||||
|
atomically $ takeTMVar writeLock |
||||||
|
forM threads killThread |
||||||
|
return() |
||||||
|
|
||||||
|
runThreaded :: XMPPThread a |
||||||
|
-> XMPPMonad ThreadId |
||||||
|
runThreaded a = do |
||||||
|
(inC, outC, stopThreads) <- startThreads |
||||||
|
workerInCh <- liftIO . newIORef $ Just inC |
||||||
|
worker <- liftIO . forkIO $ do |
||||||
|
runReaderT a (Thread workerInCh inC outC) |
||||||
|
return () |
||||||
|
return worker |
||||||
|
|
||||||
|
|
||||||
|
-- | get the inbound stanza channel, duplicate from master if necessary |
||||||
|
-- please note that once duplicated it will keep filling up |
||||||
|
getInChan = do |
||||||
|
inChR <- asks inChRef |
||||||
|
inCh <- liftIO $ readIORef inChR |
||||||
|
case inCh of |
||||||
|
Nothing -> do |
||||||
|
shadow <- asks shadowInCh |
||||||
|
inCh' <- liftIO $ atomically $ dupTChan shadow |
||||||
|
liftIO $ writeIORef inChR (Just inCh') |
||||||
|
return inCh' |
||||||
|
Just inCh -> return inCh |
||||||
|
|
||||||
|
|
||||||
|
-- | Drop the local end of the inbound stanza channel |
||||||
|
-- from our context so it can be GC-ed |
||||||
|
dropInChan :: XMPPThread () |
||||||
|
dropInChan = do |
||||||
|
r <- asks inChRef |
||||||
|
liftIO $ writeIORef r Nothing |
||||||
|
|
||||||
|
|
||||||
|
-- | Read an element from the inbound stanza channel, acquiring a copy |
||||||
|
-- of the channel as necessary |
||||||
|
pullS :: XMPPThread Stanza |
||||||
|
pullS = do |
||||||
|
c <- getInChan |
||||||
|
st <- liftIO $ atomically $ readTChan c |
||||||
|
return st |
||||||
|
|
||||||
|
-- | Send a stanza to the server |
||||||
|
sendS :: Stanza -> XMPPThread () |
||||||
|
sendS a = do |
||||||
|
out <- asks outCh |
||||||
|
liftIO . atomically $ writeTChan out a |
||||||
|
return () |
||||||
|
|
||||||
|
-- | Fork a new thread |
||||||
|
withNewThread :: XMPPThread () -> XMPPThread ThreadId |
||||||
|
withNewThread a = do |
||||||
|
thread <- ask |
||||||
|
inCH' <- liftIO $ newIORef Nothing |
||||||
|
liftIO $ forkIO $ runReaderT a (thread {inChRef = inCH'}) |
||||||
|
|
||||||
|
waitFor :: (Stanza -> Bool) -> XMPPThread Stanza |
||||||
|
waitFor f = do |
||||||
|
s <- pullS |
||||||
|
if (f s) then |
||||||
|
return s |
||||||
|
else do |
||||||
|
waitFor f |
||||||
|
|
||||||
|
connPersist :: (BS.ByteString -> IO ()) -> TMVar () -> IO () |
||||||
|
connPersist pushBS lock = forever $ do |
||||||
|
atomically $ takeTMVar lock |
||||||
|
pushBS " " |
||||||
|
atomically $ putTMVar lock () |
||||||
|
-- putStrLn "<space added>" |
||||||
|
threadDelay 30000000 |
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,32 @@ |
|||||||
|
{-# LANGUAGE OverloadedStrings #-} |
||||||
|
|
||||||
|
module Network.XMPP.Session where |
||||||
|
|
||||||
|
import Control.Monad.Trans.State |
||||||
|
|
||||||
|
import Data.Text as Text |
||||||
|
|
||||||
|
import Network.XMPP.Monad |
||||||
|
import Network.XMPP.Types |
||||||
|
import Network.XMPP.Pickle |
||||||
|
import Network.XMPP.Marshal |
||||||
|
|
||||||
|
import Text.XML.Expat.Pickle |
||||||
|
|
||||||
|
|
||||||
|
sessionIQ :: Stanza |
||||||
|
sessionIQ = SIQ $ IQ Nothing Nothing "sess" Set |
||||||
|
(pickleElem |
||||||
|
(xpElemNs "session" |
||||||
|
"urn:ietf:params:xml:ns:xmpp-session" |
||||||
|
xpUnit |
||||||
|
xpUnit) |
||||||
|
((),()) |
||||||
|
) |
||||||
|
|
||||||
|
xmppSession :: XMPPMonad () |
||||||
|
xmppSession = do |
||||||
|
push $ sessionIQ |
||||||
|
answer <- pull |
||||||
|
let SIQ (IQ Nothing Nothing "sess" Result b) = answer |
||||||
|
return () |
||||||
Loading…
Reference in new issue