Browse Source

Merge branch 'master' of git://github.com/jonkri/pontarius-xmpp into jonkri-master

Conflicts:
	source/Network/Xmpp.hs
	source/Network/Xmpp/Types.hs
master
Jon Kristensen 13 years ago
parent
commit
762bca6e83
  1. 1
      source/Network/Xmpp.hs
  2. 41
      source/Network/Xmpp/Types.hs

1
source/Network/Xmpp.hs

@ -50,6 +50,7 @@ module Network.Xmpp
, jidFromTexts , jidFromTexts
, jidToText , jidToText
, jidToTexts , jidToTexts
, parseJid
, getJid , getJid
-- * Stanzas -- * Stanzas
-- | The basic protocol data unit in XMPP is the XML stanza. The stanza is -- | The basic protocol data unit in XMPP is the XML stanza. The stanza is

41
source/Network/Xmpp/Types.hs

@ -49,6 +49,7 @@ module Network.Xmpp.Types
, jidFromTexts , jidFromTexts
, jidToText , jidToText
, jidToTexts , jidToTexts
, parseJid
, StreamEnd(..) , StreamEnd(..)
, InvalidXmppXml(..) , InvalidXmppXml(..)
, SessionConfiguration(..) , SessionConfiguration(..)
@ -888,12 +889,12 @@ data Jid = Jid { -- | The @localpart@ of a JID is an optional identifier placed
-- the entity associated with an XMPP localpart at a domain -- the entity associated with an XMPP localpart at a domain
-- (i.e., @localpart\@domainpart/resourcepart@). -- (i.e., @localpart\@domainpart/resourcepart@).
, resourcepart :: !(Maybe Text) , resourcepart :: !(Maybe Text)
} deriving (Eq, Ord, Show) -- A `Read' instance must validate. } deriving (Eq, Ord)
-- | Converts a JID to a Text. -- | Converts a JID to a Text.
jidToText :: Jid -> String jidToText :: Jid -> Text
jidToText (Jid nd dmn res) = jidToText (Jid nd dmn res) =
(maybe "" ((++ "@") . Text.unpack) nd) ++ (Text.unpack dmn) ++ Text.pack $ (maybe "" ((++ "@") . Text.unpack) nd) ++ (Text.unpack dmn) ++
maybe "" (('/' :) . Text.unpack) res maybe "" (('/' :) . Text.unpack) res
-- | Converts a JID to up to three Text values: (the optional) localpart, the -- | Converts a JID to up to three Text values: (the optional) localpart, the
@ -901,6 +902,40 @@ jidToText (Jid nd dmn res) =
jidToTexts :: Jid -> (Maybe Text, Text, Maybe Text) jidToTexts :: Jid -> (Maybe Text, Text, Maybe Text)
jidToTexts (Jid nd dmn res) = (nd, dmn, res) jidToTexts (Jid nd dmn res) = (nd, dmn, res)
-- Produces a Jid value in the format "parseJid \"<jid>\"".
instance Show Jid where
show (Jid nd dmn res) =
"parseJid \"" ++ maybe "" ((++ "@") . Text.unpack) nd ++ Text.unpack dmn ++
maybe "" (('/' :) . Text.unpack) res ++ "\""
-- The string must be in the format "parseJid \"<jid>\"".
-- TODO: This function should produce its error values in a uniform way.
-- TODO: Do we need to care about precedence here?
instance Read Jid where
readsPrec _ s = do
-- Verifies that the first word is "parseJid", parses the second word and
-- the remainder, if any, and produces these two values or fails.
let (s', r) = case lex s of
[] -> error "Expected `parseJid \"<jid>\"'"
[("parseJid", r')] -> case lex r' of
[] -> error "Expected `parseJid \"<jid>\"'"
[(s', r)] -> (s', r)
_ -> error "Expected `parseJid \"<jid>\"'"
_ -> error "Expected `parseJid \"<jid>\"'"
-- Read the JID string (removes the quotes), validate, and return.
[(parseJid (read s' :: String), r)] -- May fail with "Prelude.read: no parse"
-- or the `parseJid' error message (see below)
-- | Parses a JID string.
--
-- Note: This function is only meant to be used to reverse @Jid@ Show
-- operations; it will produce an 'undefined' value if the JID does not
-- validate; please refer to @jidFromText@ for a safe equivalent.
parseJid :: String -> Jid
parseJid s = case jidFromText $ Text.pack s of
Just jid -> jid
Nothing -> error $ "Jid value (" ++ s ++ ") did not validate"
instance IsString Jid where instance IsString Jid where
fromString = fromJust . jidFromText . Text.pack fromString = fromJust . jidFromText . Text.pack

Loading…
Cancel
Save