2012-11-02 16:59:31 +00:00
|
|
|
{- core xmpp support
|
2012-10-26 18:44:36 +00:00
|
|
|
-
|
2013-05-21 22:24:29 +00:00
|
|
|
- Copyright 2012-2013 Joey Hess <joey@kitenet.net>
|
2012-10-26 18:44:36 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2012-11-07 20:59:18 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2012-10-26 18:44:36 +00:00
|
|
|
module Assistant.XMPP where
|
|
|
|
|
2012-11-03 18:16:17 +00:00
|
|
|
import Assistant.Common
|
2012-11-03 20:00:38 +00:00
|
|
|
import Assistant.Types.NetMessager
|
2012-11-03 18:16:17 +00:00
|
|
|
import Assistant.Pairing
|
2013-05-21 22:24:29 +00:00
|
|
|
import Git.Sha (extractSha)
|
2014-02-19 05:09:17 +00:00
|
|
|
import Git
|
2012-10-26 18:44:36 +00:00
|
|
|
|
2012-11-07 20:59:18 +00:00
|
|
|
import Network.Protocol.XMPP hiding (Node)
|
2012-11-05 19:40:56 +00:00
|
|
|
import Data.Text (Text)
|
2012-10-26 18:44:36 +00:00
|
|
|
import qualified Data.Text as T
|
2012-11-10 17:00:13 +00:00
|
|
|
import qualified Data.Map as M
|
2012-11-07 20:59:18 +00:00
|
|
|
import Data.ByteString (ByteString)
|
|
|
|
import qualified Data.ByteString as B
|
2012-10-26 18:44:36 +00:00
|
|
|
import Data.XML.Types
|
2013-11-11 19:42:06 +00:00
|
|
|
import qualified "dataenc" Codec.Binary.Base64 as B64
|
2012-10-26 18:44:36 +00:00
|
|
|
|
2012-11-05 19:40:56 +00:00
|
|
|
{- Name of the git-annex tag, in our own XML namespace.
|
2012-10-26 18:44:36 +00:00
|
|
|
- (Not using a namespace URL to avoid unnecessary bloat.) -}
|
|
|
|
gitAnnexTagName :: Name
|
2012-11-07 20:59:18 +00:00
|
|
|
gitAnnexTagName = "{git-annex}git-annex"
|
2012-10-26 18:44:36 +00:00
|
|
|
|
2012-11-05 19:40:56 +00:00
|
|
|
{- Creates a git-annex tag containing a particular attribute and value. -}
|
|
|
|
gitAnnexTag :: Name -> Text -> Element
|
2012-11-07 20:59:18 +00:00
|
|
|
gitAnnexTag attr val = gitAnnexTagContent attr val []
|
|
|
|
|
|
|
|
{- Also with some content. -}
|
|
|
|
gitAnnexTagContent :: Name -> Text -> [Node] -> Element
|
|
|
|
gitAnnexTagContent attr val = Element gitAnnexTagName [(attr, [ContentText val])]
|
2012-11-05 19:40:56 +00:00
|
|
|
|
2012-11-03 18:16:17 +00:00
|
|
|
isGitAnnexTag :: Element -> Bool
|
|
|
|
isGitAnnexTag t = elementName t == gitAnnexTagName
|
|
|
|
|
2012-11-05 19:40:56 +00:00
|
|
|
{- Things that a git-annex tag can inserted into. -}
|
|
|
|
class GitAnnexTaggable a where
|
|
|
|
insertGitAnnexTag :: a -> Element -> a
|
2012-10-28 21:07:29 +00:00
|
|
|
|
2012-11-05 19:40:56 +00:00
|
|
|
extractGitAnnexTag :: a -> Maybe Element
|
2012-11-03 18:16:17 +00:00
|
|
|
|
2012-11-05 19:40:56 +00:00
|
|
|
hasGitAnnexTag :: a -> Bool
|
|
|
|
hasGitAnnexTag = isJust . extractGitAnnexTag
|
2012-10-26 18:44:36 +00:00
|
|
|
|
2012-11-05 19:40:56 +00:00
|
|
|
instance GitAnnexTaggable Message where
|
2012-11-07 19:47:30 +00:00
|
|
|
insertGitAnnexTag m elt = m { messagePayloads = elt : messagePayloads m }
|
2012-11-05 19:40:56 +00:00
|
|
|
extractGitAnnexTag = headMaybe . filter isGitAnnexTag . messagePayloads
|
2012-11-04 01:19:59 +00:00
|
|
|
|
2012-11-05 19:40:56 +00:00
|
|
|
instance GitAnnexTaggable Presence where
|
2013-03-16 19:37:23 +00:00
|
|
|
-- always mark extended away and set presence priority to negative
|
2012-11-10 19:33:12 +00:00
|
|
|
insertGitAnnexTag p elt = p
|
2013-03-16 19:37:23 +00:00
|
|
|
{ presencePayloads = extendedAway : negativePriority : elt : presencePayloads p }
|
2012-11-05 19:40:56 +00:00
|
|
|
extractGitAnnexTag = headMaybe . filter isGitAnnexTag . presencePayloads
|
2012-11-03 18:16:17 +00:00
|
|
|
|
2012-11-10 06:35:54 +00:00
|
|
|
data GitAnnexTagInfo = GitAnnexTagInfo
|
|
|
|
{ tagAttr :: Name
|
|
|
|
, tagValue :: Text
|
|
|
|
, tagElement :: Element
|
|
|
|
}
|
|
|
|
|
2012-11-10 18:01:24 +00:00
|
|
|
type Decoder = Message -> GitAnnexTagInfo -> Maybe NetMessage
|
|
|
|
|
2012-11-10 06:35:54 +00:00
|
|
|
gitAnnexTagInfo :: GitAnnexTaggable a => a -> Maybe GitAnnexTagInfo
|
|
|
|
gitAnnexTagInfo v = case extractGitAnnexTag v of
|
|
|
|
{- Each git-annex tag has a single attribute. -}
|
|
|
|
Just (tag@(Element _ [(attr, _)] _)) -> GitAnnexTagInfo
|
|
|
|
<$> pure attr
|
|
|
|
<*> attributeText attr tag
|
|
|
|
<*> pure tag
|
2012-11-05 19:40:56 +00:00
|
|
|
_ -> Nothing
|
2012-11-04 01:19:59 +00:00
|
|
|
|
2013-07-25 18:58:30 +00:00
|
|
|
{- A presence with a git-annex tag in it.
|
|
|
|
- Also includes a status tag, which may be visible in XMPP clients. -}
|
2012-11-05 19:40:56 +00:00
|
|
|
gitAnnexPresence :: Element -> Presence
|
2013-07-25 18:58:30 +00:00
|
|
|
gitAnnexPresence = insertGitAnnexTag $ addStatusTag $ emptyPresence PresenceAvailable
|
2013-07-23 16:41:28 +00:00
|
|
|
where
|
|
|
|
addStatusTag p = p
|
|
|
|
{ presencePayloads = status : presencePayloads p }
|
|
|
|
status = Element "status" [] [statusMessage]
|
|
|
|
statusMessage = NodeContent $ ContentText $ T.pack "git-annex"
|
2012-10-26 18:44:36 +00:00
|
|
|
|
2013-07-25 18:58:30 +00:00
|
|
|
{- A presence with an empty git-annex tag in it, used for letting other
|
|
|
|
- clients know we're around and are a git-annex client. -}
|
|
|
|
gitAnnexSignature :: Presence
|
|
|
|
gitAnnexSignature = gitAnnexPresence $ Element gitAnnexTagName [] []
|
|
|
|
|
2013-05-22 20:32:35 +00:00
|
|
|
{- XMPP client to server ping -}
|
|
|
|
xmppPing :: JID -> IQ
|
|
|
|
xmppPing selfjid = (emptyIQ IQGet)
|
|
|
|
{ iqID = Just "c2s1"
|
|
|
|
, iqFrom = Just selfjid
|
|
|
|
, iqTo = Just $ JID Nothing (jidDomain selfjid) Nothing
|
|
|
|
, iqPayload = Just $ Element xmppPingTagName [] []
|
|
|
|
}
|
|
|
|
|
|
|
|
xmppPingTagName :: Name
|
|
|
|
xmppPingTagName = "{urn:xmpp}ping"
|
|
|
|
|
2012-11-05 19:40:56 +00:00
|
|
|
{- A message with a git-annex tag in it. -}
|
2012-11-07 20:59:18 +00:00
|
|
|
gitAnnexMessage :: Element -> JID -> JID -> Message
|
|
|
|
gitAnnexMessage elt tojid fromjid = (insertGitAnnexTag silentMessage elt)
|
|
|
|
{ messageTo = Just tojid
|
|
|
|
, messageFrom = Just fromjid
|
|
|
|
}
|
2012-11-03 18:16:17 +00:00
|
|
|
|
2012-11-05 19:40:56 +00:00
|
|
|
{- A notification that we've pushed to some repositories, listing their
|
|
|
|
- UUIDs. -}
|
2012-11-03 18:25:06 +00:00
|
|
|
pushNotification :: [UUID] -> Presence
|
2012-11-05 19:40:56 +00:00
|
|
|
pushNotification = gitAnnexPresence . gitAnnexTag pushAttr . encodePushNotification
|
|
|
|
|
|
|
|
encodePushNotification :: [UUID] -> Text
|
|
|
|
encodePushNotification = T.intercalate uuidSep . map (T.pack . fromUUID)
|
|
|
|
|
|
|
|
decodePushNotification :: Text -> [UUID]
|
|
|
|
decodePushNotification = map (toUUID . T.unpack) . T.splitOn uuidSep
|
2012-11-03 18:25:06 +00:00
|
|
|
|
2012-11-10 17:00:13 +00:00
|
|
|
uuidSep :: Text
|
|
|
|
uuidSep = ","
|
|
|
|
|
2012-11-03 18:16:17 +00:00
|
|
|
{- A request for other git-annex clients to send presence. -}
|
|
|
|
presenceQuery :: Presence
|
2012-11-05 19:40:56 +00:00
|
|
|
presenceQuery = gitAnnexPresence $ gitAnnexTag queryAttr T.empty
|
|
|
|
|
|
|
|
{- A notification about a stage of pairing. -}
|
|
|
|
pairingNotification :: PairStage -> UUID -> JID -> JID -> Message
|
2012-11-07 20:59:18 +00:00
|
|
|
pairingNotification pairstage u = gitAnnexMessage $
|
|
|
|
gitAnnexTag pairAttr $ encodePairingNotification pairstage u
|
2012-11-03 18:16:17 +00:00
|
|
|
|
2012-11-05 19:40:56 +00:00
|
|
|
encodePairingNotification :: PairStage -> UUID -> Text
|
|
|
|
encodePairingNotification pairstage u = T.unwords $ map T.pack
|
2012-11-04 02:52:41 +00:00
|
|
|
[ show pairstage
|
|
|
|
, fromUUID u
|
|
|
|
]
|
|
|
|
|
2012-11-10 17:00:13 +00:00
|
|
|
decodePairingNotification :: Decoder
|
2012-11-10 06:35:54 +00:00
|
|
|
decodePairingNotification m = parse . words . T.unpack . tagValue
|
2012-11-03 20:00:38 +00:00
|
|
|
where
|
2012-11-04 02:52:41 +00:00
|
|
|
parse [stage, u] = PairingNotification
|
|
|
|
<$> readish stage
|
2012-11-08 18:02:37 +00:00
|
|
|
<*> (formatJID <$> messageFrom m)
|
2012-11-04 02:52:41 +00:00
|
|
|
<*> pure (toUUID u)
|
2012-11-04 01:19:59 +00:00
|
|
|
parse _ = Nothing
|
2012-11-04 02:52:41 +00:00
|
|
|
|
2012-11-10 18:01:24 +00:00
|
|
|
pushMessage :: PushStage -> JID -> JID -> Message
|
|
|
|
pushMessage = gitAnnexMessage . encode
|
2012-11-07 20:59:18 +00:00
|
|
|
where
|
2013-05-21 22:24:29 +00:00
|
|
|
encode (CanPush u shas) =
|
|
|
|
gitAnnexTag canPushAttr $ T.pack $ unwords $
|
2014-02-19 05:09:17 +00:00
|
|
|
fromUUID u : map fromRef shas
|
2013-04-30 17:22:55 +00:00
|
|
|
encode (PushRequest u) =
|
|
|
|
gitAnnexTag pushRequestAttr $ T.pack $ fromUUID u
|
|
|
|
encode (StartingPush u) =
|
|
|
|
gitAnnexTag startingPushAttr $ T.pack $ fromUUID u
|
2013-04-10 22:39:56 +00:00
|
|
|
encode (ReceivePackOutput n b) =
|
|
|
|
gitAnnexTagContent receivePackAttr (val n) $ encodeTagContent b
|
|
|
|
encode (SendPackOutput n b) =
|
|
|
|
gitAnnexTagContent sendPackAttr (val n) $ encodeTagContent b
|
2012-11-10 18:01:24 +00:00
|
|
|
encode (ReceivePackDone code) =
|
2013-04-10 22:39:56 +00:00
|
|
|
gitAnnexTag receivePackDoneAttr $ val $ encodeExitCode code
|
|
|
|
val = T.pack . show
|
2012-11-07 20:59:18 +00:00
|
|
|
|
2012-11-10 17:00:13 +00:00
|
|
|
decodeMessage :: Message -> Maybe NetMessage
|
|
|
|
decodeMessage m = decode =<< gitAnnexTagInfo m
|
2012-11-07 20:59:18 +00:00
|
|
|
where
|
2012-11-10 17:00:13 +00:00
|
|
|
decode i = M.lookup (tagAttr i) decoders >>= rundecoder i
|
|
|
|
rundecoder i d = d m i
|
|
|
|
decoders = M.fromList $ zip
|
|
|
|
[ pairAttr
|
|
|
|
, canPushAttr
|
|
|
|
, pushRequestAttr
|
|
|
|
, startingPushAttr
|
|
|
|
, receivePackAttr
|
|
|
|
, sendPackAttr
|
|
|
|
, receivePackDoneAttr
|
|
|
|
]
|
|
|
|
[ decodePairingNotification
|
2013-05-21 22:24:29 +00:00
|
|
|
, pushdecoder $ shasgen CanPush
|
2013-04-30 17:22:55 +00:00
|
|
|
, pushdecoder $ gen PushRequest
|
|
|
|
, pushdecoder $ gen StartingPush
|
|
|
|
, pushdecoder $ seqgen ReceivePackOutput
|
|
|
|
, pushdecoder $ seqgen SendPackOutput
|
2012-11-10 18:01:24 +00:00
|
|
|
, pushdecoder $
|
|
|
|
fmap (ReceivePackDone . decodeExitCode) . readish .
|
2012-11-10 17:00:13 +00:00
|
|
|
T.unpack . tagValue
|
|
|
|
]
|
2012-11-10 18:38:50 +00:00
|
|
|
pushdecoder a m' i = Pushing
|
|
|
|
<$> (formatJID <$> messageFrom m')
|
2012-11-10 18:01:24 +00:00
|
|
|
<*> a i
|
2013-05-21 22:24:29 +00:00
|
|
|
gen c i = c . toUUID <$> headMaybe (words (T.unpack (tagValue i)))
|
2013-04-30 17:22:55 +00:00
|
|
|
seqgen c i = do
|
2013-04-10 22:39:56 +00:00
|
|
|
packet <- decodeTagContent $ tagElement i
|
2013-04-13 19:46:24 +00:00
|
|
|
let seqnum = fromMaybe 0 $ readish $ T.unpack $ tagValue i
|
|
|
|
return $ c seqnum packet
|
2013-05-21 22:24:29 +00:00
|
|
|
shasgen c i = do
|
|
|
|
let (u:shas) = words $ T.unpack $ tagValue i
|
|
|
|
return $ c (toUUID u) (mapMaybe extractSha shas)
|
2012-11-10 18:01:24 +00:00
|
|
|
|
|
|
|
decodeExitCode :: Int -> ExitCode
|
|
|
|
decodeExitCode 0 = ExitSuccess
|
|
|
|
decodeExitCode n = ExitFailure n
|
|
|
|
|
|
|
|
encodeExitCode :: ExitCode -> Int
|
|
|
|
encodeExitCode ExitSuccess = 0
|
|
|
|
encodeExitCode (ExitFailure n) = n
|
2012-11-07 20:59:18 +00:00
|
|
|
|
|
|
|
{- Base 64 encoding a ByteString to use as the content of a tag. -}
|
|
|
|
encodeTagContent :: ByteString -> [Node]
|
|
|
|
encodeTagContent b = [NodeContent $ ContentText $ T.pack $ B64.encode $ B.unpack b]
|
|
|
|
|
|
|
|
decodeTagContent :: Element -> Maybe ByteString
|
|
|
|
decodeTagContent elt = B.pack <$> B64.decode s
|
|
|
|
where
|
|
|
|
s = T.unpack $ T.concat $ elementText elt
|
|
|
|
|
2012-11-04 02:52:41 +00:00
|
|
|
{- The JID without the client part. -}
|
|
|
|
baseJID :: JID -> JID
|
|
|
|
baseJID j = JID (jidNode j) (jidDomain j) Nothing
|
2012-11-05 19:40:56 +00:00
|
|
|
|
|
|
|
{- An XMPP chat message with an empty body. This should not be displayed
|
|
|
|
- by clients, but can be used for communications. -}
|
|
|
|
silentMessage :: Message
|
|
|
|
silentMessage = (emptyMessage MessageChat)
|
|
|
|
{ messagePayloads = [ emptybody ] }
|
|
|
|
where
|
|
|
|
emptybody = Element
|
2012-11-07 20:59:18 +00:00
|
|
|
{ elementName = "body"
|
2012-11-05 19:40:56 +00:00
|
|
|
, elementAttributes = []
|
|
|
|
, elementNodes = []
|
|
|
|
}
|
|
|
|
|
2013-03-16 19:37:23 +00:00
|
|
|
{- Add to a presence to mark its client as extended away. -}
|
|
|
|
extendedAway :: Element
|
|
|
|
extendedAway = Element "show" [] [NodeContent $ ContentText "xa"]
|
|
|
|
|
2012-11-10 19:33:12 +00:00
|
|
|
{- Add to a presence to give it a negative priority. -}
|
|
|
|
negativePriority :: Element
|
|
|
|
negativePriority = Element "priority" [] [NodeContent $ ContentText "-1"]
|
|
|
|
|
2012-11-10 17:00:13 +00:00
|
|
|
pushAttr :: Name
|
|
|
|
pushAttr = "push"
|
|
|
|
|
|
|
|
queryAttr :: Name
|
|
|
|
queryAttr = "query"
|
|
|
|
|
|
|
|
pairAttr :: Name
|
|
|
|
pairAttr = "pair"
|
|
|
|
|
|
|
|
canPushAttr :: Name
|
|
|
|
canPushAttr = "canpush"
|
|
|
|
|
|
|
|
pushRequestAttr :: Name
|
|
|
|
pushRequestAttr = "pushrequest"
|
|
|
|
|
|
|
|
startingPushAttr :: Name
|
|
|
|
startingPushAttr = "startingpush"
|
|
|
|
|
|
|
|
receivePackAttr :: Name
|
|
|
|
receivePackAttr = "rp"
|
|
|
|
|
|
|
|
sendPackAttr :: Name
|
|
|
|
sendPackAttr = "sp"
|
|
|
|
|
|
|
|
receivePackDoneAttr :: Name
|
|
|
|
receivePackDoneAttr = "rpdone"
|
2013-05-21 22:24:29 +00:00
|
|
|
|
|
|
|
shasAttr :: Name
|
|
|
|
shasAttr = "shas"
|