~singpolyma/network-protocol-xmpp

ref: 8e2d91e99079ac224a2df5b212f171d0d248c98e network-protocol-xmpp/Network/Protocol/XMPP/Stream.hs -rw-r--r-- 6.0 KiB
8e2d91e9 — John Millikin Use ``getTree`` to parse the <stream:features> element. 13 years ago
                                                                                
e17933b3 John Millikin
27bee8c4 John Millikin
e17933b3 John Millikin
27bee8c4 John Millikin
e17933b3 John Millikin
27bee8c4 John Millikin
e17933b3 John Millikin
27bee8c4 John Millikin
e17933b3 John Millikin
27bee8c4 John Millikin
e17933b3 John Millikin
8e2d91e9 John Millikin
27bee8c4 John Millikin
8e2d91e9 John Millikin
27bee8c4 John Millikin
e17933b3 John Millikin
8e2d91e9 John Millikin
e17933b3 John Millikin
8e2d91e9 John Millikin
e17933b3 John Millikin
27bee8c4 John Millikin
e17933b3 John Millikin
27bee8c4 John Millikin
8e2d91e9 John Millikin
27bee8c4 John Millikin
e17933b3 John Millikin
27bee8c4 John Millikin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
{- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>
   
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
-}

module Network.Protocol.XMPP.Stream (
	 Stream (
	 	 streamHostName
	 	,streamLanguage
	 	,streamVersion
	 	,streamFeatures
	 	)
	,StreamFeature (
		 FeatureStartTLS
		,FeatureSASL
		,FeatureRegister
		)
	,beginStream
	,getTree
	,putTree
	) where

import qualified System.IO as IO
import Network (HostName, PortID, connectTo)
import qualified Network.Protocol.XMPP.IncrementalXML as XML
import Data.AssocList (lookupDef)
import qualified Text.XML.HXT.DOM.QualifiedName as QN
import qualified Text.XML.HXT.DOM.XmlNode as XN
import Text.XML.HXT.DOM.TypeDefs (XmlTree)
import Text.XML.HXT.DOM.FormatXmlTree (formatXmlTree)
import Text.XML.HXT.DOM.Util (attrEscapeXml)
import Text.XML.HXT.Arrow ((>>>), (>>.))
import Data.Tree.NTree.TypeDefs (NTree(NTree))
import qualified Text.XML.HXT.Arrow as A

import Network.Protocol.XMPP.JID (JID)
import Network.Protocol.XMPP.SASL (Mechanism, findMechanism)
import Network.Protocol.XMPP.Stanzas (Stanza)
import Network.Protocol.XMPP.XMLBuilder (eventsToTree)

maxXMPPVersion = XMPPVersion 1 0

data Stream = Stream
	{
		 streamHandle :: IO.Handle
		,streamParser :: XML.Parser
		,streamHostName :: HostName
		,streamLanguage :: XMLLanguage
		,streamVersion :: XMPPVersion
		,streamFeatures :: [StreamFeature]
	}

data StreamFeature =
	  FeatureStartTLS Bool
	| FeatureSASL [Mechanism]
	| FeatureRegister
	| FeatureUnknown XmlTree
	| FeatureDebug String
	deriving (Show, Eq)

newtype XMLLanguage = XMLLanguage String
	deriving (Show, Eq)

data XMPPVersion = XMPPVersion Int Int
	deriving (Show, Eq)

-------------------------------------------------------------------------------

beginStream :: JID -> HostName -> IO.Handle -> IO Stream
beginStream jid host handle = do
	parser <- XML.newParser
	
	IO.hSetBuffering handle IO.NoBuffering
	
	-- Since only the opening tag should be written, normal XML
	-- serialization cannot be used. Be careful to escape any embedded
	-- attributes.
	IO.hPutStr handle $
		"<?xml version='1.0'?>\n" ++
		"<stream:stream xmlns='jabber:client'" ++
		" to='" ++ (attrEscapeXml . show) jid ++ "'" ++
		" version='1.0'" ++
		" xmlns:stream='http://etherx.jabber.org/streams'>"
	IO.hFlush handle
	
	[startStreamEvent] <- readEventsUntil startOfStream handle parser 1000
	featureTree <- getTree' handle parser
	return $ beginStream' handle parser startStreamEvent featureTree
	where
		streamName = QN.mkNsName "stream" "http://etherx.jabber.org/streams"
		startOfStream depth event = case (depth, event) of
			(1, (XML.BeginElement streamName _)) -> True
			otherwise -> False

