File: disco.c

package info (click to toggle)
irssi-plugin-xmpp 0.54-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,952 kB
  • sloc: ansic: 8,831; makefile: 113; perl: 58
file content (175 lines) | stat: -rw-r--r-- 4,785 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright (C) 2007,2008,2009 Colin DIDIER
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

 /* XEP-0030: Service Discovery */

#include <string.h>

#include "module.h"
#include "signals.h"

#include "xmpp-servers.h"
#include "tools.h"
#include "disco.h"

#define XMLNS_DISCO "http://jabber.org/protocol/disco#info"

static GSList *my_features;

void
disco_add_feature(char *feature)
{
	g_return_if_fail(feature != NULL && *feature != '\0');
	my_features = g_slist_insert_sorted(my_features, feature,
	    (GCompareFunc)strcmp);
}

gboolean
disco_have_feature(GSList *list, const char *feature)
{
	GSList *tmp;

	for (tmp = list; tmp != NULL; tmp = tmp->next)
		if (strcmp(feature, tmp->data) == 0)
			return TRUE;
	return FALSE;
}

static void
cleanup_features(GSList *list)
{
	GSList *tmp, *next;

	for (tmp = list; tmp != NULL; tmp = next) {
		next = tmp->next;
		g_free(tmp->data);
		list = g_slist_remove(list, tmp->data);
	}
}

void
disco_request(XMPP_SERVER_REC *server, const char *dest)
{
	LmMessage *lmsg;
	LmMessageNode *node;
	char *recoded;

	g_return_if_fail(IS_XMPP_SERVER(server));
	g_return_if_fail(dest != NULL && *dest != '\0');
	recoded = xmpp_recode_out(dest);
	lmsg = lm_message_new_with_sub_type(recoded, LM_MESSAGE_TYPE_IQ,
	    LM_MESSAGE_SUB_TYPE_GET);
	g_free(recoded);
	node = lm_message_node_add_child(lmsg->node, "query", NULL);
	lm_message_node_set_attribute(node, XMLNS, XMLNS_DISCO);
	signal_emit("xmpp send iq", 2, server, lmsg);
	lm_message_unref(lmsg);
}

static void
send_disco(XMPP_SERVER_REC *server, const char *dest)
{
	LmMessage *lmsg;
	LmMessageNode *node, *child;
	GSList *tmp;
	char *recoded;

	recoded = xmpp_recode_out(dest);
	lmsg = lm_message_new_with_sub_type(recoded, LM_MESSAGE_TYPE_IQ,
	    LM_MESSAGE_SUB_TYPE_RESULT);
	g_free(recoded);
	node = lm_message_node_add_child(lmsg->node, "query", NULL);
	lm_message_node_set_attribute(node, XMLNS, XMLNS_DISCO);
	child = lm_message_node_add_child(node, "identity", NULL);
	lm_message_node_set_attribute(child, "category", "client");
	lm_message_node_set_attribute(child, "type", "console");
	lm_message_node_set_attribute(child, "name", IRSSI_XMPP_PACKAGE);
	for (tmp = my_features; tmp != NULL; tmp = tmp->next) {
		child = lm_message_node_add_child(node, "feature", NULL);
		lm_message_node_set_attribute(child, "var", tmp->data);
	}
	signal_emit("xmpp send iq", 2, server, lmsg);
	lm_message_unref(lmsg);
}

static void
sig_recv_iq(XMPP_SERVER_REC *server, LmMessage *lmsg, const int type,
    const char *id, const char *from, const char *to)
{
	LmMessageNode *node;
	GSList *features;

	if (type == LM_MESSAGE_SUB_TYPE_RESULT) {
		node = lm_find_node(lmsg->node, "query", XMLNS, XMLNS_DISCO);
		if (node == NULL)
			return;
		features = NULL;
		for (node = node->children; node != NULL; node = node->next) {
			if (strcmp(node->name, "feature") == 0) {
				features = g_slist_prepend(features,
				    xmpp_recode_in(
			    	    lm_message_node_get_attribute(node, "var")));
			}
		}
		signal_emit("xmpp features", 3, server, from, features);
		if (strcmp(from, server->domain) == 0) {
			cleanup_features(server->server_features);
			server->server_features = features;
			signal_emit("xmpp server features", 1, server);
		} else
			cleanup_features(features);
	} else if (type == LM_MESSAGE_SUB_TYPE_GET) {
		node = lm_find_node(lmsg->node, "query", XMLNS, XMLNS_DISCO);
		if (node != NULL)
			send_disco(server, from);
	}
}

static void
sig_connected(XMPP_SERVER_REC *server)
{
	if (IS_XMPP_SERVER(server))
		disco_request(server, server->domain);
}

static void
sig_disconnected(XMPP_SERVER_REC *server)
{
	if (!IS_XMPP_SERVER(server))
		return;
	cleanup_features(server->server_features);
	server->server_features = NULL;
}

void
disco_init(void)
{
	my_features = NULL;
	disco_add_feature(XMLNS_DISCO);
	signal_add("server connected", sig_connected);
	signal_add("server disconnected", sig_disconnected);
	signal_add("xmpp recv iq", sig_recv_iq);
}

void
disco_deinit(void)
{
	signal_remove("server connected", sig_connected);
	signal_remove("server disconnected", sig_disconnected);
	signal_remove("xmpp recv iq", sig_recv_iq);
	g_slist_free(my_features);
}