New hooklist to collect avatar data from headers
[claws.git] / src / procheader.c
index 7520135113122287f70b62f8e2b07b06f53e3c59..4b7ff05ff7f41bce1256b8d24c2996bde46c19cb 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2009 Hiroyuki Yamamoto and the Claws Mail team
+ * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
  *
  * 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
@@ -19,6 +19,7 @@
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #include <glib.h>
@@ -37,7 +38,9 @@
 #include "procmsg.h"
 #include "codeconv.h"
 #include "prefs_common.h"
+#include "hooks.h"
 #include "utils.h"
+#include "defs.h"
 
 #define BUFFSIZE       8192
 
@@ -256,11 +259,36 @@ gboolean procheader_headername_equal(char * hdr1, char * hdr2)
   header->name = "From:"
   header->body = "dinh@enseirb.fr"
  */
+static gboolean header_is_addr_field(const gchar *hdr)
+{
+       static char *addr_headers[] = {
+                               "To:",
+                               "Cc:",
+                               "Bcc:",
+                               "From:",
+                               "Reply-To:",
+                               "Followup-To:",
+                               "Followup-and-Reply-To:",
+                               "Disposition-Notification-To:",
+                               "Return-Receipt-To:",
+                               NULL};
+       int i;
+
+       if (!hdr)
+               return FALSE;
+
+       for (i = 0; addr_headers[i] != NULL; i++)
+               if (!strcasecmp(hdr, addr_headers[i]))
+                       return FALSE;
+
+       return FALSE;
+}
 
 Header * procheader_parse_header(gchar * buf)
 {
-       gchar *p = buf;
+       gchar *p;
        Header * header;
+       gboolean addr_field = FALSE;
 
        if ((*buf == ':') || (*buf == ' '))
                return NULL;
@@ -269,9 +297,10 @@ Header * procheader_parse_header(gchar * buf)
                if ((*p == ':') || (*p == ' ')) {
                        header = g_new(Header, 1);
                        header->name = g_strndup(buf, p - buf + 1);
+                       addr_field = header_is_addr_field(header->name);
                        p++;
                        while (*p == ' ' || *p == '\t') p++;
-                        header->body = conv_unmime_header(p, NULL);
+                       header->body = conv_unmime_header(p, NULL, addr_field);
                        return header;
                }
        }
@@ -434,6 +463,26 @@ MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
        return parse_stream(fp, FALSE, flags, full, decrypted);
 }
 
