Debian Codegraph @

src/core/xep/version.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-0092: Software Version
20 */
21
22 #include <sys/utsname.h>
23 #include <string.h>
24
25 #include "module.h"
26 #include "queries.h"
27 #include "settings.h"
28 #include "signals.h"
29
30 #include "xmpp-servers.h"
31 #include "xmpp-commands.h"
32 #include "disco.h"
33 #include "tools.h"
34
35 #define XMLNS_VERSION "jabber:iq:version"
36
37 static void
38 send_version(XMPP_SERVER_REC *server, const char *dest,
39 const char *id)
40 {
41 LmMessage *lmsg;
42 LmMessageNode *node;
43 struct utsname u;
44 char *recoded;
45
46 recoded = xmpp_recode_out(dest);
47 lmsg = lm_message_new_with_sub_type(recoded, LM_MESSAGE_TYPE_IQ,
48 LM_MESSAGE_SUB_TYPE_RESULT);
49 g_free(recoded);
50 if (id != NULL)
51 lm_message_node_set_attribute(lmsg->node, "id", id);
52 node = lm_message_node_add_child(lmsg->node, "query", NULL);
53 lm_message_node_set_attribute(node, XMLNS, XMLNS_VERSION);
54 if (settings_get_bool("xmpp_send_version")) {
55 lm_message_node_add_child(node, "name",
56 IRSSI_XMPP_PACKAGE);
57 lm_message_node_add_child(node, "version",
58 IRSSI_XMPP_VERSION);
59 if (uname(&u) == 0)
60 lm_message_node_add_child(node, "os", u.sysname);
61 }
62 signal_emit("xmpp send iq", 2, server, lmsg);
63 lm_message_unref(lmsg);
64 }
65
66 static void
67 request_version(XMPP_SERVER_REC *server, const char *dest)
68 {
69 LmMessage *lmsg;
70 LmMessageNode *node;
71 char *recoded;
72
73 recoded = xmpp_recode_out(dest);
74 lmsg = lm_message_new_with_sub_type(recoded,
75 LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_GET);
76 g_free(recoded);
77 node = lm_message_node_add_child(lmsg->node, "query", NULL);
78 lm_message_node_set_attribute(node, XMLNS, XMLNS_VERSION);
79 signal_emit("xmpp send iq", 2, server, lmsg);
80 lm_message_unref(lmsg);
81 }
82
83 /* SYNTAX: VER [[<jid>[/<resource>]]|[<name]] */
84 static void
85 cmd_ver(const char *data, XMPP_SERVER_REC *server, WI_ITEM_REC *item)
86 {
87 char *cmd_dest, *dest;
88 void *free_arg;
89
90 CMD_XMPP_SERVER(server);
91 if (!cmd_get_params(data, &free_arg, 1, &cmd_dest))
92 return;
93 dest = xmpp_get_dest(cmd_dest, server, item);
94 request_version(server, dest);
95 g_free(dest);
96 cmd_params_free(free_arg);
97 }
98
99 static void
100 sig_recv_iq(XMPP_SERVER_REC *server, LmMessage *lmsg, const int type,
101 const char *id, const char *from, const char *to)
102 {
103 LmMessageNode *node, *child;
104 char *name, *version, *os;
105
106 if (type == LM_MESSAGE_SUB_TYPE_RESULT
107 && (node = lm_find_node(lmsg->node,"query", XMLNS,
108 XMLNS_VERSION)) != NULL) {
109 name = version = os = NULL;
110 for (child = node->children; child != NULL; child = child->next) {
111 if (child->value == NULL)
112 continue;
113 if (name == NULL && strcmp(child->name, "name") == 0)
114 g_strstrip(name = xmpp_recode_in(child->value));
115 else if (version == NULL
116 && strcmp(child->name, "version") == 0)
117 g_strstrip(version = xmpp_recode_in(child->value));
118 else if (os == NULL && strcmp(child->name, "os") == 0)
119 g_strstrip(os = xmpp_recode_in(child->value));
120 }
121 signal_emit("xmpp version", 5, server, from, name, version, os);
122 g_free(name);
123 g_free(version);
124 g_free(os);
125 } else if (type == LM_MESSAGE_SUB_TYPE_GET
126 && (node = lm_find_node(lmsg->node,"query", XMLNS,
127 XMLNS_VERSION)) != NULL)
128 send_version(server, from, id);
129 }
130
131 void
132 version_init(void)
133 {
134 disco_add_feature(XMLNS_VERSION);
135 settings_add_bool("xmpp", "xmpp_send_version", TRUE);
136 command_bind_xmpp("ver", NULL, (SIGNAL_FUNC)cmd_ver);
137 signal_add("xmpp recv iq", sig_recv_iq);
138 }
139
140 void
141 version_deinit(void)
142 {
143 command_unbind("ver", (SIGNAL_FUNC)cmd_ver);
144 signal_remove("xmpp recv iq", sig_recv_iq);
145 }