1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) version 3.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the program; if not, see <http://www.gnu.org/licenses/>
*
*
* Authors:
* Srinivasa Ragavan <srini@linux.intel.com>
*
* Copyright (C) 2009 Intel Corporation (www.intel.com)
*
*/
/* Template of the code is taken from libsoup tests/ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <libsoup/soup.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlmemory.h>
#include <libedataserver/e-proxy.h>
#include <shell/e-shell.h>
#include <e-util/e-util-private.h>
#include "mail-guess-servers.h"
static gchar *
xml_to_gchar (xmlChar *xml,
EmailProvider *provider)
{
gchar *gxml = NULL;
gchar *tmp;
gchar *repl = NULL;
const gchar *sec_part;
tmp = xml ? strstr((gchar *) xml, "\%EMAIL") : NULL;
if (!tmp) {
gxml = xml ? g_strdup ((gchar *) xml) : NULL;
} else {
decodepart:
*tmp = 0;
tmp+=6;
if (*tmp == 'A')
repl = provider->email;
else if (*tmp == 'L')
repl = provider->username;
else if (*tmp == 'D')
repl = provider->domain;
sec_part = strstr(tmp, "\%");
sec_part++;
if (!*sec_part)
sec_part = "";
gxml = g_strdup_printf("%s%s%s", gxml ? gxml : (gchar *)xml, repl, sec_part);
tmp = strstr (gxml, "\%EMAIL");
if (tmp) {
goto decodepart;
}
}
xmlFree (xml);
return gxml;
}
static SoupMessage *
get_url (SoupSession *session,
const gchar *url)
{
SoupMessage *msg;
const gchar *header;
msg = soup_message_new (SOUP_METHOD_GET, url);
soup_message_set_flags (msg, SOUP_MESSAGE_NO_REDIRECT);
soup_session_send_message (session, msg);
if (SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
header = soup_message_headers_get_one (msg->response_headers,
"Location");
if (header) {
return get_url (session, header);
}
} else if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
return msg;
}
return NULL;
}
static void
handle_incoming (xmlNodePtr head,
EmailProvider *provider)
{
xmlNodePtr node = head->children;
provider->recv_type = xml_to_gchar (
xmlGetProp (head, (xmlChar *) "type"), provider);
while (node != NULL) {
if (strcmp ((gchar *)node->name, "hostname") == 0) {
provider->recv_hostname = xml_to_gchar (
xmlNodeGetContent (node), provider);
} else if (strcmp ((gchar *)node->name, "port") == 0) {
provider->recv_port = xml_to_gchar (
xmlNodeGetContent (node), provider);
} else if (strcmp ((gchar *)node->name, "socketType") == 0) {
provider->recv_socket_type = xml_to_gchar (
xmlNodeGetContent (node), provider);
} else if (strcmp ((gchar *)node->name, "username") == 0) {
provider->recv_username = xml_to_gchar (
xmlNodeGetContent (node), provider);
} else if (strcmp ((gchar *)node->name, "authentication") == 0) {
provider->recv_auth = xml_to_gchar (
xmlNodeGetContent (node), provider);
}
node = node->next;
}
}
static void
handle_outgoing (xmlNodePtr head,
EmailProvider *provider)
{
xmlNodePtr node = head->children;
provider->send_type = xml_to_gchar (
xmlGetProp (head, (xmlChar *) "type"), provider);
while (node != NULL) {
if (strcmp ((gchar *)node->name, "hostname") == 0) {
provider->send_hostname = xml_to_gchar (
xmlNodeGetContent (node), provider);
} else if (strcmp ((gchar *)node->name, "port") == 0) {
provider->send_port = xml_to_gchar (
xmlNodeGetContent (node), provider);
} else if (strcmp ((gchar *)node->name, "socketType") == 0) {
provider->send_socket_type = xml_to_gchar (
xmlNodeGetContent (node), provider);
} else if (strcmp ((gchar *)node->name, "username") == 0) {
provider->send_username = xml_to_gchar (
xmlNodeGetContent (node), provider);
} else if (strcmp ((gchar *)node->name, "authentication") == 0) {
provider->send_auth = xml_to_gchar (
xmlNodeGetContent (node), provider);
}
node = node->next;
}
}
static gboolean
parse_message (const gchar *msg,
gint length,
EmailProvider *provider)
{
xmlDocPtr doc;
xmlNodePtr node;
doc = xmlReadMemory (msg, length, "file.xml", NULL, 0);
node = doc->children;
while (node) {
if (strcmp ((gchar *)node->name, "clientConfig") == 0) {
break;
}
node = node->next;
}
if (!node) {
g_warning ("Incorrect data: ClientConfig not found ... Quitting\n");
return FALSE;
}
node = node->children;
while (node) {
if (strcmp ((gchar *)node->name, "emailProvider") == 0) {
break;
}
node = node->next;
}
if (!node) {
g_warning ("Incorrect data: ClientConfig not found ... Quitting\n");
return FALSE;
}
node = node->children;
while (node) {
if (strcmp ((gchar *)node->name, "incomingServer") == 0) {
/* Handle Incoming */
handle_incoming (node, provider);
} else if (strcmp ((gchar *)node->name, "outgoingServer") == 0) {
/* Handle Outgoing */
handle_outgoing (node, provider);
}
node = node->next;
}
xmlFreeDoc (doc);
return TRUE;
}
static gboolean
parse_soup_message (SoupMessage *msg,
EmailProvider *provider)
{
return parse_message (
msg->response_body->data,
msg->response_body->length, provider);
}
static gboolean
is_online (void)
{
EShell *shell;
/* FIXME Pass this in. */
shell = e_shell_get_default ();
return e_shell_get_online (shell);
}
static gboolean
guess_when_online (EmailProvider *provider)
{
const gchar *cafile = NULL;
gchar *url;
EProxy *proxy;
SoupURI *parsed;
SoupMessage *msg;
SoupSession *session;
proxy = e_proxy_new ();
e_proxy_setup_proxy (proxy);
url = g_strdup_printf (
"%s/%s", "http://api.gnome.org/evolution/autoconfig",
provider->domain);
parsed = soup_uri_new (url);
soup_uri_free (parsed);
session = soup_session_sync_new_with_options (
SOUP_SESSION_SSL_CA_FILE, cafile,
SOUP_SESSION_USER_AGENT, "get ",
NULL);
if (e_proxy_require_proxy_for_uri (proxy, url)) {
SoupURI *proxy_uri = e_proxy_peek_uri_for (proxy, url);
/* fprintf (stderr, "URL '%s' requires a proxy: '%s'\n",
url, soup_uri_to_string (proxy_uri, FALSE)); */
g_object_set (session, SOUP_SESSION_PROXY_URI, proxy_uri, NULL);
}
msg = get_url (session, url);
if (!msg)
return FALSE;
parse_soup_message (msg, provider);
g_object_unref (proxy);
g_object_unref (msg);
g_object_unref (session);
g_free (url);
return TRUE;
}
static gchar *
get_filename_for_offline_autoconfig (const gchar *domain)
{
return g_build_filename (EVOLUTION_PRIVDATADIR, "mail-autoconfig", domain, NULL);
}
static gboolean
guess_when_offline (EmailProvider *provider)
{
gchar *filename;
gchar *contents;
gsize length;
gboolean success;
if (!provider->domain || provider->domain[0] == 0)
return FALSE;
success = FALSE;
filename = get_filename_for_offline_autoconfig (provider->domain);
if (!g_file_get_contents (filename, &contents, &length, NULL)) /* NULL-GError */
goto out;
success = parse_message (contents, (gint) length, provider);
out:
g_free (filename);
g_free (contents);
return success;
}
gboolean
mail_guess_servers (EmailProvider *provider)
{
if (is_online () && guess_when_online (provider))
return TRUE;
else
return guess_when_offline (provider);
}
#ifdef TEST
gint
main (gint argc,
gchar **argv)
{
EmailProvider *provider;
g_type_init ();
provider = g_new0 (EmailProvider, 1);
provider->email = "sragavan@iijmio-mail.jp";
provider->domain = "iijmio-mail.jp";
provider->username = "sragavan";
mail_guess_servers (provider);
printf (
"Recv: %s\n%s(%s), %s by %s \n "
"Send: %s\n%s(%s), %s by %s\n via %s to %s\n",
provider->recv_type, provider->recv_hostname,
provider->recv_port, provider->recv_username,
provider->recv_auth,
provider->send_type, provider->send_hostname,
provider->send_port, provider->send_username,
provider->send_auth,
provider->recv_socket_type,
provider->send_socket_type);
return 0;
}
#endif
|