diff options
Diffstat (limited to 'Presence/XMPPServer.hs')
-rw-r--r-- | Presence/XMPPServer.hs | 69 |
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 #-} |
7 | module XMPPServer | 7 | module 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 | |||
58 | import Control.Concurrent | 59 | import Control.Concurrent |
59 | import Control.Exception as Exception | 60 | import Control.Exception as Exception |
60 | import Text.Show.ByteString as L | 61 | import Text.Show.ByteString as L |
61 | import Data.Binary.Builder as B | ||
62 | import Data.Binary.Put | ||
63 | import qualified Data.Map as Map | 62 | import qualified Data.Map as Map |
64 | import GHC.Conc | 63 | import GHC.Conc |
65 | import Network.BSD hiding (getHostByAddr) | 64 | import Network.BSD hiding (getHostByAddr) |
66 | import Control.Concurrent.Async | 65 | import Control.Concurrent.Async |
67 | import qualified Data.Set as Set | 66 | import qualified Data.Set as Set |
68 | import GetHostByAddr | 67 | import GetHostByAddr |
69 | 68 | import XMPPTypes | |
70 | data Peer = LocalHost | RemotePeer SockAddr | ||
71 | deriving (Eq,Prelude.Show) | ||
72 | |||
73 | instance 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 | ||
89 | data JID = JID { name :: Maybe ByteString | ||
90 | , peer :: Peer | ||
91 | , resource :: Maybe ByteString | ||
92 | } | ||
93 | deriving (Eq,Ord) | ||
94 | 69 | ||
95 | is_remote (RemotePeer _) = True | 70 | is_remote (RemotePeer _) = True |
96 | is_remote _ = False | 71 | is_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 | ||
113 | showPeer :: Peer -> ByteString | ||
114 | showPeer LocalHost = "localhost" | ||
115 | showPeer (RemotePeer addr@(SockAddrInet _ _)) = pack $ stripColon (Prelude.show addr) | ||
116 | where stripColon s = pre where (pre,port) = break (==':') s | ||
117 | showPeer (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 | |||
122 | peerAddr :: Peer -> SockAddr | 88 | peerAddr :: Peer -> SockAddr |
123 | peerAddr (RemotePeer addr) = addr | 89 | peerAddr (RemotePeer addr) = addr |
124 | -- peerAddr LocalHost = throw exception | 90 | -- peerAddr LocalHost = throw exception |
125 | 91 | ||
126 | instance L.Show JID where | ||
127 | showp (JID n s r ) = putBuilder . B.fromLazyByteString $ n <$++> "@" <?++> showPeer s <++?> "/" <++$> r | ||
128 | |||
129 | instance Prelude.Show JID where | ||
130 | show jid = L.unpack $ L.show jid | ||
131 | |||
132 | instance NFData JID where | ||
133 | rnf v@(JID n s r) = n `seq` s `seq` r `seq` () | ||
134 | |||
135 | jid user host rsrc = JID (Just user) host (Just rsrc) | ||
136 | |||
137 | data JabberShow = Offline | ||
138 | | Away | ||
139 | | Available | ||
140 | deriving (Prelude.Show,Enum,Ord,Eq,Read) | ||
141 | |||
142 | data Presence = Presence JID JabberShow | ||
143 | deriving Prelude.Show | ||
144 | 92 | ||
145 | xmlifyPresenceForPeer sock (Presence jid stat) = do | 93 | xmlifyPresenceForPeer 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 | ||
187 | class 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 | |||
196 | class XMPPConfig config where | ||
197 | getBuddies :: config -> ByteString -> IO [ByteString] | ||
198 | getSubscribers :: config -> ByteString -> IO [ByteString] | ||
199 | |||
200 | greet host = L.unlines | 135 | greet host = L.unlines |
201 | [ "<?xml version='1.0'?>" | 136 | [ "<?xml version='1.0'?>" |
202 | , "<stream:stream" | 137 | , "<stream:stream" |