From 3ae74feaa2c64410ef2a854b3c65abfd293e241a Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 6 May 2013 21:53:59 +0100 Subject: [PATCH] Created roster module, moved roster iq's --- Makefile.am | 1 + src/xmpp/connection.c | 4 +- src/xmpp/iq.c | 84 ---------------------------- src/xmpp/roster.c | 126 ++++++++++++++++++++++++++++++++++++++++++ src/xmpp/roster.h | 29 ++++++++++ 5 files changed, 159 insertions(+), 85 deletions(-) create mode 100644 src/xmpp/roster.c create mode 100644 src/xmpp/roster.h diff --git a/Makefile.am b/Makefile.am index 61aace12..42cba7e7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -8,6 +8,7 @@ profanity_SOURCES = src/contact.c src/contact.h src/log.c src/common.c \ src/xmpp/iq.c src/xmpp/message.c src/xmpp/presence.c src/xmpp/stanza.c \ src/xmpp/stanza.h src/xmpp/message.h src/xmpp/iq.h src/xmpp/presence.h \ src/xmpp/capabilities.h src/xmpp/connection.h \ + src/xmpp/roster.c src/xmpp/roster.h \ src/ui/ui.h src/ui/window.c src/ui/window.h src/ui/core.c \ src/ui/titlebar.c src/ui/statusbar.c src/ui/inputwin.c \ src/ui/console.c src/ui/notifier.c src/ui/notifier.h \ diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index c067b736..e45ecaa7 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -39,6 +39,7 @@ #include "xmpp/iq.h" #include "xmpp/message.h" #include "xmpp/presence.h" +#include "xmpp/roster.h" #include "xmpp/stanza.h" #include "xmpp/xmpp.h" @@ -479,6 +480,7 @@ _connection_handler(xmpp_conn_t * const conn, chat_sessions_init(); + roster_add_handlers(); message_add_handlers(); presence_add_handlers(); iq_add_handlers(); @@ -488,7 +490,7 @@ _connection_handler(xmpp_conn_t * const conn, xmpp_timed_handler_add(conn, _ping_timed_handler, millis, ctx); } - iq_roster_request(); + roster_request(); jabber_conn.conn_status = JABBER_CONNECTED; if (prefs_get_reconnect() != 0) { diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index b4e4283a..c52d323e 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -45,10 +45,6 @@ static int _iq_handle_error(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); -static int _iq_handle_roster_set(xmpp_conn_t * const conn, - xmpp_stanza_t * const stanza, void * const userdata); -static int _iq_handle_roster_result(xmpp_conn_t * const conn, - xmpp_stanza_t * const stanza, void * const userdata); static int _iq_handle_ping_get(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); static int _iq_handle_version_get(xmpp_conn_t * const conn, @@ -70,8 +66,6 @@ iq_add_handlers(void) xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); HANDLE(NULL, STANZA_TYPE_ERROR, _iq_handle_error); - HANDLE(XMPP_NS_ROSTER, STANZA_TYPE_SET, _iq_handle_roster_set); - HANDLE(XMPP_NS_ROSTER, STANZA_TYPE_RESULT, _iq_handle_roster_result); HANDLE(XMPP_NS_DISCO_INFO, STANZA_TYPE_GET, _iq_handle_discoinfo_get); HANDLE(XMPP_NS_DISCO_INFO, STANZA_TYPE_RESULT, _iq_handle_discoinfo_result); HANDLE(XMPP_NS_DISCO_ITEMS, STANZA_TYPE_RESULT, _iq_handle_discoitems_result); @@ -81,16 +75,6 @@ iq_add_handlers(void) HANDLE(STANZA_NS_PING, STANZA_TYPE_GET, _iq_handle_ping_get); } -void -iq_roster_request(void) -{ - xmpp_conn_t * const conn = connection_get_conn(); - xmpp_ctx_t * const ctx = connection_get_ctx(); - xmpp_stanza_t *iq = stanza_create_roster_iq(ctx); - xmpp_send(conn, iq); - xmpp_stanza_release(iq); -} - void iq_room_list_request(gchar *conferencejid) { @@ -146,74 +130,6 @@ _iq_handle_error(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, return 1; } -static int -_iq_handle_roster_set(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, - void * const userdata) -{ - xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); - xmpp_stanza_t *item = - xmpp_stanza_get_child_by_name(query, STANZA_NAME_ITEM); - - if (item == NULL) { - return 1; - } - - const char *jid = xmpp_stanza_get_attribute(item, STANZA_ATTR_JID); - const char *sub = xmpp_stanza_get_attribute(item, STANZA_ATTR_SUBSCRIPTION); - if (g_strcmp0(sub, "remove") == 0) { - contact_list_remove(jid); - return 1; - } - - gboolean pending_out = FALSE; - const char *ask = xmpp_stanza_get_attribute(item, STANZA_ATTR_ASK); - if ((ask != NULL) && (strcmp(ask, "subscribe") == 0)) { - pending_out = TRUE; - } - - contact_list_update_subscription(jid, sub, pending_out); - - return 1; -} - -static int -_iq_handle_roster_result(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, - void * const userdata) -{ - const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); - - // handle initial roster response - if (g_strcmp0(id, "roster") == 0) { - xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); - xmpp_stanza_t *item = xmpp_stanza_get_children(query); - - while (item != NULL) { - const char *barejid = xmpp_stanza_get_attribute(item, STANZA_ATTR_JID); - const char *name = xmpp_stanza_get_attribute(item, STANZA_ATTR_NAME); - const char *sub = xmpp_stanza_get_attribute(item, STANZA_ATTR_SUBSCRIPTION); - - gboolean pending_out = FALSE; - const char *ask = xmpp_stanza_get_attribute(item, STANZA_ATTR_ASK); - if (g_strcmp0(ask, "subscribe") == 0) { - pending_out = TRUE; - } - - gboolean added = contact_list_add(barejid, name, sub, NULL, pending_out); - - if (!added) { - log_warning("Attempt to add contact twice: %s", barejid); - } - - item = xmpp_stanza_get_next(item); - } - - contact_presence_t conn_presence = accounts_get_login_presence(jabber_get_account_name()); - presence_update(conn_presence, NULL, 0); - } - - return 1; -} - static int _iq_handle_version_result(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) diff --git a/src/xmpp/roster.c b/src/xmpp/roster.c new file mode 100644 index 00000000..a071c736 --- /dev/null +++ b/src/xmpp/roster.c @@ -0,0 +1,126 @@ +/* + * roster.c + * + * Copyright (C) 2012, 2013 James Booth + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see . + * + */ + +#include + +#include +#include + +#include "contact_list.h" +#include "log.h" +#include "xmpp/connection.h" +#include "xmpp/stanza.h" +#include "xmpp/xmpp.h" + +#define HANDLE(type, func) xmpp_handler_add(conn, func, XMPP_NS_ROSTER, STANZA_NAME_IQ, type, ctx) + +static int _roster_handle_set(xmpp_conn_t * const conn, + xmpp_stanza_t * const stanza, void * const userdata); +static int _roster_handle_result(xmpp_conn_t * const conn, + xmpp_stanza_t * const stanza, void * const userdata); + +void +roster_add_handlers(void) +{ + xmpp_conn_t * const conn = connection_get_conn(); + xmpp_ctx_t * const ctx = connection_get_ctx(); + HANDLE(STANZA_TYPE_SET, _roster_handle_set); + HANDLE(STANZA_TYPE_RESULT, _roster_handle_result); +} + +void +roster_request(void) +{ + xmpp_conn_t * const conn = connection_get_conn(); + xmpp_ctx_t * const ctx = connection_get_ctx(); + xmpp_stanza_t *iq = stanza_create_roster_iq(ctx); + xmpp_send(conn, iq); + xmpp_stanza_release(iq); +} + +static int +_roster_handle_set(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, + void * const userdata) +{ + xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); + xmpp_stanza_t *item = + xmpp_stanza_get_child_by_name(query, STANZA_NAME_ITEM); + + if (item == NULL) { + return 1; + } + + const char *jid = xmpp_stanza_get_attribute(item, STANZA_ATTR_JID); + const char *sub = xmpp_stanza_get_attribute(item, STANZA_ATTR_SUBSCRIPTION); + if (g_strcmp0(sub, "remove") == 0) { + contact_list_remove(jid); + return 1; + } + + gboolean pending_out = FALSE; + const char *ask = xmpp_stanza_get_attribute(item, STANZA_ATTR_ASK); + if ((ask != NULL) && (strcmp(ask, "subscribe") == 0)) { + pending_out = TRUE; + } + + contact_list_update_subscription(jid, sub, pending_out); + + return 1; +} + +static int +_roster_handle_result(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, + void * const userdata) +{ + const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); + + // handle initial roster response + if (g_strcmp0(id, "roster") == 0) { + xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); + xmpp_stanza_t *item = xmpp_stanza_get_children(query); + + while (item != NULL) { + const char *barejid = xmpp_stanza_get_attribute(item, STANZA_ATTR_JID); + const char *name = xmpp_stanza_get_attribute(item, STANZA_ATTR_NAME); + const char *sub = xmpp_stanza_get_attribute(item, STANZA_ATTR_SUBSCRIPTION); + + gboolean pending_out = FALSE; + const char *ask = xmpp_stanza_get_attribute(item, STANZA_ATTR_ASK); + if (g_strcmp0(ask, "subscribe") == 0) { + pending_out = TRUE; + } + + gboolean added = contact_list_add(barejid, name, sub, NULL, pending_out); + + if (!added) { + log_warning("Attempt to add contact twice: %s", barejid); + } + + item = xmpp_stanza_get_next(item); + } + + contact_presence_t conn_presence = accounts_get_login_presence(jabber_get_account_name()); + presence_update(conn_presence, NULL, 0); + } + + return 1; +} diff --git a/src/xmpp/roster.h b/src/xmpp/roster.h new file mode 100644 index 00000000..ec06571d --- /dev/null +++ b/src/xmpp/roster.h @@ -0,0 +1,29 @@ +/* + * roster.h + * + * Copyright (C) 2012, 2013 James Booth + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see . + * + */ + +#ifndef ROSTER_H +#define ROSTER_H + +void roster_add_handlers(void); +void roster_request(void); + +#endif