Debian Codegraph @

src/core/protocol.c

raw symbols ◀ ▶
Source reconstructed at patch state 6 of 7 — not the installed (fully patched) source.
7 Debian patches touch this file
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 #include "module.h"
19 #include "signals.h"
20 #include "settings.h"
21
22 #include "xmpp-servers.h"
23 #include "rosters-tools.h"
24 #include "tools.h"
25 #include "xep/disco.h"
26
27 char *pgp_passwd = NULL;
28
29 static void
30 sig_set_presence(XMPP_SERVER_REC *server, const int show, const char *status,
31 const int priority)
32 {
33 LmMessage *lmsg;
34 char *str;
35 const char *pgp_keyid;
36
37 g_return_if_fail(IS_XMPP_SERVER(server));
38 if (!xmpp_presence_changed(show, server->show, status,
39 server->away_reason, priority, server->priority)) {
40 signal_stop();
41 return;
42 }
43
44 lmsg = lm_message_new(NULL, LM_MESSAGE_TYPE_PRESENCE);
45 server->show = show;
46
47 if (!xmpp_priority_out_of_bound(priority))
48 server->priority = priority;
49
50 if (show != XMPP_PRESENCE_AVAILABLE)
51 lm_message_node_add_child(lmsg->node, "show",
52 xmpp_presence_show[server->show]);
53
54 if(server->away_reason) g_free(server->away_reason);
55 server->away_reason = NULL;
56
57 if(!status) status = "";
58 server->away_reason = g_strdup(status);
59 str = xmpp_recode_out(server->away_reason);
60 lm_message_node_add_child(lmsg->node, "status", str);
61 if(!str) str = g_strdup("");
62
63 if((pgp_keyid = settings_get_str("xmpp_pgp"))) {
64 LmMessageNode *x;
65 char *signature = call_gpg("-ab", str, NULL, 0, 1);
66 disco_add_feature("jabber:x:signed");
67 disco_add_feature("jabber:x:encrypted");
68
69 if(signature) {
70 x = lm_message_node_add_child(lmsg->node, "x", signature);
71 lm_message_node_set_attribute(x, "xmlns", "jabber:x:signed");
72
73 free(signature);
74 }
75 }
76
77 g_free(str);
78
79 str = g_strdup_printf("%d", server->priority);
80 lm_message_node_add_child(lmsg->node, "priority", str);
81 g_free(str);
82
83 signal_emit("xmpp send presence", 2, server, lmsg);
84 lm_message_unref(lmsg);
85 if (show != XMPP_PRESENCE_AVAILABLE) /* away */
86 signal_emit("event 306", 2, server, server->jid);
87 else if (server->usermode_away) /* unaway */
88 signal_emit("event 305", 2, server, server->jid);
89 }
90
91 static void
92 sig_recv_message(XMPP_SERVER_REC *server, LmMessage *lmsg, const int type,
93 const char *id, const char *from, const char *to)
94 {
95 LmMessageNode *node, *encrypted;
96 char *str, *subject;
97
98 if ((type != LM_MESSAGE_SUB_TYPE_NOT_SET
99 && type != LM_MESSAGE_SUB_TYPE_HEADLINE
100 && type != LM_MESSAGE_SUB_TYPE_NORMAL
101 && type != LM_MESSAGE_SUB_TYPE_CHAT)
102 || server->ischannel(SERVER(server), from))
103 return;
104 node = lm_message_node_get_child(lmsg->node, "subject");
105 if (node != NULL && node->value != NULL && *node->value != '\0') {
106 str = xmpp_recode_in(node->value);
107 subject = g_strconcat("Subject: ", str, (void *)NULL);
108 g_free(str);
109 signal_emit("message private", 4, server, subject, from, from);
110 g_free(subject);
111 }
112
113 str = NULL;
114 encrypted = lm_find_node(lmsg->node, "x", "xmlns", "jabber:x:encrypted");
115 if(encrypted && encrypted->value) {
116 /* TODO: indicate the message was encrypted */
117 /* TODO: verify signatures */
118 char *send_to_gpg = malloc(sizeof( \
119 "-----BEGIN PGP MESSAGE-----\n\n" \
120 "-----END PGP MESSAGE-----\n")+ \
121 strlen(encrypted->value)+1 \
122 );
123 char *from_gpg;
124
125 send_to_gpg[0] = '\0';
126 strcat(send_to_gpg, "-----BEGIN PGP MESSAGE-----\n\n");
127 strcat(send_to_gpg, encrypted->value);
128 strcat(send_to_gpg, "-----END PGP MESSAGE-----\n");
129
130 from_gpg = call_gpg("-d", send_to_gpg, NULL, 0, 0);
131 if(from_gpg) {
132 str = xmpp_recode_in(from_gpg);
133 free(from_gpg);
134 }
135
136 free(send_to_gpg);
137 } else {
138 node = lm_message_node_get_child(lmsg->node, "body");
139 if (node != NULL && node->value != NULL && *node->value != '\0') {
140 str = xmpp_recode_in(node->value);
141 }
142 }
143 if(str) {
144 if (g_ascii_strncasecmp(str, "/me ", 4) == 0)
145 signal_emit("message xmpp action", 5,
146 server, str+4, from, from,
147 GINT_TO_POINTER(SEND_TARGET_NICK));
148 else
149 signal_emit("message private", 4, server,
150 str, from, from);
151 g_free(str);
152 }
153 }
154
155 void
156 protocol_init(void)
157 {
158 signal_add_first("xmpp set presence", sig_set_presence);
159 signal_add("xmpp recv message", sig_recv_message);
160 }
161
162 void
163 protocol_deinit(void)
164 {
165 signal_remove("xmpp set presence", sig_set_presence);
166 signal_remove("xmpp recv message", sig_recv_message);
167 }