File: alias_xml_files.c

package info (click to toggle)
purple-plugin-pack 2.6.3-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,488 kB
  • sloc: ansic: 33,222; sh: 10,198; makefile: 1,391; python: 400
file content (301 lines) | stat: -rw-r--r-- 8,848 bytes parent folder | download | duplicates (6)
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
/*
 * Purple Plugin Pack
 * Copyright (C) 2003-2008
 * See ../AUTHORS for a list of all authors
 *
 * listhandler: Provides importing, exporting, and copying functions
 * 				for accounts' buddy lists.
 *
 * 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 Street, Fifth Floor, Boston, MA 02111-1301, USA.
 */

#include "listhandler.h"
#include "alias_xml_files.h"

static const gchar *target_prpl_id = NULL;
static gchar *file_contents = NULL, *filename = NULL;
static gsize length;
static PurpleAccount *target_account = NULL, *source_account = NULL;
static PurpleBuddyList *buddies = NULL;
static PurpleConnection *gc = NULL;
static xmlnode *root = NULL;

static gboolean
lh_import_filter(PurpleAccount *account)
{
	const gchar *prpl_id = purple_account_get_protocol_id(account);

	if(!prpl_id)
		return FALSE;

	if(!strcmp(prpl_id, target_prpl_id))
		return TRUE;

	return FALSE;
}

static void
lh_alist_import(xmlnode *alist)
{
	xmlnode *buddy = NULL;
	PurpleBuddy *temp = NULL;

	if(alist) {
		/* get the first buddy in the alias list */
		buddy = xmlnode_get_child(alist, "buddy");

		while(buddy) {
			temp = purple_find_buddy(target_account, xmlnode_get_attrib(buddy, "screenname") );

			if(temp != NULL) {
				purple_blist_alias_buddy(temp, xmlnode_get_attrib(buddy, "alias") );
				purple_debug_info("listhandler: import", "Added alias for %s\n",
					xmlnode_get_attrib(buddy, "screenname") );
			}	
			
			buddy = xmlnode_get_next_twin(buddy);
		}
	}
}

static void
lh_alist_build_alias_tree(xmlnode *parent)
{
	/* root of tree group contact buddy */
	PurpleBlistNode *root = buddies->root, *g = NULL, *c = NULL, *b = NULL;
	xmlnode *buddy = NULL;
	PurpleBuddy *tmpbuddy = NULL;
	const char *tmpalias = NULL, *tmpname = NULL;

	/* iterate through the groups */
	for(g = root; g; g = g->next) {
		if(PURPLE_BLIST_NODE_IS_GROUP(g)) {
			const char *group_name = ((PurpleGroup *)g)->name;
			
			purple_debug_info("listhandler: export", "Node is group.  Name is: %s\n",
					group_name);

			/* iterate through the contacts */
			for(c = g->child; c; c= c->next) {
				if(PURPLE_BLIST_NODE_IS_CONTACT(c)) {
					purple_debug_info("listhandler: export",
							"Node is contact.  Will parse its children.\n");

					/* iterate through the buddies */
					for(b = c->child; b && PURPLE_BLIST_NODE_IS_BUDDY(b); b = b->next) {
						tmpbuddy = (PurpleBuddy *)b;
						if(purple_buddy_get_account(tmpbuddy) == source_account) {
							tmpalias = purple_buddy_get_alias_only(tmpbuddy);
							if (tmpalias != NULL) {
								tmpname = purple_buddy_get_name(tmpbuddy);
								buddy = xmlnode_new_child(parent, "buddy");
								xmlnode_set_attrib(buddy, "screenname", tmpname);
								xmlnode_set_attrib(buddy, "alias", tmpalias);
							}
						}
					}
				}
			}
		}
	}

	return;
}

static void
lh_alist_build_config_tree(xmlnode *parent)
{ /* we may need to expand this area later for future feature ehnancements */

	xmlnode_set_attrib(xmlnode_new_child(parent, "config-version"), "version", "2");
	xmlnode_set_attrib(xmlnode_new_child(parent, "config-type"), "type", "alias-list");
	xmlnode_set_attrib(xmlnode_new_child(parent, "prpl"), "id",
			purple_account_get_protocol_id(source_account));
	xmlnode_set_attrib(xmlnode_new_child(parent, "source"), "account",
			purple_account_get_username(source_account));

	return;
}

static xmlnode *
lh_alist_build_tree(void)
{
	xmlnode *root_node = xmlnode_new("exported_alias_list");

	lh_alist_build_config_tree(xmlnode_new_child(root_node, "config"));
	lh_alist_build_alias_tree(xmlnode_new_child(root_node, "alist"));

	return root_node;
}

