2013-01-19 19:16:12 -05:00
|
|
|
/*
|
2013-02-02 14:47:41 -05:00
|
|
|
* capabilities.c
|
2013-01-19 19:16:12 -05:00
|
|
|
*
|
2014-03-08 20:18:19 -05:00
|
|
|
* Copyright (C) 2012 - 2014 James Booth <boothj5@gmail.com>
|
2013-01-19 19:16:12 -05:00
|
|
|
*
|
|
|
|
* This file is part of Profanity.
|
|
|
|
*
|
|
|
|
* Profanity is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Profanity is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Profanity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2014-08-24 15:57:39 -04:00
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link the code of portions of this program with the OpenSSL library under
|
|
|
|
* certain conditions as described in each individual source file, and
|
|
|
|
* distribute linked combinations including the two.
|
|
|
|
*
|
|
|
|
* You must obey the GNU General Public License in all respects for all of the
|
|
|
|
* code used other than OpenSSL. If you modify file(s) with this exception, you
|
|
|
|
* may extend this exception to your version of the file(s), but you are not
|
|
|
|
* obligated to do so. If you do not wish to do so, delete this exception
|
|
|
|
* statement from your version. If you delete this exception statement from all
|
|
|
|
* source files in the program, then also delete it here.
|
|
|
|
*
|
2013-01-19 19:16:12 -05:00
|
|
|
*/
|
|
|
|
|
2013-02-02 15:55:58 -05:00
|
|
|
#include "config.h"
|
|
|
|
|
2013-09-16 20:28:35 -04:00
|
|
|
#ifdef HAVE_GIT_VERSION
|
2014-03-05 19:06:22 -05:00
|
|
|
#include "gitversion.h"
|
2013-09-16 20:28:35 -04:00
|
|
|
#endif
|
|
|
|
|
2013-01-19 19:16:12 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-01-19 19:35:40 -05:00
|
|
|
#include <glib.h>
|
2013-01-21 17:18:15 -05:00
|
|
|
#include <strophe.h>
|
2013-01-19 19:35:40 -05:00
|
|
|
|
2013-01-19 19:16:12 -05:00
|
|
|
#include "common.h"
|
2013-02-02 15:55:58 -05:00
|
|
|
#include "xmpp/xmpp.h"
|
|
|
|
#include "xmpp/stanza.h"
|
2014-09-05 16:04:16 -04:00
|
|
|
#include "xmpp/form.h"
|
2013-01-19 19:16:12 -05:00
|
|
|
|
2013-01-19 19:57:44 -05:00
|
|
|
static GHashTable *capabilities;
|
|
|
|
|
|
|
|
static void _caps_destroy(Capabilities *caps);
|
2013-01-19 19:35:40 -05:00
|
|
|
|
|
|
|
void
|
|
|
|
caps_init(void)
|
|
|
|
{
|
|
|
|
capabilities = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
|
2013-01-19 19:57:44 -05:00
|
|
|
(GDestroyNotify)_caps_destroy);
|
2013-01-19 19:35:40 -05:00
|
|
|
}
|
|
|
|
|
2013-01-19 19:57:44 -05:00
|
|
|
void
|
2013-02-05 18:06:30 -05:00
|
|
|
caps_add(const char * const caps_str, const char * const category,
|
|
|
|
const char * const type, const char * const name,
|
|
|
|
const char * const software, const char * const software_version,
|
2013-02-16 20:04:10 -05:00
|
|
|
const char * const os, const char * const os_version,
|
|
|
|
GSList *features)
|
2013-01-19 19:16:12 -05:00
|
|
|
{
|
|
|
|
Capabilities *new_caps = malloc(sizeof(struct capabilities_t));
|
|
|
|
|
2013-02-05 18:06:30 -05:00
|
|
|
if (category != NULL) {
|
|
|
|
new_caps->category = strdup(category);
|
2013-01-19 19:16:12 -05:00
|
|
|
} else {
|
2013-02-05 18:06:30 -05:00
|
|
|
new_caps->category = NULL;
|
|
|
|
}
|
|
|
|
if (type != NULL) {
|
|
|
|
new_caps->type = strdup(type);
|
|
|
|
} else {
|
|
|
|
new_caps->type = NULL;
|
|
|
|
}
|
|
|
|
if (name != NULL) {
|
|
|
|
new_caps->name = strdup(name);
|
|
|
|
} else {
|
|
|
|
new_caps->name = NULL;
|
|
|
|
}
|
|
|
|
if (software != NULL) {
|
|
|
|
new_caps->software = strdup(software);
|
|
|
|
} else {
|
|
|
|
new_caps->software = NULL;
|
|
|
|
}
|
|
|
|
if (software_version != NULL) {
|
|
|
|
new_caps->software_version = strdup(software_version);
|
|
|
|
} else {
|
|
|
|
new_caps->software_version = NULL;
|
|
|
|
}
|
|
|
|
if (os != NULL) {
|
|
|
|
new_caps->os = strdup(os);
|
|
|
|
} else {
|
|
|
|
new_caps->os = NULL;
|
|
|
|
}
|
|
|
|
if (os_version != NULL) {
|
|
|
|
new_caps->os_version = strdup(os_version);
|
|
|
|
} else {
|
|
|
|
new_caps->os_version = NULL;
|
2013-01-19 19:16:12 -05:00
|
|
|
}
|
2013-02-16 20:04:10 -05:00
|
|
|
if (features != NULL) {
|
|
|
|
new_caps->features = features;
|
|
|
|
} else {
|
|
|
|
new_caps->features = NULL;
|
|
|
|
}
|
2013-01-19 19:16:12 -05:00
|
|
|
|
2013-01-19 19:57:44 -05:00
|
|
|
g_hash_table_insert(capabilities, strdup(caps_str), new_caps);
|
2013-01-19 19:16:12 -05:00
|
|
|
}
|
|
|
|
|
2013-01-19 19:57:44 -05:00
|
|
|
gboolean
|
|
|
|
caps_contains(const char * const caps_str)
|
2013-01-19 19:16:12 -05:00
|
|
|
{
|
2013-01-19 19:57:44 -05:00
|
|
|
return (g_hash_table_lookup(capabilities, caps_str) != NULL);
|
2013-01-19 19:16:12 -05:00
|
|
|
}
|
2013-01-19 19:35:40 -05:00
|
|
|
|
2013-12-18 15:16:55 -05:00
|
|
|
static Capabilities *
|
|
|
|
_caps_get(const char * const caps_str)
|
2013-01-19 21:26:54 -05:00
|
|
|
{
|
|
|
|
return g_hash_table_lookup(capabilities, caps_str);
|
|
|
|
}
|
|
|
|
|
2013-01-21 17:18:15 -05:00
|
|
|
char *
|
2013-01-23 17:24:54 -05:00
|
|
|
caps_create_sha1_str(xmpp_stanza_t * const query)
|
2013-01-21 17:18:15 -05:00
|
|
|
{
|
2013-01-22 19:21:05 -05:00
|
|
|
char *category = NULL;
|
|
|
|
char *type = NULL;
|
|
|
|
char *lang = NULL;
|
|
|
|
char *name = NULL;
|
|
|
|
char *feature_str = NULL;
|
2013-01-21 17:18:15 -05:00
|
|
|
GSList *identities = NULL;
|
|
|
|
GSList *features = NULL;
|
|
|
|
GSList *form_names = NULL;
|
2013-01-22 19:21:05 -05:00
|
|
|
DataForm *form = NULL;
|
|
|
|
FormField *field = NULL;
|
2014-09-05 16:04:16 -04:00
|
|
|
GHashTable *forms = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)form_destroy);
|
2013-01-21 17:18:15 -05:00
|
|
|
|
|
|
|
GString *s = g_string_new("");
|
|
|
|
|
|
|
|
xmpp_stanza_t *child = xmpp_stanza_get_children(query);
|
|
|
|
while (child != NULL) {
|
|
|
|
if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_IDENTITY) == 0) {
|
2013-01-23 17:24:54 -05:00
|
|
|
category = xmpp_stanza_get_attribute(child, "category");
|
|
|
|
type = xmpp_stanza_get_attribute(child, "type");
|
|
|
|
lang = xmpp_stanza_get_attribute(child, "xml:lang");
|
|
|
|
name = xmpp_stanza_get_attribute(child, "name");
|
2013-01-21 17:18:15 -05:00
|
|
|
|
2013-08-03 07:38:38 -04:00
|
|
|
GString *identity_str = g_string_new(category);
|
2013-01-21 17:18:15 -05:00
|
|
|
g_string_append(identity_str, "/");
|
|
|
|
if (type != NULL) {
|
2013-08-03 07:38:38 -04:00
|
|
|
g_string_append(identity_str, type);
|
2013-01-21 17:18:15 -05:00
|
|
|
}
|
|
|
|
g_string_append(identity_str, "/");
|
|
|
|
if (lang != NULL) {
|
2013-08-03 07:38:38 -04:00
|
|
|
g_string_append(identity_str, lang);
|
2013-01-21 17:18:15 -05:00
|
|
|
}
|
|
|
|
g_string_append(identity_str, "/");
|
|
|
|
if (name != NULL) {
|
2013-08-03 07:38:38 -04:00
|
|
|
g_string_append(identity_str, name);
|
2013-01-21 17:18:15 -05:00
|
|
|
}
|
|
|
|
g_string_append(identity_str, "<");
|
2013-08-25 19:42:01 -04:00
|
|
|
identities = g_slist_insert_sorted(identities, g_strdup(identity_str->str), (GCompareFunc)strcmp);
|
2013-01-22 19:21:05 -05:00
|
|
|
g_string_free(identity_str, TRUE);
|
2013-01-21 17:18:15 -05:00
|
|
|
} else if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_FEATURE) == 0) {
|
2013-01-23 17:24:54 -05:00
|
|
|
feature_str = xmpp_stanza_get_attribute(child, "var");
|
2013-08-25 19:42:01 -04:00
|
|
|
features = g_slist_insert_sorted(features, g_strdup(feature_str), (GCompareFunc)strcmp);
|
2013-01-21 17:18:15 -05:00
|
|
|
} else if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_X) == 0) {
|
|
|
|
if (strcmp(xmpp_stanza_get_ns(child), STANZA_NS_DATA) == 0) {
|
2014-09-05 16:04:16 -04:00
|
|
|
form = form_create(child);
|
2013-08-25 19:42:01 -04:00
|
|
|
form_names = g_slist_insert_sorted(form_names, g_strdup(form->form_type), (GCompareFunc)strcmp);
|
2013-08-03 07:38:38 -04:00
|
|
|
g_hash_table_insert(forms, g_strdup(form->form_type), form);
|
2013-01-21 17:18:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
child = xmpp_stanza_get_next(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
GSList *curr = identities;
|
|
|
|
while (curr != NULL) {
|
2013-08-03 07:38:38 -04:00
|
|
|
g_string_append(s, curr->data);
|
2013-01-21 17:18:15 -05:00
|
|
|
curr = g_slist_next(curr);
|
|
|
|
}
|
|
|
|
|
|
|
|
curr = features;
|
|
|
|
while (curr != NULL) {
|
2013-08-03 07:38:38 -04:00
|
|
|
g_string_append(s, curr->data);
|
2013-01-21 17:18:15 -05:00
|
|
|
g_string_append(s, "<");
|
|
|
|
curr = g_slist_next(curr);
|
|
|
|
}
|
|
|
|
|
|
|
|
curr = form_names;
|
|
|
|
while (curr != NULL) {
|
2013-01-22 19:21:05 -05:00
|
|
|
form = g_hash_table_lookup(forms, curr->data);
|
2013-08-03 07:38:38 -04:00
|
|
|
g_string_append(s, form->form_type);
|
2013-01-21 17:18:15 -05:00
|
|
|
g_string_append(s, "<");
|
|
|
|
|
|
|
|
GSList *curr_field = form->fields;
|
|
|
|
while (curr_field != NULL) {
|
2013-01-22 19:21:05 -05:00
|
|
|
field = curr_field->data;
|
2013-08-03 07:38:38 -04:00
|
|
|
g_string_append(s, field->var);
|
2013-02-05 15:36:21 -05:00
|
|
|
g_string_append(s, "<");
|
2013-01-21 17:18:15 -05:00
|
|
|
GSList *curr_value = field->values;
|
|
|
|
while (curr_value != NULL) {
|
2013-08-03 07:38:38 -04:00
|
|
|
g_string_append(s, curr_value->data);
|
2013-01-21 17:18:15 -05:00
|
|
|
g_string_append(s, "<");
|
|
|
|
curr_value = g_slist_next(curr_value);
|
|
|
|
}
|
2013-02-03 12:19:10 -05:00
|
|
|
curr_field = g_slist_next(curr_field);
|
2013-01-21 17:18:15 -05:00
|
|
|
}
|
2013-02-03 12:19:10 -05:00
|
|
|
|
|
|
|
curr = g_slist_next(curr);
|
2013-01-21 17:18:15 -05:00
|
|
|
}
|
|
|
|
|
2014-06-15 14:08:18 -04:00
|
|
|
char *result = p_sha1_hash(s->str);
|
2013-01-21 17:18:15 -05:00
|
|
|
|
|
|
|
g_string_free(s, TRUE);
|
2013-08-03 07:38:38 -04:00
|
|
|
g_slist_free_full(identities, g_free);
|
|
|
|
g_slist_free_full(features, g_free);
|
|
|
|
g_slist_free_full(form_names, g_free);
|
|
|
|
g_hash_table_destroy(forms);
|
2013-01-21 17:18:15 -05:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-01-21 17:38:48 -05:00
|
|
|
xmpp_stanza_t *
|
2013-01-23 17:24:54 -05:00
|
|
|
caps_create_query_response_stanza(xmpp_ctx_t * const ctx)
|
2013-01-21 17:38:48 -05:00
|
|
|
{
|
|
|
|
xmpp_stanza_t *query = xmpp_stanza_new(ctx);
|
|
|
|
xmpp_stanza_set_name(query, STANZA_NAME_QUERY);
|
|
|
|
xmpp_stanza_set_ns(query, XMPP_NS_DISCO_INFO);
|
|
|
|
|
|
|
|
xmpp_stanza_t *identity = xmpp_stanza_new(ctx);
|
|
|
|
xmpp_stanza_set_name(identity, "identity");
|
|
|
|
xmpp_stanza_set_attribute(identity, "category", "client");
|
2013-06-24 20:00:24 -04:00
|
|
|
xmpp_stanza_set_attribute(identity, "type", "console");
|
2013-01-21 17:38:48 -05:00
|
|
|
|
|
|
|
GString *name_str = g_string_new("Profanity ");
|
|
|
|
g_string_append(name_str, PACKAGE_VERSION);
|
|
|
|
if (strcmp(PACKAGE_STATUS, "development") == 0) {
|
2013-09-16 20:28:35 -04:00
|
|
|
#ifdef HAVE_GIT_VERSION
|
|
|
|
g_string_append(name_str, "dev.");
|
|
|
|
g_string_append(name_str, PROF_GIT_BRANCH);
|
|
|
|
g_string_append(name_str, ".");
|
|
|
|
g_string_append(name_str, PROF_GIT_REVISION);
|
|
|
|
#else
|
2013-01-21 17:38:48 -05:00
|
|
|
g_string_append(name_str, "dev");
|
2013-09-16 20:28:35 -04:00
|
|
|
#endif
|
2013-01-21 17:38:48 -05:00
|
|
|
}
|
|
|
|
xmpp_stanza_set_attribute(identity, "name", name_str->str);
|
2014-06-15 16:39:54 -04:00
|
|
|
g_string_free(name_str, TRUE);
|
2013-01-21 17:38:48 -05:00
|
|
|
|
|
|
|
xmpp_stanza_t *feature_caps = xmpp_stanza_new(ctx);
|
|
|
|
xmpp_stanza_set_name(feature_caps, STANZA_NAME_FEATURE);
|
|
|
|
xmpp_stanza_set_attribute(feature_caps, STANZA_ATTR_VAR, STANZA_NS_CAPS);
|
|
|
|
|
|
|
|
xmpp_stanza_t *feature_discoinfo = xmpp_stanza_new(ctx);
|
|
|
|
xmpp_stanza_set_name(feature_discoinfo, STANZA_NAME_FEATURE);
|
|
|
|
xmpp_stanza_set_attribute(feature_discoinfo, STANZA_ATTR_VAR, XMPP_NS_DISCO_INFO);
|
|
|
|
|
2013-03-24 13:45:12 -04:00
|
|
|
xmpp_stanza_t *feature_discoitems = xmpp_stanza_new(ctx);
|
|
|
|
xmpp_stanza_set_name(feature_discoitems, STANZA_NAME_FEATURE);
|
|
|
|
xmpp_stanza_set_attribute(feature_discoitems, STANZA_ATTR_VAR, XMPP_NS_DISCO_ITEMS);
|
|
|
|
|
2013-01-21 17:38:48 -05:00
|
|
|
xmpp_stanza_t *feature_muc = xmpp_stanza_new(ctx);
|
|
|
|
xmpp_stanza_set_name(feature_muc, STANZA_NAME_FEATURE);
|
|
|
|
xmpp_stanza_set_attribute(feature_muc, STANZA_ATTR_VAR, STANZA_NS_MUC);
|
|
|
|
|
|
|
|
xmpp_stanza_t *feature_version = xmpp_stanza_new(ctx);
|
|
|
|
xmpp_stanza_set_name(feature_version, STANZA_NAME_FEATURE);
|
|
|
|
xmpp_stanza_set_attribute(feature_version, STANZA_ATTR_VAR, STANZA_NS_VERSION);
|
|
|
|
|
2013-02-16 22:23:14 -05:00
|
|
|
xmpp_stanza_t *feature_chatstates = xmpp_stanza_new(ctx);
|
|
|
|
xmpp_stanza_set_name(feature_chatstates, STANZA_NAME_FEATURE);
|
|
|
|
xmpp_stanza_set_attribute(feature_chatstates, STANZA_ATTR_VAR, STANZA_NS_CHATSTATES);
|
|
|
|
|
2013-02-17 11:43:26 -05:00
|
|
|
xmpp_stanza_t *feature_ping = xmpp_stanza_new(ctx);
|
|
|
|
xmpp_stanza_set_name(feature_ping, STANZA_NAME_FEATURE);
|
|
|
|
xmpp_stanza_set_attribute(feature_ping, STANZA_ATTR_VAR, STANZA_NS_PING);
|
|
|
|
|
2013-01-21 17:38:48 -05:00
|
|
|
xmpp_stanza_add_child(query, identity);
|
2013-02-17 11:50:44 -05:00
|
|
|
|
2013-01-21 17:38:48 -05:00
|
|
|
xmpp_stanza_add_child(query, feature_caps);
|
2013-02-16 22:23:14 -05:00
|
|
|
xmpp_stanza_add_child(query, feature_chatstates);
|
2013-02-17 11:50:44 -05:00
|
|
|
xmpp_stanza_add_child(query, feature_discoinfo);
|
2013-03-24 13:45:12 -04:00
|
|
|
xmpp_stanza_add_child(query, feature_discoitems);
|
2013-02-17 11:50:44 -05:00
|
|
|
xmpp_stanza_add_child(query, feature_muc);
|
|
|
|
xmpp_stanza_add_child(query, feature_version);
|
2013-02-17 11:43:26 -05:00
|
|
|
xmpp_stanza_add_child(query, feature_ping);
|
2013-01-21 17:38:48 -05:00
|
|
|
|
2013-08-25 07:54:34 -04:00
|
|
|
xmpp_stanza_release(feature_ping);
|
|
|
|
xmpp_stanza_release(feature_version);
|
2013-01-23 17:24:54 -05:00
|
|
|
xmpp_stanza_release(feature_muc);
|
2013-03-24 13:45:12 -04:00
|
|
|
xmpp_stanza_release(feature_discoitems);
|
2013-08-25 07:54:34 -04:00
|
|
|
xmpp_stanza_release(feature_discoinfo);
|
2013-02-16 22:23:14 -05:00
|
|
|
xmpp_stanza_release(feature_chatstates);
|
2013-08-25 07:54:34 -04:00
|
|
|
xmpp_stanza_release(feature_caps);
|
|
|
|
xmpp_stanza_release(identity);
|
2013-01-23 17:24:54 -05:00
|
|
|
|
2013-01-21 17:38:48 -05:00
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2013-12-18 15:16:55 -05:00
|
|
|
static void
|
|
|
|
_caps_close(void)
|
2013-01-19 19:35:40 -05:00
|
|
|
{
|
|
|
|
g_hash_table_destroy(capabilities);
|
|
|
|
}
|
2013-01-19 19:57:44 -05:00
|
|
|
|
|
|
|
static void
|
|
|
|
_caps_destroy(Capabilities *caps)
|
|
|
|
{
|
|
|
|
if (caps != NULL) {
|
2013-08-03 07:27:07 -04:00
|
|
|
free(caps->category);
|
|
|
|
free(caps->type);
|
|
|
|
free(caps->name);
|
|
|
|
free(caps->software);
|
|
|
|
free(caps->software_version);
|
|
|
|
free(caps->os);
|
|
|
|
free(caps->os_version);
|
2013-02-16 20:04:10 -05:00
|
|
|
if (caps->features != NULL) {
|
|
|
|
g_slist_free_full(caps->features, free);
|
|
|
|
}
|
2013-08-03 07:27:07 -04:00
|
|
|
free(caps);
|
2013-01-19 19:57:44 -05:00
|
|
|
}
|
|
|
|
}
|
2013-12-22 18:16:19 -05:00
|
|
|
|
|
|
|
void
|
|
|
|
capabilities_init_module(void)
|
|
|
|
{
|
|
|
|
caps_get = _caps_get;
|
|
|
|
caps_close = _caps_close;
|
|
|
|
}
|