claws-mail/src/send.c

429 lines
10 KiB
C
Raw Normal View History

2001-04-19 14:21:46 +02:00
/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
2001-05-10 13:19:38 +02:00
* Copyright (C) 1999-2001 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 <unistd.h>
2001-04-19 14:21:46 +02:00
#include "intl.h"
#include "send.h"
#include "socket.h"
#include "smtp.h"
#include "prefs_account.h"
#include "account.h"
#include "compose.h"
2001-05-10 13:19:38 +02:00
#include "progressdialog.h"
#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
#define SMTP_PORT 25
2001-05-10 13:19:38 +02:00
typedef struct _SendProgressDialog SendProgressDialog;
struct _SendProgressDialog
{
ProgressDialog *dialog;
GList *queue_list;
};
2001-04-19 14:21:46 +02:00
static gint send_message_smtp (GSList *to_list, const gchar *from,
const gchar *server, gushort port,
const gchar *domain, const gchar *userid,
const gchar *passwd, gboolean use_smtp_auth,
FILE *fp);
2001-04-27 22:27:24 +02:00
static SockInfo *send_smtp_open (const gchar *server, gushort port,
2001-04-19 14:21:46 +02:00
const gchar *domain, gboolean use_smtp_auth);
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-05-10 13:19:38 +02:00
static gint send_message_with_command(GSList *to_list, gchar * mailcmd,
FILE * fp);
2001-04-19 14:21:46 +02:00
gint send_message(const gchar *file, PrefsAccount *ac_prefs, GSList *to_list)
{
FILE *fp;
gint val;
gushort port;
gchar *domain;
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);
if ((fp = fopen(file, "r")) == NULL) {
FILE_OP_ERROR(file, "fopen");
return -1;
}
2001-05-14 11:32:34 +02:00
if (ac_prefs->protocol == A_LOCAL && ac_prefs->use_mail_command) {
val = send_message_with_command(to_list,
ac_prefs->mail_command,
fp);
} else {
port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
domain = ac_prefs->set_domain ? ac_prefs->domain : NULL;
val = send_message_smtp(to_list, ac_prefs->address,
ac_prefs->smtp_server, port,
domain, ac_prefs->userid,
ac_prefs->passwd,
ac_prefs->use_smtp_auth, fp);
}
2001-04-19 14:21:46 +02:00
fclose(fp);
return val;
}
enum
{
Q_SENDER = 0,
Q_SMTPSERVER = 1,
Q_RECIPIENTS = 2
};
static gint send_message_with_command(GSList *to_list, gchar * mailcmd,
FILE * fp)
{
FILE * p;
int len;
gchar * cmdline;
GSList *cur;
gchar buf[BUFFSIZE];
len = strlen(mailcmd);
for (cur = to_list; cur != NULL; cur = cur->next)
len += strlen((gchar *)cur->data) + 1;
cmdline = g_new(gchar, len + 1);
strcpy(cmdline, mailcmd);
2001-05-14 11:32:34 +02:00
for (cur = to_list; cur != NULL; cur = cur->next) {
cmdline = strncat(cmdline, " ", len);
cmdline = strncat(cmdline, (gchar *)cur->data, len);
}
log_message(_("Using command to send mail: %s ...\n"), cmdline);
p = popen(cmdline, "w");
2001-05-14 11:32:34 +02:00
if (p != NULL) {
while (fgets(buf, sizeof(buf), fp) != NULL) {
strretchomp(buf);
/* escape when a dot appears on the top */
if (buf[0] == '.')
fputs(".", p);
fputs(buf, p);
fputs("\n", p);
}
2001-05-14 11:37:34 +02:00
pclose(p);
2001-05-14 11:32:34 +02:00
}
log_message(_("Mail sent successfully ...\n"));
return 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},
{NULL, NULL, FALSE}};
FILE *fp;
gint val;
gchar *from = NULL;
gchar *server = NULL;
GSList *to_list = NULL;
gchar buf[BUFFSIZE];
gint hnum;
g_return_val_if_fail(file != NULL, -1);
if ((fp = fopen(file, "r")) == NULL) {
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;
default:
}
}
if (!to_list || !from || !server) {
g_warning(_("Queued message header is broken.\n"));
val = -1;
} else {
gushort port;
gchar *domain;
PrefsAccount *ac;
ac = account_find_from_smtp_server(from, server);
if (!ac) {
g_warning(_("Account not found. Using current account...\n"));
ac = cur_account;
}
if (ac) {
port = ac->set_smtpport ? ac->smtpport : SMTP_PORT;
domain = ac->set_domain ? ac->domain : NULL;
val = send_message_smtp
(to_list, from, server, port, domain,
ac->userid, ac->passwd, ac->use_smtp_auth, fp);
} else {
g_warning(_("Account not found.\n"));
val = send_message_smtp
(to_list, from, server, SMTP_PORT, NULL,
NULL, NULL, FALSE, fp);
}
}
slist_free_strings(to_list);
g_slist_free(to_list);
g_free(from);
g_free(server);
fclose(fp);
return val;
}
2001-05-10 13:19:38 +02:00
#define SEND_EXIT_IF_ERROR(f, s) \
{ \
if (!(f)) { \
log_warning("Error occurred while %s\n", s); \
sock_close(smtp_sock); \
smtp_sock = NULL; \
return -1; \
} \
}
#define SEND_EXIT_IF_NOTOK(f, s) \
{ \
if ((f) != SM_OK) { \
log_warning("Error occurred while %s\n", s); \
if (smtp_quit(smtp_sock) != SM_OK) \
log_warning("Error occurred while sending QUIT\n"); \
sock_close(smtp_sock); \
smtp_sock = NULL; \
return -1; \
} \
}
2001-04-19 14:21:46 +02:00
static gint send_message_smtp(GSList *to_list, const gchar *from,
const gchar *server, gushort port,
const gchar *domain, const gchar *userid,
2001-05-10 13:19:38 +02:00
const gchar *passwd, gboolean use_smtp_auth,
2001-04-19 14:21:46 +02:00
FILE *fp)
{
2001-04-27 22:27:24 +02:00
SockInfo *smtp_sock;
2001-05-10 13:19:38 +02:00
SendProgressDialog *dialog;
GtkCList *clist;
const gchar *text[3];
2001-04-19 14:21:46 +02:00
gchar buf[BUFFSIZE];
2001-05-10 13:19:38 +02:00
gchar str[BUFFSIZE];
2001-04-19 14:21:46 +02:00
GSList *cur;
2001-05-10 13:19:38 +02:00
gint size;
gint bytes = 0;
struct timeval tv_prev, tv_cur;
2001-04-19 14:21:46 +02:00
g_return_val_if_fail(to_list != NULL, -1);
g_return_val_if_fail(from != NULL, -1);
g_return_val_if_fail(server != NULL, -1);
g_return_val_if_fail(fp != NULL, -1);
2001-05-10 13:19:38 +02:00
size = get_left_file_size(fp);
if (size < 0) return -1;
dialog = send_progress_dialog_create();
text[0] = NULL;
text[1] = server;
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 ..."),
server);
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
SEND_EXIT_IF_ERROR((smtp_sock = send_smtp_open
(server, port, domain, use_smtp_auth)),
"connecting to server");
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
(smtp_from(smtp_sock, from, userid, passwd, use_smtp_auth),
"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)
SEND_EXIT_IF_NOTOK(smtp_rcpt(smtp_sock, (gchar *)cur->data),
"sending RCPT TO");
2001-05-10 13:19:38 +02:00
progress_dialog_set_label(dialog->dialog, _("Sending DATA..."));
GTK_EVENTS_FLUSH();
2001-04-19 14:21:46 +02:00
SEND_EXIT_IF_NOTOK(smtp_data(smtp_sock), "sending DATA");
2001-05-10 13:19:38 +02:00
gettimeofday(&tv_prev, NULL);
2001-04-19 14:21:46 +02:00
/* send main part */
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-05-10 13:19:38 +02:00
gettimeofday(&tv_cur, NULL);
if (tv_cur.tv_sec - tv_prev.tv_sec > 0 ||
tv_cur.tv_usec - tv_prev.tv_usec > 10000) {
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);
}
2001-04-19 14:21:46 +02:00
/* escape when a dot appears on the top */
if (buf[0] == '.')
SEND_EXIT_IF_ERROR(sock_write(smtp_sock, ".", 1),
"sending data");
SEND_EXIT_IF_ERROR(sock_puts(smtp_sock, buf), "sending data");
}
2001-05-10 13:19:38 +02:00
progress_dialog_set_label(dialog->dialog, _("Quitting..."));
GTK_EVENTS_FLUSH();
2001-04-19 14:21:46 +02:00
SEND_EXIT_IF_NOTOK(smtp_eom(smtp_sock), "terminating data");
SEND_EXIT_IF_NOTOK(smtp_quit(smtp_sock), "sending QUIT");
sock_close(smtp_sock);
2001-05-10 13:19:38 +02:00
send_progress_dialog_destroy(dialog);
2001-04-19 14:21:46 +02:00
return 0;
}
2001-04-27 22:27:24 +02:00
static SockInfo *send_smtp_open(const gchar *server, gushort port,
2001-04-19 14:21:46 +02:00
const gchar *domain, gboolean use_smtp_auth)
{
2001-04-27 22:27:24 +02:00
SockInfo *sock;
2001-04-19 14:21:46 +02:00
gint val;
2001-04-27 22:27:24 +02:00
g_return_val_if_fail(server != NULL, NULL);
2001-04-19 14:21:46 +02:00
2001-04-27 22:27:24 +02:00
if ((sock = sock_connect(server, port)) == NULL) {
2001-04-19 14:21:46 +02:00
log_warning(_("Can't connect to SMTP server: %s:%d\n"),
server, port);
2001-04-27 22:27:24 +02:00
return NULL;
2001-04-19 14:21:46 +02:00
}
if (smtp_ok(sock) == SM_OK) {
val = smtp_helo(sock, domain ? domain : get_domain_name(),
use_smtp_auth);
if (val == SM_OK) return sock;
}
log_warning(_("Error occurred while sending HELO\n"));
sock_close(sock);
2001-04-27 22:27:24 +02:00
return NULL;
2001-04-19 14:21:46 +02:00
}
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);
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;
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;
g_print("cancelled\n");
}