aboutsummaryrefslogtreecommitdiffstats
path: root/src/empathy-import-pidgin.c
blob: 4de9bce9673e9e4415c57d8c8f284d4a13608bb3 (plain) (blame)
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 * Copyright (C) 2008 Collabora Ltd.
 *
 * 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 2 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, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 *
 * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
 * */

#include <config.h>

#include <string.h>
#include <unistd.h>

#include <glib.h>
#include <glib/gstdio.h>
#include <dbus/dbus-protocol.h>
#include <libxml/parser.h>
#include <libxml/tree.h>

#include <telepathy-glib/util.h>
#include <telepathy-glib/dbus.h>

#include "empathy-import-utils.h"
#include "empathy-import-pidgin.h"

#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
#include <libempathy/empathy-debug.h>
#include <libempathy/empathy-utils.h>

#include <libempathy-gtk/empathy-ui-utils.h>

/* Pidgin to CM map */
typedef struct
{
  const gchar *protocol;
  const gchar *pidgin_name;
  const gchar *cm_name;
} PidginCmMapItem;

static PidginCmMapItem pidgin_cm_map[] =
{
  { "msn", "server", "server" },
  { "msn", "port", "port" },

  { "jabber", "connect_server", "server" },
  { "jabber", "port", "port" },
  { "jabber", "require_tls", "require-encryption" },
  { "jabber", "old_ssl", "old-ssl" },

  { "aim", "server", "server" },
  { "aim", "port", "port" },

  { "salut", "first", "first-name" },
  { "salut", "last", "last-name" },
  { "salut", "jid", "jid" },
  { "salut", "email", "email" },

  { "groupwise", "server", "server" },
  { "groupwise", "port", "port" },

  { "icq", "server", "server" },
  { "icq", "port", "port" },

  { "irc", "realname", "fullname" },
  { "irc", "ssl", "use-ssl" },
  { "irc", "port", "port" },

  { "yahoo", "server", "server" },
  { "yahoo", "port", "port" },
  { "yahoo", "xfer_port", "xfer-port" },
  { "yahoo", "ignore_invites", "ignore-invites" },
  { "yahoo", "yahoojp", "yahoojp" },
  { "yahoo", "xferjp_host", "xferjp-host" },
  { "yahoo", "serverjp", "serverjp" },
  { "yahoo", "xfer_host", "xfer-host" },
};

#define PIDGIN_ACCOUNT_TAG_NAME "name"
#define PIDGIN_ACCOUNT_TAG_ACCOUNT "account"
#define PIDGIN_ACCOUNT_TAG_PROTOCOL "protocol"
#define PIDGIN_ACCOUNT_TAG_PASSWORD "password"
#define PIDGIN_ACCOUNT_TAG_SETTINGS "settings"
#define PIDGIN_SETTING_PROP_UI "ui"
#define PIDGIN_SETTING_PROP_NAME "name"
#define PIDGIN_SETTING_PROP_TYPE "type"
#define PIDGIN_PROTOCOL_BONJOUR "bonjour"
#define PIDGIN_PROTOCOL_NOVELL "novell"

