]> code.delx.au - offlineimap/commitdiff
Added greeting and the functions needed to support it
authorJohn Goerzen <jgoerzen@complete.org>
Tue, 12 Aug 2008 06:38:19 +0000 (01:38 -0500)
committerJohn Goerzen <jgoerzen@complete.org>
Tue, 12 Aug 2008 06:38:19 +0000 (01:38 -0500)
src/Network/IMAP/Parser.hs
src/Network/IMAP/Parser/Prim.hs

index 1158c070321d30cf4e11aad322b69d31a8f15b63..23cf07461a0f89c3c12ef53bf001f86a5e673bde 100644 (file)
@@ -23,6 +23,8 @@ 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
@@ -59,3 +61,52 @@ 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
+                 sp
+                 b <- option "" respTextCodeText
+                 char ']'
+                 sp
+                 return (a ++ " " ++ b)
+          respTextCodeText = many1 (noneOf (']' : crlf))
+                 
\ No newline at end of file
index 52728c00eb624eb49e17451d0e8c8d95aaf96772..68f3875f7a293f8f225351121b2de89602d96f97 100644 (file)
@@ -59,12 +59,6 @@ dquote = '"'
 char2234 :: String
 char2234 = ['\x01'..'\x7f']
 
--- | RFC 2234
-sp :: Char
-sp = ' '
-
--- | RFC 2234
-
 ----------------------------------------------------------------------
 -- RFC 3501 primitives
 ----------------------------------------------------------------------
@@ -137,4 +131,7 @@ text = many1 textChar
 tag :: IMAPParser String
 tag = many1 tagChar
     where tagChar = (char '+' >> fail "No + for tag") <|> 
-                    astringChar
\ No newline at end of file
+                    astringChar
+
+sp :: IMAPParser Char
+sp = char ' '