NeoMutt  2024-10-02-37-gfa9146
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
rfc3676.c
Go to the documentation of this file.
1
35#include "config.h"
36#include <stdbool.h>
37#include <stdio.h>
38#include <unistd.h>
39#include "mutt/lib.h"
40#include "config/lib.h"
41#include "email/lib.h"
42#include "core/lib.h"
43#include "gui/lib.h"
44#include "rfc3676.h"
45
46#define FLOWED_MAX 72
47
52{
53 size_t width;
54 size_t spaces;
55 bool delsp;
56};
57
63static int get_quote_level(const char *line)
64{
65 int quoted = 0;
66 const char *p = line;
67
68 while (p && (*p == '>'))
69 {
70 quoted++;
71 p++;
72 }
73
74 return quoted;
75}
76
87static int space_quotes(struct State *state)
88{
89 /* Allow quote spacing in the pager even for `$text_flowed`,
90 * but obviously not when replying. */
91 const bool c_text_flowed = cs_subset_bool(NeoMutt->sub, "text_flowed");
92 if (c_text_flowed && (state->flags & STATE_REPLYING))
93 return 0;
94
95 const bool c_reflow_space_quotes = cs_subset_bool(NeoMutt->sub, "reflow_space_quotes");
96 return c_reflow_space_quotes;
97}
98
110static bool add_quote_suffix(struct State *state, int ql)
111{
112 if (state->flags & STATE_REPLYING)
113 return false;
114
115 if (space_quotes(state))
116 return false;
117
118 if (!ql && !state->prefix)
119 return false;
120
121 /* The prefix will add its own space */
122 const bool c_text_flowed = cs_subset_bool(NeoMutt->sub, "text_flowed");
123 if (!c_text_flowed && !ql && state->prefix)
124 return false;
125
126 return true;
127}
128
136static size_t print_indent(int ql, struct State *state, int add_suffix)
137{
138 size_t wid = 0;
139
140 if (state->prefix)
141 {
142 /* use given prefix only for format=fixed replies to format=flowed,
143 * for format=flowed replies to format=flowed, use '>' indentation */
144 const bool c_text_flowed = cs_subset_bool(NeoMutt->sub, "text_flowed");
145 if (c_text_flowed)
146 {
147 ql++;
148 }
149 else
150 {
151 state_puts(state, state->prefix);
152 wid = mutt_strwidth(state->prefix);
153 }
154 }
155 for (int i = 0; i < ql; i++)
156 {
157 state_putc(state, '>');
158 if (space_quotes(state))
159 state_putc(state, ' ');
160 }
161 if (add_suffix)
162 state_putc(state, ' ');
163
164 if (space_quotes(state))
165 ql *= 2;
166
167 return ql + add_suffix + wid;
168}
169
175static void flush_par(struct State *state, struct FlowedState *fst)
176{
177 if (fst->width > 0)
178 {
179 state_putc(state, '\n');
180 fst->width = 0;
181 }
182 fst->spaces = 0;
183}
184
194static int quote_width(struct State *state, int ql)
195{
196 const int screen_width = (state->flags & STATE_DISPLAY) ? state->wraplen : 80;
197 const short c_reflow_wrap = cs_subset_number(NeoMutt->sub, "reflow_wrap");
198 int width = mutt_window_wrap_cols(screen_width, c_reflow_wrap);
199 const bool c_text_flowed = cs_subset_bool(NeoMutt->sub, "text_flowed");
200 if (c_text_flowed && (state->flags & STATE_REPLYING))
201 {
202 /* When replying, force a wrap at FLOWED_MAX to comply with RFC3676
203 * guidelines */
204 if (width > FLOWED_MAX)
205 width = FLOWED_MAX;
206 ql++; /* When replying, we will add an additional quote level */
207 }
208 /* adjust the paragraph width subtracting the number of prefix chars */
209 width -= space_quotes(state) ? ql * 2 : ql;
210 /* When displaying (not replying), there may be a space between the prefix
211 * string and the paragraph */
212 if (add_quote_suffix(state, ql))
213 width--;
214 /* failsafe for really long quotes */
215 if (width <= 0)
216 width = FLOWED_MAX; /* arbitrary, since the line will wrap */
217 return width;
218}
219
228static void print_flowed_line(char *line, struct State *state, int ql,
229 struct FlowedState *fst, bool term)
230{
231 size_t width, w, words = 0;
232 char *p = NULL;
233 char last;
234
235 if (!line || (*line == '\0'))
236 {
237 /* flush current paragraph (if any) first */
238 flush_par(state, fst);
239 print_indent(ql, state, 0);
240 state_putc(state, '\n');
241 return;
242 }
243
244 width = quote_width(state, ql);
245 last = line[mutt_str_len(line) - 1];
246
247 mutt_debug(LL_DEBUG5, "f=f: line [%s], width = %ld, spaces = %zu\n", line,
248 (long) width, fst->spaces);
249
250 for (words = 0; (p = mutt_str_sep(&line, " "));)
251 {
252 mutt_debug(LL_DEBUG5, "f=f: word [%s], width: %zu, remaining = [%s]\n", p,
253 fst->width, line);
254
255 /* remember number of spaces */
256 if (*p == '\0')
257 {
258 mutt_debug(LL_DEBUG3, "f=f: additional space\n");
259 fst->spaces++;
260 continue;
261 }
262 /* there's exactly one space prior to every but the first word */
263 if (words)
264 fst->spaces++;
265
266 w = mutt_strwidth(p);
267 /* see if we need to break the line but make sure the first word is put on
268 * the line regardless; if for DelSp=yes only one trailing space is used,
269 * we probably have a long word that we should break within (we leave that
270 * up to the pager or user) */
271 if (!(!fst->spaces && fst->delsp && (last != ' ')) && (w < width) &&
272 (w + fst->width + fst->spaces > width))
273 {
274 mutt_debug(LL_DEBUG3, "f=f: break line at %zu, %zu spaces left\n",
275 fst->width, fst->spaces);
276 /* only honor trailing spaces for format=flowed replies */
277 const bool c_text_flowed = cs_subset_bool(NeoMutt->sub, "text_flowed");
278 if (c_text_flowed)
279 for (; fst->spaces; fst->spaces--)
280 state_putc(state, ' ');
281 state_putc(state, '\n');
282 fst->width = 0;
283 fst->spaces = 0;
284 words = 0;
285 }
286
287 if (!words && !fst->width)
288 fst->width = print_indent(ql, state, add_quote_suffix(state, ql));
289 fst->width += w + fst->spaces;
290 for (; fst->spaces; fst->spaces--)
291 state_putc(state, ' ');
292 state_puts(state, p);
293 words++;
294 }
295
296 if (term)
297 flush_par(state, fst);
298}
299
307static void print_fixed_line(const char *line, struct State *state, int ql,
308 struct FlowedState *fst)
309{
310 print_indent(ql, state, add_quote_suffix(state, ql));
311 if (line && *line)
312 state_puts(state, line);
313 state_putc(state, '\n');
314
315 fst->width = 0;
316 fst->spaces = 0;
317}
318
323int rfc3676_handler(struct Body *b_email, struct State *state)
324{
325 char *buf = NULL;
326 unsigned int quotelevel = 0;
327 bool delsp = false;
328 size_t sz = 0;
329 struct FlowedState fst = { 0 };
330
331 /* respect DelSp of RFC3676 only with f=f parts */
332 char *t = mutt_param_get(&b_email->parameter, "delsp");
333 if (t)
334 {
335 delsp = mutt_istr_equal(t, "yes");
336 t = NULL;
337 fst.delsp = true;
338 }
339
340 mutt_debug(LL_DEBUG3, "f=f: DelSp: %s\n", delsp ? "yes" : "no");
341
342 while ((buf = mutt_file_read_line(buf, &sz, state->fp_in, NULL, MUTT_RL_NO_FLAGS)))
343 {
344 const size_t buflen = mutt_str_len(buf);
345 const unsigned int newql = get_quote_level(buf);
346
347 /* end flowed paragraph (if we're within one) if quoting level
348 * changes (should not but can happen, see RFC3676, sec. 4.5.) */
349 if (newql != quotelevel)
350 flush_par(state, &fst);
351
352 quotelevel = newql;
353 int buf_off = newql;
354
355 /* respect sender's space-stuffing by removing one leading space */
356 if (buf[buf_off] == ' ')
357 buf_off++;
358
359 /* test for signature separator */
360 const unsigned int sigsep = mutt_str_equal(buf + buf_off, "-- ");
361
362 /* a fixed line either has no trailing space or is the
363 * signature separator */
364 const bool fixed = (buflen == buf_off) || (buf[buflen - 1] != ' ') || sigsep;
365
366 /* print fixed-and-standalone, fixed-and-empty and sigsep lines as
367 * fixed lines */
368 if ((fixed && ((fst.width == 0) || (buflen == 0))) || sigsep)
369 {
370 /* if we're within a flowed paragraph, terminate it */
371 flush_par(state, &fst);
372 print_fixed_line(buf + buf_off, state, quotelevel, &fst);
373 continue;
374 }
375
376 /* for DelSp=yes, we need to strip one SP prior to CRLF on flowed lines */
377 if (delsp && !fixed)
378 buf[buflen - 1] = '\0';
379
380 print_flowed_line(buf + buf_off, state, quotelevel, &fst, fixed);
381 }
382
383 flush_par(state, &fst);
384
385 FREE(&buf);
386 return 0;
387}
388
395{
396 if (b && (b->type == TYPE_TEXT) && mutt_istr_equal("plain", b->subtype))
397 {
398 const char *format = mutt_param_get(&b->parameter, "format");
399 if (mutt_istr_equal("flowed", format))
400 return true;
401 }
402
403 return false;
404}
405
419static void rfc3676_space_stuff(const char *filename, bool unstuff)
420{
421 FILE *fp_out = NULL;
422 char *buf = NULL;
423 size_t blen = 0;
424
425 struct Buffer *tmpfile = buf_pool_get();
426
427 FILE *fp_in = mutt_file_fopen(filename, "r");
428 if (!fp_in)
429 goto bail;
430
431 buf_mktemp(tmpfile);
432 fp_out = mutt_file_fopen(buf_string(tmpfile), "w+");
433 if (!fp_out)
434 goto bail;
435
436 while ((buf = mutt_file_read_line(buf, &blen, fp_in, NULL, MUTT_RL_NO_FLAGS)) != NULL)
437 {
438 if (unstuff)
439 {
440 if (buf[0] == ' ')
441 fputs(buf + 1, fp_out);
442 else
443 fputs(buf, fp_out);
444 }
445 else
446 {
447 if ((buf[0] == ' ') || mutt_str_startswith(buf, "From "))
448 fputc(' ', fp_out);
449 fputs(buf, fp_out);
450 }
451 fputc('\n', fp_out);
452 }
453 FREE(&buf);
454 mutt_file_fclose(&fp_in);
455 mutt_file_fclose(&fp_out);
456 mutt_file_set_mtime(filename, buf_string(tmpfile));
457
458 fp_in = mutt_file_fopen(buf_string(tmpfile), "r");
459 if (!fp_in)
460 goto bail;
461
462 if ((truncate(filename, 0) == -1) || ((fp_out = mutt_file_fopen(filename, "a")) == NULL))
463 {
464 mutt_perror("%s", filename);
465 goto bail;
466 }
467
468 mutt_file_copy_stream(fp_in, fp_out);
469 mutt_file_set_mtime(buf_string(tmpfile), filename);
470 unlink(buf_string(tmpfile));
471
472bail:
473 mutt_file_fclose(&fp_in);
474 mutt_file_fclose(&fp_out);
475 buf_pool_release(&tmpfile);
476}
477
487{
488 if (!e || !e->body || !e->body->filename)
489 return;
490
493}
494
500{
501 if (!e || !e->body || !e->body->filename)
502 return;
503
506}
507
518void mutt_rfc3676_space_unstuff_attachment(struct Body *b, const char *filename)
519{
520 if (!filename)
521 return;
522
524 return;
525
526 rfc3676_space_stuff(filename, true);
527}
528
539void mutt_rfc3676_space_stuff_attachment(struct Body *b, const char *filename)
540{
541 if (!filename)
542 return;
543
545 return;
546
547 rfc3676_space_stuff(filename, false);
548}
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:143
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:47
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
size_t mutt_strwidth(const char *s)
Measure a string's width in screen cells.
Definition: curs_lib.c:443
Structs that make up an email.
int mutt_file_copy_stream(FILE *fp_in, FILE *fp_out)
Copy the contents of one file into another.
Definition: file.c:287
char * mutt_file_read_line(char *line, size_t *size, FILE *fp, int *line_num, ReadLineFlags flags)
Read a line from a file.
Definition: file.c:808
void mutt_file_set_mtime(const char *from, const char *to)
Set the modification time of one file from another.
Definition: file.c:1068
#define mutt_file_fclose(FP)
Definition: file.h:138
#define mutt_file_fopen(PATH, MODE)
Definition: file.h:137
#define MUTT_RL_NO_FLAGS
No flags are set.
Definition: file.h:40
int rfc3676_handler(struct Body *b_email, struct State *state)
Handler for format=flowed - Implements handler_t -.
Definition: rfc3676.c:323
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
#define mutt_perror(...)
Definition: logging2.h:93
Convenience wrapper for the gui headers.
@ LL_DEBUG3
Log at debug level 3.
Definition: logging2.h:45
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
#define FREE(x)
Definition: memory.h:45
@ TYPE_TEXT
Type: 'text/*'.
Definition: mime.h:38
Convenience wrapper for the library headers.
#define state_puts(STATE, STR)
Definition: state.h:58
#define STATE_DISPLAY
Output is displayed to the user.
Definition: state.h:33
#define state_putc(STATE, STR)
Definition: state.h:59
#define STATE_REPLYING
Are we replying?
Definition: state.h:39
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:672
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:230
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
char * mutt_str_sep(char **stringp, const char *delim)
Find first occurrence of any of delim characters in *stringp.
Definition: string.c:186
int mutt_window_wrap_cols(int width, short wrap)
Calculate the wrap column for a given screen width.
Definition: mutt_window.c:371
char * mutt_param_get(const struct ParameterList *pl, const char *s)
Find a matching Parameter.
Definition: parameter.c:85
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
static void print_fixed_line(const char *line, struct State *state, int ql, struct FlowedState *fst)
Print a fixed format line.
Definition: rfc3676.c:307
static void rfc3676_space_stuff(const char *filename, bool unstuff)
Perform required RFC3676 space stuffing.
Definition: rfc3676.c:419
static size_t print_indent(int ql, struct State *state, int add_suffix)
Print indented text.
Definition: rfc3676.c:136
#define FLOWED_MAX
Definition: rfc3676.c:46
void mutt_rfc3676_space_unstuff(struct Email *e)
Remove RFC3676 space stuffing.
Definition: rfc3676.c:499
static bool add_quote_suffix(struct State *state, int ql)
Should we add a trailing space to quotes.
Definition: rfc3676.c:110
static int quote_width(struct State *state, int ql)
Calculate the paragraph width based upon the quote level.
Definition: rfc3676.c:194
void mutt_rfc3676_space_stuff_attachment(struct Body *b, const char *filename)
Stuff attachments.
Definition: rfc3676.c:539
static int space_quotes(struct State *state)
Should we add spaces between quote levels.
Definition: rfc3676.c:87
static int get_quote_level(const char *line)
Get the quote level of a line.
Definition: rfc3676.c:63
void mutt_rfc3676_space_unstuff_attachment(struct Body *b, const char *filename)
Unstuff attachments.
Definition: rfc3676.c:518
static void print_flowed_line(char *line, struct State *state, int ql, struct FlowedState *fst, bool term)
Print a format-flowed line.
Definition: rfc3676.c:228
static void flush_par(struct State *state, struct FlowedState *fst)
Write out the paragraph.
Definition: rfc3676.c:175
bool mutt_rfc3676_is_format_flowed(struct Body *b)
Is the Email "format-flowed"?
Definition: rfc3676.c:394
void mutt_rfc3676_space_stuff(struct Email *e)
Perform RFC3676 space stuffing on an Email.
Definition: rfc3676.c:486
RFC3676 Format Flowed routines.
The body of an email.
Definition: body.h:36
struct ParameterList parameter
Parameters of the content-type.
Definition: body.h:63
char * subtype
content-type subtype
Definition: body.h:61
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
The envelope/body of an email.
Definition: email.h:39
struct Body * body
List of MIME parts.
Definition: email.h:69
State of a Format-Flowed line of text.
Definition: rfc3676.c:52
bool delsp
Definition: rfc3676.c:55
size_t width
Definition: rfc3676.c:53
size_t spaces
Definition: rfc3676.c:54
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
Keep track when processing files.
Definition: state.h:48
int wraplen
Width to wrap lines to (when flags & STATE_DISPLAY)
Definition: state.h:53
StateFlags flags
Flags, e.g. STATE_DISPLAY.
Definition: state.h:52
FILE * fp_in
File to read from.
Definition: state.h:49
const char * prefix
String to add to the beginning of each output line.
Definition: state.h:51
#define buf_mktemp(buf)
Definition: tmp.h:33