-
Notifications
You must be signed in to change notification settings - Fork 8
/
imap.c
1723 lines (1442 loc) · 45.6 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.
*
* @file imap.c
*/
#include "imap.h"
#include <ctype.h>
#include <dirent.h> /* unix dir stuff */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "account.h"
#include "codesets.h"
#include "debug.h"
#include "folder.h"
#include "imap_helper.h"
#include "logging.h"
#include "mail.h"
#include "progmon.h"
#include "qsort.h"
#include "smintl.h"
#include "support_indep.h"
#include "tcp.h"
#include "request.h"
#include "subthreads.h"
#include "support.h"
#include "tcpip.h"
#ifdef _AMIGA
#undef printf
#endif
#define MIN(a,b) ((a)<(b)?(a):(b))
/**
* Maximum number of mails that can be hold before a refresh is invoked
*/
#define MAX_MAILS_PER_REFRESH 100
/**
* Set the local mail array entry for the given entry.
*
* @param local_mail_array
* @param i
* @param fn
* @return whether the mail is scheduled to be deleted or not.
*/
static int set_local_mail_array_entry(struct local_mail *local_mail_array, int i, const char *fn)
{
int todel = mail_is_marked_as_deleted_by_filename(fn);
if (fn[0] == 'u' || fn[0] == 'U' || todel)
{
local_mail_array[i].uid = atoi(fn + 1);
local_mail_array[i].todel = todel;
} else
{
local_mail_array[i].uid = 0;
local_mail_array[i].todel = 0;
}
return 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, unsigned int *num_of_mails_ptr, unsigned int *num_of_todel_mails_ptr)
{
struct local_mail *local_mail_array;
unsigned int num_of_mails = 0, num_of_todel_mails = 0;
unsigned int i;
int success = 0;
/* Will house the names of mails in case there was no index of the folder loaded */
struct string_list filename_list;
int use_filename_list = 0;
char logg_buf[80];
SM_ENTER;
string_list_init(&filename_list);
folder_lock(folder);
if (!folder->mail_infos_loaded)
{
/* No mail infos have been loaded, as we only need the filenames
* we just scan the directory rather than issuing a full rescan.
*/
DIR *dfd;
struct dirent *dptr;
SM_DEBUGF(10, ("Scanning folder \"%s\" for mails\n", folder->path));
if (!(dfd = opendir(folder->path)))
{
sm_snprintf(logg_buf, sizeof(logg_buf), _("Couldn't get mails of locally saved IMAP folder \"%s\""), folder->name);
SM_LOG_TEXT(ERROR, logg_buf);
goto bailout;
}
use_filename_list = 1;
while ((dptr = readdir(dfd)) != NULL)
{
char *name = dptr->d_name;
if (!folder_is_filename_mail(name))
continue;
string_list_insert_tail(&filename_list, name);
num_of_mails++;
}
closedir(dfd);
} else
{
num_of_mails = folder->num_mails;
}
num_of_todel_mails = 0;
SM_DEBUGF(10, ("%d mails in folder\n", num_of_mails));
if ((local_mail_array = (struct local_mail *)malloc(sizeof(*local_mail_array) * num_of_mails)))
{
if (use_filename_list)
{
struct string_node *sn;
i = 0;
sn = string_list_first(&filename_list);
while (sn)
{
const char *fn = sn->string;
num_of_todel_mails += set_local_mail_array_entry(local_mail_array, i, fn);
sn = string_node_next(sn);
i++;
}
string_list_clear(&filename_list);
} else
{
/* fill in the uids of the mails */
for (i=0;i < num_of_mails;i++)
{
const char *fn = folder->mail_info_array[i]?folder->mail_info_array[i]->filename:"";
num_of_todel_mails += set_local_mail_array_entry(local_mail_array, i, fn);
}
}
/* 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;
}
bailout:
folder_unlock(folder);
sm_snprintf(logg_buf, sizeof(logg_buf), "Folder \"%s\" has %ld local mails, %d are scheduled for deletion", folder->name, num_of_mails, num_of_todel_mails);
logg(INFO, 0, __FILE__, NULL, 0, logg_buf, LAST);
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;
}
struct imap_delete_orphan_messages_args
{
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;
/** Callback that is invoked for each local uid that is not present in the remote mailbox */
void (*delete_mail_by_uid)(char *user, char *server, char *path, unsigned int uid);
};
/**
* Delete local mails that are not listed in the remote mail array (aka orphaned messages).
*
* @param options
*/
static void imap_delete_orphan_messages(struct imap_delete_orphan_messages_args *options)
{
int i,j;
struct local_mail *local_mail_array = options->local_mail_array;
int num_of_local_mails = options->num_of_local_mails;
struct remote_mail *remote_mail_array = options->remote_mail_array;
int num_remote_mails = options->num_remote_mails;
struct imap_server *imap_server = options->imap_server;
char *imap_folder = options->imap_folder;
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) options->delete_mail_by_uid(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) options->delete_mail_by_uid(imap_server->login,imap_server->name,imap_folder,local_uid);
}
SM_LEAVE;
}
/**
* Synchronize the folder.
*
* @param conn
* @param server
* @param imap_path
* @param callbacks
* @return 1 on success, 0 for failure.
*/
static int imap_synchonize_folder(struct connection *conn, struct imap_server *server, char *imap_path, struct imap_synchronize_callbacks *callbacks)
{
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)))
{
unsigned int num_of_local_mails;
unsigned 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))
{
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];
struct imap_get_remote_mails_args args = {0};
args.conn = conn;
args.path = folder->imap_path;
args.set_status = callbacks->set_status;
args.set_status_static = callbacks->set_status_static;
if (num_of_todel_local_mails)
{
args.writemode = 1;
if ((rm = imap_get_remote_mails(&args)))
{
struct remote_mail *remote_mail_array;
unsigned int num_of_remote_mails;
unsigned 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);
callbacks->set_status(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",imap_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",imap_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);
}
}
args.writemode = 0;
/* Get information of all mails within the folder */
if ((rm = imap_get_remote_mails(&args)))
{
unsigned 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;
unsigned int num_of_remote_mails;
remote_mail_array = rm->remote_mail_array;
num_of_remote_mails = rm->num_of_remote_mail;
if (!server->keep_orphans)
{
struct imap_delete_orphan_messages_args args = {0};
args.local_mail_array = local_mail_array;
args.num_of_local_mails = num_of_local_mails;
args.remote_mail_array = remote_mail_array;
args.num_remote_mails = num_of_remote_mails;
args.imap_server = server;
args.imap_folder = imap_path;
args.delete_mail_by_uid = callbacks->delete_mail_by_uid;
imap_delete_orphan_messages(&args);
}
/* 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)
{
/* Definitely skip local mail */
j++;
/* Skip also remote, if it matches the local uid (thus, the mail was present) */
if (remote_uid == local_uid)
i++;
continue;
}
}
max_todl_bytes += remote_mail_array[i].size;
/* Note that num_msgs_to_dl <= i. Also note that harm will be done
* if num_msgs_to_dl == i. */
remote_mail_array[num_msgs_to_dl].uid = remote_mail_array[i].uid;
remote_mail_array[num_msgs_to_dl].size = remote_mail_array[i].size;
remote_mail_array[num_msgs_to_dl].flags = i;
num_msgs_to_dl++;
i++;
}
/* Assume everything is fine */
success = 1;
callbacks->init_gauge_as_bytes(max_todl_bytes);
for (i=0;i<num_msgs_to_dl;i++)
{
accu_todl_bytes += remote_mail_array[i].size;
sm_snprintf(status_buf,sizeof(status_buf),_("Fetching mail %d from server"),remote_mail_array[i].flags + 1);
callbacks->set_status(status_buf);
sprintf(tag,"%04x",imap_val++);
sprintf(send,"%s FETCH %d RFC822\r\n",tag,remote_mail_array[i].flags+1); /* We could also use the UID command */
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;
} else
{
char msgno_buf[200];
char *temp_ptr;
unsigned int todownload;
/* We only handle untagged lines here */
if (buf[0] != '*')
continue;
line++;
line = imap_get_result(line,msgno_buf,sizeof(msgno_buf));
/*atoi(msgno_buf);*/ /* ignored */
/* skip the fetch command */
line = imap_get_result(line,msgno_buf,sizeof(msgno_buf));
if ((temp_ptr = strchr(line,'{'))) /* } - avoid bracket checking problems */
{
temp_ptr++;
todownload = (unsigned int)atoi(temp_ptr);
} else todownload = 0;
if (todownload)
{
FILE *fh;
char filename_buf[32];
sprintf(filename_buf,"u%d",remote_mail_array[i].uid); /* u means unchanged */
if ((fh = fopen(filename_buf,"w")))
{
while (todownload)
{
char buf[204];
int dl;
dl = tcp_read(conn,buf,MIN((sizeof(buf)-4),todownload));
if (dl == -1 || !dl) break;
todl_bytes = MIN(accu_todl_bytes,todl_bytes + dl);
callbacks->set_gauge(todl_bytes);
fwrite(buf,1,dl,fh);
todownload -= dl;
}
fclose(fh);
callbacks->new_mail_arrived(filename_buf, server->login, server->name, imap_path);
}
}
}
}
if (!success) break;
todl_bytes = accu_todl_bytes;
callbacks->set_gauge(todl_bytes);
}
imap_free_remote_mailbox(rm);
}
}
chdir(path);
free(local_mail_array);
} else folders_unlock();
return success;
}
/*****************************************************************************/
/**
* Synchronize a single imap account.
*
* @param imap_server
* @param options
* @return 0 for failure, 1 for success.
* @note 0 currently means abort.
*/
static int imap_synchronize_really_single(struct imap_server *server, struct imap_synchronize_callbacks *callbacks)
{
struct connection *conn;
struct imap_connect_and_login_to_server_callbacks login_callbacks = {0};
char head_buf[100];
SM_DEBUGF(10,("Synchronizing with server \"%s\"\n",server->name));
sm_snprintf(head_buf,sizeof(head_buf),_("Synchronizing mails with %s"),server->name);
callbacks->set_head(head_buf);
if (server->title)
callbacks->set_title_utf8(server->title);
else
callbacks->set_title(server->name);
callbacks->set_connect_to_server(server->name);
login_callbacks.request_login = callbacks->request_login;
login_callbacks.set_status = callbacks->set_status;
if (imap_really_connect_and_login_to_server(&conn, server, &login_callbacks))
{
struct remote_folder *rf = NULL;
int num_rf;
callbacks->set_status_static(_("Login successful"));
callbacks->set_status_static(_("Checking for folders"));
SM_DEBUGF(10,("Get folders\n"));
if ((rf = imap_get_folders(conn,0,&num_rf)))
{
int i;
/* add the folders */
for (i = 0; i < num_rf; i++)
{
callbacks->add_imap_folder(server->login, server->name, rf[i].name, rf[i].delim);
}
callbacks->refresh_folders();
/* sync the folders */
for (i = 0; i < num_rf; i++)
{
if (!(imap_synchonize_folder(conn, server, rf[i].name, callbacks)))
{
sm_snprintf(head_buf,sizeof(head_buf),_("Failed to sync folder \"%s\""), rf[i].name);
callbacks->set_status(head_buf);
break;
}
}
}
tcp_disconnect(conn);
}
if (thread_aborted()) return 0;
/* We handle only the abort case as failure for now */
return 1;
}
/*****************************************************************************/
void imap_synchronize_really(struct imap_synchronize_options *options)
{
struct imap_synchronize_callbacks *callbacks = &options->callbacks;
struct imap_server *server;
SM_ENTER;
if (!open_socket_lib())
{
tell_from_subtask(N_("Cannot open the bsdsocket.library!"));
goto out;
}
server = (struct imap_server*)list_first(options->imap_list);
while (server)
{
if (!imap_synchronize_really_single(server, callbacks))
break;
server = (struct imap_server*)node_next(&server->node);
}
close_socket_lib();
out:
SM_LEAVE;
}
/*****************************************************************************/
int imap_get_folder_list_really(struct imap_get_folder_list_options *options,
struct remote_folder **all_folders_out, int *num_all_folders_out,
struct remote_folder **sub_folders_out, int *num_sub_folders_out)
{
struct imap_get_folder_list_callbacks *callbacks = &options->callbacks;
struct connection *conn = NULL;
struct connect_options conn_opts = {0};
struct string_list *all_folder_list = NULL;
struct string_list *sub_folder_list = NULL;
char head_buf[100];
int error_code;
int rc = 0;
struct imap_server *server = options->server;
if (!open_socket_lib())
return 0;
sm_snprintf(head_buf, sizeof(head_buf), _("Reading folders of %s"),server->name);
callbacks->set_head(head_buf);
if (server->title)
callbacks->set_title_utf8(server->title);
else
callbacks->set_title(server->name);
callbacks->set_connect_to_server(server->name);
conn_opts.use_ssl = server->ssl;
conn_opts.fingerprint = server->fingerprint;
if (!(conn = tcp_connect(server->name, server->port, &conn_opts, &error_code)))
goto bailout;
callbacks->set_status_static(_("Waiting for login..."));
if (!imap_wait_login(conn,server))
goto bailout;
callbacks->set_status_static(_("Login..."));
if (!imap_login(conn,server))
{
callbacks->set_status_static(_("Login failed!"));
tell_from_subtask(N_("Authentication failed!"));
goto bailout;
}
callbacks->set_status_static(_("Reading folders..."));
if (!(*all_folders_out = imap_get_folders(conn, 1, num_all_folders_out)))
goto bailout;
if (!(*sub_folders_out = imap_get_folders(conn, 0, num_sub_folders_out)))
goto bailout;
rc = 1;
bailout:
if (!rc)
{
string_list_free(sub_folder_list);
string_list_free(all_folder_list);
}
if (conn) tcp_disconnect(conn);
close_socket_lib();
return rc;
}
/*****************************************************************************/
void imap_submit_folder_list_really(struct imap_submit_folder_options *options)
{
struct imap_server *server = options->server;
struct string_list *list = options->list;
struct imap_submit_folder_list_callbacks *callbacks = &options->callbacks;
struct connection *conn;
struct connect_options conn_opts = {0};
char head_buf[100];
int error_code;
struct remote_folder *all_folders = NULL;
struct remote_folder *sub_folders = NULL;
int i, num_all_folders = 0, num_sub_folders = 0;
char *line;
char tag[20];
char send[200];
char buf[100];
struct string_node *node;
if (!open_socket_lib())
return;
sm_snprintf(head_buf,sizeof(head_buf),_("Submitting subscribed folders to %s"),server->name);
callbacks->set_head(head_buf);
if (server->title)
callbacks->set_title_utf8(server->title);
else
callbacks->set_title(server->name);
callbacks->set_connect_to_server(server->name);
conn_opts.use_ssl = server->ssl;
conn_opts.fingerprint = server->fingerprint;
if (!(conn = tcp_connect(server->name, server->port, &conn_opts, &error_code)))
goto out;
callbacks->set_status_static(_("Waiting for login..."));
if (!imap_wait_login(conn,server))
goto out;
callbacks->set_status_static(_("Login..."));
if (!imap_login(conn,server))
goto out;
callbacks->set_status_static(_("Reading folders..."));
if (!(all_folders = imap_get_folders(conn, 1, &num_all_folders)))
goto out;
callbacks->set_status_static(_("Reading subscribed folders..."));
if (!(sub_folders = imap_get_folders(conn, 0, &num_sub_folders)))
goto out;
for (node = string_list_first(list); node; node = string_node_next(node))
{
if (!imap_remote_folder_exists(sub_folders, num_sub_folders, node->string))
{
char *path = utf8toiutf7(node->string,strlen(node->string));
if (path)
{
/* subscribe this folder */
sprintf(tag,"%04x",imap_val++);
sprintf(send,"%s SUBSCRIBE \"%s\"\r\n",tag,path);
tcp_write(conn,send,strlen(send));
tcp_flush(conn);
while ((line = tcp_readln(conn)))
{
char *saved_line = line; /* for debugging reasons */
line = imap_get_result(line,buf,sizeof(buf));
if (!mystricmp(buf,tag))
{
line = imap_get_result(line,buf,sizeof(buf));
if (mystricmp(buf,"OK"))
{
/* If it is not OK it is a failure */
SM_DEBUGF(20,("%s",send));
SM_DEBUGF(20,("%s",saved_line));
tell_from_subtask(_("Subscribing folders failed!"));
}
break;
}
}
}
free(path);
}
}
/* Unsubscribe from all folders that are not listed in the given list */
for (i = 0; i < num_sub_folders; i++)
{
if (!string_list_find(list, sub_folders[i].name))
{
char *path = utf8toiutf7(node->string,strlen(node->string));
if (path)
{
/* Unsubscribe this folder */
sprintf(tag,"%04x",imap_val++);
sprintf(send,"%s UNSUBSCRIBE \"%s\"\r\n",tag,path);
tcp_write(conn,send,strlen(send));
tcp_flush(conn);
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"))
{
/* If it is not OK , it's a failure */
tell_from_subtask(_("Unsubscribing folders failed!"));
}
break;
}
}
free(path);
}
}
}
out:
imap_folders_free(all_folders, num_all_folders);
imap_folders_free(sub_folders, num_sub_folders);
tcp_disconnect(conn);
close_socket_lib();
}
/*****************************************************************************/
int imap_really_connect_and_login_to_server(struct connection **connection, struct imap_server *imap_server, struct imap_connect_and_login_to_server_callbacks *callbacks)
{
int success = 0;
char status_buf[160];
struct connect_options conn_opts = {0};
int error_code;
struct connection *imap_connection = NULL;
SM_ENTER;
if (!imap_server)
goto bailout;
if (imap_server->ask)
{
char *password = (char*)malloc(512);
char *login = (char*)malloc(512);
if (password && login)
{
if (imap_server->login) mystrlcpy(login,imap_server->login,512);
password[0] = 0;
if (callbacks->request_login(imap_server->name,login,password,512))
{
imap_server->login = mystrdup(login);
imap_server->passwd = mystrdup(password);
} else
{
free(password);
free(login);
SM_RETURN(0,"%ld");
return 0;
}
}
free(password);
free(login);
}
/* Display "Connecting" - status message */
sm_snprintf(status_buf,sizeof(status_buf),"%s: %s",imap_server->name, _("Connecting..."));
callbacks->set_status(status_buf);
conn_opts.use_ssl = imap_server->ssl;
conn_opts.fingerprint = imap_server->fingerprint;
if ((imap_connection = tcp_connect(imap_server->name, imap_server->port, &conn_opts, &error_code)))
{
SM_DEBUGF(20,("Connected to %s\n",imap_server->name));
if (imap_wait_login(imap_connection,imap_server))
{
/* Display "Logging in" - status message */
sm_snprintf(status_buf,sizeof(status_buf),"%s: %s",imap_server->name, _("Logging in..."));
callbacks->set_status(status_buf);
if (imap_login(imap_connection,imap_server))
{
/* Display "Connected" - status message */
sm_snprintf(status_buf,sizeof(status_buf),"%s: %s",imap_server->name, _("Connected"));
callbacks->set_status(status_buf);
success = 1;
} else
{
SM_DEBUGF(10,("Login failed\n"));
sm_snprintf(status_buf,sizeof(status_buf),"%s: %s",imap_server->name, _("Log in failed. Check user name and password for this account."));
callbacks->set_status(status_buf);
tcp_disconnect(imap_connection);
imap_connection = NULL;
tell_from_subtask(N_("Authentication failed!"));
}
} else
{
sm_snprintf(status_buf,sizeof(status_buf),"%s: %s",imap_server->name, _("Unexpected server answer. Connection failed."));
callbacks->set_status(status_buf);
tcp_disconnect(imap_connection);
imap_connection = NULL;
}
} else
{
/* Display "Failed to connect" - status message */
sm_snprintf(status_buf,sizeof(status_buf),"%s: %s",imap_server->name, _("Connecting to the server failed"));
callbacks->set_status(status_buf);
}
bailout:
*connection = imap_connection;
SM_RETURN(success,"%ld");
return success;
}
/*****************************************************************************/
int imap_really_download_mails(struct connection *imap_connection, struct imap_download_mails_options *options)
{
struct folder *local_folder;
struct remote_mailbox *rm;
int do_download = 1;
int downloaded_mails = 0;
int dont_use_uids = options->uid_options.imap_dont_use_uids;
unsigned int local_uid_validiy = options->uid_options.imap_uid_validity;
unsigned int local_uid_next = options->uid_options.imap_uid_next;
unsigned int uid_from = 0;
unsigned int uid_to = 0;
char *imap_local_path = options->imap_local_path;
struct imap_server *imap_server = options->imap_server;
char *imap_folder = options->imap_folder;
struct imap_download_mails_callbacks *callbacks = &options->callbacks;
struct progmon *pm;
SM_ENTER;
if (!imap_connection)
{
SM_RETURN(-1,"%d");
return -1;
}
SM_DEBUGF(10,("Downloading mails of folder \"%s\"\n",imap_folder));
/* Determine uid values from folder if this shall not be skipped and if
* the uids are not known.
*/
if (!dont_use_uids && !local_uid_next)
{
folders_lock();
if ((local_folder = folder_find_by_imap(imap_server->login, imap_server->name, imap_folder)))
{
local_uid_validiy = local_folder->imap_uid_validity;
local_uid_next = local_folder->imap_uid_next;
dont_use_uids = local_folder->imap_dont_use_uids;
}
folders_unlock();
}