From: John Goerzen Date: Mon, 11 Aug 2008 05:03:21 +0000 (-0500) Subject: Added Parser.hs, working on initial code X-Git-Url: https://code.delx.au/offlineimap/commitdiff_plain/6d02ed50239dab34fbfaadede735fe3d67e40be6 Added Parser.hs, working on initial code --- diff --git a/OfflineIMAP.cabal b/OfflineIMAP.cabal index 28fd5eb..7769796 100644 --- a/OfflineIMAP.cabal +++ b/OfflineIMAP.cabal @@ -50,9 +50,10 @@ Library Build-Depends: haskell98, network, unix, parsec, MissingH>=1.0.0, HDBC>=1.1.0, mtl, base, hslogger, - ConfigFile, filepath, directory, process + ConfigFile, filepath, directory, process, regex-posix Exposed-Modules: Data.Syncable, + Network.IMAP.Types, Network.IMAP.Parser If flag(splitBase) diff --git a/src/Network/IMAP/Parser.hs b/src/Network/IMAP/Parser.hs index 16522f4..7e0b13a 100644 --- a/src/Network/IMAP/Parser.hs +++ b/src/Network/IMAP/Parser.hs @@ -17,5 +17,42 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -} module Network.IMAP.Parser where +import Text.ParserCombinators.Parsec +import Network.IMAP.Types +import Text.Regex.Posix +import Data.Int -import \ No newline at end of file +{- | Read a full response from the server. -} +{- +readFullResponse :: Monad m => + IMAPConnection m -> -- ^ The connection to the server + IMAPString -> -- ^ The tag that we are awaiting + m IMAPString +readFullResponse conn expectedtag = + accumLines [] + where accumLines accum = + do line <- getFullLine [] +-} + +{- | Read a full line from the server, handling any continuation stuff. + +FIXME: for now, we naively assume that any line ending in '}\r\n' is +having a continuation piece. -} + +getFullLine :: Monad m => + IMAPString -> -- ^ The accumulator (empty for first call) + IMAPConnection m -> -- ^ IMAP connection + m IMAPString -- ^ Result + +getFullLine accum conn = + do input <- readLine conn + case checkContinuation input of + Nothing -> return (accum ++ input) + Just (size) -> + do literal <- readBytes conn size + getFullLine (accum ++ input ++ literal) conn + where checkContinuation :: String -> Maybe Int64 + checkContinuation i = + case i =~ "\\{([0-9]+)\\}$" of + [] -> Nothing + x -> Just (read x) diff --git a/src/Network/IMAP/Types.hs b/src/Network/IMAP/Types.hs new file mode 100644 index 0000000..71a4c17 --- /dev/null +++ b/src/Network/IMAP/Types.hs @@ -0,0 +1,32 @@ +{- offlineimap component +Copyright (C) 2008 John Goerzen + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +-} + +module Network.IMAP.Types where +import Data.Int + +-- | The fundamental type of low-level communication +type IMAPString = String + +-- | The low-level IMAP connection type. +data (Monad m) => IMAPConnection m = IMAPConnection { + readBytes :: Int64 -> m IMAPString, -- ^ Read exactly x bytes + readLine :: m IMAPString, -- ^ Read next line, stripping off trailing CRLF + writeBytes :: IMAPString -> m (), -- ^ Write text to server + closeConn :: m () -- ^ Close IMAP connection + } +