2012-10-26 18:17:09 +00:00
|
|
|
{- git-annex assistant XMPP configuration
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
|
|
|
module Assistant.WebApp.Configurators.XMPP where
|
|
|
|
|
|
|
|
import Assistant.WebApp
|
|
|
|
import Assistant.WebApp.Types
|
|
|
|
import Assistant.WebApp.SideBar
|
2012-10-27 16:25:29 +00:00
|
|
|
import Assistant.Alert
|
|
|
|
import Assistant.DaemonStatus
|
2012-10-26 18:17:09 +00:00
|
|
|
import Utility.Yesod
|
|
|
|
#ifdef WITH_XMPP
|
|
|
|
import Assistant.Common
|
2012-10-26 18:44:36 +00:00
|
|
|
import Assistant.XMPP
|
2012-10-27 04:50:14 +00:00
|
|
|
import Assistant.Pushes
|
2012-10-26 18:17:09 +00:00
|
|
|
import Utility.SRV
|
|
|
|
#endif
|
|
|
|
|
|
|
|
import Yesod
|
|
|
|
#ifdef WITH_XMPP
|
|
|
|
import Network
|
|
|
|
import Network.Protocol.XMPP
|
|
|
|
import Data.Text (Text)
|
|
|
|
import qualified Data.Text as T
|
|
|
|
#endif
|
|
|
|
|
2012-10-27 16:25:29 +00:00
|
|
|
{- Displays an alert suggesting to configure XMPP, with a button. -}
|
|
|
|
xmppNeeded :: Handler ()
|
|
|
|
xmppNeeded = whenM (isNothing <$> runAnnex Nothing getXMPPCreds) $ do
|
2012-10-29 04:15:43 +00:00
|
|
|
dstatus <- getAssistantY daemonStatus
|
2012-10-27 16:25:29 +00:00
|
|
|
urlrender <- getUrlRender
|
|
|
|
void $ liftIO $ addAlert dstatus $ xmppNeededAlert $ AlertButton
|
|
|
|
{ buttonLabel = "Configure a Jabber account"
|
|
|
|
, buttonUrl = urlrender XMPPR
|
|
|
|
, buttonAction = Just $ removeAlert dstatus
|
|
|
|
}
|
|
|
|
|
2012-10-26 18:17:09 +00:00
|
|
|
getXMPPR :: Handler RepHtml
|
|
|
|
#ifdef WITH_XMPP
|
|
|
|
getXMPPR = xmppPage $ do
|
2012-10-26 21:13:30 +00:00
|
|
|
((result, form), enctype) <- lift $ do
|
|
|
|
oldcreds <- runAnnex Nothing getXMPPCreds
|
|
|
|
runFormGet $ renderBootstrap $ xmppAForm $
|
|
|
|
creds2Form <$> oldcreds
|
2012-10-26 18:17:09 +00:00
|
|
|
let showform problem = do
|
|
|
|
let authtoken = webAppFormAuthToken
|
|
|
|
$(widgetFile "configurators/xmpp")
|
|
|
|
case result of
|
|
|
|
FormSuccess f -> maybe (showform True) (lift . storecreds)
|
|
|
|
=<< liftIO (validateForm f)
|
|
|
|
_ -> showform False
|
|
|
|
where
|
2012-10-26 21:13:30 +00:00
|
|
|
storecreds creds = do
|
|
|
|
void $ runAnnex undefined $ setXMPPCreds creds
|
2012-10-29 04:15:43 +00:00
|
|
|
liftIO . notifyRestart =<< getAssistantY pushNotifier
|
2012-10-26 21:13:30 +00:00
|
|
|
redirect ConfigR
|
2012-10-26 18:17:09 +00:00
|
|
|
#else
|
|
|
|
getXMPPR = xmppPage $
|
|
|
|
$(widgetFile "configurators/xmpp/disabled")
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_XMPP
|
|
|
|
|
|
|
|
data XMPPForm = XMPPForm
|
|
|
|
{ formJID :: Text
|
|
|
|
, formPassword :: Text }
|
|
|
|
|
2012-10-26 21:13:30 +00:00
|
|
|
creds2Form :: XMPPCreds -> XMPPForm
|
|
|
|
creds2Form c = XMPPForm (xmppJID c) (xmppPassword c)
|
|
|
|
|
2012-10-26 18:17:09 +00:00
|
|
|
xmppAForm :: (Maybe XMPPForm) -> AForm WebApp WebApp XMPPForm
|
|
|
|
xmppAForm def = XMPPForm
|
|
|
|
<$> areq jidField "Jabber address" (formJID <$> def)
|
|
|
|
<*> areq passwordField "Password" Nothing
|
|
|
|
|
|
|
|
jidField :: Field WebApp WebApp Text
|
|
|
|
jidField = checkBool (isJust . parseJID) bad textField
|
|
|
|
where
|
|
|
|
bad :: Text
|
|
|
|
bad = "This should look like an email address.."
|
|
|
|
|
|
|
|
validateForm :: XMPPForm -> IO (Maybe XMPPCreds)
|
|
|
|
validateForm f = do
|
|
|
|
let jid = fromMaybe (error "bad JID") $ parseJID (formJID f)
|
|
|
|
let domain = T.unpack $ strDomain $ jidDomain jid
|
|
|
|
hostports <- lookupSRV $ mkSRVTcp "xmpp-client" domain
|
|
|
|
let username = fromMaybe "" (strNode <$> jidNode jid)
|
|
|
|
case hostports of
|
|
|
|
((h, PortNumber p):_) -> testXMPP $ XMPPCreds
|
|
|
|
{ xmppUsername = username
|
|
|
|
, xmppPassword = formPassword f
|
|
|
|
, xmppHostname = h
|
|
|
|
, xmppPort = fromIntegral p
|
|
|
|
, xmppJID = formJID f
|
|
|
|
}
|
|
|
|
_ -> testXMPP $ XMPPCreds
|
|
|
|
{ xmppUsername = username
|
|
|
|
, xmppPassword = formPassword f
|
|
|
|
, xmppHostname = T.unpack $ strDomain $ jidDomain jid
|
|
|
|
, xmppPort = 5222
|
|
|
|
, xmppJID = formJID f
|
|
|
|
}
|
|
|
|
|
|
|
|
testXMPP :: XMPPCreds -> IO (Maybe XMPPCreds)
|
|
|
|
testXMPP creds = either (const $ return Nothing)
|
|
|
|
(const $ return $ Just creds)
|
|
|
|
=<< connectXMPP creds (const noop)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
xmppPage :: Widget -> Handler RepHtml
|
|
|
|
xmppPage w = bootstrap (Just Config) $ do
|
|
|
|
sideBarDisplay
|
|
|
|
setTitle "Jabber"
|
|
|
|
w
|