forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattach.c
211 lines (180 loc) · 5.09 KB
/
attach.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* @file
* Handling of email attachments
*
* @authors
* Copyright (C) 1996-2000,2002,2013 Michael R. Elkins <[email protected]>
* Copyright (C) 1999-2004,2006 Thomas Roessler <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page attach_attach Email attachments
*
* Handling of email attachments
*/
#include "config.h"
#include <string.h>
#include "mutt/lib.h"
#include "email/lib.h"
#include "attach.h"
/**
* mutt_aptr_new - Create a new Attachment Pointer
* @retval ptr New Attachment Pointer
*/
struct AttachPtr *mutt_aptr_new(void)
{
return mutt_mem_calloc(1, sizeof(struct AttachPtr));
}
/**
* mutt_aptr_free - Free an Attachment Pointer
* @param[out] ptr Attachment Pointer
*/
void mutt_aptr_free(struct AttachPtr **ptr)
{
if (!ptr || !*ptr)
return;
struct AttachPtr *aptr = *ptr;
FREE(&aptr->tree);
FREE(ptr);
}
/**
* mutt_actx_add_attach - Add an Attachment to an Attachment Context
* @param actx Attachment context
* @param attach Attachment to add
*/
void mutt_actx_add_attach(struct AttachCtx *actx, struct AttachPtr *attach)
{
const int grow = 5;
if (!actx || !attach)
return;
if (actx->idxlen == actx->idxmax)
{
actx->idxmax += grow;
mutt_mem_realloc(&actx->idx, actx->idxmax * sizeof(struct AttachPtr *));
mutt_mem_realloc(&actx->v2r, actx->idxmax * sizeof(short));
memset(&actx->idx[actx->idxlen], 0, grow * sizeof(struct AttachPtr *));
memset(&actx->v2r[actx->idxlen], 0, grow * sizeof(short));
}
actx->idx[actx->idxlen++] = attach;
}
/**
* mutt_actx_ins_attach - Insert an Attachment into an Attachment Context at Specified Index
* @param actx Attachment context
* @param attach Attachment to insert
* @param aidx Index to insert attachment at
*/
void mutt_actx_ins_attach(struct AttachCtx *actx, struct AttachPtr *attach, int aidx)
{
if (!actx || !attach)
return;
if ((aidx < 0) || (aidx > actx->idxmax))
return;
if (actx->idxlen == actx->idxmax)
{
actx->idxmax += 5;
mutt_mem_realloc(&actx->idx, sizeof(struct AttachPtr *) * actx->idxmax);
mutt_mem_realloc(&actx->v2r, sizeof(short) * actx->idxmax);
for (int i = actx->idxlen; i < actx->idxmax; i++)
actx->idx[i] = NULL;
}
actx->idxlen++;
for (int i = actx->idxlen - 1; i > aidx; i--)
actx->idx[i] = actx->idx[i - 1];
actx->idx[aidx] = attach;
}
/**
* mutt_actx_add_fp - Save a File handle to the Attachment Context
* @param actx Attachment context
* @param fp_new File handle to save
*/
void mutt_actx_add_fp(struct AttachCtx *actx, FILE *fp_new)
{
if (!actx || !fp_new)
return;
if (actx->fp_len == actx->fp_max)
{
actx->fp_max += 5;
mutt_mem_realloc(&actx->fp_idx, sizeof(FILE *) * actx->fp_max);
for (int i = actx->fp_len; i < actx->fp_max; i++)
actx->fp_idx[i] = NULL;
}
actx->fp_idx[actx->fp_len++] = fp_new;
}
/**
* mutt_actx_add_body - Add an email body to an Attachment Context
* @param actx Attachment context
* @param new_body Email Body to add
*/
void mutt_actx_add_body(struct AttachCtx *actx, struct Body *new_body)
{
if (!actx || !new_body)
return;
if (actx->body_len == actx->body_max)
{
actx->body_max += 5;
mutt_mem_realloc(&actx->body_idx, sizeof(struct Body *) * actx->body_max);
for (int i = actx->body_len; i < actx->body_max; i++)
actx->body_idx[i] = NULL;
}
actx->body_idx[actx->body_len++] = new_body;
}
/**
* mutt_actx_entries_free - Free entries in an Attachment Context
* @param actx Attachment context
*/
void mutt_actx_entries_free(struct AttachCtx *actx)
{
if (!actx)
return;
for (int i = 0; i < actx->idxlen; i++)
{
if (actx->idx[i]->body)
actx->idx[i]->body->aptr = NULL;
mutt_aptr_free(&actx->idx[i]);
}
actx->idxlen = 0;
actx->vcount = 0;
for (int i = 0; i < actx->fp_len; i++)
mutt_file_fclose(&actx->fp_idx[i]);
actx->fp_len = 0;
for (int i = 0; i < actx->body_len; i++)
mutt_body_free(&actx->body_idx[i]);
actx->body_len = 0;
}
/**
* mutt_actx_new - Create a new Attachment Context
* @retval ptr New Attachment Context
*/
struct AttachCtx *mutt_actx_new(void)
{
return mutt_mem_calloc(1, sizeof(struct AttachCtx));
}
/**
* mutt_actx_free - Free an Attachment Context
* @param[out] ptr Attachment context
*/
void mutt_actx_free(struct AttachCtx **ptr)
{
if (!ptr || !*ptr)
return;
struct AttachCtx *actx = *ptr;
mutt_actx_entries_free(actx);
FREE(&actx->idx);
FREE(&actx->v2r);
FREE(&actx->fp_idx);
FREE(&actx->body_idx);
FREE(ptr);
}