diff options
author | joe <joe@jerkface.net> | 2014-02-19 16:35:24 -0500 |
---|---|---|
committer | joe <joe@jerkface.net> | 2014-02-19 16:35:24 -0500 |
commit | ac3702dd365691cc9abf37248633f00f1e06cb12 (patch) | |
tree | 4f38dcfe2a1dcfd451cf3709f80f88ddf42f6ce6 /Presence/EventUtil.hs | |
parent | efa31511ba65989facc1171983d97a806908606c (diff) |
more type signatures
Diffstat (limited to 'Presence/EventUtil.hs')
-rw-r--r-- | Presence/EventUtil.hs | 17 |
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 | |||
4 | import Control.Monad | 4 | import Control.Monad |
5 | import Data.XML.Types as XML | 5 | import Data.XML.Types as XML |
6 | import qualified Data.List as List | 6 | import qualified Data.List as List |
7 | import Data.Text (Text) | ||
7 | 8 | ||
8 | -- getStreamName (EventBeginElement name _) = name | 9 | -- getStreamName (EventBeginElement name _) = name |
9 | 10 | ||
11 | isEventBeginElement :: Event -> Bool | ||
10 | isEventBeginElement (EventBeginElement {}) = True | 12 | isEventBeginElement (EventBeginElement {}) = True |
11 | isEventBeginElement _ = False | 13 | isEventBeginElement _ = False |
12 | 14 | ||
15 | isEventEndElement :: Event -> Bool | ||
13 | isEventEndElement (EventEndElement {}) = True | 16 | isEventEndElement (EventEndElement {}) = True |
14 | isEventEndElement _ = False | 17 | isEventEndElement _ = False |
15 | 18 | ||
16 | -- Note: This function ignores name space qualification | 19 | -- Note: This function ignores name space qualification |
20 | elementAttrs :: | ||
21 | MonadPlus m => | ||
22 | Text -> Event -> m [(Name, [Content])] | ||
17 | elementAttrs expected (EventBeginElement name attrs) | 23 | elementAttrs expected (EventBeginElement name attrs) |
18 | | nameLocalName name==expected | 24 | | nameLocalName name==expected |
19 | = return attrs | 25 | = return attrs |
20 | elementAttrs _ _ = mzero | 26 | elementAttrs _ _ = mzero |
21 | 27 | ||
28 | streamP :: Text -> Name | ||
22 | streamP name = Name name (Just "http://etherx.jabber.org/streams") (Just "stream") | 29 | streamP name = Name name (Just "http://etherx.jabber.org/streams") (Just "stream") |
23 | 30 | ||
31 | attr :: Name -> Text -> (Name,[Content]) | ||
24 | attr name value = (name,[ContentText value]) | 32 | attr name value = (name,[ContentText value]) |
25 | 33 | ||
34 | isServerIQOf :: Event -> Text -> Bool | ||
26 | isServerIQOf (EventBeginElement name attrs) testType | 35 | isServerIQOf (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 |
30 | isServerIQOf _ _ = False | 39 | isServerIQOf _ _ = False |
31 | 40 | ||
41 | isClientIQOf :: Event -> Text -> Bool | ||
32 | isClientIQOf (EventBeginElement name attrs) testType | 42 | isClientIQOf (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 |
36 | isClientIQOf _ _ = False | 46 | isClientIQOf _ _ = False |
37 | 47 | ||
48 | matchAttrib :: Name -> Text -> [(Name, [Content])] -> Bool | ||
38 | matchAttrib name value attrs = | 49 | matchAttrib 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 | ||
55 | lookupAttrib :: Name -> [(Name, [Content])] -> Maybe Text | ||
44 | lookupAttrib name attrs = | 56 | lookupAttrib 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 | ||
62 | tagAttrs :: Event -> [(Name, [Content])] | ||
50 | tagAttrs (EventBeginElement _ xs) = xs | 63 | tagAttrs (EventBeginElement _ xs) = xs |
51 | tagAttrs _ = [] | 64 | tagAttrs _ = [] |
52 | 65 | ||
@@ -59,8 +72,12 @@ iqTypeError = "error" | |||
59 | -} | 72 | -} |
60 | 73 | ||
61 | 74 | ||
75 | tagName :: Event -> Name | ||
62 | tagName (EventBeginElement n _) = n | 76 | tagName (EventBeginElement n _) = n |
63 | tagName _ = "" | 77 | tagName _ = "" |
64 | 78 | ||
79 | closerFor :: Event -> Event | ||
65 | closerFor (EventBeginElement n _) = EventEndElement n | 80 | closerFor (EventBeginElement n _) = EventEndElement n |
66 | closerFor _ = error "closerFor: unsupported event" | 81 | closerFor _ = error "closerFor: unsupported event" |
82 | |||
83 | |||