Bug #606181 - Accepting bad SSL certificate applies to any hostname
[platform/upstream/evolution-data-server.git] / camel / camel-imapx-utils.h
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU Lesser General Public
7  * License as published by the Free Software Foundation.
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 GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #if !defined (__CAMEL_H_INSIDE__) && !defined (CAMEL_COMPILATION)
21 #error "Only <camel/camel.h> can be included directly."
22 #endif
23
24
25 #ifndef CAMEL_IMAPX_UTILS_H
26 #define CAMEL_IMAPX_UTILS_H
27
28 #include <camel/camel-store.h>
29
30 struct _CamelIMAPXStream;
31 struct _CamelFlag;
32 struct _CamelIMAPXNamespaceList;
33 struct _CamelIMAPXStore;
34
35 /* list of strings we know about that can be *quickly* tokenised */
36 typedef enum _camel_imapx_id_t {
37         IMAPX_UNKNOWN = 0,
38         IMAPX_ALERT,
39         IMAPX_APPENDUID,
40         IMAPX_BAD,
41         IMAPX_BODY,
42         IMAPX_BODYSTRUCTURE,
43         IMAPX_BYE,
44         IMAPX_CAPABILITY,
45         IMAPX_CLOSED,
46         IMAPX_COPYUID,
47         IMAPX_ENVELOPE,
48         IMAPX_EXISTS,
49         IMAPX_EXPUNGE,
50         IMAPX_FETCH,
51         IMAPX_FLAGS,
52         IMAPX_HIGHESTMODSEQ,
53         IMAPX_INTERNALDATE,
54         IMAPX_LIST,
55         IMAPX_LSUB,
56         IMAPX_MESSAGES,
57         IMAPX_MODSEQ,
58         IMAPX_NAMESPACE,
59         IMAPX_NEWNAME,
60         IMAPX_NO,
61         IMAPX_NOMODSEQ,
62         IMAPX_OK,
63         IMAPX_PARSE,
64         IMAPX_PERMANENTFLAGS,
65         IMAPX_PREAUTH,
66         IMAPX_READ_ONLY,
67         IMAPX_READ_WRITE,
68         IMAPX_RECENT,
69         IMAPX_RFC822_HEADER,
70         IMAPX_RFC822_SIZE,
71         IMAPX_RFC822_TEXT,
72         IMAPX_STATUS,
73         IMAPX_TRYCREATE,
74         IMAPX_UID,
75         IMAPX_UIDVALIDITY,
76         IMAPX_UNSEEN,
77         IMAPX_UIDNEXT,
78         IMAPX_VANISHED,
79 } camel_imapx_id_t;
80
81 /* str MUST be in upper case, tokenised using gperf function */
82 camel_imapx_id_t imapx_tokenise (register const gchar *str, register guint len);
83
84 /* this flag should be part of imapfoldersummary */
85 enum {
86         CAMEL_IMAPX_MESSAGE_RECENT = (1 << 21),
87 };
88
89 /* ********************************************************************** */
90
91 GPtrArray *imapx_parse_uids (struct _CamelIMAPXStream *is, GCancellable *cancellable, GError **error);
92 void imapx_parse_flags (struct _CamelIMAPXStream *stream, guint32 *flagsp, struct _CamelFlag **user_flagsp, GCancellable *cancellable, GError **error);
93 void imapx_write_flags (GString *string, guint32 flags, struct _CamelFlag *user_flags);
94 gboolean imapx_update_message_info_flags (CamelMessageInfo *info, guint32 server_flags, CamelFlag *server_user_flags, guint32 permanent_flags, CamelFolder *folder, gboolean unsolicited);
95 void imapx_set_message_info_flags_for_new_message (CamelMessageInfo *info, guint32 server_flags, CamelFlag *server_user_flags,
96                                                         CamelFolder *folder);
97 void imapx_update_store_summary (CamelFolder *folder);
98
99 /* ********************************************************************** */
100 enum {
101         IMAPX_CAPABILITY_IMAP4                  = (1 << 0),
102         IMAPX_CAPABILITY_IMAP4REV1              = (1 << 1),
103         IMAPX_CAPABILITY_STATUS                 = (1 << 2),
104         IMAPX_CAPABILITY_NAMESPACE              = (1 << 3),
105         IMAPX_CAPABILITY_UIDPLUS                = (1 << 4),
106         IMAPX_CAPABILITY_LITERALPLUS            = (1 << 5),
107         IMAPX_CAPABILITY_STARTTLS               = (1 << 6),
108         IMAPX_CAPABILITY_IDLE                   = (1 << 7),
109         IMAPX_CAPABILITY_CONDSTORE              = (1 << 8),
110         IMAPX_CAPABILITY_QRESYNC                = (1 << 9),
111         IMAPX_CAPABILITY_LIST_STATUS            = (1 << 10),
112         IMAPX_CAPABILITY_LIST_EXTENDED          = (1 << 11),
113 };
114
115 struct _capability_info {
116         guint32 capa;
117         GHashTable *auth_types;
118 };
119
120 struct _capability_info *imapx_parse_capability (struct _CamelIMAPXStream *stream, GCancellable *cancellable, GError **error);
121 void imapx_free_capability (struct _capability_info *);
122
123 gboolean imapx_parse_param_list (struct _CamelIMAPXStream *is, struct _camel_header_param **plist, GCancellable *cancellable, GError **error) /* IO,PARSE */;
124 struct _CamelContentDisposition *imapx_parse_ext_optional (struct _CamelIMAPXStream *is, GCancellable *cancellable, GError **error) /* IO,PARSE */;
125 struct _CamelMessageContentInfo *imapx_parse_body_fields (struct _CamelIMAPXStream *is, GCancellable *cancellable, GError **error) /* IO,PARSE */;
126 struct _camel_header_address *imapx_parse_address_list (struct _CamelIMAPXStream *is, GCancellable *cancellable, GError **error) /* IO,PARSE */;
127 struct _CamelMessageInfo *imapx_parse_envelope (struct _CamelIMAPXStream *is, GCancellable *cancellable, GError **error) /* IO, PARSE */;
128 struct _CamelMessageContentInfo *imapx_parse_body (struct _CamelIMAPXStream *is, GCancellable *cancellable, GError **error) /* IO,PARSE */;
129 gchar *imapx_parse_section (struct _CamelIMAPXStream *is, GCancellable *cancellable, GError **error) /* IO,PARSE */;
130 void imapx_free_body (struct _CamelMessageContentInfo *cinfo);
131
132 /* ********************************************************************** */
133 /* all the possible stuff we might get from a fetch request */
134 /* this assumes the caller/server doesn't send any one of these types twice */
135 struct _fetch_info {
136         guint32 got;            /* what we got, see below */
137         CamelStream *body;      /* BODY[.*](<.*>)? */
138         CamelStream *text;      /* RFC822.TEXT */
139         CamelStream *header;    /* RFC822.HEADER */
140         struct _CamelMessageInfo *minfo; /* ENVELOPE */
141         struct _CamelMessageContentInfo *cinfo; /* BODYSTRUCTURE,BODY */
142         guint32 size;           /* RFC822.SIZE */
143         guint32 offset;         /* start offset of a BODY[]<offset.length> request */
144         guint32 flags;          /* FLAGS */
145         guint64 modseq;         /* MODSEQ */
146         struct _CamelFlag *user_flags;
147         gchar *date;            /* INTERNALDATE */
148         gchar *section;         /* section for a BODY[section] request */
149         gchar *uid;             /* UID */
150 };
151
152 #define FETCH_BODY (1 << 0)
153 #define FETCH_TEXT (1 << 1)
154 #define FETCH_HEADER (1 << 2)
155 #define FETCH_MINFO (1 << 3)
156 #define FETCH_CINFO (1 << 4)
157 #define FETCH_SIZE (1 << 5)
158 #define FETCH_OFFSET (1 << 6)
159 #define FETCH_FLAGS (1 << 7)
160 #define FETCH_DATE (1 << 8)
161 #define FETCH_SECTION (1 << 9)
162 #define FETCH_UID (1 << 10)
163 #define FETCH_MODSEQ (1 << 11)
164
165 struct _fetch_info *imapx_parse_fetch (struct _CamelIMAPXStream *is, GCancellable *cancellable, GError **error);
166 void imapx_free_fetch (struct _fetch_info *finfo);
167 void imapx_dump_fetch (struct _fetch_info *finfo);
168
169 /* ********************************************************************** */
170
171 struct _status_info {
172         camel_imapx_id_t result; /* ok/no/bad/preauth only, user_cancel - client response */
173         camel_imapx_id_t condition; /* read-only/read-write/alert/parse/trycreate/newname/permanentflags/uidvalidity/unseen/highestmodseq */
174
175         union {
176                 struct {
177                         gchar *oldname;
178                         gchar *newname;
179                 } newname;
180                 guint32 permanentflags;
181                 guint64 uidvalidity;
182                 guint32 uidnext;
183                 guint32 unseen;
184                 guint64 highestmodseq;
185                 struct {
186                         guint64 uidvalidity;
187                         guint32 uid;
188                 } appenduid;
189                 struct {
190                         guint64 uidvalidity;
191                         GPtrArray *uids;
192                         GPtrArray *copied_uids;
193                 } copyuid;
194                 struct _capability_info *cinfo;
195         } u;
196
197         gchar *text;
198 };
199
200 struct _status_info *imapx_parse_status (struct _CamelIMAPXStream *is, GCancellable *cancellable, GError **error);
201 struct _status_info *imapx_copy_status (struct _status_info *sinfo);
202 void imapx_free_status (struct _status_info *sinfo);
203
204 /* ********************************************************************** */
205 /* parses the response from the status command */
206 struct _state_info {
207         gchar *name;
208         guint32 messages;
209         guint32 recent;
210         guint32 uidnext;
211         guint32 unseen;
212         guint64 uidvalidity;
213         guint64 highestmodseq;
214 };
215
216 /* use g_free to free the return value */
217 struct _state_info *imapx_parse_status_info (struct _CamelIMAPXStream *is, GCancellable *cancellable, GError **error);
218
219 /* ********************************************************************** */
220
221 /* should this just return a FolderInfo?
222  * should this just return the name & flags & separator by reference? */
223 struct _list_info {
224         guint32 flags : 24;
225         gchar separator;
226         gchar *name;
227 };
228
229 struct _list_info *imapx_parse_list (struct _CamelIMAPXStream *is, GCancellable *cancellable, GError **error);
230 gchar *imapx_list_get_path (struct _list_info *li);
231 void imapx_free_list (struct _list_info *linfo);
232
233 /* ********************************************************************** */
234 typedef struct _IMAPXJobQueueInfo {
235         guint queue_len;
236
237         /* list of folders for which jobs are in the queue */
238         GHashTable *folders;
239 } IMAPXJobQueueInfo;
240
241 void camel_imapx_destroy_job_queue_info (IMAPXJobQueueInfo *jinfo);
242
243 /* ********************************************************************** */
244
245 extern guchar imapx_specials[256];
246
247 #define IMAPX_TYPE_CHAR (1 << 0)
248 #define IMAPX_TYPE_TEXT_CHAR (1 << 1)
249 #define IMAPX_TYPE_QUOTED_CHAR (1 << 2)
250 #define IMAPX_TYPE_ATOM_CHAR (1 << 3)
251 #define IMAPX_TYPE_TOKEN_CHAR (1 << 4)
252 #define IMAPX_TYPE_NOTID_CHAR (1 << 5)
253
254 guchar imapx_is_mask (const gchar *p);
255
256 #define imapx_is_text_char(c) ((imapx_specials[((unsigned char)(c))&0xff] & IMAPX_TYPE_TEXT_CHAR) != 0)
257 #define imapx_is_quoted_char(c) ((imapx_specials[((unsigned char)(c))&0xff] & IMAPX_TYPE_QUOTED_CHAR) != 0)
258 #define imapx_is_atom_char(c) ((imapx_specials[((unsigned char)(c))&0xff] & IMAPX_TYPE_ATOM_CHAR) != 0)
259 #define imapx_is_token_char(c) ((imapx_specials[((unsigned char)(c))&0xff] & IMAPX_TYPE_TOKEN_CHAR) != 0)
260 #define imapx_is_notid_char(c) ((imapx_specials[((unsigned char)(c))&0xff] & IMAPX_TYPE_NOTID_CHAR) != 0)
261
262 #define imapx_is_atom(s) (imapx_is_mask(s) & IMAPX_TYPE_ATOM_CHAR)
263
264 extern gint camel_imapx_debug_flags;
265 #define CAMEL_IMAPX_DEBUG_command       (1 << 0)
266 #define CAMEL_IMAPX_DEBUG_debug         (1 << 1)
267 #define CAMEL_IMAPX_DEBUG_extra         (1 << 2)
268 #define CAMEL_IMAPX_DEBUG_io            (1 << 3)
269 #define CAMEL_IMAPX_DEBUG_token         (1 << 4)
270 #define CAMEL_IMAPX_DEBUG_parse         (1 << 5)
271 #define CAMEL_IMAPX_DEBUG_conman        (1 << 6)
272
273 /* Set this to zero to remove all debug output at build time */
274 #define CAMEL_IMAPX_DEBUG_ALL           ((1 << 7)-1)
275
276 #define camel_debug_flag(type) (camel_imapx_debug_flags & CAMEL_IMAPX_DEBUG_ALL & CAMEL_IMAPX_DEBUG_ ## type)
277 #define camel_imapx_debug(type, tagprefix, fmt, ...) do {               \
278                 if (camel_debug_flag (type))                            \
279                         printf ("[imapx:%c] " fmt, tagprefix , ##__VA_ARGS__); \
280         } while (0)
281
282 /* ********************************************************************** */
283
284 void imapx_utils_init (void);
285
286 /* chen adds from old imap provider - place it in right place */
287 gchar *imapx_path_to_physical (const gchar *prefix, const gchar *vpath);
288 gchar *imapx_concat (struct _CamelIMAPXStore *imapx_store, const gchar *prefix, const gchar *suffix);
289 gchar * imapx_get_temp_uid (void);
290
291 void camel_imapx_namespace_list_clear (struct _CamelIMAPXNamespaceList *nsl);
292 struct _CamelIMAPXNamespaceList * imapx_parse_namespace_list (struct _CamelIMAPXStream *stream, GCancellable *cancellable, GError **error);
293 struct _CamelIMAPXNamespaceList *camel_imapx_namespace_list_copy (const struct _CamelIMAPXNamespaceList *nsl);
294
295 #endif