2012-11-02 16:59:31 +00:00
|
|
|
{- xmpp buddies
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Assistant.XMPP.Buddies where
|
|
|
|
|
|
|
|
import Assistant.XMPP
|
|
|
|
import Common.Annex
|
2012-11-03 01:13:06 +00:00
|
|
|
import Assistant.Types.Buddies
|
2012-11-02 16:59:31 +00:00
|
|
|
|
|
|
|
import Network.Protocol.XMPP
|
|
|
|
import qualified Data.Map as M
|
|
|
|
import qualified Data.Set as S
|
2012-11-03 01:13:06 +00:00
|
|
|
import Data.Text (Text)
|
|
|
|
import qualified Data.Text as T
|
2012-11-02 16:59:31 +00:00
|
|
|
|
2012-11-05 21:43:17 +00:00
|
|
|
genBuddyKey :: JID -> BuddyKey
|
|
|
|
genBuddyKey j = BuddyKey $ formatJID $ baseJID j
|
2012-11-02 16:59:31 +00:00
|
|
|
|
2012-11-03 20:00:38 +00:00
|
|
|
buddyName :: JID -> Text
|
|
|
|
buddyName j = maybe (T.pack "") strNode (jidNode j)
|
|
|
|
|
2012-11-12 17:27:30 +00:00
|
|
|
ucFirst :: Text -> Text
|
|
|
|
ucFirst s = let (first, rest) = T.splitAt 1 s
|
|
|
|
in T.concat [T.toUpper first, rest]
|
|
|
|
|
2012-11-03 01:13:06 +00:00
|
|
|
{- Summary of info about a buddy.
|
|
|
|
-
|
|
|
|
- If the buddy has no clients at all anymore, returns Nothing. -}
|
2012-11-10 20:35:09 +00:00
|
|
|
buddySummary :: [JID] -> Buddy -> Maybe (Text, Bool, Bool, Bool, BuddyKey)
|
|
|
|
buddySummary pairedwith b = case clients of
|
|
|
|
((Client j):_) -> Just (buddyName j, away, canpair, alreadypaired j, genBuddyKey j)
|
2012-11-03 01:13:06 +00:00
|
|
|
[] -> Nothing
|
|
|
|
where
|
|
|
|
away = S.null (buddyPresent b) && S.null (buddyAssistants b)
|
|
|
|
canpair = not $ S.null (buddyAssistants b)
|
|
|
|
clients = S.toList $ buddyPresent b `S.union` buddyAway b `S.union` buddyAssistants b
|
2012-11-10 20:35:09 +00:00
|
|
|
alreadypaired j = baseJID j `elem` pairedwith
|
2012-11-02 16:59:31 +00:00
|
|
|
|
|
|
|
{- Updates the buddies with XMPP presence info. -}
|
|
|
|
updateBuddies :: Presence -> Buddies -> Buddies
|
|
|
|
updateBuddies p@(Presence { presenceFrom = Just jid }) = M.alter update key
|
|
|
|
where
|
2012-11-05 21:43:17 +00:00
|
|
|
key = genBuddyKey jid
|
2012-11-02 16:59:31 +00:00
|
|
|
update (Just b) = Just $ applyPresence p b
|
|
|
|
update Nothing = newBuddy p
|
|
|
|
updateBuddies _ = id
|
|
|
|
|
|
|
|
{- Creates a new buddy based on XMPP presence info. -}
|
|
|
|
newBuddy :: Presence -> Maybe Buddy
|
|
|
|
newBuddy p
|
|
|
|
| presenceType p == PresenceAvailable = go
|
|
|
|
| presenceType p == PresenceUnavailable = go
|
|
|
|
| otherwise = Nothing
|
|
|
|
where
|
|
|
|
go = make <$> presenceFrom p
|
|
|
|
make _jid = applyPresence p $ Buddy
|
|
|
|
{ buddyPresent = S.empty
|
|
|
|
, buddyAway = S.empty
|
|
|
|
, buddyAssistants = S.empty
|
2012-11-05 21:43:17 +00:00
|
|
|
, buddyPairing = False
|
2012-11-02 16:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
applyPresence :: Presence -> Buddy -> Buddy
|
|
|
|
applyPresence p b = fromMaybe b $! go <$> presenceFrom p
|
|
|
|
where
|
|
|
|
go jid
|
2012-11-03 01:36:26 +00:00
|
|
|
| presenceType p == PresenceUnavailable = b
|
|
|
|
{ buddyAway = addto $ buddyAway b
|
|
|
|
, buddyPresent = removefrom $ buddyPresent b
|
|
|
|
, buddyAssistants = removefrom $ buddyAssistants b
|
|
|
|
}
|
2012-11-05 19:40:56 +00:00
|
|
|
| hasGitAnnexTag p = b
|
2012-11-03 01:36:26 +00:00
|
|
|
{ buddyAssistants = addto $ buddyAssistants b
|
|
|
|
, buddyAway = removefrom $ buddyAway b }
|
2012-11-02 16:59:31 +00:00
|
|
|
| presenceType p == PresenceAvailable = b
|
|
|
|
{ buddyPresent = addto $ buddyPresent b
|
|
|
|
, buddyAway = removefrom $ buddyAway b
|
|
|
|
}
|
|
|
|
| otherwise = b
|
|
|
|
where
|
|
|
|
client = Client jid
|
|
|
|
removefrom = S.filter (/= client)
|
|
|
|
addto = S.insert client
|