NeoMutt  2022-04-29-215-gc12b98
Teaching an old dog new tricks
DOXYGEN
attach.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <string.h>
32#include "mutt/lib.h"
33#include "email/lib.h"
34#include "attach.h"
35
41{
42 return mutt_mem_calloc(1, sizeof(struct AttachPtr));
43}
44
49void mutt_aptr_free(struct AttachPtr **ptr)
50{
51 if (!ptr || !*ptr)
52 return;
53
54 struct AttachPtr *aptr = *ptr;
55 FREE(&aptr->tree);
56
57 FREE(ptr);
58}
59
65void mutt_actx_add_attach(struct AttachCtx *actx, struct AttachPtr *attach)
66{
67 const int grow = 5;
68
69 if (!actx || !attach)
70 return;
71
72 if (actx->idxlen == actx->idxmax)
73 {
74 actx->idxmax += grow;
75 mutt_mem_realloc(&actx->idx, actx->idxmax * sizeof(struct AttachPtr *));
76 mutt_mem_realloc(&actx->v2r, actx->idxmax * sizeof(short));
77
78 memset(&actx->idx[actx->idxlen], 0, grow * sizeof(struct AttachPtr *));
79 memset(&actx->v2r[actx->idxlen], 0, grow * sizeof(short));
80 }
81
82 actx->idx[actx->idxlen++] = attach;
83}
84
91void mutt_actx_ins_attach(struct AttachCtx *actx, struct AttachPtr *attach, int aidx)
92{
93 if (!actx || !attach)
94 return;
95
96 if ((aidx < 0) || (aidx > actx->idxmax))
97 return;
98
99 if (actx->idxlen == actx->idxmax)
100 {
101 actx->idxmax += 5;
102 mutt_mem_realloc(&actx->idx, sizeof(struct AttachPtr *) * actx->idxmax);
103 mutt_mem_realloc(&actx->v2r, sizeof(short) * actx->idxmax);
104 for (int i = actx->idxlen; i < actx->idxmax; i++)
105 actx->idx[i] = NULL;
106 }
107
108 actx->idxlen++;
109
110 for (int i = actx->idxlen - 1; i > aidx; i--)
111 actx->idx[i] = actx->idx[i - 1];
112
113 actx->idx[aidx] = attach;
114}
115
121void mutt_actx_add_fp(struct AttachCtx *actx, FILE *fp_new)
122{
123 if (!actx || !fp_new)
124 return;
125
126 if (actx->fp_len == actx->fp_max)
127 {
128 actx->fp_max += 5;
129 mutt_mem_realloc(&actx->fp_idx, sizeof(FILE *) * actx->fp_max);
130 for (int i = actx->fp_len; i < actx->fp_max; i++)
131 actx->fp_idx[i] = NULL;
132 }
133
134 actx->fp_idx[actx->fp_len++] = fp_new;
135}
136
142void mutt_actx_add_body(struct AttachCtx *actx, struct Body *new_body)
143{
144 if (!actx || !new_body)
145 return;
146
147 if (actx->body_len == actx->body_max)
148 {
149 actx->body_max += 5;
150 mutt_mem_realloc(&actx->body_idx, sizeof(struct Body *) * actx->body_max);
151 for (int i = actx->body_len; i < actx->body_max; i++)
152 actx->body_idx[i] = NULL;
153 }
154
155 actx->body_idx[actx->body_len++] = new_body;
156}
157
163{
164 if (!actx)
165 return;
166
167 for (int i = 0; i < actx->idxlen; i++)
168 {
169 if (actx->idx[i]->body)
170 actx->idx[i]->body->aptr = NULL;
171 mutt_aptr_free(&actx->idx[i]);
172 }
173 actx->idxlen = 0;
174 actx->vcount = 0;
175
176 for (int i = 0; i < actx->fp_len; i++)
177 mutt_file_fclose(&actx->fp_idx[i]);
178 actx->fp_len = 0;
179
180 for (int i = 0; i < actx->body_len; i++)
181 mutt_body_free(&actx->body_idx[i]);
182 actx->body_len = 0;
183}
184
190{
191 return mutt_mem_calloc(1, sizeof(struct AttachCtx));
192}
193
198void mutt_actx_free(struct AttachCtx **ptr)
199{
200 if (!ptr || !*ptr)
201 return;
202
203 struct AttachCtx *actx = *ptr;
204
206 FREE(&actx->idx);
207 FREE(&actx->v2r);
208 FREE(&actx->fp_idx);
209 FREE(&actx->body_idx);
210 FREE(ptr);
211}
struct AttachCtx * mutt_actx_new(void)
Create a new Attachment Context.
Definition: attach.c:189
void mutt_actx_add_body(struct AttachCtx *actx, struct Body *new_body)
Add an email body to an Attachment Context.
Definition: attach.c:142
void mutt_actx_add_attach(struct AttachCtx *actx, struct AttachPtr *attach)
Add an Attachment to an Attachment Context.
Definition: attach.c:65
void mutt_actx_ins_attach(struct AttachCtx *actx, struct AttachPtr *attach, int aidx)
Insert an Attachment into an Attachment Context at Specified Index.
Definition: attach.c:91
void mutt_actx_add_fp(struct AttachCtx *actx, FILE *fp_new)
Save a File handle to the Attachment Context.
Definition: attach.c:121
struct AttachPtr * mutt_aptr_new(void)
Create a new Attachment Pointer.
Definition: attach.c:40
void mutt_actx_entries_free(struct AttachCtx *actx)
Free entries in an Attachment Context.
Definition: attach.c:162
void mutt_aptr_free(struct AttachPtr **ptr)
Free an Attachment Pointer.
Definition: attach.c:49
void mutt_actx_free(struct AttachCtx **ptr)
Free an Attachment Context.
Definition: attach.c:198
Handling of email attachments.
void mutt_body_free(struct Body **ptr)
Free a Body.
Definition: body.c:57
Structs that make up an email.
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:152
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
#define FREE(x)
Definition: memory.h:43
Convenience wrapper for the library headers.
A set of attachments.
Definition: attach.h:51
short vcount
The number of virtual attachments.
Definition: attach.h:60
short body_len
Number of Body parts.
Definition: attach.h:67
short fp_max
Size of FILE array.
Definition: attach.h:64
struct AttachPtr ** idx
Array of attachments.
Definition: attach.h:55
struct Body ** body_idx
Extra struct Body* used for decryption.
Definition: attach.h:66
short fp_len
Number of FILE handles.
Definition: attach.h:63
short body_max
Size of Body array.
Definition: attach.h:68
FILE ** fp_idx
Extra FILE* used for decryption.
Definition: attach.h:62
short idxmax
Size of attachment array.
Definition: attach.h:57
short idxlen
Number of attachmentes.
Definition: attach.h:56
short * v2r
Mapping from virtual to real attachment.
Definition: attach.h:59
An email to which things will be attached.
Definition: attach.h:35
struct Body * body
Attachment.
Definition: attach.h:36
char * tree
Tree characters to display.
Definition: attach.h:39
The body of an email.
Definition: body.h:36
struct AttachPtr * aptr
Menu information, used in recvattach.c.
Definition: body.h:74