forked from sba1/simplemail
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimap.c
2772 lines (2374 loc) · 70.2 KB
/
imap.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
/***************************************************************************
SimpleMail - Copyright (C) 2000 Hynek Schlawack and Sebastian Bauer
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 Place, Suite 330, Boston, MA 02111-1307 USA
***************************************************************************/
/**
*
* @brief Provides IMAP4 support.
*
* Functions in this file are responsible for the communication with IMAP4 servers.
* The communications happens in a separate thread of name #IMAP_THREAD_NAME.
*
* @file imap.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include "account.h"
#include "codesets.h"
#include "debug.h"
#include "mail.h"
#include "folder.h"
#include "lists.h"
#include "parse.h"
#include "progmon.h"
#include "qsort.h"
#include "simplemail.h"
#include "smintl.h"
#include "subthreads.h"
#include "support_indep.h"
#include "status.h"
#include "tcp.h"
#include "support.h"
#include "tcpip.h"
#include "imap.h"
#ifdef _AMIGA
#undef printf
#endif
#define MIN(a,b) ((a)<(b)?(a):(b))
/**
* The name of the IMAP thread.
*/
#define IMAP_THREAD_NAME "SimpleMail - IMAP thread"
/**
* Maximum number of mails that can be hold before a refresh is invoked
*/
#define MAX_MAILS_PER_REFRESH 100
/* TODO: Get rid of this one */
static int val;
#define RM_FLAG_SEEN (1L<<0)
#define RM_FLAG_ANSWERED (1L<<1)
#define RM_FLAG_FLAGGED (1L<<2)
struct remote_mail
{
unsigned int uid;
unsigned int flags;
unsigned int size;
/* only if headers are requested */
char *headers;
};
struct local_mail
{
unsigned int uid;
unsigned int todel;
};
/**
* Returns the local mail array of a given folder. 0 for failure (does not
* change the contents of the ptrs in that case). Free the array with free()
* as soon as no longer needed. The mail array is sorted according to the uid
* field.
*
* @param folder
* @param local_mail_array_ptr
* @param num_of_mails_ptr
* @param num_of_todel_mails_ptr
* @return 0 on failure, otherwise something different.
*/
static int get_local_mail_array(struct folder *folder, struct local_mail **local_mail_array_ptr, int *num_of_mails_ptr, int *num_of_todel_mails_ptr)
{
struct local_mail *local_mail_array;
int num_of_mails, num_of_todel_mails;
void *handle = NULL;
int i,success = 0;
SM_ENTER;
folder_lock(folder);
folder_next_mail(folder,&handle);
num_of_mails = folder->num_mails;
num_of_todel_mails = 0;
if ((local_mail_array = malloc(sizeof(*local_mail_array) * num_of_mails)))
{
/* fill in the uids of the mails */
for (i=0;i < num_of_mails;i++)
{
if (folder->mail_info_array[i] && folder->mail_info_array[i]->filename[0] == 'u')
{
local_mail_array[i].uid = atoi(folder->mail_info_array[i]->filename + 1);
local_mail_array[i].todel = mail_is_marked_as_deleted(folder->mail_info_array[i]);
num_of_todel_mails += !!local_mail_array[i].todel;
} else
{
local_mail_array[i].uid = 0;
local_mail_array[i].todel = 0;
}
}
/* now sort them */
#define local_mail_lt(a,b) ((a->uid < b->uid)?1:0)
QSORT(struct local_mail, local_mail_array, num_of_mails, local_mail_lt);
success = 1;
*local_mail_array_ptr = local_mail_array;
*num_of_mails_ptr = num_of_mails;
*num_of_todel_mails_ptr = num_of_todel_mails;
}
folder_unlock(folder);
SM_DEBUGF(20, ("num_of_mails=%d, num_of_todel_mails=%d\n", num_of_mails, num_of_todel_mails));
SM_RETURN(success,"%d");
return success;
}
/**
* Delete local mails that are not listed in the remote mail array (aka orphaned messages).
*
* @param local_mail_array
* @param num_of_local_mails length of local_mail_array
* @param remote_mail_array
* @param num_remote_mails length of remote_mail_array
* @param imap_server
* @param imap_folder
*/
static void imap_delete_orphan_messages(struct local_mail *local_mail_array, int num_of_local_mails, struct remote_mail *remote_mail_array, int num_remote_mails, struct imap_server *imap_server, char *imap_folder)
{
int i,j;
SM_ENTER;
SM_DEBUGF(20,("num_of_local_mails=%d, num_remote_mails=%d\n", num_of_local_mails, num_remote_mails));
i = j = 0;
while (i<num_of_local_mails && j<num_remote_mails)
{
unsigned int local_uid = local_mail_array[i].uid;
unsigned int remote_uid = remote_mail_array[j].uid;
if (local_uid < remote_uid)
{
if (local_uid) thread_call_parent_function_sync(NULL,callback_delete_mail_by_uid,4,imap_server->login,imap_server->name,imap_folder,local_uid);
i++;
}
else if (local_uid > remote_uid) j++;
else
{
i++;
/* FIXME: If local mail list would not (possibly) contain ties, we
* could increment j as well */
}
}
/* Delete the rest */
for (;i<num_of_local_mails;i++)
{
unsigned int local_uid = local_mail_array[i].uid;
if (local_uid) thread_call_parent_function_sync(NULL,callback_delete_mail_by_uid,4,imap_server->login,imap_server->name,imap_folder,local_uid);
}
SM_LEAVE;
}
/**
* Frees the given name list
* @param list
*/
static void imap_free_name_list(struct list *list)
{
if (!list) return;
string_list_clear(list);
free(list);
}
/**
* Writes the next word into the dest buffer but not more than dest_size.
*
* @param src
* @param dest
* @param dest_size
* @return
*/
static char *imap_get_result(char *src, char *dest, int dest_size)
{
char c;
char delim = 0;
dest_size--;
dest[0] = 0;
if (!src) return NULL;
while ((c = *src))
{
if (!isspace((unsigned char)c))
break;
src++;
}
if (c)
{
int i = 0;
int b = 0;
int incr_delim = 0;
if (c == '(') { incr_delim = c; delim = ')';}
else if (c== '"') delim = '"';
else if (c== '[') delim = ']';
if (delim)
{
src++;
b++;
}
while ((c = *src))
{
if (c == incr_delim)
{
b++;
} else
if (c == delim && !(--b))
{
src++;
break;
}
if (!delim)
{
if (isspace((unsigned char)c)) break;
}
if (i<dest_size)
dest[i++] = c;
src++;
}
dest[i] = 0;
return src;
}
return NULL;
}
/**************************************************************************
Create back a RFC822 Adress Field from an Address part of an envelope
**************************************************************************/
#if 0
static char *imap_build_address_header(char *str)
{
char buf[360];
char name[100];
char nil[100];
char user[100];
char domain[100];
if (imap_get_result(str,buf,sizeof(buf)))
{
char *addr;
int addr_len;
int use_name = 0;
char *temp = buf;
if (strncmp(temp,"NIL",3)) use_name = 1;
temp = imap_get_result(temp,name,sizeof(name));
temp = imap_get_result(temp,nil,sizeof(nil));
temp = imap_get_result(temp,user,sizeof(user));
temp = imap_get_result(temp,domain,sizeof(domain));
addr_len = (use_name?(strlen(name)+6):0) + strlen(user) + strlen(domain) + 10;
if ((addr = malloc(addr_len)))
{
char *fmt;
if (use_name)
{
if (needs_quotation(name)) fmt = "\"%s\" <%s@%s>";
else fmt = "%s <%s@%s>";
sm_snprintf(addr,addr_len,fmt,name,user,domain);
} else
{
sm_snprintf(addr,addr_len,"%s@%s",user,domain);
}
return addr;
}
}
return NULL;
}
#endif
/**
* Send a simple imap command only to check for success/failure.
*
* @param conn
* @param cmd
* @return
*/
static int imap_send_simple_command(struct connection *conn, char *cmd)
{
char send[200];
char tag[20];
char buf[380];
char *line;
int success;
/* Now really remove the message */
sprintf(tag,"%04x",val++);
sm_snprintf(send,sizeof(send),"%s %s\r\n",tag,cmd);
tcp_write(conn,send,strlen(send));
tcp_flush(conn);
success = 0;
while ((line = tcp_readln(conn)))
{
line = imap_get_result(line,buf,sizeof(buf));
if (!mystricmp(buf,tag))
{
line = imap_get_result(line,buf,sizeof(buf));
if (!mystricmp(buf,"OK"))
success = 1;
break;
}
}
return success;
}
/**
* Waits for an OK after an connect, i.e., until login credentials are requested.
*
* @param conn
* @param server
* @return
*/
static int imap_wait_login(struct connection *conn, struct imap_server *server)
{
char *line;
char buf[100];
/* At the moment the loop is not necessary */
while ((line = tcp_readln(conn)))
{
SM_DEBUGF(20,("recv: %s",line));
line = imap_get_result(line,buf,sizeof(buf));
line = imap_get_result(line,buf,sizeof(buf));
if (!mystricmp(buf,"OK")) return 1;
else return 0;
}
return 0;
}
/**
* Perform a login for the given connection to the given imap server.
*
* @param conn
* @param server
* @return
*/
static int imap_login(struct connection *conn, struct imap_server *server)
{
char *line;
char tag[16];
char send[200];
char buf[100];
sprintf(tag,"%04x",val++);
/* Logging */
sm_snprintf(send,sizeof(send),"%s LOGIN %s %s", tag, server->login, "XXX");
SM_DEBUGF(20,("send: %s\n",send));
/* Build the IMAP command */
if (has_spaces(server->passwd))
sm_snprintf(buf,sizeof(buf),"\"%s\"", server->passwd);
else
mystrlcpy(buf,server->passwd,sizeof(buf));
sm_snprintf(send,sizeof(send),"%s LOGIN %s %s\r\n", tag, server->login, buf);
tcp_write(conn,send,strlen(send));
tcp_flush(conn);
while ((line = tcp_readln(conn)))
{
SM_DEBUGF(20,("recv: %s",line));
line = imap_get_result(line,buf,sizeof(buf));
if (!mystricmp(buf,tag))
{
line = imap_get_result(line,buf,sizeof(buf));
if (!mystricmp(buf,"OK"))
{
return 1;
}
break;
}
}
return 0;
}
/** Describes a remote mailbox and its contents */
struct remote_mailbox
{
struct remote_mail *remote_mail_array; /* may be NULL if remote_mail_num == 0 */
int num_of_remote_mail;
unsigned int uid_validity;
unsigned int uid_next;
};
/**
* Frees memory associated with the remote mailbox including the remote mailbox itself.
* @param rm
*/
static void imap_free_remote_mailbox(struct remote_mailbox *rm)
{
int i;
if (!rm) return;
if (rm->remote_mail_array)
{
for (i=0; i < rm->num_of_remote_mail; i++)
free(rm->remote_mail_array[i].headers);
}
free(rm->remote_mail_array);
free(rm);
}
/**
* Selects the given mailbox (as identified by path).
*
* @param conn defines the connection
* @param path defines the utf8 encoded path
* @param writemode whether mailbox should be selected in mailbox.
* @return a rmemote_mailbox. Field remote_mail_array will be NULL.
*/
static struct remote_mailbox *imap_select_mailbox(struct connection *conn, char *path, int writemode)
{
struct remote_mailbox *rm;
char status_buf[200];
char send[200];
char buf[512];
char tag[20];
char *line;
int success = 0;
unsigned int uid_validity = 0; /* Note that valid uids are non-zero */
unsigned int uid_next = 0;
int num_of_remote_mails = 0;
if (!path) return NULL;
if (!(path = utf8toiutf7(path,strlen(path)))) return NULL;
if (!(rm = (struct remote_mailbox*)malloc(sizeof(*rm)))) return NULL;
memset(rm,0,sizeof(*rm));
sm_snprintf(status_buf,sizeof(status_buf),_("Examining folder %s"),path);
thread_call_parent_function_sync(NULL,status_set_status,1,status_buf);
sprintf(tag,"%04x",val++);
sm_snprintf(send,sizeof(send),"%s %s \"%s\"\r\n",tag,writemode?"SELECT":"EXAMINE",path);
SM_DEBUGF(10,("Examining folder %s: %s",path,send));
tcp_write(conn,send,strlen(send));
tcp_flush(conn);
while ((line = tcp_readln(conn)))
{
line = imap_get_result(line,buf,sizeof(buf));
SM_DEBUGF(10,("Server: %s",line));
if (!mystricmp(buf,tag))
{
line = imap_get_result(line,buf,sizeof(buf));
if (!mystricmp(buf,"OK"))
success = 1;
break;
} else
{
/* untagged */
char first[200];
char second[200];
line = imap_get_result(line,first,sizeof(first));
line = imap_get_result(line,second,sizeof(second));
if (!mystricmp("EXISTS",second))
{
num_of_remote_mails = atoi(first);
} else if (!mystricmp("OK",first))
{
/* Store first identifier of valid untagged response in first */
line = imap_get_result(second,first,sizeof(first));
if (!mystricmp("UIDVALIDITY",first))
{
/* [UIDVALIDITY n] */
imap_get_result(line,first,sizeof(first));
uid_validity = strtoul(first,NULL,10);
} else if (!mystricmp("UIDNEXT",first))
{
/* [UIDNEXT n] */
imap_get_result(line,first,sizeof(first));
uid_next = strtoul(first,NULL,10);
}
}
}
}
if (success)
{
sm_snprintf(status_buf,sizeof(status_buf),_("Identified %d mails in %s"),num_of_remote_mails,path);
thread_call_parent_function_async_string(status_set_status,1,status_buf);
SM_DEBUGF(10,("Identified %d mails in %s (uid_validity=%u, uid_next=%u)\n",num_of_remote_mails,path,uid_validity,uid_next));
rm->uid_next = uid_next;
rm->uid_validity = uid_validity;
rm->num_of_remote_mail = num_of_remote_mails;
} else
{
thread_call_function_async(thread_get_main(),status_set_status,1,N_("Failed examining the folder"));
SM_DEBUGF(10,("Failed examining the folder\n"));
free(rm);
rm = NULL;
}
free(path);
return rm;
}
/**
* Read information of all mails in the given path. Put
* this back into an array.
*
* @param conn defines the connection
* @param path defines the utf8 encoded path.
* @param writemode
* @param headers specifies whether headers are requested.
* @param uid_start
* @param uid_end
*
* @return returns information of the mailbox in form of a remote_mailbox object.
* NULL on failure (for any reasons). If not NULL, the elements in remote_mail_array
* are sorted according to their uids.
*
* @note the given path stays in the selected/examine state.
* @note the returned structure must be free with imap_free_remote_mailbox()
*/
static struct remote_mailbox *imap_get_remote_mails(struct connection *conn, char *path, int writemode, int headers, unsigned int uid_start, unsigned int uid_end)
{
/* get number of remote mails */
char *line;
char tag[20];
char send[200];
char buf[2048];
int num_of_remote_mails = 0;
int success = 0;
struct remote_mail *remote_mail_array = NULL;
struct remote_mailbox *rm;
SM_ENTER;
if (!(rm = imap_select_mailbox(conn,path,writemode)))
{
SM_RETURN(NULL,"%p");
return NULL;
}
if (!uid_start) uid_end = 0;
else if (!uid_end) uid_start = 0;
if ((num_of_remote_mails = rm->num_of_remote_mail))
{
if ((remote_mail_array = malloc(sizeof(struct remote_mail)*num_of_remote_mails)))
{
unsigned int max_uid = 0; /* Max UID discovered so far */
unsigned int fetch_time_ref;
int needs_to_be_sorted = 0;
fetch_time_ref = time_reference_ticks();
memset(remote_mail_array,0,sizeof(struct remote_mail)*num_of_remote_mails);
sprintf(tag,"%04x",val++);
sm_snprintf(send,sizeof(send),"%s %sFETCH %d:%d (UID FLAGS RFC822.SIZE%s)\r\n",tag,uid_start?"UID ":"",uid_start?uid_start:1,uid_start?uid_end:num_of_remote_mails,headers?" BODY[HEADER.FIELDS (FROM DATE SUBJECT TO CC)]":"");
SM_DEBUGF(10,("Fetching remote mail array: %s",send));
tcp_write(conn,send,strlen(send));
tcp_flush(conn);
while ((line = tcp_readln(conn)))
{
SM_DEBUGF(10,("Server: %s",line));
line = imap_get_result(line,buf,sizeof(buf));
if (!mystricmp(buf,tag))
{
line = imap_get_result(line,buf,sizeof(buf));
if (!mystricmp(buf,"OK")) success = 1;
break;
} else
{
/* untagged */
unsigned int msgno;
int is_mail = 0;
unsigned int uid = 0;
unsigned int flags = 0;
unsigned int size = 0;
char *headers = NULL;
char msgno_buf[100];
char stuff_buf[1024];
char cmd_buf[1024];
char *temp;
int i;
line = imap_get_result(line,msgno_buf,sizeof(msgno_buf));
line = imap_get_result(line,cmd_buf,sizeof(cmd_buf));
imap_get_result(line,stuff_buf,sizeof(stuff_buf)); /* don't update the line because BODY[HEADER.FIELDS] would be skipped and because it is parsed diffently */
msgno = (unsigned int)atoi(msgno_buf);
temp = stuff_buf;
for (i=0;i<4;i++)
{
temp = imap_get_result(temp,cmd_buf,sizeof(cmd_buf));
if (!mystricmp(cmd_buf,"UID"))
{
temp = imap_get_result(temp,cmd_buf,sizeof(cmd_buf));
uid = atoi(cmd_buf);
is_mail = 1;
}
else if (!mystricmp(cmd_buf,"FLAGS"))
{
temp = imap_get_result(temp,cmd_buf,sizeof(cmd_buf));
if (strstr(cmd_buf,"\\Seen")) flags |= RM_FLAG_SEEN;
if (strstr(cmd_buf,"\\Answered")) flags |= RM_FLAG_ANSWERED;
if (strstr(cmd_buf,"\\Flagged")) flags |= RM_FLAG_FLAGGED;
}
else if (!mystricmp(cmd_buf,"RFC822.SIZE"))
{
temp = imap_get_result(temp,cmd_buf,sizeof(cmd_buf));
size = atoi(cmd_buf);
}
else if (!mystrnicmp(cmd_buf,"BODY",4))
{
char *temp_ptr;
int todownload;
if ((temp_ptr = strchr(line,'{'))) /* } - avoid bracket checking problems */
{
temp_ptr++;
todownload = atoi(temp_ptr);
} else todownload = 0;
if (todownload)
{
int pos = 0;
if ((headers = malloc(todownload+1)))
{
headers[todownload]=0;
while (todownload)
{
int dl;
dl = tcp_read(conn,headers + pos,todownload);
if (dl == -1 || !dl) break;
todownload -= dl;
pos += dl;
}
}
}
}
}
if (msgno <= num_of_remote_mails && msgno > 0 && is_mail)
{
remote_mail_array[msgno-1].uid = uid;
remote_mail_array[msgno-1].flags = flags;
remote_mail_array[msgno-1].size = size;
remote_mail_array[msgno-1].headers = headers;
if (uid < max_uid) needs_to_be_sorted = 1;
else max_uid = uid;
} else
{
free(headers);
}
}
}
if (needs_to_be_sorted)
{
SM_DEBUGF(10,("Sorting remote array\n"));
#define remote_mail_lt(a,b) ((a->uid < b->uid)?1:0)
QSORT(struct remote_mail, remote_mail_array, num_of_remote_mails, remote_mail_lt);
}
SM_DEBUGF(10,("Remote mail array fetched after %d ms\n",time_ms_passed(fetch_time_ref)));
}
}
if (!success)
{
free(remote_mail_array);
free(rm);
rm = NULL;
} else
{
rm->remote_mail_array = remote_mail_array;
rm->num_of_remote_mail = num_of_remote_mails;
}
SM_RETURN(rm, "%p");
return rm;
}
/**
* Returns a list with string_node nodes which describes the folder names.
* If you only want the subscribed folders set all to 0. Note that the
* INBOX folder is always included if it does exist.
*
* @param conn the connection to write against
* @param all whether all folders shall be returned or only the subscribed ones.
* @return the list with string_nodes
*/
static struct list *imap_get_folders(struct connection *conn, int all)
{
int ok;
char *line;
char tag[20];
char send[200];
char buf[100];
struct list *list = malloc(sizeof(struct list));
if (!list) return NULL;
list_init(list);
ok = 0;
sprintf(tag,"%04x",val++);
sprintf(send,"%s %s \"\" *\r\n",tag,all?"LIST":"LSUB");
tcp_write(conn,send,strlen(send));
tcp_flush(conn);
SM_DEBUGF(20,("%s",send));
while ((line = tcp_readln(conn)))
{
SM_DEBUGF(20,("%s\n",line));
line = imap_get_result(line,buf,sizeof(buf));
if (!mystricmp(buf,tag))
{
line = imap_get_result(line,buf,sizeof(buf));
if (!mystricmp(buf,"OK")) ok = 1;
break;
} else
{
char *utf_name;
/* command */
line = imap_get_result(line,buf,sizeof(buf));
/* read flags */
line = imap_get_result(line,buf,sizeof(buf));
/* read delim */
line = imap_get_result(line,buf,sizeof(buf));
/* read name */
line = imap_get_result(line,buf,sizeof(buf));
if ((utf_name = iutf7ntoutf8(buf, strlen(buf))))
{
string_list_insert_tail(list,utf_name);
free(utf_name);
}
}
}
/* Some IMAP servers don't list the INBOX server on LSUB and don't allow subscribing of it,
* so we add it manually in case it exists*/
if (ok && !all && !string_list_find(list,"INBOX"))
{
sprintf(tag,"%04x",val++);
sprintf(send,"%s STATUS INBOX (MESSAGES)\r\n",tag);
tcp_write(conn,send,strlen(send));
tcp_flush(conn);
SM_DEBUGF(20,("%s",send));
while ((line = tcp_readln(conn)))
{
SM_DEBUGF(20,("%s\n",line));
line = imap_get_result(line,buf,sizeof(buf));
if (!mystricmp(buf,tag))
{
line = imap_get_result(line,buf,sizeof(buf));
if (!mystricmp(buf,"OK"))
string_list_insert_tail(list,"INBOX");
break;
}
}
}
if (ok) return list;
imap_free_name_list(list);
return NULL;
}
/**
* Synchronize the folder.
*
* @param conn
* @param server
* @param imap_path
* @return
*/
static int imap_synchonize_folder(struct connection *conn, struct imap_server *server, char *imap_path)
{
struct folder *folder;
int success = 0;
SM_DEBUGF(10,("Synchronizing folder \"%s\"\n",imap_path));
folders_lock();
if ((folder = folder_find_by_imap(server->login,server->name, imap_path)))
{
int num_of_local_mails;
int num_of_todel_local_mails;
struct local_mail *local_mail_array;
char path[380];
/* Get the local mails */
if (!get_local_mail_array(folder, &local_mail_array, &num_of_local_mails, &num_of_todel_local_mails))
{
folder_unlock(folder);
folders_unlock();
return 0;
}
/* Change the current directory to the to be synchronized folder */
folder_lock(folder);
getcwd(path, sizeof(path));
if (chdir(folder->path) == -1)
{
free(local_mail_array);
folder_unlock(folder);
folders_unlock();
return 0;
}
folder_unlock(folder);
folders_unlock();
if (local_mail_array)
{
struct remote_mailbox *rm;
/* get number of remote mails */
char *line;
char tag[20];
char send[200];
char buf[100];
char status_buf[200];
if (num_of_todel_local_mails)
{
if ((rm = imap_get_remote_mails(conn, folder->imap_path, 1, 0, 0, 0)))
{
struct remote_mail *remote_mail_array;
int num_of_remote_mails;
int i,j;
remote_mail_array = rm->remote_mail_array;
num_of_remote_mails = rm->num_of_remote_mail;
sm_snprintf(status_buf,sizeof(status_buf),_("Removing %d mails from server"),num_of_todel_local_mails);
thread_call_parent_function_async_string(status_set_status, 1, status_buf);
for (i = 0 ; i < num_of_local_mails; i++)
{
if (mail_is_marked_as_deleted(folder->mail_info_array[i]))
{
for (j = 0; j < num_of_remote_mails; j++)
{
if (local_mail_array[i].uid == remote_mail_array[j].uid)
{
sprintf(tag,"%04x",val++);
sprintf(send,"%s STORE %d +FLAGS.SILENT (\\Deleted)\r\n",tag,j+1);
tcp_write(conn,send,strlen(send));
tcp_flush(conn);
success = 0;
while ((line = tcp_readln(conn)))
{
line = imap_get_result(line,buf,sizeof(buf));
if (!mystricmp(buf,tag))
{
line = imap_get_result(line,buf,sizeof(buf));
if (!mystricmp(buf,"OK"))
success = 1;
break;
}
}
if (!success) break;
}
}
}
}
if (success)
{
sprintf(tag,"%04x",val++);
sprintf(send,"%s EXPUNGE\r\n",tag);
tcp_write(conn,send,strlen(send));
tcp_flush(conn);
success = 0;
while ((line = tcp_readln(conn)))
{
line = imap_get_result(line,buf,sizeof(buf));
if (!mystricmp(buf,tag))
{
line = imap_get_result(line,buf,sizeof(buf));
if (!mystricmp(buf,"OK"))
success = 1;
break;
}
}
}
imap_free_remote_mailbox(rm);
}
}
/* Get information of all mails within the folder */
if ((rm = imap_get_remote_mails(conn, folder->imap_path, 0, 0, 0, 0)))
{
int i,j;
unsigned int max_todl_bytes = 0;
unsigned int accu_todl_bytes = 0; /* this represents the exact todl bytes according to the RFC822.SIZE */
unsigned int todl_bytes = 0;
unsigned int num_msgs_to_dl = 0; /* number of mails that we really want to download */
struct remote_mail *remote_mail_array;
int num_of_remote_mails;
remote_mail_array = rm->remote_mail_array;
num_of_remote_mails = rm->num_of_remote_mail;
imap_delete_orphan_messages(local_mail_array,num_of_local_mails,remote_mail_array,num_of_remote_mails, server, imap_path);
/* Determine the number of bytes and mails which we are going to download
* Note that the contents of remote mail is partly destroyed. When we are
* ready, the top of the remote array contains the uids (as uids), the sizes
* (as size), and the indices (in field flags) that we want to download.
* The number of mails is kept in num_msgs_to_dl.
*/
i=j=0;
while (i<num_of_remote_mails)
{
if (j < num_of_local_mails)
{
unsigned int remote_uid = remote_mail_array[i].uid;
unsigned int local_uid = local_mail_array[j].uid;
if (remote_uid >= local_uid)