diff options
Diffstat (limited to 'email-core/email-core-imap-idle.c')
-rwxr-xr-x | email-core/email-core-imap-idle.c | 177 |
1 files changed, 129 insertions, 48 deletions
diff --git a/email-core/email-core-imap-idle.c b/email-core/email-core-imap-idle.c index 9443d58..7f5e4f2 100755 --- a/email-core/email-core-imap-idle.c +++ b/email-core/email-core-imap-idle.c @@ -47,6 +47,7 @@ #include "email-core-account.h" #include "email-core-alarm.h" #include "email-utilities.h" +#include "email-core-container.h" /*Definitions copied temporarily from ssl_unix.c */ #define SSLBUFLEN 8192 @@ -67,6 +68,7 @@ typedef struct _email_imap_idle_connection_info_t { int mailbox_id; MAILSTREAM *mail_stream; int socket_fd; + char *multi_user_name; } email_imap_idle_connection_info_t; int imap_idle_pipe_fd[2]; @@ -134,7 +136,10 @@ static int emcore_clear_old_connections(int input_epoll_fd, GList **input_imap_i } /* Close connection */ - connection_info->mail_stream = mail_close(connection_info->mail_stream); + if (connection_info->mail_stream) + connection_info->mail_stream = mail_close(connection_info->mail_stream); + + EM_SAFE_FREE(connection_info->multi_user_name); EM_SAFE_FREE(connection_info); index = g_list_next(index); @@ -148,7 +153,7 @@ FINISH_OFF: return err; } -static int emcore_imap_idle_insert_sync_event(int input_account_id, int input_mailbox_id, int *err_code) +static int emcore_imap_idle_insert_sync_event(char *multi_user_name, int input_account_id, int input_mailbox_id, int *err_code) { EM_DEBUG_FUNC_BEGIN(); @@ -163,7 +168,7 @@ static int emcore_imap_idle_insert_sync_event(int input_account_id, int input_ma goto FINISH_OFF; } - event_data = em_malloc(sizeof(email_event_t)); + event_data = em_malloc(sizeof(email_event_t)); if (event_data == NULL) { EM_DEBUG_EXCEPTION("em_malloc failed"); err = EMAIL_ERROR_OUT_OF_MEMORY; @@ -173,6 +178,7 @@ static int emcore_imap_idle_insert_sync_event(int input_account_id, int input_ma event_data->type = EMAIL_EVENT_SYNC_HEADER; event_data->event_param_data_5 = input_mailbox_id; event_data->account_id = input_account_id; + event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name); if (!emcore_insert_event(event_data, &handle, &err)) { EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err); @@ -195,7 +201,7 @@ FINISH_OFF: return ret; } -static int emcore_parse_imap_idle_response(int input_account_id, int input_mailbox_id, MAILSTREAM *input_mailstream, int input_socket_fd) +static int emcore_parse_imap_idle_response(char *multi_user_name, int input_account_id, int input_mailbox_id, MAILSTREAM *input_mailstream, int input_socket_fd) { EM_DEBUG_FUNC_BEGIN("input_account_id[%d] input_mailbox_id [%d] input_mailstream[%p] input_socket_fd[%d]", input_account_id, input_mailbox_id, input_mailstream, input_socket_fd); int err = EMAIL_ERROR_NONE; @@ -228,7 +234,7 @@ static int emcore_parse_imap_idle_response(int input_account_id, int input_mailb } else { if (strstr(p, "EXIST") != NULL) { - if (!emcore_imap_idle_insert_sync_event(input_account_id, input_mailbox_id, &err)) + if (!emcore_imap_idle_insert_sync_event(multi_user_name, input_account_id, input_mailbox_id, &err)) EM_DEBUG_EXCEPTION_SEC("Syncing mailbox[%d] failed with err_code [%d]", input_mailbox_id, err); } else @@ -256,7 +262,7 @@ FINISH_OFF: } /* connects to given mailbox. send idle and returns socket id */ -static int emcore_connect_and_idle_on_mailbox(GList **input_connection_list, email_account_t *input_account, email_mailbox_t *input_mailbox, MAILSTREAM **output_mailstream, int *output_socket_fd) +static int emcore_connect_and_idle_on_mailbox(char *multi_user_name, GList **input_connection_list, email_account_t *input_account, email_mailbox_t *input_mailbox, MAILSTREAM **output_mailstream, int *output_socket_fd) { EM_DEBUG_FUNC_BEGIN("input_connection_list [%p] input_account [%p] input_mailbox[%p] output_mailstream [%p] output_socket_fd [%p]", input_connection_list, input_account, input_mailbox, output_mailstream, output_socket_fd); void *mail_stream = NULL; @@ -284,7 +290,7 @@ static int emcore_connect_and_idle_on_mailbox(GList **input_connection_list, ema EM_DEBUG_EXCEPTION("emcore_get_empty_session failed..."); /* Open connection to mailbox */ - if (!emcore_connect_to_remote_mailbox(input_mailbox->account_id, input_mailbox->mailbox_id, (void **)&mail_stream, &err) || !mail_stream) { + if (!emcore_connect_to_remote_mailbox(multi_user_name, input_mailbox->account_id, input_mailbox->mailbox_id, (void **)&mail_stream, &err) || !mail_stream) { EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err); goto FINISH_OFF; } @@ -343,10 +349,11 @@ static int emcore_connect_and_idle_on_mailbox(GList **input_connection_list, ema goto FINISH_OFF; } - connection_info->account_id = input_account->account_id; - connection_info->mailbox_id = input_mailbox->mailbox_id; - connection_info->mail_stream = mail_stream; - connection_info->socket_fd = socket_fd; + connection_info->account_id = input_account->account_id; + connection_info->mailbox_id = input_mailbox->mailbox_id; + connection_info->mail_stream = mail_stream; + connection_info->socket_fd = socket_fd; + connection_info->multi_user_name = EM_SAFE_STRDUP(multi_user_name); imap_idle_connection_list = g_list_append(imap_idle_connection_list, (gpointer)connection_info); @@ -378,7 +385,7 @@ static int emcore_imap_idle_cb(email_alarm_data_t *alarm_data, void *user_parame return err; } -static int emcore_refresh_alarm_for_imap_idle() +static int emcore_refresh_alarm_for_imap_idle(char *multi_user_name) { EM_DEBUG_FUNC_BEGIN(); int err = EMAIL_ERROR_NONE; @@ -395,7 +402,7 @@ static int emcore_refresh_alarm_for_imap_idle() emcore_delete_alram_data_by_reference_id(EMAIL_ALARM_CLASS_IMAP_IDLE, 0); } - if (!emcore_get_account_reference_list(&account_ref_list, &account_count, &err)) { + if ((err = emcore_get_account_reference_list(multi_user_name, &account_ref_list, &account_count)) != EMAIL_ERROR_NONE) { EM_DEBUG_LOG("emcore_get_account_reference_list failed [%d]", err); goto FINISH_OFF; } @@ -406,14 +413,14 @@ static int emcore_refresh_alarm_for_imap_idle() } } - if(account_ref_list) + if (account_ref_list) emcore_free_account_list(&account_ref_list, account_count, NULL); if (auto_sync_account_count) { time(¤t_time); trigger_at_time = current_time + (29 * 60); - if ((err = emcore_add_alarm(trigger_at_time, EMAIL_ALARM_CLASS_IMAP_IDLE, 0, emcore_imap_idle_cb, NULL)) != EMAIL_ERROR_NONE) { + if ((err = emcore_add_alarm(multi_user_name, trigger_at_time, EMAIL_ALARM_CLASS_IMAP_IDLE, 0, emcore_imap_idle_cb, NULL)) != EMAIL_ERROR_NONE) { EM_DEBUG_EXCEPTION("emcore_add_alarm failed [%d]",err); } } @@ -442,6 +449,8 @@ void* emcore_imap_idle_worker(void* thread_user_data) email_mailbox_t inbox_mailbox = {0}; email_account_t *account_ref_list = NULL; GList *imap_idle_connection_list = NULL; + GList *zone_name_list = NULL; + GList *node = NULL; email_imap_idle_connection_info_t *connection_info = NULL; EM_DEBUG_LOG("emcore_imap_idle_worker start "); @@ -470,56 +479,124 @@ void* emcore_imap_idle_worker(void* thread_user_data) EM_DEBUG_LOG("Enter imap idle loop"); while (1) { if (refresh_connection_flag) { + EM_DEBUG_LOG("Clear old connections"); emcore_clear_old_connections(epoll_fd, &imap_idle_connection_list); EM_DEBUG_LOG("Getting account list"); - if (!emcore_get_account_reference_list(&account_ref_list, &account_count, &err)) { - EM_DEBUG_LOG("emcore_get_account_reference_list failed [%d]", err); - break; + if ((err = emcore_get_zone_name_list(&zone_name_list)) != EMAIL_ERROR_NONE) { + EM_DEBUG_EXCEPTION("emcore_get_zone_name_list failed : err[%d]", err); + if (zone_name_list) g_list_free(zone_name_list); + continue; } - for (i = 0; i < account_count; i++) { - if (account_ref_list[i].incoming_server_type != EMAIL_SERVER_TYPE_IMAP4 || (account_ref_list[i].check_interval != 0 && account_ref_list[i].peak_interval != 0)) { - EM_DEBUG_LOG("account_id[%d] is not for auto sync", account_ref_list[i].account_id); - continue; - } + if (g_list_length(zone_name_list) <= 1) { + if ((err = emcore_get_account_reference_list(NULL, &account_ref_list, &account_count)) != EMAIL_ERROR_NONE) + EM_DEBUG_LOG("emcore_get_account_reference_list failed [%d]", err); - /* TODO: peak schedule handling */ + for (i = 0; i < account_count; i++) { + if (account_ref_list[i].incoming_server_type != EMAIL_SERVER_TYPE_IMAP4 || (account_ref_list[i].check_interval != 0 && account_ref_list[i].peak_interval != 0)) { + EM_DEBUG_LOG("account_id[%d] is not for auto sync", account_ref_list[i].account_id); + continue; + } - memset(&inbox_mailbox, 0, sizeof(email_mailbox_t)); + /* TODO: peak schedule handling */ - if (!emcore_get_mailbox_by_type(account_ref_list[i].account_id, EMAIL_MAILBOX_TYPE_INBOX, &inbox_mailbox, &err)) { - EM_DEBUG_EXCEPTION("emcore_get_mailbox_by_type failed [%d]", err); - continue; - } + memset(&inbox_mailbox, 0, sizeof(email_mailbox_t)); + + if (!emcore_get_mailbox_by_type(NULL, account_ref_list[i].account_id, EMAIL_MAILBOX_TYPE_INBOX, &inbox_mailbox, &err)) { + EM_DEBUG_EXCEPTION("emcore_get_mailbox_by_type failed [%d]", err); + continue; + } - EM_DEBUG_LOG("Connect to IMAP server"); - err = emcore_connect_and_idle_on_mailbox(&imap_idle_connection_list, &(account_ref_list[i]), &inbox_mailbox, &mail_stream, &socket_fd); + EM_DEBUG_LOG("Connect to IMAP server"); + err = emcore_connect_and_idle_on_mailbox(NULL, &imap_idle_connection_list, &(account_ref_list[i]), &inbox_mailbox, &mail_stream, &socket_fd); - emcore_free_mailbox(&inbox_mailbox); + emcore_free_mailbox(&inbox_mailbox); - if (err != EMAIL_ERROR_NONE) { - EM_DEBUG_EXCEPTION("emcore_connect_and_idle_on_mailbox failed [%d]", err); - continue; - } + if (err != EMAIL_ERROR_NONE) { + EM_DEBUG_EXCEPTION("emcore_connect_and_idle_on_mailbox failed [%d]", err); + continue; + } - ev.events = EPOLLIN; - ev.data.fd = socket_fd; + ev.events = EPOLLIN; + ev.data.fd = socket_fd; - EM_DEBUG_LOG("Add listener for socket buffer"); - if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, socket_fd, &ev) == -1) { - EM_DEBUG_EXCEPTION("epoll_ctl failed: %s[%d]", EM_STRERROR(errno_buf), errno); + EM_DEBUG_LOG("Add listener for socket buffer"); + if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, socket_fd, &ev) == -1) { + EM_DEBUG_EXCEPTION("epoll_ctl failed: %s[%d]", EM_STRERROR(errno_buf), errno); + } } - } - if(account_ref_list) - emcore_free_account_list(&account_ref_list, account_count, NULL); + if(account_ref_list) + emcore_free_account_list(&account_ref_list, account_count, NULL); + + EM_DEBUG_LOG(" Delete old an alarm and add an alarm to refresh connections every 29min"); + emcore_refresh_alarm_for_imap_idle(NULL); + + refresh_connection_flag = 0; + } else { + node = g_list_first(zone_name_list); + + while (node != NULL) { + if (!node->data) + node = g_list_next(node); - EM_DEBUG_LOG(" Delete old an alarm and add an alarm to refresh connections every 29min"); - emcore_refresh_alarm_for_imap_idle(); + EM_DEBUG_LOG("Data name of node : [%s]", node->data); + + if ((err = emcore_get_account_reference_list(node->data, &account_ref_list, &account_count)) != EMAIL_ERROR_NONE) { + EM_DEBUG_LOG("emcore_get_account_reference_list failed [%d]", err); + } + + for (i = 0; i < account_count; i++) { + if (account_ref_list[i].incoming_server_type != EMAIL_SERVER_TYPE_IMAP4 || (account_ref_list[i].check_interval != 0 && account_ref_list[i].peak_interval != 0)) { + EM_DEBUG_LOG("account_id[%d] is not for auto sync", account_ref_list[i].account_id); + continue; + } + + /* TODO: peak schedule handling */ + + memset(&inbox_mailbox, 0, sizeof(email_mailbox_t)); + + if (!emcore_get_mailbox_by_type(node->data, account_ref_list[i].account_id, EMAIL_MAILBOX_TYPE_INBOX, &inbox_mailbox, &err)) { + EM_DEBUG_EXCEPTION("emcore_get_mailbox_by_type failed [%d]", err); + continue; + } + + EM_DEBUG_LOG("Connect to IMAP server"); + err = emcore_connect_and_idle_on_mailbox(node->data, &imap_idle_connection_list, &(account_ref_list[i]), &inbox_mailbox, &mail_stream, &socket_fd); + + emcore_free_mailbox(&inbox_mailbox); + + if (err != EMAIL_ERROR_NONE) { + EM_DEBUG_EXCEPTION("emcore_connect_and_idle_on_mailbox failed [%d]", err); + continue; + } + + ev.events = EPOLLIN; + ev.data.fd = socket_fd; + + EM_DEBUG_LOG("Add listener for socket buffer"); + if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, socket_fd, &ev) == -1) { + EM_DEBUG_EXCEPTION("epoll_ctl failed: %s[%d]", EM_STRERROR(errno_buf), errno); + } + } + + if(account_ref_list) + emcore_free_account_list(&account_ref_list, account_count, NULL); + + EM_DEBUG_LOG(" Delete old an alarm and add an alarm to refresh connections every 29min"); + emcore_refresh_alarm_for_imap_idle(node->data); + + EM_SAFE_FREE(node->data); + node = g_list_next(node); + } + + refresh_connection_flag = 0; + } - refresh_connection_flag = 0; + if (zone_name_list) + g_list_free(zone_name_list); } EM_DEBUG_LOG("Waiting......."); @@ -547,7 +624,11 @@ void* emcore_imap_idle_worker(void* thread_user_data) continue; } - if ((err = emcore_parse_imap_idle_response(connection_info->account_id, connection_info->mailbox_id, connection_info->mail_stream, connection_info->socket_fd)) != EMAIL_ERROR_NONE) { + if ((err = emcore_parse_imap_idle_response(connection_info->multi_user_name, + connection_info->account_id, + connection_info->mailbox_id, + connection_info->mail_stream, + connection_info->socket_fd)) != EMAIL_ERROR_NONE) { EM_DEBUG_EXCEPTION("emcore_parse_imap_idle_response failed. [%d] ", err); refresh_connection_flag = 1; // mail_stream = mail_close (mail_stream); |