]> code.delx.au - offlineimap/commitdiff
Starting to work on Connection
authorJohn Goerzen <jgoerzen@complete.org>
Mon, 11 Aug 2008 09:59:55 +0000 (04:59 -0500)
committerJohn Goerzen <jgoerzen@complete.org>
Mon, 11 Aug 2008 09:59:55 +0000 (04:59 -0500)
OfflineIMAP.cabal
src/Network/IMAP/Connection.hs [new file with mode: 0644]

index 7769796c839bf0651c5bcbf761bfda38910b4bb7..164e6a52966e034b4d010fc920991bb8934b1d9d 100644 (file)
@@ -54,7 +54,8 @@ Library
 
   Exposed-Modules: Data.Syncable,
      Network.IMAP.Types,
-     Network.IMAP.Parser
+     Network.IMAP.Parser,
+     Network.IMAP.Connection
 
   If flag(splitBase)
     Build-Depends: base >= 3, directory, random, process, old-time,
diff --git a/src/Network/IMAP/Connection.hs b/src/Network/IMAP/Connection.hs
new file mode 100644 (file)
index 0000000..2a37042
--- /dev/null
@@ -0,0 +1,56 @@
+{- offlineimap component
+Copyright (C) 2008 John Goerzen <jgoerzen@complete.org>
+
+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.Connection where
+import Network.IMAP.Types
+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 =
+    IMAPConnection {readBytes = lreadBytes,
+                    readLine = lreadLine,
+                    writeBytes = lwriteBytes,
+                    closeConn = return ()}
+    where 
+          lreadBytes count = 
+              do (s,sw) <- get
+                 if genericLength s < count
+                    then fail "EOF in input in readBytes"
+                    else do let (r, s') = genericSplitAt count s
+                            put (s', sw)
+                            return r
+          lreadLine count =
+              do (s, sw) <- get
+                 (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
+                           return line
+
+          lwriteBytes outdata =
+              do (s, sw) <- get
+                 put (s, sw ++ outdata)
+