summaryrefslogtreecommitdiff
path: root/camel/providers/imapx/camel-imapx-status-response.c
blob: 3e1168b218ae6acfbffde3ee7e986b68d6214835 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
 * camel-imapx-status-response.c
 *
 * This library is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * SECTION: camel-imapx-status-response
 * @include: camel/camel.h
 * @short_description: Stores an IMAP STATUS respose
 *
 * #CamelIMAPXStatusResponse encapsulates an IMAP STATUS response, which
 * describes the current status of a mailbox in terms of various message
 * counts and change tracking indicators.
 **/

#include "camel-imapx-status-response.h"

#include "camel-imapx-utils.h"

#define CAMEL_IMAPX_STATUS_RESPONSE_GET_PRIVATE(obj) \
	(G_TYPE_INSTANCE_GET_PRIVATE \
	((obj), CAMEL_TYPE_IMAPX_STATUS_RESPONSE, CamelIMAPXStatusResponsePrivate))

struct _CamelIMAPXStatusResponsePrivate {
	gchar *mailbox_name;

	guint32 messages;
	guint32 recent;
	guint32 unseen;
	guint32 uidnext;
	guint32 uidvalidity;
	guint64 highestmodseq;

	gboolean have_messages;
	gboolean have_recent;
	gboolean have_unseen;
	gboolean have_uidnext;
	gboolean have_uidvalidity;
	gboolean have_highestmodseq;
};

G_DEFINE_TYPE (
	CamelIMAPXStatusResponse,
	camel_imapx_status_response,
	G_TYPE_OBJECT)

static void
imapx_status_response_finalize (GObject *object)
{
	CamelIMAPXStatusResponsePrivate *priv;

	priv = CAMEL_IMAPX_STATUS_RESPONSE_GET_PRIVATE (object);

	g_free (priv->mailbox_name);

	/* Chain up to parent's finalize() method. */
	G_OBJECT_CLASS (camel_imapx_status_response_parent_class)->
		finalize (object);
}

static void
camel_imapx_status_response_class_init (CamelIMAPXStatusResponseClass *class)
{
	GObjectClass *object_class;

	g_type_class_add_private (
		class, sizeof (CamelIMAPXStatusResponsePrivate));

	object_class = G_OBJECT_CLASS (class);
	object_class->finalize = imapx_status_response_finalize;
}

static void
camel_imapx_status_response_init (CamelIMAPXStatusResponse *response)
{
	response->priv = CAMEL_IMAPX_STATUS_RESPONSE_GET_PRIVATE (response);
}

/**
 * camel_imapx_status_response_new:
 * @stream: a #CamelIMAPXInputStream
 * @inbox_separator: the separator character for INBOX
 * @cancellable: a #GCancellable
 * @error: return location for a #GError, or %NULL
 *
 * Attempts to parse an IMAP STATUS response from @stream and, if successful,
 * stores the response data in a new #CamelIMAPXStatusResponse.  If an error
 * occurs, the function sets @error and returns %NULL.
 *
 * Returns: a #CamelIMAPXStatusResponse, or %NULL
 *
 * Since: 3.10
 **/
