summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Presence/XMPPServer.hs28
-rw-r--r--Presence/main.hs15
2 files changed, 35 insertions, 8 deletions
diff --git a/Presence/XMPPServer.hs b/Presence/XMPPServer.hs
index ace36f80..87745e96 100644
--- a/Presence/XMPPServer.hs
+++ b/Presence/XMPPServer.hs
@@ -2,6 +2,7 @@
2{-# LANGUAGE ScopedTypeVariables #-} 2{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE ViewPatterns #-} 3{-# LANGUAGE ViewPatterns #-}
4{-# LANGUAGE TupleSections #-} 4{-# LANGUAGE TupleSections #-}
5{-# LANGUAGE TypeFamilies #-}
5module XMPPServer where -- ( listenForXmppClients ) where 6module XMPPServer where -- ( listenForXmppClients ) where
6 7
7import Data.HList.TypeEqGeneric1() 8import Data.HList.TypeEqGeneric1()
@@ -12,6 +13,7 @@ import Server
12import Data.ByteString.Lazy.Char8 as L 13import Data.ByteString.Lazy.Char8 as L
13 ( hPutStrLn 14 ( hPutStrLn
14 , unlines 15 , unlines
16 , ByteString
15 , pack 17 , pack
16 , unpack ) 18 , unpack )
17import qualified Data.ByteString.Lazy.Char8 as L 19import qualified Data.ByteString.Lazy.Char8 as L
@@ -22,7 +24,7 @@ import System.IO
22import Data.HList 24import Data.HList
23import AdaptServer 25import AdaptServer
24import Text.XML.HaXml.Lex (xmlLex) 26import Text.XML.HaXml.Lex (xmlLex)
25import Text.XML.HaXml.Parse (xmlParseWith,element,{-doctypedecl,-}processinginstruction,elemOpenTag,elemCloseTag) 27import Text.XML.HaXml.Parse (XParser,xmlParseWith,element,{-doctypedecl,-}processinginstruction,elemOpenTag,elemCloseTag)
26import Text.XML.HaXml.Types as Hax hiding (Element) -- (ElemTag,QName(..),Namespace(..),Element(..),Content(..),AttValue(..)) 28import Text.XML.HaXml.Types as Hax hiding (Element) -- (ElemTag,QName(..),Namespace(..),Element(..),Content(..),AttValue(..))
27import qualified Text.XML.HaXml.Types as Hax (Element(..)) 29import qualified Text.XML.HaXml.Types as Hax (Element(..))
28import Text.XML.HaXml.Posn (Posn) 30import Text.XML.HaXml.Posn (Posn)
@@ -37,6 +39,13 @@ import Network.Socket
37import Data.String 39import Data.String
38 40
39 41
42class XMPPSession session where
43 data XMPPClass session
44 newSession :: XMPPClass session -> Socket -> Handle -> IO session
45 setResource :: session -> ByteString -> IO ()
46 getJID :: session -> IO ByteString
47 closeSession :: session -> IO ()
48
40 49
41greet host = L.unlines 50greet host = L.unlines
42 [ "<?xml version='1.0'?>" 51 [ "<?xml version='1.0'?>"
@@ -56,13 +65,19 @@ greet host = L.unlines
56 , "</stream:features>" 65 , "</stream:features>"
57 ] 66 ]
58 67
59startCon sock st = do 68newtype TaggedXMPPSession s = TaggedXMPPSession s
69
70startCon session_factory sock st = do
60 let h = hOccursFst st :: Handle 71 let h = hOccursFst st :: Handle
61 cred <- getLocalPeerCred sock 72 cred <- getLocalPeerCred sock
62 Prelude.putStrLn $ "PEER CRED: "++show cred 73 Prelude.putStrLn $ "PEER CRED: "++show cred
63 pname <- getPeerName sock 74 pname <- getPeerName sock
75 session <- newSession session_factory sock h
64 Prelude.putStrLn $ "PEER NAME: "++show pname 76 Prelude.putStrLn $ "PEER NAME: "++show pname
65 return (ConnectionFinalizer (return ()) .*. st) 77
78 return ( ConnectionFinalizer (return ())
79 .*. TaggedXMPPSession session
80 .*. st)
66 81
67iq_query_unavailable host id mjid xmlns kind = L.unlines $ 82iq_query_unavailable host id mjid xmlns kind = L.unlines $
68 [ "<iq type='error'" 83 [ "<iq type='error'"
@@ -228,7 +243,8 @@ mapRight f (Left y,ls) = (Left y ,ls)
228 243
229xmppParse :: [(Posn, TokenT)] -> (Either String XmppObject, [(Posn, TokenT)]) 244xmppParse :: [(Posn, TokenT)] -> (Either String XmppObject, [(Posn, TokenT)])
230xmppParse ls = runTryParse $ do 245xmppParse ls = runTryParse $ do
231 let xml tag = mapRight tag . flip xmlParseWith ls 246 let xml :: (t -> b) -> XParser t -> (Either String b, [(Posn, TokenT)])
247 xml tag = mapRight tag . flip xmlParseWith ls
232 Try . xml Element $ element 248 Try . xml Element $ element
233 Try . xml OpenTag $ elemOpenTag 249 Try . xml OpenTag $ elemOpenTag
234 Try . xml CloseTag $ elemCloseTag streamName 250 Try . xml CloseTag $ elemCloseTag streamName
@@ -253,11 +269,11 @@ xmppParseOld ls =
253 269
254 270
255 271
256listenForXmppClients port st = do 272listenForXmppClients session_factory port st = do
257 let (start,dopkt) = 273 let (start,dopkt) =
258 adaptServer ( xmlLex "stream" . unpack 274 adaptServer ( xmlLex "stream" . unpack
259 , xmppParse) 275 , xmppParse)
260 (startCon,doCon) 276 (startCon session_factory,doCon)
261 doServer (port .*. st) 277 doServer (port .*. st)
262 dopkt 278 dopkt
263 start 279 start
diff --git a/Presence/main.hs b/Presence/main.hs
index 7df81903..63ebf817 100644
--- a/Presence/main.hs
+++ b/Presence/main.hs
@@ -1,4 +1,6 @@
1{-# LANGUAGE CPP #-} 1{-# LANGUAGE CPP #-}
2{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE TypeFamilies #-}
2 4
3import System.Directory 5import System.Directory
4import Control.Monad 6import Control.Monad
@@ -35,6 +37,15 @@ utmp_event e = do
35 forM_ ids putStrLn 37 forM_ ids putStrLn
36#endif 38#endif
37 39
40data UnixSession = UnixSession
41
42instance XMPPSession UnixSession where
43 data XMPPClass UnixSession = UnixSessions
44 newSession _ sock handle = return UnixSession
45 setResource _ resource = return ()
46 getJID _ = return "nobody@fake.bad"
47 closeSession _ = return ()
48
38on_chvt vtnum = do 49on_chvt vtnum = do
39 putStrLn $ "changed vt to "++ show vtnum 50 putStrLn $ "changed vt to "++ show vtnum
40 51
@@ -55,7 +66,7 @@ start = do
55 print wd 66 print wd
56#endif 67#endif
57 mtty <- monitorTTY on_chvt 68 mtty <- monitorTTY on_chvt
58 sock <- listenForXmppClients 5222 HNil 69 sock <- listenForXmppClients UnixSessions 5222 HNil
59 putStrLn "Hit enter to terminate..." 70 putStrLn "Hit enter to terminate..."
60 getLine 71 getLine
61 {- 72 {-