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.Parser.Prim where
20 import Text.ParserCombinators.Parsec
21 import Network.IMAP.Types
23 type IMAPParser a = CharParser () a
25 ----------------------------------------------------------------------
26 -- RFC 2234 (ABNF) declarations
27 ----------------------------------------------------------------------
31 ctl = '\x7f' : ['\x00'..'\x1f']
44 digit2234 = ['0'..'9']
48 hexdig = digit2234 ++ ['A'..'F']
52 alpha = ['A'..'Z'] ++ ['a'..'z']
60 char2234 = ['\x01'..'\x7f']
62 ----------------------------------------------------------------------
63 -- RFC 3501 primitives
64 ----------------------------------------------------------------------
67 atomSpecials :: String
68 atomSpecials = "(){ " ++ ctl ++ listWildcards ++ quotedSpecials ++ respSpecials
70 listWildcards :: String
73 quotedSpecials :: String
74 quotedSpecials = ['\\', dquote]
76 respSpecials :: String
79 ----------------------------------------------------------------------
80 -- Parsec RFC 3501 primitives
81 ----------------------------------------------------------------------
83 -- | Technically, <any CHAR except atom-specials> but we will
84 -- be more permissive.
86 -- FIXME: not stated in RFC, but perhaps CRLF is also excluded?
87 atomChar :: IMAPParser Char
88 atomChar = noneOf atomSpecials
90 atom :: IMAPParser String
93 astringChar :: IMAPParser Char
94 astringChar = atomChar <|> oneOf respSpecials
96 astring :: IMAPParser String
97 astring = many1 astringChar <|> string3501
99 string3501 :: IMAPParser String
100 string3501 = quoted <|> literal
102 literal :: IMAPParser String
105 scount <- many1 digit
108 let icount = (read scount)::Int
111 quoted :: IMAPParser String
114 strdata <- many quotedChar
118 quotedChar :: IMAPParser Char
120 noneOf quotedSpecials <|> (do char '\\'
124 -- | Fixme: should exclude 8-bit data per RFC3501
125 textChar :: IMAPParser Char
126 textChar = noneOf crlf
128 text :: IMAPParser String
129 text = many1 textChar
131 tag :: IMAPParser String
133 where tagChar = (char '+' >> fail "No + for tag") <|>
136 sp :: IMAPParser Char