~singpolyma/jingle-xmpp

jingle-xmpp/Util.hs -rw-r--r-- 3.8 KiB
4c93bbd1Stephen Paul Weber Loosen clock 11 months ago
                                                                                
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
module Util where

import Prelude ()
import BasicPrelude
import Control.Concurrent
	(ThreadId, forkFinally, myThreadId, throwTo)
import Data.Void                       (absurd)
import qualified Control.Exception     as Ex
import qualified Data.XML.Types        as XML
import qualified Network.Protocol.XMPP as XMPP
import           UnexceptionalIO       (Unexceptional)
import qualified UnexceptionalIO       as UIO

newtype JingleSID = JingleSID Text deriving (Show, Ord, Eq)
newtype JingleTSID = JingleTSID Text deriving (Show, Ord, Eq)

s :: (IsString s) => String -> s
s = fromString

fromIO_ :: (Unexceptional m) => IO a -> m a
fromIO_ = fmap (either absurd id) . UIO.fromIO' (error . show)

castException :: (Ex.Exception e1, Ex.Exception e2) => e1 -> Maybe e2
castException = Ex.fromException . Ex.toException

-- Re-throws all by ThreadKilled async to parent thread
-- Makes sync child exceptions async in parent, which is a bit sloppy
forkXMPP :: XMPP.XMPP () -> XMPP.XMPP ThreadId
forkXMPP kid = do
	parent <- liftIO myThreadId
	session <- XMPP.getSession
	liftIO $ forkFinally
		(void $ XMPP.runXMPP session kid)
		(either (handler parent) (const $ return ()))
	where
	handler parent e
		| Just Ex.ThreadKilled <- castException e = return ()
		| otherwise = throwTo parent e

iqReply :: Maybe XML.Element -> XMPP.IQ -> XMPP.IQ
iqReply payload iq = iq {
	XMPP.iqType = XMPP.IQResult,
	XMPP.iqFrom = XMPP.iqTo iq,
	XMPP.iqTo = XMPP.iqFrom iq,
	XMPP.iqPayload = payload
}

iqError :: XML.Element -> XMPP.IQ -> XMPP.IQ
iqError payload iq = (iqReply (Just payload) iq) {
	XMPP.iqType = XMPP.IQError
}

iqNewRequest :: XMPP.IQ -> XMPP.IQType -> Text -> XML.Element -> XMPP.IQ
iqNewRequest iq iqtype iqid payload = (XMPP.emptyIQ iqtype) {
		XMPP.iqTo = XMPP.iqFrom iq,
		XMPP.iqFrom = XMPP.iqTo iq,
		XMPP.iqID = Just iqid,
		XMPP.iqPayload = Just payload
	}

messageError :: XML.Element -> XMPP.Message -> XMPP.Message
messageError payload message = message {
	XMPP.messageType = XMPP.MessageError,
	XMPP.messageFrom = XMPP.messageTo message,
	XMPP.messageTo = XMPP.messageFrom message,
	XMPP.messagePayloads = payload : XMPP.messagePayloads message
}

notImplemented :: XML.Element
notImplemented =
	errorPayload "cancel" "feature-not-implemented" (s"Unknown request") []

overChildrenOf :: (XML.Element -> [a]) -> [XML.Element] -> [a]
overChildrenOf f el = f =<< XML.elementChildren =<< el

elementAttributeText :: XML.Name -> XML.Name -> XML.Element -> [Text]
elementAttributeText = (.: XML.isNamed) . mapMaybe . XML.attributeText

child :: (XMPP.Stanza s) => XML.Name -> s -> Maybe XML.Element
child name = listToMaybe .
	(XML.isNamed name <=< XMPP.stanzaPayloads)

errorChild :: (XMPP.Stanza s) => s -> Maybe XML.Element
errorChild = child (s"{jabber:component:accept}error")

errorPayload :: String -> String -> Text -> [XML.Node] -> XML.Element
errorPayload typ definedCondition english morePayload =
	XML.Element (s"{jabber:component:accept}error")
	[(s"type", [XML.ContentText $ fromString typ])]
	(
		(
			XML.NodeElement $ XML.Element definedConditionName [] []
		) :
		(
			XML.NodeElement $ XML.Element
				(s"{urn:ietf:params:xml:ns:xmpp-stanzas}text")
				[(s"xml:lang", [XML.ContentText $ s"en"])]
				[XML.NodeContent $ XML.ContentText english]
		) :
		morePayload
	)
	where
	definedConditionName = fromString $
		"{urn:ietf:params:xml:ns:xmpp-stanzas}" ++ definedCondition

mkDiscoIdentity :: Text -> Text -> Text -> XML.Element
mkDiscoIdentity category typ name =
	XML.Element (s"{http://jabber.org/protocol/disco#info}identity") [
		(s"category", [XML.ContentText category]),
		(s"type", [XML.ContentText typ]),
		(s"name", [XML.ContentText name])
	] []

mkDiscoFeature :: Text -> XML.Element
mkDiscoFeature var =
	XML.Element (s"{http://jabber.org/protocol/disco#info}feature") [
		(s"var", [XML.ContentText var])
	] []

infixr 9 .:
(.:) :: (c -> d) -> (a -> b -> c) -> a -> b -> d
(.:) = (.) . (.)