| 1 | /* |
|---|
| 2 | * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org> |
|---|
| 3 | * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org> |
|---|
| 4 | * |
|---|
| 5 | * This program is free software; you can redistribute it and/or modify |
|---|
| 6 | * it under the terms of the GNU General Public License as published by |
|---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | * (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with this program; if not, write to the Free Software |
|---|
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #if HAVE_CONFIG_H |
|---|
| 21 | # include "config.h" |
|---|
| 22 | #endif |
|---|
| 23 | |
|---|
| 24 | #include "mutt.h" |
|---|
| 25 | #include "mutt_menu.h" |
|---|
| 26 | #include "attach.h" |
|---|
| 27 | #include "mutt_curses.h" |
|---|
| 28 | #include "keymap.h" |
|---|
| 29 | #include "rfc1524.h" |
|---|
| 30 | #include "mime.h" |
|---|
| 31 | #include "pager.h" |
|---|
| 32 | #include "mailbox.h" |
|---|
| 33 | #include "copy.h" |
|---|
| 34 | #include "mx.h" |
|---|
| 35 | #include "mutt_crypt.h" |
|---|
| 36 | |
|---|
| 37 | #include <ctype.h> |
|---|
| 38 | #include <stdlib.h> |
|---|
| 39 | #include <unistd.h> |
|---|
| 40 | #include <sys/wait.h> |
|---|
| 41 | #include <sys/stat.h> |
|---|
| 42 | #include <fcntl.h> |
|---|
| 43 | #include <string.h> |
|---|
| 44 | #include <errno.h> |
|---|
| 45 | |
|---|
| 46 | int mutt_get_tmp_attachment (BODY *a) |
|---|
| 47 | { |
|---|
| 48 | char type[STRING]; |
|---|
| 49 | char tempfile[_POSIX_PATH_MAX]; |
|---|
| 50 | rfc1524_entry *entry = rfc1524_new_entry(); |
|---|
| 51 | FILE *fpin = NULL, *fpout = NULL; |
|---|
| 52 | struct stat st; |
|---|
| 53 | |
|---|
| 54 | if(a->unlink) |
|---|
| 55 | return 0; |
|---|
| 56 | |
|---|
| 57 | snprintf(type, sizeof(type), "%s/%s", TYPE(a), a->subtype); |
|---|
| 58 | rfc1524_mailcap_lookup(a, type, entry, 0); |
|---|
| 59 | rfc1524_expand_filename(entry->nametemplate, a->filename, |
|---|
| 60 | tempfile, sizeof(tempfile)); |
|---|
| 61 | |
|---|
| 62 | rfc1524_free_entry(&entry); |
|---|
| 63 | |
|---|
| 64 | if(stat(a->filename, &st) == -1) |
|---|
| 65 | return -1; |
|---|
| 66 | |
|---|
| 67 | if((fpin = fopen(a->filename, "r")) && (fpout = safe_fopen(tempfile, "w"))) /* __FOPEN_CHECKED__ */ |
|---|
| 68 | { |
|---|
| 69 | mutt_copy_stream (fpin, fpout); |
|---|
| 70 | mutt_str_replace (&a->filename, tempfile); |
|---|
| 71 | a->unlink = 1; |
|---|
| 72 | |
|---|
| 73 | if(a->stamp >= st.st_mtime) |
|---|
| 74 | mutt_stamp_attachment(a); |
|---|
| 75 | } |
|---|
| 76 | else |
|---|
| 77 | mutt_perror(fpin ? tempfile : a->filename); |
|---|
| 78 | |
|---|
| 79 | if(fpin) fclose(fpin); |
|---|
| 80 | if(fpout) fclose(fpout); |
|---|
| 81 | |
|---|
| 82 | return a->unlink ? 0 : -1; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | /* return 1 if require full screen redraw, 0 otherwise */ |
|---|
| 87 | int mutt_compose_attachment (BODY *a) |
|---|
| 88 | { |
|---|
| 89 | char type[STRING]; |
|---|
| 90 | char command[STRING]; |
|---|
| 91 | char newfile[_POSIX_PATH_MAX] = ""; |
|---|
| 92 | rfc1524_entry *entry = rfc1524_new_entry (); |
|---|
| 93 | short unlink_newfile = 0; |
|---|
| 94 | int rc = 0; |
|---|
| 95 | |
|---|
| 96 | snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype); |
|---|
| 97 | if (rfc1524_mailcap_lookup (a, type, entry, M_COMPOSE)) |
|---|
| 98 | { |
|---|
| 99 | if (entry->composecommand || entry->composetypecommand) |
|---|
| 100 | { |
|---|
| 101 | |
|---|
| 102 | if (entry->composetypecommand) |
|---|
| 103 | strfcpy (command, entry->composetypecommand, sizeof (command)); |
|---|
| 104 | else |
|---|
| 105 | strfcpy (command, entry->composecommand, sizeof (command)); |
|---|
| 106 | if (rfc1524_expand_filename (entry->nametemplate, |
|---|
| 107 | a->filename, newfile, sizeof (newfile))) |
|---|
| 108 | { |
|---|
| 109 | dprint(1, (debugfile, "oldfile: %s\t newfile: %s\n", |
|---|
| 110 | a->filename, newfile)); |
|---|
| 111 | if (safe_symlink (a->filename, newfile) == -1) |
|---|
| 112 | { |
|---|
| 113 | if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) != M_YES) |
|---|
| 114 | goto bailout; |
|---|
| 115 | } |
|---|
| 116 | else |
|---|
| 117 | unlink_newfile = 1; |
|---|
| 118 | } |
|---|
| 119 | else |
|---|
| 120 | strfcpy(newfile, a->filename, sizeof(newfile)); |
|---|
| 121 | |
|---|
| 122 | if (rfc1524_expand_command (a, newfile, type, |
|---|
| 123 | command, sizeof (command))) |
|---|
| 124 | { |
|---|
| 125 | /* For now, editing requires a file, no piping */ |
|---|
| 126 | mutt_error _("Mailcap compose entry requires %%s"); |
|---|
| 127 | } |
|---|
| 128 | else |
|---|
| 129 | { |
|---|
| 130 | int r; |
|---|
| 131 | |
|---|
| 132 | mutt_endwin (NULL); |
|---|
| 133 | if ((r = mutt_system (command)) == -1) |
|---|
| 134 | mutt_error (_("Error running \"%s\"!"), command); |
|---|
| 135 | |
|---|
| 136 | if (r != -1 && entry->composetypecommand) |
|---|
| 137 | { |
|---|
| 138 | BODY *b; |
|---|
| 139 | FILE *fp, *tfp; |
|---|
| 140 | char tempfile[_POSIX_PATH_MAX]; |
|---|
| 141 | |
|---|
| 142 | if ((fp = safe_fopen (a->filename, "r")) == NULL) |
|---|
| 143 | { |
|---|
| 144 | mutt_perror _("Failure to open file to parse headers."); |
|---|
| 145 | goto bailout; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | b = mutt_read_mime_header (fp, 0); |
|---|
| 149 | if (b) |
|---|
| 150 | { |
|---|
| 151 | if (b->parameter) |
|---|
| 152 | { |
|---|
| 153 | mutt_free_parameter (&a->parameter); |
|---|
| 154 | a->parameter = b->parameter; |
|---|
| 155 | b->parameter = NULL; |
|---|
| 156 | } |
|---|
| 157 | if (b->description) { |
|---|
| 158 | FREE (&a->description); |
|---|
| 159 | a->description = b->description; |
|---|
| 160 | b->description = NULL; |
|---|
| 161 | } |
|---|
| 162 | if (b->form_name) |
|---|
| 163 | { |
|---|
| 164 | FREE (&a->form_name); |
|---|
| 165 | a->form_name = b->form_name; |
|---|
| 166 | b->form_name = NULL; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /* Remove headers by copying out data to another file, then |
|---|
| 170 | * copying the file back */ |
|---|
| 171 | fseeko (fp, b->offset, 0); |
|---|
| 172 | mutt_mktemp (tempfile); |
|---|
| 173 | if ((tfp = safe_fopen (tempfile, "w")) == NULL) |
|---|
| 174 | { |
|---|
| 175 | mutt_perror _("Failure to open file to strip headers."); |
|---|
| 176 | goto bailout; |
|---|
| 177 | } |
|---|
| 178 | mutt_copy_stream (fp, tfp); |
|---|
| 179 | fclose (fp); |
|---|
| 180 | fclose (tfp); |
|---|
| 181 | mutt_unlink (a->filename); |
|---|
| 182 | if (mutt_rename_file (tempfile, a->filename) != 0) |
|---|
| 183 | { |
|---|
| 184 | mutt_perror _("Failure to rename file."); |
|---|
| 185 | goto bailout; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | mutt_free_body (&b); |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | else |
|---|
| 195 | { |
|---|
| 196 | rfc1524_free_entry (&entry); |
|---|
| 197 | mutt_message (_("No mailcap compose entry for %s, creating empty file."), |
|---|
| 198 | type); |
|---|
| 199 | return 1; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | rc = 1; |
|---|
| 203 | |
|---|
| 204 | bailout: |
|---|
| 205 | |
|---|
| 206 | if(unlink_newfile) |
|---|
| 207 | unlink(newfile); |
|---|
| 208 | |
|---|
| 209 | rfc1524_free_entry (&entry); |
|---|
| 210 | return rc; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | /* |
|---|
| 214 | * Currently, this only works for send mode, as it assumes that the |
|---|
| 215 | * BODY->filename actually contains the information. I'm not sure |
|---|
| 216 | * we want to deal with editing attachments we've already received, |
|---|
| 217 | * so this should be ok. |
|---|
| 218 | * |
|---|
| 219 | * Returns 1 if editor found, 0 if not (useful to tell calling menu to |
|---|
| 220 | * redraw) |
|---|
| 221 | */ |
|---|
| 222 | int mutt_edit_attachment (BODY *a) |
|---|
| 223 | { |
|---|
| 224 | char type[STRING]; |
|---|
| 225 | char command[STRING]; |
|---|
| 226 | char newfile[_POSIX_PATH_MAX] = ""; |
|---|
| 227 | rfc1524_entry *entry = rfc1524_new_entry (); |
|---|
| 228 | short unlink_newfile = 0; |
|---|
| 229 | int rc = 0; |
|---|
| 230 | |
|---|
| 231 | snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype); |
|---|
| 232 | if (rfc1524_mailcap_lookup (a, type, entry, M_EDIT)) |
|---|
| 233 | { |
|---|
| 234 | if (entry->editcommand) |
|---|
| 235 | { |
|---|
| 236 | |
|---|
| 237 | strfcpy (command, entry->editcommand, sizeof (command)); |
|---|
| 238 | if (rfc1524_expand_filename (entry->nametemplate, |
|---|
| 239 | a->filename, newfile, sizeof (newfile))) |
|---|
| 240 | { |
|---|
| 241 | dprint(1, (debugfile, "oldfile: %s\t newfile: %s\n", |
|---|
| 242 | a->filename, newfile)); |
|---|
| 243 | if (safe_symlink (a->filename, newfile) == -1) |
|---|
| 244 | { |
|---|
| 245 | if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) != M_YES) |
|---|
| 246 | goto bailout; |
|---|
| 247 | } |
|---|
| 248 | else |
|---|
| 249 | unlink_newfile = 1; |
|---|
| 250 | } |
|---|
| 251 | else |
|---|
| 252 | strfcpy(newfile, a->filename, sizeof(newfile)); |
|---|
| 253 | |
|---|
| 254 | if (rfc1524_expand_command (a, newfile, type, |
|---|
| 255 | command, sizeof (command))) |
|---|
| 256 | { |
|---|
| 257 | /* For now, editing requires a file, no piping */ |
|---|
| 258 | mutt_error _("Mailcap Edit entry requires %%s"); |
|---|
| 259 | goto bailout; |
|---|
| 260 | } |
|---|
| 261 | else |
|---|
| 262 | { |
|---|
| 263 | mutt_endwin (NULL); |
|---|
| 264 | if (mutt_system (command) == -1) |
|---|
| 265 | { |
|---|
| 266 | mutt_error (_("Error running \"%s\"!"), command); |
|---|
| 267 | goto bailout; |
|---|
| 268 | } |
|---|
| 269 | } |
|---|
| 270 | } |
|---|
| 271 | } |
|---|
| 272 | else if (a->type == TYPETEXT) |
|---|
| 273 | { |
|---|
| 274 | /* On text, default to editor */ |
|---|
| 275 | mutt_edit_file (NONULL (Editor), a->filename); |
|---|
| 276 | } |
|---|
| 277 | else |
|---|
| 278 | { |
|---|
| 279 | rfc1524_free_entry (&entry); |
|---|
| 280 | mutt_error (_("No mailcap edit entry for %s"),type); |
|---|
| 281 | return 0; |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | rc = 1; |
|---|
| 285 | |
|---|
| 286 | bailout: |
|---|
| 287 | |
|---|
| 288 | if(unlink_newfile) |
|---|
| 289 | unlink(newfile); |
|---|
| 290 | |
|---|
| 291 | rfc1524_free_entry (&entry); |
|---|
| 292 | return rc; |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | /* for compatibility with metamail */ |
|---|
| 297 | static int is_mmnoask (const char *buf) |
|---|
| 298 | { |
|---|
| 299 | char tmp[LONG_STRING], *p, *q; |
|---|
| 300 | int lng; |
|---|
| 301 | |
|---|
| 302 | if ((p = getenv ("MM_NOASK")) != NULL && *p) |
|---|
| 303 | { |
|---|
| 304 | if (mutt_strcmp (p, "1") == 0) |
|---|
| 305 | return (1); |
|---|
| 306 | |
|---|
| 307 | strfcpy (tmp, p, sizeof (tmp)); |
|---|
| 308 | p = tmp; |
|---|
| 309 | |
|---|
| 310 | while ((p = strtok (p, ",")) != NULL) |
|---|
| 311 | { |
|---|
| 312 | if ((q = strrchr (p, '/')) != NULL) |
|---|
| 313 | { |
|---|
| 314 | if (*(q+1) == '*') |
|---|
| 315 | { |
|---|
| 316 | if (ascii_strncasecmp (buf, p, q-p) == 0) |
|---|
| 317 | return (1); |
|---|
| 318 | } |
|---|
| 319 | else |
|---|
| 320 | { |
|---|
| 321 | if (ascii_strcasecmp (buf, p) == 0) |
|---|
| 322 | return (1); |
|---|
| 323 | } |
|---|
| 324 | } |
|---|
| 325 | else |
|---|
| 326 | { |
|---|
| 327 | lng = mutt_strlen (p); |
|---|
| 328 | if (buf[lng] == '/' && mutt_strncasecmp (buf, p, lng) == 0) |
|---|
| 329 | return (1); |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | p = NULL; |
|---|
| 333 | } |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | return (0); |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | void mutt_check_lookup_list (BODY *b, char *type, int len) |
|---|
| 340 | { |
|---|
| 341 | LIST *t = MimeLookupList; |
|---|
| 342 | int i; |
|---|
| 343 | |
|---|
| 344 | for (; t; t = t->next) { |
|---|
| 345 | i = mutt_strlen (t->data) - 1; |
|---|
| 346 | if ((i > 0 && t->data[i-1] == '/' && t->data[i] == '*' && |
|---|
| 347 | ascii_strncasecmp (type, t->data, i) == 0) || |
|---|
| 348 | ascii_strcasecmp (type, t->data) == 0) { |
|---|
| 349 | |
|---|
| 350 | BODY tmp = {0}; |
|---|
| 351 | int n; |
|---|
| 352 | if ((n = mutt_lookup_mime_type (&tmp, b->filename)) != TYPEOTHER) { |
|---|
| 353 | snprintf (type, len, "%s/%s", |
|---|
| 354 | n == TYPEAUDIO ? "audio" : |
|---|
| 355 | n == TYPEAPPLICATION ? "application" : |
|---|
| 356 | n == TYPEIMAGE ? "image" : |
|---|
| 357 | n == TYPEMESSAGE ? "message" : |
|---|
| 358 | n == TYPEMODEL ? "model" : |
|---|
| 359 | n == TYPEMULTIPART ? "multipart" : |
|---|
| 360 | n == TYPETEXT ? "text" : |
|---|
| 361 | n == TYPEVIDEO ? "video" : "other", |
|---|
| 362 | tmp.subtype); |
|---|
| 363 | dprint(1, (debugfile, "mutt_check_lookup_list: \"%s\" -> %s\n", |
|---|
| 364 | b->filename, type)); |
|---|
| 365 | } |
|---|
| 366 | if (tmp.subtype) |
|---|
| 367 | FREE (&tmp.subtype); |
|---|
| 368 | if (tmp.xtype) |
|---|
| 369 | FREE (&tmp.xtype); |
|---|
| 370 | } |
|---|
| 371 | } |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | int mutt_is_autoview (BODY *b, const char *type) |
|---|
| 375 | { |
|---|
| 376 | LIST *t = AutoViewList; |
|---|
| 377 | char _type[SHORT_STRING]; |
|---|
| 378 | int i; |
|---|
| 379 | |
|---|
| 380 | if (!type) |
|---|
| 381 | snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype); |
|---|
| 382 | else |
|---|
| 383 | strncpy (_type, type, sizeof(_type)); |
|---|
| 384 | |
|---|
| 385 | mutt_check_lookup_list (b, _type, sizeof(_type)); |
|---|
| 386 | type = _type; |
|---|
| 387 | |
|---|
| 388 | if (mutt_needs_mailcap (b)) |
|---|
| 389 | { |
|---|
| 390 | if (option (OPTIMPLICITAUTOVIEW)) |
|---|
| 391 | return 1; |
|---|
| 392 | |
|---|
| 393 | if (is_mmnoask (type)) |
|---|
| 394 | return 1; |
|---|
| 395 | } |
|---|
| 396 | |
|---|
| 397 | for (; t; t = t->next) { |
|---|
| 398 | i = mutt_strlen (t->data) - 1; |
|---|
| 399 | if ((i > 0 && t->data[i-1] == '/' && t->data[i] == '*' && |
|---|
| 400 | ascii_strncasecmp (type, t->data, i) == 0) || |
|---|
| 401 | ascii_strcasecmp (type, t->data) == 0) |
|---|
| 402 | return 1; |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | return 0; |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | /* returns -1 on error, 0 or the return code from mutt_do_pager() on success */ |
|---|
| 409 | int mutt_view_attachment (FILE *fp, BODY *a, int flag, HEADER *hdr, |
|---|
| 410 | ATTACHPTR **idx, short idxlen) |
|---|
| 411 | { |
|---|
| 412 | char tempfile[_POSIX_PATH_MAX] = ""; |
|---|
| 413 | char pagerfile[_POSIX_PATH_MAX] = ""; |
|---|
| 414 | int is_message; |
|---|
| 415 | int use_mailcap; |
|---|
| 416 | int use_pipe = 0; |
|---|
| 417 | int use_pager = 1; |
|---|
| 418 | char type[STRING]; |
|---|
| 419 | char command[STRING]; |
|---|
| 420 | char descrip[STRING]; |
|---|
| 421 | char *fname; |
|---|
| 422 | rfc1524_entry *entry = NULL; |
|---|
| 423 | int rc = -1; |
|---|
| 424 | int unlink_tempfile = 0; |
|---|
| 425 | |
|---|
| 426 | is_message = mutt_is_message_type(a->type, a->subtype); |
|---|
| 427 | if (WithCrypto && is_message && a->hdr && (a->hdr->security & ENCRYPT) && |
|---|
| 428 | !crypt_valid_passphrase(a->hdr->security)) |
|---|
| 429 | return (rc); |
|---|
| 430 | use_mailcap = (flag == M_MAILCAP || |
|---|
| 431 | (flag == M_REGULAR && mutt_needs_mailcap (a))); |
|---|
| 432 | snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype); |
|---|
| 433 | |
|---|
| 434 | if (use_mailcap) |
|---|
| 435 | { |
|---|
| 436 | entry = rfc1524_new_entry (); |
|---|
| 437 | if (!rfc1524_mailcap_lookup (a, type, entry, 0)) |
|---|
| 438 | { |
|---|
| 439 | if (flag == M_REGULAR) |
|---|
| 440 | { |
|---|
| 441 | /* fallback to view as text */ |
|---|
| 442 | rfc1524_free_entry (&entry); |
|---|
| 443 | mutt_error _("No matching mailcap entry found. Viewing as text."); |
|---|
| 444 | flag = M_AS_TEXT; |
|---|
| 445 | use_mailcap = 0; |
|---|
| 446 | } |
|---|
| 447 | else |
|---|
| 448 | goto return_error; |
|---|
| 449 | } |
|---|
| 450 | } |
|---|
| 451 | |
|---|
| 452 | if (use_mailcap) |
|---|
| 453 | { |
|---|
| 454 | if (!entry->command) |
|---|
| 455 | { |
|---|
| 456 | mutt_error _("MIME type not defined. Cannot view attachment."); |
|---|
| 457 | goto return_error; |
|---|
| 458 | } |
|---|
| 459 | strfcpy (command, entry->command, sizeof (command)); |
|---|
| 460 | |
|---|
| 461 | if (fp) |
|---|
| 462 | { |
|---|
| 463 | fname = safe_strdup (a->filename); |
|---|
| 464 | mutt_sanitize_filename (fname, 1); |
|---|
| 465 | } |
|---|
| 466 | else |
|---|
| 467 | fname = a->filename; |
|---|
| 468 | |
|---|
| 469 | if (rfc1524_expand_filename (entry->nametemplate, fname, |
|---|
| 470 | tempfile, sizeof (tempfile))) |
|---|
| 471 | { |
|---|
| 472 | if (fp == NULL && mutt_strcmp(tempfile, a->filename)) |
|---|
| 473 | { |
|---|
| 474 | /* send case: the file is already there */ |
|---|
| 475 | if (safe_symlink (a->filename, tempfile) == -1) |
|---|
| 476 | { |
|---|
| 477 | if (mutt_yesorno (_("Can't match nametemplate, continue?"), M_YES) == M_YES) |
|---|
| 478 | strfcpy (tempfile, a->filename, sizeof (tempfile)); |
|---|
| 479 | else |
|---|
| 480 | goto return_error; |
|---|
| 481 | } |
|---|
| 482 | else |
|---|
| 483 | unlink_tempfile = 1; |
|---|
| 484 | } |
|---|
| 485 | } |
|---|
| 486 | else if (fp == NULL) /* send case */ |
|---|
| 487 | strfcpy (tempfile, a->filename, sizeof (tempfile)); |
|---|
| 488 | |
|---|
| 489 | if (fp) |
|---|
| 490 | { |
|---|
| 491 | /* recv case: we need to save the attachment to a file */ |
|---|
| 492 | FREE (&fname); |
|---|
| 493 | if (mutt_save_attachment (fp, a, tempfile, 0, NULL) == -1) |
|---|
| 494 | goto return_error; |
|---|
| 495 | } |
|---|
| 496 | |
|---|
| 497 | use_pipe = rfc1524_expand_command (a, tempfile, type, |
|---|
| 498 | command, sizeof (command)); |
|---|
| 499 | use_pager = entry->copiousoutput; |
|---|
| 500 | } |
|---|
| 501 | |
|---|
| 502 | if (use_pager) |
|---|
| 503 | { |
|---|
| 504 | if (fp && !use_mailcap && a->filename) |
|---|
| 505 | { |
|---|
| 506 | /* recv case */ |
|---|
| 507 | strfcpy (pagerfile, a->filename, sizeof (pagerfile)); |
|---|
| 508 | mutt_adv_mktemp (pagerfile, sizeof(pagerfile)); |
|---|
| 509 | } |
|---|
| 510 | else |
|---|
| 511 | mutt_mktemp (pagerfile); |
|---|
| 512 | } |
|---|
| 513 | |
|---|
| 514 | if (use_mailcap) |
|---|
| 515 | { |
|---|
| 516 | pid_t thepid = 0; |
|---|
| 517 | int tempfd = -1, pagerfd = -1; |
|---|
| 518 | |
|---|
| 519 | if (!use_pager) |
|---|
| 520 | mutt_endwin (NULL); |
|---|
| 521 | |
|---|
| 522 | if (use_pager || use_pipe) |
|---|
| 523 | { |
|---|
| 524 | if (use_pager && ((pagerfd = safe_open (pagerfile, O_CREAT | O_EXCL | O_WRONLY)) == -1)) |
|---|
| 525 | { |
|---|
| 526 | mutt_perror ("open"); |
|---|
| 527 | goto return_error; |
|---|
| 528 | } |
|---|
| 529 | if (use_pipe && ((tempfd = open (tempfile, 0)) == -1)) |
|---|
| 530 | { |
|---|
| 531 | if(pagerfd != -1) |
|---|
| 532 | close(pagerfd); |
|---|
| 533 | mutt_perror ("open"); |
|---|
| 534 | goto return_error; |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | if ((thepid = mutt_create_filter_fd (command, NULL, NULL, NULL, |
|---|
| 538 | use_pipe ? tempfd : -1, use_pager ? pagerfd : -1, -1)) == -1) |
|---|
| 539 | { |
|---|
| 540 | if(pagerfd != -1) |
|---|
| 541 | close(pagerfd); |
|---|
| 542 | |
|---|
| 543 | if(tempfd != -1) |
|---|
| 544 | close(tempfd); |
|---|
| 545 | |
|---|
| 546 | mutt_error _("Cannot create filter"); |
|---|
| 547 | goto return_error; |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| 550 | if (use_pager) |
|---|
| 551 | { |
|---|
| 552 | if (a->description) |
|---|
| 553 | snprintf (descrip, sizeof (descrip), |
|---|
| 554 | "---Command: %-20.20s Description: %s", |
|---|
| 555 | command, a->description); |
|---|
| 556 | else |
|---|
| 557 | snprintf (descrip, sizeof (descrip), |
|---|
| 558 | "---Command: %-30.30s Attachment: %s", command, type); |
|---|
| 559 | } |
|---|
| 560 | |
|---|
| 561 | if ((mutt_wait_filter (thepid) || (entry->needsterminal && |
|---|
| 562 | option (OPTWAITKEY))) && !use_pager) |
|---|
| 563 | mutt_any_key_to_continue (NULL); |
|---|
| 564 | |
|---|
| 565 | if (tempfd != -1) |
|---|
| 566 | close (tempfd); |
|---|
| 567 | if (pagerfd != -1) |
|---|
| 568 | close (pagerfd); |
|---|
| 569 | } |
|---|
| 570 | else |
|---|
| 571 | { |
|---|
| 572 | /* interactive command */ |
|---|
| 573 | if (mutt_system (command) || |
|---|
| 574 | (entry->needsterminal && option (OPTWAITKEY))) |
|---|
| 575 | mutt_any_key_to_continue (NULL); |
|---|
| 576 | } |
|---|
| 577 | } |
|---|
| 578 | else |
|---|
| 579 | { |
|---|
| 580 | /* Don't use mailcap; the attachment is viewed in the pager */ |
|---|
| 581 | |
|---|
| 582 | if (flag == M_AS_TEXT) |
|---|
| 583 | { |
|---|
| 584 | /* just let me see the raw data */ |
|---|
| 585 | if (mutt_save_attachment (fp, a, pagerfile, 0, NULL)) |
|---|
| 586 | goto return_error; |
|---|
| 587 | } |
|---|
| 588 | else |
|---|
| 589 | { |
|---|
| 590 | /* Use built-in handler */ |
|---|
| 591 | set_option (OPTVIEWATTACH); /* disable the "use 'v' to view this part" |
|---|
| 592 | * message in case of error */ |
|---|
| 593 | if (mutt_decode_save_attachment (fp, a, pagerfile, M_DISPLAY, 0)) |
|---|
| 594 | { |
|---|
| 595 | unset_option (OPTVIEWATTACH); |
|---|
| 596 | goto return_error; |
|---|
| 597 | } |
|---|
| 598 | unset_option (OPTVIEWATTACH); |
|---|
| 599 | } |
|---|
| 600 | |
|---|
| 601 | if (a->description) |
|---|
| 602 | strfcpy (descrip, a->description, sizeof (descrip)); |
|---|
| 603 | else if (a->filename) |
|---|
| 604 | snprintf (descrip, sizeof (descrip), "---Attachment: %s : %s", |
|---|
| 605 | a->filename, type); |
|---|
| 606 | else |
|---|
| 607 | snprintf (descrip, sizeof (descrip), "---Attachment: %s", type); |
|---|
| 608 | } |
|---|
| 609 | |
|---|
| 610 | /* We only reach this point if there have been no errors */ |
|---|
| 611 | |
|---|
| 612 | if (use_pager) |
|---|
| 613 | { |
|---|
| 614 | pager_t info; |
|---|
| 615 | |
|---|
| 616 | memset (&info, 0, sizeof (info)); |
|---|
| 617 | info.fp = fp; |
|---|
| 618 | info.bdy = a; |
|---|
| 619 | info.ctx = Context; |
|---|
| 620 | info.idx = idx; |
|---|
| 621 | info.idxlen = idxlen; |
|---|
| 622 | info.hdr = hdr; |
|---|
| 623 | |
|---|
| 624 | rc = mutt_do_pager (descrip, pagerfile, |
|---|
| 625 | M_PAGER_ATTACHMENT | (is_message ? M_PAGER_MESSAGE : 0), &info); |
|---|
| 626 | *pagerfile = '\0'; |
|---|
| 627 | } |
|---|
| 628 | else |
|---|
| 629 | rc = 0; |
|---|
| 630 | |
|---|
| 631 | return_error: |
|---|
| 632 | |
|---|
| 633 | if (entry) |
|---|
| 634 | rfc1524_free_entry (&entry); |
|---|
| 635 | if (fp && tempfile[0]) |
|---|
| 636 | mutt_unlink (tempfile); |
|---|
| 637 | else if (unlink_tempfile) |
|---|
| 638 | unlink(tempfile); |
|---|
| 639 | |
|---|
| 640 | if (pagerfile[0]) |
|---|
| 641 | mutt_unlink (pagerfile); |
|---|
| 642 | |
|---|
| 643 | return rc; |
|---|
| 644 | } |
|---|
| 645 | |
|---|
| 646 | /* returns 1 on success, 0 on error */ |
|---|
| 647 | int mutt_pipe_attachment (FILE *fp, BODY *b, const char *path, char *outfile) |
|---|
| 648 | { |
|---|
| 649 | pid_t thepid; |
|---|
| 650 | int out = -1; |
|---|
| 651 | int rv = 0; |
|---|
| 652 | |
|---|
| 653 | if (outfile && *outfile) |
|---|
| 654 | if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0) |
|---|
| 655 | { |
|---|
| 656 | mutt_perror ("open"); |
|---|
| 657 | return 0; |
|---|
| 658 | } |
|---|
| 659 | |
|---|
| 660 | mutt_endwin (NULL); |
|---|
| 661 | |
|---|
| 662 | if (fp) |
|---|
| 663 | { |
|---|
| 664 | /* recv case */ |
|---|
| 665 | |
|---|
| 666 | STATE s; |
|---|
| 667 | |
|---|
| 668 | memset (&s, 0, sizeof (STATE)); |
|---|
| 669 | |
|---|
| 670 | if (outfile && *outfile) |
|---|
| 671 | thepid = mutt_create_filter_fd (path, &s.fpout, NULL, NULL, -1, out, -1); |
|---|
| 672 | else |
|---|
| 673 | thepid = mutt_create_filter (path, &s.fpout, NULL, NULL); |
|---|
| 674 | |
|---|
| 675 | if (thepid < 0) |
|---|
| 676 | { |
|---|
| 677 | mutt_perror _("Can't create filter"); |
|---|
| 678 | goto bail; |
|---|
| 679 | } |
|---|
| 680 | |
|---|
| 681 | s.fpin = fp; |
|---|
| 682 | mutt_decode_attachment (b, &s); |
|---|
| 683 | safe_fclose (&s.fpout); |
|---|
| 684 | } |
|---|
| 685 | else |
|---|
| 686 | { |
|---|
| 687 | /* send case */ |
|---|
| 688 | |
|---|
| 689 | FILE *ifp, *ofp; |
|---|
| 690 | |
|---|
| 691 | if ((ifp = fopen (b->filename, "r")) == NULL) |
|---|
| 692 | { |
|---|
| 693 | mutt_perror ("fopen"); |
|---|
| 694 | if (outfile && *outfile) |
|---|
| 695 | { |
|---|
| 696 | close (out); |
|---|
| 697 | unlink (outfile); |
|---|
| 698 | } |
|---|
| 699 | return 0; |
|---|
| 700 | } |
|---|
| 701 | |
|---|
| 702 | if (outfile && *outfile) |
|---|
| 703 | thepid = mutt_create_filter_fd (path, &ofp, NULL, NULL, -1, out, -1); |
|---|
| 704 | else |
|---|
| 705 | thepid = mutt_create_filter (path, &ofp, NULL, NULL); |
|---|
| 706 | |
|---|
| 707 | if (thepid < 0) |
|---|
| 708 | { |
|---|
| 709 | mutt_perror _("Can't create filter"); |
|---|
| 710 | safe_fclose (&ifp); |
|---|
| 711 | goto bail; |
|---|
| 712 | } |
|---|
| 713 | |
|---|
| 714 | mutt_copy_stream (ifp, ofp); |
|---|
| 715 | safe_fclose (&ofp); |
|---|
| 716 | safe_fclose (&ifp); |
|---|
| 717 | } |
|---|
| 718 | |
|---|
| 719 | rv = 1 |
|---|