2 * Copyright (C) 2005 Andreas Krennmair <ak@synflood.at>
3 * Copyright (C) 2005 Peter J. Holzer <hjp@hjp.net>
4 * Copyright (C) 2005-9 Rocco Rutte <pdmef@gmx.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 /* This file was originally part of mutt-ng */
36 #include "mutt_curses.h"
42 typedef struct flowed_state
48 static int get_quote_level (const char *line)
51 char *p = (char *) line;
53 while (p && *p == '>')
62 static size_t print_indent (int ql, STATE *s, int sp)
69 /* use given prefix only for format=fixed replies to format=flowed,
70 * for format=flowed replies to format=flowed, use '>' indentation
72 if (option (OPTTEXTFLOWED))
76 state_puts (s->prefix, s);
77 wid = mutt_strwidth (s->prefix);
81 for (i = 0; i < ql; i++)
88 static void flush_par (STATE *s, flowed_state_t *fst)
98 static int quote_width (STATE *s, int ql)
100 size_t width = (Wrap ? mutt_term_width (Wrap) : FLOWED_MAX) - 1;
101 if (s->flags & M_REPLYING && width > FLOWED_MAX)
108 static void print_flowed_line (char *line, STATE *s, int ql,
109 flowed_state_t *fst, int term)
111 size_t width, w, words = 0;
116 /* flush current paragraph (if any) first */
118 print_indent (ql, s, 0);
119 state_putc ('\n', s);
123 width = quote_width (s, ql);
125 dprint (4, (debugfile, "f=f: line [%s], width = %ld, spaces = %d\n",
126 NONULL(line), (long)width, fst->spaces));
128 for (p = (char *)line, words = 0; (p = strsep (&line, " ")) != NULL ; )
130 dprint(4,(debugfile,"f=f: word [%s], width: %d, remaining = [%s]\n",
131 p, fst->width, line));
133 /* remember number of spaces */
136 dprint(4,(debugfile,"f=f: additional space\n"));
140 /* there's exactly one space prior to every but the first word */
144 w = mutt_strwidth (p);
145 /* see if we need to break the line but make sure the first
146 word is put on the line regardless */
147 if (w < width && w + fst->width + fst->spaces > width)
149 dprint(4,(debugfile,"f=f: break line at %d, %d spaces left\n",
150 fst->width, fst->spaces));
151 /* only honor trailing spaces for format=flowed replies */
152 if (option(OPTTEXTFLOWED))
153 for ( ; fst->spaces; fst->spaces--)
155 state_putc ('\n', s);
161 if (!words && !fst->width)
162 fst->width = print_indent (ql, s, !(s->flags & M_REPLYING) &&
163 (ql > 0 || s->prefix));
164 fst->width += w + fst->spaces;
165 for ( ; fst->spaces; fst->spaces--)
175 static void print_fixed_line (const char *line, STATE *s, int ql,
178 print_indent (ql, s, !(s->flags & M_REPLYING) && (ql > 0 || s->prefix));
180 state_puts (line, s);
181 state_putc ('\n', s);
187 int rfc3676_handler (BODY * a, STATE * s)
189 int bytes = a->length;
190 char buf[LONG_STRING];
192 unsigned int quotelevel = 0, newql = 0, sigsep = 0;
193 int buf_off = 0, buf_len;
194 int delsp = 0, fixed = 0;
197 memset (&fst, 0, sizeof (fst));
199 /* respect DelSp of RfC3676 only with f=f parts */
200 if ((t = (char *) mutt_get_parameter ("delsp", a->parameter)))
202 delsp = mutt_strlen (t) == 3 && ascii_strncasecmp (t, "yes", 3) == 0;
206 dprint (4, (debugfile, "f=f: DelSp: %s\n", delsp ? "yes" : "no"));
208 while (bytes > 0 && fgets (buf, sizeof (buf), s->fpin))
211 buf_len = mutt_strlen (buf);
214 newql = get_quote_level (buf);
216 /* end flowed paragraph (if we're within one) if quoting level
217 * changes (should not but can happen, see RFC 3676, sec. 4.5.)
219 if (newql != quotelevel)
224 /* XXX - If a line is longer than buf (shouldn't happen), it is split.
225 * This will almost always cause an unintended line break, and
226 * possibly a change in quoting level. But that's better than not
227 * displaying it at all.
229 if ((t = strrchr (buf, '\r')) || (t = strrchr (buf, '\n')))
237 /* respect sender's space-stuffing by removing one leading space */
238 if (buf[buf_off] == ' ')
241 /* test for signature separator */
242 sigsep = ascii_strcmp (buf + buf_off, "-- ") == 0;
244 /* a fixed line either has no trailing space or is the
245 * signature separator */
246 fixed = buf_len == buf_off || buf[buf_len - 1] != ' ' || sigsep;
248 /* print fixed-and-standalone, fixed-and-empty and sigsep lines as
250 if ((fixed && (!fst.width || !buf_len)) || sigsep)
252 /* if we're within a flowed paragraph, terminate it */
254 print_fixed_line (buf + buf_off, s, quotelevel, &fst);
258 /* for DelSp=yes, we need to strip one SP prior to CRLF on flowed lines */
260 buf[--buf_len] = '\0';
262 print_flowed_line (buf + buf_off, s, quotelevel, &fst, fixed);
271 * This routine does RfC3676 space stuffing since it's a MUST.
272 * Space stuffing means that we have to add leading spaces to
274 * - lines starting with a space
275 * - lines starting with 'From '
276 * This routine is only called once right after editing the
277 * initial message so it's up to the user to take care of stuffing
278 * when editing the message several times before actually sending it
280 * This is more or less a hack as it replaces the message's content with
281 * a freshly created copy in a tempfile and modifies the file's mtime
282 * so we don't trigger code paths watching for mtime changes
284 void rfc3676_space_stuff (HEADER* hdr)
289 unsigned char c = '\0';
291 FILE *in = NULL, *out = NULL;
292 char buf[LONG_STRING];
293 char tmpfile[_POSIX_PATH_MAX];
295 if (!hdr || !hdr->content || !hdr->content->filename)
298 dprint (2, (debugfile, "f=f: postprocess %s\n", hdr->content->filename));
300 if ((in = safe_fopen (hdr->content->filename, "r")) == NULL)
303 mutt_mktemp (tmpfile);
304 if ((out = safe_fopen (tmpfile, "w+")) == NULL)
310 while (fgets (buf, sizeof (buf), in))
312 if (ascii_strncmp ("From ", buf, 5) == 0 || buf[0] == ' ') {
316 len = mutt_strlen (buf);
322 dprint (4, (debugfile, "f=f: line %d needs space-stuffing: '%s'\n",
332 mutt_set_mtime (hdr->content->filename, tmpfile);
333 unlink (hdr->content->filename);
334 mutt_str_replace (&hdr->content->filename, tmpfile);