Mercurial > pidgin.yaz
diff libpurple/protocols/bonjour/mdns_common.c @ 18859:c1546f6c0432
Yay for code reuse.
author | Daniel Atallah <daniel.atallah@gmail.com> |
---|---|
date | Fri, 10 Aug 2007 01:29:48 +0000 |
parents | 1787e601aafc |
children | 2d6d936867bc |
line wrap: on
line diff
--- a/libpurple/protocols/bonjour/mdns_common.c Thu Aug 09 22:49:53 2007 +0000 +++ b/libpurple/protocols/bonjour/mdns_common.c Fri Aug 10 01:29:48 2007 +0000 @@ -29,20 +29,15 @@ /** * Allocate space for the dns-sd data. */ -BonjourDnsSd * -bonjour_dns_sd_new() -{ +BonjourDnsSd * bonjour_dns_sd_new() { BonjourDnsSd *data = g_new0(BonjourDnsSd, 1); - return data; } /** * Deallocate the space of the dns-sd data. */ -void -bonjour_dns_sd_free(BonjourDnsSd *data) -{ +void bonjour_dns_sd_free(BonjourDnsSd *data) { g_free(data->first); g_free(data->last); g_free(data->phsh); @@ -52,11 +47,90 @@ g_free(data); } +static GSList *generate_presence_txt_records(BonjourDnsSd *data) { + GSList *ret = NULL; + PurpleKeyValuePair *kvp; + char portstring[6]; + const char *jid, *aim, *email; + + /* Convert the port to a string */ + snprintf(portstring, sizeof(portstring), "%d", data->port_p2pj); + + jid = purple_account_get_string(data->account, "jid", NULL); + aim = purple_account_get_string(data->account, "AIM", NULL); + email = purple_account_get_string(data->account, "email", NULL); + +#define _M_ADD_R(k, v) \ + kvp = g_new0(PurpleKeyValuePair, 1); \ + kvp->key = g_strdup(k); \ + kvp->value = g_strdup(v); \ + ret = g_slist_prepend(ret, kvp); \ + + /* We should try to follow XEP-0174, but some clients have "issues", so we humor them. + * See http://telepathy.freedesktop.org/wiki/SalutInteroperability + */ + + /* Needed by iChat */ + _M_ADD_R("txtvers", "1") + /* Needed by Gaim/Pidgin <= 2.0.1 (remove at some point) */ + _M_ADD_R("1st", data->first) + /* Needed by Gaim/Pidgin <= 2.0.1 (remove at some point) */ + _M_ADD_R("last", data->last) + /* Needed by Adium */ + _M_ADD_R("port.p2pj", portstring) + /* Needed by iChat, Gaim/Pidgin <= 2.0.1 */ + _M_ADD_R("status", data->status) + _M_ADD_R("node", "libpurple") + _M_ADD_R("ver", VERSION) + /* Currently always set to "!" since we don't support AV and wont ever be in a conference */ + _M_ADD_R("vc", data->vc) + if (email != NULL && *email != '\0') { + _M_ADD_R("email", email) + } + if (jid != NULL && *jid != '\0') { + _M_ADD_R("jid", jid) + } + /* Nonstandard, but used by iChat */ + if (aim != NULL && *aim != '\0') { + _M_ADD_R("AIM", aim) + } + if (data->msg != NULL && *data->msg != '\0') { + _M_ADD_R("msg", data->msg) + } + if (data->phsh != NULL && *data->phsh != '\0') { + _M_ADD_R("phsh", data->phsh) + } + + /* TODO: ext, nick */ + return ret; +} + +static void free_presence_txt_records(GSList *lst) { + PurpleKeyValuePair *kvp; + while(lst) { + kvp = lst->data; + g_free(kvp->key); + g_free(kvp->value); + g_free(kvp); + lst = g_slist_remove(lst, lst->data); + } +} + +static gboolean publish_presence(BonjourDnsSd *data, PublishType type) { + GSList *txt_records; + gboolean ret; + + txt_records = generate_presence_txt_records(data); + ret = _mdns_publish(data, type, txt_records); + free_presence_txt_records(txt_records); + + return ret; +} + /** * Send a new dns-sd packet updating our status. */ -void -bonjour_dns_sd_send_status(BonjourDnsSd *data, const char *status, const char *status_message) { +void bonjour_dns_sd_send_status(BonjourDnsSd *data, const char *status, const char *status_message) { g_free(data->status); g_free(data->msg); @@ -64,7 +138,7 @@ data->msg = g_strdup(status_message); /* Update our text record with the new status */ - _mdns_publish(data, PUBLISH_UPDATE); /* <--We must control the errors */ + publish_presence(data, PUBLISH_UPDATE); } /** @@ -74,8 +148,7 @@ _mdns_retrieve_buddy_icon(buddy); } -void -bonjour_dns_sd_update_buddy_icon(BonjourDnsSd *data) { +void bonjour_dns_sd_update_buddy_icon(BonjourDnsSd *data) { PurpleStoredImage *img; if ((img = purple_buddy_icons_find_account_icon(data->account))) { @@ -107,7 +180,7 @@ g_free(enc); /* Update our TXT record */ - _mdns_publish(data, PUBLISH_UPDATE); + publish_presence(data, PUBLISH_UPDATE); } purple_imgstore_unref(img); @@ -120,7 +193,7 @@ g_free(data->phsh); data->phsh = NULL; /* Update our TXT record */ - _mdns_publish(data, PUBLISH_UPDATE); + publish_presence(data, PUBLISH_UPDATE); } } } @@ -129,19 +202,14 @@ * Advertise our presence within the dns-sd daemon and start browsing * for other bonjour peers. */ -gboolean -bonjour_dns_sd_start(BonjourDnsSd *data) -{ - PurpleConnection *gc; - - gc = purple_account_get_connection(data->account); +gboolean bonjour_dns_sd_start(BonjourDnsSd *data) { /* Initialize the dns-sd data and session */ if (!_mdns_init_session(data)) return FALSE; /* Publish our bonjour IM client at the mDNS daemon */ - if (!_mdns_publish(data, PUBLISH_START)) + if (!publish_presence(data, PUBLISH_START)) return FALSE; /* Advise the daemon that we are waiting for connections */ @@ -157,7 +225,6 @@ * Unregister the "_presence._tcp" service at the mDNS daemon. */ -void -bonjour_dns_sd_stop(BonjourDnsSd *data) { +void bonjour_dns_sd_stop(BonjourDnsSd *data) { _mdns_stop(data); }