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.
|
|
|
|
-}
|
|
|
|
|
2013-06-03 20:33:05 +00:00
|
|
|
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings, FlexibleContexts #-}
|
2012-10-26 18:17:09 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
|
|
|
module Assistant.WebApp.Configurators.XMPP where
|
|
|
|
|
2012-11-25 04:26:46 +00:00
|
|
|
import Assistant.WebApp.Common
|
2012-11-03 01:13:06 +00:00
|
|
|
import Assistant.WebApp.Notifications
|
|
|
|
import Utility.NotificationBroadcaster
|
2013-03-15 21:52:41 +00:00
|
|
|
import qualified Remote
|
2012-11-03 01:13:06 +00:00
|
|
|
#ifdef WITH_XMPP
|
2012-11-02 16:59:31 +00:00
|
|
|
import Assistant.XMPP.Client
|
2012-11-03 01:13:06 +00:00
|
|
|
import Assistant.XMPP.Buddies
|
2012-11-12 05:48:15 +00:00
|
|
|
import Assistant.Types.Buddies
|
2012-11-03 18:16:17 +00:00
|
|
|
import Assistant.NetMessager
|
2012-11-12 05:48:15 +00:00
|
|
|
import Assistant.Alert
|
|
|
|
import Assistant.DaemonStatus
|
2013-03-15 21:52:41 +00:00
|
|
|
import Assistant.WebApp.RepoList
|
|
|
|
import Assistant.WebApp.Configurators
|
|
|
|
import Assistant.XMPP
|
2012-10-26 18:17:09 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_XMPP
|
|
|
|
import Network.Protocol.XMPP
|
2013-05-27 18:36:20 +00:00
|
|
|
import Network
|
2012-10-26 18:17:09 +00:00
|
|
|
import qualified Data.Text as T
|
|
|
|
#endif
|
|
|
|
|
2013-03-15 21:52:41 +00:00
|
|
|
{- Displays an alert suggesting to configure XMPP. -}
|
2012-10-27 16:25:29 +00:00
|
|
|
xmppNeeded :: Handler ()
|
2012-11-12 05:48:15 +00:00
|
|
|
#ifdef WITH_XMPP
|
2013-03-04 20:36:38 +00:00
|
|
|
xmppNeeded = whenM (isNothing <$> liftAnnex getXMPPCreds) $ do
|
2012-10-27 16:25:29 +00:00
|
|
|
urlrender <- getUrlRender
|
2012-10-30 21:14:26 +00:00
|
|
|
void $ liftAssistant $ do
|
|
|
|
close <- asIO1 removeAlert
|
2012-10-30 19:39:15 +00:00
|
|
|
addAlert $ xmppNeededAlert $ AlertButton
|
|
|
|
{ buttonLabel = "Configure a Jabber account"
|
2013-04-30 19:19:16 +00:00
|
|
|
, buttonUrl = urlrender XMPPConfigR
|
2012-10-30 19:39:15 +00:00
|
|
|
, buttonAction = Just close
|
|
|
|
}
|
2012-11-12 05:48:15 +00:00
|
|
|
#else
|
|
|
|
xmppNeeded = return ()
|
|
|
|
#endif
|
2012-10-27 16:25:29 +00:00
|
|
|
|
2013-03-24 22:55:19 +00:00
|
|
|
{- When appropriate, displays an alert suggesting to configure a cloud repo
|
2013-03-15 21:52:41 +00:00
|
|
|
- to suppliment an XMPP remote. -}
|
2013-03-24 22:55:19 +00:00
|
|
|
checkCloudRepos :: UrlRenderer -> Remote -> Assistant ()
|
2013-03-15 21:52:41 +00:00
|
|
|
#ifdef WITH_XMPP
|
2013-03-24 22:55:19 +00:00
|
|
|
checkCloudRepos urlrenderer r =
|
|
|
|
unlessM (syncingToCloudRemote <$> getDaemonStatus) $ do
|
|
|
|
buddyname <- getBuddyName $ Remote.uuid r
|
2013-04-04 05:48:26 +00:00
|
|
|
button <- mkAlertButton "Add a cloud repository" urlrenderer $
|
|
|
|
NeedCloudRepoR $ Remote.uuid r
|
|
|
|
void $ addAlert $ cloudRepoNeededAlert buddyname button
|
2013-03-15 21:52:41 +00:00
|
|
|
#else
|
2013-03-24 22:55:19 +00:00
|
|
|
checkCloudRepos _ _ = noop
|
2013-03-15 21:52:41 +00:00
|
|
|
#endif
|
|
|
|
|
2013-04-16 23:23:26 +00:00
|
|
|
#ifdef WITH_XMPP
|
2013-03-15 21:52:41 +00:00
|
|
|
{- Returns the name of the friend corresponding to a
|
|
|
|
- repository's UUID, but not if it's our name. -}
|
|
|
|
getBuddyName :: UUID -> Assistant (Maybe String)
|
|
|
|
getBuddyName u = go =<< getclientjid
|
|
|
|
where
|
|
|
|
go Nothing = return Nothing
|
|
|
|
go (Just myjid) = (T.unpack . buddyName <$>)
|
|
|
|
. headMaybe
|
|
|
|
. filter (\j -> baseJID j /= baseJID myjid)
|
|
|
|
. map fst
|
|
|
|
. filter (\(_, r) -> Remote.uuid r == u)
|
|
|
|
<$> getXMPPRemotes
|
|
|
|
getclientjid = maybe Nothing parseJID . xmppClientID
|
|
|
|
<$> getDaemonStatus
|
2013-04-16 23:23:26 +00:00
|
|
|
#endif
|
2013-03-15 21:52:41 +00:00
|
|
|
|
|
|
|
getNeedCloudRepoR :: UUID -> Handler RepHtml
|
|
|
|
#ifdef WITH_XMPP
|
|
|
|
getNeedCloudRepoR for = page "Cloud repository needed" (Just Configuration) $ do
|
2013-03-16 04:12:28 +00:00
|
|
|
buddyname <- liftAssistant $ getBuddyName for
|
2013-03-15 21:52:41 +00:00
|
|
|
$(widgetFile "configurators/xmpp/needcloudrepo")
|
|
|
|
#else
|
2013-04-16 23:23:26 +00:00
|
|
|
getNeedCloudRepoR _ = xmppPage $
|
2013-03-15 21:52:41 +00:00
|
|
|
$(widgetFile "configurators/xmpp/disabled")
|
|
|
|
#endif
|
|
|
|
|
2013-04-30 19:19:16 +00:00
|
|
|
getXMPPConfigR :: Handler RepHtml
|
|
|
|
getXMPPConfigR = postXMPPConfigR
|
|
|
|
|
|
|
|
postXMPPConfigR :: Handler RepHtml
|
|
|
|
postXMPPConfigR = xmppform DashboardR
|
|
|
|
|
|
|
|
getXMPPConfigForPairFriendR :: Handler RepHtml
|
|
|
|
getXMPPConfigForPairFriendR = postXMPPConfigForPairFriendR
|
|
|
|
|
|
|
|
postXMPPConfigForPairFriendR :: Handler RepHtml
|
|
|
|
postXMPPConfigForPairFriendR = xmppform StartXMPPPairFriendR
|
|
|
|
|
|
|
|
getXMPPConfigForPairSelfR :: Handler RepHtml
|
|
|
|
getXMPPConfigForPairSelfR = postXMPPConfigForPairSelfR
|
|
|
|
|
|
|
|
postXMPPConfigForPairSelfR :: Handler RepHtml
|
|
|
|
postXMPPConfigForPairSelfR = xmppform StartXMPPPairSelfR
|
|
|
|
|
|
|
|
xmppform :: Route WebApp -> Handler RepHtml
|
2013-03-09 02:25:23 +00:00
|
|
|
#ifdef WITH_XMPP
|
2013-04-30 19:19:16 +00:00
|
|
|
xmppform next = xmppPage $ do
|
2013-06-03 17:51:54 +00:00
|
|
|
((result, form), enctype) <- liftH $ do
|
2013-03-04 20:36:38 +00:00
|
|
|
oldcreds <- liftAnnex getXMPPCreds
|
2013-03-16 22:48:23 +00:00
|
|
|
runFormPost $ renderBootstrap $ xmppAForm $
|
2012-10-26 21:13:30 +00:00
|
|
|
creds2Form <$> oldcreds
|
2012-11-25 04:38:11 +00:00
|
|
|
let showform problem = $(widgetFile "configurators/xmpp")
|
2012-10-26 18:17:09 +00:00
|
|
|
case result of
|
2013-06-03 17:51:54 +00:00
|
|
|
FormSuccess f -> either (showform . Just) (liftH . storecreds)
|
2012-10-26 18:17:09 +00:00
|
|
|
=<< liftIO (validateForm f)
|
2012-11-13 17:21:09 +00:00
|
|
|
_ -> showform Nothing
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
storecreds creds = do
|
2013-03-04 20:36:38 +00:00
|
|
|
void $ liftAnnex $ setXMPPCreds creds
|
2012-11-03 18:16:17 +00:00
|
|
|
liftAssistant notifyNetMessagerRestart
|
2013-04-30 19:19:16 +00:00
|
|
|
redirect next
|
2013-03-04 20:03:24 +00:00
|
|
|
#else
|
2013-05-02 17:04:49 +00:00
|
|
|
xmppform _ = xmppPage $
|
2013-03-04 20:03:24 +00:00
|
|
|
$(widgetFile "configurators/xmpp/disabled")
|
2012-11-03 01:13:06 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
{- Called by client to get a list of buddies.
|
|
|
|
-
|
|
|
|
- Returns a div, which will be inserted into the calling page.
|
|
|
|
-}
|
|
|
|
getBuddyListR :: NotificationId -> Handler RepHtml
|
|
|
|
getBuddyListR nid = do
|
|
|
|
waitNotifier getBuddyListBroadcaster nid
|
|
|
|
|
2012-11-25 04:26:46 +00:00
|
|
|
p <- widgetToPageContent buddyListDisplay
|
|
|
|
hamletToRepHtml $ [hamlet|^{pageBody p}|]
|
2012-11-03 01:13:06 +00:00
|
|
|
|
|
|
|
buddyListDisplay :: Widget
|
|
|
|
buddyListDisplay = do
|
|
|
|
autoUpdate ident NotifierBuddyListR (10 :: Int) (10 :: Int)
|
|
|
|
#ifdef WITH_XMPP
|
2013-03-16 04:12:28 +00:00
|
|
|
myjid <- liftAssistant $ xmppClientID <$> getDaemonStatus
|
2013-03-15 19:05:02 +00:00
|
|
|
let isself (BuddyKey b) = Just b == myjid
|
2013-03-16 04:12:28 +00:00
|
|
|
buddies <- liftAssistant $ do
|
2013-03-15 21:52:41 +00:00
|
|
|
pairedwith <- map fst <$> getXMPPRemotes
|
2012-11-10 20:35:09 +00:00
|
|
|
catMaybes . map (buddySummary pairedwith)
|
|
|
|
<$> (getBuddyList <<~ buddyList)
|
2012-11-03 01:13:06 +00:00
|
|
|
$(widgetFile "configurators/xmpp/buddylist")
|
2012-11-12 05:48:15 +00:00
|
|
|
#endif
|
2012-11-03 01:13:06 +00:00
|
|
|
where
|
|
|
|
ident = "buddylist"
|
2012-10-26 18:17:09 +00:00
|
|
|
|
|
|
|
#ifdef WITH_XMPP
|
|
|
|
|
2013-03-15 21:52:41 +00:00
|
|
|
getXMPPRemotes :: Assistant [(JID, Remote)]
|
|
|
|
getXMPPRemotes = catMaybes . map pair . filter isXMPPRemote . syncGitRemotes
|
|
|
|
<$> getDaemonStatus
|
|
|
|
where
|
|
|
|
pair r = maybe Nothing (\jid -> Just (jid, r)) $
|
|
|
|
parseJID $ getXMPPClientID r
|
|
|
|
|
2012-10-26 18:17:09 +00:00
|
|
|
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)
|
|
|
|
|
2013-06-03 20:33:05 +00:00
|
|
|
xmppAForm :: (Maybe XMPPForm) -> MkAForm XMPPForm
|
2012-10-26 18:17:09 +00:00
|
|
|
xmppAForm def = XMPPForm
|
|
|
|
<$> areq jidField "Jabber address" (formJID <$> def)
|
|
|
|
<*> areq passwordField "Password" Nothing
|
|
|
|
|
2013-06-03 20:33:05 +00:00
|
|
|
jidField :: MkField Text
|
2012-10-26 18:17:09 +00:00
|
|
|
jidField = checkBool (isJust . parseJID) bad textField
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
bad :: Text
|
|
|
|
bad = "This should look like an email address.."
|
2012-10-26 18:17:09 +00:00
|
|
|
|
2013-05-27 18:36:20 +00:00
|
|
|
validateForm :: XMPPForm -> IO (Either String XMPPCreds)
|
2012-10-26 18:17:09 +00:00
|
|
|
validateForm f = do
|
|
|
|
let jid = fromMaybe (error "bad JID") $ parseJID (formJID f)
|
|
|
|
let username = fromMaybe "" (strNode <$> jidNode jid)
|
2013-05-27 17:45:07 +00:00
|
|
|
testXMPP $ XMPPCreds
|
|
|
|
{ xmppUsername = username
|
|
|
|
, xmppPassword = formPassword f
|
|
|
|
, xmppHostname = T.unpack $ strDomain $ jidDomain jid
|
|
|
|
, xmppPort = 5222
|
|
|
|
, xmppJID = formJID f
|
|
|
|
}
|
2012-10-26 18:17:09 +00:00
|
|
|
|
2013-05-27 18:36:20 +00:00
|
|
|
testXMPP :: XMPPCreds -> IO (Either String XMPPCreds)
|
|
|
|
testXMPP creds = do
|
|
|
|
(good, bad) <- partition (either (const False) (const True) . snd)
|
|
|
|
<$> connectXMPP creds (const noop)
|
|
|
|
case good of
|
|
|
|
(((h, PortNumber p), _):_) -> return $ Right $ creds
|
|
|
|
{ xmppHostname = h
|
|
|
|
, xmppPort = fromIntegral p
|
|
|
|
}
|
|
|
|
(((h, _), _):_) -> return $ Right $ creds
|
|
|
|
{ xmppHostname = h
|
|
|
|
}
|
|
|
|
_ -> return $ Left $ intercalate "; " $ map formatlog bad
|
|
|
|
where
|
|
|
|
formatlog ((h, p), Left e) = "host " ++ h ++ ":" ++ showport p ++ " failed: " ++ show e
|
|
|
|
formatlog _ = ""
|
2012-10-26 18:17:09 +00:00
|
|
|
|
2013-05-27 18:36:20 +00:00
|
|
|
showport (PortNumber n) = show n
|
|
|
|
showport (Service s) = s
|
|
|
|
showport (UnixSocket s) = s
|
2012-10-26 18:17:09 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
xmppPage :: Widget -> Handler RepHtml
|
2012-12-30 03:10:18 +00:00
|
|
|
xmppPage = page "Jabber" (Just Configuration)
|