summaryrefslogtreecommitdiff
path: root/Presence/EventUtil.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2014-02-19 16:35:24 -0500
committerjoe <joe@jerkface.net>2014-02-19 16:35:24 -0500
commitac3702dd365691cc9abf37248633f00f1e06cb12 (patch)
tree4f38dcfe2a1dcfd451cf3709f80f88ddf42f6ce6 /Presence/EventUtil.hs
parentefa31511ba65989facc1171983d97a806908606c (diff)
more type signatures
Diffstat (limited to 'Presence/EventUtil.hs')
-rw-r--r--Presence/EventUtil.hs17
1 files changed, 17 insertions, 0 deletions
diff --git a/Presence/EventUtil.hs b/Presence/EventUtil.hs
index 02a37472..908e09e0 100644
--- a/Presence/EventUtil.hs
+++ b/Presence/EventUtil.hs
@@ -4,49 +4,62 @@ module EventUtil where
4import Control.Monad 4import Control.Monad
5import Data.XML.Types as XML 5import Data.XML.Types as XML
6import qualified Data.List as List 6import qualified Data.List as List
7import Data.Text (Text)
7 8
8-- getStreamName (EventBeginElement name _) = name 9-- getStreamName (EventBeginElement name _) = name
9 10
11isEventBeginElement :: Event -> Bool
10isEventBeginElement (EventBeginElement {}) = True 12isEventBeginElement (EventBeginElement {}) = True
11isEventBeginElement _ = False 13isEventBeginElement _ = False
12 14
15isEventEndElement :: Event -> Bool
13isEventEndElement (EventEndElement {}) = True 16isEventEndElement (EventEndElement {}) = True
14isEventEndElement _ = False 17isEventEndElement _ = False
15 18
16-- Note: This function ignores name space qualification 19-- Note: This function ignores name space qualification
20elementAttrs ::
21 MonadPlus m =>
22 Text -> Event -> m [(Name, [Content])]
17elementAttrs expected (EventBeginElement name attrs) 23elementAttrs expected (EventBeginElement name attrs)
18 | nameLocalName name==expected 24 | nameLocalName name==expected
19 = return attrs 25 = return attrs
20elementAttrs _ _ = mzero 26elementAttrs _ _ = mzero
21 27
28streamP :: Text -> Name
22streamP name = Name name (Just "http://etherx.jabber.org/streams") (Just "stream") 29streamP name = Name name (Just "http://etherx.jabber.org/streams") (Just "stream")
23 30
31attr :: Name -> Text -> (Name,[Content])
24attr name value = (name,[ContentText value]) 32attr name value = (name,[ContentText value])
25 33
34isServerIQOf :: Event -> Text -> Bool
26isServerIQOf (EventBeginElement name attrs) testType 35isServerIQOf (EventBeginElement name attrs) testType
27 | name=="{jabber:server}iq" 36 | name=="{jabber:server}iq"
28 && matchAttrib "type" testType attrs 37 && matchAttrib "type" testType attrs
29 = True 38 = True
30isServerIQOf _ _ = False 39isServerIQOf _ _ = False
31 40
41isClientIQOf :: Event -> Text -> Bool
32isClientIQOf (EventBeginElement name attrs) testType 42isClientIQOf (EventBeginElement name attrs) testType
33 | name=="{jabber:client}iq" 43 | name=="{jabber:client}iq"
34 && matchAttrib "type" testType attrs 44 && matchAttrib "type" testType attrs
35 = True 45 = True
36isClientIQOf _ _ = False 46isClientIQOf _ _ = False
37 47
48matchAttrib :: Name -> Text -> [(Name, [Content])] -> Bool
38matchAttrib name value attrs = 49matchAttrib name value attrs =
39 case List.find ( (==name) . fst) attrs of 50 case List.find ( (==name) . fst) attrs of
40 Just (_,[ContentText x]) | x==value -> True 51 Just (_,[ContentText x]) | x==value -> True
41 Just (_,[ContentEntity x]) | x==value -> True 52 Just (_,[ContentEntity x]) | x==value -> True
42 _ -> False 53 _ -> False
43 54
55lookupAttrib :: Name -> [(Name, [Content])] -> Maybe Text
44lookupAttrib name attrs = 56lookupAttrib name attrs =
45 case List.find ( (==name) . fst) attrs of 57 case List.find ( (==name) . fst) attrs of
46 Just (_,[ContentText x]) -> Just x 58 Just (_,[ContentText x]) -> Just x
47 Just (_,[ContentEntity x]) -> Just x 59 Just (_,[ContentEntity x]) -> Just x
48 _ -> Nothing 60 _ -> Nothing
49 61
62tagAttrs :: Event -> [(Name, [Content])]
50tagAttrs (EventBeginElement _ xs) = xs 63tagAttrs (EventBeginElement _ xs) = xs
51tagAttrs _ = [] 64tagAttrs _ = []
52 65
@@ -59,8 +72,12 @@ iqTypeError = "error"
59-} 72-}
60 73
61 74
75tagName :: Event -> Name
62tagName (EventBeginElement n _) = n 76tagName (EventBeginElement n _) = n
63tagName _ = "" 77tagName _ = ""
64 78
79closerFor :: Event -> Event
65closerFor (EventBeginElement n _) = EventEndElement n 80closerFor (EventBeginElement n _) = EventEndElement n
66closerFor _ = error "closerFor: unsupported event" 81closerFor _ = error "closerFor: unsupported event"
82
83