-
Notifications
You must be signed in to change notification settings - Fork 56
Quote and encode IMAP mailbox names #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mpscholten
wants to merge
1
commit into
qnikst:master
Choose a base branch
from
mpscholten:upstream/imap-mailbox-quoting
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| module Network.HaskellNet.IMAP.UTF7 | ||
| ( encodeMailboxName | ||
| , decodeMailboxName | ||
| ) | ||
| where | ||
|
|
||
| import Data.Bits | ||
| import qualified Data.ByteString as B | ||
| import Data.Char (ord) | ||
| import Data.Word (Word8) | ||
| import qualified Data.Text as Text | ||
| import qualified Data.Text.Encoding as TextEncoding | ||
| import qualified Data.Text.Encoding.Error as TextEncodingError | ||
|
|
||
| encodeMailboxName :: String -> String | ||
| encodeMailboxName [] = [] | ||
| encodeMailboxName ('&':cs) = "&-" ++ encodeMailboxName cs | ||
| encodeMailboxName cs@(c:rest') | ||
| | isDirect c = c : encodeMailboxName rest' | ||
| | otherwise = | ||
| let (encoded, rest) = span isShifted cs | ||
| in '&' : encodeBase64 (B.unpack (TextEncoding.encodeUtf16BE (Text.pack encoded))) | ||
| ++ "-" ++ encodeMailboxName rest | ||
| where | ||
| isShifted ch = ch /= '&' && not (isDirect ch) | ||
|
|
||
| decodeMailboxName :: String -> String | ||
| decodeMailboxName [] = [] | ||
| decodeMailboxName ('&':'-':cs) = '&' : decodeMailboxName cs | ||
| decodeMailboxName ('&':cs) = | ||
| let (encoded, rest) = break (== '-') cs | ||
| rest' = dropDash rest | ||
| shifted = '&' : encoded ++ dash rest | ||
| in case rest of | ||
| '-' : _ -> | ||
| case decodeBase64 encoded of | ||
| Just decoded | ||
| | even (length decoded) -> | ||
| Text.unpack (TextEncoding.decodeUtf16BEWith | ||
| TextEncodingError.lenientDecode | ||
| (B.pack decoded)) | ||
| ++ decodeMailboxName rest' | ||
| _ -> shifted ++ decodeMailboxName rest' | ||
| _ -> shifted ++ decodeMailboxName rest' | ||
| where | ||
| dropDash ('-':rest) = rest | ||
| dropDash rest = rest | ||
| dash ('-':_) = "-" | ||
| dash _ = "" | ||
| decodeMailboxName (c:cs) = c : decodeMailboxName cs | ||
|
|
||
| isDirect :: Char -> Bool | ||
| isDirect c = ord c >= 0x20 && ord c <= 0x7e && c /= '&' | ||
|
|
||
| alphabet :: String | ||
| alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+," | ||
|
|
||
| encodeBase64 :: [Word8] -> String | ||
| encodeBase64 [] = [] | ||
| encodeBase64 [a] = | ||
| [ alphabetAt (fromIntegral a `shiftR` 2) | ||
| , alphabetAt ((fromIntegral a .&. 0x03) `shiftL` 4) | ||
| ] | ||
| encodeBase64 [a, b] = | ||
| [ alphabetAt (fromIntegral a `shiftR` 2) | ||
| , alphabetAt (((fromIntegral a .&. 0x03) `shiftL` 4) .|. (fromIntegral b `shiftR` 4)) | ||
| , alphabetAt ((fromIntegral b .&. 0x0f) `shiftL` 2) | ||
| ] | ||
| encodeBase64 (a:b:c:rest) = | ||
| [ alphabetAt (fromIntegral a `shiftR` 2) | ||
| , alphabetAt (((fromIntegral a .&. 0x03) `shiftL` 4) .|. (fromIntegral b `shiftR` 4)) | ||
| , alphabetAt (((fromIntegral b .&. 0x0f) `shiftL` 2) .|. (fromIntegral c `shiftR` 6)) | ||
| , alphabetAt (fromIntegral c .&. 0x3f) | ||
| ] ++ encodeBase64 rest | ||
|
|
||
| alphabetAt :: Int -> Char | ||
| alphabetAt n = alphabet !! n | ||
|
|
||
| decodeBase64 :: String -> Maybe [Word8] | ||
| decodeBase64 [] = Just [] | ||
| decodeBase64 chars = | ||
| let (chunk, rest) = splitAt 4 chars | ||
| in do decodedChunk <- traverse base64Value chunk >>= decodeChunk | ||
| decodedRest <- decodeBase64 rest | ||
| return (decodedChunk ++ decodedRest) | ||
|
|
||
| decodeChunk :: [Int] -> Maybe [Word8] | ||
| decodeChunk [a, b] = | ||
| Just [fromIntegral $ (a `shiftL` 2) .|. (b `shiftR` 4)] | ||
| decodeChunk [a, b, c] = | ||
| Just [ fromIntegral $ (a `shiftL` 2) .|. (b `shiftR` 4) | ||
| , fromIntegral $ ((b .&. 0x0f) `shiftL` 4) .|. (c `shiftR` 2) | ||
| ] | ||
| decodeChunk [a, b, c, d] = | ||
| Just [ fromIntegral $ (a `shiftL` 2) .|. (b `shiftR` 4) | ||
| , fromIntegral $ ((b .&. 0x0f) `shiftL` 4) .|. (c `shiftR` 2) | ||
| , fromIntegral $ ((c .&. 0x03) `shiftL` 6) .|. d | ||
| ] | ||
| decodeChunk _ = Nothing | ||
|
|
||
| base64Value :: Char -> Maybe Int | ||
| base64Value c = | ||
| case lookup c (zip alphabet [0..]) of | ||
| Just n -> Just n | ||
| Nothing -> Nothing |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
mboxcontains a space, this now sends a valid quoted STATUS command, but conforming servers echo that mailbox as a string, e.g.* STATUS "foo bar" (...);pStatusLinestill discards the name withanyCharmanyTillspace, so it stops inside the quoted name and then fails to parse the stats. In that scenariostatus conn "foo bar" ...throws a parse error instead of returning the status; the parser needs to consume an IMAP astring/quoted mailbox before the status list.Useful? React with 👍 / 👎.