Remove inclusion of sys/socket.h from nntp-thread.c
[claws.git] / src / plugins / libravatar / libravatar_federation.c
blob09824e23bd2594aa69517a2ee6956479372ef5ce
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
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.
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.
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/>.
19 #include "config.h"
21 #include <stdio.h>
23 #include "libravatar_federation.h"
24 #include "utils.h"
25 #include "gtkutils.h"
27 #define MISSING "x"
29 #if defined USE_GNUTLS
30 static GHashTable *federated = NULL;
32 /**
33 * Get the associated avatar URL for a domain.
35 * @param domain Domain to get the URL for.
37 * @return The avatar URL for the domain or NULL if not found.
39 static gchar *get_federated_url_for_domain(const gchar *domain)
41 gchar *found;
43 if (federated == NULL) {
44 return NULL;
47 found = (gchar *) g_hash_table_lookup(federated, domain);
49 if (found != NULL)
50 debug_print("cached avatar url for domain %s found: %s\n", domain, found);
51 else
52 debug_print("cached avatar url for domain %s not found\n", domain);
54 return found;
57 /**
58 * Adds a URL for a domain.
60 * @param url The computed avatar URL.
61 * @param domain Associated domain.
63 static void add_federated_url_for_domain(const gchar *url, const gchar *domain)
65 if (url == NULL)
66 return;
68 if (federated == NULL)
69 federated = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
71 debug_print("new cached avatar url for domain %s: %s\n", domain, url);
72 g_hash_table_insert(federated, g_strdup(domain), g_strdup(url));
74 #endif
76 /**
77 * Retrieves the federated URL for a given email address.
79 * @param address The email address.
81 * @return The avatar URL for the domain of the address.
83 gchar *federated_url_for_address(const gchar *address)
85 #if defined USE_GNUTLS
86 gchar *domain = NULL, *last = NULL, *addr = NULL, *url = NULL;
87 gchar *host = NULL;
88 guint16 port = 0;
90 if (address == NULL || *address == '\0')
91 goto invalid_addr;
93 addr = g_strdup(address);
94 domain = strchr(addr, '@');
95 if (domain == NULL)
96 goto invalid_addr;
98 ++domain;
99 if (strlen(domain) < 5)
100 goto invalid_addr;
102 last = domain;
103 while (*last != '\0' && *last != ' ' && *last != '\t' && *last != '>')
104 ++last;
105 *last = '\0';
107 /* try cached domains */
108 url = get_federated_url_for_domain(domain);
109 if (url != NULL) {
110 g_free(addr);
111 if (!strcmp(url, MISSING)) {
112 return NULL;
114 return g_strdup(url);
117 /* not cached, try secure service first */
118 if (auto_configure_service_sync("avatars-sec", domain, &host, &port)) {
119 if (port != 443) {
120 url = g_strdup_printf("https://%s:%d/avatar", host, port);
121 } else {
122 url = g_strdup_printf("https://%s/avatar", host);
124 } else { /* try standard one if no secure service available */
125 if (auto_configure_service_sync("avatars", domain, &host, &port)) {
126 if (port != 80) {
127 url = g_strdup_printf("http://%s:%d/avatar", host, port);
128 } else {
129 url = g_strdup_printf("http://%s/avatar", host);
131 } else {
132 debug_print("libravatar federated domain for %s not found\n", domain);
135 if (url != NULL) {
136 add_federated_url_for_domain(url, domain);
137 } else {
138 add_federated_url_for_domain(MISSING, domain);
141 g_free(addr);
142 return url;
144 invalid_addr:
145 if (addr != NULL)
146 g_free(addr);
148 debug_print("invalid address for libravatar federated domain\n");
149 return NULL;
150 #else
151 debug_print("federated domains disabled (built without GnuTLS support)\n");
152 return NULL;
153 #endif