summaryrefslogtreecommitdiff
path: root/Presence/XMPPServer.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Presence/XMPPServer.hs')
-rw-r--r--Presence/XMPPServer.hs69
1 files changed, 2 insertions, 67 deletions
diff --git a/Presence/XMPPServer.hs b/Presence/XMPPServer.hs
index e8ab5490..813c7c7b 100644
--- a/Presence/XMPPServer.hs
+++ b/Presence/XMPPServer.hs
@@ -6,6 +6,7 @@
6-- {-# LANGUAGE GADTs #-} 6-- {-# LANGUAGE GADTs #-}
7module XMPPServer 7module XMPPServer
8 ( module XMPPServer 8 ( module XMPPServer
9 , module XMPPTypes
9 , quitListening 10 , quitListening
10 ) where 11 ) where
11 12
@@ -58,39 +59,13 @@ import Control.Concurrent.STM
58import Control.Concurrent 59import Control.Concurrent
59import Control.Exception as Exception 60import Control.Exception as Exception
60import Text.Show.ByteString as L 61import Text.Show.ByteString as L
61import Data.Binary.Builder as B
62import Data.Binary.Put
63import qualified Data.Map as Map 62import qualified Data.Map as Map
64import GHC.Conc 63import GHC.Conc
65import Network.BSD hiding (getHostByAddr) 64import Network.BSD hiding (getHostByAddr)
66import Control.Concurrent.Async 65import Control.Concurrent.Async
67import qualified Data.Set as Set 66import qualified Data.Set as Set
68import GetHostByAddr 67import GetHostByAddr
69 68import XMPPTypes
70data Peer = LocalHost | RemotePeer SockAddr
71 deriving (Eq,Prelude.Show)
72
73instance Ord Peer where
74 LocalHost <= _
75 = True
76 RemotePeer (SockAddrUnix a) <= RemotePeer (SockAddrUnix b)
77 = a <= b
78 RemotePeer (SockAddrUnix _) <= _
79 = True
80 RemotePeer (SockAddrInet aport a) <= RemotePeer (SockAddrInet bport b)
81 = (a,aport) <= (b,bport)
82 RemotePeer (SockAddrInet aport a) <= _
83 = True
84 RemotePeer (SockAddrInet6 aport aflow a ascope) <= RemotePeer (SockAddrInet6 bport bflow b bscope)
85 = (a,aport,ascope,aflow) <= (b,bport,bscope,bflow)
86 a <= b = False
87
88-- | Jabber ID (JID) datatype
89data JID = JID { name :: Maybe ByteString
90 , peer :: Peer
91 , resource :: Maybe ByteString
92 }
93 deriving (Eq,Ord)
94 69
95is_remote (RemotePeer _) = True 70is_remote (RemotePeer _) = True
96is_remote _ = False 71is_remote _ = False
@@ -110,37 +85,10 @@ getNamesForPeer peer@(RemotePeer addr) = do
110 return . map pack $ names 85 return . map pack $ names
111 86
112 87
113showPeer :: Peer -> ByteString
114showPeer LocalHost = "localhost"
115showPeer (RemotePeer addr@(SockAddrInet _ _)) = pack $ stripColon (Prelude.show addr)
116 where stripColon s = pre where (pre,port) = break (==':') s
117showPeer (RemotePeer addr@(SockAddrInet6 _ _ _ _)) = pack $ stripColon (Prelude.show addr)
118 where stripColon s = if null bracket then pre else pre ++ "]"
119 where
120 (pre,bracket) = break (==']') s
121
122peerAddr :: Peer -> SockAddr 88peerAddr :: Peer -> SockAddr
123peerAddr (RemotePeer addr) = addr 89peerAddr (RemotePeer addr) = addr
124-- peerAddr LocalHost = throw exception 90-- peerAddr LocalHost = throw exception
125 91
126instance L.Show JID where
127 showp (JID n s r ) = putBuilder . B.fromLazyByteString $ n <$++> "@" <?++> showPeer s <++?> "/" <++$> r
128
129instance Prelude.Show JID where
130 show jid = L.unpack $ L.show jid
131
132instance NFData JID where
133 rnf v@(JID n s r) = n `seq` s `seq` r `seq` ()
134
135jid user host rsrc = JID (Just user) host (Just rsrc)
136
137data JabberShow = Offline
138 | Away
139 | Available
140 deriving (Prelude.Show,Enum,Ord,Eq,Read)
141
142data Presence = Presence JID JabberShow
143 deriving Prelude.Show
144 92
145xmlifyPresenceForPeer sock (Presence jid stat) = do 93xmlifyPresenceForPeer sock (Presence jid stat) = do
146 -- TODO: accept socket argument and determine local ip address 94 -- TODO: accept socket argument and determine local ip address
@@ -184,19 +132,6 @@ instance NFData Presence where
184 rnf (Presence jid stat) = rnf jid `seq` stat `seq` () 132 rnf (Presence jid stat) = rnf jid `seq` stat `seq` ()
185 133
186 134
187class XMPPSession session where
188 data XMPPClass session
189 newSession :: XMPPClass session -> Socket -> Handle -> IO session
190 setResource :: session -> ByteString -> IO ()
191 getJID :: session -> IO JID
192 closeSession :: session -> IO ()
193 subscribe :: session -> Maybe JID -> IO (TChan Presence)
194 announcePresence :: session -> Presence -> IO ()
195
196class XMPPConfig config where
197 getBuddies :: config -> ByteString -> IO [ByteString]
198 getSubscribers :: config -> ByteString -> IO [ByteString]
199
200greet host = L.unlines 135greet host = L.unlines
201 [ "<?xml version='1.0'?>" 136 [ "<?xml version='1.0'?>"
202 , "<stream:stream" 137 , "<stream:stream"