CamelIMAPXStatusResponse *
camel_imapx_status_response_new (CamelIMAPXInputStream *stream,
                                 gchar inbox_separator,
                                 GCancellable *cancellable,
                                 GError **error)
{
	CamelIMAPXStatusResponse *response;
	camel_imapx_token_t tok;
	guchar *token;
	guint len;

	g_return_val_if_fail (CAMEL_IS_IMAPX_INPUT_STREAM (stream), NULL);

	response = g_object_new (CAMEL_TYPE_IMAPX_STATUS_RESPONSE, NULL);

	/* Parse mailbox name. */

	response->priv->mailbox_name = camel_imapx_parse_mailbox (
		stream, inbox_separator, cancellable, error);
	if (response->priv->mailbox_name == NULL)
		goto fail;

	/* Parse status attributes. */

	tok = camel_imapx_input_stream_token (
		CAMEL_IMAPX_INPUT_STREAM (stream),
		&token, &len, cancellable, error);
	if (tok == IMAPX_TOK_ERROR)
		goto fail;
	if (tok != '(') {
		g_set_error (
			error, CAMEL_IMAPX_ERROR, CAMEL_IMAPX_ERROR_SERVER_RESPONSE_MALFORMED,
			"status: expecting '('");
		goto fail;
	}

	tok = camel_imapx_input_stream_token (
		CAMEL_IMAPX_INPUT_STREAM (stream),
		&token, &len, cancellable, error);

	while (tok == IMAPX_TOK_TOKEN) {
		guint64 number;
		gboolean success;

		switch (imapx_tokenise ((gchar *) token, len)) {
			case IMAPX_MESSAGES:
				success = camel_imapx_input_stream_number (
					CAMEL_IMAPX_INPUT_STREAM (stream),
					&number, cancellable, error);
				response->priv->messages = (guint32) number;
				response->priv->have_messages = TRUE;
				break;

			case IMAPX_RECENT:
				success = camel_imapx_input_stream_number (
					CAMEL_IMAPX_INPUT_STREAM (stream),
					&number, cancellable, error);
				response->priv->recent = (guint32) number;
				response->priv->have_recent = TRUE;
				break;

			case IMAPX_UNSEEN:
				success = camel_imapx_input_stream_number (
					CAMEL_IMAPX_INPUT_STREAM (stream),
					&number, cancellable, error);
				response->priv->unseen = (guint32) number;
				response->priv->have_unseen = TRUE;
				break;

			case IMAPX_UIDNEXT:
				success = camel_imapx_input_stream_number (
					CAMEL_IMAPX_INPUT_STREAM (stream),
					&number, cancellable, error);
				response->priv->uidnext = (guint32) number;
				response->priv->have_uidnext = TRUE;
				break;

			case IMAPX_UIDVALIDITY:
				success = camel_imapx_input_stream_number (
					CAMEL_IMAPX_INPUT_STREAM (stream),
					&number, cancellable, error);
				response->priv->uidvalidity = (guint32) number;
				response->priv->have_uidvalidity = TRUE;
				break;

			/* See RFC 4551 section 3.6 */
			case IMAPX_HIGHESTMODSEQ:
				success = camel_imapx_input_stream_number (
					CAMEL_IMAPX_INPUT_STREAM (stream),
					&number, cancellable, error);
				response->priv->highestmodseq = number;
				response->priv->have_highestmodseq = TRUE;
				break;

			default:
				g_set_error (
					error, CAMEL_IMAPX_ERROR, CAMEL_IMAPX_ERROR_SERVER_RESPONSE_MALFORMED,
					"unknown status attribute");
				success = FALSE;
				break;
		}

		if (!success)
			goto fail;

		tok = camel_imapx_input_stream_token (
			CAMEL_IMAPX_INPUT_STREAM (stream),
			&token, &len, cancellable, error);
	}

	if (tok == IMAPX_TOK_ERROR)
		goto fail;

	if (tok != ')') {
		g_set_error (
			error, CAMEL_IMAPX_ERROR, CAMEL_IMAPX_ERROR_SERVER_RESPONSE_MALFORMED,
			"status: expecting ')' or attribute");
		goto fail;
	}

	return response;

fail:
	g_clear_object (&response);

	return NULL;
}

/**
 * camel_imapx_status_response_get_mailbox_name:
 * @response: a #CamelIMAPXStatusResponse
 *
 * Returns the mailbox name for @response.
 *
 * Returns: the mailbox name
 *
 * Since: 3.10
 **/
const gchar *
camel_imapx_status_response_get_mailbox_name (CamelIMAPXStatusResponse *response)
{
	g_return_val_if_fail (CAMEL_IS_IMAPX_STATUS_RESPONSE (response), NULL);

	return response->priv->mailbox_name;
}

/**
 * camel_imapx_status_response_get_messages:
 * @response: a #CamelIMAPXStatusResponse
 * @out_messages: return location for the status value, or %NULL
 *
 * If @response includes an updated "MESSAGES" value, write the value to
 * @out_messages and return %TRUE.  Otherwise leave @out_messages unset
 * and return %FALSE.
 *
 * The "MESSAGES" value refers to the number of messages in the mailbox.
 *
 * The @out_messages argument can be %NULL, in which case the function
 * simply returns whether an updated "MESSAGES" value is present.
 *
 * Returns: whether @out_messages was set
 *
 * Since: 3.10
 **/
gboolean
camel_imapx_status_response_get_messages (CamelIMAPXStatusResponse *response,
                                          guint32 *out_messages)
{
	g_return_val_if_fail (
		CAMEL_IS_IMAPX_STATUS_RESPONSE (response), FALSE);

	if (out_messages != NULL && response->priv->have_messages)
		*out_messages = response->priv->messages;

	return response->priv->have_messages;
}

/**
 * camel_imapx_status_response_get_recent:
 * @response: a #CamelIMAPXStatusResponse
 * @out_recent: return location for the status value, or %NULL
 *
 * If @response includes an updated "RECENT" value, write the value to
 * @out_recent and return %TRUE.  Otherwise leave @out_recent unset and
 * return %FALSE.
 *
 * The "RECENT" value refers to the number of messages with the \Recent
 * flag set.
 *
 * The @out_recent argument can be %NULL, in which case the function
 * simply returns whether an updated "RECENT" value is present.
 *
 * Returns: whether @out_recent was set
 *
 * Since: 3.10
 **/
gboolean
camel_imapx_status_response_get_recent (CamelIMAPXStatusResponse *response,
                                        guint32 *out_recent)
{
	g_return_val_if_fail (
		CAMEL_IS_IMAPX_STATUS_RESPONSE (response), FALSE);

	if (out_recent != NULL && response->priv->have_recent)
		*out_recent = response->priv->recent;

	return response->priv->have_recent;
}

