Debian Codegraph @

src/core/tools.c

raw symbols ◀ ▶
Source reconstructed at patch state 4 of 15 — not the installed (fully patched) source.
15 Debian patches touch this file
1 /*
2 * Copyright (C) 2007 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 <string.h>
24
25 #include "module.h"
26 #include "recode.h"
27 #include "settings.h"
28 #include "signals.h"
29 #include "xmpp-servers.h"
30
31 #define XMPP_PRIORITY_MIN -128
32 #define XMPP_PRIORITY_MAX 127
33
34 static const char *utf8_charset = "UTF-8";
35
36 char *call_gpg(char *switches, char *input, char *input2, \
37 int get_stderr, int snip_data) {
38 int pipefd[2], tmp_fd, tmp2_fd = 0, in_data = !snip_data;
39 FILE *cstream;
40 char *cmd, *tmp_path, *tmp2_path = NULL, *output = NULL;
41 size_t output_size = 0;
42 char buf[100], buf2[100] = "";
43 const char *keyid = settings_get_str("xmpp_pgp");
44
45 if(keyid) { /* If no keyID, then we don't need a password */
46 if(pipe(pipefd)) goto pgp_error;
47 if(!pgp_passwd) pgp_passwd = get_password("OpenPGP Password:");
48
49 if(write(pipefd[1], pgp_passwd, strlen(pgp_passwd)) < 1) goto pgp_error;
50 if(close(pipefd[1])) goto pgp_error;
51 }
52
53 if(!(tmp_path = tempnam(NULL, "irssi-xmpp-gpg"))) goto pgp_error;
54 if((tmp_fd = open(tmp_path, O_WRONLY|O_CREAT|O_EXCL, \
55 S_IRUSR|S_IWUSR)) < 0)
56 goto pgp_error;
57
58 if(write(tmp_fd, input, strlen(input)) < 0) goto pgp_error;
59
60 if(input2) {
61 if(!(tmp2_path = tempnam(NULL, "irssi-xmpp-gpg"))) goto pgp_error;
62 if((tmp2_fd = open(tmp2_path, O_WRONLY|O_CREAT|O_EXCL, \
63 S_IRUSR|S_IWUSR)) < 0)
64 goto pgp_error;
65
66 if(write(tmp2_fd, input2, strlen(input2)) < 0) goto pgp_error;
67 }
68
69 cmd = malloc(sizeof("gpg -u '' --passphrase-fd '' --trust-model always" \
70 " -qo - --batch --no-tty '' '' 2>/dev/null") \
71 +strlen(switches)+8+strlen(tmp_path)+ \
72 (tmp2_path ? strlen(tmp2_path) : 0));
73 if(keyid) {
74 sprintf(cmd, "gpg -u '%s' --passphrase-fd '%d' ", keyid, pipefd[0]);
75 } else {
76 strcpy(cmd, "gpg ");
77 }
78 strcat(cmd, switches);
79 strcat(cmd, " --trust-model always -qo - --batch --no-tty '");
80 strcat(cmd, tmp_path);
81 strcat(cmd, "' ");
82
83 if(tmp2_path) {
84 strcat(cmd, "'");
85 strcat(cmd, tmp2_path);
86 strcat(cmd, "'");
87 }
88
89 if(get_stderr) {
90 strcat(cmd, " 2>&1");
91 } else {
92 strcat(cmd, " 2>/dev/null");
93 }
94
95 fflush(NULL);
96 cstream = popen(cmd, "r");
97
98 while(fgets(buf, sizeof(buf)-1, cstream)) {
99 if(strlen(buf2) > 0) {
100 output = realloc(output, output_size+strlen(buf2)+1);
101 if(!output) goto pgp_error;
102 if(output_size < 1) output[0] = '\0';
103 output_size += strlen(buf2);
104 strcat(output, buf2);
105 }
106
107 if(!in_data && buf[0] == '\n') {
108 in_data = 1;
109 continue;
110 } else if(in_data) {
111 strcpy(buf2, buf);
112 }
113 }
114
115 /* Get last line if not snipping */
116 if(!snip_data && strlen(buf2) > 0) {
117 output = realloc(output, output_size+strlen(buf2)+1);
118 if(!output) goto pgp_error;
119 if(output_size < 1) output[0] = '\0';
120 output_size += strlen(buf2);
121 strcat(output, buf2);
122 }
123
124 if(pclose(cstream) == 512) { /* TODO: more check exit code */
125 g_free(pgp_passwd);
126 pgp_passwd = NULL;
127 output = call_gpg(switches, input, input2, get_stderr, snip_data);
128 }
129
130 close(tmp_fd);
131 free(tmp_path);
132 if(tmp2_fd) close(tmp2_fd);
133 if(tmp2_path) free(tmp2_path);
134 if(keyid) close(pipefd[0]);
135 free(cmd);
136
137 return output;
138 pgp_error:
139 return NULL;
140 }
141
142
143
144 static gboolean
145 xmpp_get_local_charset(const char **charset)
146 {
147 *charset = settings_get_str("term_charset");
148 if (is_valid_charset(*charset))
149 return (g_ascii_strcasecmp(*charset, utf8_charset) == 0);
150 return g_get_charset(charset);
151 }
152
153 char *
154 xmpp_recode_out(const char *str)
155 {
156 const char *charset;
157 char *recoded, *stripped;
158
159 if (str == NULL || *str == '\0')
160 return NULL;
161 recoded = stripped = NULL;
162 signal_emit("xmpp formats strip codes", 2, str, &stripped);
163 if (stripped != NULL)
164 str = stripped;
165 if (!xmpp_get_local_charset(&charset) && charset != NULL)
166 recoded = g_convert_with_fallback(str, -1, utf8_charset,
167 charset, NULL, NULL, NULL, NULL);
168 recoded = recoded != NULL ? recoded : g_strdup(str);
169 g_free(stripped);
170 return recoded;
171 }
172
173 char *
174 xmpp_recode_in(const char *str)
175 {
176 const char *charset;
177 char *recoded, *to = NULL;
178
179 if (str == NULL || *str == '\0')
180 return NULL;
181 if (xmpp_get_local_charset(&charset) || charset == NULL)
182 return g_strdup(str);
183 if (settings_get_bool("recode_transliterate") &&
184 g_ascii_strcasecmp(charset, "//TRANSLIT") != 0)
185 charset = to = g_strconcat(charset ,"//TRANSLIT", (void *)NULL);
186 recoded = g_convert_with_fallback(str, -1, charset, utf8_charset, NULL,
187 NULL, NULL, NULL);
188 g_free(to);
189 return (recoded != NULL) ? recoded : g_strdup(str);
190 }
191
192 char *
193 xmpp_find_resource_sep(const char *jid)
194 {
195 return jid == NULL ? NULL : g_utf8_strchr(jid, -1, '/');
196 }
197
198 char *
199 xmpp_extract_resource(const char *jid)
200 {
201 char *pos;
202
203 g_return_val_if_fail(jid != NULL, NULL);
204 pos = xmpp_find_resource_sep(jid);
205 return (pos != NULL) ? g_strdup(pos + 1) : NULL;
206 }
207
208 char *
209 xmpp_strip_resource(const char *jid)
210 {
211 char *pos;
212
213 g_return_val_if_fail(jid != NULL, NULL);
214 pos = xmpp_find_resource_sep(jid);
215 return (pos != NULL) ? g_strndup(jid, pos - jid) : g_strdup(jid);
216 }
217
218 char *
219 xmpp_extract_user(const char *jid)
220 {
221 char *pos;
222
223 g_return_val_if_fail(jid != NULL, NULL);
224 pos = g_utf8_strchr(jid, -1, '@');
225 return (pos != NULL) ? g_strndup(jid, pos - jid) :
226 xmpp_strip_resource(jid);
227 }
228
229 char *
230 xmpp_extract_domain(const char *jid)
231 {
232 char *pos1, *pos2;
233
234 pos1 = g_utf8_strchr(jid, -1, '@');
235 pos2 = xmpp_find_resource_sep(jid);
236 if (pos1 == NULL)
237 return NULL;
238 if (pos2 != NULL && pos2 < pos1)
239 return g_strdup(pos1 + 1);
240 return (pos2 != NULL) ?
241 g_strndup(pos1 + 1, pos2 - pos1 - 1) : g_strdup(pos1 + 1);
242 }
243
244 gboolean
245 xmpp_have_domain(const char *jid)
246 {
247 char *pos;
248
249 g_return_val_if_fail(jid != NULL, FALSE);
250 pos = g_utf8_strchr(jid, -1, '@');
251 return (pos != NULL && *(pos+1) != '\0');
252 }
253
254 gboolean
255 xmpp_have_resource(const char *jid)
256 {
257 char *pos;
258
259 g_return_val_if_fail(jid != NULL, FALSE);
260 pos = xmpp_find_resource_sep(jid);
261 return (pos != NULL && *(pos+1) != '\0');
262 }
263
264 gboolean
265 xmpp_priority_out_of_bound(const int priority)
266 {
267 return (XMPP_PRIORITY_MIN <= priority
268 && priority <= XMPP_PRIORITY_MAX) ? FALSE : TRUE;
269 }
270
271 gboolean
272 xmpp_presence_changed(const int show, const int old_show, const char *status,
273 const char *old_status, const int priority, const int old_priority)
274 {
275 return (show != old_show)
276 || (status == NULL && old_status != NULL)
277 || (status != NULL && old_status == NULL)
278 || (status != NULL && old_status != NULL
279 && strcmp(status, old_status) != 0)
280 || (priority != old_priority);
281 }