Debian Codegraph @

src/core/protocol.c

raw symbols ◀ ▶
Source reconstructed at patch state 1 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 #define _POSIX_SOURCE 1
19 #define _BSD_SOURCE 1
20 #define _SVID_SOURCE 1
21 #include <stdio.h>
22
23 #include "module.h"
24 #include "signals.h"
25 #include "settings.h"
26
27 #include "xmpp-servers.h"
28 #include "rosters-tools.h"
29 #include "tools.h"
30
31 char *pgp_passwd = NULL;
32
33 char *call_gpg(char *keyid, char *switches, char *input) {
34 int pipefd[2], tmp_fd, in_data = 0;
35 FILE *cstream;
36 char *cmd, *tmp_path, *output = NULL;
37 size_t output_size = 0;
38 char buf[100], buf2[100] = "";
39
40 if(pipe(pipefd)) goto pgp_error;
41 if(!pgp_passwd) pgp_passwd = get_password("OpenPGP Password:");
42
43 if(write(pipefd[1], pgp_passwd, strlen(pgp_passwd)) < 1) goto pgp_error;
44 if(close(pipefd[1])) goto pgp_error;
45
46 if(!(tmp_path = tempnam(NULL, "irssi-xmpp-gpg"))) goto pgp_error;
47 if((tmp_fd = open(tmp_path, O_WRONLY|O_CREAT|O_EXCL, \
48 S_IRUSR|S_IWUSR)) < 0)
49 goto pgp_error;
50
51 if(write(tmp_fd, input, strlen(input)) < 0) goto pgp_error;
52
53 cmd = malloc(sizeof("gpg -u '' -qo - --no-tty --passphrase-fd '' ''") \
54 +strlen(switches)+6+strlen(tmp_path));
55 sprintf(cmd, "gpg -u '%s' %s -qo - --no-tty --passphrase-fd '%d' '%s'", \
56 keyid, switches, pipefd[0], tmp_path);
57 fflush(NULL);
58 cstream = popen(cmd, "r");
59
60 while(fgets(buf, sizeof(buf)-1, cstream)) {
61 if(strlen(buf2) > 0) {
62 output = realloc(output, output_size+strlen(buf2)+1);
63 if(!output) goto pgp_error;
64 if(output_size < 1) output[0] = '\0';
65 output_size += strlen(buf2);
66 strcat(output, buf2);
67 }
68
69 if(!in_data && buf[0] == '\n') {
70 in_data = 1;
71 continue;
72 } else if(in_data) {
73 strcpy(buf2, buf);
74 }
75 }
76
77 pclose(cstream); /* TODO: check exit code */
78
79 close(tmp_fd);
80 close(pipefd[0]);
81 free(tmp_path);
82 free(cmd);
83
84 return output;
85 pgp_error:
86 return NULL;
87 }
88
89 static void
90 sig_set_presence(XMPP_SERVER_REC *server, const int show, const char *status,
91 const int priority)
92 {
93 LmMessage *lmsg;
94 char *str;
95 const char *pgp_keyid;
96
97 g_return_if_fail(IS_XMPP_SERVER(server));
98 if (!xmpp_presence_changed(show, server->show, status,
99 server->away_reason, priority, server->priority)) {
100 signal_stop();
101 return;
102 }
103
104 lmsg = lm_message_new(NULL, LM_MESSAGE_TYPE_PRESENCE);
105 server->show = show;
106
107 if (!xmpp_priority_out_of_bound(priority))
108 server->priority = priority;
109
110 if (show != XMPP_PRESENCE_AVAILABLE)
111 lm_message_node_add_child(lmsg->node, "show",
112 xmpp_presence_show[server->show]);
113
114 if(server->away_reason) g_free(server->away_reason);
115 server->away_reason = NULL;
116
117 if(!status) status = "";
118 server->away_reason = g_strdup(status);
119 str = xmpp_recode_out(server->away_reason);
120 lm_message_node_add_child(lmsg->node, "status", str);
121 if(!str) str = g_strdup("");
122
123 if((pgp_keyid = settings_get_str("xmpp_pgp"))) {
124 LmMessageNode *x;
125 char *signature = call_gpg(pgp_keyid, "-ab", str);
126
127 x = lm_message_node_add_child(lmsg->node, "x", signature);
128 lm_message_node_set_attribute(x, "xmlns", "jabber:x:signed");
129
130 free(signature);
131 }
132
133 g_free(str);
134
135 str = g_strdup_printf("%d", server->priority);
136 lm_message_node_add_child(lmsg->node, "priority", str);
137 g_free(str);
138
139 signal_emit("xmpp send presence", 2, server, lmsg);
140 lm_message_unref(lmsg);
141 if (show != XMPP_PRESENCE_AVAILABLE) /* away */
142 signal_emit("event 306", 2, server, server->jid);
143 else if (server->usermode_away) /* unaway */
144 signal_emit("event 305", 2, server, server->jid);
145 }
146
147 static void
148 sig_recv_message(XMPP_SERVER_REC *server, LmMessage *lmsg, const int type,
149 const char *id, const char *from, const char *to)
150 {
151 LmMessageNode *node;
152 char *str, *subject;
153
154 if ((type != LM_MESSAGE_SUB_TYPE_NOT_SET
155 && type != LM_MESSAGE_SUB_TYPE_HEADLINE
156 && type != LM_MESSAGE_SUB_TYPE_NORMAL
157 && type != LM_MESSAGE_SUB_TYPE_CHAT)
158 || server->ischannel(SERVER(server), from))
159 return;
160 node = lm_message_node_get_child(lmsg->node, "subject");
161 if (node != NULL && node->value != NULL && *node->value != '\0') {
162 str = xmpp_recode_in(node->value);
163 subject = g_strconcat("Subject: ", str, (void *)NULL);
164 g_free(str);
165 signal_emit("message private", 4, server, subject, from, from);
166 g_free(subject);
167 }
168 node = lm_message_node_get_child(lmsg->node, "body");
169 if (node != NULL && node->value != NULL && *node->value != '\0') {
170 str = xmpp_recode_in(node->value);
171 if (g_ascii_strncasecmp(str, "/me ", 4) == 0)
172 signal_emit("message xmpp action", 5,
173 server, str+4, from, from,
174 GINT_TO_POINTER(SEND_TARGET_NICK));
175 else
176 signal_emit("message private", 4, server,
177 str, from, from);
178 g_free(str);
179 }
180 }
181
182 void
183 protocol_init(void)
184 {
185 signal_add_first("xmpp set presence", sig_set_presence);
186 signal_add("xmpp recv message", sig_recv_message);
187 }
188
189 void
190 protocol_deinit(void)
191 {
192 signal_remove("xmpp set presence", sig_set_presence);
193 signal_remove("xmpp recv message", sig_recv_message);
194 }