-
Notifications
You must be signed in to change notification settings - Fork 8
/
imap_thread.c
705 lines (575 loc) · 18.9 KB
/
imap_thread.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
/**
* @file
*
* The communications happens in a separate thread of name #IMAP_THREAD_NAME.
*/
#include "imap_thread.h"
#include <stdlib.h>
#include <string.h>
#include "account.h"
#include "debug.h"
#include "folder.h"
#include "imap.h"
#include "lists.h"
#include "mail.h"
#include "request.h"
#include "simplemail.h"
#include "status.h"
#include "subthreads.h"
#include "support_indep.h"
#include "tcp.h"
#include "tcpip.h"
/*****************************************************************************/
/**
* The name of the IMAP thread.
*/
#define IMAP_THREAD_NAME "SimpleMail - IMAP thread"
/*****************************************************************************/
/* Very similar to trans_set_status(). TODO: consolidate */
static void imap_set_status(const char *str)
{
thread_call_parent_function_async_string(status_set_status, 1, str);
}
static void imap_set_status_static(const char *str)
{
thread_call_function_async(thread_get_main(), status_set_status, 1, str);
}
static void imap_set_connect_to_server(const char *server)
{
thread_call_parent_function_async_string(status_set_connect_to_server, 1, server);
}
static void imap_set_head(const char *head)
{
thread_call_parent_function_async_string(status_set_head, 1, head);
}
static void imap_set_title_utf8(const char *title)
{
thread_call_parent_function_async_string(status_set_title_utf8, 1, title);
}
static void imap_set_title(const char *title)
{
thread_call_parent_function_async_string(status_set_title, 1, title);
}
static void imap_new_mails_arrived(int num_filenames, char **filenames, char *user, char *server, char *path)
{
thread_call_parent_function_sync(NULL, callback_new_imap_mails_arrived, 5, num_filenames, filenames, user, server, path);
}
static void imap_new_uids(unsigned int uid_validity, unsigned int uid_next, char *user, char *server, char *path)
{
thread_call_parent_function_sync(NULL, callback_new_imap_uids, 5, uid_validity, uid_next, user, server, path);
}
static int imap_request_login(const char *text, char *login, char *password, int len)
{
return thread_call_parent_function_sync(NULL,sm_request_login,4,text,login,password,len);
}
static void imap_delete_mail_by_uid(char *user, char *server, char *path, unsigned int uid)
{
thread_call_parent_function_sync(NULL, callback_delete_mail_by_uid, 4, user, server, path, uid);
}
static void imap_add_imap_folder(char *user, char *server, char *path, char delim)
{
thread_call_parent_function_sync(NULL, callback_add_imap_folder, 4, user, server, path, delim);
}
static void imap_refresh_folders(void)
{
thread_call_parent_function_sync(NULL,callback_refresh_folders,0);
}
/*****************************************************************************/
/**
* Contains the arguments submitted to the get folder list thread.
*/
struct imap_get_folder_list_entry_msg
{
struct imap_server *server;
imap_get_folder_list_callback_t callback;
};
/**
* Entry point for the thread to get the folder list.
*
* @param msg
* @return
*/
static int imap_get_folder_list_entry(struct imap_get_folder_list_entry_msg *msg)
{
struct imap_server *server = imap_duplicate(msg->server);
imap_get_folder_list_callback_t callback = msg->callback;
if (thread_parent_task_can_contiue())
{
struct imap_get_folder_list_options options = {0};
struct remote_folder *all_folders;
struct remote_folder *sub_folders;
int num_all_folders, num_sub_folders;
thread_call_function_async(thread_get_main(),status_init,1,0);
thread_call_function_async(thread_get_main(),status_open,0);
options.server = server;
options.callbacks.set_status = imap_set_status;
options.callbacks.set_status_static = imap_set_status_static;
options.callbacks.set_connect_to_server = imap_set_connect_to_server;
options.callbacks.set_head = imap_set_head;
options.callbacks.set_title = imap_set_title;
options.callbacks.set_title_utf8 = imap_set_title_utf8;
if (!(imap_get_folder_list_really(&options, &all_folders, &num_all_folders, &sub_folders, &num_sub_folders)))
return 0;
thread_call_parent_function_sync(NULL, callback, 5, server, all_folders, num_all_folders, sub_folders, num_sub_folders);
thread_call_function_async(thread_get_main(),status_close,0);
}
return 0;
}
/*****************************************************************************/
int imap_get_folder_list(struct imap_server *server, imap_get_folder_list_callback_t callback)
{
struct imap_get_folder_list_entry_msg msg;
msg.server = server;
msg.callback = callback;
return thread_start(THREAD_FUNCTION(&imap_get_folder_list_entry),&msg);
}
/**
* Contains the arguments send to the submit folder list thread.
*/
struct imap_submit_folder_list_entry_msg
{
struct imap_server *server;
struct string_list *list;
};
/**
* The entry point for the submit folder list thread.
*
* @param msg
* @return
*/
static int imap_submit_folder_list_entry(struct imap_submit_folder_list_entry_msg *msg)
{
struct imap_server *server = imap_duplicate(msg->server);
struct string_list list;
struct string_node *node;
string_list_init(&list);
node = string_list_first(msg->list);
while (node)
{
string_list_insert_tail(&list,node->string);
node = string_node_next(node);
}
if (thread_parent_task_can_contiue())
{
struct imap_submit_folder_options options = {0};
thread_call_function_async(thread_get_main(),status_init,1,0);
thread_call_function_async(thread_get_main(),status_open,0);
options.server = server;
options.list = &list;
options.callbacks.set_status = imap_set_status;
options.callbacks.set_status_static = imap_set_status_static;
options.callbacks.set_connect_to_server = imap_set_connect_to_server;
options.callbacks.set_head = imap_set_head;
options.callbacks.set_title = imap_set_title;
options.callbacks.set_title_utf8 = imap_set_title_utf8;
imap_submit_folder_list_really(&options);
thread_call_function_async(thread_get_main(),status_close,0);
}
string_list_clear(&list);
return 0;
}
/*****************************************************************************/
int imap_submit_folder_list(struct imap_server *server, struct string_list *list)
{
struct imap_submit_folder_list_entry_msg msg;
msg.server = server;
msg.list = list;
return thread_start(THREAD_FUNCTION(&imap_submit_folder_list_entry),&msg);
}
/*****************************************************************************/
/***** IMAP Thread *****/
static thread_t imap_thread;
static struct connection *imap_connection;
static int imap_socket_lib_open;
static struct imap_server *imap_server;
static char *imap_folder; /** The path of the folder on the imap server */
static char *imap_local_path; /** The local path of the folder */
/**************************************************************************/
/**
* The entry point for the imap thread. It just go into the wait state and
* then frees all resources when finished
*
* @param test
*/
static void imap_thread_entry(void *test)
{
if (thread_parent_task_can_contiue())
{
thread_wait(NULL,NULL,NULL,0);
imap_free(imap_server);
free(imap_folder);
free(imap_local_path);
if (imap_connection)
{
tcp_disconnect(imap_connection);
imap_connection = NULL;
}
if (imap_socket_lib_open)
{
close_socket_lib();
imap_socket_lib_open = 0;
}
}
}
/**
* The function creates the IMAP thread. It doesn't start the thread
* if it is already started.
*
* @return whether the thread has been started.
*/
static int imap_start_thread(void)
{
if (imap_thread) return 1;
imap_thread = thread_add(IMAP_THREAD_NAME, THREAD_FUNCTION(&imap_thread_entry),NULL);
return !!imap_thread;
}
/**
* Open the socket library, but make sure that this happns only once.
*
* @return socket is open (1) or not (0).
*/
static int imap_open_socket_lib(void)
{
if (!imap_socket_lib_open)
imap_socket_lib_open = open_socket_lib();
return imap_socket_lib_open;
}
/**
* Disconnect the current imap connection if any is open.
*/
static void imap_disconnect(void)
{
if (imap_connection)
{
tcp_disconnect(imap_connection);
imap_connection = NULL;
}
}
static int imap_thread_really_login_to_given_server(struct imap_server *server)
{
if (!imap_open_socket_lib())
return 0;
if (!imap_connection || imap_new_connection_needed(imap_server,server))
{
free(imap_folder); imap_folder = NULL;
free(imap_local_path); imap_local_path = NULL;
if (imap_server) imap_free(imap_server);
if ((imap_server = imap_duplicate(server)))
{
struct imap_connect_and_login_to_server_callbacks callbacks = {0};
callbacks.set_status = imap_set_status;
callbacks.request_login = imap_request_login;
imap_disconnect();
return imap_really_connect_and_login_to_server(&imap_connection, imap_server, &callbacks);
}
return 0;
}
return 1;
}
/**
* Connect to the given imap server. Shall be invoked only in the context of the IMAP thread.
*
* @param server
* @param folder
* @param local_path
* @return
*/
static int imap_thread_connect_to_server(struct imap_server *server, char *folder, char *local_path)
{
static int connecting;
int rc = 0;
SM_ENTER;
/* Ignore this request if we are already connecting. This can happen for instance, if a synchronous call
* to the parent task is issued. */
if (connecting)
{
SM_DEBUGF(5, ("Ignoring connect to server request for %s\n", local_path));
if (server) imap_free(server);
free(folder);
free(local_path);
goto bailout;
}
if (!imap_open_socket_lib())
goto bailout;
connecting = 1;
if (!imap_connection || imap_new_connection_needed(imap_server,server))
{
struct imap_connect_to_server_options options = {0};
imap_disconnect();
if (imap_server) imap_free(imap_server);
imap_server = server;
free(imap_folder);
imap_folder = folder;
free(imap_local_path);
imap_local_path = local_path;
options.imap_local_path = imap_local_path;
options.imap_server = imap_server;
options.imap_folder = imap_folder;
options.callbacks.set_status = imap_set_status;
options.callbacks.request_login = imap_request_login;
options.callbacks.add_imap_folder = imap_add_imap_folder;
options.callbacks.refresh_folders = imap_refresh_folders;
options.download_callbacks.new_uids = imap_new_uids;
options.download_callbacks.new_mails_arrived = imap_new_mails_arrived;
options.download_callbacks.set_status = imap_set_status;
options.download_callbacks.set_status_static = imap_set_status_static;
imap_really_connect_to_server(&imap_connection, &options);
rc = 1;
} else
{
struct imap_download_mails_options download_options = {0};
imap_free(server);
free(imap_folder);
imap_folder = folder;
free(imap_local_path);
imap_local_path = local_path;
download_options.imap_folder = imap_folder;
download_options.imap_server = imap_server;
download_options.imap_local_path = imap_local_path;
download_options.callbacks.new_mails_arrived = imap_new_mails_arrived;
download_options.callbacks.new_uids = imap_new_uids;
download_options.callbacks.set_status = imap_set_status;
download_options.callbacks.set_status_static = imap_set_status_static;
download_options.callbacks.delete_mail_by_uid = imap_delete_mail_by_uid;
imap_really_download_mails(imap_connection, &download_options);
rc = 1;
}
connecting = 0;
bailout:
SM_RETURN(rc,"%d");
return rc;
}
/**
* Download the given mail. Usually called in the context of the imap thread.
*
* @param server
* @param local_path
* @param m
* @param callback called on the context of the calling task.
* @param userdata user data supplied for the callback
* @return
*/
static int imap_thread_download_mail(struct imap_server *server, char *local_path, struct mail_info *m, void (*callback)(struct mail_info *m, void *userdata), void *userdata)
{
if (!imap_thread_really_login_to_given_server(server)) return 0;
return imap_really_download_mail(imap_connection, local_path, m, callback, userdata);
}
/**
* Move a given mail from one folder into another one of the given imap account.
*
* Usually called in the context of the imap thread.
*
* @param mail
* @param server
* @param src_folder
* @param dest_folder
* @return success or failure.
*/
static int imap_thread_move_mail(struct mail_info *mail, struct imap_server *server, struct folder *src_folder, struct folder *dest_folder)
{
if (!imap_thread_really_login_to_given_server(server)) return 0;
return imap_really_move_mail(imap_connection, mail, server, src_folder, dest_folder);
}
/**
* Delete a mail permanently from the server
*
* @param filename
* @param server
* @param folder
* @return success or not.
* @note This function can only be called in the context of the imap thread.
*/
static int imap_thread_delete_mail_by_filename(char *filename, struct imap_server *server, struct folder *folder)
{
struct imap_delete_mail_by_filename_options options = {0};
if (!imap_thread_really_login_to_given_server(server)) return 0;
options.filename = filename;
options.folder = folder;
return imap_really_delete_mail_by_filename(imap_connection, &options);
}
/**
* Store the mail represented by the mail located in the given source_dir
* on the given server in the given dest_folder.
*
* @param mail
* @param source_dir
* @param server
* @param dest_folder
* @return success or not.
* @note This function can only be called in the context of the imap thread.
*/
static int imap_thread_append_mail(struct mail_info *mail, char *source_dir, struct imap_server *server, struct folder *dest_folder)
{
if (!imap_thread_really_login_to_given_server(server)) return 0;
return imap_really_append_mail(imap_connection, mail, source_dir, server, dest_folder);
}
/*****************************************************************************/
void imap_thread_connect(struct folder *folder)
{
struct imap_server *server;
char *imap_folder;
char *imap_local_path;
SM_ENTER;
if (!(server = account_find_imap_server_by_folder(folder)))
{
SM_DEBUGF(5,("Server for folder %p (%s) not found\n",folder,folder?folder->name:"NONE"));
goto bailout;
}
if (!imap_start_thread())
{
SM_DEBUGF(5,("Could not start IMAP thread\n"));
goto bailout;
}
if (!(server = imap_duplicate(server)))
{
SM_DEBUGF(5,("Could not duplicate imap server\n"));
goto bailout;
}
if ((!(imap_folder = mystrdup(folder->imap_path))) && folder->imap_path != NULL && strlen(folder->imap_path))
{
SM_DEBUGF(5,("Could not duplicate imap path\n"));
goto bailout;
}
if (!(imap_local_path = mystrdup(folder->path)))
{
SM_DEBUGF(5,("Could not duplicate folder path\n"));
goto bailout;
}
thread_call_function_async(imap_thread, imap_thread_connect_to_server, 3, server, imap_folder, imap_local_path);
bailout:
SM_LEAVE;
}
/*****************************************************************************/
int imap_download_mail(struct folder *f, struct mail_info *m)
{
struct imap_server *server;
if (!(m->flags & MAIL_FLAGS_PARTIAL)) return 0;
if (!(server = account_find_imap_server_by_folder(f))) return 0;
if (!imap_start_thread()) return 0;
if (thread_call_function_sync(imap_thread, imap_thread_download_mail, 5, server, f->path, m, (void (*)(struct mail_info *, void *))NULL, (void *)NULL))
{
folder_set_mail_flags(f, m, (m->flags & (~MAIL_FLAGS_PARTIAL)));
return 1;
}
return 0;
}
struct imap_download_data
{
struct imap_server *server;
char *local_path;
char *remote_path;
void (*callback)(struct mail_info *m, void *userdata);
void *userdata;
};
/**
* Function that is called when an email has been downloaded. It is assumed
* that is called on parent task.
*
* @param m
* @param userdata
*/
static void imap_download_mail_callback_on_parent_task(struct mail_info *m, void *userdata)
{
struct imap_download_data *d = (struct imap_download_data*)userdata;
struct folder *local_folder;
SM_ENTER;
SM_DEBUGF(20,("m=%p, d=%p, local_path=%p\n",m,d,d->local_path));
folders_lock();
if ((local_folder = folder_find_by_path(d->local_path)))
folder_lock(local_folder);
folders_unlock();
if (local_folder)
{
folder_set_mail_flags(local_folder, m, (m->flags & (~MAIL_FLAGS_PARTIAL)));
folder_unlock(local_folder);
}
d->callback(m,d->userdata);
mail_dereference(m);
free(d->remote_path);
free(d->local_path);
imap_free(d->server);
free(d);
SM_LEAVE;
}
/**
* Function that is called when an email has been downloaded asynchronously.
* It is assumed that this called on a task that is not the main task.
*
* @param m
* @param userdata
*/
static void imap_download_mail_async_callback(struct mail_info *m, void *userdata)
{
thread_call_function_async(thread_get_main(), imap_download_mail_callback_on_parent_task, 2, m, userdata);
}
/*****************************************************************************/
int imap_download_mail_async(struct folder *f, struct mail_info *m, void (*callback)(struct mail_info *m, void *userdata), void *userdata)
{
struct imap_download_data *d = NULL;
SM_ENTER;
if (!imap_start_thread()) goto bailout;
if (!(d = (struct imap_download_data *)malloc(sizeof(*d)))) goto bailout;
memset(d,0,sizeof(*d));
d->userdata = userdata;
if (!(d->server = account_find_imap_server_by_folder(f))) goto bailout;
if (!(d->server = imap_duplicate(d->server))) goto bailout;
if (!(d->local_path = mystrdup(f->path))) goto bailout;
if (!(d->remote_path = mystrdup(f->imap_path))) goto bailout;
mail_reference(m);
d->callback = callback;
if (!thread_call_function_async(imap_thread, imap_thread_download_mail, 5, d->server, d->local_path, m, imap_download_mail_async_callback, (void*)d))
goto bailout;
SM_RETURN(1,"%ld");
return 1;
bailout:
if (d)
{
if (d->remote_path)
{
free(d->remote_path);
mail_dereference(m);
}
free(d->local_path);
imap_free(d->server);
free(d);
}
SM_RETURN(0,"%ld");
return 0;
}
/*****************************************************************************/
int imap_move_mail(struct mail_info *mail, struct folder *src_folder, struct folder *dest_folder)
{
struct imap_server *server;
if (!imap_start_thread()) return 0;
if (!folder_on_same_imap_server(src_folder,dest_folder)) return 0;
if (!(server = account_find_imap_server_by_folder(src_folder))) return 0;
if (thread_call_function_sync(imap_thread, imap_thread_move_mail, 4, mail, server, src_folder, dest_folder))
{
return 1;
}
return 0;
}
/*****************************************************************************/
int imap_delete_mail_by_filename(char *filename, struct folder *folder)
{
struct imap_server *server;
if (!imap_start_thread()) return 0;
if (!(server = account_find_imap_server_by_folder(folder))) return 0;
if (thread_call_function_sync(imap_thread, imap_thread_delete_mail_by_filename, 3, filename, server, folder))
{
return 1;
}
return 0;
}
/*****************************************************************************/
int imap_append_mail(struct mail_info *mail, char *source_dir, struct folder *dest_folder)
{
struct imap_server *server;
if (!imap_start_thread()) return 0;
if (!(server = account_find_imap_server_by_folder(dest_folder))) return 0;
if (thread_call_function_sync(imap_thread, imap_thread_append_mail, 4, mail, source_dir, server, dest_folder))
{
return 1;
}
return 0;
}