Line data Source code
1 : /** 2 : * @file 3 : * Handling of email attachments 4 : * 5 : * @authors 6 : * Copyright (C) 1996-2000,2002,2013 Michael R. Elkins <me@mutt.org> 7 : * Copyright (C) 1999-2004,2006 Thomas Roessler <roessler@does-not-exist.org> 8 : * 9 : * @copyright 10 : * This program is free software: you can redistribute it and/or modify it under 11 : * the terms of the GNU General Public License as published by the Free Software 12 : * Foundation, either version 2 of the License, or (at your option) any later 13 : * version. 14 : * 15 : * This program is distributed in the hope that it will be useful, but WITHOUT 16 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 17 : * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 18 : * details. 19 : * 20 : * You should have received a copy of the GNU General Public License along with 21 : * this program. If not, see <http://www.gnu.org/licenses/>. 22 : */ 23 : 24 : /** 25 : * @page email_attach Handling of email attachments 26 : * 27 : * Handling of email attachments 28 : */ 29 : 30 : #include "config.h" 31 : #include "mutt/lib.h" 32 : #include "attach.h" 33 : #include "body.h" 34 : 35 : /** 36 : * mutt_actx_add_attach - Add an Attachment to an Attachment Context 37 : * @param actx Attachment context 38 : * @param attach Attachment to add 39 : */ 40 4 : void mutt_actx_add_attach(struct AttachCtx *actx, struct AttachPtr *attach) 41 : { 42 4 : if (!actx || !attach) 43 4 : return; 44 : 45 0 : if (actx->idxlen == actx->idxmax) 46 : { 47 0 : actx->idxmax += 5; 48 0 : mutt_mem_realloc(&actx->idx, sizeof(struct AttachPtr *) * actx->idxmax); 49 0 : mutt_mem_realloc(&actx->v2r, sizeof(short) * actx->idxmax); 50 0 : for (int i = actx->idxlen; i < actx->idxmax; i++) 51 0 : actx->idx[i] = NULL; 52 : } 53 : 54 0 : actx->idx[actx->idxlen++] = attach; 55 : } 56 : 57 : /** 58 : * mutt_actx_add_fp - Save a File handle to the Attachment Context 59 : * @param actx Attachment context 60 : * @param fp_new File handle to save 61 : */ 62 4 : void mutt_actx_add_fp(struct AttachCtx *actx, FILE *fp_new) 63 : { 64 4 : if (!actx || !fp_new) 65 4 : return; 66 : 67 0 : if (actx->fp_len == actx->fp_max) 68 : { 69 0 : actx->fp_max += 5; 70 0 : mutt_mem_realloc(&actx->fp_idx, sizeof(FILE *) * actx->fp_max); 71 0 : for (int i = actx->fp_len; i < actx->fp_max; i++) 72 0 : actx->fp_idx[i] = NULL; 73 : } 74 : 75 0 : actx->fp_idx[actx->fp_len++] = fp_new; 76 : } 77 : 78 : /** 79 : * mutt_actx_add_body - Add an email box to an Attachment Context 80 : * @param actx Attachment context 81 : * @param new_body Email Body to add 82 : */ 83 4 : void mutt_actx_add_body(struct AttachCtx *actx, struct Body *new_body) 84 : { 85 4 : if (!actx || !new_body) 86 4 : return; 87 : 88 0 : if (actx->body_len == actx->body_max) 89 : { 90 0 : actx->body_max += 5; 91 0 : mutt_mem_realloc(&actx->body_idx, sizeof(struct Body *) * actx->body_max); 92 0 : for (int i = actx->body_len; i < actx->body_max; i++) 93 0 : actx->body_idx[i] = NULL; 94 : } 95 : 96 0 : actx->body_idx[actx->body_len++] = new_body; 97 : } 98 : 99 : /** 100 : * mutt_actx_entries_free - Free entries in an Attachment Context 101 : * @param actx Attachment context 102 : */ 103 4 : void mutt_actx_entries_free(struct AttachCtx *actx) 104 : { 105 4 : if (!actx) 106 2 : return; 107 : 108 2 : for (int i = 0; i < actx->idxlen; i++) 109 : { 110 0 : if (actx->idx[i]->body) 111 0 : actx->idx[i]->body->aptr = NULL; 112 0 : FREE(&actx->idx[i]->tree); 113 0 : FREE(&actx->idx[i]); 114 : } 115 2 : actx->idxlen = 0; 116 2 : actx->vcount = 0; 117 : 118 2 : for (int i = 0; i < actx->fp_len; i++) 119 0 : mutt_file_fclose(&actx->fp_idx[i]); 120 2 : actx->fp_len = 0; 121 : 122 2 : for (int i = 0; i < actx->body_len; i++) 123 0 : mutt_body_free(&actx->body_idx[i]); 124 2 : actx->body_len = 0; 125 : } 126 : 127 : /** 128 : * mutt_actx_new - Create a new Attachment Context 129 : * @retval ptr New Attachment Context 130 : */ 131 2 : struct AttachCtx *mutt_actx_new(void) 132 : { 133 2 : return mutt_mem_calloc(1, sizeof(struct AttachCtx)); 134 : } 135 : 136 : /** 137 : * mutt_actx_free - Free an Attachment Context 138 : * @param[out] ptr Attachment context 139 : */ 140 6 : void mutt_actx_free(struct AttachCtx **ptr) 141 : { 142 6 : if (!ptr || !*ptr) 143 4 : return; 144 : 145 2 : struct AttachCtx *actx = *ptr; 146 : 147 2 : mutt_actx_entries_free(actx); 148 2 : FREE(&actx->idx); 149 2 : FREE(&actx->v2r); 150 2 : FREE(&actx->fp_idx); 151 2 : FREE(&actx->body_idx); 152 2 : FREE(ptr); 153 : }