Statistics
| Revision:

root / src / send_message.c @ 1

History | View | Annotate | Download (15.2 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2003 Hiroyuki Yamamoto
4
 *
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.
9
 *
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.
14
 *
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.
18
 */
19

    
20
#ifdef HAVE_CONFIG_H
21
#  include "config.h"
22
#endif
23

    
24
#include "defs.h"
25

    
26
#include <glib.h>
27
#include <gtk/gtkmain.h>
28
#include <gtk/gtksignal.h>
29
#include <gtk/gtkwindow.h>
30
#include <gtk/gtkclist.h>
31
#include <stdio.h>
32
#include <string.h>
33

    
34
#include "intl.h"
35
#include "send_message.h"
36
#include "session.h"
37
#include "ssl.h"
38
#include "smtp.h"
39
#include "news.h"
40
#include "prefs_common.h"
41
#include "prefs_account.h"
42
#include "procheader.h"
43
#include "account.h"
44
#include "progressdialog.h"
45
#include "statusbar.h"
46
#include "inputdialog.h"
47
#include "alertpanel.h"
48
#include "manage_window.h"
49
#include "utils.h"
50

    
51
#define SMTP_PORT        25
52
#if USE_SSL
53
#define SSMTP_PORT        465
54
#endif
55

    
56
typedef struct _SendProgressDialog        SendProgressDialog;
57

    
58
struct _SendProgressDialog
59
{
60
        ProgressDialog *dialog;
61
        Session *session;
62
        gboolean cancelled;
63
};
64

    
65
static gint send_message_local                (const gchar                *command,
66
                                         FILE                        *fp);
67
static gint send_message_smtp                (PrefsAccount                *ac_prefs,
68
                                         GSList                        *to_list,
69
                                         FILE                        *fp);
70

    
71
static gint send_recv_message                (Session                *session,
72
                                         const gchar                *msg,
73
                                         gpointer                 data);
74
static gint send_send_data_progressive        (Session                *session,
75
                                         guint                         cur_len,
76
                                         guint                         total_len,
77
                                         gpointer                 data);
78
static gint send_send_data_finished        (Session                *session,
79
                                         guint                         len,
80
                                         gpointer                 data);
81

    
82
static SendProgressDialog *send_progress_dialog_create(void);
83
static void send_progress_dialog_destroy(SendProgressDialog *dialog);
84

    
85
static void send_cancel_button_cb        (GtkWidget        *widget,
86
                                         gpointer         data);
87

    
88
static void send_put_error                (Session        *session);
89

    
90

    
91
gint send_message(const gchar *file, PrefsAccount *ac_prefs, GSList *to_list)
92
{
93
        FILE *fp;
94
        gint val;
95

    
96
        g_return_val_if_fail(file != NULL, -1);
97
        g_return_val_if_fail(ac_prefs != NULL, -1);
98
        g_return_val_if_fail(to_list != NULL, -1);
99

    
100
        if ((fp = fopen(file, "rb")) == NULL) {
101
                FILE_OP_ERROR(file, "fopen");
102
                return -1;
103
        }
104

    
105
        if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
106
                val = send_message_local(prefs_common.extsend_cmd, fp);
107
                fclose(fp);
108
                return val;
109
        }
110

    
111
        val = send_message_smtp(ac_prefs, to_list, fp);
112

    
113
        fclose(fp);
114
        return val;
115
}
116

    
117
enum
118
{
119
        Q_SENDER     = 0,
120
        Q_SMTPSERVER = 1,
121
        Q_RECIPIENTS = 2,
122
        Q_ACCOUNT_ID = 3
123
};
124

    
125
QueueInfo *send_get_queue_info(const gchar *file)
126
{
127
        static HeaderEntry qentry[] = {{"S:",   NULL, FALSE},
128
                                       {"SSV:", NULL, FALSE},
129
                                       {"R:",   NULL, FALSE},
130
                                       {"AID:", NULL, FALSE},
131
                                       {NULL,   NULL, FALSE}};
132
        FILE *fp;
133
        gchar buf[BUFFSIZE];
134
        gint hnum;
135
        QueueInfo *qinfo;
136

    
137
        g_return_val_if_fail(file != NULL, NULL);
138

    
139
        if ((fp = fopen(file, "rb")) == NULL) {
140
                FILE_OP_ERROR(file, "fopen");
141
                return NULL;
142
        }
143

    
144
        qinfo = g_new0(QueueInfo, 1);
145

    
146
        while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
147
               != -1) {
148
                gchar *p;
149

    
150
                p = buf + strlen(qentry[hnum].name);
151

    
152
                switch (hnum) {
153
                case Q_SENDER:
154
                        if (!qinfo->from)
155
                                qinfo->from = g_strdup(p);
156
                        break;
157
                case Q_SMTPSERVER:
158
                        if (!qinfo->server)
159
                                qinfo->server = g_strdup(p);
160
                        break;
161
                case Q_RECIPIENTS:
162
                        qinfo->to_list =
163
                                address_list_append(qinfo->to_list, p);
164
                        break;
165
                case Q_ACCOUNT_ID:
166
                        qinfo->ac = account_find_from_id(atoi(p));
167
                        break;
168
                default:
169
                        break;
170
                }
171
        }
