diff options
-rw-r--r-- | Presence/XMPPServer.hs | 28 | ||||
-rw-r--r-- | Presence/main.hs | 15 |
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 #-} | ||
5 | module XMPPServer where -- ( listenForXmppClients ) where | 6 | module XMPPServer where -- ( listenForXmppClients ) where |
6 | 7 | ||
7 | import Data.HList.TypeEqGeneric1() | 8 | import Data.HList.TypeEqGeneric1() |
@@ -12,6 +13,7 @@ import Server | |||
12 | import Data.ByteString.Lazy.Char8 as L | 13 | import Data.ByteString.Lazy.Char8 as L |
13 | ( hPutStrLn | 14 | ( hPutStrLn |
14 | , unlines | 15 | , unlines |
16 | , ByteString | ||
15 | , pack | 17 | , pack |
16 | , unpack ) | 18 | , unpack ) |
17 | import qualified Data.ByteString.Lazy.Char8 as L | 19 | import qualified Data.ByteString.Lazy.Char8 as L |
@@ -22,7 +24,7 @@ import System.IO | |||
22 | import Data.HList | 24 | import Data.HList |
23 | import AdaptServer | 25 | import AdaptServer |
24 | import Text.XML.HaXml.Lex (xmlLex) | 26 | import Text.XML.HaXml.Lex (xmlLex) |
25 | import Text.XML.HaXml.Parse (xmlParseWith,element,{-doctypedecl,-}processinginstruction,elemOpenTag,elemCloseTag) | 27 | import Text.XML.HaXml.Parse (XParser,xmlParseWith,element,{-doctypedecl,-}processinginstruction,elemOpenTag,elemCloseTag) |
26 | import Text.XML.HaXml.Types as Hax hiding (Element) -- (ElemTag,QName(..),Namespace(..),Element(..),Content(..),AttValue(..)) | 28 | import Text.XML.HaXml.Types as Hax hiding (Element) -- (ElemTag,QName(..),Namespace(..),Element(..),Content(..),AttValue(..)) |
27 | import qualified Text.XML.HaXml.Types as Hax (Element(..)) | 29 | import qualified Text.XML.HaXml.Types as Hax (Element(..)) |
28 | import Text.XML.HaXml.Posn (Posn) | 30 | import Text.XML.HaXml.Posn (Posn) |
@@ -37,6 +39,13 @@ import Network.Socket | |||
37 | import Data.String | 39 | import Data.String |
38 | 40 | ||
39 | 41 | ||
42 | class 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 | ||
41 | greet host = L.unlines | 50 | greet 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 | ||
59 | startCon sock st = do | 68 | newtype TaggedXMPPSession s = TaggedXMPPSession s |
69 | |||
70 | startCon 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 | ||
67 | iq_query_unavailable host id mjid xmlns kind = L.unlines $ | 82 | iq_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 | ||
229 | xmppParse :: [(Posn, TokenT)] -> (Either String XmppObject, [(Posn, TokenT)]) | 244 | xmppParse :: [(Posn, TokenT)] -> (Either String XmppObject, [(Posn, TokenT)]) |
230 | xmppParse ls = runTryParse $ do | 245 | xmppParse 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 | ||
256 | listenForXmppClients port st = do | 272 | listenForXmppClients 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 | ||
3 | import System.Directory | 5 | import System.Directory |
4 | import Control.Monad | 6 | import Control.Monad |
@@ -35,6 +37,15 @@ utmp_event e = do | |||
35 | forM_ ids putStrLn | 37 | forM_ ids putStrLn |
36 | #endif | 38 | #endif |
37 | 39 | ||
40 | data UnixSession = UnixSession | ||
41 | |||
42 | instance 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 | |||
38 | on_chvt vtnum = do | 49 | on_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 | {- |