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