172

    
173
        qinfo->fp = fp;
174

    
175
        if (((!qinfo->ac || (qinfo->ac && qinfo->ac->protocol != A_NNTP)) &&
176
             !qinfo->to_list) || !qinfo->from) {
177
                g_warning(_("Queued message header is broken.\n"));
178
                send_queue_info_free(qinfo);
179
                return NULL;
180
        }
181

    
182
        if (!qinfo->ac) {
183
                qinfo->ac = account_find_from_smtp_server(qinfo->from,
184
                                                          qinfo->server);
185
                if (!qinfo->ac) {
186
                        g_warning("Account not found. "
187
                                  "Using current account...\n");
188
                        qinfo->ac = cur_account;
189
                }
190
        }
191

    
192
        return qinfo;
193
}
194

    
195
void send_queue_info_free(QueueInfo *qinfo)
196
{
197
        if (qinfo == NULL) return;
198

    
199
        slist_free_strings(qinfo->to_list);
200
        g_slist_free(qinfo->to_list);
201
        g_free(qinfo->from);
202
        g_free(qinfo->server);
203
        if (qinfo->fp)
204
                fclose(qinfo->fp);
205
        g_free(qinfo);
206
}
207

    
208
gint send_message_queue(QueueInfo *qinfo)
209
{
210
        gint val = 0;
211
        glong fpos;
212
        PrefsAccount *mailac = NULL, *newsac = NULL;
213

    
214
        g_return_val_if_fail(qinfo != NULL, -1);
215

    
216
        if (prefs_common.use_extsend && prefs_common.extsend_cmd) {
217
                val = send_message_local(prefs_common.extsend_cmd, qinfo->fp);
218
        } else {
219
                if (qinfo->ac && qinfo->ac->protocol == A_NNTP) {
220
                        newsac = qinfo->ac;
221

    
222
                        /* search mail account */
223
                        mailac = account_find_from_address(qinfo->from);
224
                        if (!mailac) {
225
                                if (cur_account &&
226
                                    cur_account->protocol != A_NNTP)
227
                                        mailac = cur_account;
228
                                else {
229
                                        mailac = account_get_default();
230
                                        if (mailac->protocol == A_NNTP)
231
                                                mailac = NULL;
232
                                }
233
                        }
234
                } else
235
                        mailac = qinfo->ac;
236

    
237
                fpos = ftell(qinfo->fp);
238
                if (qinfo->to_list) {
239
                        if (mailac)
240
                                val = send_message_smtp(mailac, qinfo->to_list,
241
                                                        qinfo->fp);
242
                        else {
243
                                PrefsAccount tmp_ac;
244

    
245
                                g_warning("Account not found.\n");
246

    
247
                                memset(&tmp_ac, 0, sizeof(PrefsAccount));
248
                                tmp_ac.address = qinfo->from;
249
                                tmp_ac.smtp_server = qinfo->server;
250
                                tmp_ac.smtpport = SMTP_PORT;
251
                                val = send_message_smtp(&tmp_ac, qinfo->to_list,
252
                                                        qinfo->fp);
253
                        }
254
                }
255

    
256
                if (val == 0 && newsac) {
257
                        fseek(qinfo->fp, fpos, SEEK_SET);
258
                        val = news_post_stream(FOLDER(newsac->folder),
259
                                               qinfo->fp);
260
                        if (val < 0)
261
                                alertpanel_error(_("Error occurred while posting the message to %s ."),
262
                                                 newsac->nntp_server);
263
                }
264
        }
265

    
266
        return val;
267
}
268

    
269
static gint send_message_local(const gchar *command, FILE *fp)
270
{
271
        FILE *pipefp;
272
        gchar buf[BUFFSIZE];
273

    
274
        g_return_val_if_fail(command != NULL, -1);
275
        g_return_val_if_fail(fp != NULL, -1);
276

    
277
        pipefp = popen(command, "w");
278
        if (!pipefp) {
279
                g_warning("Can't execute external command: %s\n", command);
280
                return -1;
281
        }
282

    
283
        while (fgets(buf, sizeof(buf), fp) != NULL) {
284
                strretchomp(buf);
285
                if (buf[0] == '.' && buf[1] == '\0')
286
                        fputc('.', pipefp);
287
                fputs(buf, pipefp);
288
                fputc('\n', pipefp);
289
        }
290

    
291
        pclose(pipefp);
292

    
293
        return 0;
294
}
295

    
296
static gint send_message_smtp(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp)
297
{
298
        Session *session;
299
        SMTPSession *smtp_session;
300
        gushort port;
301
        SendProgressDialog *dialog;
302
        GtkCList *clist;
303
        const gchar *text[3];
304
        gchar buf[BUFFSIZE];
305
        gint ret = 0;
306

    
307
        g_return_val_if_fail(ac_prefs != NULL, -1);
308
        g_return_val_if_fail(ac_prefs->address != NULL, -1);
309
        g_return_val_if_fail(ac_prefs->smtp_server != NULL, -1);
310
        g_return_val_if_fail(to_list != NULL, -1);
311
        g_return_val_if_fail(fp != NULL, -1);
312

    
313
        session = smtp_session_new();
314
        smtp_session = SMTP_SESSION(session);
315

    
316
        smtp_session->hostname =
317
                ac_prefs->set_domain ? g_strdup(ac_prefs->domain) : NULL;
318

    
319
        if (ac_prefs->use_smtp_auth) {
320
                smtp_session->forced_auth_type = ac_prefs->smtp_auth_type;
321

    
322
                if (ac_prefs->smtp_userid) {
323
                        smtp_session->user = g_strdup(ac_prefs->smtp_userid);
324
                        if (ac_prefs->smtp_passwd)
325
                                smtp_session->pass =
326
                                        g_strdup(ac_prefs->smtp_passwd);
327
                        else if (ac_prefs->tmp_smtp_pass)
328
                                smtp_session->pass =
329
                                        g_strdup(ac_prefs->tmp_smtp_pass);
330
                        else {
331
                                smtp_session->pass =
332
                                        input_dialog_query_password
333
                                                (ac_prefs->smtp_server,
334
                                                 smtp_session->user);
335
                                if (!smtp_session->pass)
336
                                        smtp_session->pass = g_strdup("");
337
                                ac_prefs->tmp_smtp_pass =
338
                                        g_strdup(smtp_session->pass);
339
                        }
340
                } else {
341
                        smtp_session->user = g_strdup(ac_prefs->userid);
342
                        if (ac_prefs->passwd)
343
                                smtp_session->pass = g_strdup(ac_prefs->passwd);
344
                        else if (ac_prefs->tmp_pass)
345
                                smtp_session->pass =
346
                                        g_strdup(ac_prefs->tmp_pass);
347
                        else {
348
                                smtp_session->pass =
349
                                        input_dialog_query_password
350
                                                (ac_prefs->smtp_server,
351
                                                 smtp_session->user);
352
                                if (!smtp_session->pass)
353
                                        smtp_session->pass = g_strdup("");
354
                                ac_prefs->tmp_pass =
355
                                        g_strdup(smtp_session->pass);
356
                        }
357
                }
358
        } else {
359
                smtp_session->user = NULL;
360
                smtp_session->pass = NULL;
361
        }
362

    
363
        smtp_session->from = g_strdup(ac_prefs->address);
364
        smtp_session->to_list = to_list;
365
        smtp_session->cur_to = to_list;
366
        smtp_session->send_data = get_outgoing_rfc2822_str(fp);
367
        smtp_session->send_data_len = strlen(smtp_session->send_data);
368

    
369
#if USE_SSL
370
        port = ac_prefs->set_smtpport ? ac_prefs->smtpport :
371
                ac_prefs->ssl_smtp == SSL_TUNNEL ? SSMTP_PORT : SMTP_PORT;
372
        session->ssl_type = ac_prefs->ssl_smtp;
373
        if (ac_prefs->ssl_smtp != SSL_NONE)
374
                session->nonblocking = ac_prefs->use_nonblocking_ssl;
375
#else
376
        port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
377
#endif
378

    
379
        dialog = send_progress_dialog_create();
380
        dialog->session = session;
381

    
382
        text[0] = NULL;
383
        text[1] = ac_prefs->smtp_server;
384
        text[2] = _("Connecting");
385
        clist = GTK_CLIST(dialog->dialog->clist);
386
        gtk_clist_append(clist, (gchar **)text);
387

    
388
        g_snprintf(buf, sizeof(buf), _("Connecting to SMTP server: %s ..."),
389
                   ac_prefs->smtp_server);
390
        progress_dialog_set_label(dialog->dialog, buf);
391
        log_message("%s\n", buf);
392

    
393
        session_set_recv_message_notify(session, send_recv_message, dialog);
394
        session_set_send_data_progressive_notify
395
                (session, send_send_data_progressive, dialog);
396
        session_set_send_data_notify(session, send_send_data_finished, dialog);
397

    
398
        if (session_connect(session, ac_prefs->smtp_server, port) < 0) {
399
                session_destroy(session);
400
                send_progress_dialog_destroy(dialog);
401
                return -1;
402
        }
403

    
404
        debug_print("send_message_smtp(): begin event loop\n");
405

    
406
        while (session_is_connected(session) && dialog->cancelled == FALSE)
407
                gtk_main_iteration();
408

    
409
        if (SMTP_SESSION(session)->error_val == SM_AUTHFAIL) {
410
                if (ac_prefs->smtp_userid && ac_prefs->tmp_smtp_pass) {
411
                        g_free(ac_prefs->tmp_smtp_pass);
412
                        ac_prefs->tmp_smtp_pass = NULL;
413
                }
414
                ret = -1;
415
        } else if (session->state == SESSION_ERROR ||
416
                   session->state == SESSION_EOF ||
417
                   session->state == SESSION_TIMEOUT ||
418
                   SMTP_SESSION(session)->state == SMTP_ERROR ||
419
                   SMTP_SESSION(session)->error_val != SM_OK)
420
                ret = -1;
421
        else if (dialog->cancelled == TRUE)
422
                ret = -1;
423

    
424
        if (ret == -1) {
425
                manage_window_focus_in(dialog->dialog->window, NULL, NULL);
426
                send_put_error(session);
427
                manage_window_focus_out(dialog->dialog->window, NULL, NULL);
428
        }
429

    
430
        session_destroy(session);
431
        send_progress_dialog_destroy(dialog);
432

    
433
        return ret;
434
}
435

    
436
static gint send_recv_message(Session *session, const gchar *msg, gpointer data)
437
{
438
        gchar buf[BUFFSIZE];
439
        SMTPSession *smtp_session = SMTP_SESSION(session);
440
        SendProgressDialog *dialog = (SendProgressDialog *)data;
441
        gchar *state_str = NULL;
442

    
443
        g_return_val_if_fail(dialog != NULL, -1);
444

    
445
        switch (smtp_session->state) {
446
        case SMTP_READY:
447
        case SMTP_CONNECTED:
448
                return 0;
449
        case SMTP_HELO:
450
                g_snprintf(buf, sizeof(buf), _("Sending HELO..."));
451
                state_str = _("Authenticating");
452
                statusbar_print_all(_("Sending message..."));
453
                break;
454
        case SMTP_EHLO:
455
                g_snprintf(buf, sizeof(buf), _("Sending EHLO..."));
456
                state_str = _("Authenticating");
457
                statusbar_print_all(_("Sending message..."));
458
                break;
459
        case SMTP_AUTH:
460
                g_snprintf(buf, sizeof(buf), _("Authenticating..."));
461
                state_str = _("Authenticating");
462
                break;
463
        case SMTP_FROM:
464
                g_snprintf(buf, sizeof(buf), _("Sending MAIL FROM..."));
465
                state_str = _("Sending");
466
                break;
467
        case SMTP_RCPT:
468
                g_snprintf(buf, sizeof(buf), _("Sending RCPT TO..."));
469
                state_str = _("Sending");
470
                break;
471
        case SMTP_DATA:
472
        case SMTP_EOM:
473
                g_snprintf(buf, sizeof(buf), _("Sending DATA..."));
474
                state_str = _("Sending");
475
                break;
476
        case SMTP_QUIT:
477
                g_snprintf(buf, sizeof(buf), _("Quitting..."));
478
                state_str = _("Quitting");
479
                break;
480
        case SMTP_ERROR:
481
                g_warning("send: error: %s\n", msg);
482
                return 0;
483
        default:
484
                return 0;
485
        }
486

    
487
        progress_dialog_set_label(dialog->dialog, buf);
488
        gtk_clist_set_text(GTK_CLIST(dialog->dialog->clist), 0, 2, state_str);
489

    
490
        return 0;
491
}
492

    
493
static gint send_send_data_progressive(Session *session, guint cur_len,
494
                                       guint total_len, gpointer data)
