Mercurial > pidgin
comparison 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 |
comparison
equal
deleted
inserted
replaced
11983:39f2aa4350d8 | 11984:59494e5f7fdf |
---|---|
18 * You should have received a copy of the GNU General Public License | 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 | 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 | 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 */ | 21 */ |
22 #include <glib.h> | 22 #include <glib.h> |
23 #include <pwd.h> | |
23 | 24 |
24 #include "internal.h" | 25 #include "internal.h" |
25 #include "account.h" | 26 #include "account.h" |
26 #include "accountopt.h" | 27 #include "accountopt.h" |
27 #include "version.h" | 28 #include "version.h" |
29 | 30 |
30 #include "bonjour.h" | 31 #include "bonjour.h" |
31 #include "dns_sd.h" | 32 #include "dns_sd.h" |
32 #include "jabber.h" | 33 #include "jabber.h" |
33 #include "buddy.h" | 34 #include "buddy.h" |
35 | |
36 static char *default_firstname; | |
37 static char *default_lastname; | |
38 static char *default_hostname; | |
34 | 39 |
35 static void | 40 static void |
36 bonjour_removeallfromlocal(GaimConnection *gc) | 41 bonjour_removeallfromlocal(GaimConnection *gc) |
37 { | 42 { |
38 GaimAccount *account = gaim_connection_get_account(gc); | 43 GaimAccount *account = gaim_connection_get_account(gc); |
75 BonjourData *bd = NULL; | 80 BonjourData *bd = NULL; |
76 GaimStatus *status; | 81 GaimStatus *status; |
77 GaimPresence *presence; | 82 GaimPresence *presence; |
78 | 83 |
79 gc->flags |= GAIM_CONNECTION_HTML; | 84 gc->flags |= GAIM_CONNECTION_HTML; |
80 gc->proto_data = g_new(BonjourData, 1); | 85 gc->proto_data = g_new0(BonjourData, 1); |
81 bd = gc->proto_data; | 86 bd = gc->proto_data; |
82 | 87 |
83 /* Start waiting for jabber connections (iChat style) */ | 88 /* Start waiting for jabber connections (iChat style) */ |
84 bd->jabber_data = g_new(BonjourJabber, 1); | 89 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); | 90 bd->jabber_data->port = gaim_account_get_int(account, "port", BONJOUR_DEFAULT_PORT_INT); |
87 bd->jabber_data->account = account; | 91 bd->jabber_data->account = account; |
88 | 92 |
89 if (bonjour_jabber_start(bd->jabber_data) == -1) { | 93 if (bonjour_jabber_start(bd->jabber_data) == -1) { |
90 /* Send a message about the connection error */ | 94 /* Send a message about the connection error */ |
91 gaim_debug_error("bonjour", "Unable to listen for incoming IM connections"); | 95 gaim_connection_error(gc, _("Unable to listen for incoming IM connections\n")); |
92 | 96 |
93 /* Free the data */ | 97 /* Free the data */ |
94 g_free(bd->jabber_data); | 98 g_free(bd->jabber_data); |
95 g_free(bd); | 99 bd->jabber_data = NULL; |
96 return; | 100 return; |
97 } | 101 } |
98 | 102 |
99 /* Connect to the mDNS daemon looking for buddies in the LAN */ | 103 /* Connect to the mDNS daemon looking for buddies in the LAN */ |
100 bd->dns_sd_data = bonjour_dns_sd_new(); | 104 bd->dns_sd_data = bonjour_dns_sd_new(); |
101 bd->dns_sd_data->name = (sw_string)gaim_account_get_username(account); | 105 bd->dns_sd_data->name = (sw_string)gaim_account_get_username(account); |
102 bd->dns_sd_data->txtvers = g_strdup("1"); | 106 bd->dns_sd_data->txtvers = g_strdup("1"); |
103 bd->dns_sd_data->version = g_strdup("1"); | 107 bd->dns_sd_data->version = g_strdup("1"); |
104 bd->dns_sd_data->first = g_strdup(gaim_account_get_string(account, "first", "TODO")); | 108 bd->dns_sd_data->first = g_strdup(gaim_account_get_string(account, "first", default_firstname)); |
105 bd->dns_sd_data->last = g_strdup(gaim_account_get_string(account, "last", "")); | 109 bd->dns_sd_data->last = g_strdup(gaim_account_get_string(account, "last", default_lastname)); |
106 bd->dns_sd_data->port_p2pj = gaim_account_get_int(account, "port", BONJOUR_DEFAULT_PORT_INT); | 110 bd->dns_sd_data->port_p2pj = gaim_account_get_int(account, "port", BONJOUR_DEFAULT_PORT_INT); |
107 bd->dns_sd_data->phsh = g_strdup(""); | 111 bd->dns_sd_data->phsh = g_strdup(""); |
108 bd->dns_sd_data->email = g_strdup(gaim_account_get_string(account, "email", "")); | 112 bd->dns_sd_data->email = g_strdup(gaim_account_get_string(account, "email", "")); |
109 bd->dns_sd_data->vc = g_strdup(""); | 113 bd->dns_sd_data->vc = g_strdup(""); |
110 bd->dns_sd_data->jid = g_strdup(""); | 114 bd->dns_sd_data->jid = g_strdup(""); |
140 { | 144 { |
141 GaimGroup *bonjour_group = gaim_find_group(BONJOUR_GROUP_NAME); | 145 GaimGroup *bonjour_group = gaim_find_group(BONJOUR_GROUP_NAME); |
142 BonjourData *bd = (BonjourData*)connection->proto_data; | 146 BonjourData *bd = (BonjourData*)connection->proto_data; |
143 | 147 |
144 /* Stop looking for buddies in the LAN */ | 148 /* Stop looking for buddies in the LAN */ |
145 if (connection != NULL) { | 149 if (bd->dns_sd_data != NULL) |
150 { | |
146 bonjour_dns_sd_stop(bd->dns_sd_data); | 151 bonjour_dns_sd_stop(bd->dns_sd_data); |
147 if (bd != NULL) { | 152 bonjour_dns_sd_free(bd->dns_sd_data); |
148 bonjour_dns_sd_free(bd->dns_sd_data); | 153 } |
149 } | 154 |
150 } | 155 if (bd->jabber_data != NULL) |
151 | 156 { |
152 /* Stop waiting for conversations */ | 157 /* Stop waiting for conversations */ |
153 bonjour_jabber_stop(bd->jabber_data); | 158 bonjour_jabber_stop(bd->jabber_data); |
154 g_free(bd->jabber_data); | 159 g_free(bd->jabber_data); |
160 } | |
155 | 161 |
156 /* Remove all the bonjour buddies */ | 162 /* Remove all the bonjour buddies */ |
157 bonjour_removeallfromlocal(connection); | 163 bonjour_removeallfromlocal(connection); |
158 | 164 |
159 /* Delete the bonjour group */ | 165 /* Delete the bonjour group */ |
191 bd = gc->proto_data; | 197 bd = gc->proto_data; |
192 disconnected = gaim_account_is_disconnected(account); | 198 disconnected = gaim_account_is_disconnected(account); |
193 type = gaim_status_get_type(status); | 199 type = gaim_status_get_type(status); |
194 primitive = gaim_status_type_get_primitive(type); | 200 primitive = gaim_status_type_get_primitive(type); |
195 presence = gaim_account_get_presence(account); | 201 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 | 202 |
201 message = gaim_status_get_attr_string(status, "message"); | 203 message = gaim_status_get_attr_string(status, "message"); |
202 if (message == NULL) | 204 if (message == NULL) |
203 message = ""; | 205 message = ""; |
204 | 206 |
314 g_string_append_printf(ret, _("\n<b>Status:</b> %s"), status_description); | 316 g_string_append_printf(ret, _("\n<b>Status:</b> %s"), status_description); |
315 if (message != NULL) | 317 if (message != NULL) |
316 g_string_append_printf(ret, _("\n<b>Message:</b> %s"), message); | 318 g_string_append_printf(ret, _("\n<b>Message:</b> %s"), message); |
317 | 319 |
318 return g_string_free(ret, FALSE); | 320 return g_string_free(ret, FALSE); |
321 } | |
322 | |
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; | |
319 } | 331 } |
320 | 332 |
321 static GaimPlugin *my_protocol = NULL; | 333 static GaimPlugin *my_protocol = NULL; |
322 | 334 |
323 static GaimPluginProtocolInfo prpl_info = | 335 static GaimPluginProtocolInfo prpl_info = |
401 N_("Bonjour Protocol Plugin"), | 413 N_("Bonjour Protocol Plugin"), |
402 NULL, /**< author */ | 414 NULL, /**< author */ |
403 GAIM_WEBSITE, /**< homepage */ | 415 GAIM_WEBSITE, /**< homepage */ |
404 | 416 |
405 NULL, /**< load */ | 417 NULL, /**< load */ |
406 NULL, /**< unload */ | 418 plugin_unload, /**< unload */ |
407 NULL, /**< destroy */ | 419 NULL, /**< destroy */ |
408 | 420 |
409 NULL, /**< ui_info */ | 421 NULL, /**< ui_info */ |
410 &prpl_info, /**< extra_info */ | 422 &prpl_info, /**< extra_info */ |
411 NULL, /**< prefs_info */ | 423 NULL, /**< prefs_info */ |
412 NULL | 424 NULL |
413 }; | 425 }; |
414 | 426 |
415 static void | 427 static void |
416 init_plugin(GaimPlugin *plugin) | 428 initialize_default_account_values() |
417 { | 429 { |
418 GaimAccountUserSplit *split; | 430 struct passwd *info; |
419 GaimAccountOption *option; | 431 char *fullname = NULL; |
432 char *splitpoint = NULL; | |
420 char hostname[255]; | 433 char hostname[255]; |
421 | 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 */ | |
422 if (gethostname(hostname, 255) != 0) { | 461 if (gethostname(hostname, 255) != 0) { |
423 gaim_debug_warning("rendezvous", "Error %d when getting host name. Using \"localhost.\"\n", errno); | 462 gaim_debug_warning("rendezvous", "Error %d when getting host name. Using \"localhost.\"\n", errno); |
424 strcpy(hostname, "localhost"); | 463 strcpy(hostname, "localhost"); |
425 } | 464 } |
465 default_hostname = g_strdup(hostname); | |
466 } | |
467 | |
468 static void | |
469 init_plugin(GaimPlugin *plugin) | |
470 { | |
471 GaimAccountUserSplit *split; | |
472 GaimAccountOption *option; | |
473 | |
474 initialize_default_account_values(); | |
426 | 475 |
427 /* Creating the user splits */ | 476 /* Creating the user splits */ |
428 split = gaim_account_user_split_new(_("Host name"), hostname, '@'); | 477 split = gaim_account_user_split_new(_("Host name"), default_hostname, '@'); |
429 prpl_info.user_splits = g_list_append(prpl_info.user_splits, split); | 478 prpl_info.user_splits = g_list_append(prpl_info.user_splits, split); |
430 | 479 |
431 /* Creating the options for the protocol */ | 480 /* Creating the options for the protocol */ |
432 option = gaim_account_option_int_new(_("Port"), "port", 5298); | 481 option = gaim_account_option_int_new(_("Port"), "port", 5298); |
433 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); | 482 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); |
434 | 483 |
435 option = gaim_account_option_string_new(_("First name"), "first", "Gaim"); | 484 option = gaim_account_option_string_new(_("First name"), "first", default_firstname); |
436 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); | 485 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); |
437 | 486 |
438 option = gaim_account_option_string_new(_("Last name"), "last", "User"); | 487 option = gaim_account_option_string_new(_("Last name"), "last", default_lastname); |
439 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); | 488 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); |
440 | 489 |
441 option = gaim_account_option_string_new(_("Email"), "email", ""); | 490 option = gaim_account_option_string_new(_("Email"), "email", ""); |
442 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); | 491 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option); |
443 | 492 |