]> code.delx.au - offlineimap/blobdiff - src/Network/IMAP/Connection.hs
Comment typo
[offlineimap] / src / Network / IMAP / Connection.hs
index bd1b4547dad4d799e32400460f60092c5b3dfcb3..2c06d20451cdc758515e9d415ca2bec54dae1474 100644 (file)
@@ -20,7 +20,9 @@ module Network.IMAP.Connection where
 import Network.IMAP.Types
 import Control.Monad.State
 import Data.List.Utils(spanList)
-import Data.List(genericSplitAt, genericLength)
+import Data.List(genericSplitAt, genericLength, intercalate)
+
+type IMAPState = State (IMAPString, IMAPString)
 
 {- | Set up an IMAPConnection that runs in the State monad.
 
@@ -29,7 +31,7 @@ The state is (bufferToClient, bufferFromClient)
 Remember that EOL in IMAP protocols is \r\n!
 
 closeConnection is ignored with this monad. -}
-newStringConnection :: IMAPConnection (State (IMAPString, IMAPString))
+newStringConnection :: IMAPConnection IMAPState
 newStringConnection =
     IMAPConnection {readBytes = lreadBytes,
                     readLine = lreadLine,
@@ -48,7 +50,7 @@ newStringConnection =
                  let (line, remainder) = spanList (\x -> "\r\n" /= take 2 x) s
                  case remainder of
                    [] -> fail "EOF in input in readLine"
-                   r -> do put (drop 2 r, sw) -- strip of \r\n
+                   r -> do put (drop 2 r, sw) -- strip off \r\n
                            return line
 
           lwriteBytes outdata =
@@ -57,15 +59,23 @@ newStringConnection =
 
 {- | Runs a State monad with a String connection.  Returns
 (retval, remainingBufferToClient, bufferFromClient) -}
-{-
-runStringConnection :: 
-    IMAPString -> 
-    ((State s a) -> a) ->
-    (a, IMAPString, IMAPString)
--}
 runStringConnection ::
        IMAPString               -- ^ Buffer to send to clients
-    -> (IMAPConnection (State (IMAPString, IMAPString)) -> State (IMAPString, IMAPString) a) -- ^ Function to run
+    -> (IMAPConnection IMAPState -> IMAPState a) -- ^ Function to run
     -> (a, (String, String))    -- ^ Results: func result, buffer status
 runStringConnection sbuf func =
     runState (func newStringConnection) (sbuf::String, []::String)
+
+{- | Runs a State monad with a String connection, initializing it with 
+the passed lines.  -}
+runLinesConnection ::
+       [IMAPString]             -- ^ Buffer to send to clients
+    -> (IMAPConnection IMAPState -> IMAPState a) -- ^ Function to run
+    -> (a, (String, String))    -- ^ Results: func result, buffer status
+runLinesConnection sbuf func 
+    | sbuf == [] = 
+        -- For the empty input, no \r\n after.
+        runStringConnection [] func
+    | otherwise = 
+        -- Put \r\n between the lines, and also after the last one.
+        runStringConnection (intercalate "\r\n" sbuf ++ "\r\n") func
\ No newline at end of file