495
{
496
        gchar buf[BUFFSIZE];
497
        SendProgressDialog *dialog = (SendProgressDialog *)data;
498

    
499
        g_return_val_if_fail(dialog != NULL, -1);
500

    
501
        if (SMTP_SESSION(session)->state != SMTP_SEND_DATA &&
502
            SMTP_SESSION(session)->state != SMTP_EOM)
503
                return 0;
504

    
505
        g_snprintf(buf, sizeof(buf), _("Sending message (%d / %d bytes)"),
506
                   cur_len, total_len);
507
        progress_dialog_set_label(dialog->dialog, buf);
508
        progress_dialog_set_percentage
509
                (dialog->dialog, (gfloat)cur_len / (gfloat)total_len);
510

    
511
        return 0;
512
}
513

    
514
static gint send_send_data_finished(Session *session, guint len, gpointer data)
515
{
516
        SendProgressDialog *dialog = (SendProgressDialog *)data;
517

    
518
        g_return_val_if_fail(dialog != NULL, -1);
519

    
520
        send_send_data_progressive(session, len, len, dialog);
521
        return 0;
522
}
523

    
524
static SendProgressDialog *send_progress_dialog_create(void)
525
{
526
        SendProgressDialog *dialog;
527
        ProgressDialog *progress;
528

    
529
        dialog = g_new0(SendProgressDialog, 1);
530

    
531
        progress = progress_dialog_create();
532
        gtk_window_set_title(GTK_WINDOW(progress->window),
533
                             _("Sending message"));
534
        g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
535
                         G_CALLBACK(send_cancel_button_cb), dialog);
