NeoMutt  2025-01-09-41-g086358
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
recvattach.h File Reference

Routines for managing attachments. More...

#include <stdbool.h>
#include <stdio.h>
+ Include dependency graph for recvattach.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void mutt_attach_init (struct AttachCtx *actx)
 Create a new Attachment context.
 
void mutt_update_tree (struct AttachCtx *actx)
 Refresh the list of attachments.
 
void dlg_attachment (struct ConfigSubset *sub, struct MailboxView *mv, struct Email *e, FILE *fp, bool attach_msg)
 Show the attachments in a Menu -.
 
void mutt_generate_recvattach_list (struct AttachCtx *actx, struct Email *e, struct Body *parts, FILE *fp, int parent_type, int level, bool decrypted)
 Create a list of attachments.
 
struct AttachPtrcurrent_attachment (struct AttachCtx *actx, struct Menu *menu)
 Get the current attachment.
 
void mutt_update_recvattach_menu (struct AttachCtx *actx, struct Menu *menu, bool init)
 Update the Attachment Menu.
 
void recvattach_edit_content_type (struct AttachCtx *actx, struct Menu *menu, struct Email *e)
 Edit the content type of an attachment.
 
int ba_add_tagged (struct BodyArray *ba, struct AttachCtx *actx, struct Menu *menu)
 Get an array of tagged Attachments.
 

Detailed Description

Routines for managing attachments.

Authors
  • Richard Russon

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/.

Definition in file recvattach.h.

Function Documentation

◆ mutt_attach_init()

void mutt_attach_init ( struct AttachCtx actx)

Create a new Attachment context.

Parameters
actxAttachment context

Definition at line 1188 of file recvattach.c.

1189{
1190 /* Collapse the attachments if '$digest_collapse' is set AND if...
1191 * the outer container is of type 'multipart/digest' */
1192 bool digest = mutt_istr_equal(actx->email->body->subtype, "digest");
1193
1194 const bool c_digest_collapse = cs_subset_bool(NeoMutt->sub, "digest_collapse");
1195 for (int i = 0; i < actx->idxlen; i++)
1196 {
1197 actx->idx[i]->body->tagged = false;
1198
1199 /* OR an inner container is of type 'multipart/digest' */
1200 actx->idx[i]->collapsed = (c_digest_collapse &&
1201 (digest ||
1202 ((actx->idx[i]->body->type == TYPE_MULTIPART) &&
1203 mutt_istr_equal(actx->idx[i]->body->subtype, "digest"))));
1204 }
1205}
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:47
@ TYPE_MULTIPART
Type: 'multipart/*'.
Definition: mime.h:37
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:672
struct Email * email
Used by recvattach for updating.
Definition: attach.h:64
struct AttachPtr ** idx
Array of attachments.
Definition: attach.h:67
short idxlen
Number of attachmentes.
Definition: attach.h:68
struct Body * body
Attachment.
Definition: attach.h:36
bool collapsed
Group is collapsed.
Definition: attach.h:44
bool tagged
This attachment is tagged.
Definition: body.h:90
char * subtype
content-type subtype
Definition: body.h:61
unsigned int type
content-type primary type, ContentType
Definition: body.h:40
struct Body * body
List of MIME parts.
Definition: email.h:69
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_update_tree()

void mutt_update_tree ( struct AttachCtx actx)

Refresh the list of attachments.

Parameters
actxAttachment context

Definition at line 122 of file recvattach.c.

