Load roster before sending presence

This commit is contained in:
James Booth
2012-10-28 20:52:30 +00:00
parent 4b6002ae63
commit d13794bf60
10 changed files with 144 additions and 139 deletions

View File

@@ -26,6 +26,7 @@
#include <strophe.h>
#include "common.h"
#include "contact_list.h"
#include "jabber.h"
#include "log.h"
#include "preferences.h"
@@ -49,6 +50,8 @@ static void _xmpp_file_logger(void * const userdata,
const char * const msg);
static xmpp_log_t * _xmpp_get_file_logger();
static void _jabber_roster_request(void);
// XMPP event handlers
static void _connection_handler(xmpp_conn_t * const conn,
const xmpp_conn_event_t status, const int error,
@@ -164,26 +167,6 @@ jabber_send(const char * const msg, const char * const recipient)
free(coded_msg3);
}
void
jabber_roster_request(void)
{
xmpp_stanza_t *iq, *query;
iq = xmpp_stanza_new(jabber_conn.ctx);
xmpp_stanza_set_name(iq, "iq");
xmpp_stanza_set_type(iq, "get");
xmpp_stanza_set_id(iq, "roster");
query = xmpp_stanza_new(jabber_conn.ctx);
xmpp_stanza_set_name(query, "query");
xmpp_stanza_set_ns(query, XMPP_NS_ROSTER);
xmpp_stanza_add_child(iq, query);
xmpp_stanza_release(query);
xmpp_send(jabber_conn.conn, iq);
xmpp_stanza_release(iq);
}
void
jabber_subscribe(const char * const recipient)
{
@@ -192,7 +175,7 @@ jabber_subscribe(const char * const recipient)
presence = xmpp_stanza_new(jabber_conn.ctx);
xmpp_stanza_set_name(presence, "presence");
xmpp_stanza_set_type(presence, "subscribe");
xmpp_stanza_set_attribute(presence, "to", recipient);
xmpp_stanza_set_attribute(presence, "to", recipient);
xmpp_send(jabber_conn.conn, presence);
xmpp_stanza_release(presence);
}
@@ -266,6 +249,26 @@ jabber_free_resources(void)
xmpp_shutdown();
}
static void
_jabber_roster_request(void)
{
xmpp_stanza_t *iq, *query;
iq = xmpp_stanza_new(jabber_conn.ctx);
xmpp_stanza_set_name(iq, "iq");
xmpp_stanza_set_type(iq, "get");
xmpp_stanza_set_id(iq, "roster");
query = xmpp_stanza_new(jabber_conn.ctx);
xmpp_stanza_set_name(query, "query");
xmpp_stanza_set_ns(query, XMPP_NS_ROSTER);
xmpp_stanza_add_child(iq, query);
xmpp_stanza_release(query);
xmpp_send(jabber_conn.conn, iq);
xmpp_stanza_release(iq);
}
static int
_message_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata)
@@ -333,20 +336,14 @@ _connection_handler(xmpp_conn_t * const conn,
const char *jid = xmpp_conn_get_jid(conn);
prof_handle_login_success(jid);
xmpp_stanza_t* pres;
xmpp_handler_add(conn, _message_handler, NULL, "message", NULL, ctx);
xmpp_handler_add(conn, _presence_handler, NULL, "presence", NULL, ctx);
xmpp_id_handler_add(conn, _roster_handler, "roster", ctx);
xmpp_timed_handler_add(conn, _ping_timed_handler, PING_INTERVAL, ctx);
pres = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(pres, "presence");
xmpp_send(conn, pres);
xmpp_stanza_release(pres);
_jabber_roster_request();
jabber_conn.conn_status = JABBER_CONNECTED;
jabber_conn.presence = PRESENCE_ONLINE;
jabber_roster_request();
} else {
// received close stream response from server after disconnect
@@ -375,6 +372,7 @@ static int
_roster_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata)
{
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
xmpp_stanza_t *query, *item;
char *type = xmpp_stanza_get_type(stanza);
@@ -382,27 +380,29 @@ _roster_handler(xmpp_conn_t * const conn,
log_error("Roster query failed");
else {
query = xmpp_stanza_get_child_by_name(stanza, "query");
GSList *roster = NULL;
item = xmpp_stanza_get_children(query);
while (item != NULL) {
const char *name = xmpp_stanza_get_attribute(item, "name");
const char *jid = xmpp_stanza_get_attribute(item, "jid");
const char *name = xmpp_stanza_get_attribute(item, "name");
const char *sub = xmpp_stanza_get_attribute(item, "subscription");
jabber_roster_entry *entry = malloc(sizeof(jabber_roster_entry));
if (sub != NULL) {
if (strcmp(sub, "none") != 0) {
if (name != NULL) {
entry->name = strdup(name);
} else {
entry->name = NULL;
contact_list_add(jid, name, "offline", NULL, sub);
}
}
entry->jid = strdup(jid);
roster = g_slist_append(roster, entry);
item = xmpp_stanza_get_next(item);
}
prof_handle_roster(roster);
/*
xmpp_stanza_t* pres;
pres = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(pres, "presence");
xmpp_send(conn, pres);
xmpp_stanza_release(pres);
*/
}
return 1;
@@ -449,13 +449,14 @@ _presence_handler(xmpp_conn_t * const conn,
char *type = xmpp_stanza_get_attribute(stanza, "type");
if (type != NULL) {
// allow all subscription requests for now
if (strcmp(type, "subscribe") == 0) {
xmpp_stanza_t *presence;
presence = xmpp_stanza_new(jabber_conn.ctx);
xmpp_stanza_set_name(presence, "presence");
xmpp_stanza_set_type(presence, "subscribed");
xmpp_stanza_set_attribute(presence, "to", short_from);
xmpp_stanza_set_attribute(presence, "to", short_from);
xmpp_send(jabber_conn.conn, presence);
xmpp_stanza_release(presence);
return 1;