/**
 * camel_imapx_status_response_get_unseen:
 * @response: a #CamelIMAPXStatusResponse
 * @out_unseen: return location for the status value, or %NULL
 *
 * If @response includes an updated "UNSEEN" value, write the value to
 * @out_unseen and return %TRUE.  Otherwise leave @out_unseen unset and
 * return %FALSE.
 *
 * The "UNSEEN" value refers to the number of messages which do not have
 * the \Seen flag set.
 *
 * The @out_unseen argument can be %NULL, in which case the function
 * simply returns whether an updated "UNSEEN" value is present.
 *
 * Returns: whether @out_unseen was set
 *
 * Since: 3.10
 **/
gboolean
camel_imapx_status_response_get_unseen (CamelIMAPXStatusResponse *response,
                                        guint32 *out_unseen)
{
	g_return_val_if_fail (
		CAMEL_IS_IMAPX_STATUS_RESPONSE (response), FALSE);

	if (out_unseen != NULL && response->priv->have_unseen)
		*out_unseen = response->priv->unseen;

	return response->priv->have_unseen;
}

/**
 * camel_imapx_status_response_get_uidnext:
 * @response: a #CamelIMAPXStatusResponse
 * @out_uidnext: return location for the status value, or %NULL
 *
 * If @response includes an updated "UIDNEXT" value, write the value to
 * @out_uidnext and return %TRUE.  Otherwise leave @out_uidnext unset and
 * return %FALSE.
 *
 * The "UIDNEXT" value refers to the next unique identifier value of the
 * mailbox.
 *
 * The @out_uidnext argument can be %NULL, in which case the function
 * simply returns whether an updated "UIDNEXT" value is present.
 *
 * Returns: whether @out_uidnext was set
 *
 * Since: 3.10
 **/
gboolean
camel_imapx_status_response_get_uidnext (CamelIMAPXStatusResponse *response,
                                         guint32 *out_uidnext)
{
	g_return_val_if_fail (
		CAMEL_IS_IMAPX_STATUS_RESPONSE (response), FALSE);

	if (out_uidnext != NULL && response->priv->have_uidnext)
		*out_uidnext = response->priv->uidnext;

	return response->priv->have_uidnext;
}

/**
 * camel_imapx_status_response_get_uidvalidity:
 * @response: a #CamelIMAPXStatusResponse
 * @out_uidvalidity: return location for the status value, or %NULL
 *
 * If @response includes an updated "UIDVALIDITY" value, write the value to
 * @out_uidvalidity and return %TRUE.  Otherwise leave @out_uidvalidity unset
 * and return %FALSE.
 *
 * The "UIDVALIDITY" value refers to the unique identifier validity of the
 * mailbox.
 *
 * The @out_uidvalidity argument can be %NULL, in which case the function
 * simply returns whether an updated "UIDVALIDITY" value is present.
 *
 * Returns: whether @out_uidvalidity was set
 *
 * Since: 3.10
 **/
gboolean
camel_imapx_status_response_get_uidvalidity (CamelIMAPXStatusResponse *response,
                                             guint32 *out_uidvalidity)
{
	g_return_val_if_fail (
		CAMEL_IS_IMAPX_STATUS_RESPONSE (response), FALSE);

	if (out_uidvalidity != NULL && response->priv->have_uidvalidity)
		*out_uidvalidity = response->priv->uidvalidity;

	return response->priv->have_uidvalidity;
}

/**
 * camel_imapx_status_response_get_highestmodseq:
 * @response: a #CamelIMAPXStatusResponse
 * @out_highestmodseq: return location for the status value, or %NULL
 *
 * If @response includes an updated "HIGHESTMODSEQ" value, write the value to
 * @out_highestmodseq and return %TRUE.  Otherwise leave @out_highestmodseq
 * unset and return %FALSE.
 *
 * The "HIGHESTMODSEQ" value refers to the the highest mod-sequence value of
 * all messages in the mailbox, assuming the server supports the persistent
 * storage of mod-sequences.
 *
 * The @out_highestmodseq argument can be %NULL, in which case the function
 * simply returns whether an updated "HIGHESTMODSEQ" value is present.
 *
 * Returns: whether @out_highestmodseq was set
 *
 * Since: 3.10
 **/
gboolean
camel_imapx_status_response_get_highestmodseq (CamelIMAPXStatusResponse *response,
                                               guint64 *out_highestmodseq)
{
	g_return_val_if_fail (
		CAMEL_IS_IMAPX_STATUS_RESPONSE (response), FALSE);

	if (out_highestmodseq != NULL && response->priv->have_highestmodseq)
		*out_highestmodseq = response->priv->highestmodseq;

	return response->priv->have_highestmodseq;
}