]> code.delx.au - offlineimap/blobdiff - src/Network/IMAP/Parser.hs
Working on respText
[offlineimap] / src / Network / IMAP / Parser.hs
index c8cf6cac9a4981bec4da700d61c97079679d040e..e945d5efad4d23b16a6649d1c1426a6945e2dc00 100644 (file)
@@ -21,18 +21,21 @@ import Text.ParserCombinators.Parsec
 import Network.IMAP.Types
 import Text.Regex.Posix
 import Data.Int
+import Data.List
+
+import Network.IMAP.Parser.Prim
 
 {- | 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 =
+readFullResponse conn =
     accumLines []
     where accumLines accum = 
-              do line <- getFullLine []
--}
+              do line <- getFullLine [] conn
+                 if "* " `isPrefixOf` line
+                    then accumLines (accum ++ line ++ "\r\n")
+                    else return (accum ++ line ++ "\r\n")
 
 {- | Read a full line from the server, handling any continuation stuff.
 
@@ -58,3 +61,51 @@ getFullLine accum conn =
               case i =~ "\\{([0-9]+)\\}$" :: (String, String, String, [String]) of
                 (_, _, _, [x]) -> Just (read x)
                 _ -> Nothing
+
+----------------------------------------------------------------------
+-- Response parsing
+----------------------------------------------------------------------
+
+{- | Returns Left for a "BYE" response, or Right if we are ready to
+proceed with auth (or preauth). -}
+greeting :: IMAPParser (Either RespText (AuthReady, RespText))
+greeting =
+    do string "* "
+       (respCondBye >>= return . Left) <|>
+          (respCondAuth >>= return . Right)
+
+data AuthReady = AUTHOK | AUTHPREAUTH
+          deriving (Eq, Read, Show)
+
+data RespText = RespText {respTextCode :: Maybe String,
+                           respTextMsg :: String}
+                 deriving (Eq, Read, Show)
+
+respCondAuth :: IMAPParser (AuthReady, RespText)
+respCondAuth =
+    do s <- (string "OK" >> return AUTHOK) <|>
+            (string "PREAUTH" >> return AUTHPREAUTH)
+       sp
+       t <- respText
+       return (s, t)
+
+respCondBye :: IMAPParser RespText
+respCondBye =
+    do string "BYE "
+       respText
+
+-- Less strict than mandated in RFC3501 formal syntax
+respText :: IMAPParser RespText
+respText =
+    do code <- optionMaybe respTextCode
+       t <- text
+       return $ RespText code t
+    where respTextCode =
+              do char '['
+                 a <- atom
+                 b <- option "" (sp >> respTextCodeText)
+                 char ']'
+                 sp
+                 return (a ++ " " ++ b)
+          respTextCodeText = many1 (noneOf (']' : crlf))
+                 
\ No newline at end of file