New address book.
[claws.git] / src / textview.c
index dd0488fa0e21f1015b225f105210d58255d531f1..1ab32234f2475dcaa336d937c1df32fc925443d7 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999,2000 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2001 Hiroyuki Yamamoto
  *
  * 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
@@ -47,8 +47,7 @@
 #include "html.h"
 #include "compose.h"
 #include "addressbook.h"
-#include "headers_display.h"
-#include "prefs_display_headers.h"
+#include "displayheader.h"
 
 #define FONT_LOAD(font, s) \
 { \
@@ -152,6 +151,8 @@ TextView *textview_create(void)
        /* create GtkText widgets for single-byte and multi-byte character */
        text_sb = gtk_text_new(NULL, NULL);
        text_mb = gtk_text_new(NULL, NULL);
+       GTK_TEXT(text_sb)->default_tab_width = 8;
+       GTK_TEXT(text_mb)->default_tab_width = 8;
        gtk_widget_show(text_sb);
        gtk_widget_show(text_mb);
        gtk_text_set_word_wrap(GTK_TEXT(text_sb), TRUE);
@@ -308,11 +309,13 @@ void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
 
        /* display attached RFC822 single text message */
        if (mimeinfo->parent && mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
+               if (headers) procheader_header_array_destroy(headers);
                if (!mimeinfo->sub || mimeinfo->sub->children) return;
                headers = textview_scan_header(textview, fp);
                mimeinfo = mimeinfo->sub;
        } else if (!mimeinfo->parent &&
                   mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
+               if (headers) procheader_header_array_destroy(headers);
                if (!mimeinfo->sub) return;
                headers = textview_scan_header(textview, fp);
                mimeinfo = mimeinfo->sub;
@@ -331,16 +334,8 @@ void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
        gtk_text_freeze(text);
 
        if (headers) {
-               gint i;
-
                textview_show_header(textview, headers);
-               for (i = 0; i < headers->len; i++) {
-                       Header *header = g_ptr_array_index(headers, i);
-                       g_free(header->body);
-                       g_free(header->name);
-                       g_free(header);
-               }
-               g_ptr_array_free(headers, TRUE);
+               procheader_header_array_destroy(headers);
        }
 
        tmpfp = procmime_decode_content(NULL, fp, mimeinfo);
@@ -358,6 +353,9 @@ void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
        gtk_text_thaw(text);
 }
 
+#define TEXT_INSERT(str) \
+       gtk_text_insert(text, textview->msgfont, NULL, NULL, str, -1)
+
 void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
 {
        GtkText *text;
@@ -370,26 +368,17 @@ void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
 
        gtk_text_freeze(text);
 
-       gtk_text_insert
-               (text, textview->msgfont, NULL, NULL,
-                _("To save this part, pop up the context menu with\n"), -1);
-       gtk_text_insert
-               (text, textview->msgfont, NULL, NULL,
-                _("right click and select `Save as...', or press `y' key.\n\n"), -1);
-
-       gtk_text_insert
-               (text, textview->msgfont, NULL, NULL,
-                _("To display this part as a text message, select\n"), -1);
-       gtk_text_insert
-               (text, textview->msgfont, NULL, NULL,
-                _("`Display as text', or press `t' key.\n\n"), -1);
-
-       gtk_text_insert
-               (text, textview->msgfont, NULL, NULL,
-                _("To open this part with external program, select `Open',\n"), -1);
-       gtk_text_insert
-               (text, textview->msgfont, NULL, NULL,
-                _("or double-click, or click the center button, or press `l' key."), -1);
+       TEXT_INSERT(_("To save this part, pop up the context menu with "));
+       TEXT_INSERT(_("right click and select `Save as...', "));
+       TEXT_INSERT(_("or press `y' key.\n\n"));
+
+       TEXT_INSERT(_("To display this part as a text message, select "));
+       TEXT_INSERT(_("`Display as text', or press `t' key.\n\n"));
+
+       TEXT_INSERT(_("To open this part with external program, select "));
+       TEXT_INSERT(_("`Open' or `Open with...', "));
+       TEXT_INSERT(_("or double-click, or click the center button, "));
+       TEXT_INSERT(_("or press `l' key."));
 
        gtk_text_thaw(text);
 }
@@ -408,24 +397,19 @@ void textview_show_signature_part(TextView *textview, MimeInfo *partinfo)
        gtk_text_freeze(text);
 
        if (partinfo->sigstatus_full == NULL) {
-               gtk_text_insert
-                       (text, textview->msgfont, NULL, NULL,
-                        _("This signature has not been checked yet.\n"), -1);
-               gtk_text_insert
-                       (text, textview->msgfont, NULL, NULL,
-                        _("To check it, pop up the context menu with\n"), -1);
-               gtk_text_insert
-                       (text, textview->msgfont, NULL, NULL,
-                        _("right click and select `Check signature'.\n"), -1);
+               TEXT_INSERT(_("This signature has not been checked yet.\n"));
+               TEXT_INSERT(_("To check it, pop up the context menu with\n"));
+               TEXT_INSERT(_("right click and select `Check signature'.\n"));
        } else {
-               gtk_text_insert(text, textview->msgfont, NULL, NULL,
-                               partinfo->sigstatus_full, -1);
+               TEXT_INSERT(partinfo->sigstatus_full);
        }
 
        gtk_text_thaw(text);
 }
 #endif /* USE_GPGME */
 
