]> code.delx.au - offlineimap/blob - src/Network/IMAP/Parser/Prim.hs
52728c00eb624eb49e17451d0e8c8d95aaf96772
[offlineimap] / src / Network / IMAP / Parser / Prim.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.Parser.Prim where
20 import Text.ParserCombinators.Parsec
21 import Network.IMAP.Types
22
23 type IMAPParser a = CharParser () a
24
25 ----------------------------------------------------------------------
26 -- RFC 2234 (ABNF) declarations
27 ----------------------------------------------------------------------
28
29 -- | RFC 2234
30 ctl :: String
31 ctl = '\x7f' : ['\x00'..'\x1f']
32
33 -- | RFC 2234
34 cr, lf :: Char
35 cr = '\x0d'
36 lf = '\x0a'
37
38 -- | RFC 2234
39 crlf :: String
40 crlf = [cr, lf]
41
42 -- | RFC 2234
43 digit2234 :: String
44 digit2234 = ['0'..'9']
45
46 -- | RFC 2234
47 hexdig :: String
48 hexdig = digit2234 ++ ['A'..'F']
49
50 -- | RFC 2234
51 alpha :: String
52 alpha = ['A'..'Z'] ++ ['a'..'z']
53
54 -- | RFC 2234
55 dquote :: Char
56 dquote = '"'
57
58 -- | RFC 2234
59 char2234 :: String
60 char2234 = ['\x01'..'\x7f']
61
62 -- | RFC 2234
63 sp :: Char
64 sp = ' '
65
66 -- | RFC 2234
67
68 ----------------------------------------------------------------------
69 -- RFC 3501 primitives
70 ----------------------------------------------------------------------
71
72 -- | RFC 3501
73 atomSpecials :: String
74 atomSpecials = "(){ " ++ ctl ++ listWildcards ++ quotedSpecials ++ respSpecials
75
76 listWildcards :: String
77 listWildcards = "%*"
78
79 quotedSpecials :: String
80 quotedSpecials = ['\\', dquote]
81
82 respSpecials :: String
83 respSpecials = "]"
84
85 ----------------------------------------------------------------------
86 -- Parsec RFC 3501 primitives
87 ----------------------------------------------------------------------
88
89 -- | Technically, <any CHAR except atom-specials> but we will
90 -- be more permissive.
91 --
92 -- FIXME: not stated in RFC, but perhaps CRLF is also excluded?
93 atomChar :: IMAPParser Char
94 atomChar = noneOf atomSpecials
95
96 atom :: IMAPParser String
97 atom = many1 atomChar
98
99 astringChar :: IMAPParser Char
100 astringChar = atomChar <|> oneOf respSpecials
101
102 astring :: IMAPParser String
103 astring = many1 astringChar <|> string3501
104
105 string3501 :: IMAPParser String
106 string3501 = quoted <|> literal
107
108 literal :: IMAPParser String
109 literal =
110 do char '{'
111 scount <- many1 digit
112 char '}'
113 string crlf
114 let icount = (read scount)::Int
115 count icount anyChar
116
117 quoted :: IMAPParser String
118 quoted =
119 do char dquote
120 strdata <- many quotedChar
121 char dquote
122 return strdata
123
124 quotedChar :: IMAPParser Char
125 quotedChar =
126 noneOf quotedSpecials <|> (do char '\\'
127 oneOf quotedSpecials
128 )
129
130 -- | Fixme: should exclude 8-bit data per RFC3501
131 textChar :: IMAPParser Char
132 textChar = noneOf crlf
133
134 text :: IMAPParser String
135 text = many1 textChar
136
137 tag :: IMAPParser String
138 tag = many1 tagChar
139 where tagChar = (char '+' >> fail "No + for tag") <|>
140 astringChar