2
* Copyright (C) 2007 Colin DIDIER
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.
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.
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.
18
#define _POSIX_SOURCE 1
20
#define _SVID_SOURCE 1
29
#include "xmpp-servers.h"
31
#define XMPP_PRIORITY_MIN -128
32
#define XMPP_PRIORITY_MAX 127
34
static const char *utf8_charset = "UTF-8";
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;
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");
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:");
49
if(write(pipefd[1], pgp_passwd, strlen(pgp_passwd)) < 1) goto pgp_error;
50
if(close(pipefd[1])) goto pgp_error;
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)
58
if(write(tmp_fd, input, strlen(input)) < 0) goto pgp_error;
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)
66
if(write(tmp2_fd, input2, strlen(input2)) < 0) goto pgp_error;
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));
74
sprintf(cmd, "gpg -u '%s' --passphrase-fd '%d' ", keyid, pipefd[0]);
78
strcat(cmd, switches);
79
strcat(cmd, " -qo - --batch --no-tty '");
80
strcat(cmd, tmp_path);
85
strcat(cmd, tmp2_path);
92
strcat(cmd, " 2>/dev/null");
96
cstream = popen(cmd, "r");
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);
107
if(!in_data && buf[0] == '\n') {
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);
124
pclose(cstream); /* TODO: check exit code */
128
if(tmp2_fd) close(tmp2_fd);
129
if(tmp2_path) free(tmp2_path);
130
if(keyid) close(pipefd[0]);
141
xmpp_get_local_charset(G_CONST_RETURN char **charset)
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);
150
xmpp_recode_out(const char *str)
152
G_CONST_RETURN char *charset;
153
char *recoded, *stripped;
155
if (str == NULL || *str == '\0')
157
recoded = stripped = NULL;
158
signal_emit("xmpp formats strip codes", 2, str, &stripped);
159
if (stripped != NULL)
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);
170
xmpp_recode_in(const char *str)
172
G_CONST_RETURN char *charset;
173
char *recoded, *to = NULL;
175
if (str == NULL || *str == '\0')
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,
185
return (recoded != NULL) ? recoded : g_strdup(str);
189
xmpp_find_resource_sep(const char *jid)
191
return jid == NULL ? NULL : g_utf8_strchr(jid, -1, '/');
195
xmpp_extract_resource(const char *jid)
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;
205
xmpp_strip_resource(const char *jid)
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);
215
xmpp_extract_user(const char *jid)
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);
226
xmpp_extract_domain(const char *jid)
230
pos1 = g_utf8_strchr(jid, -1, '@');
231
pos2 = xmpp_find_resource_sep(jid);
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);
241
xmpp_have_domain(const char *jid)
245
g_return_val_if_fail(jid != NULL, FALSE);
246
pos = g_utf8_strchr(jid, -1, '@');
247
return (pos != NULL && *(pos+1) != '\0');
251
xmpp_have_resource(const char *jid)
255
g_return_val_if_fail(jid != NULL, FALSE);
256
pos = xmpp_find_resource_sep(jid);
257
return (pos != NULL && *(pos+1) != '\0');
261
xmpp_priority_out_of_bound(const int priority)
263
return (XMPP_PRIORITY_MIN <= priority
264
&& priority <= XMPP_PRIORITY_MAX) ? FALSE : TRUE;
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)
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);