static void
import_dialog_pidgin_parse_setting (EmpathyImportAccountData *data,
                                    xmlNodePtr setting)
{
  PidginCmMapItem *item = NULL;
  gchar *tag_name;
  gchar *type = NULL;
  gchar *content;
  guint i;
  GValue *value = NULL;

  /* We can't do anything if the setting don't have a name */
  tag_name = (gchar *) xmlGetProp (setting,
      (xmlChar *) PIDGIN_SETTING_PROP_NAME);
  if (!tag_name)
    return;

  /* Search for the map corresponding to setting we are parsing */
  for (i = 0; i < G_N_ELEMENTS (pidgin_cm_map); i++)
    {
      if (!tp_strdiff (data->protocol, pidgin_cm_map[i].protocol) &&
          !tp_strdiff (tag_name, pidgin_cm_map[i].pidgin_name))
        {
          item = pidgin_cm_map + i;
          break;
        }
    }
  g_free (tag_name);

  /* If we didn't find the item, there is nothing we can do */
  if (!item)
    return;

  type = (gchar *) xmlGetProp (setting, (xmlChar *) PIDGIN_SETTING_PROP_TYPE);
  content = (gchar *) xmlNodeGetContent (setting);

  if (!tp_strdiff (type, "bool"))
    {
      i = (gint) g_ascii_strtod (content, NULL);
      value = tp_g_value_slice_new (G_TYPE_BOOLEAN);
      g_value_set_boolean (value, i != 0);
    }
  else if (!tp_strdiff (type, "int"))
    {
      TpConnectionManager *cm = NULL;
      const TpConnectionManagerProtocol *proto;
      const TpConnectionManagerParam *param;
      const gchar *signature;
      int signature_i;

      if (!empathy_import_protocol_is_supported (data->protocol, &cm))
        return;

      proto = tp_connection_manager_get_protocol (cm, data->protocol);
      param = tp_connection_manager_protocol_get_param (proto, item->cm_name);
      signature = tp_connection_manager_param_get_dbus_signature (param);
      signature_i = (int) (*signature);

      i = (gint) g_ascii_strtod (content, NULL);

      if (signature_i == DBUS_TYPE_INT16 ||
          signature_i == DBUS_TYPE_INT32)
        {
          value = tp_g_value_slice_new (G_TYPE_INT);
          g_value_set_int (value, i);
        }
      else if (signature_i == DBUS_TYPE_UINT16 ||
          signature_i == DBUS_TYPE_UINT32)
        {
          value = tp_g_value_slice_new (G_TYPE_UINT);
          g_value_set_uint (value, (guint) i);
        }
    }
  else if (!tp_strdiff (type, "string"))
    {
      value = tp_g_value_slice_new (G_TYPE_STRING);
      g_value_set_string (value, content);
    }

  if (value)
    g_hash_table_insert (data->settings, (gpointer) item->cm_name, value);

  g_free (type);
  g_free (content);
}

static void
import_dialog_pidgin_handle_settings (EmpathyImportAccountData *data,
                                      xmlNodePtr settings)
{
  xmlNodePtr setting;
  gchar *tag_ui, *name, *type, *content;

  tag_ui = (gchar *) xmlGetProp (settings, (xmlChar *) PIDGIN_SETTING_PROP_UI);

  /* UI settings - fetch the Enabled parameter.
   * The expected value of the ui property is 'gtk-gaim', which looks obsolete,
   * but still valid for 2.7.3.
   */
  if (tag_ui && !tp_strdiff (tag_ui, "gtk-gaim"))
    {
      for (setting = settings->children; setting; setting = setting->next)
        {
          name = (gchar *) xmlGetProp (setting,
              (xmlChar *) PIDGIN_SETTING_PROP_NAME);
          type = (gchar *) xmlGetProp (setting,
              (xmlChar *) PIDGIN_SETTING_PROP_TYPE);
          /* The Enabled parameter is supposed to be boolean.
           * Pidgin name of the setting is 'auto-login'.
           */
          if (!tp_strdiff (name, "auto-login") && !tp_strdiff (type, "bool"))
            {
              content = (gchar *) xmlNodeGetContent (setting);
              data->enabled = (0 != (gint) g_ascii_strtod (content, NULL));
              g_free (content);
            }
          g_free (type);
          g_free (name);
        }
    }
  /* General settings. */
  else
    {
      for (setting = settings->children; setting; setting = setting->next)
        import_dialog_pidgin_parse_setting (data, setting);
    }

  g_free (tag_ui);
}

