1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-09-22 19:45:54 -04:00

Moved ping iq creation to stanza

This commit is contained in:
James Booth 2012-11-10 00:43:09 +00:00
parent 0da40a34d5
commit 7512d70ff5
3 changed files with 56 additions and 41 deletions

View File

@ -314,6 +314,27 @@ _jabber_roster_request(void)
xmpp_stanza_release(iq); xmpp_stanza_release(iq);
} }
static int
_message_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata)
{
gchar *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE);
if (type == NULL) {
log_error("Message stanza received with no type attribute");
return 1;
} else if (strcmp(type, STANZA_TYPE_ERROR) == 0) {
return _error_message_handler(stanza);
} else if (strcmp(type, STANZA_TYPE_GROUPCHAT) == 0) {
return _groupchat_message_handler(stanza);
} else if (strcmp(type, STANZA_TYPE_CHAT) == 0) {
return _chat_message_handler(stanza);
} else {
log_error("Message stanza received with unknown type: %s", type);
return 1;
}
}
static int static int
_groupchat_message_handler(xmpp_stanza_t * const stanza) _groupchat_message_handler(xmpp_stanza_t * const stanza)
{ {
@ -404,13 +425,9 @@ _chat_message_handler(xmpp_stanza_t * const stanza)
strcpy(from_cpy, from); strcpy(from_cpy, from);
char *short_from = strtok(from_cpy, "/"); char *short_from = strtok(from_cpy, "/");
//determine chatstate support of recipient // determine chatstate support of recipient
gboolean recipient_supports = FALSE; gboolean recipient_supports = FALSE;
if ((xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_ACTIVE) != NULL) || if (stanza_contains_chat_state(stanza)) {
(xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_COMPOSING) != NULL) ||
(xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_PAUSED) != NULL) ||
(xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_GONE) != NULL) ||
(xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_INACTIVE) != NULL)) {
recipient_supports = TRUE; recipient_supports = TRUE;
} }
@ -464,27 +481,6 @@ _chat_message_handler(xmpp_stanza_t * const stanza)
return 1; return 1;
} }
static int
_message_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata)
{
gchar *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE);
if (type == NULL) {
log_error("Message stanza received with no type attribute");
return 1;
} else if (strcmp(type, STANZA_TYPE_ERROR) == 0) {
return _error_message_handler(stanza);
} else if (strcmp(type, STANZA_TYPE_GROUPCHAT) == 0) {
return _groupchat_message_handler(stanza);
} else if (strcmp(type, STANZA_TYPE_CHAT) == 0) {
return _chat_message_handler(stanza);
} else {
log_error("Message stanza received with unknown type: %s", type);
return 1;
}
}
static void static void
_connection_handler(xmpp_conn_t * const conn, _connection_handler(xmpp_conn_t * const conn,
const xmpp_conn_event_t status, const int error, const xmpp_conn_event_t status, const int error,
@ -571,20 +567,7 @@ _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata)
if (jabber_conn.conn_status == JABBER_CONNECTED) { if (jabber_conn.conn_status == JABBER_CONNECTED) {
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
xmpp_stanza_t *iq, *ping; xmpp_stanza_t *iq = stanza_create_ping_iq(ctx);
iq = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(iq, STANZA_NAME_IQ);
xmpp_stanza_set_type(iq, STANZA_TYPE_GET);
xmpp_stanza_set_id(iq, "c2s1");
ping = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(ping, STANZA_NAME_PING);
xmpp_stanza_set_ns(ping, STANZA_NS_PING);
xmpp_stanza_add_child(iq, ping);
xmpp_stanza_release(ping);
xmpp_send(conn, iq); xmpp_send(conn, iq);
xmpp_stanza_release(iq); xmpp_stanza_release(iq);
} }

View File

@ -168,3 +168,32 @@ stanza_create_roster_iq(xmpp_ctx_t *ctx)
return iq; return iq;
} }
gboolean
stanza_contains_chat_state(xmpp_stanza_t *stanza)
{
return ((xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_ACTIVE) != NULL) ||
(xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_COMPOSING) != NULL) ||
(xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_PAUSED) != NULL) ||
(xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_GONE) != NULL) ||
(xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_INACTIVE) != NULL));
}
xmpp_stanza_t *
stanza_create_ping_iq(xmpp_ctx_t *ctx)
{
xmpp_stanza_t *iq = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(iq, STANZA_NAME_IQ);
xmpp_stanza_set_type(iq, STANZA_TYPE_GET);
xmpp_stanza_set_id(iq, "c2s1");
xmpp_stanza_t *ping = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(ping, STANZA_NAME_PING);
xmpp_stanza_set_ns(ping, STANZA_NS_PING);
xmpp_stanza_add_child(iq, ping);
xmpp_stanza_release(ping);
return iq;
}

View File

@ -85,5 +85,8 @@ xmpp_stanza_t* stanza_create_presence(xmpp_ctx_t *ctx, const char * const show,
const char * const status); const char * const status);
xmpp_stanza_t* stanza_create_roster_iq(xmpp_ctx_t *ctx); xmpp_stanza_t* stanza_create_roster_iq(xmpp_ctx_t *ctx);
xmpp_stanza_t* stanza_create_ping_iq(xmpp_ctx_t *ctx);
gboolean stanza_contains_chat_state(xmpp_stanza_t *stanza);
#endif #endif