Restore behaviour of missing image mode "None"
[claws.git] / src / plugins / libravatar / libravatar.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2014-2015 Ricardo Mones and the Claws Mail Team
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 3 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, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #include "claws-features.h"
22 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26
27 #include <curl/curl.h>
28
29 #include "version.h"
30 #include "libravatar.h"
31 #include "libravatar_prefs.h"
32 #include "libravatar_cache.h"
33 #include "libravatar_image.h"
34 #include "libravatar_missing.h"
35 #include "libravatar_federation.h"
36 #include "prefs_common.h"
37 #include "procheader.h"
38 #include "procmsg.h"
39 #include "utils.h"
40 #include "md5.h"
41
42 /* indexes of keys are default_mode - 10 if applicable */
43 static const char *def_mode[] = {
44         "404",  /* not used, only useful in web pages */
45         "mm",
46         "identicon",
47         "monsterid",
48         "wavatar",
49         "retro"
50 };
51
52 static gulong update_hook_id = HOOK_NONE;
53 static gulong render_hook_id = HOOK_NONE;
54 static gchar *cache_dir = NULL; /* dir-separator terminated */
55
56 static gboolean libravatar_header_update_hook(gpointer source, gpointer data)
57 {
58         AvatarCaptureData *acd = (AvatarCaptureData *)source;
59
60         debug_print("libravatar avatar_header_update invoked\n");
61
62         if (!strcmp(acd->header, "From:")) {
63                 gchar *a, *lower;
64
65                 a = g_strdup(acd->content);
66                 extract_address(a);
67
68                 /* string to lower */
69                 for (lower = a; *lower; lower++)
70                         *lower = g_ascii_tolower(*lower);
71
72                 debug_print("libravatar added '%s'\n", a);
73                 procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_LIBRAVATAR, a);
74                 g_free(a);
75         }
76
77         return FALSE; /* keep getting */
78 }
79
80 static gchar *federated_base_url_from_address(const gchar *address)
81 {
82 #if defined USE_GNUTLS
83         gchar *base_url = NULL;
84
85         if (!libravatarprefs.allow_federated) {
86                 debug_print("federated domains disabled by configuration\n");
87                 goto default_url;
88         }
89
90         base_url = federated_url_for_address(address);
91         if (base_url != NULL) {
92                 return base_url;
93         }
94
95 default_url:
96 #endif
97         return g_strdup(libravatarprefs.base_url);
98 }
99
100 static GtkWidget *image_widget_from_pixbuf(GdkPixbuf *picture)
101 {
102         GtkWidget *image = NULL;
103
104         if (picture) {
105                 image = gtk_image_new_from_pixbuf(picture);
106                 g_object_unref(picture);
107         } else
108                 g_warning("null picture returns null widget");
109
110         return image;
111 }
112
113 static GtkWidget *image_widget_from_filename(const gchar *filename)
114 {
115         GdkPixbuf *picture = NULL;
116         GError *error = NULL;
117         gint w, h;
118
119         gdk_pixbuf_get_file_info(filename, &w, &h);
120
121         if (w != AVATAR_SIZE || h != AVATAR_SIZE)
122                 /* server can provide a different size from the requested in URL */
123                 picture = gdk_pixbuf_new_from_file_at_scale(
124                                 filename, AVATAR_SIZE, AVATAR_SIZE, TRUE, &error);
125         else    /* exact size */
126                 picture = gdk_pixbuf_new_from_file(filename, &error);
127
128         if (error != NULL) {
129                 g_warning("failed to load image '%s': %s", filename, error->message);
130                 g_error_free(error);
131                 return NULL;
132         }
133
134         return image_widget_from_pixbuf(picture);
135 }
136
137 static gchar *cache_name_for_md5(const gchar *md5)
138 {
139         if (libravatarprefs.default_mode >= DEF_MODE_MM
140                         && libravatarprefs.default_mode <= DEF_MODE_RETRO) {
141                 /* cache dir for generated avatars */
142                 return g_strconcat(cache_dir, def_mode[libravatarprefs.default_mode - 10],
143                                    G_DIR_SEPARATOR_S, md5, NULL);
144         }
145         /* default cache dir */
146         return g_strconcat(cache_dir, md5, NULL);
147 }
148
149 static GtkWidget *image_widget_from_url(const gchar *url, const gchar *md5)
150 {
151         GtkWidget *image = NULL;
152         AvatarImageFetch aif;
153
154         aif.url = url;
155         aif.md5 = md5;
156         aif.filename = cache_name_for_md5(md5);
157         libravatar_image_fetch(&aif);
158         if (aif.pixbuf) {
159                 image = gtk_image_new_from_pixbuf(aif.pixbuf);
160                 g_object_unref(aif.pixbuf);
161         }
162         g_free(aif.filename);
163
164         return image;
165 }
166
167 static gboolean is_recent_enough(const gchar *filename)
168 {
169         GStatBuf s;
170         time_t t;
171
172         if (libravatarprefs.cache_icons) {
173                 t = time(NULL);
174                 if (t != (time_t)-1 && !g_stat(filename, &s)) {
175                         if (t - s.st_ctime <= libravatarprefs.cache_interval * 3600)
176                                 return TRUE;
177                 }
178         }
179
180         return FALSE; /* re-download */
181 }
182
183 static GtkWidget *image_widget_from_cached_md5(const gchar *md5)
184 {
185         GtkWidget *image = NULL;
186         gchar *filename;
187
188         filename = cache_name_for_md5(md5);
189         if (is_file_exist(filename) && is_recent_enough(filename)) {
190                 debug_print("found cached image for %s\n", md5);
191                 image = image_widget_from_filename(filename);
192         }
193         g_free(filename);
194
195         return image;
196 }
197
198 static gchar *libravatar_url_for_md5(const gchar *base, const gchar *md5)
199 {
200         if (libravatarprefs.default_mode >= DEF_MODE_404) {
201                 return g_strdup_printf("%s/%s?s=%u&d=%s",
202                                 base, md5, AVATAR_SIZE,
203                                 def_mode[libravatarprefs.default_mode - 10]);
204         } else if (libravatarprefs.default_mode == DEF_MODE_URL) {
205                 gchar *escaped = g_uri_escape_string(libravatarprefs.default_mode_url, "/", TRUE);
206                 gchar *url = g_strdup_printf("%s/%s?s=%u&d=%s",
207                                 base, md5, AVATAR_SIZE, escaped);
208                 g_free(escaped);
209                 return url;
210         } else if (libravatarprefs.default_mode == DEF_MODE_NONE) {
211                 return g_strdup_printf("%s/%s?s=%u&d=404",
212                                 base, md5, AVATAR_SIZE);
213         }
214
215         g_warning("invalid libravatar default mode: %d", libravatarprefs.default_mode);
216         return NULL;
217 }
218
219 static gboolean libravatar_image_render_hook(gpointer source, gpointer data)
220 {
221         AvatarRender *ar = (AvatarRender *)source;
222         GtkWidget *image = NULL;
223         gchar *a = NULL, *url = NULL;
224         gchar md5sum[33];
225
226         debug_print("libravatar avatar_image_render invoked\n");
227
228         a = procmsg_msginfo_get_avatar(ar->full_msginfo, AVATAR_LIBRAVATAR);
229         if (a != NULL) {
230                 gchar *base;
231
232                 md5_hex_digest(md5sum, a);
233                 /* try missing cache */
234                 if (is_missing_md5(libravatarmisses, md5sum)) {
235                         return FALSE;
236                 }
237                 /* try disk cache */
238                 image = image_widget_from_cached_md5(md5sum);
239                 if (image != NULL) {
240                         if (ar->image) /* previous plugin set one */
241                                 gtk_widget_destroy(ar->image);
242                         ar->image = image;
243                         ar->type  = AVATAR_LIBRAVATAR;
244                         return FALSE;
245                 }
246                 /* not cached copy: try network */
247                 if (prefs_common_get_prefs()->work_offline) {
248                         debug_print("working off-line: libravatar network retrieval skipped\n");
249                         return FALSE;
250                 }
251                 base = federated_base_url_from_address(a);
252                 url = libravatar_url_for_md5(base, md5sum);
253                 if (url != NULL) {
254                         image = image_widget_from_url(url, md5sum);
255                         g_free(url);
256                         if (image != NULL) {
257                                 if (ar->image) /* previous plugin set one */
258                                         gtk_widget_destroy(ar->image);
259                                 ar->image = image;
260                                 ar->type  = AVATAR_LIBRAVATAR;
261                         }
262                 }
263                 g_free(base);
264
265                 return TRUE;
266         }
267
268         return FALSE; /* keep rendering */
269 }
270
271 static gint cache_dir_init()
272 {
273         cache_dir = libravatar_cache_init(def_mode, DEF_MODE_MM - 10, DEF_MODE_RETRO - 10);
274         cm_return_val_if_fail (cache_dir != NULL, -1);
275
276         return 0;
277 }
278
279 static gint missing_cache_init()
280 {
281         gchar *cache_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
282                                         LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
283                                         LIBRAVATAR_MISSING_FILE, NULL);
284
285         libravatarmisses = missing_load_from_file(cache_file);
286         g_free(cache_file);
287
288         if (libravatarmisses == NULL)
289                 return -1;
290
291         return 0;
292 }
293
294 static void missing_cache_done()
295 {
296         gchar *cache_file;
297
298         if (libravatarmisses != NULL) {
299                 cache_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
300                                         LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
301                                         LIBRAVATAR_MISSING_FILE, NULL);
302                 missing_save_to_file(libravatarmisses, cache_file);
303                 g_free(cache_file);
304                 g_hash_table_destroy(libravatarmisses);
305         }
306 }
307
308 static void unregister_hooks()
309 {
310         if (render_hook_id != HOOK_NONE) {
311                 hooks_unregister_hook(AVATAR_IMAGE_RENDER_HOOKLIST,
312                                       render_hook_id);
313                 render_hook_id = HOOK_NONE;
314         }
315         if (update_hook_id != HOOK_NONE) {
316                 hooks_unregister_hook(AVATAR_HEADER_UPDATE_HOOKLIST,
317                                       update_hook_id);
318                 update_hook_id = HOOK_NONE;
319         }
320 }
321
322 /**
323  * Initialize plugin.
324  *
325  * @param error  For storing the returned error message.
326  *
327  * @return 0 if initialization succeeds, -1 on failure.
328  */
329 gint plugin_init(gchar **error)
330 {
331         if (!check_plugin_version(MAKE_NUMERIC_VERSION(3,9,3,29),
332                                   VERSION_NUMERIC, _("Libravatar"), error))
333                 return -1;
334         /* get info from headers */
335         update_hook_id = hooks_register_hook(AVATAR_HEADER_UPDATE_HOOKLIST,
336                                              libravatar_header_update_hook,
337                                              NULL);
338         if (update_hook_id == HOOK_NONE) {
339                 *error = g_strdup(_("Failed to register avatar header update hook"));
340                 return -1;
341         }
342         /* get image for displaying */
343         render_hook_id = hooks_register_hook(AVATAR_IMAGE_RENDER_HOOKLIST,
344                                              libravatar_image_render_hook,
345                                              NULL);
346         if (render_hook_id == HOOK_NONE) {
347                 unregister_hooks();
348                 *error = g_strdup(_("Failed to register avatar image render hook"));
349                 return -1;
350         }
351         /* cache dir */
352         if (cache_dir_init() == -1) {
353                 unregister_hooks();
354                 *error = g_strdup(_("Failed to create avatar image cache directory"));
355                 return -1;
356         }
357         /* preferences page */
358         libravatar_prefs_init();
359         /* curl library */
360         curl_global_init(CURL_GLOBAL_DEFAULT);
361         /* missing cache */
362         if (missing_cache_init() == -1) {
363                 unregister_hooks();
364                 *error = g_strdup(_("Failed to load missing items cache"));
365                 return -1;
366         }
367         debug_print("Libravatar plugin loaded\n");
368
369         return 0;
370 }
371
372 /**
373  * Destructor for the plugin.
374  * Unregister the callback function and frees matcher.
375  *
376  * @return Always TRUE.
377  */
378 gboolean plugin_done(void)
379 {
380         unregister_hooks();
381         libravatar_prefs_done();
382         missing_cache_done();
383         if (cache_dir != NULL)
384                 g_free(cache_dir);
385         debug_print("Libravatar plugin unloaded\n");
386
387         return TRUE;
388 }
389
390 /**
391  * Get the name of the plugin.
392  *
393  * @return The plugin's name, maybe translated.
394  */
395 const gchar *plugin_name(void)
396 {
397         return _("Libravatar");
398 }
399
400 /**
401  * Get the description of the plugin.
402  *
403  * @return The plugin's description, maybe translated.
404  */
405 const gchar *plugin_desc(void)
406 {
407         return _("Display libravatar profiles' images for mail messages. More\n"
408                  "info about libravatar at http://www.libravatar.org/. If you have\n"
409                  "a gravatar.com profile but not a libravatar one, those will also\n"
410                  "be retrieved (when redirections are allowed in plugin config).\n"
411                  "Plugin config page is available from main window at:\n"
412                  "/Configuration/Preferences/Plugins/Libravatar.\n\n"
413                  "This plugin uses libcurl to retrieve images, so if you're behind a\n"
414                  "proxy please refer to curl(1) manpage for details on 'http_proxy'\n"
415                  "configuration. More details about this and others on README file.\n\n"
416                  "Feedback to <ricardo@mones.org> is welcome.\n");
417 }
418
419 /**
420  * Get the kind of plugin.
421  *
422  * @return The "GTK2" constant.
423  */
424 const gchar *plugin_type(void)
425 {
426         return "GTK2";
427 }
428
429 /**
430  * Get the license acronym the plugin is released under.
431  *
432  * @return The "GPL3+" constant.
433  */
434 const gchar *plugin_licence(void)
435 {
436         return "GPL3+";
437 }
438
439 /**
440  * Get the version of the plugin.
441  *
442  * @return The current version string.
443  */
444 const gchar *plugin_version(void)
445 {
446         return VERSION;
447 }
448
449 /**
450  * Get the features implemented by the plugin.
451  *
452  * @return A constant PluginFeature structure with the features.
453  */
454 struct PluginFeature *plugin_provides(void)
455 {
456         static struct PluginFeature features[] =
457                 { {PLUGIN_OTHER, N_("Libravatar")},
458                   {PLUGIN_NOTHING, NULL}};
459
460         return features;
461 }
462