]> code.delx.au - offlineimap/blob - src/Network/IMAP/Parser.hs
7e0b13a0f030b4875267e97a6fad82d492b3b2a0
[offlineimap] / src / Network / IMAP / Parser.hs
1 {- offlineimap component
2 Copyright (C) 2008 John Goerzen <jgoerzen@complete.org>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 -}
18
19 module Network.IMAP.Parser where
20 import Text.ParserCombinators.Parsec
21 import Network.IMAP.Types
22 import Text.Regex.Posix
23 import Data.Int
24
25 {- | Read a full response from the server. -}
26 {-
27 readFullResponse :: Monad m =>
28 IMAPConnection m -> -- ^ The connection to the server
29 IMAPString -> -- ^ The tag that we are awaiting
30 m IMAPString
31 readFullResponse conn expectedtag =
32 accumLines []
33 where accumLines accum =
34 do line <- getFullLine []
35 -}
36
37 {- | Read a full line from the server, handling any continuation stuff.
38
39 FIXME: for now, we naively assume that any line ending in '}\r\n' is
40 having a continuation piece. -}
41
42 getFullLine :: Monad m =>
43 IMAPString -> -- ^ The accumulator (empty for first call)
44 IMAPConnection m -> -- ^ IMAP connection
45 m IMAPString -- ^ Result
46
47 getFullLine accum conn =
48 do input <- readLine conn
49 case checkContinuation input of
50 Nothing -> return (accum ++ input)
51 Just (size) ->
52 do literal <- readBytes conn size
53 getFullLine (accum ++ input ++ literal) conn
54 where checkContinuation :: String -> Maybe Int64
55 checkContinuation i =
56 case i =~ "\\{([0-9]+)\\}$" of
57 [] -> Nothing
58 x -> Just (read x)