claws-mail/src/send.c

561 lines
13 KiB
C
Raw Normal View History

2001-04-19 14:21:46 +02:00
/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
2002-02-28 11:06:43 +01:00
* Copyright (C) 1999-2002 Hiroyuki Yamamoto
2001-04-19 14:21:46 +02:00
*
* 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.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "defs.h"
#include <glib.h>
2001-05-10 13:19:38 +02:00
#include <gtk/gtkmain.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkwindow.h>
#include <gtk/gtkclist.h>
2001-04-19 14:21:46 +02:00
#include <stdio.h>
#include <string.h>
2001-05-10 13:19:38 +02:00
#include <sys/time.h>
#include <signal.h>
2001-05-10 13:19:38 +02:00
#include <unistd.h>
2001-04-19 14:21:46 +02:00
#include "intl.h"
#include "send.h"
#include "socket.h"
2001-08-27 13:07:43 +02:00
#include "ssl.h"
2001-04-19 14:21:46 +02:00
#include "smtp.h"
2001-08-30 14:59:38 +02:00
#include "prefs_common.h"
2001-04-19 14:21:46 +02:00
#include "prefs_account.h"
#include "account.h"
#include "compose.h"
2001-05-10 13:19:38 +02:00
#include "progressdialog.h"
2001-11-07 11:29:45 +01:00
#include "inputdialog.h"
2001-05-10 13:19:38 +02:00
#include "manage_window.h"
2001-04-19 14:21:46 +02:00
#include "procmsg.h"
#include "procheader.h"
#include "utils.h"
2001-05-10 13:19:38 +02:00
#include "gtkutils.h"
2001-04-19 14:21:46 +02:00
2001-05-10 13:19:38 +02:00
typedef struct _SendProgressDialog SendProgressDialog;
struct _SendProgressDialog
{
ProgressDialog *dialog;
GList *queue_list;
2001-06-15 12:29:27 +02:00
gboolean cancelled;
2001-05-10 13:19:38 +02:00
};
2001-04-19 14:21:46 +02:00
2001-09-02 17:07:05 +02:00
static gint send_message_data (SendProgressDialog *dialog, SockInfo *sock,
FILE *fp, gint size);
2001-05-10 13:19:38 +02:00
static SendProgressDialog *send_progress_dialog_create(void);
static void send_progress_dialog_destroy(SendProgressDialog *dialog);
static void send_cancel(GtkWidget *widget, gpointer data);
2001-04-19 14:21:46 +02:00
2001-11-07 11:29:45 +01:00
2001-04-19 14:21:46 +02:00
gint send_message(const gchar *file, PrefsAccount *ac_prefs, GSList *to_list)
{
FILE *fp;
gint val;
g_return_val_if_fail(file != NULL, -1);
g_return_val_if_fail(ac_prefs != NULL, -1);
g_return_val_if_fail(to_list != NULL, -1);
2002-03-14 11:17:32 +01:00
if ((fp = fopen(file, "rb")) == NULL) {
2001-04-19 14:21:46 +02:00
FILE_OP_ERROR(file, "fopen");
return -1;
}
2002-03-10 15:09:59 +01:00
printf("account: %p\n", ac_prefs);
if (ac_prefs->use_mail_command && ac_prefs->mail_command &&
(*ac_prefs->mail_command)) {
val = send_message_local(ac_prefs->mail_command, fp);
fclose(fp);
return val;
}
else if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
2001-08-30 14:59:38 +02:00
val = send_message_local(prefs_common.extsend_cmd, fp);
fclose(fp);
return val;
}
2002-03-10 15:09:59 +01:00
else {
val = send_message_smtp(ac_prefs, to_list, fp);
fclose(fp);
return val;
}
2001-04-19 14:21:46 +02:00
}
enum
{
Q_SENDER = 0,
Q_SMTPSERVER = 1,
2001-08-29 12:02:29 +02:00
Q_RECIPIENTS = 2,
Q_ACCOUNT_ID = 3
2001-04-19 14:21:46 +02:00
};
2002-03-10 15:09:59 +01:00
#if 0
2001-04-19 14:21:46 +02:00
gint send_message_queue(const gchar *file)
{
static HeaderEntry qentry[] = {{"S:", NULL, FALSE},
{"SSV:", NULL, FALSE},
{"R:", NULL, FALSE},
2001-08-29 12:02:29 +02:00
{"AID:", NULL, FALSE},
2001-04-19 14:21:46 +02:00
{NULL, NULL, FALSE}};
FILE *fp;
gint val;
gchar *from = NULL;
gchar *server = NULL;
GSList *to_list = NULL;
gchar buf[BUFFSIZE];
gint hnum;
2001-08-29 12:02:29 +02:00
PrefsAccount *ac = NULL;
2001-04-19 14:21:46 +02:00
g_return_val_if_fail(file != NULL, -1);
2002-03-14 11:17:32 +01:00
if ((fp = fopen(file, "rb")) == NULL) {
2001-04-19 14:21:46 +02:00
FILE_OP_ERROR(file, "fopen");
return -1;
}
while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
!= -1) {
gchar *p = buf + strlen(qentry[hnum].name);
switch (hnum) {
case Q_SENDER:
if (!from) from = g_strdup(p);
break;
case Q_SMTPSERVER:
if (!server) server = g_strdup(p);
break;
case Q_RECIPIENTS:
to_list = address_list_append(to_list, p);
break;
2001-08-29 12:02:29 +02:00
case Q_ACCOUNT_ID:
ac = account_find_from_id(atoi(p));
break;
2001-04-19 14:21:46 +02:00
default:
2002-03-07 14:22:39 +01:00
break;
2001-04-19 14:21:46 +02:00
}
}
2001-08-29 12:02:29 +02:00
if (!to_list || !from) {
2001-04-19 14:21:46 +02:00
g_warning(_("Queued message header is broken.\n"));
val = -1;
2002-03-10 15:09:59 +01:00
} else if (ac && ac->use_mail_command && ac->mail_command &&
(*ac->mail_command)) {
val = send_message_local(ac->mail_command, fp);
2001-08-30 14:59:38 +02:00
} else if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
val = send_message_local(prefs_common.extsend_cmd, fp);
2001-04-19 14:21:46 +02:00
} else {
if (!ac) {
2001-08-29 12:02:29 +02:00
ac = account_find_from_smtp_server(from, server);
if (!ac) {
g_warning(_("Account not found. "
"Using current account...\n"));
ac = cur_account;
}
2001-04-19 14:21:46 +02:00
}
2001-11-07 11:29:45 +01:00
if (ac)
val = send_message_smtp(ac, to_list, fp);
else {
PrefsAccount tmp_ac;
2001-04-19 14:21:46 +02:00
g_warning(_("Account not found.\n"));
2001-11-07 11:29:45 +01:00
memset(&tmp_ac, 0, sizeof(PrefsAccount));
tmp_ac.address = from;
tmp_ac.smtp_server = server;
tmp_ac.smtpport = SMTP_PORT;
val = send_message_smtp(&tmp_ac, to_list, fp);
2001-04-19 14:21:46 +02:00
}
}
slist_free_strings(to_list);
g_slist_free(to_list);
g_free(from);
g_free(server);
fclose(fp);
return val;
}
2002-03-10 15:09:59 +01:00
#endif
2001-04-19 14:21:46 +02:00
gint send_message_local(const gchar *command, FILE *fp)
2001-08-30 14:59:38 +02:00
{
FILE *pipefp;
gchar buf[BUFFSIZE];
2002-03-10 15:09:59 +01:00
int r;
sigset_t osig, mask;
2001-08-30 14:59:38 +02:00
g_return_val_if_fail(command != NULL, -1);
g_return_val_if_fail(fp != NULL, -1);
pipefp = popen(command, "w");
if (!pipefp) {
g_warning(_("Can't execute external command: %s\n"), command);
return -1;
}
while (fgets(buf, sizeof(buf), fp) != NULL) {
strretchomp(buf);
/* escape when a dot appears on the top */
if (buf[0] == '.')
fputc('.', pipefp);
fputs(buf, pipefp);
fputc('\n', pipefp);
}
/* we need to block SIGCHLD, otherwise pspell's handler will wait()
* the pipecommand away and pclose will return -1 because of its
* failed wait4().
*/
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
sigprocmask(SIG_BLOCK, &mask, &osig);
2002-03-10 15:09:59 +01:00
r = pclose(pipefp);
sigprocmask(SIG_SETMASK, &osig, NULL);
2002-03-10 15:09:59 +01:00
if (r != 0) {
g_warning(_("external command `%s' failed with code `%i'\n"), command, r);
2002-03-10 15:09:59 +01:00
return -1;
}
2001-08-30 14:59:38 +02:00
return 0;
}
2001-06-15 12:29:27 +02:00
#define EXIT_IF_CANCELLED() \
{ \
if (dialog->cancelled) { \
2002-04-09 17:21:52 +02:00
if (session) \
session_destroy(session); \
2001-06-15 12:29:27 +02:00
send_progress_dialog_destroy(dialog); \
return -1; \
} \
}
2001-05-10 13:19:38 +02:00
#define SEND_EXIT_IF_ERROR(f, s) \
{ \
2001-06-15 12:29:27 +02:00
EXIT_IF_CANCELLED(); \
2001-05-10 13:19:38 +02:00
if (!(f)) { \
log_warning("Error occurred while %s\n", s); \
2002-04-09 17:21:52 +02:00
if (session) \
session_destroy(session); \
2001-05-20 09:18:04 +02:00
send_progress_dialog_destroy(dialog); \
2001-05-10 13:19:38 +02:00
return -1; \
} \
}
#define SEND_EXIT_IF_NOTOK(f, s) \
{ \
gint ok; \
2001-11-07 11:29:45 +01:00
\
2001-06-15 12:29:27 +02:00
EXIT_IF_CANCELLED(); \
if ((ok = (f)) != SM_OK) { \
2001-05-10 13:19:38 +02:00
log_warning("Error occurred while %s\n", s); \
if (ok == SM_AUTHFAIL) { \
2002-04-09 17:21:52 +02:00
log_warning(_("SMTP AUTH failed\n")); \
2001-11-07 11:29:45 +01:00
if (ac_prefs->tmp_pass) { \
g_free(ac_prefs->tmp_pass); \
ac_prefs->tmp_pass = NULL; \
} \
2002-02-28 11:06:43 +01:00
if (ac_prefs->tmp_smtp_pass) { \
g_free(ac_prefs->tmp_smtp_pass); \
ac_prefs->tmp_smtp_pass = NULL; \
} \
} \
2002-04-09 17:21:52 +02:00
if (smtp_quit(session->sock) != SM_OK) \
log_warning(_("Error occurred while sending QUIT\n")); \
session_destroy(session); \
2001-05-20 09:18:04 +02:00
send_progress_dialog_destroy(dialog); \
2001-05-10 13:19:38 +02:00
return -1; \
} \
}
gint send_message_smtp(PrefsAccount *ac_prefs, GSList *to_list,
2001-08-27 13:07:43 +02:00
FILE *fp)
2001-04-19 14:21:46 +02:00
{
2001-05-10 13:19:38 +02:00
SendProgressDialog *dialog;
2002-04-09 17:21:52 +02:00
Session *session = NULL;
2001-05-10 13:19:38 +02:00
GtkCList *clist;
const gchar *text[3];
2001-04-19 14:21:46 +02:00
gchar buf[BUFFSIZE];
2001-11-07 11:29:45 +01:00
gushort port;
gchar *domain;
gchar *user = NULL;
2001-11-07 11:29:45 +01:00
gchar *pass = NULL;
2001-04-19 14:21:46 +02:00
GSList *cur;
2001-05-10 13:19:38 +02:00
gint size;
2001-04-19 14:21:46 +02:00
2001-11-07 11:29:45 +01:00
g_return_val_if_fail(ac_prefs != NULL, -1);
g_return_val_if_fail(ac_prefs->address != NULL, -1);
g_return_val_if_fail(ac_prefs->smtp_server != NULL, -1);
2001-04-19 14:21:46 +02:00
g_return_val_if_fail(to_list != NULL, -1);
g_return_val_if_fail(fp != NULL, -1);
2001-11-07 11:29:45 +01:00
size = get_left_file_size(fp);
if (size < 0) return -1;
#if USE_SSL
2001-11-07 11:29:45 +01:00
port = ac_prefs->set_smtpport ? ac_prefs->smtpport :
ac_prefs->ssl_smtp == SSL_SMTP_TUNNEL ? SSMTP_PORT : SMTP_PORT;
#else
2001-11-07 11:29:45 +01:00
port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
#endif
2001-11-07 11:29:45 +01:00
domain = ac_prefs->set_domain ? ac_prefs->domain : NULL;
if (ac_prefs->use_smtp_auth) {
if (ac_prefs->smtp_userid) {
user = ac_prefs->smtp_userid;
2002-02-28 11:06:43 +01:00
if (ac_prefs->smtp_passwd)
pass = ac_prefs->smtp_passwd;
2002-02-28 11:06:43 +01:00
else if (ac_prefs->tmp_smtp_pass)
pass = ac_prefs->tmp_smtp_pass;
else {
2002-01-16 12:48:36 +01:00
pass = input_dialog_query_password
2002-02-28 11:06:43 +01:00
(ac_prefs->smtp_server, user);
if (!pass) pass = g_strdup("");
2002-02-28 11:06:43 +01:00
ac_prefs->tmp_smtp_pass = pass;
}
} else {
user = ac_prefs->userid;
2002-02-28 11:06:43 +01:00
if (ac_prefs->passwd)
pass = ac_prefs->passwd;
2002-02-28 11:06:43 +01:00
else if (ac_prefs->tmp_pass)
pass = ac_prefs->tmp_pass;
else {
2002-01-16 12:48:36 +01:00
pass = input_dialog_query_password
2002-02-28 11:06:43 +01:00
(ac_prefs->smtp_server, user);
if (!pass) pass = g_strdup("");
ac_prefs->tmp_pass = pass;
}
}
}
2001-05-10 13:19:38 +02:00
dialog = send_progress_dialog_create();
text[0] = NULL;
2001-11-07 11:29:45 +01:00
text[1] = ac_prefs->smtp_server;
2001-05-10 13:19:38 +02:00
text[2] = _("Standby");
clist = GTK_CLIST(dialog->dialog->clist);
gtk_clist_append(clist, (gchar **)text);
g_snprintf(buf, sizeof(buf), _("Connecting to SMTP server: %s ..."),
2001-11-07 11:29:45 +01:00
ac_prefs->smtp_server);
2001-05-10 13:19:38 +02:00
log_message("%s\n", buf);
progress_dialog_set_label(dialog->dialog, buf);
gtk_clist_set_text(clist, 0, 2, _("Connecting"));
GTK_EVENTS_FLUSH();
2001-04-19 14:21:46 +02:00
2001-08-27 13:07:43 +02:00
#if USE_SSL
2002-04-09 17:21:52 +02:00
SEND_EXIT_IF_ERROR((session = smtp_session_new
2001-11-07 11:29:45 +01:00
(ac_prefs->smtp_server, port, domain,
2002-04-09 17:21:52 +02:00
user, pass, ac_prefs->ssl_smtp)),
2001-04-19 14:21:46 +02:00
"connecting to server");
#else
2002-04-09 17:21:52 +02:00
SEND_EXIT_IF_ERROR((session = smtp_session_new
2001-11-07 11:29:45 +01:00
(ac_prefs->smtp_server, port, domain,
2002-04-09 17:21:52 +02:00
user, pass)),
"connecting to server");
#endif
2001-05-10 13:19:38 +02:00
progress_dialog_set_label(dialog->dialog, _("Sending MAIL FROM..."));
gtk_clist_set_text(clist, 0, 2, _("Sending"));
GTK_EVENTS_FLUSH();
2001-04-19 14:21:46 +02:00
SEND_EXIT_IF_NOTOK
2002-04-09 17:21:52 +02:00
(smtp_from(SMTP_SESSION(session), ac_prefs->address),
2001-04-19 14:21:46 +02:00
"sending MAIL FROM");
2001-05-10 13:19:38 +02:00
progress_dialog_set_label(dialog->dialog, _("Sending RCPT TO..."));
GTK_EVENTS_FLUSH();
2001-04-19 14:21:46 +02:00
for (cur = to_list; cur != NULL; cur = cur->next)
2002-04-09 17:21:52 +02:00
SEND_EXIT_IF_NOTOK(smtp_rcpt(session->sock, (gchar *)cur->data),
2001-04-19 14:21:46 +02:00
"sending RCPT TO");
2001-05-10 13:19:38 +02:00
progress_dialog_set_label(dialog->dialog, _("Sending DATA..."));
GTK_EVENTS_FLUSH();
2002-04-09 17:21:52 +02:00
SEND_EXIT_IF_NOTOK(smtp_data(session->sock), "sending DATA");
2001-04-19 14:21:46 +02:00
2001-09-02 17:07:05 +02:00
/* send main part */
2002-04-09 17:21:52 +02:00
SEND_EXIT_IF_ERROR
(send_message_data(dialog, session->sock, fp, size) == 0,
"sending data");
2001-09-02 17:07:05 +02:00
progress_dialog_set_label(dialog->dialog, _("Quitting..."));
GTK_EVENTS_FLUSH();
2002-04-09 17:21:52 +02:00
SEND_EXIT_IF_NOTOK(smtp_eom(session->sock), "terminating data");
SEND_EXIT_IF_NOTOK(smtp_quit(session->sock), "sending QUIT");
2001-09-02 17:07:05 +02:00
2002-04-09 17:21:52 +02:00
session_destroy(session);
2001-09-02 17:07:05 +02:00
send_progress_dialog_destroy(dialog);
return 0;
}
#undef EXIT_IF_CANCELLED
#undef SEND_EXIT_IF_ERROR
#undef SEND_EXIT_IF_NOTOK
#define EXIT_IF_CANCELLED() \
{ \
if (dialog->cancelled) return -1; \
}
#define SEND_EXIT_IF_ERROR(f) \
{ \
EXIT_IF_CANCELLED(); \
if ((f) <= 0) return -1; \
}
#define SEND_DIALOG_UPDATE() \
{ \
gettimeofday(&tv_cur, NULL); \
if (tv_cur.tv_sec - tv_prev.tv_sec > 0 || \
tv_cur.tv_usec - tv_prev.tv_usec > UI_REFRESH_INTERVAL) { \
g_snprintf(str, sizeof(str), \
_("Sending message (%d / %d bytes)"), \
bytes, size); \
progress_dialog_set_label(dialog->dialog, str); \
progress_dialog_set_percentage \
(dialog->dialog, (gfloat)bytes / (gfloat)size); \
GTK_EVENTS_FLUSH(); \
gettimeofday(&tv_prev, NULL); \
} \
}
static gint send_message_data(SendProgressDialog *dialog, SockInfo *sock,
FILE *fp, gint size)
{
gchar buf[BUFFSIZE];
gchar str[BUFFSIZE];
gint bytes = 0;
struct timeval tv_prev, tv_cur;
2001-05-10 13:19:38 +02:00
gettimeofday(&tv_prev, NULL);
2001-09-02 17:07:05 +02:00
/* output header part */
2001-04-19 14:21:46 +02:00
while (fgets(buf, sizeof(buf), fp) != NULL) {
2001-05-10 13:19:38 +02:00
bytes += strlen(buf);
2001-04-19 14:21:46 +02:00
strretchomp(buf);
2001-09-02 17:07:05 +02:00
SEND_DIALOG_UPDATE();
if (!g_strncasecmp(buf, "Bcc:", 4)) {
gint next;
for (;;) {
next = fgetc(fp);
2001-09-02 20:20:59 +02:00
if (next == EOF)
break;
else if (next != ' ' && next != '\t') {
2001-09-02 17:07:05 +02:00
ungetc(next, fp);
break;
}
if (fgets(buf, sizeof(buf), fp) == NULL)
break;
else
bytes += strlen(buf);
}
} else {
SEND_EXIT_IF_ERROR(sock_puts(sock, buf));
2001-09-02 20:20:59 +02:00
if (buf[0] == '\0')
break;
2001-05-10 13:19:38 +02:00
}
2001-09-02 17:07:05 +02:00
}
/* output body part */
while (fgets(buf, sizeof(buf), fp) != NULL) {
bytes += strlen(buf);
strretchomp(buf);
SEND_DIALOG_UPDATE();
2001-05-10 13:19:38 +02:00
2001-04-19 14:21:46 +02:00
/* escape when a dot appears on the top */
if (buf[0] == '.')
2001-09-02 17:07:05 +02:00
SEND_EXIT_IF_ERROR(sock_write(sock, ".", 1));
2001-04-19 14:21:46 +02:00
2001-09-02 17:07:05 +02:00
SEND_EXIT_IF_ERROR(sock_puts(sock, buf));
2001-04-19 14:21:46 +02:00
}
2001-09-02 17:07:05 +02:00
g_snprintf(str, sizeof(str), _("Sending message (%d / %d bytes)"),
bytes, size);
progress_dialog_set_label(dialog->dialog, str);
progress_dialog_set_percentage
(dialog->dialog, (gfloat)bytes / (gfloat)size);
2001-05-10 13:19:38 +02:00
GTK_EVENTS_FLUSH();
2001-04-19 14:21:46 +02:00
return 0;
}
2001-09-02 17:07:05 +02:00
#undef EXIT_IF_CANCELLED
#undef SEND_EXIT_IF_ERROR
#undef SEND_DIALOG_UPDATE
2001-05-10 13:19:38 +02:00
static SendProgressDialog *send_progress_dialog_create(void)
{
SendProgressDialog *dialog;
ProgressDialog *progress;
dialog = g_new0(SendProgressDialog, 1);
progress = progress_dialog_create();
gtk_window_set_title(GTK_WINDOW(progress->window),
_("Sending message"));
gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
GTK_SIGNAL_FUNC(send_cancel), dialog);
gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
GTK_SIGNAL_FUNC(gtk_true), NULL);
2001-08-22 12:58:38 +02:00
gtk_window_set_modal(GTK_WINDOW(progress->window), TRUE);
2001-05-10 13:19:38 +02:00
manage_window_set_transient(GTK_WINDOW(progress->window));
progress_dialog_set_value(progress, 0.0);
gtk_widget_show_now(progress->window);
dialog->dialog = progress;
dialog->queue_list = NULL;
2001-06-15 12:29:27 +02:00
dialog->cancelled = FALSE;
2001-05-10 13:19:38 +02:00
return dialog;
}
static void send_progress_dialog_destroy(SendProgressDialog *dialog)
{
g_return_if_fail(dialog != NULL);
progress_dialog_destroy(dialog->dialog);
g_free(dialog);
}
static void send_cancel(GtkWidget *widget, gpointer data)
{
SendProgressDialog *dialog = data;
2001-06-15 12:29:27 +02:00
dialog->cancelled = TRUE;
2001-05-10 13:19:38 +02:00
}