Fix typo, thanks wwp!
[claws.git] / src / plugins / libravatar / libravatar.c
index 9f40a95c9fb2e9b81f0593ea8587f4cd7659638e..7870bc94db42a802a11d4bf7930d5647922eab47 100644 (file)
@@ -31,6 +31,7 @@
 #include "libravatar.h"
 #include "libravatar_prefs.h"
 #include "libravatar_missing.h"
 #include "libravatar.h"
 #include "libravatar_prefs.h"
 #include "libravatar_missing.h"
+#include "libravatar_federation.h"
 #include "prefs_common.h"
 #include "procheader.h"
 #include "procmsg.h"
 #include "prefs_common.h"
 #include "procheader.h"
 #include "procmsg.h"
@@ -50,7 +51,6 @@ static const char *def_mode[] = {
 static guint update_hook_id;
 static guint render_hook_id;
 static gchar *cache_dir = NULL; /* dir-separator terminated */
 static guint update_hook_id;
 static guint render_hook_id;
 static gchar *cache_dir = NULL; /* dir-separator terminated */
-static GHashTable *misses;
 
 static gboolean libravatar_header_update_hook(gpointer source, gpointer data)
 {
 
 static gboolean libravatar_header_update_hook(gpointer source, gpointer data)
 {
@@ -59,8 +59,15 @@ static gboolean libravatar_header_update_hook(gpointer source, gpointer data)
        debug_print("libravatar avatar_header_update invoked\n");
 
        if (!strcmp(acd->header, "From:")) {
        debug_print("libravatar avatar_header_update invoked\n");
 
        if (!strcmp(acd->header, "From:")) {
-               gchar *a = g_strdup(acd->content);
+               gchar *a, *lower;
+
+               a = g_strdup(acd->content);
                extract_address(a);
                extract_address(a);
+
+               /* string to lower */
+               for (lower = a; *lower; lower++)
+                       *lower = g_ascii_tolower(*lower);
+
                debug_print("libravatar added '%s'\n", a);
                procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_LIBRAVATAR, a);
        }
                debug_print("libravatar added '%s'\n", a);
                procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_LIBRAVATAR, a);
        }
@@ -70,10 +77,21 @@ static gboolean libravatar_header_update_hook(gpointer source, gpointer data)
 
 static gchar *federated_base_url_from_address(const gchar *address)
 {
 
 static gchar *federated_base_url_from_address(const gchar *address)
 {
-       /*
-          TODO: no federation supported right now
-          Details on http://wiki.libravatar.org/running_your_own/
-        */
+#if (defined USE_GNUTLS && GLIB_CHECK_VERSION(2,22,0))
+       gchar *base_url = NULL;
+
+       if (!libravatarprefs.allow_federated) {
+               debug_print("federated domains disabled by configuration\n");
+               goto default_url;
+       }
+
+       base_url = federated_url_for_address(address);
+       if (base_url != NULL) {
+               return base_url;
+       }
+
+default_url:
+#endif
        return g_strdup(libravatarprefs.base_url);
 }
 
        return g_strdup(libravatarprefs.base_url);
 }
 
@@ -141,6 +159,8 @@ static GtkWidget *image_widget_from_url(const gchar *url, const gchar *md5)
        }
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_image_data_cb);
        }
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_image_data_cb);
+       curl_easy_setopt(curl, CURLOPT_TIMEOUT, prefs_common_get_prefs()->io_timeout_secs);
+       curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
 
        filename = cache_name_for_md5(md5);
        file = fopen(filename, "wb");
 
        filename = cache_name_for_md5(md5);
        file = fopen(filename, "wb");
@@ -148,7 +168,8 @@ static GtkWidget *image_widget_from_url(const gchar *url, const gchar *md5)
                long filesize;
 
                if (libravatarprefs.allow_redirects) {
                long filesize;
 
                if (libravatarprefs.allow_redirects) {
-                       long maxredirs = (libravatarprefs.default_mode == DEF_MODE_MM)? 2L: 1L;
+                       long maxredirs = (libravatarprefs.default_mode == DEF_MODE_URL)? 3L
+                               : ((libravatarprefs.default_mode == DEF_MODE_MM)? 2L: 1L);
 
                        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
                        curl_easy_setopt(curl, CURLOPT_MAXREDIRS, maxredirs);
 
                        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
                        curl_easy_setopt(curl, CURLOPT_MAXREDIRS, maxredirs);
@@ -164,13 +185,13 @@ static GtkWidget *image_widget_from_url(const gchar *url, const gchar *md5)
                else
                        image = image_widget_from_filename(filename);
 
                else
                        image = image_widget_from_filename(filename);
 
-               if (!libravatarprefs.cache_icons) {
+               if (!libravatarprefs.cache_icons || filesize == 0) {
                        if (g_unlink(filename) < 0)
                                g_warning("failed to delete cache file %s\n", filename);
                }
 
                if (filesize == 0)
                        if (g_unlink(filename) < 0)
                                g_warning("failed to delete cache file %s\n", filename);
                }
 
                if (filesize == 0)
-                       missing_add_md5(misses, md5);
+                       missing_add_md5(libravatarmisses, md5);
        } else {
                g_warning("could not open '%s' for writting\n", filename);
        }
        } else {
                g_warning("could not open '%s' for writting\n", filename);
        }
