]> git.0d.be Git - empathy.git/blob - src/empathy-import-pidgin.c
It is a map from pidgin to CM, not MC.
[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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
20  * */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #include <glib.h>
27 #include <glib/gstdio.h>
28 #include <libxml/parser.h>
29 #include <libxml/tree.h>
30
31 #include <libmissioncontrol/mc-account.h>
32 #include <telepathy-glib/util.h>
33
34 #include "empathy-import-dialog.h"
35 #include "empathy-import-pidgin.h"
36
37 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
38 #include <libempathy/empathy-debug.h>
39 #include <libempathy/empathy-utils.h>
40
41 #include <libempathy-gtk/empathy-ui-utils.h>
42
43 /* Pidgin to CM map */
44 typedef struct
45 {
46   gchar *protocol;
47   gchar *pidgin_name;
48   gchar *cm_name;
49 } PidginCmMapItem;
50
51 static PidginCmMapItem pidgin_cm_map[] =
52 {
53   { "msn", "server", "server" },
54   { "msn", "port", "port" },
55
56   { "jabber", "connect_server", "server" },
57   { "jabber", "port", "port" },
58   { "jabber", "require_tls", "require-encryption" },
59   { "jabber", "old_ssl", "old-ssl" },
60
61   { "aim", "server", "server" },
62   { "aim", "port", "port" },
63
64   { "salut", "first", "first-name" },
65   { "salut", "last", "last-name" },
66   { "salut", "jid", "jid" },
67   { "salut", "email", "email" },
68
69   { "groupwise", "server", "server" },
70   { "groupwise", "port", "port" },
71
72   { "icq", "server", "server" },
73   { "icq", "port", "port" },
74
75   { "irc", "realname", "fullname" },
76   { "irc", "ssl", "use-ssl" },
77   { "irc", "port", "port" },
78
79   { "yahoo", "server", "server" },
80   { "yahoo", "port", "port" },
81   { "yahoo", "xfer_port", "xfer-port" },
82   { "yahoo", "ignore_invites", "ignore-invites" },
83   { "yahoo", "yahoojp", "yahoojp" },
84   { "yahoo", "xferjp_host", "xferjp-host" },
85   { "yahoo", "serverjp", "serverjp" },
86   { "yahoo", "xfer_host", "xfer-host" },
87 };
88
89 #define PIDGIN_ACCOUNT_TAG_NAME "name"
90 #define PIDGIN_ACCOUNT_TAG_ACCOUNT "account"
91 #define PIDGIN_ACCOUNT_TAG_PROTOCOL "protocol"
92 #define PIDGIN_ACCOUNT_TAG_PASSWORD "password"
93 #define PIDGIN_ACCOUNT_TAG_SETTINGS "settings"
94 #define PIDGIN_SETTING_PROP_TYPE "type"
95 #define PIDGIN_PROTOCOL_BONJOUR "bonjour"
96 #define PIDGIN_PROTOCOL_NOVELL "novell"
97
98 static void
99 import_dialog_pidgin_parse_setting (EmpathyImportAccountData *data,
100                                     xmlNodePtr setting)
101 {
102   PidginCmMapItem *item = NULL;
103   gchar *tag_name;
104   gchar *type = NULL;
105   gchar *content;
106   gint i;
107   GValue *value = NULL;
108
109   /* We can't do anything if the setting don't have a name */
110   tag_name = (gchar *) xmlGetProp (setting, PIDGIN_ACCOUNT_TAG_NAME);
111   if (!tag_name)
112     return;
113
114   /* Search for the map corresponding to setting we are parsing */
115   for (i = 0; i < G_N_ELEMENTS (pidgin_cm_map); i++)
116     {
117       if (!tp_strdiff (mc_profile_get_protocol_name (data->profile),
118             pidgin_cm_map[i].protocol) &&
119           !tp_strdiff (tag_name, pidgin_cm_map[i].pidgin_name))
120         {
121           item = pidgin_cm_map + i;
122           break;
123         }
124     }
125   g_free (tag_name);
126
127   /* If we didn't find the item, there is nothing we can do */
128   if (!item)
129     return;
130
131   type = (gchar *) xmlGetProp (setting, PIDGIN_SETTING_PROP_TYPE);
132   content = (gchar *) xmlNodeGetContent (setting);
133
134   if (!tp_strdiff (type, "bool"))
135     {
136       i = (gint) g_ascii_strtod (content, NULL);
137       value = tp_g_value_slice_new (G_TYPE_BOOLEAN);
138       g_value_set_boolean (value, i != 0);
139     }
140   else if (!tp_strdiff (type, "int"))
141     {
142       i = (gint) g_ascii_strtod (content, NULL);
143       value = tp_g_value_slice_new (G_TYPE_INT);
144       g_value_set_int (value, i);
145     }
146   else if (!tp_strdiff (type, "string"))
147     {
148       value = tp_g_value_slice_new (G_TYPE_STRING);
149       g_value_set_string (value, content);
150     }
151
152   if (value)
153     g_hash_table_insert (data->settings, item->cm_name, value);
154
155   g_free (type);
156   g_free (content);
157 }
158
159 GList *
160 empathy_import_pidgin_load (void)
161 {
162   xmlNodePtr rootnode, node, child, setting;
163   xmlParserCtxtPtr ctxt;
164   xmlDocPtr doc;
165   gchar *filename;
166   GList *accounts = NULL;
167
168   /* Load pidgin accounts xml */
169   ctxt = xmlNewParserCtxt ();
170   filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
171       NULL);
172
173   if (g_access (filename, R_OK) != 0)
174     goto FILENAME;
175
176   doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
177
178   rootnode = xmlDocGetRootElement (doc);
179   if (rootnode == NULL)
180     goto OUT;
181
182   for (node = rootnode->children; node; node = node->next)
183     {
184       EmpathyImportAccountData *data;
185
186       /* If it is not an account node, skip. */
187       if (tp_strdiff ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT))
188         continue;
189
190       /* Create account data struct */
191       data = empathy_import_account_data_new ();
192
193       /* Parse account's child nodes to fill the account data struct */
194       for (child = node->children; child; child = child->next)
195         {
196           GValue *value;
197
198           /* Protocol */
199           if (!tp_strdiff ((gchar *) child->name,
200               PIDGIN_ACCOUNT_TAG_PROTOCOL))
201             {
202               gchar *content;
203               const gchar *protocol;
204
205               protocol = content = (gchar *) xmlNodeGetContent (child);
206
207               if (g_str_has_prefix (protocol, "prpl-"))
208                 protocol += 5;
209
210               if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_BONJOUR))
211                 protocol = "salut";
212               else if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_NOVELL))
213                 protocol = "groupwise";
214
215               data->profile = mc_profile_lookup (protocol);
216               g_free (content);
217
218               if (data->profile == NULL)
219                 break;
220             }
221
222           /* Username and IRC server. */
223           else if (!tp_strdiff ((gchar *) child->name,
224               PIDGIN_ACCOUNT_TAG_NAME))
225             {
226               gchar *name;
227               GStrv name_resource = NULL;
228               GStrv nick_server = NULL;
229               const gchar *username;
230
231               name = (gchar *) xmlNodeGetContent (child);
232
233               /* Split "username/resource" */
234               if (g_strrstr (name, "/") != NULL)
235                 {
236                   name_resource = g_strsplit (name, "/", 2);
237                   username = name_resource[0];
238                 }
239               else
240                 username = name;
241
242              /* Split "username@server" if it is an IRC account */
243              if (strstr (name, "@") && !tp_strdiff (
244                    mc_profile_get_protocol_name (data->profile), "irc"))
245               {
246                 nick_server = g_strsplit (name, "@", 2);
247                 username = nick_server[0];
248
249                 /* Add the server setting */
250                 value = tp_g_value_slice_new (G_TYPE_STRING);
251                 g_value_set_string (value, nick_server[1]);
252                 g_hash_table_insert (data->settings, "server", value);
253               }
254
255               /* Add the account setting */
256               value = tp_g_value_slice_new (G_TYPE_STRING);
257               g_value_set_string (value, username);
258               g_hash_table_insert (data->settings, "account", value);
259
260               g_strfreev (name_resource);
261               g_strfreev (nick_server);
262               g_free (name);
263             }
264
265           /* Password */
266           else if (!tp_strdiff ((gchar *) child->name,
267               PIDGIN_ACCOUNT_TAG_PASSWORD))
268             {
269               gchar *password;
270
271               password = (gchar *) xmlNodeGetContent (child);
272
273               /* Add the password setting */
274               value = tp_g_value_slice_new (G_TYPE_STRING);
275               g_value_set_string (value, password);
276               g_hash_table_insert (data->settings, "password", value);
277
278               g_free (password);
279             }
280
281           /* Other settings */
282           else if (!tp_strdiff ((gchar *) child->name,
283               PIDGIN_ACCOUNT_TAG_SETTINGS))
284               for (setting = child->children; setting; setting = setting->next)
285                 import_dialog_pidgin_parse_setting (data, setting);
286         }
287
288       /* If we have the needed settings, add the account data to the list,
289        * otherwise free the data */
290       if (data->profile != NULL && g_hash_table_size (data->settings) > 0)
291         accounts = g_list_prepend (accounts, data);
292       else
293         empathy_import_account_data_free (data);
294     }
295
296 OUT:
297   xmlFreeDoc(doc);
298   xmlFreeParserCtxt (ctxt);
299
300 FILENAME:
301   g_free (filename);
302
303   return accounts;
304 }
305