GList *
empathy_import_pidgin_load (void)
{
  xmlNodePtr rootnode, node, child;
  xmlParserCtxtPtr ctxt;
  xmlDocPtr doc;
  gchar *filename;
  GList *accounts = NULL;

  /* Load pidgin accounts xml */
  ctxt = xmlNewParserCtxt ();
  filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
      NULL);

  if (g_access (filename, R_OK) != 0)
    goto FILENAME;

  doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);

  rootnode = xmlDocGetRootElement (doc);
  if (rootnode == NULL)
    goto OUT;

  for (node = rootnode->children; node; node = node->next)
    {
      EmpathyImportAccountData *data;

      /* If it is not an account node, skip. */
      if (tp_strdiff ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT))
        continue;

      /* Create account data struct */
      data = empathy_import_account_data_new ("Pidgin");

      /* Parse account's child nodes to fill the account data struct */
      for (child = node->children; child; child = child->next)
        {
          GValue *value;

          /* Protocol */
          if (!tp_strdiff ((gchar *) child->name,
              PIDGIN_ACCOUNT_TAG_PROTOCOL))
            {
              xmlChar *content;
              const gchar *protocol;

              content = xmlNodeGetContent (child);

              protocol = (const gchar *) content;

              if (g_str_has_prefix (protocol, "prpl-"))
                protocol += 5;

              if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_BONJOUR))
                data->protocol = g_strdup ("salut");
              else if (!tp_strdiff (protocol, PIDGIN_PROTOCOL_NOVELL))
                data->protocol = g_strdup ("groupwise");
              else
                data->protocol = g_strdup (protocol);

              xmlFree (content);

              if (data->protocol == NULL)
                break;
            }

          /* Username and IRC server. */
          else if (!tp_strdiff ((gchar *) child->name,
              PIDGIN_ACCOUNT_TAG_NAME))
            {
              gchar *name;
              GStrv name_resource = NULL;
              GStrv nick_server = NULL;
              const gchar *username;

              name = (gchar *) xmlNodeGetContent (child);

              /* Split "username/resource" */
              if (g_strrstr (name, "/") != NULL)
                {
                  name_resource = g_strsplit (name, "/", 2);
                  username = name_resource[0];
                }
              else
                username = name;

             /* Split "username@server" if it is an IRC account */
             if (strstr (name, "@") && !tp_strdiff (data->protocol, "irc"))
              {
                nick_server = g_strsplit (name, "@", 2);
                username = nick_server[0];

                /* Add the server setting */
                value = tp_g_value_slice_new (G_TYPE_STRING);
                g_value_set_string (value, nick_server[1]);
                g_hash_table_insert (data->settings, (gpointer) "server", value);
              }

              /* Add the account setting */
              value = tp_g_value_slice_new (G_TYPE_STRING);
              g_value_set_string (value, username);
              g_hash_table_insert (data->settings, (gpointer) "account", value);

              g_strfreev (name_resource);
              g_strfreev (nick_server);
              g_free (name);
            }

          /* Password */
          else if (!tp_strdiff ((gchar *) child->name,
              PIDGIN_ACCOUNT_TAG_PASSWORD))
            {
              gchar *password;

              password = (gchar *) xmlNodeGetContent (child);

              /* Add the password setting */
              value = tp_g_value_slice_new (G_TYPE_STRING);
              g_value_set_string (value, password);
              g_hash_table_insert (data->settings, (gpointer) "password", value);

              g_free (password);
            }

          /* Other settings */
          else if (!tp_strdiff ((gchar *) child->name,
              PIDGIN_ACCOUNT_TAG_SETTINGS))
            import_dialog_pidgin_handle_settings (data, child);
        }

      /* If we have the needed settings, add the account data to the list,
       * otherwise free the data */
      if (data->protocol != NULL && g_hash_table_size (data->settings) > 0)
        {
          /* Special-case XMPP:
           * http://bugzilla.gnome.org/show_bug.cgi?id=579992 */
          if (!tp_strdiff (data->protocol, "jabber"))
            {
              if (EMP_STR_EMPTY (tp_asv_get_string (data->settings, "server")))
                {
                  g_hash_table_remove (data->settings, "port");
                  g_hash_table_remove (data->settings, "server");
                }
            }

          /* If there is no password then MC treats the account as not
           * ready and doesn't display it. */
          if (!g_hash_table_lookup (data->settings, "password"))
            {
              GValue *value;
              value = tp_g_value_slice_new (G_TYPE_STRING);
              g_value_set_string (value, "");
              g_hash_table_insert (data->settings, (gpointer) "password", value);
            }

          accounts = g_list_prepend (accounts, data);
        }
      else
        empathy_import_account_data_free (data);
    }

OUT:
  xmlFreeDoc (doc);
  xmlFreeParserCtxt (ctxt);

FILENAME:
  g_free (filename);

  return accounts;
}

gboolean
empathy_import_pidgin_accounts_to_import (void)
{
  gchar *filename;
  gboolean out;
  GFile *file;

  filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
      NULL);
  file = g_file_new_for_path (filename);
  out = g_file_query_exists (file, NULL);

  g_free (filename);
  g_object_unref (file);

  return out;
}