talons

Fork of Claws Mail https://www.claws-mail
Log | Files | Refs | README | LICENSE

commit 9a8c64d48a0dcacffcfafb1d9759614516cb28cb
parent a9aae8f1aded116587af78fa7882b213dd9908fb
Author: Ricardo Mones <ricardo@mones.org>
Date:   Sun, 27 Apr 2014 02:45:18 +0200

Retrieve avatar service URL from address

Diffstat:
Msrc/plugins/libravatar/Makefile.am | 3++-
Asrc/plugins/libravatar/libravatar_federation.c | 147+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/plugins/libravatar/libravatar_federation.h | 27+++++++++++++++++++++++++++
3 files changed, 176 insertions(+), 1 deletion(-)

diff --git a/src/plugins/libravatar/Makefile.am b/src/plugins/libravatar/Makefile.am @@ -73,5 +73,6 @@ libravatar_la_CPPFLAGS = \ libravatar_la_SOURCES = \ libravatar.c libravatar.h \ libravatar_prefs.c libravatar_prefs.h \ - libravatar_missing.c libravatar_missing.h + libravatar_missing.c libravatar_missing.h \ + libravatar_federation.c libravatar_federation.h diff --git a/src/plugins/libravatar/libravatar_federation.c b/src/plugins/libravatar/libravatar_federation.c @@ -0,0 +1,147 @@ +/* + * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client + * Copyright (C) 1999-2014 Hiroyuki Yamamoto and the Claws Mail Team + * Copyright (C) 2014 Ricardo Mones + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdio.h> + +#include "libravatar_federation.h" +#include "libravatar_prefs.h" +#include "utils.h" + +#define MISSING "x" + +static GHashTable *federated = NULL; + +/** + * Get the associated avatar URL for a domain. + * + * @param domain Domain to get the URL for. + * + * @return The avatar URL for the domain or NULL if not found. + */ +static gchar *get_federated_url_for_domain(const gchar *domain) +{ + gchar *found; + + if (federated == NULL) { + return NULL; + } + + found = (gchar *) g_hash_table_lookup(federated, domain); + + if (found != NULL) + debug_print("cached avatar url for domain %s found: %s\n", domain, found); + else + debug_print("cached avatar url for domain %s not found\n", domain); + + return found; +} + +/** + * Adds a URL for a domain. + * + * @param url The computed avatar URL. + * @param domain Associated domain. + */ +static void add_federated_url_for_domain(const gchar *url, const gchar *domain) +{ + if (url == NULL) + return; + + if (federated == NULL) + federated = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); + + debug_print("new cached avatar url for domain %s: %s\n", domain, url); + g_hash_table_insert(federated, g_strdup(domain), g_strdup(url)); +} + +/** + * Retrieves the federated URL for a given email address. + * + * @param address The email address. + * + * @return The avatar URL for the domain of the address. + */ +gchar *federated_url_for_address(const gchar *address) +{ + gchar *domain = NULL, *last = NULL, *addr = NULL, *url = NULL; + gchar *host = NULL; + guint16 port = 0; + + if (address == NULL || *address == '\0') + goto invalid_addr; + + addr = g_strdup(address); + domain = strchr(addr, '@'); + if (domain == NULL) + goto invalid_addr; + + ++domain; + if (strlen(domain) < 5) + goto invalid_addr; + + last = domain; + while (*last != '\0' && *last != ' ' && *last != '\t' && *last != '>') + ++last; + *last = '\0'; + + /* try cached domains */ + url = get_federated_url_for_domain(domain); + if (url != NULL) { + g_free(addr); + if (!strcmp(url, MISSING)) { + return g_strdup(libravatarprefs.base_url); + } + return g_strdup(url); + } + + /* not cached, try secure service first */ + if (auto_configure_service("avatars-sec", domain, &host, &port)) { + if (port != 443) { + url = g_strdup_printf("https://%s:%d/avatar", host, port); + } else { + url = g_strdup_printf("https://%s/avatar", host); + } + } else { /* try standard one if no secure service available */ + if (auto_configure_service("avatars", domain, &host, &port)) { + if (port != 80) { + url = g_strdup_printf("http://%s:%d/avatar", host, port); + } else { + url = g_strdup_printf("http://%s/avatar", host); + } + } else { + debug_print("libravatar federated domain for %s not found\n", domain); + } + } + if (url != NULL) { + add_federated_url_for_domain(url, domain); + } else { + add_federated_url_for_domain(MISSING, domain); + } + + g_free(addr); + return url; + +invalid_addr: + if (addr != NULL) + g_free(addr); + + debug_print("invalid address for libravatar federated domain\n"); + return NULL; +} + diff --git a/src/plugins/libravatar/libravatar_federation.h b/src/plugins/libravatar/libravatar_federation.h @@ -0,0 +1,27 @@ +/* + * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client + * Copyright (C) 1999-2014 Hiroyuki Yamamoto and the Claws Mail Team + * Copyright (C) 2014 Ricardo Mones + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef __LIBRAVATAR_FEDERATION_H +#define __LIBRAVATAR_FEDERATION_H + +#include <glib.h> + +gchar *federated_url_for_address (const gchar *address); + +#endif