Debian Codegraph @

src/core/tools.c

raw symbols ◀ ▶
Source reconstructed at patch state 2 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 '' -qo - --batch " \
70 "--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, " -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 pclose(cstream); /* TODO: check exit code */
125
126 close(tmp_fd);
127 free(tmp_path);
128 if(tmp2_fd) close(tmp2_fd);
129 if(tmp2_path) free(tmp2_path);
130 if(keyid) close(pipefd[0]);
131 free(cmd);
132
133 return output;
134 pgp_error:
135 return NULL;
136 }
137
138
139
140 static gboolean
141 xmpp_get_local_charset(const char **charset)
142 {
143 *charset = settings_get_str("term_charset");
144 if (is_valid_charset(*charset))
145 return (g_ascii_strcasecmp(*charset, utf8_charset) == 0);
146 return g_get_charset(charset);
147 }
148
149 char *
150 xmpp_recode_out(const char *str)
151 {
152 const char *charset;
153 char *recoded, *stripped;
154
155 if (str == NULL || *str == '\0')
156 return NULL;
157 recoded = stripped = NULL;
158 signal_emit("xmpp formats strip codes", 2, str, &stripped);
159 if (stripped != NULL)
160 str = stripped;
161 if (!xmpp_get_local_charset(&charset) && charset != NULL)
162 recoded = g_convert_with_fallback(str, -1, utf8_charset,
163 charset, NULL, NULL, NULL, NULL);
164 recoded = recoded != NULL ? recoded : g_strdup(str);
165 g_free(stripped);
166 return recoded;
167 }
168
169 char *
170 xmpp_recode_in(const char *str)
171 {
172 const char *charset;
173 char *recoded, *to = NULL;
174
175 if (str == NULL || *str == '\0')
176 return NULL;
177 if (xmpp_get_local_charset(&charset) || charset == NULL)
178 return g_strdup(str);
179 if (settings_get_bool("recode_transliterate") &&
180 g_ascii_strcasecmp(charset, "//TRANSLIT") != 0)
181 charset = to = g_strconcat(charset ,"//TRANSLIT", (void *)NULL);
182 recoded = g_convert_with_fallback(str, -1, charset, utf8_charset, NULL,
183 NULL, NULL, NULL);
184 g_free(to);
185 return (recoded != NULL) ? recoded : g_strdup(str);
186 }
187
188 char *
189 xmpp_find_resource_sep(const char *jid)
190 {
191 return jid == NULL ? NULL : g_utf8_strchr(jid, -1, '/');
192 }
193
194 char *
195 xmpp_extract_resource(const char *jid)
196 {
197 char *pos;
198
199 g_return_val_if_fail(jid != NULL, NULL);
200 pos = xmpp_find_resource_sep(jid);
201 return (pos != NULL) ? g_strdup(pos + 1) : NULL;
202 }
203
204 char *
205 xmpp_strip_resource(const char *jid)
206 {
207 char *pos;
208
209 g_return_val_if_fail(jid != NULL, NULL);
210 pos = xmpp_find_resource_sep(jid);
211 return (pos != NULL) ? g_strndup(jid, pos - jid) : g_strdup(jid);
212 }
213
214 char *
215 xmpp_extract_user(const char *jid)
216 {
217 char *pos;
218
219 g_return_val_if_fail(jid != NULL, NULL);
220 pos = g_utf8_strchr(jid, -1, '@');
221 return (pos != NULL) ? g_strndup(jid, pos - jid) :
222 xmpp_strip_resource(jid);
223 }
224
225 char *
226 xmpp_extract_domain(const char *jid)
227 {
228 char *pos1, *pos2;
229
230 pos1 = g_utf8_strchr(jid, -1, '@');
231 pos2 = xmpp_find_resource_sep(jid);
232 if (pos1 == NULL)
233 return NULL;
234 if (pos2 != NULL && pos2 < pos1)
235 return g_strdup(pos1 + 1);
236 return (pos2 != NULL) ?
237 g_strndup(pos1 + 1, pos2 - pos1 - 1) : g_strdup(pos1 + 1);
238 }
239
240 gboolean
241 xmpp_have_domain(const char *jid)
242 {
243 char *pos;
244
245 g_return_val_if_fail(jid != NULL, FALSE);
246 pos = g_utf8_strchr(jid, -1, '@');
247 return (pos != NULL && *(pos+1) != '\0');
248 }
249
250 gboolean
251 xmpp_have_resource(const char *jid)
252 {
253 char *pos;
254
255 g_return_val_if_fail(jid != NULL, FALSE);
256 pos = xmpp_find_resource_sep(jid);
257 return (pos != NULL && *(pos+1) != '\0');
258 }
259
260 gboolean
261 xmpp_priority_out_of_bound(const int priority)
262 {
263 return (XMPP_PRIORITY_MIN <= priority
264 && priority <= XMPP_PRIORITY_MAX) ? FALSE : TRUE;
265 }
266
267 gboolean
268 xmpp_presence_changed(const int show, const int old_show, const char *status,
269 const char *old_status, const int priority, const int old_priority)
270 {
271 return (show != old_show)
272 || (status == NULL && old_status != NULL)
273 || (status != NULL && old_status == NULL)
274 || (status != NULL && old_status != NULL
275 && strcmp(status, old_status) != 0)
276 || (priority != old_priority);
277 }