NeoMutt  2024-11-14-138-ge5ca67
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
expando.c
Go to the documentation of this file.
1
29#include <stdbool.h>
30#include <stdio.h>
31#include "mutt/lib.h"
32#include "email/lib.h"
33#include "core/lib.h"
34#include "expando.h"
35#include "lib.h"
36#include "color/lib.h"
37#include "expando/lib.h"
38#include "index/lib.h"
39#include "attach.h"
40#include "muttlib.h"
41
42static void body_file_disposition(const struct ExpandoNode *node, void *data,
43 MuttFormatFlags flags, struct Buffer *buf);
44
48static void attach_charset(const struct ExpandoNode *node, void *data,
49 MuttFormatFlags flags, struct Buffer *buf)
50{
51 const struct AttachPtr *aptr = data;
52
53 char tmp[128] = { 0 };
54
55 if (mutt_is_text_part(aptr->body) && mutt_body_get_charset(aptr->body, tmp, sizeof(tmp)))
56 {
57 buf_strcpy(buf, tmp);
58 }
59}
60
64static long attach_number_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
65{
66 const struct AttachPtr *aptr = data;
67
68 return aptr->num + 1;
69}
70
74static void attach_tree(const struct ExpandoNode *node, void *data,
75 MuttFormatFlags flags, struct Buffer *buf)
76{
77 const struct AttachPtr *aptr = data;
78
80 node_expando_set_has_tree(node, true);
81 const char *s = aptr->tree;
82 buf_strcpy(buf, s);
83}
84
88static long body_attach_count_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
89{
90 const struct AttachPtr *aptr = data;
91 const struct Body *body = aptr->body;
92
93 return body->attach_count + body->attach_qualifies;
94}
95
99static void body_attach_qualifies(const struct ExpandoNode *node, void *data,
100 MuttFormatFlags flags, struct Buffer *buf)
101{
102 const struct AttachPtr *aptr = data;
103
104 // NOTE(g0mb4): use $to_chars?
105 const char *s = aptr->body->attach_qualifies ? "Q" : " ";
106 buf_strcpy(buf, s);
107}
108
112static long body_attach_qualifies_num(const struct ExpandoNode *node,
113 void *data, MuttFormatFlags flags)
114{
115 const struct AttachPtr *aptr = data;
116 return aptr->body->attach_qualifies;
117}
118
122static void body_charset_convert(const struct ExpandoNode *node, void *data,
123 MuttFormatFlags flags, struct Buffer *buf)
124{
125 const struct AttachPtr *aptr = data;
126
127 // NOTE(g0mb4): use $to_chars?
128 const char *s = ((aptr->body->type != TYPE_TEXT) || aptr->body->noconv) ? "n" : "c";
129 buf_strcpy(buf, s);
130}
131
135static void body_deleted(const struct ExpandoNode *node, void *data,
136 MuttFormatFlags flags, struct Buffer *buf)
137{
138 const struct AttachPtr *aptr = data;
139
140 // NOTE(g0mb4): use $to_chars?
141 const char *s = aptr->body->deleted ? "D" : " ";
142 buf_strcpy(buf, s);
143}
144
148static long body_deleted_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
149{
150 const struct AttachPtr *aptr = data;
151 return aptr->body->deleted;
152}
153
157static void body_description(const struct ExpandoNode *node, void *data,
158 MuttFormatFlags flags, struct Buffer *buf)
159{
160 const struct AttachPtr *aptr = data;
161
162 const struct Expando *c_message_format = cs_subset_expando(NeoMutt->sub, "message_format");
163 if (aptr->body->description)
164 {
165 const char *s = aptr->body->description;
166 buf_strcpy(buf, s);
167 return;
168 }
169
170 if (mutt_is_message_type(aptr->body->type, aptr->body->subtype) &&
171 c_message_format && aptr->body->email)
172 {
173 mutt_make_string(buf, -1, c_message_format, NULL, -1, aptr->body->email,
175
176 return;
177 }
178
179 if (!aptr->body->d_filename && !aptr->body->filename)
180 {
181 const char *s = "<no description>";
182 buf_strcpy(buf, s);
183 return;
184 }
185
186 body_file_disposition(node, data, flags, buf);
187}
188
192static void body_disposition(const struct ExpandoNode *node, void *data,
193 MuttFormatFlags flags, struct Buffer *buf)
194{
195 const struct AttachPtr *aptr = data;
196
197 // NOTE(g0mb4): use $to_chars?
198 static const char dispchar[] = { 'I', 'A', 'F', '-' };
199 char ch;
200
201 if (aptr->body->disposition < sizeof(dispchar))
202 {
203 ch = dispchar[aptr->body->disposition];
204 }
205 else
206 {
207 mutt_debug(LL_DEBUG1, "ERROR: invalid content-disposition %d\n", aptr->body->disposition);
208 ch = '!';
209 }
210
211 buf_printf(buf, "%c", ch);
212}
213
217static void body_file(const struct ExpandoNode *node, void *data,
218 MuttFormatFlags flags, struct Buffer *buf)
219{
220 const struct AttachPtr *aptr = data;
221
222 if (aptr->body->filename && (*aptr->body->filename == '/'))
223 {
224 struct Buffer *path = buf_pool_get();
225
226 buf_strcpy(path, aptr->body->filename);
227 buf_pretty_mailbox(path);
228 buf_copy(buf, path);
229 buf_pool_release(&path);
230 }
231 else
232 {
233 const char *s = aptr->body->filename;
234 buf_strcpy(buf, s);
235 }
236}
237
241static void body_file_disposition(const struct ExpandoNode *node, void *data,
242 MuttFormatFlags flags, struct Buffer *buf)
243{
244 const struct AttachPtr *aptr = data;
245
246 if (aptr->body->d_filename)
247 {
248 const char *s = aptr->body->d_filename;
249 buf_strcpy(buf, s);
250 return;
251 }
252
253 body_file(node, data, flags, buf);
254}
255
259static void body_file_size(const struct ExpandoNode *node, void *data,
260 MuttFormatFlags flags, struct Buffer *buf)
261{
262 const struct AttachPtr *aptr = data;
263
264 char tmp[128] = { 0 };
265
266 size_t l = 0;
267 if (aptr->body->filename && (flags & MUTT_FORMAT_STAT_FILE))
268 {
269 l = mutt_file_get_size(aptr->body->filename);
270 }
271 else
272 {
273 l = aptr->body->length;
274 }
275
276 mutt_str_pretty_size(tmp, sizeof(tmp), l);
277 buf_strcpy(buf, tmp);
278}
279
283static long body_file_size_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
284{
285 const struct AttachPtr *aptr = data;
286
287 if (aptr->body->filename && (flags & MUTT_FORMAT_STAT_FILE))
288 return mutt_file_get_size(aptr->body->filename);
289
290 return aptr->body->length;
291}
292
296static void body_mime_encoding(const struct ExpandoNode *node, void *data,
297 MuttFormatFlags flags, struct Buffer *buf)
298{
299 const struct AttachPtr *aptr = data;
300
301 const char *s = ENCODING(aptr->body->encoding);
302 buf_strcpy(buf, s);
303}
304
308static void body_mime_major(const struct ExpandoNode *node, void *data,
309 MuttFormatFlags flags, struct Buffer *buf)
310{
311 const struct AttachPtr *aptr = data;
312
313 const char *s = TYPE(aptr->body);
314 buf_strcpy(buf, s);
315}
316
320static void body_mime_minor(const struct ExpandoNode *node, void *data,
321 MuttFormatFlags flags, struct Buffer *buf)
322{
323 const struct AttachPtr *aptr = data;
324
325 const char *s = aptr->body->subtype;
326 buf_strcpy(buf, s);
327}
328
332static void body_tagged(const struct ExpandoNode *node, void *data,
333 MuttFormatFlags flags, struct Buffer *buf)
334{
335 const struct AttachPtr *aptr = data;
336
337 // NOTE(g0mb4): use $to_chars?
338 const char *s = aptr->body->tagged ? "*" : " ";
339 buf_strcpy(buf, s);
340}
341
345static long body_tagged_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
346{
347 const struct AttachPtr *aptr = data;
348 return aptr->body->tagged;
349}
350
354static void body_unlink(const struct ExpandoNode *node, void *data,
355 MuttFormatFlags flags, struct Buffer *buf)
356{
357 const struct AttachPtr *aptr = data;
358
359 // NOTE(g0mb4): use $to_chars?
360 const char *s = aptr->body->unlink ? "-" : " ";
361 buf_strcpy(buf, s);
362}
363
367static long body_unlink_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
368{
369 const struct AttachPtr *aptr = data;
370 return aptr->body->unlink;
371}
372
379 // clang-format off
380
384
389 { ED_BODY, ED_BOD_FILE, body_file, NULL },
399
400 { -1, -1, NULL, NULL },
401 // clang-format on
402};
const struct ExpandoRenderCallback AttachRenderCallbacks[]
Callbacks for Attachment Expandos.
Definition: expando.c:378
Handling of email attachments.
@ ED_ATT_NUMBER
AttachPtr.num.
Definition: attach.h:55
@ ED_ATT_TREE
AttachPtr.tree.
Definition: attach.h:56
@ ED_ATT_CHARSET
AttachPtr.body.
Definition: attach.h:54
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:395
size_t buf_copy(struct Buffer *dst, const struct Buffer *src)
Copy a Buffer's contents to another Buffer.
Definition: buffer.c:601
Color and attribute parsing.
@ MT_COLOR_TREE
Index: tree-drawing characters.
Definition: color.h:79
const struct Expando * cs_subset_expando(const struct ConfigSubset *sub, const char *name)
Get an Expando config item by name.
Definition: config_type.c:357
Convenience wrapper for the core headers.
int mutt_make_string(struct Buffer *buf, size_t max_cols, const struct Expando *exp, struct Mailbox *m, int inpgr, struct Email *e, MuttFormatFlags flags, const char *progress)
Create formatted strings using mailbox expandos.
Definition: dlg_index.c:803
@ ED_BODY
Body ED_BOD_ ExpandoDataBody.
Definition: domain.h:38
@ ED_ATTACH
Attach ED_ATT_ ExpandoDataAttach.
Definition: domain.h:36
char * mutt_body_get_charset(struct Body *b, char *buf, size_t buflen)
Get a body's character set.
Definition: body.c:133
@ ED_BOD_DESCRIPTION
Body.description.
Definition: body.h:106
@ ED_BOD_CHARSET_CONVERT
Body.type.
Definition: body.h:104
@ ED_BOD_DELETED
Body.deleted.
Definition: body.h:105
@ ED_BOD_UNLINK
Body.unlink.
Definition: body.h:115
@ ED_BOD_FILE_SIZE
Body.filename.
Definition: body.h:110
@ ED_BOD_DISPOSITION
Body.disposition.
Definition: body.h:107
@ ED_BOD_ATTACH_QUALIFIES
Body.attach_qualifies.
Definition: body.h:103
@ ED_BOD_MIME_MAJOR
Body.type, Body.xtype.
Definition: body.h:112
@ ED_BOD_TAGGED
Body.tagged.
Definition: body.h:114
@ ED_BOD_ATTACH_COUNT
Body.attach_count.
Definition: body.h:102
@ ED_BOD_FILE
Body.filename.
Definition: body.h:108
@ ED_BOD_MIME_MINOR
Body.subtype.
Definition: body.h:113
@ ED_BOD_FILE_DISPOSITION
Body.d_filename.
Definition: body.h:109
@ ED_BOD_MIME_ENCODING
Body.encoding.
Definition: body.h:111
Structs that make up an email.
bool mutt_is_message_type(int type, const char *subtype)
Determine if a mime type matches a message or not.
Definition: parse.c:1498
Parse Expando string.
long mutt_file_get_size(const char *path)
Get the size of a file.
Definition: file.c:1519
static long body_deleted_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
Body: Deleted - Implements get_number_t -.
Definition: expando.c:148
static long body_tagged_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
Body: Is Tagged - Implements get_number_t -.
Definition: expando.c:345
static long body_file_size_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
Body: Size - Implements get_number_t -.
Definition: expando.c:283
static long attach_number_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
Attachment: Index number - Implements get_number_t -.
Definition: expando.c:64
static long body_attach_count_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
Body: Number of MIME parts - Implements get_number_t -.
Definition: expando.c:88
static long body_unlink_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
Body: Unlink flag - Implements get_number_t -.
Definition: expando.c:367
static long body_attach_qualifies_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
Body: Attachment counting - Implements get_number_t -.
Definition: expando.c:112
static void body_deleted(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Body: Deleted - Implements get_string_t -.
Definition: expando.c:135
static void body_file_size(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Body: Size - Implements get_string_t -.
Definition: expando.c:259
static void body_tagged(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Body: Is Tagged - Implements get_string_t -.
Definition: expando.c:332
static void body_disposition(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Body: Disposition flag - Implements get_string_t -.
Definition: expando.c:192
static void body_charset_convert(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Body: Requires conversion flag - Implements get_string_t -.
Definition: expando.c:122
static void attach_tree(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Attachment: Tree characters - Implements get_string_t -.
Definition: expando.c:74
static void attach_charset(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Attachment: Charset - Implements get_string_t -.
Definition: expando.c:48
static void body_mime_major(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Body: Major MIME type - Implements get_string_t -.
Definition: expando.c:308
static void body_file_disposition(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Body: Filename in header - Implements get_string_t -.
Definition: expando.c:241
static void body_description(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Body: Description - Implements get_string_t -.
Definition: expando.c:157
static void body_mime_encoding(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Body: MIME type - Implements get_string_t -.
Definition: expando.c:296
static void body_file(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Body: Filename - Implements get_string_t -.
Definition: expando.c:217
static void body_mime_minor(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Body: MIME subtype - Implements get_string_t -.
Definition: expando.c:320
static void body_unlink(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Body: Unlink flag - Implements get_string_t -.
Definition: expando.c:354
static void body_attach_qualifies(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Body: Attachment counting - Implements get_string_t -.
Definition: expando.c:99
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
GUI manage the main index (list of emails)
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
@ TYPE_TEXT
Type: 'text/*'.
Definition: mime.h:38
#define ENCODING(x)
Definition: mime.h:92
#define TYPE(body)
Definition: mime.h:89
Convenience wrapper for the library headers.
bool mutt_is_text_part(const struct Body *b)
Is this part of an email in plain text?
Definition: muttlib.c:407
void buf_pretty_mailbox(struct Buffer *buf)
Shorten a mailbox path using '~' or '='.
Definition: muttlib.c:519
void mutt_str_pretty_size(char *buf, size_t buflen, size_t num)
Display an abbreviated size, like 3.4K.
Definition: muttlib.c:1004
Some miscellaneous functions.
void node_expando_set_color(const struct ExpandoNode *node, int cid)
Set the colour for an Expando.
Definition: node_expando.c:100
void node_expando_set_has_tree(const struct ExpandoNode *node, bool has_tree)
Set the has_tree flag for an Expando.
Definition: node_expando.c:115
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:96
#define MUTT_FORMAT_FORCESUBJ
Print the subject even if unchanged.
Definition: render.h:34
#define MUTT_FORMAT_ARROWCURSOR
Reserve space for arrow_cursor.
Definition: render.h:37
uint8_t MuttFormatFlags
Flags for expando_render(), e.g. MUTT_FORMAT_FORCESUBJ.
Definition: render.h:32
#define MUTT_FORMAT_STAT_FILE
Used by attach_format_str.
Definition: render.h:36
Sidebar Expando definitions.
Key value store.
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
int num
Attachment index number.
Definition: attach.h:41
The body of an email.
Definition: body.h:36
char * d_filename
filename to be used for the content-disposition header If NULL, filename is used instead.
Definition: body.h:56
signed short attach_count
Number of attachments.
Definition: body.h:91
bool deleted
Attachment marked for deletion.
Definition: body.h:88
bool noconv
Don't do character set conversion.
Definition: body.h:46
bool unlink
If true, filename should be unlink()ed before free()ing this structure.
Definition: body.h:68
LOFF_T length
length (in bytes) of attachment
Definition: body.h:53
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
char * description
content-description
Definition: body.h:55
unsigned int disposition
content-disposition, ContentDisposition
Definition: body.h:42
bool attach_qualifies
This attachment should be counted.
Definition: body.h:87
bool tagged
This attachment is tagged.
Definition: body.h:90
char * subtype
content-type subtype
Definition: body.h:61
unsigned int encoding
content-transfer-encoding, ContentEncoding
Definition: body.h:41
unsigned int type
content-type primary type, ContentType
Definition: body.h:40
char * filename
When sending a message, this is the file to which this structure refers.
Definition: body.h:59
String manipulation buffer.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:37
Basic Expando Node.
Definition: node.h:67
Parsed Expando trees.
Definition: expando.h:41
struct ExpandoNode * node
Parsed tree.
Definition: expando.h:43
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46