summaryrefslogtreecommitdiff
path: root/camel/providers/imap4/camel-imap4-command.c
blob: c1ff3a289b0725dc0fb739360e71141577f562a4 (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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*  Camel
 *  Copyright (C) 1999-2004 Jeffrey Stedfast
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <camel/camel-stream-null.h>
#include <camel/camel-stream-filter.h>
#include <camel/camel-mime-filter-crlf.h>

#include "camel-imap4-stream.h"
#include "camel-imap4-engine.h"
#include "camel-imap4-folder.h"
#include "camel-imap4-specials.h"

#include "camel-imap4-command.h"


#define d(x) x


enum {
	IMAP4_STRING_ATOM,
	IMAP4_STRING_QSTRING,
	IMAP4_STRING_LITERAL,
};

static int
imap4_string_get_type (const char *str)
{
	int type = 0;
	
	while (*str) {
		if (!is_atom (*str)) {
			if (is_qsafe (*str))
				type = IMAP4_STRING_QSTRING;
			else
				return IMAP4_STRING_LITERAL;
		}
		str++;
	}
	
	return type;
}

#if 0
static gboolean
imap4_string_is_atom_safe (const char *str)
{
	while (is_atom (*str))
		str++;
	
	return *str == '\0';
}

static gboolean
imap4_string_is_quote_safe (const char *str)
{
	while (is_qsafe (*str))
		str++;
	
	return *str == '\0';
}
#endif

static size_t
camel_imap4_literal_length (CamelIMAP4Literal *literal)
{
	CamelStream *stream, *null;
	CamelMimeFilter *crlf;
	size_t len;
	
	if (literal->type == CAMEL_IMAP4_LITERAL_STRING)
		return strlen (literal->literal.string);
	
	null = camel_stream_null_new ();
	crlf = camel_mime_filter_crlf_new (CAMEL_MIME_FILTER_CRLF_ENCODE, CAMEL_MIME_FILTER_CRLF_MODE_CRLF_ONLY);
	stream = (CamelStream *) camel_stream_filter_new_with_stream (null);
	camel_stream_filter_add ((CamelStreamFilter *) stream, crlf);
	camel_object_unref (crlf);
	
	switch (literal->type) {
	case CAMEL_IMAP4_LITERAL_STREAM:
		camel_stream_write_to_stream (literal->literal.stream, stream);
		camel_stream_reset (literal->literal.stream);
		break;
	case CAMEL_IMAP4_LITERAL_WRAPPER:
		camel_data_wrapper_write_to_stream (literal->literal.wrapper, stream);
		break;
	default:
		g_assert_not_reached ();
		break;
	}
	
	len = ((CamelStreamNull *) null)->written;
	
	camel_object_unref (stream);
	camel_object_unref (null);
	
	return len;
}

static CamelIMAP4CommandPart *
command_part_new (void)
{
	CamelIMAP4CommandPart *part;
	
	part = g_new (CamelIMAP4CommandPart, 1);
	part->next = NULL;
	part->buffer = NULL;
	part->buflen = 0;
	part->literal = NULL;
	
	return part;
}

static void
imap4_command_append_string (CamelIMAP4Engine *engine, CamelIMAP4CommandPart **tail, GString *str, const char *string)
{
	CamelIMAP4CommandPart *part;
	CamelIMAP4Literal *literal;
	
	switch (imap4_string_get_type (string)) {
	case IMAP4_STRING_ATOM:
		/* string is safe as it is... */
		g_string_append (str, string);
		break;
	case IMAP4_STRING_QSTRING:
		/* we need to quote the string */
		/* FIXME: need to escape stuff */
		g_string_append_printf (str, "\"%s\"", string);
		break;
	case IMAP4_STRING_LITERAL:
		if (engine->capa & CAMEL_IMAP4_CAPABILITY_LITERALPLUS) {
			/* we have to send a literal, but the server supports LITERAL+ so use that */
			g_string_append_printf (str, "{%u+}\r\n%s", strlen (string), string);
		} else {
			/* we have to make it a literal */
			literal = g_new (CamelIMAP4Literal, 1);
			literal->type = CAMEL_IMAP4_LITERAL_STRING;
			literal->literal.string = g_strdup (string);
			
			g_string_append_printf (str, "{%u}\r\n", strlen (string));
			
			(*tail)->buffer = g_strdup (str->str);
			(*tail)->buflen = str->len;
			(*tail)->literal = literal;
			
			part = command_part_new ();
			(*tail)->next = part;
			(*tail) = part;
			
			g_string_truncate (str, 0);
		}
		break;
	}
}

CamelIMAP4Command *
camel_imap4_command_newv (CamelIMAP4Engine *engine, CamelIMAP4Folder *imap4_folder, const char *format, va_list args)
{
	CamelIMAP4CommandPart *parts, *part, *tail;
	CamelIMAP4Command *ic;
	const char *start;
	GString *str;
	
	tail = parts = command_part_new ();
	
	str = g_string_new ("");
	start = format;
	
	while (*format) {
		register char ch = *format++;
		
		if (ch == '%') {
			CamelIMAP4Literal *literal;
			CamelIMAP4Folder *folder;
			unsigned int u;
			char *string;
			size_t len;
			void *obj;
			int c, d;
			
			g_string_append_len (str, start, format - start - 1);
			
			switch (*format) {
			case '%':
				/* literal % */
				g_string_append_c (str, '%');
				break;
			case 'c':
				/* character */
				c = va_arg (args, int);
				g_string_append_c (str, c);
				break;
			case 'd':
				/* integer */
				d = va_arg (args, int);
				g_string_append_printf (str, "%d", d);
				break;
			case 'u':
				/* unsigned integer */
				u = va_arg (args, unsigned int);
				g_string_append_printf (str, "%u", u);
				break;
			case 'F':
				/* CamelIMAP4Folder */
				folder = va_arg (args, CamelIMAP4Folder *);
				string = (char *) camel_imap4_folder_utf7_name (folder);
				imap4_command_append_string (engine, &tail, str, string);
				break;
			case 'L':
				/* Literal */
				obj = va_arg (args, void *);
				
				literal = g_new (CamelIMAP4Literal, 1);
				if (CAMEL_IS_DATA_WRAPPER (obj)) {
					literal->type = CAMEL_IMAP4_LITERAL_WRAPPER;
					literal->literal.wrapper = obj;
				} else if (CAMEL_IS_STREAM (obj)) {
					literal->type = CAMEL_IMAP4_LITERAL_STREAM;
					literal->literal.stream = obj;
				} else {
					g_assert_not_reached ();
				}
				
				camel_object_ref (obj);
				
				/* FIXME: take advantage of LITERAL+? */
				len = camel_imap4_literal_length (literal);
				g_string_append_printf (str, "{%u}\r\n", len);
				
				tail->buffer = g_strdup (str->str);
				tail->buflen = str->len;
				tail->literal = literal;
				
				part = command_part_new ();
				tail->next = part;
				tail = part;
				
				g_string_truncate (str, 0);
				
				break;
			case 'S':
				/* string which may need to be quoted or made into a literal */
				string = va_arg (args, char *);
				imap4_command_append_string (engine, &tail, str, string);
				break;
			case 's':
				/* safe atom string */
				string = va_arg (args, char *);
				g_string_append (str, string);
				break;
			default:
				g_warning ("unknown formatter %%%c", *format);
				g_string_append_c (str, '%');
				g_string_append_c (str, *format);
				break;
			}
			
			format++;
			
			start = format;
		}
	}
	
	g_string_append (str, start);
	tail->buffer = str->str;
	tail->buflen = str->len;
	tail->literal = NULL;
	g_string_free (str, FALSE);
	
	ic = g_new0 (CamelIMAP4Command, 1);
	((EDListNode *) ic)->next = NULL;
	((EDListNode *) ic)->prev = NULL;
	ic->untagged = g_hash_table_new (g_str_hash, g_str_equal);
	ic->status = CAMEL_IMAP4_COMMAND_QUEUED;
	ic->resp_codes = g_ptr_array_new ();
	ic->engine = engine;
	ic->ref_count = 1;
	ic->parts = parts;
	ic->part = parts;
	
	camel_exception_init (&ic->ex);
	
	if (imap4_folder) {
		camel_object_ref (imap4_folder);
		ic->folder = imap4_folder;
	} else
		ic->folder = NULL;
	
	return ic;
}

CamelIMAP4Command *
camel_imap4_command_new (CamelIMAP4Engine *engine, CamelIMAP4Folder *folder, const char *format, ...)
{
	CamelIMAP4Command *command;
	va_list args;
	
	va_start (args, format);
	command = camel_imap4_command_newv (engine, folder, format, args);
	va_end (args);
	
	return command;
}

void
camel_imap4_command_register_untagged (CamelIMAP4Command *ic, const char *atom, CamelIMAP4UntaggedCallback untagged)
{
	g_hash_table_insert (ic->untagged, g_strdup (atom), untagged);
}

void
camel_imap4_command_ref (CamelIMAP4Command *ic)
{
	ic->ref_count++;
}

void
camel_imap4_command_unref (CamelIMAP4Command *ic)
{
	CamelIMAP4CommandPart *part, *next;
	int i;
	
	if (ic == NULL)
		return;
	
	ic->ref_count--;
	if (ic->ref_count == 0) {
		if (ic->folder)
			camel_object_unref (ic->folder);
		
		g_free (ic->tag);
		
		for (i = 0; i < ic->resp_codes->len; i++) {
			CamelIMAP4RespCode *resp_code;
			
			resp_code = ic->resp_codes->pdata[i];
			camel_imap4_resp_code_free (resp_code);
		}
		g_ptr_array_free (ic->resp_codes, TRUE);
		
		g_hash_table_foreach (ic->untagged, (GHFunc) g_free, NULL);
		g_hash_table_destroy (ic->untagged);
		
		camel_exception_clear (&ic->ex);
		
		part = ic->parts;
		while (part != NULL) {
			g_free (part->buffer);
			if (part->literal) {
				switch (part->literal->type) {
				case CAMEL_IMAP4_LITERAL_STRING:
					g_free (part->literal->literal.string);
					break;
				case CAMEL_IMAP4_LITERAL_STREAM:
					camel_object_unref (part->literal->literal.stream);
					break;
				case CAMEL_IMAP4_LITERAL_WRAPPER:
					camel_object_unref (part->literal->literal.wrapper);
					break;
				}
				
				g_free (part->literal);
			}
			
			next = part->next;
			g_free (part);
			part = next;
		}
		
		g_free (ic);
	}
}


static int
imap4_literal_write_to_stream (CamelIMAP4Literal *literal, CamelStream *stream)
{
	CamelStream *istream, *ostream = NULL;
	CamelDataWrapper *wrapper;
	CamelMimeFilter *crlf;
	char *string;
	
	if (literal->type == CAMEL_IMAP4_LITERAL_STRING) {
		string = literal->literal.string;
		if (camel_stream_write (stream, string, strlen (string)) == -1)
			return -1;
		
		return 0;
	}
	
	crlf = camel_mime_filter_crlf_new (CAMEL_MIME_FILTER_CRLF_ENCODE, CAMEL_MIME_FILTER_CRLF_MODE_CRLF_ONLY);
	ostream = (CamelStream *) camel_stream_filter_new_with_stream (stream);
	camel_stream_filter_add ((CamelStreamFilter *) ostream, crlf);
	camel_object_unref (crlf);
	
	/* write the literal */
	switch (literal->type) {
	case CAMEL_IMAP4_LITERAL_STREAM:
		istream = literal->literal.stream;
		if (camel_stream_write_to_stream (istream, ostream) == -1)
			goto exception;
		break;
	case CAMEL_IMAP4_LITERAL_WRAPPER:
		wrapper = literal->literal.wrapper;
		if (camel_data_wrapper_write_to_stream (wrapper, ostream) == -1)
			goto exception;
		break;
	}
	
	camel_object_unref (ostream);
	ostream = NULL;
	
#if 0
	if (camel_stream_write (stream, "\r\n", 2) == -1)
		return -1;
#endif
	
	return 0;
	
 exception:
	
	camel_object_unref (ostream);
	
	return -1;
}


static void
unexpected_token (camel_imap4_token_t *token)
{
	switch (token->token) {
	case CAMEL_IMAP4_TOKEN_NO_DATA:
		fprintf (stderr, "*** NO DATA ***");
		break;
	case CAMEL_IMAP4_TOKEN_ERROR:
		fprintf (stderr, "*** ERROR ***");
		break;
	case CAMEL_IMAP4_TOKEN_NIL:
		fprintf (stderr, "NIL");
		break;
	case CAMEL_IMAP4_TOKEN_ATOM:
	        fprintf (stderr, "%s", token->v.atom);
		break;
	case CAMEL_IMAP4_TOKEN_QSTRING:
	        fprintf (stderr, "\"%s\"", token->v.qstring);
		break;
	case CAMEL_IMAP4_TOKEN_LITERAL:
		fprintf (stderr, "{%u}", token->v.literal);
		break;
	default:
		fprintf (stderr, "%c", (unsigned char) (token->token & 0xff));
		break;
	}
}

int
camel_imap4_command_step (CamelIMAP4Command *ic)
{
	CamelIMAP4Engine *engine = ic->engine;
	int result = CAMEL_IMAP4_RESULT_NONE;
	CamelIMAP4Literal *literal;
	camel_imap4_token_t token;
	unsigned char *linebuf;
	ssize_t nwritten;
	size_t len;
	
	g_assert (ic->part != NULL);
	
	if (ic->part == ic->parts) {
		ic->tag = g_strdup_printf ("%c%.5u", engine->tagprefix, engine->tag++);
		camel_stream_printf (engine->ostream, "%s ", ic->tag);
		d(fprintf (stderr, "sending : %s ", ic->tag));
	}
	
#if d(!)0
	{
		int sending = ic->part != ic->parts;
		unsigned char *eoln, *eob;
		
		linebuf = ic->part->buffer;
		eob = linebuf + ic->part->buflen;
		
		do {
			eoln = linebuf;
			while (eoln < eob && *eoln != '\n')
				eoln++;
			
			if (eoln < eob)
				eoln++;
			
			if (sending)
				fwrite ("sending : ", 1, 10, stderr);
			fwrite (linebuf, 1, eoln - linebuf, stderr);
			
			linebuf = eoln + 1;
			sending = 1;
		} while (linebuf < eob);
	}
#endif
	
	linebuf = ic->part->buffer;
	len = ic->part->buflen;
	
	if ((nwritten = camel_stream_write (engine->ostream, linebuf, len)) == -1)
		goto exception;
	
	if (camel_stream_flush (engine->ostream) == -1)
		goto exception;
	
	/* now we need to read the response(s) from the IMAP4 server */
	
	do {
		if (camel_imap4_engine_next_token (engine, &token, &ic->ex) == -1)
			goto exception;
		
		if (token.token == '+') {
			/* we got a continuation response from the server */
			literal = ic->part->literal;
			
			if (camel_imap4_engine_line (engine, &linebuf, &len, &ic->ex) == -1)
				goto exception;
			
			if (literal) {
				if (imap4_literal_write_to_stream (literal, engine->ostream) == -1)
					goto exception;
				
				g_free (linebuf);
				linebuf = NULL;
				
				break;
			} else if (ic->plus) {
				/* command expected a '+' response - probably AUTHENTICATE? */
				if (ic->plus (engine, ic, linebuf, len, &ic->ex) == -1) {
					g_free (linebuf);
					return -1;
				}
				
				/* now we need to wait for a "<tag> OK/NO/BAD" response */
			} else {
				/* FIXME: error?? */
				g_assert_not_reached ();
			}
			
			g_free (linebuf);
			linebuf = NULL;
		} else if (token.token == '*') {
			/* we got an untagged response, let the engine handle this */
			if (camel_imap4_engine_handle_untagged_1 (engine, &token, &ic->ex) == -1)
				goto exception;
		} else if (token.token == CAMEL_IMAP4_TOKEN_ATOM && !strcmp (token.v.atom, ic->tag)) {
			/* we got "<tag> OK/NO/BAD" */
			fprintf (stderr, "got %s response\n", token.v.atom);
			
			if (camel_imap4_engine_next_token (engine, &token, &ic->ex) == -1)
				goto exception;
			
			if (token.token == CAMEL_IMAP4_TOKEN_ATOM) {
				if (!strcmp (token.v.atom, "OK"))
					result = CAMEL_IMAP4_RESULT_OK;
				else if (!strcmp (token.v.atom, "NO"))
					result = CAMEL_IMAP4_RESULT_NO;
				else if (!strcmp (token.v.atom, "BAD"))
					result = CAMEL_IMAP4_RESULT_BAD;
				
				if (result == CAMEL_IMAP4_RESULT_NONE) {
					fprintf (stderr, "expected OK/NO/BAD but got %s\n", token.v.atom);
					goto unexpected;
				}
				
				if (camel_imap4_engine_next_token (engine, &token, &ic->ex) == -1)
					goto exception;
				
				if (token.token == '[') {
					/* we have a response code */
					camel_imap4_stream_unget_token (engine->istream, &token);
					if (camel_imap4_engine_parse_resp_code (engine, &ic->ex) == -1)
						goto exception;
				} else if (token.token != '\n') {
					/* just gobble up the rest of the line */
					if (camel_imap4_engine_line (engine, NULL, NULL, &ic->ex) == -1)
						goto exception;
				}
			} else {
				fprintf (stderr, "expected anything but this: ");
				unexpected_token (&token);
				fprintf (stderr, "\n");
				
				goto unexpected;
			}
			
			break;
		} else {
			fprintf (stderr, "wtf is this: ");
			unexpected_token (&token);
			fprintf (stderr, "\n");
			
		unexpected:
			
			/* no fucking clue what we got... */
			if (camel_imap4_engine_line (engine, &linebuf, &len, &ic->ex) == -1)
				goto exception;
			
			camel_exception_setv (&ic->ex, CAMEL_EXCEPTION_SYSTEM,
					      _("Unexpected response from IMAP4 server %s: %s"),
					      engine->url->host, linebuf);
			
			g_free (linebuf);
			
			goto exception;
		}
	} while (1);
	
	/* status should always be ACTIVE here... */
	if (ic->status == CAMEL_IMAP4_COMMAND_ACTIVE) {
		ic->part = ic->part->next;
		if (ic->part == NULL || result) {
			ic->status = CAMEL_IMAP4_COMMAND_COMPLETE;
			ic->result = result;
			return 1;
		}
	}
	
	return 0;
	
 exception:
	
	ic->status = CAMEL_IMAP4_COMMAND_ERROR;
	
	return -1;
}


void
camel_imap4_command_reset (CamelIMAP4Command *ic)
{
	int i;
	
	for (i = 0; i < ic->resp_codes->len; i++)
		camel_imap4_resp_code_free (ic->resp_codes->pdata[i]);
	g_ptr_array_set_size (ic->resp_codes, 0);
	
	ic->status = CAMEL_IMAP4_COMMAND_QUEUED;
	ic->result = CAMEL_IMAP4_RESULT_NONE;
	ic->part = ic->parts;
	g_free (ic->tag);
	ic->tag = NULL;
	
	camel_exception_clear (&ic->ex);
}