diff options
Diffstat (limited to 'src/xmpp/iq.c')
-rw-r--r-- | src/xmpp/iq.c | 194 |
1 files changed, 193 insertions, 1 deletions
diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index 1242c6cf..ef1a45b5 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -64,6 +64,7 @@ #include "roster_list.h" #include "xmpp/xmpp.h" #include "plugins/plugins.h" +#include "tools/http_upload.h" typedef struct p_room_info_data_t { char *room; @@ -87,6 +88,8 @@ 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 _disco_info_response_id_handler_onconnect(xmpp_stanza_t *const stanza, void *const userdata); +static int _http_upload_response_id_handler(xmpp_stanza_t *const stanza, void *const upload_ctx); 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); @@ -293,6 +296,42 @@ iq_disable_carbons(void) } void +iq_http_upload_request(HTTPUpload *upload) +{ + GSList *disco_items = jabber_get_disco_items(); + DiscoInfo *disco_info; + if (disco_items && (g_slist_length(disco_items) > 0)) { + while (disco_items) { + disco_info = disco_items->data; + if (g_hash_table_lookup_extended(disco_info->features, STANZA_NS_HTTP_UPLOAD, NULL, NULL)) { + break; + } + disco_items = g_slist_next(disco_items); + if (!disco_items) { + cons_show_error("XEP-0363 HTTP File Upload is not supported by the server"); + return; + } + } + } else { + cons_show_error("No disco items"); + return; + } + + xmpp_ctx_t * const ctx = connection_get_ctx(); + char *id = create_unique_id("http_upload_request"); + + xmpp_stanza_t *iq = stanza_create_http_upload_request(ctx, id, disco_info->item, upload); + + id_handler_add(id, _http_upload_response_id_handler, upload); + + free(id); + + send_iq_stanza(iq); + xmpp_stanza_release(iq); + return; +} + +void iq_disco_info_request(gchar *jid) { xmpp_ctx_t * const ctx = connection_get_ctx(); @@ -308,6 +347,21 @@ iq_disco_info_request(gchar *jid) } void +iq_disco_info_request_onconnect(gchar *jid) +{ + xmpp_ctx_t * const ctx = connection_get_ctx(); + char *id = create_unique_id("disco_info_onconnect"); + xmpp_stanza_t *iq = stanza_create_disco_info_iq(ctx, id, jid, NULL); + + id_handler_add(id, _disco_info_response_id_handler_onconnect, NULL); + + free(id); + + send_iq_stanza(iq); + xmpp_stanza_release(iq); +} + +void iq_last_activity_request(gchar *jid) { xmpp_ctx_t * const ctx = connection_get_ctx(); @@ -429,6 +483,15 @@ iq_disco_items_request(gchar *jid) } void +iq_disco_items_request_onconnect(gchar *jid) +{ + xmpp_ctx_t * const ctx = connection_get_ctx(); + xmpp_stanza_t *iq = stanza_create_disco_items_iq(ctx, "discoitemsreq_onconnect", jid); + send_iq_stanza(iq); + xmpp_stanza_release(iq); +} + +void iq_send_software_version(const char *const fulljid) { xmpp_ctx_t * const ctx = connection_get_ctx(); @@ -1817,6 +1880,122 @@ _disco_info_response_id_handler(xmpp_stanza_t *const stanza, void *const userdat return 0; } +static int +_disco_info_response_id_handler_onconnect(xmpp_stanza_t *const stanza, void *const userdata) +{ + const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); + const char *type = xmpp_stanza_get_type(stanza); + + if (from) { + log_info("Received disco#info response from: %s", from); + } else { + log_info("Received disco#info response"); + } + + // handle error responses + if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { + char *error_message = stanza_get_error_message(stanza); + if (from) { + log_error("Service discovery failed for %s: %s", from, error_message); + } else { + log_error("Service discovery failed: %s", error_message); + } + free(error_message); + return 0; + } + + xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); + + if (query) { + xmpp_stanza_t *child = xmpp_stanza_get_children(query); + + GSList *disco_items = jabber_get_disco_items(); + DiscoInfo *disco_info; + if (disco_items && (g_slist_length(disco_items) > 0)) { + while (disco_items) { + disco_info = disco_items->data; + if (g_strcmp0(disco_info->item, from) == 0) { + break; + } + disco_items = g_slist_next(disco_items); + if (!disco_items) { + log_error("No matching disco item found for %s", from); + return 1; + } + } + } else { + return 1; + } + + while (child) { + const char *stanza_name = xmpp_stanza_get_name(child); + if (g_strcmp0(stanza_name, STANZA_NAME_FEATURE) == 0) { + const char *var = xmpp_stanza_get_attribute(child, STANZA_ATTR_VAR); + if (var) { + g_hash_table_add(disco_info->features, strdup(var)); + } + } + child = xmpp_stanza_get_next(child); + } + } + + return 0; +} + +static int +_http_upload_response_id_handler(xmpp_stanza_t *const stanza, void *const userdata) +{ + HTTPUpload *upload = (HTTPUpload *)userdata; + const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); + const char *type = xmpp_stanza_get_type(stanza); + + if (from) { + log_info("Received http_upload response from: %s", from); + } else { + log_info("Received http_upload response"); + } + + // handle error responses + if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { + char *error_message = stanza_get_error_message(stanza); + if (from) { + cons_show_error("Uploading '%s' failed for %s: %s", upload->filename, from, error_message); + } else { + cons_show_error("Uploading '%s' failed: %s", upload->filename, error_message); + } + free(error_message); + return 0; + } + + xmpp_stanza_t *slot = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_SLOT); + + if (slot && g_strcmp0(xmpp_stanza_get_ns(slot), STANZA_NS_HTTP_UPLOAD) == 0) { + xmpp_stanza_t *put = xmpp_stanza_get_child_by_name(slot, STANZA_NAME_PUT); + xmpp_stanza_t *get = xmpp_stanza_get_child_by_name(slot, STANZA_NAME_GET); + + if (put && get) { + char *put_url = xmpp_stanza_get_text(put); + char *get_url = xmpp_stanza_get_text(get); + + upload->put_url = strdup(put_url); + upload->get_url = strdup(get_url); + + xmpp_conn_t *conn = connection_get_conn(); + xmpp_ctx_t *ctx = xmpp_conn_get_context(conn); + if (put_url) xmpp_free(ctx, put_url); + if (get_url) xmpp_free(ctx, get_url); + + pthread_create(&(upload->worker), NULL, &http_file_put, upload); + upload_processes = g_slist_append(upload_processes, upload); + } else { + log_error("Invalid XML in HTTP Upload slot"); + return 1; + } + } + + return 0; +} + static void _disco_items_result_handler(xmpp_stanza_t *const stanza) { @@ -1825,7 +2004,7 @@ _disco_items_result_handler(xmpp_stanza_t *const stanza) const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); GSList *items = NULL; - if ((g_strcmp0(id, "confreq") == 0) || (g_strcmp0(id, "discoitemsreq") == 0)) { + if ((g_strcmp0(id, "confreq") == 0) || (g_strcmp0(id, "discoitemsreq") == 0) || (g_strcmp0(id, "discoitemsreq_onconnect") == 0)) { log_debug("Response to query: %s", id); xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); @@ -1857,6 +2036,19 @@ _disco_items_result_handler(xmpp_stanza_t *const stanza) cons_show_room_list(items, from); } else if (g_strcmp0(id, "discoitemsreq") == 0) { cons_show_disco_items(items, from); + } else if (g_strcmp0(id, "discoitemsreq_onconnect") == 0) { + GSList *res_items = items; + if (res_items && (g_slist_length(res_items) > 0)) { + while (res_items) { + DiscoItem *item = res_items->data; + DiscoInfo *info = malloc(sizeof(struct disco_info_t)); + info->item = strdup(item->jid); + info->features = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL); + jabber_set_disco_items(g_slist_append(jabber_get_disco_items(), info)); + iq_disco_info_request_onconnect(info->item); + res_items = g_slist_next(res_items); + } + } } g_slist_free_full(items, (GDestroyNotify)_item_destroy); |