536
        g_signal_connect(G_OBJECT(progress->window), "delete_event",
537
                         G_CALLBACK(gtk_true), NULL);
538
        gtk_window_set_modal(GTK_WINDOW(progress->window), TRUE);
539
        manage_window_set_transient(GTK_WINDOW(progress->window));
540

    
541
        progress_dialog_set_value(progress, 0.0);
542

    
543
        gtk_widget_show_now(progress->window);
544

    
545
        dialog->dialog = progress;
546

    
547
        return dialog;
548
}
549

    
550
static void send_progress_dialog_destroy(SendProgressDialog *dialog)
551
{
552
        g_return_if_fail(dialog != NULL);
553

    
554
        progress_dialog_destroy(dialog->dialog);
555
        g_free(dialog);
556
}
557

    
558
static void send_cancel_button_cb(GtkWidget *widget, gpointer data)
559
{
560
        SendProgressDialog *dialog = (SendProgressDialog *)data;
561

    
562
        dialog->cancelled = TRUE;
563
}
564

    
565
static void send_put_error(Session *session)
566
{
567
        gchar *msg;
568
        gchar *log_msg = NULL;
569
        gchar *err_msg = NULL;
570

    
571
        msg = SMTP_SESSION(session)->error_msg;
572

    
573
        switch (SMTP_SESSION(session)->error_val) {
574
        case SM_ERROR:
575
        case SM_UNRECOVERABLE:
576
                log_msg = _("Error occurred while sending the message.");
577
                if (msg)
578
                        err_msg = g_strdup_printf
579
                                (_("Error occurred while sending the message:\n%s"),
580
                                 msg);
581
                else
582
                        err_msg = g_strdup(log_msg);
583
                break;
584
        case SM_AUTHFAIL:
585
                log_msg = _("Authentication failed.");
586
                if (msg)
587
                        err_msg = g_strdup_printf
588
                                (_("Authentication failed:\n%s"), msg);
589
                else
590
                        err_msg = g_strdup(log_msg);
591
                break;
592
        default:
593
                switch (session->state) {
594
                case SESSION_ERROR:
595
                        log_msg =
596
                                _("Error occurred while sending the message.");
597
                        err_msg = g_strdup(log_msg);
598
                        break;
599
                case SESSION_EOF:
600
                        log_msg = _("Connection closed by the remote host.");
601
                        err_msg = g_strdup(log_msg);
602
                        break;
603
                case SESSION_TIMEOUT:
604
                        log_msg = _("Session timed out.");
605
                        err_msg = g_strdup(log_msg);
606
                        break;
607
                default:
608
                        break;
609
                }
610
                break;
611
        }
612

    
613
        if (log_msg)
614
                log_warning("%s\n", log_msg);
615
        if (err_msg) {
616
                alertpanel_error("%s", err_msg);
617
                g_free(err_msg);
618
        }
619
}
620