Debian Codegraph @

src/core/protocol.c

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