Debian Codegraph @

src/core/xep/oob.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2007,2008,2009 Colin DIDIER
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 /*
19 * XEP-0066: Out of Band Data
20 */
21
22 #include "module.h"
23 #include "signals.h"
24
25 #include "xmpp-servers.h"
26 #include "tools.h"
27 #include "disco.h"
28 #include "muc.h"
29
30 #define XMLNS_OOB_X "jabber:x:oob"
31 #define XMLNS_OOB_IQ "jabber:iq:oob"
32
33 static void
34 lm_message_node_delete_child(LmMessageNode *child)
35 {
36 LmMessageNode *n;
37 LmMessageNode *parent = child->parent;
38 child->parent = NULL;
39 for (n = parent->children; n; ) {
40 LmMessageNode *next = n->next;
41 if (n == child) {
42 LmMessageNode *prev = n->prev;
43 if (next != NULL) {
44 next->prev = prev;
45 }
46 if (prev != NULL) {
47 prev->next = next;
48 }
49 if (n == parent->children) {
50 parent->children = next;
51 }
52 n->prev = NULL;
53 n->next = NULL;
54 lm_message_node_unref(n);
55 }
56 n = next;
57 }
58 }
59
60 static void
61 sig_recv_x(XMPP_SERVER_REC *server, LmMessage *lmsg, const int type,
62 const char *id, const char *from, const char *to)
63 {
64 LmMessageNode *node, *child;
65 const char *url, *desc;
66 char *url_recoded, *desc_recoded, *str;
67
68 node = lm_find_node(lmsg->node, "x", XMLNS, XMLNS_OOB_X);
69 if (node != NULL) {
70 child = lm_message_node_get_child(node, "url");
71 if (child == NULL || child->value == NULL)
72 return;
73 url = child->value;
74 child = lm_message_node_get_child(node, "desc");
75 desc = child != NULL ? child->value : NULL;
76 if (lm_message_get_type(lmsg) == LM_MESSAGE_TYPE_MESSAGE) {
77 LmMessageNode *body = lm_message_node_get_child(lmsg->node, "body");
78 if (body != NULL && g_strcmp0(url, lm_message_node_get_value(body)) == 0) {
79 lm_message_node_delete_child(body);
80 }
81 }
82 url_recoded = xmpp_recode_in(url);
83 if (desc != NULL) {
84 desc_recoded = xmpp_recode_in(desc);
85 str = g_strconcat(desc_recoded, ": ", url_recoded, (void *)NULL);
86 g_free(url_recoded);
87 g_free(desc_recoded);
88 } else
89 str = url_recoded;
90 if (lm_message_get_sub_type(lmsg) == LM_MESSAGE_SUB_TYPE_GROUPCHAT) {
91 char *nick, *channel;
92
93 nick = muc_extract_nick(from);
94 channel = muc_extract_channel(from);
95
96 signal_emit("message public", 5,
97 server, str, nick, "", channel);
98
99 g_free(channel);
100 g_free(nick);
101 } else {
102 signal_emit("message private", 4, server, str, from, from);
103 }
104 g_free(str);
105 }
106 }
107
108 void
109 oob_init(void)
110 {
111 disco_add_feature(XMLNS_OOB_X);
112 signal_add("xmpp recv message", sig_recv_x);
113 signal_add("xmpp recv presence", sig_recv_x);
114 }
115
116 void
117 oob_deinit(void)
118 {
119 signal_remove("xmpp recv message", sig_recv_x);
120 signal_remove("xmpp recv presence", sig_recv_x);
121 }