diff options
author | joe <joe@jerkface.net> | 2013-06-17 02:21:57 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2013-06-17 02:21:57 -0400 |
commit | 57a7a887adb443c516230ac23602b52e1d94d240 (patch) | |
tree | 98aaf54a7e26315fcde477153394fcda8bbf02fe | |
parent | 87c5c2ae4fb60b58dcfb172c5e9b44151b02d777 (diff) |
progress on sessions
-rw-r--r-- | Presence/XMPPServer.hs | 70 | ||||
-rw-r--r-- | Presence/main.hs | 8 |
2 files changed, 29 insertions, 49 deletions
diff --git a/Presence/XMPPServer.hs b/Presence/XMPPServer.hs index 87745e96..044ed9e4 100644 --- a/Presence/XMPPServer.hs +++ b/Presence/XMPPServer.hs | |||
@@ -3,6 +3,7 @@ | |||
3 | {-# LANGUAGE ViewPatterns #-} | 3 | {-# LANGUAGE ViewPatterns #-} |
4 | {-# LANGUAGE TupleSections #-} | 4 | {-# LANGUAGE TupleSections #-} |
5 | {-# LANGUAGE TypeFamilies #-} | 5 | {-# LANGUAGE TypeFamilies #-} |
6 | -- {-# LANGUAGE GADTs #-} | ||
6 | module XMPPServer where -- ( listenForXmppClients ) where | 7 | module XMPPServer where -- ( listenForXmppClients ) where |
7 | 8 | ||
8 | import Data.HList.TypeEqGeneric1() | 9 | import Data.HList.TypeEqGeneric1() |
@@ -37,6 +38,8 @@ import Control.Arrow | |||
37 | import LocalPeerCred | 38 | import LocalPeerCred |
38 | import Network.Socket | 39 | import Network.Socket |
39 | import Data.String | 40 | import Data.String |
41 | import Control.Monad.Trans.Maybe | ||
42 | import Control.Monad.IO.Class | ||
40 | 43 | ||
41 | 44 | ||
42 | class XMPPSession session where | 45 | class XMPPSession session where |
@@ -65,7 +68,7 @@ greet host = L.unlines | |||
65 | , "</stream:features>" | 68 | , "</stream:features>" |
66 | ] | 69 | ] |
67 | 70 | ||
68 | newtype TaggedXMPPSession s = TaggedXMPPSession s | 71 | -- data TaggedXMPPSession s = TaggedXMPPSession s |
69 | 72 | ||
70 | startCon session_factory sock st = do | 73 | startCon session_factory sock st = do |
71 | let h = hOccursFst st :: Handle | 74 | let h = hOccursFst st :: Handle |
@@ -75,9 +78,7 @@ startCon session_factory sock st = do | |||
75 | session <- newSession session_factory sock h | 78 | session <- newSession session_factory sock h |
76 | Prelude.putStrLn $ "PEER NAME: "++show pname | 79 | Prelude.putStrLn $ "PEER NAME: "++show pname |
77 | 80 | ||
78 | return ( ConnectionFinalizer (return ()) | 81 | return ( session .*. ConnectionFinalizer (closeSession session) .*. st) |
79 | .*. TaggedXMPPSession session | ||
80 | .*. st) | ||
81 | 82 | ||
82 | iq_query_unavailable host id mjid xmlns kind = L.unlines $ | 83 | iq_query_unavailable host id mjid xmlns kind = L.unlines $ |
83 | [ "<iq type='error'" | 84 | [ "<iq type='error'" |
@@ -143,42 +144,37 @@ iqresult_info host id mjid = L.unlines $ | |||
143 | , "</iq>" | 144 | , "</iq>" |
144 | ] | 145 | ] |
145 | 146 | ||
146 | iqresponse host (Elem _ attrs content) = do | 147 | iqresponse session host (Elem _ attrs content) = runMaybeT $ do |
147 | id <- fmap pack (lookup (N "id") attrs >>= unattr) | 148 | id <- MaybeT . return $ fmap pack (lookup (N "id") attrs >>= unattr) |
148 | typ <- fmap pack (lookup (N "type") attrs >>= unattr) | 149 | typ <- MaybeT . return $ fmap pack (lookup (N "type") attrs >>= unattr) |
149 | case typ of | 150 | case typ of |
150 | "set" -> do | 151 | "set" -> do |
151 | let string (CString _ s _) = [s] | 152 | let string (CString _ s _) = [s] |
152 | mplus (do | 153 | mplus (do |
153 | rsrc <- listToMaybe . Prelude.concatMap string . tagcontent "resource" . tagcontent "bind" $ content | 154 | rsrc <- MaybeT . return . fmap pack $ listToMaybe . Prelude.concatMap string . tagcontent "resource" . tagcontent "bind" $ content |
154 | let jid = "TODO" <++> "@" <++> host <++> "/" <++> pack rsrc | 155 | -- let jid = "TODO" <++> "@" <++> host <++> "/" <++> pack rsrc |
155 | Just $ iq_bind_reply id jid ) | 156 | liftIO $ do |
157 | setResource session rsrc | ||
158 | jid <- getJID session | ||
159 | return $ iq_bind_reply id jid ) | ||
156 | (do | 160 | (do |
157 | guard (hasElem "session" content) | 161 | guard (hasElem "session" content) |
158 | Just (iq_session_reply host id)) | 162 | return (iq_session_reply host id)) |
159 | 163 | ||
160 | "get" -> {- trace ("iq-get "++show (attrs,content)) $ -} do | 164 | "get" -> {- trace ("iq-get "++show (attrs,content)) $ -} do |
161 | (tag,as) <- lookup (N "xmlns") (anytagattrs content) | 165 | (tag,as) <- MaybeT . return $ lookup (N "xmlns") (anytagattrs content) |
162 | xmlns <- fmap pack $ listToMaybe . astring $ as | 166 | xmlns <- MaybeT . return $ fmap pack $ listToMaybe . astring $ as |
163 | let servicekind = case tag of { (N s) -> pack s ; _ -> "query" } | 167 | let servicekind = case tag of { (N s) -> pack s ; _ -> "query" } |
164 | case xmlns of | 168 | case xmlns of |
165 | "urn:xmpp:ping" -> do | 169 | "urn:xmpp:ping" -> do |
166 | let to = case fmap pack (lookup (N "from") attrs >>= unattr) of | 170 | let to = case fmap pack (lookup (N "from") attrs >>= unattr) of |
167 | Just jid -> "to='" <++> jid <++> "' " | 171 | Just jid -> "to='" <++> jid <++> "' " |
168 | Nothing -> "" | 172 | Nothing -> "" |
169 | Just $ "<iq from='" <++> host <++> "' " <++> to <++> "id='" <++> id <++> "' type='result'/>" | 173 | return $ "<iq from='" <++> host <++> "' " <++> to <++> "id='" <++> id <++> "' type='result'/>" |
170 | 174 | ||
171 | _ -> Just (iq_query_unavailable host id Nothing xmlns servicekind) | 175 | _ -> return (iq_query_unavailable host id Nothing xmlns servicekind) |
172 | _ -> Nothing | 176 | _ -> MaybeT (return Nothing) |
173 | 177 | ||
174 | -- <presence> | ||
175 | -- <priority>1</priority> | ||
176 | -- <c xmlns='http://jabber.org/protocol/caps' | ||
177 | -- node='http://pidgin.im/' | ||
178 | -- hash='sha-1' ver='lV6i//bt2U8Rm0REcX8h4F3Nk3M=' | ||
179 | -- ext='voice-v1 camera-v1 video-v1'/> | ||
180 | -- <x xmlns='vcard-temp:x:update'/> | ||
181 | -- </presence> | ||
182 | 178 | ||
183 | presence_response host (Elem _ attrs content) = do | 179 | presence_response host (Elem _ attrs content) = do |
184 | -- let id = fmap pack (lookup (N "id") attrs >>= unattr) | 180 | -- let id = fmap pack (lookup (N "id") attrs >>= unattr) |
@@ -196,14 +192,16 @@ doCon st elem cont = do | |||
196 | hsend r = do | 192 | hsend r = do |
197 | hPutStrLn h r | 193 | hPutStrLn h r |
198 | L.putStrLn $ "\nOUT:\n" <++> r | 194 | L.putStrLn $ "\nOUT:\n" <++> r |
199 | 195 | session = hHead st | |
196 | |||
200 | putStrLn $ (show $ hang (text "\nIN:") 2 $ pp elem) ++ "\n" | 197 | putStrLn $ (show $ hang (text "\nIN:") 2 $ pp elem) ++ "\n" |
201 | 198 | ||
202 | case elem of | 199 | case elem of |
203 | OpenTag _ -> | 200 | OpenTag _ -> |
204 | hsend (greet host) | 201 | hsend (greet host) |
205 | Element e@(Elem (N "iq") _ _) -> | 202 | Element e@(Elem (N "iq") _ _) -> do |
206 | case iqresponse host e of | 203 | rpns <- iqresponse session host e |
204 | case rpns of | ||
207 | Nothing -> trace "IGNORE: no response to <iq>" $ return () | 205 | Nothing -> trace "IGNORE: no response to <iq>" $ return () |
208 | Just r -> hsend r | 206 | Just r -> hsend r |
209 | Element e@(Elem (N "presence") _ _) -> | 207 | Element e@(Elem (N "presence") _ _) -> |
@@ -249,26 +247,8 @@ xmppParse ls = runTryParse $ do | |||
249 | Try . xml OpenTag $ elemOpenTag | 247 | Try . xml OpenTag $ elemOpenTag |
250 | Try . xml CloseTag $ elemCloseTag streamName | 248 | Try . xml CloseTag $ elemCloseTag streamName |
251 | Try . xml ProcessingInstruction $ processinginstruction | 249 | Try . xml ProcessingInstruction $ processinginstruction |
252 | |||
253 | {- | ||
254 | xmppParseOld :: [(Posn, TokenT)] -> (Either String XmppObject, [(Posn, TokenT)]) | ||
255 | xmppParseOld ls = | ||
256 | case xmlParseWith element ls of | ||
257 | (Right e,rs) -> (Right (Element e), rs) | ||
258 | (Left _,_) -> | ||
259 | case xmlParseWith elemOpenTag ls of | ||
260 | (Right e,rs) -> (Right (OpenTag e),rs) | ||
261 | (Left _,_) -> | ||
262 | case xmlParseWith (elemCloseTag streamName) ls of | ||
263 | (Right (),rs) -> (Right (CloseTag ()),rs) | ||
264 | (Left _,_) -> | ||
265 | case xmlParseWith processinginstruction ls of | ||
266 | (Right e,rs) -> (Right (ProcessingInstruction e),rs) | ||
267 | (Left err,rs) -> (Left err,rs) | ||
268 | -} | ||
269 | 250 | ||
270 | 251 | ||
271 | |||
272 | listenForXmppClients session_factory port st = do | 252 | listenForXmppClients session_factory port st = do |
273 | let (start,dopkt) = | 253 | let (start,dopkt) = |
274 | adaptServer ( xmlLex "stream" . unpack | 254 | adaptServer ( xmlLex "stream" . unpack |
diff --git a/Presence/main.hs b/Presence/main.hs index 63ebf817..7a5939ff 100644 --- a/Presence/main.hs +++ b/Presence/main.hs | |||
@@ -41,10 +41,10 @@ data UnixSession = UnixSession | |||
41 | 41 | ||
42 | instance XMPPSession UnixSession where | 42 | instance XMPPSession UnixSession where |
43 | data XMPPClass UnixSession = UnixSessions | 43 | data XMPPClass UnixSession = UnixSessions |
44 | newSession _ sock handle = return UnixSession | 44 | newSession _ sock handle = putStrLn "SESSION: open" >> return UnixSession |
45 | setResource _ resource = return () | 45 | setResource _ resource = putStrLn "SESSION: resource" |
46 | getJID _ = return "nobody@fake.bad" | 46 | getJID _ = putStrLn "SESSION: jid" >> return "nobody@fake.bad" |
47 | closeSession _ = return () | 47 | closeSession _ = putStrLn "SESSION: close" >> return () |
48 | 48 | ||
49 | on_chvt vtnum = do | 49 | on_chvt vtnum = do |
50 | putStrLn $ "changed vt to "++ show vtnum | 50 | putStrLn $ "changed vt to "++ show vtnum |