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