Use GSList functions instead of manual iterations
[pidgin-git.git] / libpurple / protocols / bonjour / buddy.c
blob70b52dc105496b4373c9b2fa2d6371ecea82e0b2
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301, USA.
17 #include <glib.h>
18 #include <stdlib.h>
20 #include "internal.h"
21 #include <purple.h>
23 #include "buddy.h"
24 #include "bonjour.h"
25 #include "mdns_interface.h"
27 /**
28 * Creates a new buddy.
30 BonjourBuddy *
31 bonjour_buddy_new(const gchar *name, PurpleAccount* account)
33 BonjourBuddy *buddy = g_new0(BonjourBuddy, 1);
35 buddy->account = account;
36 buddy->name = g_strdup(name);
38 _mdns_init_buddy(buddy);
40 return buddy;
43 void
44 clear_bonjour_buddy_values(BonjourBuddy *buddy)
46 g_clear_pointer(&buddy->first, g_free);
47 g_clear_pointer(&buddy->email, g_free);
48 g_clear_pointer(&buddy->ext, g_free);
49 g_clear_pointer(&buddy->jid, g_free);
50 g_clear_pointer(&buddy->last, g_free);
51 g_clear_pointer(&buddy->msg, g_free);
52 g_clear_pointer(&buddy->nick, g_free);
53 g_clear_pointer(&buddy->node, g_free);
54 g_clear_pointer(&buddy->phsh, g_free);
55 g_clear_pointer(&buddy->status, g_free);
56 g_clear_pointer(&buddy->vc, g_free);
57 g_clear_pointer(&buddy->ver, g_free);
58 g_clear_pointer(&buddy->AIM, g_free);
61 void
62 set_bonjour_buddy_value(BonjourBuddy* buddy, const char *record_key, const char *value, guint32 len){
63 gchar **fld = NULL;
65 g_return_if_fail(record_key != NULL);
67 if (purple_strequal(record_key, "1st"))
68 fld = &buddy->first;
69 else if(purple_strequal(record_key, "email"))
70 fld = &buddy->email;
71 else if(purple_strequal(record_key, "ext"))
72 fld = &buddy->ext;
73 else if(purple_strequal(record_key, "jid"))
74 fld = &buddy->jid;
75 else if(purple_strequal(record_key, "last"))
76 fld = &buddy->last;
77 else if(purple_strequal(record_key, "msg"))
78 fld = &buddy->msg;
79 else if(purple_strequal(record_key, "nick"))
80 fld = &buddy->nick;
81 else if(purple_strequal(record_key, "node"))
82 fld = &buddy->node;
83 else if(purple_strequal(record_key, "phsh"))
84 fld = &buddy->phsh;
85 else if(purple_strequal(record_key, "status"))
86 fld = &buddy->status;
87 else if(purple_strequal(record_key, "vc"))
88 fld = &buddy->vc;
89 else if(purple_strequal(record_key, "ver"))
90 fld = &buddy->ver;
91 else if(purple_strequal(record_key, "AIM"))
92 fld = &buddy->AIM;
94 if(fld == NULL)
95 return;
97 g_free(*fld);
98 *fld = NULL;
99 *fld = g_strndup(value, len);
103 * Check if all the compulsory buddy data is present.
105 gboolean
106 bonjour_buddy_check(BonjourBuddy *buddy)
108 if (buddy->account == NULL)
109 return FALSE;
111 if (buddy->name == NULL)
112 return FALSE;
114 return TRUE;
118 * If the buddy does not yet exist, then create it and add it to
119 * our buddy list. In either case we set the correct status for
120 * the buddy.
122 void
123 bonjour_buddy_add_to_purple(BonjourBuddy *bonjour_buddy, PurpleBuddy *buddy)
125 PurpleGroup *group;
126 PurpleAccount *account = bonjour_buddy->account;
127 const char *status_id, *old_hash, *new_hash, *name;
129 /* Translate between the Bonjour status and the Purple status */
130 if (bonjour_buddy->status != NULL && g_ascii_strcasecmp("dnd", bonjour_buddy->status) == 0)
131 status_id = BONJOUR_STATUS_ID_AWAY;
132 else
133 status_id = BONJOUR_STATUS_ID_AVAILABLE;
136 * TODO: Figure out the idle time by getting the "away"
137 * field from the DNS SD.
140 /* Make sure the Bonjour group exists in our buddy list */
141 group = purple_blist_find_group(BONJOUR_GROUP_NAME); /* Use the buddy's domain, instead? */
142 if (group == NULL) {
143 group = purple_group_new(BONJOUR_GROUP_NAME);
144 purple_blist_add_group(group, NULL);
147 /* Make sure the buddy exists in our buddy list */
148 if (buddy == NULL)
149 buddy = purple_blist_find_buddy(account, bonjour_buddy->name);
151 if (buddy == NULL) {
152 buddy = purple_buddy_new(account, bonjour_buddy->name, NULL);
153 purple_blist_node_set_transient(PURPLE_BLIST_NODE(buddy), TRUE);
154 purple_blist_add_buddy(buddy, NULL, group, NULL);
157 name = purple_buddy_get_name(buddy);
158 purple_buddy_set_protocol_data(buddy, bonjour_buddy);
160 /* Create the alias for the buddy using the first and the last name */
161 if (bonjour_buddy->nick && *bonjour_buddy->nick)
162 purple_serv_got_alias(purple_account_get_connection(account), name, bonjour_buddy->nick);
163 else {
164 gchar *alias = NULL;
165 const char *first, *last;
166 first = bonjour_buddy->first;
167 last = bonjour_buddy->last;
168 if ((first && *first) || (last && *last))
169 alias = g_strdup_printf("%s%s%s",
170 (first && *first ? first : ""),
171 (first && *first && last && *last ? " " : ""),
172 (last && *last ? last : ""));
173 purple_serv_got_alias(purple_account_get_connection(account), name, alias);
174 g_free(alias);
177 /* Set the user's status */
178 if (bonjour_buddy->msg != NULL)
179 purple_protocol_got_user_status(account, name, status_id,
180 "message", bonjour_buddy->msg, NULL);
181 else
182 purple_protocol_got_user_status(account, name, status_id, NULL);
184 purple_protocol_got_user_idle(account, name, FALSE, 0);
186 /* TODO: Because we don't save Bonjour buddies in blist.xml,
187 * we will always have to look up the buddy icon at login time.
188 * I think we should figure out a way to do something about this. */
190 /* Deal with the buddy icon */
191 old_hash = purple_buddy_icons_get_checksum_for_user(buddy);
192 new_hash = (bonjour_buddy->phsh && *(bonjour_buddy->phsh)) ? bonjour_buddy->phsh : NULL;
193 if (new_hash && !purple_strequal(old_hash, new_hash)) {
194 /* Look up the new icon data */
195 /* TODO: Make sure the hash assigned to the retrieved buddy icon is the same
196 * as what we looked up. */
197 bonjour_dns_sd_retrieve_buddy_icon(bonjour_buddy);
198 } else if (!new_hash)
199 purple_buddy_icons_set_for_user(account, name, NULL, 0, NULL);
203 * The buddy has signed off Bonjour.
204 * If the buddy is being saved, mark as offline, otherwise delete
206 void bonjour_buddy_signed_off(PurpleBuddy *pb) {
207 if (purple_blist_node_is_transient(PURPLE_BLIST_NODE(pb))) {
208 purple_account_remove_buddy(purple_buddy_get_account(pb), pb, NULL);
209 purple_blist_remove_buddy(pb);
210 } else {
211 purple_protocol_got_user_status(purple_buddy_get_account(pb),
212 purple_buddy_get_name(pb), "offline", NULL);
213 bonjour_buddy_delete(purple_buddy_get_protocol_data(pb));
214 purple_buddy_set_protocol_data(pb, NULL);
219 * We got the buddy icon data; deal with it
221 void bonjour_buddy_got_buddy_icon(BonjourBuddy *buddy, gconstpointer data, gsize len) {
222 /* Recalculate the hash instead of using the current phsh to make sure it is accurate for the icon. */
223 gchar *hash;
225 if (data == NULL || len == 0)
226 return;
228 hash = g_compute_checksum_for_data(G_CHECKSUM_SHA1, data, len);
230 purple_debug_info("bonjour", "Got buddy icon for %s icon hash='%s' phsh='%s'.\n", buddy->name,
231 hash, buddy->phsh ? buddy->phsh : "(null)");
233 purple_buddy_icons_set_for_user(buddy->account, buddy->name,
234 g_memdup(data, len), len, hash);
236 g_free(hash);
240 * Deletes a buddy from memory.
242 void
243 bonjour_buddy_delete(BonjourBuddy *buddy)
245 g_free(buddy->name);
246 g_slist_free_full(buddy->ips, g_free);
247 g_free(buddy->first);
248 g_free(buddy->phsh);
249 g_free(buddy->status);
250 g_free(buddy->email);
251 g_free(buddy->last);
252 g_free(buddy->jid);
253 g_free(buddy->AIM);
254 g_free(buddy->vc);
255 g_free(buddy->msg);
256 g_free(buddy->ext);
257 g_free(buddy->nick);
258 g_free(buddy->node);
259 g_free(buddy->ver);
261 bonjour_jabber_close_conversation(buddy->conversation);
262 buddy->conversation = NULL;
264 /* Clean up any mdns implementation data */
265 _mdns_delete_buddy(buddy);
267 g_free(buddy);