2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <gtk/gtkmain.h>
28 #include <gtk/gtksignal.h>
29 #include <gtk/gtkwindow.h>
30 #include <gtk/gtkclist.h>
36 #include "send_message.h"
40 #include "prefs_common.h"
41 #include "prefs_account.h"
42 #include "procheader.h"
44 #include "progressdialog.h"
45 #include "inputdialog.h"
46 #include "manage_window.h"
49 #include "statusbar.h"
53 typedef struct _SendProgressDialog SendProgressDialog;
55 struct _SendProgressDialog
57 ProgressDialog *dialog;
62 static gint send_message_local (const gchar *command,
64 static gint send_message_smtp (PrefsAccount *ac_prefs,
69 static gint send_recv_message (Session *session,
72 static gint send_send_data_progressive (Session *session,
76 static gint send_send_data_finished (Session *session,
80 static SendProgressDialog *send_progress_dialog_create (void);
81 static void send_progress_dialog_destroy (SendProgressDialog *dialog);
83 static void send_cancel_button_cb (GtkWidget *widget,
86 gint send_message(const gchar *file, PrefsAccount *ac_prefs, GSList *to_list)
91 g_return_val_if_fail(file != NULL, -1);
92 g_return_val_if_fail(ac_prefs != NULL, -1);
93 g_return_val_if_fail(to_list != NULL, -1);
95 if ((fp = fopen(file, "rb")) == NULL) {
96 FILE_OP_ERROR(file, "fopen");
100 if (ac_prefs->use_mail_command && ac_prefs->mail_command &&
101 (*ac_prefs->mail_command)) {
102 val = send_message_local(ac_prefs->mail_command, fp);
106 else if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
107 val = send_message_local(prefs_common.extsend_cmd, fp);
112 val = send_message_smtp(ac_prefs, to_list, fp);
128 gint send_message_queue(const gchar *file)
130 static HeaderEntry qentry[] = {{"S:", NULL, FALSE},
131 {"SSV:", NULL, FALSE},
133 {"AID:", NULL, FALSE},
134 {NULL, NULL, FALSE}};
138 gchar *server = NULL;
139 GSList *to_list = NULL;
143 PrefsAccount *ac = NULL, *mailac = NULL, *newsac = NULL;
145 g_return_val_if_fail(file != NULL, -1);
147 if ((fp = fopen(file, "rb")) == NULL) {
148 FILE_OP_ERROR(file, "fopen");
152 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
156 p = buf + strlen(qentry[hnum].name);
160 if (!from) from = g_strdup(p);
163 if (!server) server = g_strdup(p);
166 to_list = address_list_append(to_list, p);
169 ac = account_find_from_id(atoi(p));
176 if (((!ac || (ac && ac->protocol != A_NNTP)) && !to_list) || !from) {
177 g_warning("Queued message header is broken.\n");
179 } else if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
180 val = send_message_local(prefs_common.extsend_cmd, fp);
182 if (ac && ac->protocol == A_NNTP) {
185 /* search mail account */
186 mailac = account_find_from_address(from);
189 cur_account->protocol != A_NNTP)
190 mailac = cur_account;
192 mailac = account_get_default();
193 if (mailac->protocol == A_NNTP)
200 ac = account_find_from_smtp_server(from, server);
202 g_warning("Account not found. "
203 "Using current account...\n");
205 if (ac && ac->protocol != A_NNTP)
213 val = send_message_smtp(mailac, to_list, fp);
217 g_warning("Account not found.\n");
219 memset(&tmp_ac, 0, sizeof(PrefsAccount));
220 tmp_ac.address = from;
221 tmp_ac.smtp_server = server;
222 tmp_ac.smtpport = SMTP_PORT;
223 val = send_message_smtp(&tmp_ac, to_list, fp);
227 if (val == 0 && newsac) {
228 fseek(fp, fpos, SEEK_SET);
229 val = news_post_stream(FOLDER(newsac->folder), fp);
233 slist_free_strings(to_list);
234 g_slist_free(to_list);
243 gint send_message_local(const gchar *command, FILE *fp)
250 g_return_val_if_fail(command != NULL, -1);
251 g_return_val_if_fail(fp != NULL, -1);
253 pipefp = popen(command, "w");
255 g_warning("Can't execute external command: %s\n", command);
259 while (fgets(buf, sizeof(buf), fp) != NULL) {
265 /* we need to block SIGCHLD, otherwise pspell's handler will wait()
266 * the pipecommand away and pclose will return -1 because of its
270 sigaddset(&mask, SIGCHLD);
271 sigprocmask(SIG_BLOCK, &mask, &osig);
275 sigprocmask(SIG_SETMASK, &osig, NULL);
277 g_warning("external command `%s' failed with code `%i'\n", command, r);
284 gint send_message_smtp(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp)
287 SMTPSession *smtp_session;
289 SendProgressDialog *dialog;
291 const gchar *text[3];
295 g_return_val_if_fail(ac_prefs != NULL, -1);
296 g_return_val_if_fail(ac_prefs->address != NULL, -1);
297 g_return_val_if_fail(ac_prefs->smtp_server != NULL, -1);
298 g_return_val_if_fail(to_list != NULL, -1);
299 g_return_val_if_fail(fp != NULL, -1);
301 session = smtp_session_new();
302 smtp_session = SMTP_SESSION(session);
304 smtp_session->hostname =
305 ac_prefs->set_domain ? g_strdup(ac_prefs->domain) : NULL;
307 if (ac_prefs->use_smtp_auth) {
308 smtp_session->forced_auth_type = ac_prefs->smtp_auth_type;
310 if (ac_prefs->smtp_userid) {
311 smtp_session->user = g_strdup(ac_prefs->smtp_userid);
312 if (ac_prefs->smtp_passwd)
314 g_strdup(ac_prefs->smtp_passwd);
315 else if (ac_prefs->tmp_smtp_pass)
317 g_strdup(ac_prefs->tmp_smtp_pass);
320 input_dialog_query_password
321 (ac_prefs->smtp_server,
323 if (!smtp_session->pass)
324 smtp_session->pass = g_strdup("");
325 ac_prefs->tmp_smtp_pass =
326 g_strdup(smtp_session->pass);
329 smtp_session->user = g_strdup(ac_prefs->userid);
330 if (ac_prefs->passwd)
331 smtp_session->pass = g_strdup(ac_prefs->passwd);
332 else if (ac_prefs->tmp_pass)
334 g_strdup(ac_prefs->tmp_pass);
337 input_dialog_query_password
338 (ac_prefs->smtp_server,
340 if (!smtp_session->pass)
341 smtp_session->pass = g_strdup("");
343 g_strdup(smtp_session->pass);
347 smtp_session->user = NULL;
348 smtp_session->pass = NULL;
351 smtp_session->from = g_strdup(ac_prefs->address);
352 smtp_session->to_list = to_list;
353 smtp_session->cur_to = to_list;
354 smtp_session->send_data = get_outgoing_rfc2822_str(fp);
355 smtp_session->send_data_len = strlen(smtp_session->send_data);
358 port = ac_prefs->set_smtpport ? ac_prefs->smtpport :
359 ac_prefs->ssl_smtp == SSL_TUNNEL ? SSMTP_PORT : SMTP_PORT;
360 session->ssl_type = ac_prefs->ssl_smtp;
362 port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
365 dialog = send_progress_dialog_create();
366 dialog->session = session;
369 text[1] = ac_prefs->smtp_server;
370 text[2] = _("Connecting");
371 clist = GTK_CLIST(dialog->dialog->clist);
372 gtk_clist_append(clist, (gchar **)text);
374 if (ac_prefs->pop_before_smtp
375 && (ac_prefs->protocol == A_APOP || ac_prefs->protocol == A_POP3)
376 && (time(NULL) - ac_prefs->last_pop_login_time) > (60 * ac_prefs->pop_before_smtp_timeout)) {
377 g_snprintf(buf, sizeof(buf), _("Doing POP before SMTP..."));
379 progress_dialog_set_label(dialog->dialog, buf);
380 gtk_clist_set_text(clist, 0, 2, _("POP before SMTP"));
382 inc_pop_before_smtp(ac_prefs);
385 g_snprintf(buf, sizeof(buf), _("Connecting to SMTP server: %s ..."),
386 ac_prefs->smtp_server);
387 progress_dialog_set_label(dialog->dialog, buf);
388 log_message("%s\n", buf);
390 session_set_recv_message_notify(session, send_recv_message, dialog);
391 session_set_send_data_progressive_notify
392 (session, send_send_data_progressive, dialog);
393 session_set_send_data_notify(session, send_send_data_finished, dialog);
395 if (session_connect(session, ac_prefs->smtp_server, port) < 0) {
396 session_destroy(session);
397 send_progress_dialog_destroy(dialog);
401 g_print("parent: begin event loop\n");
403 while (session->state != SESSION_DISCONNECTED &&
404 session->state != SESSION_ERROR)
405 gtk_main_iteration();
407 if (SMTP_SESSION(session)->error_val == SM_AUTHFAIL) {
408 if (ac_prefs->smtp_userid && ac_prefs->tmp_smtp_pass) {
409 g_free(ac_prefs->tmp_smtp_pass);
410 ac_prefs->tmp_smtp_pass = NULL;
413 } else if (session->state == SESSION_ERROR ||
414 SMTP_SESSION(session)->state == SMTP_ERROR ||
415 SMTP_SESSION(session)->error_val != SM_OK)
417 else if (dialog->cancelled == TRUE)
420 session_destroy(session);
421 send_progress_dialog_destroy(dialog);
423 statusbar_verbosity_set(FALSE);
427 static gint send_recv_message(Session *session, const gchar *msg, gpointer data)
429 SMTPSession *smtp_session;
430 SendProgressDialog *dialog;
434 dialog = (SendProgressDialog *) data;
436 smtp_session = SMTP_SESSION(session);
438 switch (smtp_session->state) {
443 g_snprintf(buf, sizeof(buf), _("Sending HELO..."));
444 state_str = _("Authenticating");
447 g_snprintf(buf, sizeof(buf), _("Sending EHLO..."));
448 state_str = _("Authenticating");
451 g_snprintf(buf, sizeof(buf), _("Authenticating..."));
452 state_str = _("Authenticating");
455 g_snprintf(buf, sizeof(buf), _("Sending MAIL FROM..."));
456 state_str = _("Sending");
459 g_snprintf(buf, sizeof(buf), _("Sending RCPT TO..."));
460 state_str = _("Sending");
464 g_snprintf(buf, sizeof(buf), _("Sending DATA..."));
465 state_str = _("Sending");
468 g_snprintf(buf, sizeof(buf), _("Quitting..."));
469 state_str = _("Quitting");
472 case SMTP_AUTH_FAILED:
473 g_warning("send: error: %s\n", msg);
479 progress_dialog_set_label(dialog->dialog, buf);
480 gtk_clist_set_text(GTK_CLIST(dialog->dialog->clist), 0, 2, state_str);
485 static gint send_send_data_progressive(Session *session, guint cur_len,
486 guint total_len, gpointer data)
488 SendProgressDialog *dialog = (SendProgressDialog *)data;
491 g_snprintf(buf, sizeof(buf), _("Sending message (%d / %d bytes)"),
493 progress_dialog_set_label(dialog->dialog, buf);
494 progress_dialog_set_percentage
495 (dialog->dialog, (gfloat)cur_len / (gfloat)total_len);
499 static gint send_send_data_finished(Session *session, guint len, gpointer data)
501 SendProgressDialog *dialog = (SendProgressDialog *)data;
504 g_snprintf(buf, sizeof(buf), _("Sending message (%d / %d bytes)"),
506 progress_dialog_set_label(dialog->dialog, buf);
507 progress_dialog_set_percentage
508 (dialog->dialog, (gfloat)len / (gfloat)len);
512 static SendProgressDialog *send_progress_dialog_create(void)
514 SendProgressDialog *dialog;
515 ProgressDialog *progress;
517 dialog = g_new0(SendProgressDialog, 1);
519 progress = progress_dialog_create();
520 gtk_window_set_title(GTK_WINDOW(progress->window),
521 _("Sending message"));
522 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
523 GTK_SIGNAL_FUNC(send_cancel_button_cb), dialog);
524 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
525 GTK_SIGNAL_FUNC(gtk_true), NULL);
526 gtk_window_set_modal(GTK_WINDOW(progress->window), TRUE);
527 manage_window_set_transient(GTK_WINDOW(progress->window));
529 progress_dialog_set_value(progress, 0.0);
531 if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
532 gtk_widget_show_now(progress->window);
535 dialog->dialog = progress;
540 static void send_progress_dialog_destroy(SendProgressDialog *dialog)
542 g_return_if_fail(dialog != NULL);
543 if (prefs_common.send_dialog_mode == SEND_DIALOG_ALWAYS) {
544 progress_dialog_destroy(dialog->dialog);
549 static void send_cancel_button_cb(GtkWidget *widget, gpointer data)
551 SendProgressDialog *dialog = (SendProgressDialog *)data;
552 Session *session = dialog->session;
554 session->state = SESSION_DISCONNECTED;
555 dialog->cancelled = TRUE;