forked from dinhvh/libetpan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailimap.c
2602 lines (2060 loc) · 67 KB
/
mailimap.c
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* libEtPan! -- a mail stuff library
*
* Copyright (C) 2001, 2005 - DINH Viet Hoa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the libEtPan! project nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* $Id: mailimap.c,v 1.51 2011/07/10 23:10:12 hoa Exp $
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "mailimap.h"
#include "mailimap_parser.h"
#include "mailimap_sender.h"
#include "mailimap_extension.h"
#include "mail.h"
#include "condstore.h"
#include "condstore_private.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef USE_SASL
#include <sasl/sasl.h>
#include <sasl/saslutil.h>
#endif
#include "mailsasl.h"
#ifdef DEBUG
#include "mailimap_print.h"
#endif
/*
RFC 2060 : IMAP4rev1
draft-crispin-imapv-15
RFC 2222 : Simple Authentication and Security Layer
2061 IMAP4 Compatibility with IMAP2bis. M. Crispin. December 1996.
(Format: TXT=5867 bytes) (Obsoletes RFC1730) (Status: INFORMATIONAL)
2062 Internet Message Access Protocol - Obsolete Syntax. M. Crispin.
December 1996. (Format: TXT=14222 bytes) (Status: INFORMATIONAL)
2086 IMAP4 ACL extension. J. Myers. January 1997. (Format: TXT=13925
bytes) (Status: PROPOSED STANDARD)
2087 IMAP4 QUOTA extension. J. Myers. January 1997. (Format: TXT=8542
bytes) (Status: PROPOSED STANDARD)
2088 IMAP4 non-synchronizing literals. J. Myers. January 1997.
(Format: TXT=4052 bytes) (Status: PROPOSED STANDARD)
2177 IMAP4 IDLE command. B. Leiba. June 1997. (Format: TXT=6770 bytes)
(Status: PROPOSED STANDARD)
2180 IMAP4 Multi-Accessed Mailbox Practice. M. Gahrns. July 1997.
(Format: TXT=24750 bytes) (Status: INFORMATIONAL)
2192 IMAP URL Scheme. C. Newman. September 1997. (Format: TXT=31426
bytes) (Status: PROPOSED STANDARD)
2193 IMAP4 Mailbox Referrals. M. Gahrns. September 1997. (Format:
TXT=16248 bytes) (Status: PROPOSED STANDARD)
2195 IMAP/POP AUTHorize Extension for Simple Challenge/Response. J.
Klensin, R. Catoe, P. Krumviede. September 1997. (Format: TXT=10468
bytes) (Obsoletes RFC2095) (Status: PROPOSED STANDARD)
2221 IMAP4 Login Referrals. M. Gahrns. October 1997. (Format: TXT=9251
bytes) (Status: PROPOSED STANDARD)
2342 IMAP4 Namespace. M. Gahrns, C. Newman. May 1998. (Format:
TXT=19489 bytes) (Status: PROPOSED STANDARD)
2359 IMAP4 UIDPLUS extension. J. Myers. June 1998. (Format: TXT=10862
bytes) (Status: PROPOSED STANDARD)
2595 Using TLS with IMAP, POP3 and ACAP. C. Newman. June 1999.
(Format: TXT=32440 bytes) (Status: PROPOSED STANDARD)
2683 IMAP4 Implementation Recommendations. B. Leiba. September 1999.
(Format: TXT=56300 bytes) (Status: INFORMATIONAL)
2971 IMAP4 ID extension. T. Showalter. October 2000. (Format:
TXT=14670 bytes) (Status: PROPOSED STANDARD)
http://www.ietf.org/ids.by.wg/imapext.html
*/
static inline void imap_logger(mailstream * s, int log_type,
const char * str, size_t size, void * context);
static int parse_greeting(mailimap * session,
struct mailimap_greeting ** result);
/* struct mailimap_response_info * */
static void resp_text_store(mailimap * session,
struct mailimap_resp_text *
resp_text)
{
struct mailimap_resp_text_code * resp_text_code;
resp_text_code = resp_text->rsp_code;
if (resp_text_code != NULL) {
switch (resp_text_code->rc_type) {
case MAILIMAP_RESP_TEXT_CODE_ALERT:
if (session->imap_response_info) {
if (session->imap_response_info->rsp_alert != NULL)
free(session->imap_response_info->rsp_alert);
session->imap_response_info->rsp_alert = strdup(resp_text->rsp_text);
}
break;
case MAILIMAP_RESP_TEXT_CODE_BADCHARSET:
if (session->imap_response_info) {
if (session->imap_response_info->rsp_badcharset != NULL) {
clist_foreach(resp_text_code->rc_data.rc_badcharset,
(clist_func) mailimap_astring_free, NULL);
clist_free(resp_text_code->rc_data.rc_badcharset);
}
session->imap_response_info->rsp_badcharset =
resp_text_code->rc_data.rc_badcharset;
resp_text_code->rc_data.rc_badcharset = NULL;
}
break;
case MAILIMAP_RESP_TEXT_CODE_CAPABILITY_DATA:
if (session->imap_connection_info) {
if (session->imap_connection_info->imap_capability != NULL)
mailimap_capability_data_free(session->imap_connection_info->imap_capability);
session->imap_connection_info->imap_capability =
resp_text_code->rc_data.rc_cap_data;
/* detach before free */
resp_text_code->rc_data.rc_cap_data = NULL;
}
break;
case MAILIMAP_RESP_TEXT_CODE_PARSE:
if (session->imap_response_info) {
if (session->imap_response_info->rsp_parse != NULL)
free(session->imap_response_info->rsp_parse);
session->imap_response_info->rsp_parse = strdup(resp_text->rsp_text);
}
break;
case MAILIMAP_RESP_TEXT_CODE_PERMANENTFLAGS:
if (session->imap_selection_info) {
if (session->imap_selection_info->sel_perm_flags != NULL) {
clist_foreach(session->imap_selection_info->sel_perm_flags,
(clist_func) mailimap_flag_perm_free, NULL);
clist_free(session->imap_selection_info->sel_perm_flags);
}
session->imap_selection_info->sel_perm_flags =
resp_text_code->rc_data.rc_perm_flags;
/* detach before free */
resp_text_code->rc_data.rc_perm_flags = NULL;
}
break;
case MAILIMAP_RESP_TEXT_CODE_READ_ONLY:
if (session->imap_selection_info)
session->imap_selection_info->sel_perm = MAILIMAP_MAILBOX_READONLY;
break;
case MAILIMAP_RESP_TEXT_CODE_READ_WRITE:
if (session->imap_selection_info)
session->imap_selection_info->sel_perm = MAILIMAP_MAILBOX_READWRITE;
break;
case MAILIMAP_RESP_TEXT_CODE_TRY_CREATE:
if (session->imap_response_info)
session->imap_response_info->rsp_trycreate = TRUE;
break;
case MAILIMAP_RESP_TEXT_CODE_UIDNEXT:
if (session->imap_selection_info)
session->imap_selection_info->sel_uidnext =
resp_text_code->rc_data.rc_uidnext;
break;
case MAILIMAP_RESP_TEXT_CODE_UIDVALIDITY:
if (session->imap_selection_info)
session->imap_selection_info->sel_uidvalidity =
resp_text_code->rc_data.rc_uidvalidity;
break;
case MAILIMAP_RESP_TEXT_CODE_UNSEEN:
if (session->imap_selection_info)
session->imap_selection_info->sel_first_unseen =
resp_text_code->rc_data.rc_first_unseen;
break;
case MAILIMAP_RESP_TEXT_CODE_OTHER:
if (session->imap_response_info) {
if (session->imap_response_info->rsp_atom != NULL)
free(session->imap_response_info->rsp_atom);
if (session->imap_response_info->rsp_value != NULL)
free(session->imap_response_info->rsp_value);
session->imap_response_info->rsp_atom =
resp_text_code->rc_data.rc_atom.atom_name;
resp_text_code->rc_data.rc_atom.atom_name = NULL;
session->imap_response_info->rsp_value =
resp_text_code->rc_data.rc_atom.atom_value;
resp_text_code->rc_data.rc_atom.atom_value = NULL;
}
break;
case MAILIMAP_RESP_TEXT_CODE_EXTENSION:
mailimap_extension_data_store(session, &(resp_text_code->rc_data.rc_ext_data));
break;
}
}
}
static void resp_cond_state_store(mailimap * session,
struct mailimap_resp_cond_state * resp_cond_state)
{
resp_text_store(session, resp_cond_state->rsp_text);
}
static void mailbox_data_store(mailimap * session,
struct mailimap_mailbox_data * mb_data)
{
int r;
switch (mb_data->mbd_type) {
case MAILIMAP_MAILBOX_DATA_FLAGS:
if (session->imap_selection_info) {
if (session->imap_selection_info->sel_flags != NULL)
mailimap_flag_list_free(session->imap_selection_info->sel_flags);
session->imap_selection_info->sel_flags = mb_data->mbd_data.mbd_flags;
mb_data->mbd_data.mbd_flags = NULL;
}
break;
case MAILIMAP_MAILBOX_DATA_LIST:
if (session->imap_response_info) {
r = clist_append(session->imap_response_info->rsp_mailbox_list,
mb_data->mbd_data.mbd_list);
if (r == 0)
mb_data->mbd_data.mbd_list = NULL;
else {
/* TODO must handle error case */
}
}
break;
case MAILIMAP_MAILBOX_DATA_LSUB:
if (session->imap_response_info) {
r = clist_append(session->imap_response_info->rsp_mailbox_lsub,
mb_data->mbd_data.mbd_lsub);
if (r == 0)
mb_data->mbd_data.mbd_lsub = NULL;
else {
/* TODO must handle error case */
}
}
break;
case MAILIMAP_MAILBOX_DATA_SEARCH:
if (session->imap_response_info) {
if (session->imap_response_info->rsp_search_result != NULL) {
if (mb_data->mbd_data.mbd_search != NULL) {
clist_concat(session->imap_response_info->rsp_search_result,
mb_data->mbd_data.mbd_search);
clist_free(mb_data->mbd_data.mbd_search);
mb_data->mbd_data.mbd_search = NULL;
}
}
else {
if (mb_data->mbd_data.mbd_search != NULL) {
session->imap_response_info->rsp_search_result =
mb_data->mbd_data.mbd_search;
mb_data->mbd_data.mbd_search = NULL;
}
}
}
break;
case MAILIMAP_MAILBOX_DATA_STATUS:
if (session->imap_response_info) {
if (session->imap_response_info->rsp_status != NULL)
mailimap_mailbox_data_status_free(session->imap_response_info->rsp_status);
session->imap_response_info->rsp_status = mb_data->mbd_data.mbd_status;
#if 0
if (session->imap_selection_info != NULL) {
clistiter * cur;
for(cur = clist_begin(mb_data->status->status_info_list)
; cur != NULL ; cur = clist_next(cur)) {
struct mailimap_status_info * info;
info = clist_content(cur);
switch (info->att) {
case MAILIMAP_STATUS_ATT_MESSAGES:
session->imap_selection_info->exists = info->value;
break;
case MAILIMAP_STATUS_ATT_RECENT:
session->imap_selection_info->recent = info->value;
break;
case MAILIMAP_STATUS_ATT_UIDNEXT:
session->imap_selection_info->uidnext = info->value;
break;
case MAILIMAP_STATUS_ATT_UIDVALIDITY:
session->imap_selection_info->uidvalidity = info->value;
break;
case MAILIMAP_STATUS_ATT_UNSEEN:
session->imap_selection_info->unseen = info->value;
break;
}
}
}
#endif
#if 0
mailimap_mailbox_data_status_free(mb_data->status);
#endif
mb_data->mbd_data.mbd_status = NULL;
}
break;
case MAILIMAP_MAILBOX_DATA_EXISTS:
if (session->imap_selection_info) {
session->imap_selection_info->sel_exists = mb_data->mbd_data.mbd_exists;
session->imap_selection_info->sel_has_exists = 1;
}
break;
case MAILIMAP_MAILBOX_DATA_RECENT:
if (session->imap_selection_info) {
session->imap_selection_info->sel_recent = mb_data->mbd_data.mbd_recent;
session->imap_selection_info->sel_has_recent = 1;
}
break;
case MAILIMAP_MAILBOX_DATA_EXTENSION_DATA:
mailimap_extension_data_store(session, &mb_data->mbd_data.mbd_extension);
break;
}
}
static void
message_data_store(mailimap * session,
struct mailimap_message_data * msg_data)
{
uint32_t * expunged;
int r;
switch (msg_data->mdt_type) {
case MAILIMAP_MESSAGE_DATA_EXPUNGE:
if (session->imap_response_info) {
expunged = mailimap_number_alloc_new(msg_data->mdt_number);
if (expunged != NULL) {
r = clist_append(session->imap_response_info->rsp_expunged, expunged);
if (r == 0) {
/* do nothing */
}
else {
/* TODO : must handle error case */
mailimap_number_alloc_free(expunged);
}
if (session->imap_selection_info != NULL)
session->imap_selection_info->sel_exists --;
}
}
break;
case MAILIMAP_MESSAGE_DATA_FETCH:
r = clist_append(session->imap_response_info->rsp_fetch_list,
msg_data->mdt_msg_att);
if (r == 0) {
msg_data->mdt_msg_att->att_number = msg_data->mdt_number;
msg_data->mdt_msg_att = NULL;
}
else {
/* TODO : must handle error case */
}
break;
}
}
static void
cont_req_or_resp_data_store(mailimap * session,
struct mailimap_cont_req_or_resp_data * cont_req_or_resp_data)
{
if (cont_req_or_resp_data->rsp_type == MAILIMAP_RESP_RESP_DATA) {
struct mailimap_response_data * resp_data;
resp_data = cont_req_or_resp_data->rsp_data.rsp_resp_data;
switch (resp_data->rsp_type) {
case MAILIMAP_RESP_DATA_TYPE_COND_STATE:
resp_cond_state_store(session, resp_data->rsp_data.rsp_cond_state);
break;
case MAILIMAP_RESP_DATA_TYPE_MAILBOX_DATA:
mailbox_data_store(session, resp_data->rsp_data.rsp_mailbox_data);
break;
case MAILIMAP_RESP_DATA_TYPE_MESSAGE_DATA:
message_data_store(session, resp_data->rsp_data.rsp_message_data);
break;
case MAILIMAP_RESP_DATA_TYPE_CAPABILITY_DATA:
if (session->imap_connection_info) {
if (session->imap_connection_info->imap_capability != NULL)
mailimap_capability_data_free(session->imap_connection_info->imap_capability);
session->imap_connection_info->imap_capability = resp_data->rsp_data.rsp_capability_data;
resp_data->rsp_data.rsp_capability_data = NULL;
}
break;
case MAILIMAP_RESP_DATA_TYPE_EXTENSION_DATA:
mailimap_extension_data_store(session, &(resp_data->rsp_data.rsp_extension_data));
break;
}
}
}
static void response_tagged_store(mailimap * session,
struct mailimap_response_tagged * tagged)
{
resp_cond_state_store(session, tagged->rsp_cond_state);
}
static void resp_cond_bye_store(mailimap * session,
struct mailimap_resp_cond_bye * resp_cond_bye)
{
resp_text_store(session, resp_cond_bye->rsp_text);
}
static void response_fatal_store(mailimap * session,
struct mailimap_response_fatal * fatal)
{
resp_cond_bye_store(session, fatal->rsp_bye);
}
static void response_done_store(mailimap * session,
struct mailimap_response_done * resp_done)
{
switch(resp_done->rsp_type) {
case MAILIMAP_RESP_DONE_TYPE_TAGGED:
response_tagged_store(session, resp_done->rsp_data.rsp_tagged);
break;
case MAILIMAP_RESP_DONE_TYPE_FATAL:
response_fatal_store(session, resp_done->rsp_data.rsp_fatal);
break;
}
}
static void
response_store(mailimap * session,
struct mailimap_response * response)
{
clistiter * cur;
if (session->imap_response_info) {
mailimap_response_info_free(session->imap_response_info);
session->imap_response_info = NULL;
}
session->imap_response_info = mailimap_response_info_new();
if (session->imap_response_info == NULL) {
/* ignored error */
return;
}
if (response->rsp_cont_req_or_resp_data_list != NULL) {
for(cur = clist_begin(response->rsp_cont_req_or_resp_data_list) ;
cur != NULL ; cur = clist_next(cur)) {
struct mailimap_cont_req_or_resp_data * cont_req_or_resp_data;
cont_req_or_resp_data = clist_content(cur);
cont_req_or_resp_data_store(session, cont_req_or_resp_data);
}
}
response_done_store(session, response->rsp_resp_done);
}
static void resp_cond_auth_store(mailimap * session,
struct mailimap_resp_cond_auth * cond_auth)
{
resp_text_store(session, cond_auth->rsp_text);
}
static void greeting_store(mailimap * session,
struct mailimap_greeting * greeting)
{
switch (greeting->gr_type) {
case MAILIMAP_GREETING_RESP_COND_AUTH:
resp_cond_auth_store(session, greeting->gr_data.gr_auth);
break;
case MAILIMAP_GREETING_RESP_COND_BYE:
resp_cond_bye_store(session, greeting->gr_data.gr_bye);
break;
}
}
LIBETPAN_EXPORT
int mailimap_connect(mailimap * session, mailstream * s)
{
struct mailimap_greeting * greeting;
int r;
int auth_type;
struct mailimap_connection_info * connection_info;
if (session->imap_state != MAILIMAP_STATE_DISCONNECTED)
return MAILIMAP_ERROR_BAD_STATE;
session->imap_stream = s;
mailstream_set_logger(s, imap_logger, session);
if (session->imap_connection_info)
mailimap_connection_info_free(session->imap_connection_info);
connection_info = mailimap_connection_info_new();
if (connection_info != NULL)
session->imap_connection_info = connection_info;
if (mailimap_read_line(session) == NULL) {
return MAILIMAP_ERROR_STREAM;
}
r = parse_greeting(session, &greeting);
if (r != MAILIMAP_NO_ERROR) {
return r;
}
auth_type = greeting->gr_data.gr_auth->rsp_type;
mailimap_greeting_free(greeting);
switch (auth_type) {
case MAILIMAP_RESP_COND_AUTH_PREAUTH:
session->imap_state = MAILIMAP_STATE_AUTHENTICATED;
return MAILIMAP_NO_ERROR_AUTHENTICATED;
default:
session->imap_state = MAILIMAP_STATE_NON_AUTHENTICATED;
return MAILIMAP_NO_ERROR_NON_AUTHENTICATED;
}
}
/* ********************************************************************** */
LIBETPAN_EXPORT
int mailimap_append(mailimap * session, const char * mailbox,
struct mailimap_flag_list * flag_list,
struct mailimap_date_time * date_time,
const char * literal, size_t literal_size)
{
struct mailimap_response * response;
int r;
int error_code;
struct mailimap_continue_req * cont_req;
size_t indx;
size_t fixed_literal_size;
if ((session->imap_state != MAILIMAP_STATE_AUTHENTICATED) &&
(session->imap_state != MAILIMAP_STATE_SELECTED))
return MAILIMAP_ERROR_BAD_STATE;
r = mailimap_send_current_tag(session);
if (r != MAILIMAP_NO_ERROR)
return r;
fixed_literal_size = mailstream_get_data_crlf_size(literal, literal_size);
r = mailimap_append_send(session->imap_stream, mailbox, flag_list, date_time,
fixed_literal_size);
if (r != MAILIMAP_NO_ERROR)
return r;
if (mailstream_flush(session->imap_stream) == -1)
return MAILIMAP_ERROR_STREAM;
if (mailimap_read_line(session) == NULL)
return MAILIMAP_ERROR_STREAM;
indx = 0;
r = mailimap_continue_req_parse(session->imap_stream,
session->imap_stream_buffer,
&indx, &cont_req,
session->imap_progr_rate, session->imap_progr_fun);
if (r == MAILIMAP_NO_ERROR)
mailimap_continue_req_free(cont_req);
if (r == MAILIMAP_ERROR_PARSE) {
r = mailimap_parse_response(session, &response);
if (r != MAILIMAP_NO_ERROR)
return r;
mailimap_response_free(response);
return MAILIMAP_ERROR_APPEND;
}
if (session->imap_body_progress_fun != NULL) {
r = mailimap_literal_data_send_with_context(session->imap_stream, literal, literal_size,
session->imap_body_progress_fun,
session->imap_progress_context);
}
else {
r = mailimap_literal_data_send(session->imap_stream, literal, literal_size,
session->imap_progr_rate, session->imap_progr_fun);
}
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_crlf_send(session->imap_stream);
if (r != MAILIMAP_NO_ERROR)
return r;
if (mailstream_flush(session->imap_stream) == -1)
return MAILIMAP_ERROR_STREAM;
if (mailimap_read_line(session) == NULL)
return MAILIMAP_ERROR_STREAM;
r = mailimap_parse_response(session, &response);
if (r != MAILIMAP_NO_ERROR)
return r;
error_code = response->rsp_resp_done->rsp_data.rsp_tagged->rsp_cond_state->rsp_type;
mailimap_response_free(response);
switch (error_code) {
case MAILIMAP_RESP_COND_STATE_OK:
return MAILIMAP_NO_ERROR;
default:
return MAILIMAP_ERROR_APPEND;
}
}
LIBETPAN_EXPORT
int mailimap_noop(mailimap * session)
{
struct mailimap_response * response;
int r;
int error_code;
r = mailimap_send_current_tag(session);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_noop_send(session->imap_stream);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_crlf_send(session->imap_stream);
if (r != MAILIMAP_NO_ERROR)
return r;
if (mailstream_flush(session->imap_stream) == -1)
return MAILIMAP_ERROR_STREAM;
if (mailimap_read_line(session) == NULL)
return MAILIMAP_ERROR_STREAM;
r = mailimap_parse_response(session, &response);
if (r != MAILIMAP_NO_ERROR)
return r;
error_code = response->rsp_resp_done->rsp_data.rsp_tagged->rsp_cond_state->rsp_type;
mailimap_response_free(response);
switch (error_code) {
case MAILIMAP_RESP_COND_STATE_OK:
return MAILIMAP_NO_ERROR;
default:
return MAILIMAP_ERROR_NOOP;
}
}
LIBETPAN_EXPORT
int mailimap_logout(mailimap * session)
{
struct mailimap_response * response;
int r;
int error_code;
int res;
r = mailimap_send_current_tag(session);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto close;
}
r = mailimap_logout_send(session->imap_stream);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto close;
}
r = mailimap_crlf_send(session->imap_stream);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto close;
}
if (mailstream_flush(session->imap_stream) == -1) {
res = MAILIMAP_ERROR_STREAM;
goto close;
}
if (mailimap_read_line(session) == NULL) {
res = MAILIMAP_ERROR_STREAM;
goto close;
}
r = mailimap_parse_response(session, &response);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto close;
}
error_code = response->rsp_resp_done->rsp_data.rsp_tagged->rsp_cond_state->rsp_type;
mailimap_response_free(response);
switch (error_code) {
case MAILIMAP_RESP_COND_STATE_OK:
if (session->imap_connection_info) {
mailimap_connection_info_free(session->imap_connection_info);
session->imap_connection_info = NULL;
}
res = MAILIMAP_NO_ERROR;
goto close;
default:
res = MAILIMAP_ERROR_LOGOUT;
goto close;
}
close:
mailstream_close(session->imap_stream);
session->imap_stream = NULL;
session->imap_state = MAILIMAP_STATE_DISCONNECTED;
return res;
}
/* send the results back to the caller */
/* duplicate the result */
static struct mailimap_capability *
mailimap_capability_dup(struct mailimap_capability * orig_cap)
{
struct mailimap_capability * cap;
char * auth_type;
char * name;
name = NULL;
auth_type = NULL;
switch (orig_cap->cap_type) {
case MAILIMAP_CAPABILITY_NAME:
name = strdup(orig_cap->cap_data.cap_name);
if (name == NULL)
goto err;
break;
case MAILIMAP_CAPABILITY_AUTH_TYPE:
auth_type = strdup(orig_cap->cap_data.cap_auth_type);
if (auth_type == NULL)
goto err;
break;
}
cap = mailimap_capability_new(orig_cap->cap_type, auth_type, name);
if (cap == NULL)
goto free;
return cap;
free:
if (name != NULL)
free(name);
if (auth_type != NULL)
free(auth_type);
err:
return NULL;
}
static struct mailimap_capability_data *
mailimap_capability_data_dup(struct mailimap_capability_data * orig_cap_data)
{
struct mailimap_capability_data * cap_data;
struct mailimap_capability * cap_dup;
clist * list;
clistiter * cur;
int r;
list = clist_new();
if (list == NULL)
goto err;
for(cur = clist_begin(orig_cap_data->cap_list) ;
cur != NULL ; cur = clist_next(cur)) {
struct mailimap_capability * cap;
cap = clist_content(cur);
cap_dup = mailimap_capability_dup(cap);
if (cap_dup == NULL)
goto list;
r = clist_append(list, cap_dup);
if (r < 0) {
mailimap_capability_free(cap_dup);
goto list;
}
}
cap_data = mailimap_capability_data_new(list);
if (cap_data == NULL)
goto list;
return cap_data;
list:
clist_foreach(list, (clist_func) mailimap_capability_free, NULL);
clist_free(list);
err:
return NULL;
}
LIBETPAN_EXPORT
int mailimap_capability(mailimap * session,
struct mailimap_capability_data ** result)
{
struct mailimap_response * response;
int r;
int error_code;
struct mailimap_capability_data * cap_data;
r = mailimap_send_current_tag(session);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_capability_send(session->imap_stream);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_crlf_send(session->imap_stream);
if (r != MAILIMAP_NO_ERROR)
return r;
if (mailstream_flush(session->imap_stream) == -1)
return MAILIMAP_ERROR_STREAM;
if (mailimap_read_line(session) == NULL)
return MAILIMAP_ERROR_STREAM;
r = mailimap_parse_response(session, &response);
if (r != MAILIMAP_NO_ERROR)
return r;
error_code = response->rsp_resp_done->rsp_data.rsp_tagged->rsp_cond_state->rsp_type;
mailimap_response_free(response);
switch (error_code) {
case MAILIMAP_RESP_COND_STATE_OK:
cap_data =
mailimap_capability_data_dup(session->imap_connection_info->imap_capability);
if (cap_data == NULL)
return MAILIMAP_ERROR_MEMORY;
* result = cap_data;
return MAILIMAP_NO_ERROR;
default:
return MAILIMAP_ERROR_CAPABILITY;
}
}
LIBETPAN_EXPORT
int mailimap_check(mailimap * session)
{
struct mailimap_response * response;
int r;
int error_code;
if (session->imap_state != MAILIMAP_STATE_SELECTED)
return MAILIMAP_ERROR_BAD_STATE;
r = mailimap_send_current_tag(session);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_check_send(session->imap_stream);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_crlf_send(session->imap_stream);
if (r != MAILIMAP_NO_ERROR)
return r;
if (mailstream_flush(session->imap_stream) == -1)
return MAILIMAP_ERROR_STREAM;
if (mailimap_read_line(session) == NULL)
return MAILIMAP_ERROR_STREAM;
r = mailimap_parse_response(session, &response);
if (r != MAILIMAP_NO_ERROR)
return r;
error_code = response->rsp_resp_done->rsp_data.rsp_tagged->rsp_cond_state->rsp_type;
mailimap_response_free(response);
switch (error_code) {
case MAILIMAP_RESP_COND_STATE_OK:
return MAILIMAP_NO_ERROR;
default:
return MAILIMAP_ERROR_CHECK;
}
}
LIBETPAN_EXPORT
int mailimap_close(mailimap * session)
{
struct mailimap_response * response;
int r;
int error_code;
if (session->imap_state != MAILIMAP_STATE_SELECTED)
return MAILIMAP_ERROR_BAD_STATE;
r = mailimap_send_current_tag(session);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_close_send(session->imap_stream);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_crlf_send(session->imap_stream);
if (r != MAILIMAP_NO_ERROR)
return r;
if (mailstream_flush(session->imap_stream) == -1)
return MAILIMAP_ERROR_STREAM;
if (mailimap_read_line(session) == NULL)
return MAILIMAP_ERROR_STREAM;
r = mailimap_parse_response(session, &response);
if (r != MAILIMAP_NO_ERROR)
return r;
error_code = response->rsp_resp_done->rsp_data.rsp_tagged->rsp_cond_state->rsp_type;
mailimap_response_free(response);
switch (error_code) {
case MAILIMAP_RESP_COND_STATE_OK:
/* leave selected state */
mailimap_selection_info_free(session->imap_selection_info);