Debian Codegraph @

src/core/tools.c

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