]> code.delx.au - offlineimap/blob - src/Network/IMAP/Parser/Prim.hs
Added greeting and the functions needed to support it
[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 ----------------------------------------------------------------------
63 -- RFC 3501 primitives
64 ----------------------------------------------------------------------
65
66 -- | RFC 3501
67 atomSpecials :: String
68 atomSpecials = "(){ " ++ ctl ++ listWildcards ++ quotedSpecials ++ respSpecials
69
70 listWildcards :: String
71 listWildcards = "%*"
72
73 quotedSpecials :: String
74 quotedSpecials = ['\\', dquote]
75
76 respSpecials :: String
77 respSpecials = "]"
78
79 ----------------------------------------------------------------------
80 -- Parsec RFC 3501 primitives
81 ----------------------------------------------------------------------
82
83 -- | Technically, <any CHAR except atom-specials> but we will
84 -- be more permissive.
85 --
86 -- FIXME: not stated in RFC, but perhaps CRLF is also excluded?
87 atomChar :: IMAPParser Char
88 atomChar = noneOf atomSpecials
89
90 atom :: IMAPParser String
91 atom = many1 atomChar
92
93 astringChar :: IMAPParser Char
94 astringChar = atomChar <|> oneOf respSpecials
95
96 astring :: IMAPParser String
97 astring = many1 astringChar <|> string3501
98
99 string3501 :: IMAPParser String
100 string3501 = quoted <|> literal
101
102 literal :: IMAPParser String
103 literal =
104 do char '{'
105 scount <- many1 digit
106 char '}'
107 string crlf
108 let icount = (read scount)::Int
109 count icount anyChar
110
111 quoted :: IMAPParser String
112 quoted =
113 do char dquote
114 strdata <- many quotedChar
115 char dquote
116 return strdata
117
118 quotedChar :: IMAPParser Char
119 quotedChar =
120 noneOf quotedSpecials <|> (do char '\\'
121 oneOf quotedSpecials
122 )
123
124 -- | Fixme: should exclude 8-bit data per RFC3501
125 textChar :: IMAPParser Char
126 textChar = noneOf crlf
127
128 text :: IMAPParser String
129 text = many1 textChar
130
131 tag :: IMAPParser String
132 tag = many1 tagChar
133 where tagChar = (char '+' >> fail "No + for tag") <|>
134 astringChar
135
136 sp :: IMAPParser Char
137 sp = char ' '