+#undef TEXT_INSERT
+
 static void textview_show_html(TextView *textview, FILE *fp,
                               CodeConverter *conv)
 {
@@ -493,6 +477,8 @@ static gchar *make_uri_string(const gchar *bp, const gchar *ep)
         !isspace(ch) && \
         !strchr("()<>\"", (ch)))
 
+#define IS_QUOTE(ch) ((ch) == '\'' || (ch) == '"')
+
 /* get_email_part() - retrieves an email address. Returns TRUE if succesful */
 static gboolean get_email_part(const gchar *start, const gchar *scanpos,
                               const gchar **bp, const gchar **ep)
@@ -500,8 +486,14 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
        /* more complex than the uri part because we need to scan back and forward starting from
         * the scan position. */
        gboolean result = FALSE;
-       const gchar *bp_;
-       const gchar *ep_;
+       const gchar *bp_ = NULL;
+       const gchar *ep_ = NULL;
+
+       /* the informative part of the email address (describing the name
+        * of the email address owner) may contain quoted parts. the
+        * closure stack stores the last encountered quotes. */
+       gchar closure_stack[128];
+       gchar *ptr = closure_stack;
 
        g_return_val_if_fail(start != NULL, FALSE);
        g_return_val_if_fail(scanpos != NULL, FALSE);
@@ -533,58 +525,60 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
                        result = TRUE;
                }
        }
