diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/python_plugins.c | 3 | ||||
-rw-r--r-- | src/xmpp/bookmark.c | 53 | ||||
-rw-r--r-- | src/xmpp/connection.c | 1 | ||||
-rw-r--r-- | src/xmpp/connection.h | 8 | ||||
-rw-r--r-- | src/xmpp/iq.c | 480 | ||||
-rw-r--r-- | src/xmpp/message.c | 239 | ||||
-rw-r--r-- | src/xmpp/presence.c | 219 | ||||
-rw-r--r-- | src/xmpp/roster.c | 92 |
8 files changed, 457 insertions, 638 deletions
diff --git a/src/plugins/python_plugins.c b/src/plugins/python_plugins.c index 5ae03879..baaf7b5b 100644 --- a/src/plugins/python_plugins.c +++ b/src/plugins/python_plugins.c @@ -635,6 +635,7 @@ python_on_message_stanza_receive_hook(ProfPlugin *plugin, const char *const text } } + allow_python_threads(); return TRUE; } @@ -699,6 +700,7 @@ python_on_presence_stanza_receive_hook(ProfPlugin *plugin, const char *const tex } } + allow_python_threads(); return TRUE; } @@ -763,6 +765,7 @@ python_on_iq_stanza_receive_hook(ProfPlugin *plugin, const char *const text) } } + allow_python_threads(); return TRUE; } diff --git a/src/xmpp/bookmark.c b/src/xmpp/bookmark.c index 97eb1b07..65b265c5 100644 --- a/src/xmpp/bookmark.c +++ b/src/xmpp/bookmark.c @@ -64,22 +64,16 @@ static Autocomplete bookmark_ac; static GList *bookmark_list; // id handlers -static int _bookmark_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); - -// scheduled -static int _bookmark_handle_delete(xmpp_conn_t *const conn, void *const userdata); +static int _bookmark_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata); static void _bookmark_item_destroy(gpointer item); static int _match_bookmark_by_jid(gconstpointer a, gconstpointer b); static void _send_bookmarks(void); -static void _send_iq_stanza(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza); - void bookmark_request(void) { char *id; - xmpp_conn_t *conn = connection_get_conn(); xmpp_ctx_t *ctx = connection_get_ctx(); xmpp_stanza_t *iq; @@ -92,12 +86,11 @@ bookmark_request(void) bookmark_list = NULL; } - xmpp_timed_handler_add(conn, _bookmark_handle_delete, BOOKMARK_TIMEOUT, id); - xmpp_id_handler_add(conn, _bookmark_result_id_handler, id, id); + id_handler_add(id, _bookmark_result_id_handler, id); iq = stanza_create_bookmarks_storage_request(ctx); xmpp_stanza_set_id(iq, id); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } @@ -247,11 +240,8 @@ bookmark_autocomplete_reset(void) } static int -_bookmark_result_id_handler(xmpp_conn_t *const conn, - xmpp_stanza_t *const stanza, void *const userdata) +_bookmark_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza bookmark result id handler fired"); - xmpp_ctx_t *ctx = connection_get_ctx(); char *id = (char *)userdata; xmpp_stanza_t *ptr; @@ -265,7 +255,6 @@ _bookmark_result_id_handler(xmpp_conn_t *const conn, Jid *my_jid; Bookmark *item; - xmpp_timed_handler_delete(conn, _bookmark_handle_delete); g_free(id); name = xmpp_stanza_get_name(stanza); @@ -366,22 +355,6 @@ _bookmark_result_id_handler(xmpp_conn_t *const conn, return 0; } -static int -_bookmark_handle_delete(xmpp_conn_t *const conn, - void *const userdata) -{ - char *id = (char *)userdata; - - assert(id != NULL); - - log_debug("Timeout for handler with id=%s", id); - - xmpp_id_handler_delete(conn, _bookmark_result_id_handler, id); - g_free(id); - - return 0; -} - static void _bookmark_item_destroy(gpointer item) { @@ -409,7 +382,6 @@ _match_bookmark_by_jid(gconstpointer a, gconstpointer b) static void _send_bookmarks(void) { - xmpp_conn_t *conn = connection_get_conn(); xmpp_ctx_t *ctx = connection_get_ctx(); xmpp_stanza_t *iq = xmpp_stanza_new(ctx); @@ -481,21 +453,6 @@ _send_bookmarks(void) xmpp_stanza_release(storage); xmpp_stanza_release(query); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } - -static void -_send_iq_stanza(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza) -{ - char *text; - size_t text_size; - xmpp_stanza_to_text(stanza, &text, &text_size); - - char *plugin_text = plugins_on_iq_stanza_send(text); - if (plugin_text) { - xmpp_send_raw_string(conn, "%s", plugin_text); - } else { - xmpp_send_raw_string(conn, "%s", text); - } -} diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index ab46e4b7..0959470a 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -644,7 +644,6 @@ _connection_handler(xmpp_conn_t *const conn, const xmpp_conn_event_t status, con chat_sessions_init(); - roster_add_handlers(); message_add_handlers(); presence_add_handlers(); iq_add_handlers(); diff --git a/src/xmpp/connection.h b/src/xmpp/connection.h index e75cc2ad..857fc88b 100644 --- a/src/xmpp/connection.h +++ b/src/xmpp/connection.h @@ -46,6 +46,8 @@ #include "resource.h" +typedef int(*ProfIdCallback)(xmpp_stanza_t *const stanza, void *const userdata); + xmpp_conn_t* connection_get_conn(void); xmpp_ctx_t* connection_get_ctx(void); void connection_set_priority(int priority); @@ -53,4 +55,10 @@ void connection_set_presence_message(const char *const message); void connection_add_available_resource(Resource *resource); void connection_remove_available_resource(const char *const resource); +void send_iq_stanza(xmpp_stanza_t *const stanza); + +void roster_set_handler(xmpp_stanza_t *const stanza); +void roster_result_handler(xmpp_stanza_t *const stanza); +void id_handler_add(const char *const id, ProfIdCallback func, void *userdata); + #endif diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index b671ee07..fd876604 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -65,72 +65,151 @@ #include "xmpp/xmpp.h" #include "plugins/plugins.h" -#define HANDLE(ns, type, func) xmpp_handler_add(conn, func, ns, STANZA_NAME_IQ, type, ctx) - typedef struct p_room_info_data_t { char *room; gboolean display; } ProfRoomInfoData; -// regular handlers +typedef int(*ProfIdCallback)(xmpp_stanza_t *const stanza, void *const userdata); + +typedef struct p_id_handle_t { + ProfIdCallback func; + void *userdata; +} ProfIdHandler; + static int _iq_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _error_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _disco_info_get_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _disco_items_get_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _disco_items_result_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _last_activity_get_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _version_get_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _ping_get_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); - -// id handlers -static int _version_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _disco_info_response_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _last_activity_response_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _room_info_response_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _destroy_room_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _room_config_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _room_config_submit_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _room_affiliation_list_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _room_affiliation_set_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _room_role_set_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _room_role_list_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _room_kick_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _enable_carbons_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _disable_carbons_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _manual_pong_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _caps_response_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _caps_response_for_jid_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _caps_response_legacy_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _auto_pong_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); + +static void _error_handler(xmpp_stanza_t *const stanza); +static void _disco_info_get_handler(xmpp_stanza_t *const stanza); +static void _disco_items_get_handler(xmpp_stanza_t *const stanza); +static void _disco_items_result_handler(xmpp_stanza_t *const stanza); +static void _last_activity_get_handler(xmpp_stanza_t *const stanza); +static void _version_get_handler(xmpp_stanza_t *const stanza); +static void _ping_get_handler(xmpp_stanza_t *const stanza); + +static int _version_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata); +static int _disco_info_response_id_handler(xmpp_stanza_t *const stanza, void *const userdata); +static int _last_activity_response_id_handler(xmpp_stanza_t *const stanza, void *const userdata); +static int _room_info_response_id_handler(xmpp_stanza_t *const stanza, void *const userdata); +static int _destroy_room_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata); +static int _room_config_id_handler(xmpp_stanza_t *const stanza, void *const userdata); +static int _room_config_submit_id_handler(xmpp_stanza_t *const stanza, void *const userdata); +static int _room_affiliation_list_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata); +static int _room_affiliation_set_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata); +static int _room_role_set_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata); +static int _room_role_list_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata); +static int _room_kick_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata); +static int _enable_carbons_id_handler(xmpp_stanza_t *const stanza, void *const userdata); +static int _disable_carbons_id_handler(xmpp_stanza_t *const stanza, void *const userdata); +static int _manual_pong_id_handler(xmpp_stanza_t *const stanza, void *const userdata); +static int _caps_response_id_handler(xmpp_stanza_t *const stanza, void *const userdata); +static int _caps_response_for_jid_id_handler(xmpp_stanza_t *const stanza, void *const userdata); +static int _caps_response_legacy_id_handler(xmpp_stanza_t *const stanza, void *const userdata); +static int _auto_pong_id_handler(xmpp_stanza_t *const stanza, void *const userdata); // scheduled static int _autoping_timed_send(xmpp_conn_t *const conn, void *const userdata); -static void _send_iq_stanza(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza); -static gboolean _receive_iq_stanza(xmpp_stanza_t *const stanza); - static gboolean autoping_wait = FALSE; static GTimer *autoping_time = NULL; +static GHashTable *id_handlers; + +static int +_iq_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) +{ + log_debug("iq stanza handler fired"); + + char *text; + size_t text_size; + xmpp_stanza_to_text(stanza, &text, &text_size); + gboolean cont = plugins_on_iq_stanza_receive(text); + if (!cont) { + return 1; + } + + char *type = xmpp_stanza_get_type(stanza); + + if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { + _error_handler(stanza); + } + + xmpp_stanza_t *discoinfo = xmpp_stanza_get_child_by_ns(stanza, XMPP_NS_DISCO_INFO); + if (discoinfo && (g_strcmp0(type, STANZA_TYPE_GET) == 0)) { + _disco_info_get_handler(stanza); + } + + xmpp_stanza_t *discoitems = xmpp_stanza_get_child_by_ns(stanza, XMPP_NS_DISCO_ITEMS); + if (discoitems && (g_strcmp0(type, STANZA_TYPE_GET) == 0)) { + _disco_items_get_handler(stanza); + } + if (discoitems && (g_strcmp0(type, STANZA_TYPE_RESULT) == 0)) { + _disco_items_result_handler(stanza); + } + + xmpp_stanza_t *lastactivity = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_LASTACTIVITY); + if (lastactivity && (g_strcmp0(type, STANZA_TYPE_GET) == 0)) { + _last_activity_get_handler(stanza); + } + + xmpp_stanza_t *version = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_VERSION); + if (version && (g_strcmp0(type, STANZA_TYPE_GET) == 0)) { + _version_get_handler(stanza); + } + + xmpp_stanza_t *ping = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_PING); + if (ping && (g_strcmp0(type, STANZA_TYPE_GET) == 0)) { + _ping_get_handler(stanza); + } + + xmpp_stanza_t *roster = xmpp_stanza_get_child_by_ns(stanza, XMPP_NS_ROSTER); + if (roster && (g_strcmp0(type, STANZA_TYPE_SET) == 0)) { + roster_set_handler(stanza); + } + if (roster && (g_strcmp0(type, STANZA_TYPE_RESULT) == 0)) { + roster_result_handler(stanza); + } + + char *id = xmpp_stanza_get_id(stanza); + if (id) { + ProfIdHandler *handler = g_hash_table_lookup(id_handlers, id); + if (handler) { + int keep = handler->func(stanza, handler->userdata); + if (!keep) { + free(handler); + g_hash_table_remove(id_handlers, id); + } + } + } + + return 1; +} void iq_add_handlers(void) { xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); - - HANDLE(NULL, NULL, _iq_handler); - HANDLE(NULL, STANZA_TYPE_ERROR, _error_handler); - HANDLE(XMPP_NS_DISCO_INFO, STANZA_TYPE_GET, _disco_info_get_handler); - HANDLE(XMPP_NS_DISCO_ITEMS, STANZA_TYPE_GET, _disco_items_get_handler); - HANDLE(XMPP_NS_DISCO_ITEMS, STANZA_TYPE_RESULT, _disco_items_result_handler); - HANDLE(STANZA_NS_LASTACTIVITY, STANZA_TYPE_GET, _last_activity_get_handler); - HANDLE(STANZA_NS_VERSION, STANZA_TYPE_GET, _version_get_handler); - HANDLE(STANZA_NS_PING, STANZA_TYPE_GET, _ping_get_handler); + xmpp_handler_add(conn, _iq_handler, NULL, STANZA_NAME_IQ, NULL, ctx); if (prefs_get_autoping() != 0) { int millis = prefs_get_autoping() * 1000; xmpp_timed_handler_add(conn, _autoping_timed_send, millis, ctx); } + + if (id_handlers) { + g_hash_table_destroy(id_handlers); + } + id_handlers = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL); +} + +void +id_handler_add(const char *const id, ProfIdCallback func, void *userdata) +{ + ProfIdHandler *handler = malloc(sizeof(ProfIdHandler)); + handler->func = func; + handler->userdata = userdata; + + g_hash_table_insert(id_handlers, strdup(id), handler); } void @@ -183,77 +262,71 @@ iq_set_autoping(const int seconds) void iq_room_list_request(gchar *conferencejid) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_create_disco_items_iq(ctx, "confreq", conferencejid); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } void iq_enable_carbons(void) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_enable_carbons(ctx); char *id = xmpp_stanza_get_id(iq); - xmpp_id_handler_add(conn, _enable_carbons_id_handler, id, NULL); + id_handler_add(id, _enable_carbons_id_handler, NULL); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } void iq_disable_carbons(void) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_disable_carbons(ctx); char *id = xmpp_stanza_get_id(iq); - xmpp_id_handler_add(conn, _disable_carbons_id_handler, id, NULL); + id_handler_add(id, _disable_carbons_id_handler, NULL); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } void iq_disco_info_request(gchar *jid) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); char *id = create_unique_id("disco_info"); xmpp_stanza_t *iq = stanza_create_disco_info_iq(ctx, id, jid, NULL); - xmpp_id_handler_add(conn, _disco_info_response_id_handler, id, NULL); + id_handler_add(id, _disco_info_response_id_handler, NULL); free(id); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } void iq_last_activity_request(gchar *jid) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); char *id = create_unique_id("lastactivity"); xmpp_stanza_t *iq = stanza_create_last_activity_iq(ctx, id, jid); - xmpp_id_handler_add(conn, _last_activity_response_id_handler, id, NULL); + id_handler_add(id, _last_activity_response_id_handler, NULL); free(id); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } void iq_room_info_request(const char *const room, gboolean display_result) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); char *id = create_unique_id("room_disco_info"); xmpp_stanza_t *iq = stanza_create_disco_info_iq(ctx, id, room, NULL); @@ -262,21 +335,18 @@ iq_room_info_request(const char *const room, gboolean display_result) cb_data->room = strdup(room); cb_data->display = display_result; - xmpp_id_handler_add(conn, _room_info_response_id_handler, id, cb_data); + id_handler_add(id, _room_info_response_id_handler, cb_data); free(id); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } - - void iq_send_caps_request_for_jid(const char *const to, const char *const id, const char *const node, const char *const ver) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); if (!node) { @@ -293,9 +363,9 @@ iq_send_caps_request_for_jid(const char *const to, const char *const id, xmpp_stanza_t *iq = stanza_create_disco_info_iq(ctx, id, to, node_str->str); g_string_free(node_str, TRUE); - xmpp_id_handler_add(conn, _caps_response_for_jid_id_handler, id, strdup(to)); + id_handler_add(id, _caps_response_for_jid_id_handler, strdup(to)); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } @@ -303,7 +373,6 @@ void iq_send_caps_request(const char *const to, const char *const id, const char *const node, const char *const ver) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); if (!node) { @@ -320,9 +389,9 @@ iq_send_caps_request(const char *const to, const char *const id, xmpp_stanza_t *iq = stanza_create_disco_info_iq(ctx, id, to, node_str->str); g_string_free(node_str, TRUE); - xmpp_id_handler_add(conn, _caps_response_id_handler, id, NULL); + id_handler_add(id, _caps_response_id_handler, NULL); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } @@ -330,7 +399,6 @@ void iq_send_caps_request_legacy(const char *const to, const char *const id, const char *const node, const char *const ver) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); if (!node) { @@ -346,124 +414,115 @@ iq_send_caps_request_legacy(const char *const to, const char *const id, g_string_printf(node_str, "%s#%s", node, ver); xmpp_stanza_t *iq = stanza_create_disco_info_iq(ctx, id, to, node_str->str); - xmpp_id_handler_add(conn, _caps_response_legacy_id_handler, id, node_str->str); + id_handler_add(id, _caps_response_legacy_id_handler, node_str->str); g_string_free(node_str, FALSE); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } void iq_disco_items_request(gchar *jid) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_create_disco_items_iq(ctx, "discoitemsreq", jid); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } void iq_send_software_version(const char *const fulljid) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_create_software_version_iq(ctx, fulljid); char *id = xmpp_stanza_get_id(iq); - xmpp_id_handler_add(conn, _version_result_id_handler, id, strdup(fulljid)); + id_handler_add(id, _version_result_id_handler, strdup(fulljid)); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } void iq_confirm_instant_room(const char *const room_jid) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_create_instant_room_request_iq(ctx, room_jid); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } void iq_destroy_room(const char *const room_jid) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_create_instant_room_destroy_iq(ctx, room_jid); char *id = xmpp_stanza_get_id(iq); - xmpp_id_handler_add(conn, _destroy_room_result_id_handler, id, NULL); + id_handler_add(id, _destroy_room_result_id_handler, NULL); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } void iq_request_room_config_form(const char *const room_jid) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_create_room_config_request_iq(ctx, room_jid); char *id = xmpp_stanza_get_id(iq); - xmpp_id_handler_add(conn, _room_config_id_handler, id, NULL); + id_handler_add(id, _room_config_id_handler, NULL); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } void iq_submit_room_config(const char *const room, DataForm *form) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_create_room_config_submit_iq(ctx, room, form); char *id = xmpp_stanza_get_id(iq); - xmpp_id_handler_add(conn, _room_config_submit_id_handler, id, NULL); + id_handler_add(id, _room_config_submit_id_handler, NULL); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } void iq_room_config_cancel(const char *const room_jid) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_create_room_config_cancel_iq(ctx, room_jid); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } void iq_room_affiliation_list(const char *const room, char *affiliation) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_create_room_affiliation_list_iq(ctx, room, affiliation); char *id = xmpp_stanza_get_id(iq); - xmpp_id_handler_add(conn, _room_affiliation_list_result_id_handler, id, strdup(affiliation)); + id_handler_add(id, _room_affiliation_list_result_id_handler, strdup(affiliation)); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } void iq_room_kick_occupant(const char *const room, const char *const nick, const char *const reason) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_create_room_kick_iq(ctx, room, nick, reason); char *id = xmpp_stanza_get_id(iq); - xmpp_id_handler_add(conn, _room_kick_result_id_handler, id, strdup(nick)); + id_handler_add(id, _room_kick_result_id_handler, strdup(nick)); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } @@ -476,7 +535,6 @@ void iq_room_affiliation_set(const char *const room, const char *const jid, char *affiliation, const char *const reason) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_create_room_affiliation_set_iq(ctx, room, jid, affiliation, reason); @@ -486,9 +544,9 @@ iq_room_affiliation_set(const char *const room, const char *const jid, char *aff affiliation_set->item = strdup(jid); affiliation_set->privilege = strdup(affiliation); - xmpp_id_handler_add(conn, _room_affiliation_set_result_id_handler, id, affiliation_set); + id_handler_add(id, _room_affiliation_set_result_id_handler, affiliation_set); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } @@ -496,7 +554,6 @@ void iq_room_role_set(const char *const room, const char *const nick, char *role, const char *const reason) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_create_room_role_set_iq(ctx, room, nick, role, reason); @@ -506,60 +563,42 @@ iq_room_role_set(const char *const room, const char *const nick, char *role, role_set->item = strdup(nick); role_set->privilege = strdup(role); - xmpp_id_handler_add(conn, _room_role_set_result_id_handler, id, role_set); + id_handler_add(id, _room_role_set_result_id_handler, role_set); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } void iq_room_role_list(const char *const room, char *role) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_create_room_role_list_iq(ctx, room, role); char *id = xmpp_stanza_get_id(iq); - xmpp_id_handler_add(conn, _room_role_list_result_id_handler, id, strdup(role)); + id_handler_add(id, _room_role_list_result_id_handler, strdup(role)); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } void iq_send_ping(const char *const target) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_create_ping_iq(ctx, target); char *id = xmpp_stanza_get_id(iq); GDateTime *now = g_date_time_new_now_local(); - xmpp_id_handler_add(conn, _manual_pong_id_handler, id, now); + id_handler_add(id, _manual_pong_id_handler, now); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } -static int -_iq_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) -{ - log_debug("iq stanza handler fired"); - - gboolean cont = _receive_iq_stanza(stanza); - if (!cont) { - return 1; - } - - return 1; -} - -static int -_error_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +static void +_error_handler(xmpp_stanza_t *const stanza) { - log_debug("iq stanza error handler fired"); - const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); char *error_msg = stanza_get_error_message(stanza); @@ -572,16 +611,11 @@ _error_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, } free(error_msg); - - return 1; } static int -_caps_response_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +_caps_response_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza caps response id handler fired"); - const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); @@ -653,11 +687,8 @@ _caps_response_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, } static int -_caps_response_for_jid_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +_caps_response_for_jid_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza caps response for jid id handler fired"); - char *jid = (char *)userdata; const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); @@ -714,11 +745,8 @@ _caps_response_for_jid_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const } static int -_caps_response_legacy_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +_caps_response_legacy_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza caps response legacy id handler fired"); - const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); char *expected_node = (char *)userdata; @@ -789,10 +817,8 @@ _caps_response_legacy_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const s } static int -_enable_carbons_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) +_enable_carbons_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza enable carbons id handler fired"); - char *type = xmpp_stanza_get_type(stanza); if (g_strcmp0(type, "error") == 0) { char *error_message = stanza_get_error_message(stanza); @@ -807,10 +833,8 @@ _enable_carbons_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, } static int -_disable_carbons_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) +_disable_carbons_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza disable carbons id handler fired"); - char *type = xmpp_stanza_get_type(stanza); if (g_strcmp0(type, "error") == 0) { char *error_message = stanza_get_error_message(stanza); @@ -825,11 +849,8 @@ _disable_carbons_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza } static int -_manual_pong_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +_manual_pong_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza manual pong id handler fired"); - char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); char *type = xmpp_stanza_get_type(stanza); GDateTime *sent = (GDateTime *)userdata; @@ -883,9 +904,9 @@ _autoping_timed_send(xmpp_conn_t *const conn, void *const userdata) log_debug("Autoping: Sending ping request: %s", id); // add pong handler - xmpp_id_handler_add(conn, _auto_pong_id_handler, id, ctx); + id_handler_add(id, _auto_pong_id_handler, ctx); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); autoping_wait = TRUE; if (autoping_time) { @@ -897,10 +918,8 @@ _autoping_timed_send(xmpp_conn_t *const conn, void *const userdata) } static int -_auto_pong_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) +_auto_pong_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza auto pong id handler fired"); - autoping_wait = FALSE; if (autoping_time) { g_timer_destroy(autoping_time); @@ -941,6 +960,7 @@ _auto_pong_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void log_warning("Server ping (id=%s) error type 'cancel', disabling autoping.", id); prefs_set_autoping(0); cons_show_error("Server ping not supported, autoping disabled."); + xmpp_conn_t *conn = connection_get_conn(); xmpp_timed_handler_delete(conn, _autoping_timed_send); } @@ -948,11 +968,8 @@ _auto_pong_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void } static int -_version_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +_version_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza version result id handler fired"); - char *id = xmpp_stanza_get_id(stanza); if (id) { @@ -1012,6 +1029,7 @@ _version_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, log_warning("From attribute specified different JID, using original JID."); } + xmpp_conn_t *conn = connection_get_conn(); xmpp_ctx_t *ctx = xmpp_conn_get_context(conn); Jid *jidp = jid_create((char*)userdata); @@ -1048,13 +1066,10 @@ _version_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, return 0; } -static int -_ping_get_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +static void +_ping_get_handler(xmpp_stanza_t *const stanza) { - log_debug("iq stanza ping get handler fired"); - - xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; + xmpp_ctx_t * const ctx = connection_get_ctx(); const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); const char *to = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TO); const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); @@ -1066,7 +1081,7 @@ _ping_get_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, } if ((from == NULL) || (to == NULL)) { - return 1; + return; } xmpp_stanza_t *pong = xmpp_stanza_new(ctx); @@ -1079,19 +1094,14 @@ _ping_get_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, xmpp_stanza_set_attribute(pong, STANZA_ATTR_ID, id); } - _send_iq_stanza(conn, pong); + send_iq_stanza(pong); xmpp_stanza_release(pong); - - return 1; } -static int -_version_get_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +static void +_version_get_handler(xmpp_stanza_t *const stanza) { - log_debug("iq stanza version get handler fired"); - - xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; + xmpp_ctx_t * const ctx = connection_get_ctx(); const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); @@ -1141,7 +1151,7 @@ _version_get_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, xmpp_stanza_add_child(query, version); xmpp_stanza_add_child(response, query); - _send_iq_stanza(conn, response); + send_iq_stanza(response); g_string_free(version_str, TRUE); xmpp_stanza_release(name_txt); @@ -1151,17 +1161,12 @@ _version_get_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, xmpp_stanza_release(query); xmpp_stanza_release(response); } - - return 1; } -static int -_disco_items_get_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +static void +_disco_items_get_handler(xmpp_stanza_t *const stanza) { - log_debug("iq stanza disco items get handler fired"); - - xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; + xmpp_ctx_t * const ctx = connection_get_ctx(); const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); @@ -1181,25 +1186,21 @@ _disco_items_get_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, xmpp_stanza_set_name(query, STANZA_NAME_QUERY); xmpp_stanza_set_ns(query, XMPP_NS_DISCO_ITEMS); xmpp_stanza_add_child(response, query); - _send_iq_stanza(conn, response); + + send_iq_stanza(response); xmpp_stanza_release(response); } - - return 1; } -static int -_last_activity_get_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +static void +_last_activity_get_handler(xmpp_stanza_t *const stanza) { - log_debug("iq stanza last activity get handler fired"); - xmpp_ctx_t *ctx = connection_get_ctx(); const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); if (!from) { - return 1; + return; } if (prefs_get_boolean(PREF_LASTACTIVITY)) { @@ -1221,7 +1222,7 @@ _last_activity_get_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, xmpp_stanza_add_child(response, query); xmpp_stanza_release(query); - _send_iq_stanza(conn, response); + send_iq_stanza(response); xmpp_stanza_release(response); } else { @@ -1245,21 +1246,16 @@ _last_activity_get_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, xmpp_stanza_add_child(response, error); xmpp_stanza_release(error); - _send_iq_stanza(conn, response); + send_iq_stanza(response); xmpp_stanza_release(response); } - - return 1; } -static int -_disco_info_get_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +static void +_disco_info_get_handler(xmpp_stanza_t *const stanza) { - log_debug("iq stanza disco info get handler fired"); - - xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; + xmpp_ctx_t * const ctx = connection_get_ctx(); const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); xmpp_stanza_t *incoming_query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); @@ -1284,21 +1280,16 @@ _disco_info_get_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, xmpp_stanza_set_attribute(query, STANZA_ATTR_NODE, node_str); } xmpp_stanza_add_child(response, query); - _send_iq_stanza(conn, response); + send_iq_stanza(response); xmpp_stanza_release(query); xmpp_stanza_release(response); } - - return 1; } static int -_destroy_room_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +_destroy_room_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza destroy room result id handler fired"); - const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); if (id) { @@ -1318,11 +1309,8 @@ _destroy_room_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const st } static int -_room_config_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +_room_config_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza room config id handler fired"); - const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); const char *type = xmpp_stanza_get_type(stanza); const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); @@ -1376,11 +1364,8 @@ _room_config_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, } static int -_room_affiliation_set_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +_room_affiliation_set_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza room affiliation set result id handler fired"); - const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); const char *type = xmpp_stanza_get_type(stanza); const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); @@ -1411,11 +1396,8 @@ _room_affiliation_set_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t * } static int -_room_role_set_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +_room_role_set_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza room role set result id handler fired"); - const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); const char *type = xmpp_stanza_get_type(stanza); const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); @@ -1446,10 +1428,8 @@ _room_role_set_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const s } static int -_room_affiliation_list_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) +_room_affiliation_list_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza room affiliation list result id handler fired"); - const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); const char *type = xmpp_stanza_get_type(stanza); const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); @@ -1502,10 +1482,8 @@ _room_affiliation_list_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t } static int -_room_role_list_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) +_room_role_list_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza room role list result id handler fired"); - const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); const char *type = xmpp_stanza_get_type(stanza); const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); @@ -1557,11 +1535,8 @@ _room_role_list_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const } static int -_room_config_submit_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +_room_config_submit_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza room config submit id handler fired"); - const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); const char *type = xmpp_stanza_get_type(stanza); const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); @@ -1586,10 +1561,8 @@ _room_config_submit_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const sta } static int -_room_kick_result_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) +_room_kick_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza room kick result id handler fired"); - const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); const char *type = xmpp_stanza_get_type(stanza); const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); @@ -1636,11 +1609,8 @@ _item_destroy(DiscoItem *item) } static int -_room_info_response_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +_room_info_response_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza room info response id handler fired"); - const char *type = xmpp_stanza_get_type(stanza); ProfRoomInfoData *cb_data = (ProfRoomInfoData *)userdata; log_info("Received diso#info response for room: %s", cb_data->room); @@ -1719,11 +1689,8 @@ _room_info_response_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const sta } static int -_last_activity_response_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +_last_activity_response_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza last activity response id handler fired"); - const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); if (!from) { cons_show_error("Invalid last activity response received."); @@ -1776,11 +1743,8 @@ _last_activity_response_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const } static int -_disco_info_response_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +_disco_info_response_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza disco info response id handler fired"); - const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); const char *type = xmpp_stanza_get_type(stanza); @@ -1855,12 +1819,9 @@ _disco_info_response_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const st return 0; } -static int -_disco_items_result_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +static void +_disco_items_result_handler(xmpp_stanza_t *const stanza) { - log_debug("iq stanza disco items result handler fired"); - log_debug("Received diso#items response"); const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); @@ -1901,17 +1862,16 @@ _disco_items_result_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza } g_slist_free_full(items, (GDestroyNotify)_item_destroy); - - return 1; } -static void -_send_iq_stanza(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza) +void +send_iq_stanza(xmpp_stanza_t *const stanza) { char *text; size_t text_size; xmpp_stanza_to_text(stanza, &text, &text_size); + xmpp_conn_t *conn = connection_get_conn(); char *plugin_text = plugins_on_iq_stanza_send(text); if (plugin_text) { xmpp_send_raw_string(conn, "%s", plugin_text); @@ -1919,13 +1879,3 @@ _send_iq_stanza(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza) xmpp_send_raw_string(conn, "%s", text); } } - -static gboolean -_receive_iq_stanza(xmpp_stanza_t *const stanza) -{ - char *text; - size_t text_size; - xmpp_stanza_to_text(stanza, &text, &text_size); - - return plugins_on_iq_stanza_receive(text); -} diff --git a/src/xmpp/message.c b/src/xmpp/message.c index b212dbf5..248a56f7 100644 --- a/src/xmpp/message.c +++ b/src/xmpp/message.c @@ -60,33 +60,72 @@ #include "pgp/gpg.h" #include "plugins/plugins.h" -#define HANDLE(ns, type, func) xmpp_handler_add(conn, func, ns, STANZA_NAME_MESSAGE, type, ctx) +static int _message_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -// regular handlers -static int _groupchat_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _chat_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _muc_user_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _conference_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _captcha_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _message_error_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _receipt_received_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); +static void _handle_error(xmpp_stanza_t *const stanza); +static void _handle_groupchat(xmpp_stanza_t *const stanza); +static void _handel_muc_user(xmpp_stanza_t *const stanza); +static void _handle_conference(xmpp_stanza_t *const stanza); +static void _handle_captcha(xmpp_stanza_t *const stanza); +static void _handle_receipt_received(xmpp_stanza_t *const stanza); +static void _handle_chat(xmpp_stanza_t *const stanza); -static void _send_message_stanza(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza); -static gboolean _receive_message_stanza(xmpp_stanza_t *const stanza); +static void _send_message_stanza(xmpp_stanza_t *const stanza); + +static int +_message_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) +{ + log_debug("Message stanza handler fired"); + + char *text; + size_t text_size; + xmpp_stanza_to_text(stanza, &text, &text_size); + gboolean cont = plugins_on_message_stanza_receive(text); + if (!cont) { + return 1; + } + + char *type = xmpp_stanza_get_type(stanza); + + if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { + _handle_error(stanza); + } + + if (g_strcmp0(type, STANZA_TYPE_GROUPCHAT) == 0) { + _handle_groupchat(stanza); + } + + xmpp_stanza_t *mucuser = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_USER); + if (mucuser) { + _handel_muc_user(stanza); + } + + xmpp_stanza_t *conference = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CONFERENCE); + if (conference) { + _handle_conference(stanza); + } + + xmpp_stanza_t *captcha = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CAPTCHA); + if (captcha) { + _handle_captcha(stanza); + } + + xmpp_stanza_t *receipts = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_RECEIPTS); + if (receipts) { + _handle_receipt_received(stanza); + } + + _handle_chat(stanza); + + return 1; +} void message_add_handlers(void) { xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); - - HANDLE(NULL, STANZA_TYPE_ERROR, _message_error_handler); - HANDLE(NULL, STANZA_TYPE_GROUPCHAT, _groupchat_handler); - HANDLE(NULL, NULL, _chat_handler); - HANDLE(STANZA_NS_MUC_USER, NULL, _muc_user_handler); - HANDLE(STANZA_NS_CONFERENCE, NULL, _conference_handler); - HANDLE(STANZA_NS_CAPTCHA, NULL, _captcha_handler); - HANDLE(STANZA_NS_RECEIPTS, NULL, _receipt_received_handler); + xmpp_handler_add(conn, _message_handler, NULL, STANZA_NAME_MESSAGE, NULL, ctx); } static char* @@ -126,7 +165,6 @@ _session_state(const char *const barejid) char* message_send_chat(const char *const barejid, const char *const msg) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); char *state = _session_state(barejid); @@ -144,7 +182,7 @@ message_send_chat(const char *const barejid, const char *const msg) stanza_attach_receipt_request(ctx, message); } - _send_message_stanza(conn, message); + _send_message_stanza(message); xmpp_stanza_release(message); return id; @@ -153,7 +191,6 @@ message_send_chat(const char *const barejid, const char *const msg) char* message_send_chat_pgp(const char *const barejid, const char *const msg) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); char *state = _session_state(barejid); @@ -200,7 +237,7 @@ message_send_chat_pgp(const char *const barejid, const char *const msg) stanza_attach_receipt_request(ctx, message); } - _send_message_stanza(conn, message); + _send_message_stanza(message); xmpp_stanza_release(message); return id; @@ -209,7 +246,6 @@ message_send_chat_pgp(const char *const barejid, const char *const msg) char* message_send_chat_otr(const char *const barejid, const char *const msg) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); char *state = _session_state(barejid); @@ -231,7 +267,7 @@ message_send_chat_otr(const char *const barejid, const char *const msg) stanza_attach_receipt_request(ctx, message); } - _send_message_stanza(conn, message); + _send_message_stanza(message); xmpp_stanza_release(message); return id; @@ -240,37 +276,34 @@ message_send_chat_otr(const char *const barejid, const char *const msg) void message_send_private(const char *const fulljid, const char *const msg) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); char *id = create_unique_id("prv"); xmpp_stanza_t *message = stanza_create_message(ctx, id, fulljid, STANZA_TYPE_CHAT, msg); free(id); - _send_message_stanza(conn, message); + _send_message_stanza(message); xmpp_stanza_release(message); } void message_send_groupchat(const char *const roomjid, const char *const msg) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); char *id = create_unique_id("muc"); xmpp_stanza_t *message = stanza_create_message(ctx, id, roomjid, STANZA_TYPE_GROUPCHAT, msg); free(id); - _send_message_stanza(conn, message); + _send_message_stanza(message); xmpp_stanza_release(message); } void message_send_groupchat_subject(const char *const roomjid, const char *const subject) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *message = stanza_create_room_subject_message(ctx, roomjid, subject); - _send_message_stanza(conn, message); + _send_message_stanza(message); xmpp_stanza_release(message); } @@ -278,11 +311,9 @@ void message_send_invite(const char *const roomjid, const char *const contact, const char *const reason) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *stanza; - muc_member_type_t member_type = muc_member_type(roomjid); if (member_type == MUC_MEMBER_TYPE_PUBLIC) { log_debug("Sending direct invite to %s, for %s", contact, roomjid); @@ -293,18 +324,17 @@ message_send_invite(const char *const roomjid, const char *const contact, stanza = stanza_create_mediated_invite(ctx, roomjid, contact, reason); } - _send_message_stanza(conn, stanza); + _send_message_stanza(stanza); xmpp_stanza_release(stanza); } void message_send_composing(const char *const jid) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *stanza = stanza_create_chat_state(ctx, jid, STANZA_NAME_COMPOSING); - _send_message_stanza(conn, stanza); + _send_message_stanza(stanza); xmpp_stanza_release(stanza); } @@ -312,39 +342,34 @@ message_send_composing(const char *const jid) void message_send_paused(const char *const jid) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *stanza = stanza_create_chat_state(ctx, jid, STANZA_NAME_PAUSED); - _send_message_stanza(conn, stanza); + _send_message_stanza(stanza); xmpp_stanza_release(stanza); } void message_send_inactive(const char *const jid) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *stanza = stanza_create_chat_state(ctx, jid, STANZA_NAME_INACTIVE); - _send_message_stanza(conn, stanza); + _send_message_stanza(stanza); xmpp_stanza_release(stanza); } void message_send_gone(const char *const jid) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *stanza = stanza_create_chat_state(ctx, jid, STANZA_NAME_GONE); - _send_message_stanza(conn, stanza); + _send_message_stanza(stanza); xmpp_stanza_release(stanza); } -static int -_message_error_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) +static void +_handle_error(xmpp_stanza_t *const stanza) { - log_debug("Message stanza error handler fired"); - char *id = xmpp_stanza_get_id(stanza); char *jid = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); xmpp_stanza_t *error_stanza = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_ERROR); @@ -388,39 +413,35 @@ _message_error_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, voi } free(err_msg); - - return 1; } -static int -_muc_user_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) +static void +_handel_muc_user(xmpp_stanza_t *const stanza) { - log_debug("Message stanza muc user handler fired"); - xmpp_ctx_t *ctx = connection_get_ctx(); xmpp_stanza_t *xns_muc_user = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_USER); char *room = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); if (!room) { log_warning("Message received with no from attribute, ignoring"); - return 1; + return; } // XEP-0045 xmpp_stanza_t *invite = xmpp_stanza_get_child_by_name(xns_muc_user, STANZA_NAME_INVITE); if (!invite) { - return 1; + return; } char *invitor_jid = xmpp_stanza_get_attribute(invite, STANZA_ATTR_FROM); if (!invitor_jid) { log_warning("Chat room invite received with no from attribute"); - return 1; + return; } Jid *jidp = jid_create(invitor_jid); if (!jidp) { - return 1; + return; } char *invitor = jidp->barejid; @@ -444,33 +465,29 @@ _muc_user_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *co if (password) { xmpp_free(ctx, password); } - - return 1; } -static int -_conference_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) +static void +_handle_conference(xmpp_stanza_t *const stanza) { - log_debug("Message stanza conference handler fired"); - xmpp_stanza_t *xns_conference = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CONFERENCE); char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); if (!from) { log_warning("Message received with no from attribute, ignoring"); - return 1; + return; } Jid *jidp = jid_create(from); if (!jidp) { - return 1; + return; } // XEP-0249 char *room = xmpp_stanza_get_attribute(xns_conference, STANZA_ATTR_JID); if (!room) { jid_destroy(jidp); - return 1; + return; } char *reason = xmpp_stanza_get_attribute(xns_conference, STANZA_ATTR_REASON); @@ -478,45 +495,37 @@ _conference_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void * sv_ev_room_invite(INVITE_DIRECT, jidp->barejid, room, reason, password); jid_destroy(jidp); - - return 1; } -static int -_captcha_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) +static void +_handle_captcha(xmpp_stanza_t *const stanza) { - log_debug("Message stanza captcha handler fired"); - xmpp_ctx_t *ctx = connection_get_ctx(); char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); if (!from) { log_warning("Message received with no from attribute, ignoring"); - return 1; + return; } // XEP-0158 xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY); if (!body) { - return 1; + return; } char *message = xmpp_stanza_get_text(body); if (!message) { - return 1; + return; } sv_ev_room_broadcast(from, message); xmpp_free(ctx, message); - - return 1; } -static int -_groupchat_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) +static void +_handle_groupchat(xmpp_stanza_t *const stanza) { - log_debug("Message stanza groupchat handler fired"); - xmpp_ctx_t *ctx = connection_get_ctx(); char *message = NULL; char *room_jid = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); @@ -530,7 +539,7 @@ _groupchat_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *c xmpp_free(ctx, message); jid_destroy(jid); - return 1; + return; } // handle room broadcasts @@ -538,33 +547,33 @@ _groupchat_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *c xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY); if (!body) { jid_destroy(jid); - return 1; + return; } message = xmpp_stanza_get_text(body); if (!message) { jid_destroy(jid); - return 1; + return; } sv_ev_room_broadcast(room_jid, message); xmpp_free(ctx, message); jid_destroy(jid); - return 1; + return; } if (!jid_is_valid_room_form(jid)) { log_error("Invalid room JID: %s", jid->str); jid_destroy(jid); - return 1; + return; } // room not active in profanity if (!muc_active(jid->barejid)) { log_error("Message received for inactive chat room: %s", jid->str); jid_destroy(jid); - return 1; + return; } xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY); @@ -572,13 +581,13 @@ _groupchat_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *c // check for and deal with message if (!body) { jid_destroy(jid); - return 1; + return; } message = xmpp_stanza_get_text(body); if (!message) { jid_destroy(jid); - return 1; + return; } // determine if the notifications happened whilst offline @@ -592,14 +601,11 @@ _groupchat_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *c xmpp_free(ctx, message); jid_destroy(jid); - - return 1; } void _message_send_receipt(const char *const fulljid, const char *const message_id) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *message = xmpp_stanza_new(ctx); xmpp_stanza_set_name(message, STANZA_NAME_MESSAGE); @@ -616,36 +622,32 @@ _message_send_receipt(const char *const fulljid, const char *const message_id) xmpp_stanza_add_child(message, receipt); xmpp_stanza_release(receipt); - _send_message_stanza(conn, message); + _send_message_stanza(message); xmpp_stanza_release(message); } -static int -_receipt_received_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) +static void +_handle_receipt_received(xmpp_stanza_t *const stanza) { - log_debug("Message stanza receipt received handler fired"); - xmpp_stanza_t *receipt = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_RECEIPTS); char *name = xmpp_stanza_get_name(receipt); if (g_strcmp0(name, "received") != 0) { - return 1; + return; } char *id = xmpp_stanza_get_attribute(receipt, STANZA_ATTR_ID); if (!id) { - return 1; + return; } char *fulljid = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); if (!fulljid) { - return 1; + return; } Jid *jidp = jid_create(fulljid); sv_ev_message_receipt(jidp->barejid, id); jid_destroy(jidp); - - return 1; } void @@ -761,33 +763,26 @@ _handle_carbons(xmpp_stanza_t *const stanza) return FALSE; } -static int -_chat_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) +static void +_handle_chat(xmpp_stanza_t *const stanza) { - log_debug("Message stanza chat handler fired"); - - gboolean cont = _receive_message_stanza(stanza); - if (!cont) { - return 1; - } - // ignore if type not chat or absent char *type = xmpp_stanza_get_type(stanza); if (!(g_strcmp0(type, "chat") == 0 || type == NULL)) { - return 1; + return; } // check if carbon message gboolean res = _handle_carbons(stanza); if (res) { - return 1; + return; } // ignore handled namespaces xmpp_stanza_t *conf = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CONFERENCE); xmpp_stanza_t *captcha = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CAPTCHA); if (conf || captcha) { - return 1; + return; } // some clients send the mucuser namespace with private messages @@ -796,7 +791,7 @@ _chat_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const xmpp_stanza_t *mucuser = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_USER); xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY); if (mucuser && body == NULL) { - return 1; + return; } gchar *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); @@ -806,7 +801,7 @@ _chat_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const if (muc_active(jid->barejid)) { _private_chat_handler(stanza, jid->fulljid); jid_destroy(jid); - return 1; + return; } // standard chat message, use jid without resource @@ -852,16 +847,16 @@ _chat_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const if (timestamp) g_date_time_unref(timestamp); jid_destroy(jid); - return 1; } static void -_send_message_stanza(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza) +_send_message_stanza(xmpp_stanza_t *const stanza) { char *text; size_t text_size; xmpp_stanza_to_text(stanza, &text, &text_size); + xmpp_conn_t *conn = connection_get_conn(); char *plugin_text = plugins_on_message_stanza_send(text); if (plugin_text) { xmpp_send_raw_string(conn, "%s", plugin_text); @@ -869,13 +864,3 @@ _send_message_stanza(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza) xmpp_send_raw_string(conn, "%s", text); } } - -static gboolean -_receive_message_stanza(xmpp_stanza_t *const stanza) -{ - char *text; - size_t text_size; - xmpp_stanza_to_text(stanza, &text, &text_size); - - return plugins_on_message_stanza_receive(text); -} diff --git a/src/xmpp/presence.c b/src/xmpp/presence.c index ce163ae1..888e936d 100644 --- a/src/xmpp/presence.c +++ b/src/xmpp/presence.c @@ -63,23 +63,20 @@ static Autocomplete sub_requests_ac; -#define HANDLE(ns, type, func) xmpp_handler_add(conn, func, ns, STANZA_NAME_PRESENCE, type, ctx) +static int _presence_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -// regular handlers -static int _unavailable_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _subscribe_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _subscribed_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _unsubscribed_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _available_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _muc_user_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _presence_error_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); +static void _presence_error_handler(xmpp_stanza_t *const stanza); +static void _unavailable_handler(xmpp_stanza_t *const stanza); +static void _subscribe_handler(xmpp_stanza_t *const stanza); +static void _subscribed_handler(xmpp_stanza_t *const stanza); +static void _unsubscribed_handler(xmpp_stanza_t *const stanza); +static void _muc_user_handler(xmpp_stanza_t *const stanza); +static void _available_handler(xmpp_stanza_t *const stanza); void _send_caps_request(char *node, char *caps_key, char *id, char *from); -static void _send_room_presence(xmpp_conn_t *conn, xmpp_stanza_t *presence); +static void _send_room_presence(xmpp_stanza_t *presence); -static void _send_presence_stanza(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza); -static void _send_iq_stanza(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza); -static gboolean _receive_presence_stanza(xmpp_stanza_t *const stanza); +static void _send_presence_stanza(xmpp_stanza_t *const stanza); void presence_sub_requests_init(void) @@ -87,19 +84,57 @@ presence_sub_requests_init(void) sub_requests_ac = autocomplete_new(); } +static int +_presence_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) +{ + log_debug("Presence stanza handler fired"); + + char *text; + size_t text_size; + xmpp_stanza_to_text(stanza, &text, &text_size); + gboolean cont = plugins_on_presence_stanza_receive(text); + if (!cont) { + return 1; + } + + char *type = xmpp_stanza_get_type(stanza); + + if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { + _presence_error_handler(stanza); + } + + if (g_strcmp0(type, STANZA_TYPE_UNAVAILABLE) == 0) { + _unavailable_handler(stanza); + } + + if (g_strcmp0(type, STANZA_TYPE_SUBSCRIBE) == 0) { + _subscribe_handler(stanza); + } + + if (g_strcmp0(type, STANZA_TYPE_SUBSCRIBED) == 0) { + _subscribed_handler(stanza); + } + + if (g_strcmp0(type, STANZA_TYPE_UNSUBSCRIBED) == 0) { + _unsubscribed_handler(stanza); + } + + xmpp_stanza_t *mucuser = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_USER); + if (mucuser) { + _muc_user_handler(stanza); + } + + _available_handler(stanza); + + return 1; +} + void presence_add_handlers(void) { xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); - - HANDLE(NULL, STANZA_TYPE_ERROR, _presence_error_handler); - HANDLE(STANZA_NS_MUC_USER, NULL, _muc_user_handler); - HANDLE(NULL, STANZA_TYPE_UNAVAILABLE, _unavailable_handler); - HANDLE(NULL, STANZA_TYPE_SUBSCRIBE, _subscribe_handler); - HANDLE(NULL, STANZA_TYPE_SUBSCRIBED, _subscribed_handler); - HANDLE(NULL, STANZA_TYPE_UNSUBSCRIBED, _unsubscribed_handler); - HANDLE(NULL, NULL, _available_handler); + xmpp_handler_add(conn, _presence_handler, NULL, STANZA_NAME_PRESENCE, NULL, ctx); } void @@ -108,7 +143,6 @@ presence_subscription(const char *const jid, const jabber_subscr_t action) assert(jid != NULL); xmpp_ctx_t * const ctx = connection_get_ctx(); - xmpp_conn_t * const conn = connection_get_conn(); const char *type = NULL; Jid *jidp = jid_create(jid); @@ -140,7 +174,7 @@ presence_subscription(const char *const jid, const jabber_subscr_t action) xmpp_stanza_set_name(presence, STANZA_NAME_PRESENCE); xmpp_stanza_set_type(presence, type); xmpp_stanza_set_attribute(presence, STANZA_ATTR_TO, jidp->barejid); - _send_presence_stanza(conn, presence); + _send_presence_stanza(presence); xmpp_stanza_release(presence); jid_destroy(jidp); @@ -214,7 +248,6 @@ presence_send(const resource_presence_t presence_type, const char *const msg, co } xmpp_ctx_t * const ctx = connection_get_ctx(); - xmpp_conn_t * const conn = connection_get_conn(); const int pri = accounts_get_priority_for_presence_type(jabber_get_account_name(), presence_type); const char *show = stanza_get_presence_string_from_type(presence_type); @@ -245,8 +278,8 @@ presence_send(const resource_presence_t presence_type, const char *const msg, co stanza_attach_last_activity(ctx, presence, idle); } stanza_attach_caps(ctx, presence); - _send_presence_stanza(conn, presence); - _send_room_presence(conn, presence); + _send_presence_stanza(presence); + _send_room_presence(presence); xmpp_stanza_release(presence); // set last presence for account @@ -261,7 +294,7 @@ presence_send(const resource_presence_t presence_type, const char *const msg, co } static void -_send_room_presence(xmpp_conn_t *conn, xmpp_stanza_t *presence) +_send_room_presence(xmpp_stanza_t *presence) { GList *rooms_p = muc_rooms(); GList *rooms = rooms_p; @@ -275,7 +308,7 @@ _send_room_presence(xmpp_conn_t *conn, xmpp_stanza_t *presence) xmpp_stanza_set_attribute(presence, STANZA_ATTR_TO, full_room_jid); log_debug("Sending presence to room: %s", full_room_jid); - _send_presence_stanza(conn, presence); + _send_presence_stanza(presence); free(full_room_jid); } @@ -294,7 +327,6 @@ presence_join_room(char *room, char *nick, char * passwd) log_debug("Sending room join presence to: %s", jid->fulljid); xmpp_ctx_t *ctx = connection_get_ctx(); - xmpp_conn_t *conn = connection_get_conn(); resource_presence_t presence_type = accounts_get_last_presence(jabber_get_account_name()); const char *show = stanza_get_presence_string_from_type(presence_type); @@ -308,7 +340,7 @@ presence_join_room(char *room, char *nick, char * passwd) stanza_attach_priority(ctx, presence, pri); stanza_attach_caps(ctx, presence); - _send_presence_stanza(conn, presence); + _send_presence_stanza(presence); xmpp_stanza_release(presence); jid_destroy(jid); @@ -322,7 +354,6 @@ presence_change_room_nick(const char *const room, const char *const nick) log_debug("Sending room nickname change to: %s, nick: %s", room, nick); xmpp_ctx_t *ctx = connection_get_ctx(); - xmpp_conn_t *conn = connection_get_conn(); resource_presence_t presence_type = accounts_get_last_presence(jabber_get_account_name()); const char *show = stanza_get_presence_string_from_type(presence_type); @@ -338,7 +369,7 @@ presence_change_room_nick(const char *const room, const char *const nick) stanza_attach_priority(ctx, presence, pri); stanza_attach_caps(ctx, presence); - _send_presence_stanza(conn, presence); + _send_presence_stanza(presence); xmpp_stanza_release(presence); free(full_room_jid); @@ -351,23 +382,18 @@ presence_leave_chat_room(const char *const room_jid) log_debug("Sending room leave presence to: %s", room_jid); xmpp_ctx_t *ctx = connection_get_ctx(); - xmpp_conn_t *conn = connection_get_conn(); char *nick = muc_nick(room_jid); if (nick) { - xmpp_stanza_t *presence = stanza_create_room_leave_presence(ctx, room_jid, - nick); - _send_presence_stanza(conn, presence); + xmpp_stanza_t *presence = stanza_create_room_leave_presence(ctx, room_jid, nick); + _send_presence_stanza(presence); xmpp_stanza_release(presence); } } -static int -_presence_error_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +static void +_presence_error_handler(xmpp_stanza_t *const stanza) { - log_debug("Presence stanza error handler fired"); - char *id = xmpp_stanza_get_id(stanza); char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); xmpp_stanza_t *error_stanza = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_ERROR); @@ -400,7 +426,7 @@ _presence_error_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, } cons_show_error("Error joining room %s, reason: %s", fulljid->barejid, error_cond); jid_destroy(fulljid); - return 1; + return; } // stanza_get_error never returns NULL @@ -433,17 +459,12 @@ _presence_error_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, } free(err_msg); - - return 1; } -static int -_unsubscribed_handler(xmpp_conn_t *const conn, - xmpp_stanza_t *const stanza, void *const userdata) +static void +_unsubscribed_handler(xmpp_stanza_t *const stanza) { - log_debug("Presence stanza unsubscribed handler fired"); - char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); Jid *from_jid = jid_create(from); log_debug("Unsubscribed presence handler fired for %s", from); @@ -452,16 +473,11 @@ _unsubscribed_handler(xmpp_conn_t *const conn, autocomplete_remove(sub_requests_ac, from_jid->barejid); jid_destroy(from_jid); - - return 1; } -static int -_subscribed_handler(xmpp_conn_t *const conn, - xmpp_stanza_t *const stanza, void *const userdata) +static void +_subscribed_handler(xmpp_stanza_t *const stanza) { - log_debug("Presence stanza subscribed handler fired"); - char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); Jid *from_jid = jid_create(from); log_debug("Subscribed presence handler fired for %s", from); @@ -470,40 +486,31 @@ _subscribed_handler(xmpp_conn_t *const conn, autocomplete_remove(sub_requests_ac, from_jid->barejid); jid_destroy(from_jid); - - return 1; } -static int -_subscribe_handler(xmpp_conn_t *const conn, - xmpp_stanza_t *const stanza, void *const userdata) +static void +_subscribe_handler(xmpp_stanza_t *const stanza) { - log_debug("Presence stanza subscribe handler fired"); - char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); log_debug("Subscribe presence handler fired for %s", from); Jid *from_jid = jid_create(from); if (from_jid == NULL) { - return 1; + return; } sv_ev_subscription(from_jid->barejid, PRESENCE_SUBSCRIBE); autocomplete_add(sub_requests_ac, from_jid->barejid); jid_destroy(from_jid); - - return 1; } -static int -_unavailable_handler(xmpp_conn_t *const conn, - xmpp_stanza_t *const stanza, void *const userdata) +static void +_unavailable_handler(xmpp_stanza_t *const stanza) { - log_debug("Presence stanza unavailale handler fired"); - inp_nonblocking(TRUE); + xmpp_conn_t *conn = connection_get_conn(); const char *jid = xmpp_conn_get_jid(conn); char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); log_debug("Unavailable presence handler fired for %s", from); @@ -513,7 +520,7 @@ _unavailable_handler(xmpp_conn_t *const conn, if (my_jid == NULL || from_jid == NULL) { jid_destroy(my_jid); jid_destroy(from_jid); - return 1; + return; } char *status_str = stanza_get_status(stanza, NULL); @@ -535,8 +542,6 @@ _unavailable_handler(xmpp_conn_t *const conn, free(status_str); jid_destroy(my_jid); jid_destroy(from_jid); - - return 1; } static void @@ -575,21 +580,14 @@ _handle_caps(char *jid, XMPPCaps *caps) } } -static int -_available_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) +static void +_available_handler(xmpp_stanza_t *const stanza) { - log_debug("Presence stanza available handler fired"); - - gboolean cont = _receive_presence_stanza(stanza); - if (!cont) { - return 1; - } - inp_nonblocking(TRUE); // handler still fires if error if (g_strcmp0(xmpp_stanza_get_type(stanza), STANZA_TYPE_ERROR) == 0) { - return 1; + return; } // handler still fires if other types @@ -597,12 +595,12 @@ _available_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *c (g_strcmp0(xmpp_stanza_get_type(stanza), STANZA_TYPE_SUBSCRIBE) == 0) || (g_strcmp0(xmpp_stanza_get_type(stanza), STANZA_TYPE_SUBSCRIBED) == 0) || (g_strcmp0(xmpp_stanza_get_type(stanza), STANZA_TYPE_UNSUBSCRIBED) == 0)) { - return 1; + return; } // handler still fires for muc presence if (stanza_is_muc_presence(stanza)) { - return 1; + return; } int err = 0; @@ -622,12 +620,13 @@ _available_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *c log_warning("Available presence handler fired, could not parse stanza."); break; } - return 1; + return; } else { char *jid = jid_fulljid_or_barejid(xmpp_presence->jid); log_debug("Presence available handler fired for: %s", jid); } + xmpp_conn_t *conn = connection_get_conn(); const char *my_jid_str = xmpp_conn_get_jid(conn); Jid *my_jid = jid_create(my_jid_str); @@ -656,22 +655,19 @@ _available_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *c jid_destroy(my_jid); stanza_free_presence(xmpp_presence); - - return 1; } void _send_caps_request(char *node, char *caps_key, char *id, char *from) { xmpp_ctx_t *ctx = connection_get_ctx(); - xmpp_conn_t *conn = connection_get_conn(); if (node) { log_debug("Node string: %s.", node); if (!caps_contains(caps_key)) { log_debug("Capabilities not cached for '%s', sending discovery IQ.", from); xmpp_stanza_t *iq = stanza_create_disco_info_iq(ctx, id, from, node); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } else { log_debug("Capabilities already cached, for %s", caps_key); @@ -681,11 +677,9 @@ _send_caps_request(char *node, char *caps_key, char *id, char *from) } } -static int -_muc_user_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) +static void +_muc_user_handler(xmpp_stanza_t *const stanza) { - log_debug("Presence stanza muc user handler fired"); - inp_nonblocking(TRUE); char *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE); @@ -693,14 +687,14 @@ _muc_user_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *co // handler still fires if error if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { - return 1; + return; } // invalid from attribute Jid *from_jid = jid_create(from); if (from_jid == NULL || from_jid->resourcepart == NULL) { jid_destroy(from_jid); - return 1; + return; } char *room = from_jid->barejid; @@ -832,17 +826,16 @@ _muc_user_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *co free(show_str); free(status_str); jid_destroy(from_jid); - - return 1; } static void -_send_presence_stanza(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza) +_send_presence_stanza(xmpp_stanza_t *const stanza) { char *text; size_t text_size; xmpp_stanza_to_text(stanza, &text, &text_size); + xmpp_conn_t *conn = connection_get_conn(); char *plugin_text = plugins_on_presence_stanza_send(text); if (plugin_text) { xmpp_send_raw_string(conn, "%s", plugin_text); @@ -850,29 +843,3 @@ _send_presence_stanza(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza) xmpp_send_raw_string(conn, "%s", text); } } - -static void -_send_iq_stanza(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza) -{ - char *text; - size_t text_size; - xmpp_stanza_to_text(stanza, &text, &text_size); - - char *plugin_text = plugins_on_iq_stanza_send(text); - if (plugin_text) { - xmpp_send_raw_string(conn, "%s", plugin_text); - } else { - xmpp_send_raw_string(conn, "%s", text); - } -} - -static gboolean -_receive_presence_stanza(xmpp_stanza_t *const stanza) -{ - char *text; - size_t text_size; - xmpp_stanza_to_text(stanza, &text, &text_size); - - return plugins_on_presence_stanza_receive(text); -} - diff --git a/src/xmpp/roster.c b/src/xmpp/roster.c index d1371e5e..9605f611 100644 --- a/src/xmpp/roster.c +++ b/src/xmpp/roster.c @@ -62,78 +62,56 @@ #include "xmpp/xmpp.h" #include "plugins/plugins.h" -#define HANDLE(type, func) xmpp_handler_add(conn, func, XMPP_NS_ROSTER, STANZA_NAME_IQ, type, ctx) - // callback data for group commands typedef struct _group_data { char *name; char *group; } GroupData; -// event handlers -static int _roster_set_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _roster_result_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); - // id handlers -static int _group_add_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); -static int _group_remove_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); - -static void _send_iq_stanza(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza); +static int _group_add_id_handler(xmpp_stanza_t *const stanza, void *const userdata); +static int _group_remove_id_handler(xmpp_stanza_t *const stanza, void *const userdata); // helper functions GSList* _get_groups_from_item(xmpp_stanza_t *item); 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_set_handler); - HANDLE(STANZA_TYPE_RESULT, _roster_result_handler); -} - -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); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } void roster_send_add_new(const char *const barejid, const char *const name) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); char *id = create_unique_id("roster"); xmpp_stanza_t *iq = stanza_create_roster_set(ctx, id, barejid, name, NULL); free(id); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } void roster_send_remove(const char *const barejid) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_create_roster_remove_set(ctx, barejid); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } void roster_send_name_change(const char *const barejid, const char *const new_name, GSList *groups) { - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); char *id = create_unique_id("roster"); xmpp_stanza_t *iq = stanza_create_roster_set(ctx, id, barejid, new_name, groups); free(id); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); } @@ -158,22 +136,18 @@ roster_send_add_to_group(const char *const group, PContact contact) data->name = strdup(p_contact_barejid(contact)); } - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); - xmpp_id_handler_add(conn, _group_add_id_handler, unique_id, data); + id_handler_add(unique_id, _group_add_id_handler, data); xmpp_stanza_t *iq = stanza_create_roster_set(ctx, unique_id, p_contact_barejid(contact), p_contact_name(contact), new_groups); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); free(unique_id); } static int -_group_add_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +_group_add_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza group add id handler fired"); - if (userdata) { GroupData *data = userdata; ui_group_added(data->name, data->group); @@ -196,7 +170,6 @@ roster_send_remove_from_group(const char *const group, PContact contact) groups = g_slist_next(groups); } - xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); // add an id handler to handle the response @@ -209,20 +182,17 @@ roster_send_remove_from_group(const char *const group, PContact contact) data->name = strdup(p_contact_barejid(contact)); } - xmpp_id_handler_add(conn, _group_remove_id_handler, unique_id, data); + id_handler_add(unique_id, _group_remove_id_handler, data); xmpp_stanza_t *iq = stanza_create_roster_set(ctx, unique_id, p_contact_barejid(contact), p_contact_name(contact), new_groups); - _send_iq_stanza(conn, iq); + send_iq_stanza(iq); xmpp_stanza_release(iq); free(unique_id); } static int -_group_remove_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +_group_remove_id_handler(xmpp_stanza_t *const stanza, void *const userdata) { - log_debug("iq stanza group remove id handler fired"); - if (userdata) { GroupData *data = userdata; ui_group_removed(data->name, data->group); @@ -233,19 +203,16 @@ _group_remove_id_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, return 0; } -static int -_roster_set_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, - void *const userdata) +void +roster_set_handler(xmpp_stanza_t *const stanza) { - log_debug("iq stanza roster set handler fired"); - 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; + return; } // if from attribute exists and it is not current users barejid, ignore push @@ -253,7 +220,7 @@ _roster_set_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); if (from && (strcmp(from, my_jid->barejid) != 0)) { jid_destroy(my_jid); - return 1; + return; } jid_destroy(my_jid); @@ -302,18 +269,16 @@ _roster_set_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, g_free(barejid_lower); - return 1; + return; } -static int -_roster_result_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) +void +roster_result_handler(xmpp_stanza_t *const stanza) { - log_debug("iq stanza roster result handler fired"); - const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); if (g_strcmp0(id, "roster") != 0) { - return 1; + return; } // handle initial roster response @@ -348,7 +313,7 @@ _roster_result_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, voi sv_ev_roster_received(); - return 1; + return; } GSList* @@ -369,18 +334,3 @@ _get_groups_from_item(xmpp_stanza_t *item) return groups; } - -static void -_send_iq_stanza(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza) -{ - char *text; - size_t text_size; - xmpp_stanza_to_text(stanza, &text, &text_size); - - char *plugin_text = plugins_on_iq_stanza_send(text); - if (plugin_text) { - xmpp_send_raw_string(conn, "%s", plugin_text); - } else { - xmpp_send_raw_string(conn, "%s", text); - } -} |