123{
124 char buf[256] = { 0 };
125 char *s = NULL;
126
127 mutt_update_v2r(actx);
128
129 for (int vindex = 0; vindex < actx->vcount; vindex++)
130 {
131 const int rindex = actx->v2r[vindex];
132 actx->idx[rindex]->num = vindex;
133 if ((2 * (actx->idx[rindex]->level + 2)) < sizeof(buf))
134 {
135 if (actx->idx[rindex]->level)
136 {
137 s = buf + 2 * (actx->idx[rindex]->level - 1);
138 *s++ = (actx->idx[rindex]->body->next) ? MUTT_TREE_LTEE : MUTT_TREE_LLCORNER;
139 *s++ = MUTT_TREE_HLINE;
140 *s++ = MUTT_TREE_RARROW;
141 }
142 else
143 {
144 s = buf;
145 }
146 *s = '\0';
147 }
148
149 if (actx->idx[rindex]->tree)
150 {
151 if (!mutt_str_equal(actx->idx[rindex]->tree, buf))
152 mutt_str_replace(&actx->idx[rindex]->tree, buf);
153 }
154 else
155 {
156 actx->idx[rindex]->tree = mutt_str_dup(buf);
157 }
158
159 if (((2 * (actx->idx[rindex]->level + 2)) < sizeof(buf)) &&
160 actx->idx[rindex]->level)
161 {
162 s = buf + 2 * (actx->idx[rindex]->level - 1);
163 *s++ = (actx->idx[rindex]->body->next) ? MUTT_TREE_VLINE : MUTT_TREE_SPACE;
164 *s++ = MUTT_TREE_SPACE;
165 }
166 }
167}
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:280
@ MUTT_TREE_LLCORNER
Lower left corner.
Definition: mutt_thread.h:58
@ MUTT_TREE_RARROW
Right arrow.
Definition: mutt_thread.h:64
@ MUTT_TREE_LTEE
Left T-piece.
Definition: mutt_thread.h:60
@ MUTT_TREE_VLINE
Vertical line.
Definition: mutt_thread.h:62
@ MUTT_TREE_HLINE
Horizontal line.
Definition: mutt_thread.h:61
@ MUTT_TREE_SPACE
Blank space.
Definition: mutt_thread.h:63
static void mutt_update_v2r(struct AttachCtx *actx)
Update the virtual list of attachments.
Definition: recvattach.c:91
short vcount
The number of virtual attachments.
Definition: attach.h:72
short * v2r
Mapping from virtual to real attachment.
Definition: attach.h:71
char * tree
Tree characters to display.
Definition: attach.h:39
int num
Attachment index number.
Definition: attach.h:41
int level
Nesting depth of attachment.
Definition: attach.h:40
struct Body * next
next attachment in the list
Definition: body.h:72
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_generate_recvattach_list()

void mutt_generate_recvattach_list ( struct AttachCtx actx,
struct Email e,
struct Body b,
FILE *  fp,
int  parent_type,
int  level,
bool  decrypted 
)

Create a list of attachments.

Parameters
actxAttachment context
eEmail
bBody of email
fpFile to read from
parent_typeType, e.g. TYPE_MULTIPART
levelAttachment depth
decryptedTrue if attachment has been decrypted

Definition at line 1090 of file recvattach.c.