beginStream' handle parser streamStart featureTree = let
	-- TODO: parse from streamStart
	host = "localhost"
	language = XMLLanguage "en"
	version = XMPPVersion 1 0
	
	featuresName = QN.mkNsName "features" "http://etherx.jabber.org/streams"
	
	featureRoots = A.runLA (
		A.getChildren
		>>> A.hasQName featuresName) featureTree
	features = case featureRoots of
		[] -> []
		(t:_) -> map parseFeature (A.runLA A.getChildren t)
	
	in Stream handle parser host language version features

parseFeature :: XmlTree -> StreamFeature
parseFeature t = lookupDef FeatureUnknown qname [
	 (("urn:ietf:params:xml:ns:xmpp-tls", "starttls"), parseFeatureTLS)
	,(("urn:ietf:params:xml:ns:xmpp-sasl", "mechanisms"), parseFeatureSASL)
	,(("http://jabber.org/features/iq-register", "register"), (\_ -> FeatureRegister))
	] t
	where
		qname = maybe ("", "") (\n -> (QN.namespaceUri n, QN.localPart n)) (XN.getName t)

parseFeatureTLS :: XmlTree -> StreamFeature
parseFeatureTLS t = FeatureStartTLS True -- TODO: detect whether or not required

parseFeatureSASL :: XmlTree -> StreamFeature
parseFeatureSASL t = let
	mechName = QN.mkNsName "mechanism" "urn:ietf:params:xml:ns:xmpp-sasl"
	rawMechanisms = A.runLA (
		A.getChildren
		>>> A.hasQName mechName
		>>> A.getChildren
		>>> A.getText) t
	
	-- TODO: validate mechanism names according to SASL rules
	-- <20 chars, uppercase, alphanum, etc
	in FeatureSASL (map findMechanism rawMechanisms)

-------------------------------------------------------------------------------

getTree :: Stream -> IO XmlTree
getTree s = getTree' (streamHandle s) (streamParser s)

getTree' :: IO.Handle -> XML.Parser -> IO XmlTree
getTree' h p = do
	events <- readEventsUntil finished h p 1000
	return $ eventsToTree events
	where
		finished 0 (XML.EndElement _) = True
		finished _ _ = False

putTree :: Stream -> XmlTree -> IO ()
putTree s t = do
	let root = XN.mkRoot [] [t]
	let h = streamHandle s
	[text] <- A.runX (A.constA root >>> A.writeDocumentToString [
		(A.a_no_xml_pi, "1")
		])
	IO.hPutStr h text
	IO.hFlush h

-------------------------------------------------------------------------------

readEventsUntil :: (Int -> XML.Event -> Bool) -> IO.Handle -> XML.Parser -> Int -> IO [XML.Event]
readEventsUntil done h parser timeout = readEventsUntil' done 0 [] $ do
	char <- IO.hGetChar h
	XML.incrementalParse parser [char]

readEventsUntil' done depth accum getEvents = do
	events <- getEvents
	let (done', depth', accum') = readEventsStep done events depth accum
	if done'
		then return accum'
		else readEventsUntil' done depth' accum' getEvents

readEventsStep _ [] depth accum = (False, depth, accum)
readEventsStep done (e:es) depth accum = let
	depth' = depth + case e of
		(XML.BeginElement _ _) -> 1
		(XML.EndElement _) -> (- 1)
		otherwise -> 0
	accum' = accum ++ [e]
	in if done depth' e then (True, depth', accum')
	else readEventsStep done es depth' accum'