]> git.0d.be Git - empathy.git/blob - src/empathy-import-pidgin.c
f50b18365c4002534c8d7c6a2795aca7202bfcaf
[empathy.git] / src / empathy-import-pidgin.c
1 /*
2  * Copyright (C) 2008 Collabora Ltd.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA  02110-1301  USA
18  *
19  * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
20  * */
21
22 #include <config.h>
23
24 #include <string.h>
25 #include <unistd.h>
26
27 #include <glib.h>
28 #include <glib/gstdio.h>
29 #include <dbus/dbus-protocol.h>
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
32
33 #include <telepathy-glib/util.h>
34 #include <telepathy-glib/dbus.h>
35
36 #include "empathy-import-utils.h"
37 #include "empathy-import-pidgin.h"
38
39 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
40 #include <libempathy/empathy-debug.h>
41 #include <libempathy/empathy-utils.h>
42
43 #include <libempathy-gtk/empathy-ui-utils.h>
44
45 /* Pidgin to CM map */
46 typedef struct
47 {
48   const gchar *protocol;
49   const gchar *pidgin_name;
50   const gchar *cm_name;
51 } PidginCmMapItem;
52
53 static PidginCmMapItem pidgin_cm_map[] =
54 {
55   { "msn", "server", "server" },
56   { "msn", "port", "port" },
57
58   { "jabber", "connect_server", "server" },
59   { "jabber", "port", "port" },
60   { "jabber", "require_tls", "require-encryption" },
61   { "jabber", "old_ssl", "old-ssl" },
62
63   { "aim", "server", "server" },
64   { "aim", "port", "port" },
65
66   { "salut", "first", "first-name" },
67   { "salut", "last", "last-name" },
68   { "salut", "jid", "jid" },
69   { "salut", "email", "email" },
70
71   { "groupwise", "server", "server" },
72   { "groupwise", "port", "port" },
73
74   { "icq", "server", "server" },
75   { "icq", "port", "port" },
76
77   { "irc", "realname", "fullname" },
78   { "irc", "ssl", "use-ssl" },
79   { "irc", "port", "port" },
80
81   { "yahoo", "server", "server" },
82   { "yahoo", "port", "port" },
83   { "yahoo", "xfer_port", "xfer-port" },
84   { "yahoo", "ignore_invites", "ignore-invites" },
85   { "yahoo", "yahoojp", "yahoojp" },
86   { "yahoo", "xferjp_host", "xferjp-host" },
87   { "yahoo", "serverjp", "serverjp" },
88   { "yahoo", "xfer_host", "xfer-host" },
89 };
90
91 #define PIDGIN_ACCOUNT_TAG_NAME "name"
92 #define PIDGIN_ACCOUNT_TAG_ACCOUNT "account"
93 #define PIDGIN_ACCOUNT_TAG_PROTOCOL "protocol"
94 #define PIDGIN_ACCOUNT_TAG_PASSWORD "password"
95 #define PIDGIN_ACCOUNT_TAG_SETTINGS "settings"
96 #define PIDGIN_SETTING_PROP_UI "ui"
97 #define PIDGIN_SETTING_PROP_NAME "name"
98 #define PIDGIN_SETTING_PROP_TYPE "type"
99 #define PIDGIN_PROTOCOL_BONJOUR "bonjour"
100 #define PIDGIN_PROTOCOL_NOVELL "novell"
101
102 static void
103 import_dialog_pidgin_parse_setting (EmpathyImportAccountData *data,
104                                     xmlNodePtr setting)
105 {
106   PidginCmMapItem *item = NULL;
107   gchar *tag_name;
108   gchar *type = NULL;
109   gchar *content;
110   guint i;
111   GValue *value = NULL;
112
113   /* We can't do anything if the setting don't have a name */
114   tag_name = (gchar *) xmlGetProp (setting,
115       (xmlChar *) PIDGIN_SETTING_PROP_NAME);
116   if (!tag_name)
117     return;
118
119   /* Search for the map corresponding to setting we are parsing */
120   for (i = 0; i < G_N_ELEMENTS (pidgin_cm_map); i++)
121     {
122       if (!tp_strdiff (data->protocol, pidgin_cm_map[i].protocol) &&
123           !tp_strdiff (tag_name, pidgin_cm_map[i].pidgin_name))
124         {
125           item = pidgin_cm_map + i;
126           break;
127         }
128     }
129   g_free (tag_name);
130
131   /* If we didn't find the item, there is nothing we can do */
132   if (!item)
133     return;
134
135   type = (gchar *) xmlGetProp (setting, (xmlChar *) PIDGIN_SETTING_PROP_TYPE);
136   content = (gchar *) xmlNodeGetContent (setting);
137
138   if (!tp_strdiff (type, "bool"))
139     {
140       i = (gint) g_ascii_strtod (content, NULL);
141       value = tp_g_value_slice_new (G_TYPE_BOOLEAN);
142       g_value_set_boolean (value, i != 0);
143     }
144   else if (!tp_strdiff (type, "int"))
145     {
146       TpConnectionManager *cm = NULL;
147       TpProtocol *proto;
148       const TpConnectionManagerParam *param;
149       const gchar *signature;
150       int signature_i;
151
152       if (!empathy_import_protocol_is_supported (data->protocol, &cm))
153         return;
154
155       proto = tp_connection_manager_get_protocol_object (cm, data->protocol);
156       param = tp_protocol_get_param (proto, item->cm_name);
157       signature = tp_connection_manager_param_get_dbus_signature (param);
158       signature_i = (int) (*signature);
159
160       i = (gint) g_ascii_strtod (content, NULL);
161
162       if (signature_i == DBUS_TYPE_INT16 ||
163           signature_i == DBUS_TYPE_INT32)
164         {
165           value = tp_g_value_slice_new (G_TYPE_INT);
166           g_value_set_int (value, i);
167         }
168       else if (signature_i == DBUS_TYPE_UINT16 ||
169           signature_i == DBUS_TYPE_UINT32)
170         {
171           value = tp_g_value_slice_new (G_TYPE_UINT);
172           g_value_set_uint (value, (guint) i);
173         }
174     }
175   else if (!tp_strdiff (type, "string"))
176     {
177       value = tp_g_value_slice_new (G_TYPE_STRING);
178       g_value_set_string (value, content);
179     }
180
181   if (value)
182     g_hash_table_insert (data->settings, (gpointer) item->cm_name, value);
183
184   g_free (type);
185   g_free (content);
186 }
187
188 static void
189 import_dialog_pidgin_handle_settings (EmpathyImportAccountData *data,
190                                       xmlNodePtr settings)
191 {
192   xmlNodePtr setting;
193   gchar *tag_ui, *name, *type, *content;
194
195   tag_ui = (gchar *) xmlGetProp (settings, (xmlChar *) PIDGIN_SETTING_PROP_UI);
196
197   /* UI settings - fetch the Enabled parameter.
198    * The expected value of the ui property is 'gtk-gaim', which looks obsolete,
199    * but still valid for 2.7.3.
200    */
201   if (tag_ui && !tp_strdiff (tag_ui, "gtk-gaim"))
202     {
203       for (setting = settings->children; setting; setting = setting->next)
204         {
205           name = (gchar *) xmlGetProp (setting,
206               (xmlChar *) PIDGIN_SETTING_PROP_NAME);
207           type = (gchar *) xmlGetProp (setting,
208               (xmlChar *) PIDGIN_SETTING_PROP_TYPE);
209           /* The Enabled parameter is supposed to be boolean.
210            * Pidgin name of the setting is 'auto-login'.
211            */
212           if (!tp_strdiff (name, "auto-login") && !tp_strdiff (type, "bool"))
213             {
214               content = (gchar *) xmlNodeGetContent (setting);
215               data->enabled = (0 != (gint) g_ascii_strtod (content, NULL));
216               g_free (content);
217             }
218           g_free (type);
219           g_free (name);
220         }
221     }
222   /* General settings. */
223   else
224     {
225       for (setting = settings->children; setting; setting = setting->next)
226         import_dialog_pidgin_parse_setting (data, setting);
227     }
228
229   g_free (tag_ui);
230 }
231
232 GList *
233 empathy_import_pidgin_load (void)
234 {
235   xmlNodePtr rootnode, node, child;
236   xmlParserCtxtPtr ctxt;
237   xmlDocPtr doc;
238   gchar *filename;
239   GList *accounts = NULL;
240
241   /* Load pidgin accounts xml */
242   ctxt = xmlNewParserCtxt ();
243   filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
244       NULL);
245
246   if (g_access (filename, R_OK) != 0)
247     goto FILENAME;
248
249   doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
250
251   rootnode = xmlDocGetRootElement (doc);
252   if (rootnode == NULL)
253     goto OUT;
254
255   for (node = rootnode->children; node; node = node->next)
256     {
257       EmpathyImportAccountData *data;
258
259       /* If it is not an account node, skip. */
260       if (tp_strdiff ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT))
261         continue;
262
263       /* Create account data struct */
264       data = empathy_import_account_data_new ("Pidgin");
265
266       /* Parse account's child nodes to fill the account data struct */
267       for (child = node->children; child; child = child->next)
268         {
269           GValue *value;
270
271           /* Protocol */
272           if (!tp_strdiff ((gchar *) child->name,
273               PIDGIN_ACCOUNT_TAG_PROTOCOL))
274             {
275               xmlChar *content;
276               const gchar *protocol;
277
278               content = xmlNodeGetContent (child);
279
280               protocol = (const gchar *) content;
281
282               if (g_str_has_prefix (protocol, "prpl-"))
283                 protocol += 5;
284
285               if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_BONJOUR))
286                 data->protocol = g_strdup ("salut");
287               else if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_NOVELL))
288                 data->protocol = g_strdup ("groupwise");
289               else
290                 data->protocol = g_strdup (protocol);
291
292               xmlFree (content);
293
294               if (data->protocol == NULL)
295                 break;
296             }
297
298           /* Username and IRC server. */
299           else if (!tp_strdiff ((gchar *) child->name,
300               PIDGIN_ACCOUNT_TAG_NAME))
301             {
302               gchar *name;
303               GStrv name_resource = NULL;
304               GStrv nick_server = NULL;
305               const gchar *username;
306
307               name = (gchar *) xmlNodeGetContent (child);
308
309               /* Split "username/resource" */
310               if (g_strrstr (name, "/") != NULL)
311                 {
312                   name_resource = g_strsplit (name, "/", 2);
313                   username = name_resource[0];
314                 }
315               else
316                 username = name;
317
318              /* Split "username@server" if it is an IRC account */
319              if (strstr (name, "@") && !tp_strdiff (data->protocol, "irc"))
320               {
321                 nick_server = g_strsplit (name, "@", 2);
322                 username = nick_server[0];
323
324                 /* Add the server setting */
325                 value = tp_g_value_slice_new (G_TYPE_STRING);
326                 g_value_set_string (value, nick_server[1]);
327                 g_hash_table_insert (data->settings, (gpointer) "server", value);
328               }
329
330               /* Add the account setting */
331               value = tp_g_value_slice_new (G_TYPE_STRING);
332               g_value_set_string (value, username);
333               g_hash_table_insert (data->settings, (gpointer) "account", value);
334
335               g_strfreev (name_resource);
336               g_strfreev (nick_server);
337               g_free (name);
338             }
339
340           /* Password */
341           else if (!tp_strdiff ((gchar *) child->name,
342               PIDGIN_ACCOUNT_TAG_PASSWORD))
343             {
344               gchar *password;
345
346               password = (gchar *) xmlNodeGetContent (child);
347
348               /* Add the password setting */
349               value = tp_g_value_slice_new (G_TYPE_STRING);
350               g_value_set_string (value, password);
351               g_hash_table_insert (data->settings, (gpointer) "password", value);
352
353               g_free (password);
354             }
355
356           /* Other settings */
357           else if (!tp_strdiff ((gchar *) child->name,
358               PIDGIN_ACCOUNT_TAG_SETTINGS))
359             import_dialog_pidgin_handle_settings (data, child);
360         }
361
362       /* If we have the needed settings, add the account data to the list,
363        * otherwise free the data */
364       if (data->protocol != NULL && g_hash_table_size (data->settings) > 0)
365         {
366           /* Special-case XMPP:
367            * http://bugzilla.gnome.org/show_bug.cgi?id=579992 */
368           if (!tp_strdiff (data->protocol, "jabber"))
369             {
370               if (EMP_STR_EMPTY (tp_asv_get_string (data->settings, "server")))
371                 {
372                   g_hash_table_remove (data->settings, "port");
373                   g_hash_table_remove (data->settings, "server");
374                 }
375             }
376
377           /* If there is no password then MC treats the account as not
378            * ready and doesn't display it. */
379           if (!g_hash_table_lookup (data->settings, "password"))
380             {
381               GValue *value;
382               value = tp_g_value_slice_new (G_TYPE_STRING);
383               g_value_set_string (value, "");
384               g_hash_table_insert (data->settings, (gpointer) "password", value);
385             }
386
387           accounts = g_list_prepend (accounts, data);
388         }
389       else
390         empathy_import_account_data_free (data);
391     }
392
393 OUT:
394   xmlFreeDoc (doc);
395   xmlFreeParserCtxt (ctxt);
396
397 FILENAME:
398   g_free (filename);
399
400   return accounts;
401 }
402
403 gboolean
404 empathy_import_pidgin_accounts_to_import (void)
405 {
406   gchar *filename;
407   gboolean out;
408   GFile *file;
409
410   filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
411       NULL);
412   file = g_file_new_for_path (filename);
413   out = g_file_query_exists (file, NULL);
414
415   g_free (filename);
416   g_object_unref (file);
417
418   return out;
419 }