-       
-       if (result) {
-               /* we now have at least the address. now see if this is <bracketed>; in this
-                * case we also scan for the informative part. we could make this a useful
-                * function because it tries to parse out an email address backwards. :) */
-               if ( ((bp_ - 1 > start) && *(bp_ - 1) == '<') &&  (*ep_ == '>')) {
-                       char    closure_stack[128];
-                       char   *ptr = closure_stack;
-#define FULL_STACK()   ((ptr - closure_stack) >= sizeof closure_stack) 
-#define IN_STACK()             (ptr > closure_stack)
+
+       if (!result) return FALSE;
+
+       /* skip if it's between quotes "'alfons@proteus.demon.nl'" <alfons@proteus.demon.nl> */
+       if (bp_ - 1 > start && IS_QUOTE(*(bp_ - 1)) && IS_QUOTE(*ep_)) 
+               return FALSE;
+
+       /* see if this is <bracketed>; in this case we also scan for the informative part. */
+       if (bp_ - 1 <= start || *(bp_ - 1) != '<' || *ep_ != '>')
+               return TRUE;
+
+#define FULL_STACK()   ((size_t) (ptr - closure_stack) >= sizeof closure_stack)
+#define IN_STACK()     (ptr > closure_stack)
 /* has underrun check */
-#define POP_STACK()            if(IN_STACK()) --ptr            
+#define POP_STACK()    if(IN_STACK()) --ptr
 /* has overrun check */
-#define PUSH_STACK(c)  if(!FULL_STACK()) *ptr++ = (c); else return TRUE 
+#define PUSH_STACK(c)  if(!FULL_STACK()) *ptr++ = (c); else return TRUE
 /* has underrun check */
 #define PEEK_STACK()   (IN_STACK() ? *(ptr - 1) : 0)
 
-                       ep_++;
-
-                       /* scan for the informative part */
-                       for (bp_ -= 2; bp_ >= start; bp_--) {
-                               /* if closure on the stack keep scanning */
-                               if (PEEK_STACK() == *bp_) {
-                                       POP_STACK();
-                               }                                       
-                               else {
-                                       if (*bp_ == '\'' || *bp_ == '"')  {
-                                               PUSH_STACK(*bp_);
-                                       }                                               
-                                       else {
-                                               if (!PEEK_STACK()) {
-                                                       /* if nothing in the closure stack, do the special conditions
-                                                        * the following if..else expression simply checks whether 
-                                                        * a token is acceptable. if not acceptable, the clause
-                                                        * should terminate the loop with a 'break' */
-                                                       if ( *bp_ == '-' 
-                                                       &&   (((bp_ - 1) >= start) && isalnum(*(bp_ - 1))) 
-                                                       &&   (((bp_ + 1) < ep_)    && isalnum(*(bp_ + 1))) ) {
-                                                               /* hyphens are allowed, but only in between alnums */
-                                                       }
-                                                       else if ( !ispunct(*bp_) ) {
-                                                               /* but anything not being a punctiation is ok */
-                                                       }
-                                                       else {
-                                                               break; /* anything else is rejected */
-                                                       }
-                                               }
-                                       }
-                               }
+       ep_++;
+
+       /* scan for the informative part. */
+       for (bp_ -= 2; bp_ >= start; bp_--) {
+               /* if closure on the stack keep scanning */
+               if (PEEK_STACK() == *bp_) {
+                       POP_STACK();
+                       continue;
+               }
+               if (*bp_ == '\'' || *bp_ == '"') {
+                       PUSH_STACK(*bp_);
+                       continue;
+               }
+
+               /* if nothing in the closure stack, do the special conditions
+                * the following if..else expression simply checks whether 
+                * a token is acceptable. if not acceptable, the clause
+                * should terminate the loop with a 'break' */
+               if (!PEEK_STACK()) {
+                       if (*bp_ == '-'
+                       && (((bp_ - 1) >= start) && isalnum(*(bp_ - 1)))
+                       && (((bp_ + 1) < ep_)    && isalnum(*(bp_ + 1)))) {
+                               /* hyphens are allowed, but only in
+                                  between alnums */
+                       } else if (!ispunct(*bp_)) {
+                               /* but anything not being a punctiation
+                                  is ok */
+                       } else {
+                               break; /* anything else is rejected */
                        }
-                       
-                       bp_++;
+               }
+       }
+
+       bp_++;
 
 #undef PEEK_STACK
 #undef PUSH_STACK
@@ -592,17 +586,17 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
 #undef IN_STACK
 #undef FULL_STACK
 
-                       /* scan forward (should start with an alnum) */
-                       for (; *bp_ != '<' && isspace(*bp_) && *bp_ != '"'; bp_++)
-                               ;
+       /* scan forward (should start with an alnum) */
+       for (; *bp_ != '<' && isspace(*bp_) && *bp_ != '"'; bp_++)
+               ;
+
+       *ep = ep_;
+       *bp = bp_;
 
-                       *ep = ep_;
-                       *bp = bp_;
-               }
-       }
        return result;
 }
 
+#undef IS_QUOTE
 #undef IS_RFC822_CHAR
 
 static gchar *make_email_string(const gchar *bp, const gchar *ep)
@@ -632,8 +626,8 @@ static gchar *make_email_string(const gchar *bp, const gchar *ep)
        }
 
 /* textview_make_clickable_parts() - colorizes clickable parts */
