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.
25 #include "mdns_interface.h"
28 * Creates a new buddy.
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
);
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
);
62 set_bonjour_buddy_value(BonjourBuddy
* buddy
, const char *record_key
, const char *value
, guint32 len
){
65 g_return_if_fail(record_key
!= NULL
);
67 if (purple_strequal(record_key
, "1st"))
69 else if(purple_strequal(record_key
, "email"))
71 else if(purple_strequal(record_key
, "ext"))
73 else if(purple_strequal(record_key
, "jid"))
75 else if(purple_strequal(record_key
, "last"))
77 else if(purple_strequal(record_key
, "msg"))
79 else if(purple_strequal(record_key
, "nick"))
81 else if(purple_strequal(record_key
, "node"))
83 else if(purple_strequal(record_key
, "phsh"))
85 else if(purple_strequal(record_key
, "status"))
87 else if(purple_strequal(record_key
, "vc"))
89 else if(purple_strequal(record_key
, "ver"))
91 else if(purple_strequal(record_key
, "AIM"))
99 *fld
= g_strndup(value
, len
);
103 * Check if all the compulsory buddy data is present.
106 bonjour_buddy_check(BonjourBuddy
*buddy
)
108 if (buddy
->account
== NULL
)
111 if (buddy
->name
== NULL
)
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
123 bonjour_buddy_add_to_purple(BonjourBuddy
*bonjour_buddy
, PurpleBuddy
*buddy
)
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
;
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? */
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 */
149 buddy
= purple_blist_find_buddy(account
, bonjour_buddy
->name
);
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
);
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
);
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
);
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
);
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. */
225 if (data
== NULL
|| len
== 0)
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
);
240 * Deletes a buddy from memory.
243 bonjour_buddy_delete(BonjourBuddy
*buddy
)
246 g_slist_free_full(buddy
->ips
, g_free
);
247 g_free(buddy
->first
);
249 g_free(buddy
->status
);
250 g_free(buddy
->email
);
261 bonjour_jabber_close_conversation(buddy
->conversation
);
262 buddy
->conversation
= NULL
;
264 /* Clean up any mdns implementation data */
265 _mdns_delete_buddy(buddy
);