diff options
Diffstat (limited to 'Presence/XMPPServer.hs')
-rw-r--r-- | Presence/XMPPServer.hs | 199 |
1 files changed, 199 insertions, 0 deletions
diff --git a/Presence/XMPPServer.hs b/Presence/XMPPServer.hs new file mode 100644 index 00000000..591acad6 --- /dev/null +++ b/Presence/XMPPServer.hs | |||
@@ -0,0 +1,199 @@ | |||
1 | {-# LANGUAGE OverloadedStrings #-} | ||
2 | {-# LANGUAGE ScopedTypeVariables #-} | ||
3 | {-# LANGUAGE ViewPatterns #-} | ||
4 | module XMPPServer ( listenForXmppClients ) where | ||
5 | |||
6 | import Data.HList.TypeEqGeneric1() | ||
7 | import Data.HList.TypeCastGeneric1() | ||
8 | import ByteStringOperators | ||
9 | |||
10 | import Server | ||
11 | import Data.ByteString.Lazy.Char8 as L | ||
12 | ( ByteString | ||
13 | , hPutStrLn | ||
14 | , unlines | ||
15 | , pack | ||
16 | , unpack | ||
17 | , init ) | ||
18 | import qualified Data.ByteString.Lazy.Char8 as L | ||
19 | ( putStrLn ) | ||
20 | import System.IO | ||
21 | ( Handle | ||
22 | ) | ||
23 | import Control.Concurrent (forkIO) | ||
24 | import Control.Concurrent.Chan | ||
25 | import Data.HList | ||
26 | import AdaptServer | ||
27 | import Text.XML.HaXml.Lex (xmlLex) | ||
28 | import Text.XML.HaXml.Parse (xmlParseWith,element,doctypedecl,processinginstruction,elemOpenTag,elemCloseTag) | ||
29 | import Text.XML.HaXml.Types as Hax (ElemTag,QName(..),Namespace(..),Element(..),Content(..),AttValue(..)) | ||
30 | import qualified Text.XML.HaXml.Types as Hax (Element) | ||
31 | import Data.Maybe | ||
32 | import Debug.Trace | ||
33 | |||
34 | |||
35 | |||
36 | greet host = L.unlines | ||
37 | [ "<?xml version='1.0'?>" | ||
38 | , "<stream:stream" | ||
39 | , "from='" <++> host <++> "'" | ||
40 | , "id='someid'" | ||
41 | , "xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>" | ||
42 | , "<stream:features>" | ||
43 | , " <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>" | ||
44 | {- | ||
45 | -- , " <session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>" | ||
46 | , " <mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>" | ||
47 | -- , " <mechanism>DIGEST-MD5</mechanism>" | ||
48 | , " <mechanism>PLAIN</mechanism>" | ||
49 | , " </mechanisms> " | ||
50 | -} | ||
51 | , "</stream:features>" | ||
52 | ] | ||
53 | |||
54 | startCon st = do | ||
55 | let h = hOccursFst st :: Handle | ||
56 | return (ConnectionFinalizer (return ()) .*. st) | ||
57 | |||
58 | iq_query_unavailable host id mjid xmlns = L.unlines $ | ||
59 | [ "<iq type='error'" | ||
60 | , " from='" <++> host <++> "'" | ||
61 | , case mjid of Just jid -> " to='" <++> jid <++> "'" | ||
62 | Nothing -> "" | ||
63 | , " id='" <++> id <++> "'>" | ||
64 | , " <query xmlns='" <++> xmlns <++> "'/>" | ||
65 | , " <error type='cancel'>" | ||
66 | , " <service-unavailable" | ||
67 | , " xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" | ||
68 | , " </error>" | ||
69 | , "</iq>" | ||
70 | ] | ||
71 | |||
72 | tagattrs tag content = Prelude.concatMap (\(CElem (Elem _ a _) _)->a) | ||
73 | $ Prelude.filter (bindElem tag) content | ||
74 | |||
75 | bindElem tag (CElem (Elem (N n) _ _) _) | n==tag = True | ||
76 | bindElem _ _ = False | ||
77 | |||
78 | hasElem tag content = | ||
79 | not . Prelude.null . Prelude.filter (bindElem tag) $ content | ||
80 | |||
81 | unattr (AttValue as) = listToMaybe $ Prelude.concatMap left as | ||
82 | where left (Left x) = [x] | ||
83 | left _ = [] | ||
84 | |||
85 | astring (AttValue [Left s]) = [s] | ||
86 | |||
87 | tagcontent tag content = Prelude.concatMap (\(CElem (Elem _ _ c) _)->c) | ||
88 | $ Prelude.filter (bindElem tag) content | ||
89 | |||
90 | iqresult host id (Just rsrc) = L.unlines $ | ||
91 | [ "<iq type='result' id='" <++> id <++> "'>" | ||
92 | , "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>" | ||
93 | , "<jid>" <++> id <++> "@" <++> host <++> "/" <++> rsrc <++> "</jid>" | ||
94 | , "</bind>" | ||
95 | , "</iq> " | ||
96 | ] | ||
97 | iqresult host id Nothing = L.unlines $ | ||
98 | [ "<iq type='result'" | ||
99 | , " id='" <++> id <++> "'" | ||
100 | , " from='" <++> host <++> "'" | ||
101 | , " /> " | ||
102 | ] | ||
103 | |||
104 | iqresult_info host id mjid = L.unlines $ | ||
105 | [ "<iq type='result'" | ||
106 | , " from='" <++> host <++> "'" | ||
107 | , case mjid of Just jid -> " to='" <++> jid <++> "'" | ||
108 | Nothing -> "" | ||
109 | , " id='" <++> id <++> "'>" | ||
110 | , " <query xmlns='http://jabber.org/protocol/disco#info'>" | ||
111 | , " <identity" | ||
112 | , " category='server'" | ||
113 | , " type='im'" | ||
114 | , " name='" <++> host <++> "'/>" | ||
115 | , " <feature var='http://jabber.org/protocol/disco#info'/>" | ||
116 | , " <feature var='http://jabber.org/protocol/disco#items'/>" | ||
117 | , " </query>" | ||
118 | , "</iq>" | ||
119 | ] | ||
120 | |||
121 | iqresponse host (Elem _ attrs content) = do | ||
122 | id <- fmap pack (lookup (N "id") attrs >>= unattr) | ||
123 | typ <- fmap pack (lookup (N "type") attrs >>= unattr) | ||
124 | case typ of | ||
125 | "set" -> do | ||
126 | let string (CString _ s _) = [s] | ||
127 | mplus (do | ||
128 | rsrc <- listToMaybe . Prelude.concatMap string . tagcontent "resource" . tagcontent "bind" $ content | ||
129 | Just $ iqresult host id (Just (pack rsrc)) ) | ||
130 | (do | ||
131 | guard (hasElem "session" content) | ||
132 | Just (iqresult host id Nothing)) | ||
133 | |||
134 | "get" -> trace ("iq-get "++show (attrs,content)) $ do | ||
135 | xmlns <- fmap pack $ | ||
136 | lookup (N "xmlns") (tagattrs "query" content) | ||
137 | >>= listToMaybe . astring | ||
138 | Just (iq_query_unavailable host id Nothing xmlns) | ||
139 | _ -> Nothing | ||
140 | |||
141 | |||
142 | |||
143 | doCon st elem cont = do | ||
144 | let h = hOccursFst st :: Handle | ||
145 | host = "localhost" | ||
146 | hsend r = do | ||
147 | hPutStrLn h r | ||
148 | L.putStrLn $ "SENT:\n" <++> r | ||
149 | |||
150 | case elem of | ||
151 | OpenTag _ -> | ||
152 | hsend (greet host) | ||
153 | Element e@(Elem (N "iq") _ _) -> | ||
154 | case iqresponse host e of | ||
155 | Nothing -> trace "no respones" $ return () | ||
156 | Just r -> hsend r | ||
157 | _ -> return () -- putStrLn $ "unhandled: "++show v | ||
158 | |||
159 | putStrLn (show elem) | ||
160 | cont () | ||
161 | |||
162 | instance Show Hax.ElemTag where | ||
163 | show _ = "elemtag" | ||
164 | |||
165 | data XmppObject e i t c | ||
166 | = Element e | ||
167 | | ProcessingInstruction i | ||
168 | | OpenTag t | ||
169 | | CloseTag c | ||
170 | deriving Show | ||
171 | |||
172 | streamName = QN (Namespace {nsPrefix= "stream" , nsURI="http://etherx.jabber.org/streams" }) "stream" | ||
173 | |||
174 | xmppParse ls = | ||
175 | case xmlParseWith element ls of | ||
176 | (Right e,rs) -> (Right (Element e), rs) | ||
177 | (Left _,_) -> | ||
178 | case xmlParseWith elemOpenTag ls of | ||
179 | (Right e,rs) -> (Right (OpenTag e),rs) | ||
180 | (Left _,_) -> | ||
181 | case xmlParseWith (elemCloseTag streamName) ls of | ||
182 | (Right e,rs) -> (Right (CloseTag e),rs) | ||
183 | (Left _,_) -> | ||
184 | case xmlParseWith processinginstruction ls of | ||
185 | (Right e,rs) -> (Right (ProcessingInstruction e),rs) | ||
186 | (Left err,rs) -> (Left err,rs) | ||
187 | |||
188 | |||
189 | |||
190 | listenForXmppClients port st = do | ||
191 | let (start,dopkt) = | ||
192 | adaptServer ( xmlLex "stream" . unpack | ||
193 | , xmppParse) | ||
194 | (startCon,doCon) | ||
195 | doServer (port .*. st) | ||
196 | dopkt | ||
197 | start | ||
198 | |||
199 | |||