]> code.delx.au - offlineimap/blobdiff - src/Network/IMAP/Connection.hs
Refine the State-based connection
[offlineimap] / src / Network / IMAP / Connection.hs
index 2a370421f6a4a0beffa462cb18bfd2c286150247..bd1b4547dad4d799e32400460f60092c5b3dfcb3 100644 (file)
@@ -22,14 +22,15 @@ import Control.Monad.State
 import Data.List.Utils(spanList)
 import Data.List(genericSplitAt, genericLength)
 
--- | Take an IMAPString and treat it as messages from the server.
--- | Remember that EOL in IMAP protocols is \r\n!
-
-stringConnection :: 
-    IMAPString ->               -- ^ The initial content of the buffer for the client to read from
-    IMAPString ->               -- ^ The initial content of the buffer for the client to write to
-    IMAPConnection (State (IMAPString, IMAPString))
-stringConnection sdata wdata =
+{- | Set up an IMAPConnection that runs in the State monad.
+
+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 {readBytes = lreadBytes,
                     readLine = lreadLine,
                     writeBytes = lwriteBytes,
@@ -42,9 +43,9 @@ stringConnection sdata wdata =
                     else do let (r, s') = genericSplitAt count s
                             put (s', sw)
                             return r
-          lreadLine count =
+          lreadLine =
               do (s, sw) <- get
-                 (line, remainder) <- spanList (\x -> "\r\n" /= take 2 x) s
+                 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
@@ -54,3 +55,17 @@ stringConnection sdata wdata =
               do (s, sw) <- get
                  put (s, sw ++ outdata)
 
+{- | 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
+    -> (a, (String, String))    -- ^ Results: func result, buffer status
+runStringConnection sbuf func =
+    runState (func newStringConnection) (sbuf::String, []::String)