2 * Parts were written/modified by:
3 * Andreas Krennmair <ak@synflood.at>
4 * Peter J. Holzer <hjp@hjp.net>
5 * Rocco Rutte <pdmef@cs.tu-berlin.de>
7 * This file is part of mutt-ng, see http://www.muttng.org/.
8 * It's licensed under the GNU General Public License,
9 * please see the file GPL in the top level source directory.
23 #include <lib-lib/mem.h>
24 #include <lib-lib/macros.h>
27 #include "mutt_curses.h"
34 #include "lib/debug.h"
38 static int get_quote_level (char *line)
42 for (quoted = 0; line[quoted] == '>'; quoted++);
46 static void print_flowed_line (char *line, STATE * s, int ql) {
49 int len = str_len (line);
52 if (MaxLineLength > 0) {
53 width = MaxLineLength - WrapMargin - ql - 1;
54 if (!(s->flags & M_REPLYING) && option (OPTSTUFFQUOTED))
57 width = MaxLineLength;
60 if (option (OPTMBOXPANE))
61 width = COLS - SidebarWidth - WrapMargin - ql - 1;
63 width = COLS - WrapMargin - ql - 1;
65 if (!(s->flags & M_REPLYING) && option (OPTSTUFFQUOTED))
71 if (str_len (line) == 0) {
72 if (!(s->flags & M_REPLYING) || option (OPTQUOTEEMPTY)) {
74 state_puts(s->prefix,s);
75 for (i=0;i<ql;++i) state_putc('>',s);
76 if (!(s->flags & M_REPLYING) && option(OPTSTUFFQUOTED))
86 for (; oldpos < line + len; pos += width) {
87 /* only search a new position when we're not over
88 * the end of the string w/ pos */
89 if (pos < line + len) {
91 debug_print (4, ("f=f: found space directly at width\n"));
97 debug_print (4, ("f=f: need to search for space\n"));
99 while (pos >= oldpos && *pos != ' ') {
103 debug_print (4, ("f=f: no space found while searching "
104 "to left; going right\n"));
106 while (pos < line + len && *pos && *pos != ' ') {
109 debug_print (4, ("f=f: found space at pos %d\n", pos-line));
111 debug_print (4, ("f=f: found space while searching to left\n"));
118 debug_print (4, ("f=f: line completely fits on screen\n"));
121 state_puts (s->prefix, s);
123 for (i = 0; i < ql; ++i)
125 if (!(s->flags & M_REPLYING) && option (OPTSTUFFQUOTED) &&
126 (ql > 0 || s->prefix))
128 state_puts (oldpos, s);
129 /* fprintf(stderr,"print_flowed_line: `%s'\n",oldpos); */
130 if (pos < line + len)
132 state_putc ('\n', s);
137 int rfc3676_handler (BODY * a, STATE * s) {
138 int bytes = a->length;
139 char buf[LONG_STRING];
140 char *curline = p_new(char, 1);
142 unsigned int curline_len = 1,
143 quotelevel = 0, newql = 0;
144 int buf_off, buf_len;
145 int delsp = 0, fixed = 0;
149 /* respect DelSP of RfC3676 only with f=f parts */
150 if ((t = (char*) mutt_get_parameter ("delsp", a->parameter))) {
151 delsp = str_len (t) == 3 && ascii_strncasecmp (t, "yes", 3) == 0;
155 debug_print (2, ("f=f: DelSp: %s\n", delsp ? "yes" : "no"));
157 while (bytes > 0 && fgets (buf, sizeof (buf), s->fpin)) {
159 buf_len = str_len (buf);
162 newql = get_quote_level (buf);
164 /* a change of quoting level in a paragraph - shouldn't happen,
165 * but has to be handled - see RFC 3676, sec. 4.5.
167 if (newql != quotelevel && curline && *curline) {
168 print_flowed_line (curline, s, quotelevel);
174 /* XXX - If a line is longer than buf (shouldn't happen), it is split.
175 * This will almost always cause an unintended line break, and
176 * possibly a change in quoting level. But that's better than not
177 * displaying it at all.
179 if ((t = strrchr (buf, '\r')) || (t = strrchr (buf, '\n'))) {
185 /* respect space-stuffing */
186 if (buf[buf_off] == ' ')
189 /* for DelSp=yes, we need to strip one SP prior to CRLF
190 * which may make the line look like fixed although it wasn't
191 * so keep this in mind for later processing */
192 fixed = buf_len == 0 || buf[buf_len - 1] != ' ' ||
193 (strcmp(buf + buf_off, "-- ") == 0);
195 if (delsp && buf_len >= 1 && buf[buf_len-1] == ' ')
196 buf[--buf_len] = '\0';
198 /* we're here when last space removed 'cause of DelSp was
199 * the last space and there isn't more -> done */
200 if ((buf_len - buf_off) < 0) {
201 print_flowed_line (curline, s, quotelevel);
207 /* signature separator also flushes the previous paragraph */
208 if (strcmp(buf + buf_off, "-- ") == 0 && curline && *curline) {
209 print_flowed_line (curline, s, quotelevel);
214 p_realloc(&curline, curline_len + buf_len - buf_off);
215 strcpy (curline + curline_len - 1, buf + buf_off);
216 curline_len += buf_len - buf_off;
218 /* if this was a fixed line the paragraph is finished */
220 print_flowed_line (curline, s, quotelevel);
230 void rfc3676_space_stuff (HEADER* hdr) {
234 unsigned char c = '\0';
236 FILE* in = NULL, *out = NULL;
237 char buf[LONG_STRING];
238 char tmpfile[_POSIX_PATH_MAX];
240 if (!hdr || !hdr->content || !hdr->content->filename)
243 debug_print (2, ("f=f: postprocess %s\n", hdr->content->filename));
244 if ((in = safe_fopen (hdr->content->filename, "r")) == NULL)
246 mutt_mktemp (tmpfile);
247 if ((out = safe_fopen (tmpfile, "w+")) == NULL) {
252 while (fgets (buf, sizeof (buf), in)) {
253 if (ascii_strncmp ("From ", buf, 4) == 0 || buf[0] == ' ') {
262 debug_print (4, ("f=f: line %d needs space-stuffing: '%s'\n",
272 mutt_set_mtime (hdr->content->filename, tmpfile);
273 unlink (hdr->content->filename);
274 str_replace (&hdr->content->filename, tmpfile);