1093{
1094 struct Body *bp = NULL;
1095 struct Body *new_body = NULL;
1096 FILE *fp_new = NULL;
1098
1099 for (bp = b; bp; bp = bp->next)
1100 {
1101 bool need_secured = false;
1102 bool secured = false;
1103
1105 {
1106 need_secured = true;
1107
1108 if (type & SEC_ENCRYPT)
1109 {
1111 goto decrypt_failed;
1112
1113 if (e->env)
1115 }
1116
1117 secured = (crypt_smime_decrypt_mime(fp, &fp_new, bp, &new_body) == 0);
1118 /* If the decrypt/verify-opaque doesn't generate mime output, an empty
1119 * text/plain type will still be returned by mutt_read_mime_header().
1120 * We can't distinguish an actual part from a failure, so only use a
1121 * text/plain that results from a single top-level part. */
1122 if (secured && (new_body->type == TYPE_TEXT) &&
1123 mutt_istr_equal("plain", new_body->subtype) && ((b != bp) || bp->next))
1124 {
1125 mutt_body_free(&new_body);
1126 mutt_file_fclose(&fp_new);
1127 goto decrypt_failed;
1128 }
1129
1130 if (secured && (type & SEC_ENCRYPT))
1131 e->security |= SMIME_ENCRYPT;
1132 }
1133
1134 if (((WithCrypto & APPLICATION_PGP) != 0) &&
1136 {
1137 need_secured = true;
1138
1140 goto decrypt_failed;
1141
1142 secured = (crypt_pgp_decrypt_mime(fp, &fp_new, bp, &new_body) == 0);
1143
1144 if (secured)
1145 e->security |= PGP_ENCRYPT;
1146 }
1147
1148 if (need_secured && secured)
1149 {
1150 mutt_actx_add_fp(actx, fp_new);
1151 mutt_actx_add_body(actx, new_body);
1152 mutt_generate_recvattach_list(actx, e, new_body, fp_new, parent_type, level, 1);
1153 continue;
1154 }
1155
1156 decrypt_failed:
1157 /* Fall through and show the original parts if decryption fails */
1158 if (need_secured && !secured)
1159 mutt_error(_("Can't decrypt encrypted message"));
1160
1161 struct AttachPtr *ap = mutt_aptr_new();
1162 mutt_actx_add_attach(actx, ap);
1163
1164 ap->body = bp;
1165 ap->fp = fp;
1166 bp->aptr = ap;
1168 ap->level = level;
1169 ap->decrypted = decrypted;
1170
1171 if (mutt_is_message_type(bp->type, bp->subtype))
1172 {
1173 mutt_generate_recvattach_list(actx, bp->email, bp->parts, fp, bp->type,
1174 level + 1, decrypted);
1175 e->security |= bp->email->security;
1176 }
1177 else
1178 {
1179 mutt_generate_recvattach_list(actx, e, bp->parts, fp, bp->type, level + 1, decrypted);
1180 }
1181 }
1182}
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_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_add_body(struct AttachCtx *actx, struct Body *b)
Add an email body to an Attachment Context.
Definition: attach.c:142
SecurityFlags mutt_is_application_smime(struct Body *b)
Does the message use S/MIME?
Definition: crypt.c:609
bool crypt_valid_passphrase(SecurityFlags flags)
Check that we have a usable passphrase, ask if not.
Definition: crypt.c:132
SecurityFlags mutt_is_multipart_encrypted(struct Body *b)
Does the message have encrypted parts?
Definition: crypt.c:443
SecurityFlags mutt_is_malformed_multipart_pgp_encrypted(struct Body *b)
Check for malformed layout.
Definition: crypt.c:504
int crypt_pgp_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
Wrapper for CryptModuleSpecs::decrypt_mime()
Definition: cryptglue.c:210
void crypt_smime_getkeys(struct Envelope *env)
Wrapper for CryptModuleSpecs::smime_getkeys()
Definition: cryptglue.c:454
int crypt_smime_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **b_dec)
Wrapper for CryptModuleSpecs::decrypt_mime()
Definition: cryptglue.c:432
void mutt_body_free(struct Body **ptr)
Free a Body.
Definition: body.c:58
bool mutt_is_message_type(int type, const char *subtype)
Determine if a mime type matches a message or not.
Definition: parse.c:1498
#define mutt_file_fclose(FP)
Definition: file.h:139
#define mutt_error(...)
Definition: logging2.h:92
@ TYPE_TEXT
Type: 'text/*'.
Definition: mime.h:38
#define _(a)
Definition: message.h:28
uint16_t SecurityFlags
Flags, e.g. SEC_ENCRYPT.
Definition: lib.h:82
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition: lib.h:96
#define PGP_ENCRYPT
Definition: lib.h:102
#define APPLICATION_SMIME
Use SMIME to encrypt/sign.
Definition: lib.h:97
#define SEC_ENCRYPT
Email is encrypted.
Definition: lib.h:84
#define SMIME_ENCRYPT
Definition: lib.h:108
#define WithCrypto
Definition: lib.h:122
void mutt_generate_recvattach_list(struct AttachCtx *actx, struct Email *e, struct Body *b, FILE *fp, int parent_type, int level, bool decrypted)
Create a list of attachments.
Definition: recvattach.c:1090
An email to which things will be attached.
Definition: attach.h:35
FILE * fp
Used in the recvattach menu.
Definition: attach.h:37
bool decrypted
Not part of message as stored in the email->body.
Definition: attach.h:43
int parent_type
Type of parent attachment, e.g. TYPE_MULTIPART.
Definition: attach.h:38
The body of an email.
Definition: body.h:36
struct Body * parts
parts of a multipart or message/rfc822
Definition: body.h:73
struct AttachPtr * aptr
Menu information, used in recvattach.c.
Definition: body.h:75
struct Email * email
header information for message/rfc822
Definition: body.h:74
struct Envelope * env
Envelope information.
Definition: email.h:68
SecurityFlags security
bit 0-10: flags, bit 11,12: application, bit 13: traditional pgp See: ncrypt/lib.h pgplib....
Definition: email.h:43
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ current_attachment()

struct AttachPtr * current_attachment ( struct AttachCtx actx,
struct Menu menu 
)

Get the current attachment.

Parameters
actxAttachment context
menuMenu
Return values
ptrCurrent Attachment

Definition at line 77 of file recvattach.c.

78{
79 const int virt = menu_get_index(menu);
80 const int index = actx->v2r[virt];
81
82 return actx->idx[index];
83}
int menu_get_index(struct Menu *menu)
Get the current selection in the Menu.
Definition: menu.c:160
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_update_recvattach_menu()

