1 {- offlineimap component
2 Copyright (C) 2008 John Goerzen <jgoerzen@complete.org>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 module Network.IMAP.Connection where
20 import Network.IMAP.Types
21 import Control.Monad.State
22 import Data.List.Utils(spanList)
23 import Data.List(genericSplitAt, genericLength)
25 -- | Take an IMAPString and treat it as messages from the server.
26 -- | Remember that EOL in IMAP protocols is \r\n!
29 IMAPString -> -- ^ The initial content of the buffer for the client to read from
30 IMAPString -> -- ^ The initial content of the buffer for the client to write to
31 IMAPConnection (State (IMAPString, IMAPString))
32 stringConnection sdata wdata =
33 IMAPConnection {readBytes = lreadBytes,
35 writeBytes = lwriteBytes,
36 closeConn = return ()}
40 if genericLength s < count
41 then fail "EOF in input in readBytes"
42 else do let (r, s') = genericSplitAt count s
47 let (line, remainder) = spanList (\x -> "\r\n" /= take 2 x) s
49 [] -> fail "EOF in input in readLine"
50 r -> do put (drop 2 r, sw) -- strip of \r\n
55 put (s, sw ++ outdata)