make return receipts work again
[claws.git] / src / messageview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 #include "defs.h"
21
22 #include <glib.h>
23 #include <gdk/gdkkeysyms.h>
24 #include <gtk/gtkvbox.h>
25 #include <gtk/gtkcontainer.h>
26 #include <gtk/gtkeditable.h>
27 #include <gtk/gtkwindow.h>
28 #include <gtk/gtktext.h>
29 #include <stdio.h>
30 #include <ctype.h>
31 #include <string.h>
32
33 #include "intl.h"
34 #include "main.h"
35 #include "messageview.h"
36 #include "headerview.h"
37 #include "textview.h"
38 #include "imageview.h"
39 #include "mimeview.h"
40 #include "procmsg.h"
41 #include "procheader.h"
42 #include "procmime.h"
43 #include "prefs_common.h"
44 #include "gtkutils.h"
45 #include "utils.h"
46 #include "rfc2015.h"
47 #include "account.h"
48 #include "alertpanel.h"
49 #include "send.h"
50 #include "pgptext.h"
51 #include "menu.h"
52
53 static void messageview_change_view_type(MessageView    *messageview,
54                                          MessageType     type);
55 static void messageview_destroy_cb      (GtkWidget      *widget,
56                                          MessageView    *messageview);
57 static void messageview_size_allocate_cb(GtkWidget      *widget,
58                                          GtkAllocation  *allocation);
59 static void key_pressed                 (GtkWidget      *widget,
60                                          GdkEventKey    *event,
61                                          MessageView    *messageview);
62
63 static void return_receipt_show         (NoticeView     *noticeview, 
64                                          MsgInfo        *msginfo);      
65 static void return_receipt_send_clicked (NoticeView     *noticeview, 
66                                          MsgInfo        *msginfo);
67
68 MessageView *messageview_create(void)
69 {
70         MessageView *messageview;
71         GtkWidget *vbox;
72         HeaderView *headerview;
73         TextView *textview;
74         ImageView *imageview;
75         MimeView *mimeview;
76         NoticeView *noticeview;
77
78         debug_print(_("Creating message view...\n"));
79         messageview = g_new0(MessageView, 1);
80
81         messageview->type = MVIEW_TEXT;
82
83         headerview = headerview_create();
84
85         noticeview = noticeview_create();
86
87         textview = textview_create();
88         textview->messageview = messageview;
89
90         imageview = imageview_create();
91         imageview->messageview = messageview;
92
93         mimeview = mimeview_create();
94         mimeview->textview = textview_create();
95         mimeview->textview->messageview = messageview;
96         mimeview->imageview = imageview;
97         mimeview->messageview = messageview;
98
99         vbox = gtk_vbox_new(FALSE, 0);
100         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(headerview),
101                            FALSE, FALSE, 0);
102         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(noticeview),
103                            FALSE, FALSE, 0);
104         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(textview),
105                            TRUE, TRUE, 0);
106
107         /* to remove without destroyed */
108         gtk_widget_ref(GTK_WIDGET_PTR(textview));
109         gtk_widget_ref(GTK_WIDGET_PTR(imageview));
110         gtk_widget_ref(GTK_WIDGET_PTR(mimeview));
111         gtk_widget_ref(GTK_WIDGET_PTR(mimeview->textview));
112
113         messageview->vbox       = vbox;
114         messageview->new_window = FALSE;
115         messageview->window     = NULL;
116         messageview->headerview = headerview;
117         messageview->textview   = textview;
118         messageview->imageview  = imageview;
119         messageview->mimeview   = mimeview;
120         messageview->noticeview = noticeview;
121
122         return messageview;
123 }
124
125 MessageView *messageview_create_with_new_window(void)
126 {
127         GtkWidget *window;
128         MessageView *msgview;
129
130         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
131         gtk_window_set_title(GTK_WINDOW(window), _("Sylpheed - Message View"));
132         gtk_window_set_wmclass(GTK_WINDOW(window), "message_view", "Sylpheed");
133         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
134         gtk_widget_set_usize(window, prefs_common.msgwin_width,
135                              prefs_common.msgwin_height);
136
137         msgview = messageview_create();
138
139         gtk_signal_connect(GTK_OBJECT(window), "size_allocate",
140                            GTK_SIGNAL_FUNC(messageview_size_allocate_cb),
141                            msgview);
142         gtk_signal_connect(GTK_OBJECT(window), "destroy",
143                            GTK_SIGNAL_FUNC(messageview_destroy_cb), msgview);
144         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
145                            GTK_SIGNAL_FUNC(key_pressed), msgview);
146
147         gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET_PTR(msgview));
148         gtk_widget_grab_focus(msgview->textview->text);
149         gtk_widget_show_all(window);
150
151         msgview->new_window = TRUE;
152         msgview->window = window;
153         msgview->visible = TRUE;
154
155         messageview_init(msgview);
156
157         return msgview;
158 }
159
160 void messageview_init(MessageView *messageview)
161 {
162         headerview_init(messageview->headerview);
163         textview_init(messageview->textview);
164         imageview_init(messageview->imageview);
165         mimeview_init(messageview->mimeview);
166         /*messageview_set_font(messageview);*/
167
168         noticeview_hide(messageview->noticeview);
169 }
170
171 static void notification_convert_header(gchar *dest, gint len, 
172                                         const gchar *src_,
173                                         gint header_len)
174 {
175         char *src;
176
177         g_return_if_fail(src_ != NULL);
178         g_return_if_fail(dest != NULL);
179
180         if (len < 1) return;
181
182         Xstrndup_a(src, src_, len, return);
183
184         remove_return(src);
185
186         if (is_ascii_str(src)) {
187                 strncpy2(dest, src, len);
188                 dest[len - 1] = '\0';
189                 return;
190         } else
191                 conv_encode_header(dest, len, src, header_len);
192 }
193
194 static gint disposition_notification_queue(PrefsAccount * account,
195                                            gchar * to, const gchar *file)
196 {
197         FolderItem *queue;
198         gchar *tmp;
199         FILE *fp, *src_fp;
200         gchar buf[BUFFSIZE];
201         gint num;
202
203         debug_print(_("queueing message...\n"));
204         g_return_val_if_fail(account != NULL, -1);
205
206         tmp = g_strdup_printf("%s%cqueue.%d", g_get_tmp_dir(),
207                               G_DIR_SEPARATOR, (gint)file);
208         if ((fp = fopen(tmp, "wb")) == NULL) {
209                 FILE_OP_ERROR(tmp, "fopen");
210                 g_free(tmp);
211                 return -1;
212         }
213         if ((src_fp = fopen(file, "rb")) == NULL) {
214                 FILE_OP_ERROR(file, "fopen");
215                 fclose(fp);
216                 unlink(tmp);
217                 g_free(tmp);
218                 return -1;
219         }
220         if (change_file_mode_rw(fp, tmp) < 0) {
221                 FILE_OP_ERROR(tmp, "chmod");
222                 g_warning(_("can't change file mode\n"));
223         }
224
225         /* queueing variables */
226         fprintf(fp, "AF:\n");
227         fprintf(fp, "NF:0\n");
228         fprintf(fp, "PS:10\n");
229         fprintf(fp, "SRH:1\n");
230         fprintf(fp, "SFN:\n");
231         fprintf(fp, "DSR:\n");
232         fprintf(fp, "MID:\n");
233         fprintf(fp, "CFG:\n");
234         fprintf(fp, "PT:0\n");
235         fprintf(fp, "S:%s\n", account->address);
236         fprintf(fp, "RQ:\n");
237         if (account->smtp_server)
238                 fprintf(fp, "SSV:%s\n", account->smtp_server);
239         else
240                 fprintf(fp, "SSV:\n");
241         if (account->nntp_server)
242                 fprintf(fp, "NSV:%s\n", account->nntp_server);
243         else
244                 fprintf(fp, "NSV:\n");
245         fprintf(fp, "SSH:\n");
246         fprintf(fp, "R:<%s>", to);
247         fprintf(fp, "\n");
248         fprintf(fp, "\n");
249
250         while (fgets(buf, sizeof(buf), src_fp) != NULL) {
251                 if (fputs(buf, fp) == EOF) {
252                         FILE_OP_ERROR(tmp, "fputs");
253                         fclose(fp);
254                         fclose(src_fp);
255                         unlink(tmp);
256                         g_free(tmp);
257                         return -1;
258                 }
259         }
260
261         fclose(src_fp);
262         if (fclose(fp) == EOF) {
263                 FILE_OP_ERROR(tmp, "fclose");
264                 unlink(tmp);
265                 g_free(tmp);
266                 return -1;
267         }
268
269         queue = folder_get_default_queue();
270         if ((num = folder_item_add_msg(queue, tmp, TRUE)) < 0) {
271                 g_warning(_("can't queue the message\n"));
272                 unlink(tmp);
273                 g_free(tmp);
274                 return -1;
275         }
276         g_free(tmp);
277
278         folderview_update_item(queue, TRUE);
279
280         return 0;
281 }
282
283 static gint disposition_notification_send(MsgInfo * msginfo)
284 {
285         gchar buf[BUFFSIZE];
286         gchar tmp[MAXPATHLEN + 1];
287         FILE *fp;
288         GSList * to_list;
289         gint ok;
290         gchar * to;
291
292         if ((!msginfo->returnreceiptto) && 
293             (!msginfo->dispositionnotificationto))
294                 return -1;
295
296         procmsg_msginfo_unset_flags(msginfo, MSG_RETRCPT_PENDING, 0);
297
298         /* write to temporary file */
299         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
300                    get_rc_dir(), G_DIR_SEPARATOR, (gint)msginfo);
301
302         if ((fp = fopen(tmp, "wb")) == NULL) {
303                 FILE_OP_ERROR(tmp, "fopen");
304                 return -1;
305         }
306
307         /* chmod for security */
308         if (change_file_mode_rw(fp, tmp) < 0) {
309                 FILE_OP_ERROR(tmp, "chmod");
310                 g_warning(_("can't change file mode\n"));
311         }
312
313         /* Date */
314         get_rfc822_date(buf, sizeof(buf));
315         fprintf(fp, "Date: %s\n", buf);
316
317         /* From */
318         if (cur_account->name && *cur_account->name) {
319                 notification_convert_header
320                         (buf, sizeof(buf), cur_account->name,
321                          strlen("From: "));
322                 fprintf(fp, "From: %s <%s>\n", buf, cur_account->address);
323         } else
324                 fprintf(fp, "From: %s\n", cur_account->address);
325
326         /* To */
327         if (msginfo->dispositionnotificationto)
328                 to = msginfo->dispositionnotificationto;
329         else
330                 to = msginfo->returnreceiptto;
331         fprintf(fp, "To: %s\n", to);
332
333         /* Subject */
334         notification_convert_header(buf, sizeof(buf), msginfo->subject,
335                                     strlen("Subject: "));
336         fprintf(fp, "Subject: Disposition notification: %s\n", buf);
337         debug_print("HEADER: Subject Disposition notification: %s\n", buf);
338
339         if (fclose(fp) == EOF) {
340                 FILE_OP_ERROR(tmp, "fclose");
341                 unlink(tmp);
342                 return -1;
343         }
344
345         to_list = address_list_append(NULL, msginfo->dispositionnotificationto);
346         ok = send_message(tmp, cur_account, to_list);
347         
348         if (ok < 0) {
349                 if (prefs_common.queue_msg) {
350                         AlertValue val;
351                         
352                         val = alertpanel
353                                 (_("Queueing"),
354                                  _("Error occurred while sending the notification.\n"
355                                    "Put this notification into queue folder?"),
356                                  _("OK"), _("Cancel"), NULL);
357                         if (G_ALERTDEFAULT == val) {
358                                 ok = disposition_notification_queue(cur_account, to, tmp);
359                                 if (ok < 0)
360                                         alertpanel_error(_("Can't queue the notification."));
361                         }
362                 } else
363                         alertpanel_error(_("Error occurred while sending the notification."));
364         }
365
366         if (unlink(tmp) < 0) FILE_OP_ERROR(tmp, "unlink");
367
368         return ok;
369 }
370
371 void messageview_show(MessageView *messageview, MsgInfo *msginfo,
372                       gboolean all_headers)
373 {
374         FILE *fp;
375         gchar *file;
376         MimeInfo *mimeinfo;
377         MsgInfo *tmpmsginfo;
378
379         g_return_if_fail(msginfo != NULL);
380
381 #if USE_GPGME
382         if ((fp = procmsg_open_message_decrypted(msginfo, &mimeinfo)) == NULL)
383                 return;
384 #else /* !USE_GPGME */
385         if ((fp = procmsg_open_message(msginfo)) == NULL) return;
386         mimeinfo = procmime_scan_mime_header(fp);
387 #endif /* USE_GPGME */
388         fclose(fp);
389         if (!mimeinfo) return;
390
391         file = procmsg_get_message_file_path(msginfo);
392         if (!file) {
393                 g_warning("can't get message file path.\n");
394                 procmime_mimeinfo_free(mimeinfo);
395                 return;
396         }
397
398         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
399
400         headerview_show(messageview->headerview, tmpmsginfo);
401         procmsg_msginfo_free(tmpmsginfo);
402
403         textview_set_all_headers(messageview->textview, all_headers);
404         textview_set_all_headers(messageview->mimeview->textview, all_headers);
405
406         if (mimeinfo->mime_type != MIME_TEXT &&
407             mimeinfo->mime_type != MIME_TEXT_HTML) {
408                 messageview_change_view_type(messageview, MVIEW_MIME);
409                 mimeview_show_message(messageview->mimeview, mimeinfo, file);
410         } else {
411                 messageview_change_view_type(messageview, MVIEW_TEXT);
412                 textview_show_message(messageview->textview, mimeinfo, file);
413                 procmime_mimeinfo_free(mimeinfo);
414         }
415
416         if (MSG_IS_RETRCPT_PENDING(msginfo->flags))
417                 return_receipt_show(messageview->noticeview, msginfo);
418         else 
419                 noticeview_hide(messageview->noticeview);
420
421         g_free(file);
422 }
423
424 static void messageview_change_view_type(MessageView *messageview,
425                                          MessageType type)
426 {
427         TextView *textview = messageview->textview;
428         MimeView *mimeview = messageview->mimeview;
429
430         if (messageview->type == type) return;
431
432         if (type == MVIEW_MIME) {
433                 gtkut_container_remove
434                         (GTK_CONTAINER(GTK_WIDGET_PTR(messageview)),
435                          GTK_WIDGET_PTR(textview));
436                 gtk_box_pack_start(GTK_BOX(messageview->vbox),
437                                    GTK_WIDGET_PTR(mimeview), TRUE, TRUE, 0);
438                 gtk_container_add(GTK_CONTAINER(mimeview->vbox),
439                                   GTK_WIDGET_PTR(textview));
440         } else if (type == MVIEW_TEXT) {
441                 gtkut_container_remove
442                         (GTK_CONTAINER(GTK_WIDGET_PTR(messageview)),
443                          GTK_WIDGET_PTR(mimeview));
444
445                 if (mimeview->vbox == GTK_WIDGET_PTR(textview)->parent)
446                         gtkut_container_remove(GTK_CONTAINER(mimeview->vbox),
447                                                GTK_WIDGET_PTR(textview));
448
449                 gtk_box_pack_start(GTK_BOX(messageview->vbox),
450                                    GTK_WIDGET_PTR(textview), TRUE, TRUE, 0);
451         } else
452                 return;
453
454         messageview->type = type;
455 }
456
457 void messageview_clear(MessageView *messageview)
458 {
459         messageview_change_view_type(messageview, MVIEW_TEXT);
460         headerview_clear(messageview->headerview);
461         textview_clear(messageview->textview);
462         imageview_clear(messageview->imageview);
463 }
464
465 void messageview_destroy(MessageView *messageview)
466 {
467         GtkWidget *textview  = GTK_WIDGET_PTR(messageview->textview);
468         GtkWidget *imageview = GTK_WIDGET_PTR(messageview->imageview);
469         GtkWidget *mimeview  = GTK_WIDGET_PTR(messageview->mimeview);
470
471         headerview_destroy(messageview->headerview);
472         textview_destroy(messageview->textview);
473         imageview_destroy(messageview->imageview);
474         mimeview_destroy(messageview->mimeview);
475
476         g_free(messageview);
477
478         gtk_widget_unref(textview);
479         gtk_widget_unref(imageview);
480         gtk_widget_unref(mimeview);
481 }
482
483 void messageview_quote_color_set(void)
484 {
485 }
486
487 void messageview_set_font(MessageView *messageview)
488 {
489         textview_set_font(messageview->textview, NULL);
490 }
491
492 TextView *messageview_get_current_textview(MessageView *messageview)
493 {
494         TextView *text = NULL;
495
496         if (messageview->type == MVIEW_TEXT)
497                 text = messageview->textview;
498         else if (messageview->type == MVIEW_MIME) {
499                 if (gtk_notebook_get_current_page
500                         (GTK_NOTEBOOK(messageview->mimeview->notebook)) == 0)
501                         text = messageview->textview;
502                 else if (messageview->mimeview->type == MIMEVIEW_TEXT)
503                         text = messageview->mimeview->textview;
504         }
505
506         return text;
507 }
508
509 void messageview_copy_clipboard(MessageView *messageview)
510 {
511         TextView *text;
512
513         text = messageview_get_current_textview(messageview);
514         if (text)
515                 gtk_editable_copy_clipboard(GTK_EDITABLE(text->text));
516 }
517
518 void messageview_select_all(MessageView *messageview)
519 {
520         TextView *text;
521
522         text = messageview_get_current_textview(messageview);
523         if (text)
524                 gtk_editable_select_region(GTK_EDITABLE(text->text), 0, -1);
525 }
526
527 void messageview_set_position(MessageView *messageview, gint pos)
528 {
529         textview_set_position(messageview->textview, pos);
530 }
531
532 gboolean messageview_search_string(MessageView *messageview, const gchar *str,
533                                    gboolean case_sens)
534 {
535         return textview_search_string(messageview->textview, str, case_sens);
536         return FALSE;
537 }
538
539 gboolean messageview_search_string_backward(MessageView *messageview,
540                                             const gchar *str,
541                                             gboolean case_sens)
542 {
543         return textview_search_string_backward(messageview->textview,
544                                                str, case_sens);
545         return FALSE;
546 }
547
548 gboolean messageview_is_visible(MessageView *messageview)
549 {
550         return messageview->visible;
551 }
552
553 static void messageview_destroy_cb(GtkWidget *widget, MessageView *messageview)
554 {
555         messageview_destroy(messageview);
556 }
557
558 static void messageview_size_allocate_cb(GtkWidget *widget,
559                                          GtkAllocation *allocation)
560 {
561         g_return_if_fail(allocation != NULL);
562
563         prefs_common.msgwin_width  = allocation->width;
564         prefs_common.msgwin_height = allocation->height;
565 }
566
567 static void key_pressed(GtkWidget *widget, GdkEventKey *event,
568                         MessageView *messageview)
569 {
570         if (event && event->keyval == GDK_Escape && messageview->window)
571                 gtk_widget_destroy(messageview->window);
572 }
573
574 void messageview_toggle_view_real(MessageView *messageview)
575 {
576         MainWindow *mainwin = messageview->mainwin;
577         union CompositeWin *cwin = &mainwin->win;
578         GtkWidget *vpaned = NULL;
579         GtkWidget *container = NULL;
580         GtkItemFactory *ifactory =gtk_item_factory_from_widget(mainwin->menubar);
581         
582         switch (mainwin->type) {
583         case SEPARATE_NONE:
584                 vpaned = cwin->sep_none.vpaned;
585                 container = cwin->sep_none.hpaned;
586                 break;
587         case SEPARATE_FOLDER:
588                 vpaned = cwin->sep_folder.vpaned;
589                 container = mainwin->vbox_body;
590                 break;
591         case SEPARATE_MESSAGE:
592         case SEPARATE_BOTH:
593                 return;
594         }
595
596         if (vpaned->parent != NULL) {
597                 gtk_widget_ref(vpaned);
598                 gtkut_container_remove(GTK_CONTAINER(container), vpaned);
599                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), container);
600                 menu_set_sensitive(ifactory, "/View/Expand Summary View", FALSE);
601                 gtk_widget_grab_focus(GTK_WIDGET(messageview->textview->text));
602         } else {
603                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), vpaned);
604                 gtk_container_add(GTK_CONTAINER(container), vpaned);
605                 gtk_widget_unref(vpaned);
606                 menu_set_sensitive(ifactory, "/View/Expand Summary View", TRUE);
607                 gtk_widget_grab_focus(GTK_WIDGET(mainwin->summaryview->ctree));
608         }
609 }
610
611 static void return_receipt_show(NoticeView *noticeview, MsgInfo *msginfo)
612 {
613         noticeview_set_text(noticeview, _("This messages asks for a return receipt."));
614         noticeview_set_button_text(noticeview, _("Send receipt"));
615         noticeview_set_button_press_callback(noticeview,
616                                              GTK_SIGNAL_FUNC(return_receipt_send_clicked),
617                                              (gpointer) msginfo);
618         noticeview_show(noticeview);
619 }
620
621 static void return_receipt_send_clicked(NoticeView *noticeview, MsgInfo *msginfo)
622 {
623         if (disposition_notification_send(msginfo) >= 0) 
624                 noticeview_hide(noticeview);
625 }
626
627