@ -62,25 +62,25 @@ import qualified Data.Text as DT (pack, unpack)
@@ -62,25 +62,25 @@ import qualified Data.Text as DT (pack, unpack)
fromString :: String -> Maybe Address
fromString s = fromStrings localpart server part resourcepart
fromString s = fromStrings localpart domain part resourcepart
where
Right ( localpart , server part, resourcepart ) =
Right ( localpart , domain part, resourcepart ) =
parse addressParts " " ( DBC . pack s )
-- |
-- Converts localpart, server part, and resourcepart strings to an XMPP address.
-- Converts localpart, domain part, and resourcepart strings to an XMPP address.
-- Runs the appropriate stringprep profiles and validates the parts.
fromStrings :: Maybe String -> String -> Maybe String -> Maybe Address
fromStrings l s r
| server part == Nothing = Nothing
| otherwise = if validateNonServer part localpart &&
isJust server part' &&
validateNonServer part resourcepart
then Just ( Address localpart ( fromJust server part') resourcepart )
| domain part == Nothing = Nothing
| otherwise = if validateNonDomain part localpart &&
isJust domain part' &&
validateNonDomain part resourcepart
then Just ( Address localpart ( fromJust domain part') resourcepart )
else Nothing
where
@ -92,10 +92,10 @@ fromStrings l s r
@@ -92,10 +92,10 @@ fromStrings l s r
Nothing -> Nothing
Nothing -> Nothing
-- Applies the nameprep profile on the server part string.
-- Applies the nameprep profile on the domain part string.
-- TODO: Allow unassigned?
server part :: Maybe String
server part = case runStringPrep ( namePrepProfile False ) ( DT . pack s ) of
domain part :: Maybe String
domain part = case runStringPrep ( namePrepProfile False ) ( DT . pack s ) of
Just s' -> Just $ DT . unpack s'
Nothing -> Nothing
@ -107,16 +107,16 @@ fromStrings l s r
@@ -107,16 +107,16 @@ fromStrings l s r
Nothing -> Nothing
Nothing -> Nothing
-- Returns the server part if it was a valid IP or if the toASCII
-- Returns the domain part if it was a valid IP or if the toASCII
-- function was successful, or Nothing otherwise.
server part' :: Maybe String
server part' | isIPv4address s || isIPv6address s = Just s
domain part' :: Maybe String
domain part' | isIPv4address s || isIPv6address s = Just s
| otherwise = toASCII s
-- Validates that non-server part strings have an appropriate length.
validateNonServer part :: Maybe String -> Bool
validateNonServer part Nothing = True
validateNonServer part ( Just l ) = validPartLength l
-- Validates that non-domain part strings have an appropriate length.
validateNonDomain part :: Maybe String -> Bool
validateNonDomain part Nothing = True
validateNonDomain part ( Just l ) = validPartLength l
where
validPartLength :: String -> Bool
validPartLength p = length p > 0 && length p < 1024
@ -148,7 +148,7 @@ addressParts = do
@@ -148,7 +148,7 @@ addressParts = do
-- Read until we reach an '@', a '/', or EOF.
a <- many $ noneOf [ '@' , '/' ]
-- Case 1: We found an '@', and thus the localpart. At least the server part
-- Case 1: We found an '@', and thus the localpart. At least the domain part
-- is remaining. Read the '@' and until a '/' or EOF.
do
char '@'
@ -163,13 +163,13 @@ addressParts = do
@@ -163,13 +163,13 @@ addressParts = do
return ( Just a , b , Just c )
-- Case 1B: We have reached EOF; the address is in the form
-- localpart@server part.
-- localpart@domain part.
<|> do
eof
return ( Just a , b , Nothing )
-- Case 2: We found a '/'; the address is in the form
-- server part/resourcepart.
-- domain part/resourcepart.
<|> do
char '/'
b <- many $ anyToken
@ -177,7 +177,7 @@ addressParts = do
@@ -177,7 +177,7 @@ addressParts = do
return ( Nothing , a , Just b )
-- Case 3: We have reached EOF; we have an address consisting of only a
-- server part.
-- domain part.
<|> do
eof
return ( Nothing , a , Nothing )