-static void textview_make_clickable_parts(TextView *textview, GtkText *text,
-                                         GdkFont  *textfont,
+static void textview_make_clickable_parts(TextView *textview,
+                                         GdkFont *font,
                                          GdkColor *fg_color,
                                          GdkColor *uri_color,
                                          const gchar *linebuf)
@@ -673,6 +667,8 @@ static void textview_make_clickable_parts(TextView *textview, GtkText *text,
                struct txtpos   *next;          /* next */
        } head = {NULL, NULL, 0,  NULL}, *last = &head;
 
+       GtkText *text = GTK_TEXT(textview->text);
+
        /* parse for clickable parts, and build a list of begin and end positions  */
        for (walk = linebuf, n = 0;;) {
                gint last_index = PARSE_ELEMS;
@@ -694,7 +690,7 @@ static void textview_make_clickable_parts(TextView *textview, GtkText *text,
                if (scanpos) {
                        /* check if URI can be parsed */
                        if (parser[last_index].parse(linebuf, scanpos, &bp, &ep)
-                           && (ep - bp - 1) > strlen(parser[last_index].needle)) {
+                           && (size_t) (ep - bp - 1) > strlen(parser[last_index].needle)) {
                                        ADD_TXT_POS(bp, ep, last_index);
                                        walk = ep;
                        } else
@@ -715,13 +711,14 @@ static void textview_make_clickable_parts(TextView *textview, GtkText *text,
 
                        uri = g_new(RemoteURI, 1);
                        if (last->bp - normal_text > 0)
-                               gtk_text_insert(text, textfont,
+                               gtk_text_insert(text, font,
                                                fg_color, NULL,
                                                normal_text,
                                                last->bp - normal_text);
-                       uri->uri = parser[last->pti].build_uri(last->bp, last->ep);
+                       uri->uri = parser[last->pti].build_uri(last->bp, 
+                                                              last->ep);
                        uri->start = gtk_text_get_point(text);
-                       gtk_text_insert(text, textfont, uri_color,
+                       gtk_text_insert(text, font, uri_color,
                                        NULL, last->bp, last->ep - last->bp);
                        uri->end = gtk_text_get_point(text);
                        textview->uri_list =
@@ -729,11 +726,10 @@ static void textview_make_clickable_parts(TextView *textview, GtkText *text,
                }
 
                if (*normal_text)
-                       gtk_text_insert(text, textfont, fg_color,
+                       gtk_text_insert(text, font, fg_color,
                                        NULL, normal_text, -1);
        } else
-               gtk_text_insert(text, textfont, fg_color, NULL,
-                               linebuf, -1);
+               gtk_text_insert(text, font, fg_color, NULL, linebuf, -1);
 }
 
 #undef ADD_TXT_POS
@@ -792,12 +788,11 @@ static void textview_write_line(TextView *textview, const gchar *str,
                gtk_text_insert(text, spacingfont, NULL, NULL, " ", 1);
 
        if (prefs_common.enable_color)
-               textview_make_clickable_parts(textview, text,
-                                                 textview->msgfont,
+               textview_make_clickable_parts(textview, textview->msgfont,
                                              fg_color, &uri_color, buf);
        else
-               gtk_text_insert(text, textview->msgfont, fg_color, NULL,
-                               buf, -1);
+               textview_make_clickable_parts(textview, textview->msgfont,
+                                             fg_color, NULL, buf);
 }
 
 void textview_clear(TextView *textview)
@@ -841,11 +836,11 @@ void textview_set_font(TextView *textview, const gchar *codeset)
           correctly, so it must be single-byte mode. */
        if (MB_CUR_MAX > 1) {
                if (codeset) {
-                       if (!strncasecmp(codeset, "ISO-8859-", 9) ||
-                           !strncasecmp(codeset, "KOI8-", 5)     ||
-                           !strncasecmp(codeset, "CP", 2)        ||
-                           !strncasecmp(codeset, "WINDOWS-", 8)  ||
-                           !strcasecmp(codeset, "BALTIC"))
+                       if (!g_strncasecmp(codeset, "ISO-8859-", 9) ||
+                           !g_strncasecmp(codeset, "KOI8-", 5)     ||
+                           !g_strncasecmp(codeset, "CP", 2)        ||
+                           !g_strncasecmp(codeset, "WINDOWS-", 8)  ||
+                           !g_strcasecmp(codeset, "BALTIC"))
                                use_fontset = FALSE;
                }
        } else
@@ -933,47 +928,13 @@ enum
        H_ORGANIZATION  = 11,
 };
 
-static gboolean hdrequal(char * hdr1, char * hdr2)
-{
-       int len1;
-       int len2;
-
-       len1 = strlen(hdr1);
-       len2 = strlen(hdr2);
-       if (hdr1[len1 - 1] == ':')
-               len1--;
-       if (hdr2[len2 - 1] == ':')
-               len2--;
-       if (len1 != len2)
-               return 0;
-       return (strncasecmp(hdr1, hdr2, len1) == 0);
-}
-
 static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
 {
-       /*
-       static HeaderEntry hentry[] = {{"Date:",         NULL, FALSE},
-                                      {"From:",         NULL, TRUE},
-                                      {"To:",           NULL, FALSE},
-                                      {"Newsgroups:",   NULL, FALSE},
-                                      {"Subject:",      NULL, TRUE},
-                                      {"Cc:",           NULL, FALSE},
-                                      {"Reply-To:",     NULL, FALSE},
-                                      {"Followup-To:",  NULL, FALSE},
-                                      {"X-Mailer:",     NULL, TRUE},
-                                      {"X-Newsreader:", NULL, TRUE},
-                                      {"User-Agent:",   NULL, TRUE},
-                                      {"Organization:", NULL, TRUE},
-                                      {NULL,            NULL, FALSE}};
-       */
-       gchar buf[BUFFSIZE], tmp[BUFFSIZE];
-       gint hnum;
-       HeaderEntry *hp;
-       GPtrArray *headers;
-       GSList * l;
-
-       int i;
-       GPtrArray *sorted_headers;
+       gchar buf[BUFFSIZE];
+       GPtrArray *headers, *sorted_headers;
+       GSList *disphdr_list;
+       Header *header;
+       gint i;
 
        g_return_val_if_fail(fp != NULL, NULL);
 
@@ -983,75 +944,40 @@ static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
                return NULL;
        }
 
-       headers = g_ptr_array_new();
+       headers = procheader_get_header_array_asis(fp);
 
-       /*
-       while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
-              != -1) {
-               Header *header;
+       sorted_headers = g_ptr_array_new();
 
-               hp = hentry + hnum;
+       for (disphdr_list = prefs_common.disphdr_list; disphdr_list != NULL;
+            disphdr_list = disphdr_list->next) {
+               DisplayHeaderProp *dp =
+                       (DisplayHeaderProp *)disphdr_list->data;
 
-               header = g_new(Header, 1);
-               header->name = g_strndup(buf, strlen(hp->name));
-               conv_unmime_header(tmp, sizeof(tmp), buf + strlen(hp->name),
-                                  NULL);
-               header->body = g_strdup(tmp);
+               for (i = 0; i < headers->len; i++) {
+                       header = g_ptr_array_index(headers, i);
 
-               g_ptr_array_add(headers, header);
-       }
-       */
-       //      while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
-       while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
-               gchar * p;
-               Header *header;
-
-               if (*buf == ':') continue;
-               for (p = buf; *p && *p != ' '; p++) {
-                       if (*p == ':') {
-                               header = g_new(Header, 1);
-                               header->name = g_strndup(buf, p - buf + 1);
-                               p++;
-                               /* while (*p == ' ' || *p == '\t') p++; */
-                               conv_unmime_header(tmp, sizeof(tmp), p, NULL);
-                               header->body = g_strdup(tmp);
-
-                               g_ptr_array_add(headers, header);
-                               break;
+                       if (procheader_headername_equal(header->name,
+                                                       dp->name)) {
+                               if (dp->hidden)
+                                       procheader_header_free(header);
+                               else
+                                       g_ptr_array_add(sorted_headers, header);
+
+                               g_ptr_array_remove_index(headers, i);
+                               i--;
                        }
                }
        }
 
-       sorted_headers = g_ptr_array_new();
-       for(l = prefs_display_headers ; l != NULL ; l = g_slist_next(l)) {
-               HeaderDisplayProp * dp = (HeaderDisplayProp *) l->data;
-               for(i = 0 ; i < headers->len ; i++) {
-                       Header * header = g_ptr_array_index(headers, i);
-                       if (hdrequal(header->name, dp->name)) {
-                               if (dp->hidden) {
-                                       g_ptr_array_remove_index(headers, i);
-                                       g_free(header->body);
-                                       g_free(header->name);
-                                       g_free(header);
-                                       i--;
-                               }
-                               else {
-                                       g_ptr_array_add(sorted_headers,
-                                                       header);
-                                       g_ptr_array_remove_index(headers, i);
-                                       i--;
-                               }
-                       }
+       if (prefs_common.show_other_header) {
+               for (i = 0; i < headers->len; i++) {
+                       header = g_ptr_array_index(headers, i);
+                       g_ptr_array_add(sorted_headers, header);
                }
        }
 
-       for(i = 0 ; i < headers->len ; i++) {
-               Header * header = g_ptr_array_index(headers, i);
-               g_ptr_array_add(sorted_headers, header);
-       }
+       g_ptr_array_free(headers, FALSE);
 
-       g_ptr_array_free(headers, TRUE);
-       
        return sorted_headers;
 }
 
@@ -1071,25 +997,32 @@ static void textview_show_header(TextView *textview, GPtrArray *headers)
 
                gtk_text_insert(text, textview->boldfont, NULL, NULL,
                                header->name, -1);
+               if (header->name[strlen(header->name) - 1] != ' ')
+                       gtk_text_insert(text, textview->boldfont,
+                                       NULL, NULL, " ", 1);
+
+               if (procheader_headername_equal(header->name, "Subject") ||
+                   procheader_headername_equal(header->name, "From")    ||
+                   procheader_headername_equal(header->name, "To")      ||
+                   procheader_headername_equal(header->name, "Cc"))
+                       unfold_line(header->body);
+
                if (prefs_common.enable_color &&
-                   (strncmp(header->name, "X-Mailer", 8) == 0 ||
-                    strncmp(header->name, "X-Newsreader", 12) == 0) &&
+                   (procheader_headername_equal(header->name, "X-Mailer") ||
+                    procheader_headername_equal(header->name,
+                                                "X-Newsreader")) &&
                    strstr(header->body, "Sylpheed") != NULL)
                        gtk_text_insert(text, NULL, &emphasis_color, NULL,
                                        header->body, -1);
-               else {
-                       if (prefs_common.enable_color) {
-                               textview_make_clickable_parts(textview, text,
-                                                                                         NULL,
-                                                                                         NULL, 
-                                                                                         &uri_color, 
-                                                                                         header->body);
-                       }                                                                                         
-                       else {                                                                                    
-                               gtk_text_insert(text, NULL, NULL, NULL,
-                                               header->body, -1);
-                       }                                               
-               }                                       
+               else if (prefs_common.enable_color) {
+                       textview_make_clickable_parts(textview,
+                                                     NULL, NULL, &uri_color,
+                                                     header->body);
+               } else {
+                       textview_make_clickable_parts(textview,
+                                                     NULL, NULL, NULL,
+                                                     header->body);
+               }
                gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
        }
 
@@ -1317,9 +1250,8 @@ static void textview_button_pressed(GtkWidget *widget, GdkEventButton *event,
                                    TextView *textview)
 {
        if (event &&
-           ((event->button == 1 && event->type == GDK_2BUTTON_PRESS) 
-               || event->button == 2
-               || event->button == 3)) {
+           ((event->button == 1 && event->type == GDK_2BUTTON_PRESS)
+            || event->button == 2 || event->button == 3)) {
                GSList *cur;
                guint current_pos;
 
@@ -1330,14 +1262,28 @@ static void textview_button_pressed(GtkWidget *widget, GdkEventButton *event,
 
                        if (current_pos >= uri->start &&
                            current_pos <  uri->end) {
-                               if (!strncasecmp(uri->uri, "mailto:", 7)) {
-                                       compose_new_with_recipient
-                                               (NULL, uri->uri + 7);
-                               }                                               
-                               else
+                               if (!g_strncasecmp(uri->uri, "mailto:", 7)) {
+                                       if (event->button == 3) {
+                                               gchar *fromname, *fromaddress;
+                                               /* extract url */
+                                               fromaddress = g_strdup(uri->uri + 7);
+                                               /* Hiroyuki: please put this function in utils.c! */
+                                               fromname = procheader_get_fromname(fromaddress);
+                                               extract_address(fromaddress);
+                                               g_message("adding from textview %s <%s>", fromname, fromaddress);
+                                               // Add to address book - Match
+                                               addressbook_add_contact( fromname, fromaddress, NULL );
+                                               g_free(fromaddress);
+                                               g_free(fromname);
+                                       } else {
+                                               compose_new_with_recipient
+                                                       (NULL, uri->uri + 7);
+                                       }
+                               } else {
                                        open_uri(uri->uri,
                                                 prefs_common.uri_cmd);
-                       }                               
+                               }
+                       }
                }
        }
 }