@@ -245,7 +266,7 @@ static gboolean libravatar_image_render_hook(gpointer source, gpointer data)
 
                md5_hex_digest(md5sum, a);
                /* try missing cache */
 
                md5_hex_digest(md5sum, a);
                /* try missing cache */
-               if (is_missing_md5(misses, md5sum)) {
+               if (is_missing_md5(libravatarmisses, md5sum)) {
                        return FALSE;
                }
                /* try disk cache */
                        return FALSE;
                }
                /* try disk cache */
@@ -254,6 +275,7 @@ static gboolean libravatar_image_render_hook(gpointer source, gpointer data)
                        if (ar->image) /* previous plugin set one */
                                gtk_widget_destroy(ar->image);
                        ar->image = image;
                        if (ar->image) /* previous plugin set one */
                                gtk_widget_destroy(ar->image);
                        ar->image = image;
+                       ar->type  = AVATAR_LIBRAVATAR;
                        return FALSE;
                }
                /* not cached copy: try network */
                        return FALSE;
                }
                /* not cached copy: try network */
@@ -270,6 +292,7 @@ static gboolean libravatar_image_render_hook(gpointer source, gpointer data)
                                if (ar->image) /* previous plugin set one */
                                        gtk_widget_destroy(ar->image);
                                ar->image = image;
                                if (ar->image) /* previous plugin set one */
                                        gtk_widget_destroy(ar->image);
                                ar->image = image;
+                               ar->type  = AVATAR_LIBRAVATAR;
                        }
                }
                g_free(base);
                        }
                }
                g_free(base);
@@ -315,10 +338,10 @@ static gint missing_cache_init()
                                        LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
                                        LIBRAVATAR_MISSING_FILE, NULL);
 
                                        LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
                                        LIBRAVATAR_MISSING_FILE, NULL);
 
-       misses = missing_load_from_file(cache_file);
+       libravatarmisses = missing_load_from_file(cache_file);
        g_free(cache_file);
 
        g_free(cache_file);
 
-       if (misses == NULL)
+       if (libravatarmisses == NULL)
                return -1;
 
        return 0;
                return -1;
 
        return 0;
@@ -328,13 +351,13 @@ static void missing_cache_done()
 {
        gchar *cache_file;
 
 {
        gchar *cache_file;
 
-       if (misses != NULL) {
+       if (libravatarmisses != NULL) {
                cache_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
                                        LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
                                        LIBRAVATAR_MISSING_FILE, NULL);
                cache_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
                                        LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
                                        LIBRAVATAR_MISSING_FILE, NULL);
-               missing_save_to_file(misses, cache_file);
+               missing_save_to_file(libravatarmisses, cache_file);
                g_free(cache_file);
                g_free(cache_file);
-               g_hash_table_destroy(misses);
+               g_hash_table_destroy(libravatarmisses);
        }
 }
 
        }
 }
 
@@ -429,8 +452,15 @@ const gchar *plugin_name(void)
  */
 const gchar *plugin_desc(void)
 {
  */
 const gchar *plugin_desc(void)
 {
-       return _("Get and display libravatar images for mail messages.\n\n"
-                "Info about libravatar at http://www.libravatar.org/\n\n"
+       return _("Display libravatar profiles' images for mail messages. More\n"
+                "info about libravatar at http://www.libravatar.org/. If you have\n"
+                "a gravatar.com profile but not a libravatar one, those will also\n"
+                "be retrieved (when redirections are allowed in plugin config).\n"
+                "Plugin config page is available from main window at:\n"
+                "/Configuration/Preferences/Plugins/Libravatar.\n\n"
+                "This plugin uses libcurl to retrieve images, so if you're behind a\n"
+                "proxy please refer to curl(1) manpage for details on 'http_proxy'\n"
+                "configuration. More details about this and others on README file.\n\n"
                 "Feedback to <ricardo@mones.org> is welcome.\n");
 }
 
                 "Feedback to <ricardo@mones.org> is welcome.\n");
 }