void mutt_update_recvattach_menu ( struct AttachCtx actx,
struct Menu menu,
bool  init 
)

Update the Attachment Menu.

Parameters
actxAttachment context
menuMenu listing Attachments
initIf true, create a new Attachments context

Definition at line 1213 of file recvattach.c.

1214{
1215 if (init)
1216 {
1217 mutt_generate_recvattach_list(actx, actx->email, actx->email->body,
1218 actx->fp_root, -1, 0, 0);
1219 mutt_attach_init(actx);
1220 }
1221
1222 mutt_update_tree(actx);
1223
1224 menu->max = actx->vcount;
1225
1226 const int index = menu_get_index(menu);
1227 if (index >= menu->max)
1228 menu_set_index(menu, menu->max - 1);
1230}
#define MENU_REDRAW_INDEX
Redraw the index.
Definition: lib.h:56
void menu_queue_redraw(struct Menu *menu, MenuRedrawFlags redraw)
Queue a request for a redraw.
Definition: menu.c:184
MenuRedrawFlags menu_set_index(struct Menu *menu, int index)
Set the current selection in the Menu.
Definition: menu.c:174
void mutt_attach_init(struct AttachCtx *actx)
Create a new Attachment context.
Definition: recvattach.c:1188
void mutt_update_tree(struct AttachCtx *actx)
Refresh the list of attachments.
Definition: recvattach.c:122
FILE * fp_root
Used by recvattach for updating.
Definition: attach.h:65
int max
Number of entries in the menu.
Definition: lib.h:81
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ recvattach_edit_content_type()

void recvattach_edit_content_type ( struct AttachCtx actx,
struct Menu menu,
struct Email e 
)

Edit the content type of an attachment.

Parameters
actxAttachment context
menuMenu listing Attachments
eEmail

Definition at line 931 of file recvattach.c.

932{
933 struct AttachPtr *cur_att = current_attachment(actx, menu);
934 if (!mutt_edit_content_type(e, cur_att->body, cur_att->fp))
935 return;
936
937 /* The mutt_update_recvattach_menu() will overwrite any changes
938 * made to a decrypted cur_att->body, so warn the user. */
939 if (cur_att->decrypted)
940 {
941 mutt_message(_("Structural changes to decrypted attachments are not supported"));
942 mutt_sleep(1);
943 }
944 /* Editing the content type can rewrite the body structure. */
945 for (int i = 0; i < actx->idxlen; i++)
946 actx->idx[i]->body = NULL;
948 mutt_update_recvattach_menu(actx, menu, true);
949}
void mutt_actx_entries_free(struct AttachCtx *actx)
Free entries in an Attachment Context.
Definition: attach.c:162
bool mutt_edit_content_type(struct Email *e, struct Body *b, FILE *fp)
Edit the content type of an attachment.
Definition: external.c:1070
#define mutt_message(...)
Definition: logging2.h:91
void mutt_sleep(short s)
Sleep for a while.
Definition: muttlib.c:842
void mutt_update_recvattach_menu(struct AttachCtx *actx, struct Menu *menu, bool init)
Update the Attachment Menu.
Definition: recvattach.c:1213
struct AttachPtr * current_attachment(struct AttachCtx *actx, struct Menu *menu)
Get the current attachment.
Definition: recvattach.c:77
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ba_add_tagged()

int ba_add_tagged ( struct BodyArray *  ba,
struct AttachCtx actx,
struct Menu menu 
)

Get an array of tagged Attachments.

Parameters
baEmpty BodyArray to populate
actxList of Attachments
menuMenu
Return values
numNumber of selected Attachments
-1Error

Definition at line 1240 of file recvattach.c.

1241{
1242 if (!ba || !actx || !menu)
1243 return -1;
1244
1245 if (menu->tag_prefix)
1246 {
1247 for (int i = 0; i < actx->idxlen; i++)
1248 {
1249 struct Body *b = actx->idx[i]->body;
1250 if (b->tagged)
1251 {
1252 ARRAY_ADD(ba, b);
1253 }
1254 }
1255 }
1256 else
1257 {
1258 struct AttachPtr *cur = current_attachment(actx, menu);
1259 if (!cur)
1260 return -1;
1261
1262 ARRAY_ADD(ba, cur->body);
1263 }
1264
1265 return ARRAY_SIZE(ba);
1266}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition: array.h:156
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:87
bool tag_prefix
User has pressed <tag-prefix>
Definition: lib.h:85
+ Here is the call graph for this function:
+ Here is the caller graph for this function: