2012-11-02 16:59:31 +00:00
|
|
|
{- xmpp client support
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Assistant.XMPP.Client where
|
|
|
|
|
|
|
|
import Assistant.Common
|
|
|
|
import Utility.SRV
|
2012-11-14 23:32:27 +00:00
|
|
|
import Creds
|
2012-11-02 16:59:31 +00:00
|
|
|
|
|
|
|
import Network.Protocol.XMPP
|
|
|
|
import Network
|
|
|
|
import Control.Concurrent
|
|
|
|
import qualified Data.Text as T
|
|
|
|
|
|
|
|
{- Everything we need to know to connect to an XMPP server. -}
|
|
|
|
data XMPPCreds = XMPPCreds
|
|
|
|
{ xmppUsername :: T.Text
|
|
|
|
, xmppPassword :: T.Text
|
|
|
|
, xmppHostname :: HostName
|
|
|
|
, xmppPort :: Int
|
|
|
|
, xmppJID :: T.Text
|
|
|
|
}
|
|
|
|
deriving (Read, Show)
|
|
|
|
|
2013-05-27 18:36:20 +00:00
|
|
|
connectXMPP :: XMPPCreds -> (JID -> XMPP a) -> IO [(HostPort, Either SomeException ())]
|
2012-11-02 16:59:31 +00:00
|
|
|
connectXMPP c a = case parseJID (xmppJID c) of
|
|
|
|
Nothing -> error "bad JID"
|
|
|
|
Just jid -> connectXMPP' jid c a
|
|
|
|
|
|
|
|
{- Do a SRV lookup, but if it fails, fall back to the cached xmppHostname. -}
|
2013-05-27 18:36:20 +00:00
|
|
|
connectXMPP' :: JID -> XMPPCreds -> (JID -> XMPP a) -> IO [(HostPort, Either SomeException ())]
|
unify exception handling into Utility.Exception
Removed old extensible-exceptions, only needed for very old ghc.
Made webdav use Utility.Exception, to work after some changes in DAV's
exception handling.
Removed Annex.Exception. Mostly this was trivial, but note that
tryAnnex is replaced with tryNonAsync and catchAnnex replaced with
catchNonAsync. In theory that could be a behavior change, since the former
caught all exceptions, and the latter don't catch async exceptions.
However, in practice, nothing in the Annex monad uses async exceptions.
Grepping for throwTo and killThread only find stuff in the assistant,
which does not seem related.
Command.Add.undo is changed to accept a SomeException, and things
that use it for rollback now catch non-async exceptions, rather than
only IOExceptions.
2014-08-08 01:55:44 +00:00
|
|
|
connectXMPP' jid c a = reverse <$> (handlesrv =<< lookupSRV srvrecord)
|
2012-11-02 16:59:31 +00:00
|
|
|
where
|
|
|
|
srvrecord = mkSRVTcp "xmpp-client" $
|
|
|
|
T.unpack $ strDomain $ jidDomain jid
|
|
|
|
serverjid = JID Nothing (jidDomain jid) Nothing
|
|
|
|
|
unify exception handling into Utility.Exception
Removed old extensible-exceptions, only needed for very old ghc.
Made webdav use Utility.Exception, to work after some changes in DAV's
exception handling.
Removed Annex.Exception. Mostly this was trivial, but note that
tryAnnex is replaced with tryNonAsync and catchAnnex replaced with
catchNonAsync. In theory that could be a behavior change, since the former
caught all exceptions, and the latter don't catch async exceptions.
However, in practice, nothing in the Annex monad uses async exceptions.
Grepping for throwTo and killThread only find stuff in the assistant,
which does not seem related.
Command.Add.undo is changed to accept a SomeException, and things
that use it for rollback now catch non-async exceptions, rather than
only IOExceptions.
2014-08-08 01:55:44 +00:00
|
|
|
handlesrv [] = do
|
2013-05-27 18:36:20 +00:00
|
|
|
let h = xmppHostname c
|
|
|
|
let p = PortNumber $ fromIntegral $ xmppPort c
|
|
|
|
r <- run h p $ a jid
|
2013-07-21 01:18:55 +00:00
|
|
|
return [r]
|
unify exception handling into Utility.Exception
Removed old extensible-exceptions, only needed for very old ghc.
Made webdav use Utility.Exception, to work after some changes in DAV's
exception handling.
Removed Annex.Exception. Mostly this was trivial, but note that
tryAnnex is replaced with tryNonAsync and catchAnnex replaced with
catchNonAsync. In theory that could be a behavior change, since the former
caught all exceptions, and the latter don't catch async exceptions.
However, in practice, nothing in the Annex monad uses async exceptions.
Grepping for throwTo and killThread only find stuff in the assistant,
which does not seem related.
Command.Add.undo is changed to accept a SomeException, and things
that use it for rollback now catch non-async exceptions, rather than
only IOExceptions.
2014-08-08 01:55:44 +00:00
|
|
|
handlesrv srvs = go [] srvs
|
2013-07-21 01:18:55 +00:00
|
|
|
|
|
|
|
go l [] = return l
|
2013-05-27 18:36:20 +00:00
|
|
|
go l ((h,p):rest) = do
|
2012-11-02 16:59:31 +00:00
|
|
|
{- Try each SRV record in turn, until one connects,
|
|
|
|
- at which point the MVar will be full. -}
|
|
|
|
mv <- newEmptyMVar
|
|
|
|
r <- run h p $ do
|
|
|
|
liftIO $ putMVar mv ()
|
|
|
|
a jid
|
2013-05-27 18:36:20 +00:00
|
|
|
ifM (isEmptyMVar mv)
|
|
|
|
( go (r : l) rest
|
|
|
|
, return (r : l)
|
|
|
|
)
|
2012-11-02 16:59:31 +00:00
|
|
|
|
|
|
|
{- Async exceptions are let through so the XMPP thread can
|
|
|
|
- be killed. -}
|
2013-05-27 18:36:20 +00:00
|
|
|
run h p a' = do
|
|
|
|
r <- tryNonAsync $
|
|
|
|
runClientError (Server serverjid h p) jid
|
|
|
|
(xmppUsername c) (xmppPassword c) (void a')
|
|
|
|
return ((h, p), r)
|
2012-11-02 16:59:31 +00:00
|
|
|
|
|
|
|
{- XMPP runClient, that throws errors rather than returning an Either -}
|
|
|
|
runClientError :: Server -> JID -> T.Text -> T.Text -> XMPP a -> IO a
|
|
|
|
runClientError s j u p x = either (error . show) return =<< runClient s j u p x
|
|
|
|
|
|
|
|
getXMPPCreds :: Annex (Maybe XMPPCreds)
|
2012-11-14 23:32:27 +00:00
|
|
|
getXMPPCreds = parse <$> readCacheCreds xmppCredsFile
|
|
|
|
where
|
|
|
|
parse s = readish =<< s
|
2012-11-02 16:59:31 +00:00
|
|
|
|
|
|
|
setXMPPCreds :: XMPPCreds -> Annex ()
|
2012-11-14 23:32:27 +00:00
|
|
|
setXMPPCreds creds = writeCacheCreds (show creds) xmppCredsFile
|
2012-11-02 16:59:31 +00:00
|
|
|
|
2012-11-14 23:32:27 +00:00
|
|
|
xmppCredsFile :: FilePath
|
|
|
|
xmppCredsFile = "xmpp"
|