]> code.delx.au - offlineimap/blob - src/Network/IMAP/Connection.hs
Fixed compilation errors
[offlineimap] / src / Network / IMAP / Connection.hs
1 {- offlineimap component
2 Copyright (C) 2008 John Goerzen <jgoerzen@complete.org>
3
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.
8
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.
13
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
17 -}
18
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)
24
25 -- | Take an IMAPString and treat it as messages from the server.
26 -- | Remember that EOL in IMAP protocols is \r\n!
27
28 stringConnection ::
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,
34 readLine = lreadLine,
35 writeBytes = lwriteBytes,
36 closeConn = return ()}
37 where
38 lreadBytes count =
39 do (s,sw) <- get
40 if genericLength s < count
41 then fail "EOF in input in readBytes"
42 else do let (r, s') = genericSplitAt count s
43 put (s', sw)
44 return r
45 lreadLine =
46 do (s, sw) <- get
47 let (line, remainder) = spanList (\x -> "\r\n" /= take 2 x) s
48 case remainder of
49 [] -> fail "EOF in input in readLine"
50 r -> do put (drop 2 r, sw) -- strip of \r\n
51 return line
52
53 lwriteBytes outdata =
54 do (s, sw) <- get
55 put (s, sw ++ outdata)
56