static void
lh_alist_export_request_cb(void *user_data, const char *filename)
{
	FILE *export = fopen(filename, "w");

	if(export) {
		int xmlstrlen = 0;
		xmlnode *tree = lh_alist_build_tree();
		char *xmlstring = xmlnode_to_formatted_str(tree, &xmlstrlen);

		purple_debug_info("listhandler: export",
				"XML tree built and converted to string.  String is:\n\n%s\n",
				xmlstring);

		fprintf(export, "%s\n", xmlstring);
		
		fclose(export);
		
		g_free(xmlstring);
		xmlnode_free(tree);
	} else
		purple_debug_info("listhandler: export", "Can't save file %s\n",
				filename ? filename : "NULL");
	
	return;
}

static void
lh_alist_export_cb(void *ignored, PurpleRequestFields *fields)
{
	/* get the source account from the dialog we requested */
	source_account = purple_request_fields_get_account(fields, "generic_source_acct");

	/* get the connection from the account */
	gc = purple_account_get_connection(source_account);

	/* this grabs the purple buddy list, which will be walked thru later */
	buddies = purple_get_blist();

	if(buddies)
		purple_request_file(listhandler, _("Save Generic .alist File"), NULL,
				TRUE, G_CALLBACK(lh_alist_export_request_cb), NULL,
				source_account, NULL, NULL, NULL);
	else
		purple_debug_info("listhandler: export alias", "blist not returned\n");

	return;
}

void
lh_alist_export_action_cb(PurplePluginAction *action)
{
	PurpleRequestFields *request;
	PurpleRequestFieldGroup *group;
	PurpleRequestField *field;

	request = purple_request_fields_new();

	group = purple_request_field_group_new(NULL);
	purple_request_fields_add_group(request, group);

	field = purple_request_field_account_new("generic_source_acct", _("Account"), NULL);
	purple_request_field_set_required(field, TRUE);
	purple_request_field_account_set_show_all(field, TRUE);
	purple_request_field_group_add_field(group, field);

	purple_request_fields(purple_get_blist(), _("Listhandler - Exporting"),
						_("Choose the account to export from:"), NULL, request,
						_("_Export"), G_CALLBACK(lh_alist_export_cb), _("_Cancel"),
						NULL, NULL, NULL, NULL, NULL);

	return;
}

static void
lh_alist_import_target_request_cb(void *ignored, PurpleRequestFields *fields)
{
	/* get the target account */
	target_account = purple_request_fields_get_account(fields, "generic_target_acct");

	purple_debug_info("listhandler: import",
			"Got the target account and its connection.\n");

	purple_debug_info("listhandler: import", "Parsing Alias List in XML and setting aliases \n");

	lh_alist_import(xmlnode_get_child(root, "alist"));

	purple_debug_info("listhandler: import", "Finished setting aliases.  "
			"Freeing allocated memory.\n");

	xmlnode_free(root);
}

static void
lh_alist_import_target_request(void)
{
	PurpleRequestFields *request;
	PurpleRequestFieldGroup *group;
	PurpleRequestField *field;
	GError *error = NULL;

	/* we need to make sure which prpl this list came from so we
	 * can filter the accounts list before showing it */

	/* read the file and get the root xml node within it*/
	g_file_get_contents(filename, &file_contents, &length, &error);
	root = xmlnode_from_str(file_contents, length);

	target_prpl_id = xmlnode_get_attrib(xmlnode_get_child(xmlnode_get_child(root, "config"),
				"prpl"), "id");

	purple_debug_info("listhandler: import", "Beginning Request API calls\n");

	request = purple_request_fields_new();

	group = purple_request_field_group_new(NULL);
	purple_request_fields_add_group(request, group);

	field = purple_request_field_account_new("generic_target_acct", _("Account"), NULL);
	purple_request_field_account_set_filter(field, lh_import_filter);
	purple_request_field_set_required(field, TRUE);
	purple_request_field_group_add_field(group, field);

	purple_request_fields(purple_get_blist(), _("Listhandler - Importing"),
						_("Choose the account to import to:"), NULL, request,
						_("_Import"),
						G_CALLBACK(lh_alist_import_target_request_cb),
						_("_Cancel"), NULL, NULL, NULL, NULL, NULL);

	purple_debug_info("listhandler: import", "Ending Request API calls\n");

	g_free(filename);

	return;
}

static void
lh_alist_import_request_cb(void *user_data, const char *file)
{
	purple_debug_info("listhandler: import", "Beginning import\n");

	if(file) {
		filename = g_strdup(file);

		lh_alist_import_target_request();
	}
}


void
lh_alist_import_action_cb(PurplePluginAction *action)
{
	purple_debug_info("listhandler: import", "Requesting the file.\n");

	purple_request_file(listhandler, _("Choose A Generic Buddy List File To Import"),
			NULL, FALSE, G_CALLBACK(lh_alist_import_request_cb),
			NULL, NULL, NULL, NULL, NULL);
	
	return;
}