static IncSession *inc_session_new(PrefsAccount *account) { IncSession *session; cm_return_val_if_fail(account != NULL, NULL); if (account->protocol != A_POP3) return NULL; if (!account->recv_server || !account->userid) return NULL; session = g_new0(IncSession, 1); session->session = pop3_session_new(account); session->session->data = session; POP3_SESSION(session->session)->drop_message = inc_drop_message; session_set_recv_message_notify(session->session, inc_recv_message, session); session_set_recv_data_progressive_notify(session->session, inc_recv_data_progressive, session); session_set_recv_data_notify(session->session, inc_recv_data_finished, session); return session; }
static void pop3_session_destroy(Session *session) { Pop3Session *pop3_session = POP3_SESSION(session); gint n; cm_return_if_fail(session != NULL); for (n = 1; n <= pop3_session->count; n++) g_free(pop3_session->msg[n].uidl); g_free(pop3_session->msg); if (pop3_session->uidl_table) { hash_free_strings(pop3_session->uidl_table); g_hash_table_destroy(pop3_session->uidl_table); } if (pop3_session->partial_recv_table) { hash_free_strings(pop3_session->partial_recv_table); g_hash_table_destroy(pop3_session->partial_recv_table); } g_free(pop3_session->greeting); g_free(pop3_session->user); g_free(pop3_session->pass); g_free(pop3_session->error_msg); pop3_session->ac_prefs->receive_in_progress = FALSE; }
void inc_pop_before_smtp(PrefsAccount *acc) { IncProgressDialog *inc_dialog; IncSession *session; MainWindow *mainwin; mainwin = mainwindow_get_mainwindow(); session = inc_session_new(acc); if (!session) return; POP3_SESSION(session->session)->pop_before_smtp = TRUE; inc_dialog = inc_progress_dialog_create(FALSE); inc_dialog->queue_list = g_list_append(inc_dialog->queue_list, session); /* FIXME: assumes to attach to first main window */ inc_dialog->mainwin = mainwin; inc_progress_dialog_set_list(inc_dialog); if (mainwin) { toolbar_main_set_sensitive(mainwin); main_window_set_menu_sensitive(mainwin); } inc_start(inc_dialog); }
static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog, IncSession *inc_session) { ProgressDialog *dialog = inc_dialog->dialog; Pop3Session *session; cm_return_if_fail(inc_session != NULL); session = POP3_SESSION(inc_session->session); switch (session->state) { case POP3_GREETING: break; case POP3_GETAUTH_USER: case POP3_GETAUTH_PASS: case POP3_GETAUTH_APOP: progress_dialog_set_label(dialog, _("Authenticating...")); statuswindow_pop_all(); statusbar_print_all(_("Retrieving messages from %s (%s) ..."), SESSION(session)->server, session->ac_prefs->account_name); break; case POP3_GETRANGE_STAT: progress_dialog_set_label (dialog, _("Getting the number of new messages (STAT)...")); break; case POP3_GETRANGE_LAST: progress_dialog_set_label (dialog, _("Getting the number of new messages (LAST)...")); break; case POP3_GETRANGE_UIDL: progress_dialog_set_label (dialog, _("Getting the number of new messages (UIDL)...")); break; case POP3_GETSIZE_LIST: progress_dialog_set_label (dialog, _("Getting the size of messages (LIST)...")); break; case POP3_RETR: case POP3_RETR_RECV: case POP3_DELETE: break; case POP3_LOGOUT: progress_dialog_set_label(dialog, _("Quitting")); break; default: break; } }
static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog) { GList *list; for (list = inc_dialog->queue_list; list != NULL; list = list->next) { IncSession *session = list->data; Pop3Session *pop3_session = POP3_SESSION(session->session); session->data = inc_dialog; progress_dialog_list_set(inc_dialog->dialog, -1, NULL, pop3_session->ac_prefs->account_name, _("Standby")); } }
static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog, IncSession *inc_session) { gchar buf[MESSAGEBUFSIZE]; Pop3Session *pop3_session = POP3_SESSION(inc_session->session); gchar *total_size_str; gint cur_total; gint total; if (!pop3_session->new_msg_exist) return; cur_total = inc_session->cur_total_bytes; total = pop3_session->total_bytes; if (pop3_session->state == POP3_RETR || pop3_session->state == POP3_RETR_RECV || pop3_session->state == POP3_DELETE) { Xstrdup_a(total_size_str, to_human_readable((goffset)total), return); g_snprintf(buf, sizeof(buf), _("Retrieving message (%d / %d) (%s / %s)"), pop3_session->cur_msg, pop3_session->count, to_human_readable((goffset)cur_total), total_size_str); progress_dialog_set_label(inc_dialog->dialog, buf); }
static gint pop3_session_recv_msg(Session *session, const gchar *msg) { Pop3Session *pop3_session = POP3_SESSION(session); Pop3ErrorValue val = PS_SUCCESS; const gchar *body; body = msg; if (pop3_session->state != POP3_GETRANGE_UIDL_RECV && pop3_session->state != POP3_GETSIZE_LIST_RECV) { val = pop3_ok(pop3_session, msg); if (val != PS_SUCCESS) { if (val != PS_NOTSUPPORTED) { pop3_session->state = POP3_ERROR; return -1; } } if (*body == '+' || *body == '-') body++; while (g_ascii_isalpha(*body)) body++; while (g_ascii_isspace(*body)) body++; } switch (pop3_session->state) { case POP3_READY: case POP3_GREETING: pop3_greeting_recv(pop3_session, body); #ifdef USE_GNUTLS if (pop3_session->ac_prefs->ssl_pop == SSL_STARTTLS) val = pop3_stls_send(pop3_session); else #endif if (pop3_session->ac_prefs->use_apop_auth) val = pop3_getauth_apop_send(pop3_session); else val = pop3_getauth_user_send(pop3_session); break; #ifdef USE_GNUTLS case POP3_STLS: if (pop3_stls_recv(pop3_session) != PS_SUCCESS) return -1; if (pop3_session->ac_prefs->use_apop_auth) val = pop3_getauth_apop_send(pop3_session); else val = pop3_getauth_user_send(pop3_session); break; #endif case POP3_GETAUTH_USER: val = pop3_getauth_pass_send(pop3_session); break; case POP3_GETAUTH_PASS: case POP3_GETAUTH_APOP: if (!pop3_session->pop_before_smtp) val = pop3_getrange_stat_send(pop3_session); else val = pop3_logout_send(pop3_session); break; case POP3_GETRANGE_STAT: if (pop3_getrange_stat_recv(pop3_session, body) < 0) return -1; if (pop3_session->count > 0) val = pop3_getrange_uidl_send(pop3_session); else val = pop3_logout_send(pop3_session); break; case POP3_GETRANGE_LAST: if (val == PS_NOTSUPPORTED) pop3_session->error_val = PS_SUCCESS; else if (pop3_getrange_last_recv(pop3_session, body) < 0) return -1; if (pop3_session->cur_msg > 0) val = pop3_getsize_list_send(pop3_session); else val = pop3_logout_send(pop3_session); break; case POP3_GETRANGE_UIDL: if (val == PS_NOTSUPPORTED) { pop3_session->error_val = PS_SUCCESS; val = pop3_getrange_last_send(pop3_session); } else { pop3_session->state = POP3_GETRANGE_UIDL_RECV; session_recv_data(session, 0, ".\r\n"); } break; case POP3_GETSIZE_LIST: pop3_session->state = POP3_GETSIZE_LIST_RECV; session_recv_data(session, 0, ".\r\n"); break; case POP3_RETR: pop3_session->state = POP3_RETR_RECV; session_recv_data(session, 0, ".\r\n"); break; case POP3_TOP: if (val == PS_NOTSUPPORTED) { pop3_session->error_val = PS_SUCCESS; } else { pop3_session->state = POP3_TOP_RECV; session_recv_data(session, 0, ".\r\n"); } break; case POP3_DELETE: pop3_delete_recv(pop3_session); if (pop3_session->cur_msg == pop3_session->count) val = pop3_logout_send(pop3_session); else { pop3_session->cur_msg++; if (pop3_lookup_next(pop3_session) == POP3_ERROR) return -1; } break; case POP3_LOGOUT: pop3_session->state = POP3_DONE; session_disconnect(session); break; case POP3_ERROR: default: return -1; } return val == PS_SUCCESS?0:-1; }
static gint pop3_session_recv_data_finished(Session *session, guchar *data, guint len) { Pop3Session *pop3_session = POP3_SESSION(session); Pop3ErrorValue val = PS_SUCCESS; switch (pop3_session->state) { case POP3_GETRANGE_UIDL_RECV: val = pop3_getrange_uidl_recv(pop3_session, data, len); if (val == PS_SUCCESS) { if (pop3_session->new_msg_exist) pop3_getsize_list_send(pop3_session); else pop3_logout_send(pop3_session); } else return -1; break; case POP3_GETSIZE_LIST_RECV: val = pop3_getsize_list_recv(pop3_session, data, len); if (val == PS_SUCCESS) { if (pop3_lookup_next(pop3_session) == POP3_ERROR) return -1; } else return -1; break; case POP3_RETR_RECV: if (pop3_retr_recv(pop3_session, data, len) < 0) return -1; if (pop3_session->ac_prefs->rmmail && pop3_session->ac_prefs->msg_leave_time == 0 && pop3_session->ac_prefs->msg_leave_hour == 0 && pop3_session->msg[pop3_session->cur_msg].recv_time != RECV_TIME_KEEP) pop3_delete_send(pop3_session); else if (pop3_session->cur_msg == pop3_session->count) pop3_logout_send(pop3_session); else { pop3_session->cur_msg++; if (pop3_lookup_next(pop3_session) == POP3_ERROR) return -1; } break; case POP3_TOP_RECV: if (pop3_top_recv(pop3_session, data, len) < 0) return -1; if (pop3_session->cur_msg == pop3_session->count) pop3_logout_send(pop3_session); else { pop3_session->cur_msg++; if (pop3_lookup_next(pop3_session) == POP3_ERROR) return -1; } break; case POP3_TOP: log_warning(LOG_PROTOCOL, _("TOP command unsupported\n")); if (pop3_session->cur_msg == pop3_session->count) pop3_logout_send(pop3_session); else { pop3_session->cur_msg++; if (pop3_lookup_next(pop3_session) == POP3_ERROR) return -1; } break; case POP3_ERROR: default: return -1; } return 0; }
static IncState inc_pop3_session_do(IncSession *session) { Pop3Session *pop3_session = POP3_SESSION(session->session); IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data; gchar *server; gchar *account_name; gushort port; gchar *buf; debug_print("getting new messages of account %s...\n", pop3_session->ac_prefs->account_name); pop3_session->ac_prefs->last_pop_login_time = time(NULL); buf = g_strdup_printf(_("%s: Retrieving new messages"), pop3_session->ac_prefs->recv_server); gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf); g_free(buf); server = pop3_session->ac_prefs->recv_server; account_name = pop3_session->ac_prefs->account_name; #ifdef USE_GNUTLS port = pop3_session->ac_prefs->set_popport ? pop3_session->ac_prefs->popport : pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110; SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop; if (pop3_session->ac_prefs->ssl_pop != SSL_NONE) SESSION(pop3_session)->nonblocking = pop3_session->ac_prefs->use_nonblocking_ssl; #else if (pop3_session->ac_prefs->ssl_pop != SSL_NONE) { if (alertpanel_full(_("Insecure connection"), _("This connection is configured to be secured " "using SSL, but SSL is not available in this " "build of Claws Mail. \n\n" "Do you want to continue connecting to this " "server? The communication would not be " "secure."), GTK_STOCK_CANCEL, _("Con_tinue connecting"), NULL, FALSE, NULL, ALERT_WARNING, G_ALERTDEFAULT) != G_ALERTALTERNATE) return INC_CANCEL; } port = pop3_session->ac_prefs->set_popport ? pop3_session->ac_prefs->popport : 110; #endif buf = g_strdup_printf(_("Account '%s': Connecting to POP3 server: %s:%d..."), account_name, server, port); statuswindow_print_all("%s", buf); log_message(LOG_PROTOCOL, "%s\n", buf); progress_dialog_set_label(inc_dialog->dialog, buf); GTK_EVENTS_FLUSH(); g_free(buf); session_set_timeout(SESSION(pop3_session), prefs_common.io_timeout_secs * 1000); if (session_connect(SESSION(pop3_session), server, port) < 0) { if(!prefs_common.no_recv_err_panel) { if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) || ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) { manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL); } alertpanel_error(_("Can't connect to POP3 server: %s:%d"), server, port); manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL); } else { log_error(LOG_PROTOCOL, _("Can't connect to POP3 server: %s:%d\n"), server, port); } session->inc_state = INC_CONNECT_ERROR; statuswindow_pop_all(); return INC_CONNECT_ERROR; } while (session_is_running(SESSION(pop3_session)) && session->inc_state != INC_CANCEL) gtk_main_iteration(); if (session->inc_state == INC_SUCCESS) { switch (pop3_session->error_val) { case PS_SUCCESS: switch (SESSION(pop3_session)->state) { case SESSION_ERROR: if (pop3_session->state == POP3_READY) session->inc_state = INC_CONNECT_ERROR; else session->inc_state = INC_ERROR; break; case SESSION_EOF: session->inc_state = INC_EOF; break; case SESSION_TIMEOUT: session->inc_state = INC_TIMEOUT; break; default: session->inc_state = INC_SUCCESS; break; } break; case PS_AUTHFAIL: session->inc_state = INC_AUTH_FAILED; break; case PS_IOERR: session->inc_state = INC_IO_ERROR; break; case PS_SOCKET: session->inc_state = INC_SOCKET_ERROR; break; case PS_LOCKBUSY: session->inc_state = INC_LOCKED; break; default: session->inc_state = INC_ERROR; break; } } session_disconnect(SESSION(pop3_session)); statusbar_pop_all(); return session->inc_state; }
static gint inc_start(IncProgressDialog *inc_dialog) { IncSession *session; GList *qlist; Pop3Session *pop3_session; IncState inc_state; gint error_num = 0; gint new_msgs = 0; gchar *msg; gchar *fin_msg; FolderItem *processing, *inbox; GSList *msglist, *msglist_element; gboolean cancelled = FALSE; qlist = inc_dialog->queue_list; while (qlist != NULL) { GList *next = qlist->next; session = qlist->data; pop3_session = POP3_SESSION(session->session); pop3_session->user = g_strdup(pop3_session->ac_prefs->userid); if (pop3_session->ac_prefs->passwd) pop3_session->pass = g_strdup(pop3_session->ac_prefs->passwd); else { gchar *pass; if (inc_dialog->show_dialog) manage_window_focus_in (inc_dialog->dialog->window, NULL, NULL); pass = input_dialog_query_password_keep (pop3_session->ac_prefs->recv_server, pop3_session->user, &(pop3_session->ac_prefs->session_passwd)); if (inc_dialog->show_dialog) manage_window_focus_out (inc_dialog->dialog->window, NULL, NULL); if (pass) { pop3_session->pass = pass; } } qlist = next; } #define SET_PIXMAP_AND_TEXT(pix, str) \ { \ progress_dialog_list_set(inc_dialog->dialog, \ inc_dialog->cur_row, \ pix, \ NULL, \ str); \ } for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) { session = inc_dialog->queue_list->data; pop3_session = POP3_SESSION(session->session); GSList *filtered, *unfiltered; if (pop3_session->pass == NULL) { SET_PIXMAP_AND_TEXT(okpix, _("Cancelled")); inc_session_destroy(session); inc_dialog->queue_list = g_list_remove(inc_dialog->queue_list, session); continue; } inc_progress_dialog_clear(inc_dialog); progress_dialog_scroll_to_row(inc_dialog->dialog, inc_dialog->cur_row); SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving")); /* begin POP3 session */ inc_state = inc_pop3_session_do(session); switch (inc_state) { case INC_SUCCESS: if (pop3_session->cur_total_num > 0) msg = g_strdup_printf( ngettext("Done (%d message (%s) received)", "Done (%d messages (%s) received)", pop3_session->cur_total_num), pop3_session->cur_total_num, to_human_readable((goffset)pop3_session->cur_total_recv_bytes)); else msg = g_strdup_printf(_("Done (no new messages)")); SET_PIXMAP_AND_TEXT(okpix, msg); g_free(msg); break; case INC_CONNECT_ERROR: SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed")); break; case INC_AUTH_FAILED: SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed")); if (pop3_session->ac_prefs->session_passwd) { g_free(pop3_session->ac_prefs->session_passwd); pop3_session->ac_prefs->session_passwd = NULL; } break; case INC_LOCKED: SET_PIXMAP_AND_TEXT(errorpix, _("Locked")); break; case INC_ERROR: case INC_NO_SPACE: case INC_IO_ERROR: case INC_SOCKET_ERROR: case INC_EOF: SET_PIXMAP_AND_TEXT(errorpix, _("Error")); break; case INC_TIMEOUT: SET_PIXMAP_AND_TEXT(errorpix, _("Timeout")); break; case INC_CANCEL: SET_PIXMAP_AND_TEXT(okpix, _("Cancelled")); if (!inc_dialog->show_dialog) cancelled = TRUE; break; default: break; } if (pop3_session->error_val == PS_AUTHFAIL) { if(!prefs_common.no_recv_err_panel) { if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) || ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL); } } /* CLAWS: perform filtering actions on dropped message */ /* CLAWS: get default inbox (perhaps per account) */ if (pop3_session->ac_prefs->inbox) { /* CLAWS: get destination folder / mailbox */ inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox); if (!inbox) inbox = folder_get_default_inbox(); } else inbox = folder_get_default_inbox(); /* get list of messages in processing */ processing = folder_get_default_processing(); folder_item_scan(processing); msglist = folder_item_get_msg_list(processing); /* process messages */ folder_item_update_freeze(); procmsg_msglist_filter(msglist, pop3_session->ac_prefs, &filtered, &unfiltered, pop3_session->ac_prefs->filter_on_recv); filtering_move_and_copy_msgs(msglist); if (unfiltered != NULL) folder_item_move_msgs(inbox, unfiltered); for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) { MsgInfo *msginfo = (MsgInfo *)msglist_element->data; procmsg_msginfo_free(msginfo); } folder_item_update_thaw(); g_slist_free(msglist); g_slist_free(filtered); g_slist_free(unfiltered); statusbar_pop_all(); new_msgs += pop3_session->cur_total_num; pop3_write_uidl_list(pop3_session); if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) { error_num++; if (inc_dialog->show_dialog) manage_window_focus_in (inc_dialog->dialog->window, NULL, NULL); inc_put_error(inc_state, pop3_session); if (inc_dialog->show_dialog) manage_window_focus_out (inc_dialog->dialog->window, NULL, NULL); if (inc_state == INC_NO_SPACE || inc_state == INC_IO_ERROR) break; } folder_item_free_cache(processing, TRUE); inc_session_destroy(session); inc_dialog->queue_list = g_list_remove(inc_dialog->queue_list, session); } #undef SET_PIXMAP_AND_TEXT if (new_msgs > 0) fin_msg = g_strdup_printf(ngettext("Finished (%d new message)", "Finished (%d new messages)", new_msgs), new_msgs); else fin_msg = g_strdup_printf(_("Finished (no new messages)")); progress_dialog_set_label(inc_dialog->dialog, fin_msg); while (inc_dialog->queue_list != NULL) { session = inc_dialog->queue_list->data; inc_session_destroy(session); inc_dialog->queue_list = g_list_remove(inc_dialog->queue_list, session); } if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog) inc_progress_dialog_destroy(inc_dialog); else { gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), fin_msg); gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn), GTK_STOCK_CLOSE); } g_free(fin_msg); return new_msgs; }