+static gboolean avatar_from_some_face(gpointer source, gpointer userdata)
+{
+       AvatarCaptureData *acd = (AvatarCaptureData *)source;
+       
+       if (*(acd->content) == '\0') /* won't be null, but may be empty */
+               return FALSE;
+
+       if (!strcmp(acd->header, hentry_full[H_FACE].name)) {
+               debug_print("avatar_from_some_face: found 'Face' header\n");
+               procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_FACE, acd->content);
+       }
+#if HAVE_LIBCOMPFACE
+       else if (!strcmp(acd->header, hentry_full[H_X_FACE].name)) {
+               debug_print("avatar_from_some_face: found 'X-Face' header\n");
+               procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_XFACE, acd->content);
+       }
+#endif
+       return FALSE;
+}
+
 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                             gboolean full, gboolean decrypted)
 {
@@ -444,6 +493,7 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
        HeaderEntry *hentry;
        gint hnum;
        void *orig_data = data;
+       guint hook_id = -1;
 
        get_one_field_func get_one_field =
                isstring ? (get_one_field_func)string_get_one_field
@@ -481,6 +531,10 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
        
        msginfo->inreplyto = NULL;
 
+       if (prefs_common.enable_avatars | AVATARS_ENABLE_CAPTURE) {
+               hook_id = hooks_register_hook(AVATAR_HEADER_UPDATE_HOOKLIST, avatar_from_some_face, NULL);
+       }
+
        while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
               != -1) {
                hp = buf + strlen(hentry[hnum].name);
@@ -510,13 +564,13 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                        break;
                case H_FROM:
                        if (msginfo->from) break;
-                        msginfo->from = conv_unmime_header(hp, NULL);
+                       msginfo->from = conv_unmime_header(hp, NULL, TRUE);
                        msginfo->fromname = procheader_get_fromname(msginfo->from);
                        remove_return(msginfo->from);
                        remove_return(msginfo->fromname);
                        break;
                case H_TO:
-                        tmp = conv_unmime_header(hp, NULL);
+                       tmp = conv_unmime_header(hp, NULL, TRUE);
                        remove_return(tmp);
                        if (msginfo->to) {
                                p = msginfo->to;
@@ -528,7 +582,7 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                         g_free(tmp);                                
                        break;
                case H_CC:
-                        tmp = conv_unmime_header(hp, NULL);
+                       tmp = conv_unmime_header(hp, NULL, TRUE);
                        remove_return(tmp);
                        if (msginfo->cc) {
                                p = msginfo->cc;
@@ -550,7 +604,7 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                        break;
                case H_SUBJECT:
                        if (msginfo->subject) break;
-                       msginfo->subject = conv_unmime_header(hp, NULL);
+                       msginfo->subject = conv_unmime_header(hp, NULL, FALSE);
                        unfold_line(msginfo->subject);
                        break;
                case H_MSG_ID:
@@ -587,18 +641,6 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                        MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
                        break;
 #endif                 
-               case H_FACE:
-                       if (!msginfo->extradata)
-                               msginfo->extradata = g_new0(MsgInfoExtraData, 1);
-                       if (msginfo->extradata->face) break;
-                       msginfo->extradata->face = g_strdup(hp);
-                       break;
-               case H_X_FACE:
-                       if (!msginfo->extradata)
-                               msginfo->extradata = g_new0(MsgInfoExtraData, 1);
-                       if (msginfo->extradata->xface) break;
-                       msginfo->extradata->xface = g_strdup(hp);
-                       break;
                case H_DISPOSITION_NOTIFICATION_TO:
                        if (!msginfo->extradata)
                                msginfo->extradata = g_new0(MsgInfoExtraData, 1);
@@ -707,12 +749,28 @@ static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
                default:
                        break;
                }
+               /* to avoid performance penalty hooklist is invoked only for
+                  headers known to be able to generate avatars */
+               if (hnum == H_FROM || hnum == H_X_FACE || hnum == H_FACE) {
+                       AvatarCaptureData *acd = g_new0(AvatarCaptureData, 1);
+                       /* no extra memory is wasted, hooks are expected to
+                          take care of copying members when needed */
+                       acd->msginfo = msginfo;
+                       acd->header  = hentry_full[hnum].name;
+                       acd->content = hp;
+                       hooks_invoke(AVATAR_HEADER_UPDATE_HOOKLIST, (gpointer)acd);
+                       g_free(acd);
+               }
        }
 
        if (!msginfo->inreplyto && msginfo->references)
                msginfo->inreplyto =
                        g_strdup((gchar *)msginfo->references->data);
 
+       if (hook_id != -1) {
+               hooks_unregister_hook(AVATAR_HEADER_UPDATE_HOOKLIST, hook_id);
+       }
+
        return msginfo;
 }
 
@@ -1007,3 +1065,51 @@ gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len,
 
        return 0;
 }
+
+HeaderEntry *procheader_entries_from_str(const gchar *str)
+{
+       HeaderEntry *entries = NULL, *he;
+       int numh = 0, i = 0;
+       gchar **names = NULL;
+       const gchar *s = str;
+
+       if (s == NULL) {
+               return NULL;
+       }
+       while (*s != '\0') {
+               if (*s == ' ') ++numh;
+               ++s;
+       }
+       if (numh == 0) {
+               return NULL;
+       }
+       entries = g_new0(HeaderEntry, numh + 1); /* room for last NULL */
+       s = str;
+       ++s; /* skip first space */
+       names = g_strsplit(s, " ", numh);
+       he = entries;
+       while (names[i]) {
+               he->name = g_strdup_printf("%s:", names[i]);
+               he->body = NULL;
+               he->unfold = FALSE;
+               ++i, ++he;
+       }
+       he->name = NULL;
+       g_strfreev(names);
+       return entries;
+}
+
+void procheader_entries_free (HeaderEntry *entries)
+{
+       if (entries != NULL) {
+               HeaderEntry *he = entries;
+               while (he->name != NULL) {
+                       g_free(he->name);
+                       if (he->body != NULL)
+                               g_free(he->body);
+                       ++he;                   
+               }
+               g_free(entries);
+       }
+}
+