1 /**
2  * @file buddylist.c
3  *
4  * purple
5  *
6  * Copyright (C) 2005  Bartosz Oler <bartosz@bzimage.us>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
21  */
22 
23 
24 #include <libgadu.h>
25 
26 #include "gg.h"
27 #include "gg-utils.h"
28 #include "buddylist.h"
29 
30 #define F_FIRSTNAME 0
31 #define F_LASTNAME 1
32 /* #define F_ 2 */
33 #define F_NICKNAME 3
34 #define F_PHONE 4
35 #define F_GROUP 5
36 #define F_UIN 6
37 
38 /* void ggp_buddylist_send(PurpleConnection *gc) {{{ */
ggp_buddylist_send(PurpleConnection * gc)39 void ggp_buddylist_send(PurpleConnection *gc)
40 {
41 	GGPInfo *info = gc->proto_data;
42 	PurpleAccount *account = purple_connection_get_account(gc);
43 	GSList *buddies;
44 	uin_t *userlist;
45 	gchar *types;
46 	int i = 0, ret = 0;
47 	int size;
48 
49 	buddies = purple_find_buddies(account, NULL);
50 
51 	size = g_slist_length(buddies);
52 	userlist = g_new(uin_t, size);
53 	types    = g_new(gchar, size);
54 
55 	for (buddies = purple_find_buddies(account, NULL); buddies;
56 			buddies = g_slist_delete_link(buddies, buddies), ++i)
57 	{
58 		PurpleBuddy *buddy = buddies->data;
59 		const gchar *name = purple_buddy_get_name(buddy);
60 
61 		userlist[i] = ggp_str_to_uin(name);
62 		types[i]    = GG_USER_NORMAL;
63 		purple_debug_info("gg", "ggp_buddylist_send: adding %d\n",
64 		                  userlist[i]);
65 	}
66 
67 	ret = gg_notify_ex(info->session, userlist, types, size);
68 	purple_debug_info("gg", "send: ret=%d; size=%d\n", ret, size);
69 
70 	if (userlist) {
71 		g_free(userlist);
72 		g_free(types);
73 	}
74 }
75 /* }}} */
76 
77 /* void ggp_buddylist_load(PurpleConnection *gc, char *buddylist) {{{ */
ggp_buddylist_load(PurpleConnection * gc,char * buddylist)78 void ggp_buddylist_load(PurpleConnection *gc, char *buddylist)
79 {
80 	PurpleBuddy *buddy;
81 	PurpleGroup *group;
82 	gchar **users_tbl;
83 	int i;
84 	char *utf8buddylist = charset_convert(buddylist, "CP1250", "UTF-8");
85 
86 	/* Don't limit the number of records in a buddylist. */
87 	users_tbl = g_strsplit(utf8buddylist, "\r\n", -1);
88 
89 	for (i = 0; users_tbl[i] != NULL; i++) {
90 		gchar **data_tbl;
91 		gchar *name, *show, *g;
92 
93 		if (strlen(users_tbl[i]) == 0)
94 			continue;
95 
96 		data_tbl = g_strsplit(users_tbl[i], ";", 8);
97 		if (ggp_array_size(data_tbl) < 8) {
98 			purple_debug_warning("gg",
99 				"Something is wrong on line %d of the buddylist. Skipping.\n",
100 				i + 1);
101 			continue;
102 		}
103 
104 		show = data_tbl[F_NICKNAME];
105 		name = data_tbl[F_UIN];
106 		if ('\0' == *name || !atol(name)) {
107 			purple_debug_warning("gg",
108 				"Identifier on line %d of the buddylist is not a number. Skipping.\n",
109 				i + 1);
110 			continue;
111 		}
112 
113 		if ('\0' == *show) {
114 			show = name;
115 		}
116 
117 		purple_debug_info("gg", "got buddy: name=%s; show=%s\n", name, show);
118 
119 		if (purple_find_buddy(purple_connection_get_account(gc), name)) {
120 			g_strfreev(data_tbl);
121 			continue;
122 		}
123 
124 		g = g_strdup("Gadu-Gadu");
125 
126 		if ('\0' != *(data_tbl[F_GROUP])) {
127 			/* XXX: Probably buddy should be added to all the groups. */
128 			/* Hard limit to at most 50 groups */
129 			gchar **group_tbl = g_strsplit(data_tbl[F_GROUP], ",", 50);
130 			if (ggp_array_size(group_tbl) > 0) {
131 				g_free(g);
132 				g = g_strdup(group_tbl[0]);
133 			}
134 			g_strfreev(group_tbl);
135 		}
136 
137 		buddy = purple_buddy_new(purple_connection_get_account(gc), name,
138 					 strlen(show) ? show : NULL);
139 
140 		if (!(group = purple_find_group(g))) {
141 			group = purple_group_new(g);
142 			purple_blist_add_group(group, NULL);
143 		}
144 
145 		purple_blist_add_buddy(buddy, NULL, group, NULL);
146 		g_free(g);
147 
148 		g_strfreev(data_tbl);
149 	}
150 	g_strfreev(users_tbl);
151 	g_free(utf8buddylist);
152 
153 	ggp_buddylist_send(gc);
154 }
155 /* }}} */
156 
157 /* char *ggp_buddylist_dump(PurpleAccount *account) {{{ */
ggp_buddylist_dump(PurpleAccount * account)158 char *ggp_buddylist_dump(PurpleAccount *account)
159 {
160 	GSList *buddies;
161 	GString *buddylist = g_string_sized_new(1024);
162 	char *ptr;
163 
164 	for (buddies = purple_find_buddies(account, NULL); buddies;
165 			buddies = g_slist_delete_link(buddies, buddies)) {
166 		PurpleBuddy *buddy = buddies->data;
167 		PurpleGroup *group = purple_buddy_get_group(buddy);
168 		const char *bname = purple_buddy_get_name(buddy);
169 		const char *gname = purple_group_get_name(group);
170 		const char *alias = purple_buddy_get_alias(buddy);
171 
172 		if (alias == NULL)
173 			alias = bname;
174 
175 		g_string_append_printf(buddylist,
176 				"%s;%s;%s;%s;%s;%s;%s;%s%s\r\n",
177 				alias, alias, alias, alias,
178 				"", gname, bname, "", "");
179 	}
180 
181 	ptr = charset_convert(buddylist->str, "UTF-8", "CP1250");
182 	g_string_free(buddylist, TRUE);
183 	return ptr;
184 }
185 /* }}} */
186 
187 
188 /* vim: set ts=8 sts=0 sw=8 noet: */
189