Mercurial > pidgin
annotate src/protocols/bonjour/bonjour.c @ 11933:1b0c83666f98
[gaim-migrate @ 14224]
The mixed whitespace in this file was confusing Evan. I also cleaned up a couple other stylistic things.
committer: Tailor Script <tailor@pidgin.im>
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Mon, 31 Oct 2005 20:33:13 +0000 |
parents | 0d9c6d2ad64b |
children | bf4fe41d5e94 |
rev | line source |
---|---|
11477 | 1 /* |
2 * gaim - Bonjour Protocol Plugin | |
3 * | |
4 * Gaim is the legal property of its developers, whose names are too numerous | |
5 * to list here. Please refer to the COPYRIGHT file distributed with this | |
6 * source distribution. | |
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 */ | |
22 #include <glib.h> | |
23 | |
24 #include "internal.h" | |
25 #include "account.h" | |
26 #include "accountopt.h" | |
27 #include "version.h" | |
28 #include "debug.h" | |
29 | |
30 #include "bonjour.h" | |
31 #include "dns_sd.h" | |
32 #include "jabber.h" | |
33 #include "buddy.h" | |
34 | |
11691 | 35 static void |
36 bonjour_removeallfromlocal(GaimConnection *gc) | |
37 { | |
38 GaimAccount *account = gaim_connection_get_account(gc); | |
39 GaimBuddyList *blist; | |
40 GaimBlistNode *gnode, *cnode, *bnode; | |
41 GaimBuddy *buddy; | |
42 | |
43 blist = gaim_get_blist(); | |
44 if (blist == NULL) | |
45 return; | |
46 | |
47 /* Go through and remove all buddies that belong to this account */ | |
48 for (gnode = blist->root; gnode; gnode = gnode->next) | |
49 { | |
50 if (!GAIM_BLIST_NODE_IS_GROUP(gnode)) | |
51 continue; | |
52 for (cnode = gnode->child; cnode; cnode = cnode->next) | |
53 { | |
54 if (!GAIM_BLIST_NODE_IS_CONTACT(cnode)) | |
55 continue; | |
56 for (bnode = cnode->child; bnode; bnode = bnode->next) | |
57 { | |
58 if (!GAIM_BLIST_NODE_IS_BUDDY(bnode)) | |
59 continue; | |
60 buddy = (GaimBuddy *)bnode; | |
61 if (buddy->account != account) | |
62 continue; | |
63 gaim_prpl_got_user_status(account, buddy->name, "offline", NULL); | |
64 gaim_blist_remove_buddy(buddy); | |
65 } | |
66 } | |
67 } | |
68 } | |
69 | |
11539 | 70 void |
11837 | 71 bonjour_login(GaimAccount *account) |
11477 | 72 { |
73 GaimConnection *gc = gaim_account_get_connection(account); | |
11539 | 74 GaimGroup *bonjour_group = NULL; |
75 BonjourData *bd = NULL; | |
11837 | 76 GaimStatus *status; |
11834 | 77 GaimPresence *presence; |
11539 | 78 |
11477 | 79 gc->flags |= GAIM_CONNECTION_HTML; |
80 gc->proto_data = g_new(BonjourData, 1); | |
81 bd = gc->proto_data; | |
11539 | 82 |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11718
diff
changeset
|
83 /* Start waiting for jabber connections (iChat style) */ |
11477 | 84 bd->jabber_data = g_new(BonjourJabber, 1); |
85 bd->jabber_data->name = gc->account->username; | |
86 bd->jabber_data->port = gaim_account_get_int(account, "port", BONJOUR_DEFAULT_PORT_INT); | |
87 bd->jabber_data->account = account; | |
11539 | 88 |
11477 | 89 if (bonjour_jabber_start(bd->jabber_data) == -1) { |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11718
diff
changeset
|
90 /* Send a message about the connection error */ |
11927 | 91 gaim_debug_error("bonjour", "Unable to listen for incoming IM connections"); |
11539 | 92 |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11718
diff
changeset
|
93 /* Free the data */ |
11477 | 94 g_free(bd->jabber_data); |
95 g_free(bd); | |
96 return; | |
97 } | |
11539 | 98 |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11718
diff
changeset
|
99 /* Connect to the mDNS daemon looking for buddies in the LAN */ |
11477 | 100 bd->dns_sd_data = bonjour_dns_sd_new(); |
101 bd->dns_sd_data->name = (sw_string)gaim_account_get_username(account); | |
102 bd->dns_sd_data->txtvers = g_strdup("1"); | |
103 bd->dns_sd_data->version = g_strdup("1"); | |
11496 | 104 bd->dns_sd_data->first = g_strdup(gaim_account_get_string(account, "first", "TODO")); |
105 bd->dns_sd_data->last = g_strdup(gaim_account_get_string(account, "last", "")); | |
11477 | 106 bd->dns_sd_data->port_p2pj = gaim_account_get_int(account, "port", BONJOUR_DEFAULT_PORT_INT); |
107 bd->dns_sd_data->phsh = g_strdup(""); | |
11496 | 108 bd->dns_sd_data->email = g_strdup(gaim_account_get_string(account, "email", "")); |
11477 | 109 bd->dns_sd_data->vc = g_strdup(""); |
110 bd->dns_sd_data->jid = g_strdup(""); | |
111 bd->dns_sd_data->AIM = g_strdup(""); | |
11834 | 112 |
113 status = gaim_account_get_active_status(account); | |
114 presence = gaim_account_get_presence(account); | |
115 if (gaim_presence_is_available(presence)) | |
116 bd->dns_sd_data->status = g_strdup("avail"); | |
117 else if (gaim_presence_is_idle(presence)) | |
118 bd->dns_sd_data->status = g_strdup("away"); | |
119 else | |
120 bd->dns_sd_data->status = g_strdup("dnd"); | |
121 bd->dns_sd_data->msg = g_strdup(gaim_status_get_attr_string(status, "message")); | |
11539 | 122 |
11477 | 123 bd->dns_sd_data->account = account; |
11927 | 124 if (!bonjour_dns_sd_start(bd->dns_sd_data)) |
125 { | |
126 gaim_connection_error(gc, _("Unable to establish connection with the local mDNS server. Is it running?")); | |
127 return; | |
128 } | |
11539 | 129 |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11718
diff
changeset
|
130 /* Create a group for bonjour buddies */ |
11477 | 131 bonjour_group = gaim_group_new(BONJOUR_GROUP_NAME); |
132 gaim_blist_add_group(bonjour_group, NULL); | |
11539 | 133 |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11718
diff
changeset
|
134 /* Show the buddy list by telling Gaim we have already connected */ |
11477 | 135 gaim_connection_set_state(gc, GAIM_CONNECTED); |
136 } | |
137 | |
11539 | 138 void |
139 bonjour_close(GaimConnection *connection) | |
11477 | 140 { |
11539 | 141 GaimGroup *bonjour_group = gaim_find_group(BONJOUR_GROUP_NAME); |
142 BonjourData *bd = (BonjourData*)connection->proto_data; | |
143 | |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11718
diff
changeset
|
144 /* Stop looking for buddies in the LAN */ |
11477 | 145 if (connection != NULL) { |
146 bonjour_dns_sd_stop(bd->dns_sd_data); | |
147 if (bd != NULL) { | |
148 bonjour_dns_sd_free(bd->dns_sd_data); | |
149 } | |
150 } | |
11539 | 151 |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11718
diff
changeset
|
152 /* Stop waiting for conversations */ |
11477 | 153 bonjour_jabber_stop(bd->jabber_data); |
154 g_free(bd->jabber_data); | |
11539 | 155 |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11718
diff
changeset
|
156 /* Remove all the bonjour buddies */ |
11691 | 157 bonjour_removeallfromlocal(connection); |
11539 | 158 |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11718
diff
changeset
|
159 /* Delete the bonjour group */ |
11477 | 160 gaim_blist_remove_group(bonjour_group); |
11539 | 161 |
11477 | 162 } |
163 | |
11539 | 164 const char * |
165 bonjour_list_icon(GaimAccount *account, GaimBuddy *buddy) | |
11477 | 166 { |
167 return BONJOUR_ICON_NAME; | |
168 } | |
169 | |
11539 | 170 int |
171 bonjour_send_im(GaimConnection *connection, const char *to, const char *msg, GaimConvImFlags flags) | |
11477 | 172 { |
173 if(!to || !msg) | |
174 return 0; | |
11539 | 175 |
11693 | 176 return bonjour_jabber_send_message(((BonjourData*)(connection->proto_data))->jabber_data, to, msg); |
11477 | 177 } |
178 | |
11539 | 179 void |
180 bonjour_set_status(GaimAccount *account, GaimStatus *status) | |
11477 | 181 { |
11496 | 182 GaimConnection *gc; |
183 BonjourData *bd; | |
184 gboolean disconnected; | |
185 GaimStatusType *type; | |
186 int primitive; | |
187 GaimPresence *presence; | |
11841 | 188 const char *message, *bonjour_status; |
11477 | 189 |
11839 | 190 gc = gaim_account_get_connection(account); |
191 bd = gc->proto_data; | |
11496 | 192 disconnected = gaim_account_is_disconnected(account); |
193 type = gaim_status_get_type(status); | |
194 primitive = gaim_status_type_get_primitive(type); | |
195 presence = gaim_account_get_presence(account); | |
196 | |
197 if (!gaim_account_is_connected(account)) | |
198 /* TODO: Does this mean we're connecting? */ | |
199 return; | |
200 | |
201 message = gaim_status_get_attr_string(status, "message"); | |
202 if (message == NULL) | |
203 message = ""; | |
204 | |
205 /* | |
206 * The three possible status for Bonjour are | |
207 * -available ("avail") | |
208 * -idle ("away") | |
209 * -away ("dnd") | |
210 * Each of them can have an optional message. | |
211 */ | |
11835 | 212 if (gaim_presence_is_available(presence)) |
11841 | 213 bonjour_status = "avail"; |
11835 | 214 else if (gaim_presence_is_idle(presence)) |
11841 | 215 bonjour_status = "away"; |
11835 | 216 else |
11841 | 217 bonjour_status = "dnd"; |
11477 | 218 |
11841 | 219 bonjour_dns_sd_send_status(bd->dns_sd_data, bonjour_status, message); |
11477 | 220 } |
221 | |
11496 | 222 static GList * |
223 bonjour_status_types(GaimAccount *account) | |
11477 | 224 { |
11496 | 225 GList *status_types = NULL; |
226 GaimStatusType *type; | |
227 | |
228 g_return_val_if_fail(account != NULL, NULL); | |
229 | |
230 type = gaim_status_type_new_full(GAIM_STATUS_OFFLINE, | |
231 BONJOUR_STATUS_ID_OFFLINE, | |
232 _("Offline"), TRUE, TRUE, FALSE); | |
233 status_types = g_list_append(status_types, type); | |
11477 | 234 |
11496 | 235 type = gaim_status_type_new_with_attrs(GAIM_STATUS_AVAILABLE, |
236 BONJOUR_STATUS_ID_AVAILABLE, | |
237 _("Available"), TRUE, TRUE, FALSE, | |
238 "message", _("Message"), | |
239 gaim_value_new(GAIM_TYPE_STRING), NULL); | |
240 status_types = g_list_append(status_types, type); | |
11477 | 241 |
11496 | 242 type = gaim_status_type_new_with_attrs(GAIM_STATUS_AWAY, |
243 BONJOUR_STATUS_ID_AWAY, | |
244 _("Away"), TRUE, TRUE, FALSE, | |
245 "message", _("Message"), | |
246 gaim_value_new(GAIM_TYPE_STRING), NULL); | |
247 status_types = g_list_append(status_types, type); | |
248 | |
249 return status_types; | |
11477 | 250 } |
251 | |
11539 | 252 static void |
253 bonjour_convo_closed(GaimConnection *connection, const char *who) | |
11477 | 254 { |
11539 | 255 GaimBuddy *buddy = gaim_find_buddy(connection->account, who); |
256 | |
11477 | 257 bonjour_jabber_close_conversation(((BonjourData*)(connection->proto_data))->jabber_data, buddy); |
258 } | |
259 | |
11539 | 260 static void |
261 bonjour_list_emblems(GaimBuddy *buddy, | |
11496 | 262 const char **se, const char **sw, |
263 const char **nw,const char **ne) | |
11477 | 264 { |
11496 | 265 GaimPresence *presence; |
266 | |
267 presence = gaim_buddy_get_presence(buddy); | |
268 | |
269 if (!gaim_presence_is_available(presence)) | |
270 *se = "away"; | |
271 } | |
272 | |
11539 | 273 static char * |
274 bonjour_status_text(GaimBuddy *buddy) | |
11496 | 275 { |
276 GaimPresence *presence; | |
277 | |
278 presence = gaim_buddy_get_presence(buddy); | |
279 | |
280 if (gaim_presence_is_available(presence)) | |
281 return g_strdup(""); | |
282 else | |
283 return g_strdup("Away"); | |
11477 | 284 } |
285 | |
11539 | 286 static char * |
287 bonjour_tooltip_text(GaimBuddy *buddy) | |
11477 | 288 { |
11496 | 289 GString *ret; |
290 GaimPresence *presence; | |
291 GaimStatus *status; | |
292 const char *status_description; | |
293 const char *message; | |
294 | |
295 presence = gaim_buddy_get_presence(buddy); | |
296 status = gaim_presence_get_active_status(presence); | |
297 message = gaim_status_get_attr_string(status, "message"); | |
298 | |
299 if (gaim_presence_is_available(presence)) | |
300 status_description = gaim_status_get_name(status); | |
301 else if (gaim_presence_is_idle(presence)) | |
302 status_description = _("Idle"); | |
303 else | |
304 status_description = gaim_status_get_name(status); | |
305 | |
306 ret = g_string_new(""); | |
11519 | 307 g_string_append_printf(ret, _("\n<b>Status:</b> %s"), status_description); |
308 if (message != NULL) | |
309 g_string_append_printf(ret, _("\n<b>Message:</b> %s"), message); | |
11496 | 310 |
311 return g_string_free(ret, FALSE); | |
11477 | 312 } |
313 | |
314 static GaimPlugin *my_protocol = NULL; | |
315 | |
316 static GaimPluginProtocolInfo prpl_info = | |
317 { | |
318 OPT_PROTO_NO_PASSWORD, | |
319 NULL, /* user_splits */ | |
320 NULL, /* protocol_options */ | |
321 {"png", 0, 0, 96, 96, GAIM_ICON_SCALE_DISPLAY}, /* icon_spec */ | |
322 bonjour_list_icon, /* list_icon */ | |
323 bonjour_list_emblems, /* list_emblems */ | |
324 bonjour_status_text, /* status_text */ | |
325 bonjour_tooltip_text, /* tooltip_text */ | |
326 bonjour_status_types, /* status_types */ | |
327 NULL, /* blist_node_menu */ | |
328 NULL, /* chat_info */ | |
329 NULL, /* chat_info_defaults */ | |
330 bonjour_login, /* login */ | |
331 bonjour_close, /* close */ | |
332 bonjour_send_im, /* send_im */ | |
333 NULL, /* set_info */ | |
334 NULL, /* send_typing */ | |
335 NULL, /* get_info */ | |
336 bonjour_set_status, /* set_status */ | |
337 NULL, /* set_idle */ | |
338 NULL, /* change_passwd */ | |
339 NULL, /* add_buddy */ | |
340 NULL, /* add_buddies */ | |
341 NULL, /* remove_buddy */ | |
342 NULL, /* remove_buddies */ | |
343 NULL, /* add_permit */ | |
344 NULL, /* add_deny */ | |
345 NULL, /* rem_permit */ | |
346 NULL, /* rem_deny */ | |
347 NULL, /* set_permit_deny */ | |
348 NULL, /* join_chat */ | |
349 NULL, /* reject_chat */ | |
350 NULL, /* get_chat_name */ | |
351 NULL, /* chat_invite */ | |
352 NULL, /* chat_leave */ | |
353 NULL, /* chat_whisper */ | |
354 NULL, /* chat_send */ | |
355 NULL, /* keepalive */ | |
356 NULL, /* register_user */ | |
357 NULL, /* get_cb_info */ | |
358 NULL, /* get_cb_away */ | |
359 NULL, /* alias_buddy */ | |
360 NULL, /* group_buddy */ | |
361 NULL, /* rename_group */ | |
362 NULL, /* buddy_free */ | |
363 bonjour_convo_closed, /* convo_closed */ | |
364 NULL, /* normalize */ | |
365 NULL, /* set_buddy_icon */ | |
366 NULL, /* remove_group */ | |
367 NULL, /* get_cb_real_name */ | |
368 NULL, /* set_chat_topic */ | |
369 NULL, /* find_blist_chat */ | |
370 NULL, /* roomlist_get_list */ | |
371 NULL, /* roomlist_cancel */ | |
372 NULL, /* roomlist_expand_category */ | |
373 NULL, /* can_receive_file */ | |
374 NULL /* send_file */ | |
375 }; | |
376 | |
377 static GaimPluginInfo info = | |
378 { | |
379 GAIM_PLUGIN_MAGIC, | |
380 GAIM_MAJOR_VERSION, | |
381 GAIM_MINOR_VERSION, | |
382 GAIM_PLUGIN_PROTOCOL, /**< type */ | |
383 NULL, /**< ui_requirement */ | |
384 0, /**< flags */ | |
385 NULL, /**< dependencies */ | |
386 GAIM_PRIORITY_DEFAULT, /**< priority */ | |
387 | |
388 "prpl-bonjour", /**< id */ | |
389 "Bonjour", /**< name */ | |
390 VERSION, /**< version */ | |
391 /** summary */ | |
392 N_("Bonjour Protocol Plugin"), | |
393 /** description */ | |
394 N_("Bonjour Protocol Plugin"), | |
395 NULL, /**< author */ | |
396 GAIM_WEBSITE, /**< homepage */ | |
397 | |
398 NULL, /**< load */ | |
399 NULL, /**< unload */ | |
400 NULL, /**< destroy */ | |
401 | |
402 NULL, /**< ui_info */ | |
403 &prpl_info, /**< extra_info */ | |
404 NULL, /**< prefs_info */ | |
405 NULL | |
406 }; | |
407 | |
408 static void | |
409 init_plugin(GaimPlugin *plugin) | |
410 { | |
411 GaimAccountUserSplit *split; | |
412 GaimAccountOption *option; | |
413 char hostname[255]; | |
414 | |
415 if (gethostname(hostname, 255) != 0) { | |
416 gaim_debug_warning("rendezvous", "Error %d when getting host name. Using \"localhost.\"\n", errno); | |
417 strcpy(hostname, "localhost"); | |
418 } | |
419 | |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11718
diff
changeset
|
420 /* Creating the user splits */ |
11477 | 421 split = gaim_account_user_split_new(_("Host name"), hostname, '@'); |
422 prpl_info.user_splits = g_list_append(prpl_info.user_splits, split); | |
11539 | 423 |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11718
diff
changeset
|
424 /* Creating the options for the protocol */ |
11477 | 425 option = gaim_account_option_int_new(_("Port"), "port", 5298); |
426 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); | |
427 | |
428 option = gaim_account_option_string_new(_("First name"), "first", "Gaim"); | |
429 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); | |
430 | |
431 option = gaim_account_option_string_new(_("Last name"), "last", "User"); | |
432 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); | |
433 | |
434 option = gaim_account_option_string_new(_("Email"), "email", ""); | |
435 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); | |
11539 | 436 |
11477 | 437 my_protocol = plugin; |
438 } | |
439 | |
440 GAIM_INIT_PLUGIN(bonjour, init_plugin, info); |