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/str.h>
25 #include <lib-lib/ascii.h>
26 #include <lib-lib/macros.h>
27 #include <lib-lib/file.h>
30 #include "mutt_curses.h"
35 #include "lib/debug.h"
39 static int get_quote_level (char *line)
43 for (quoted = 0; line[quoted] == '>'; quoted++);
47 static void print_flowed_line (char *line, STATE * s, int ql) {
50 int len = m_strlen(line);
53 if (MaxLineLength > 0) {
54 width = MaxLineLength - WrapMargin - ql - 1;
55 if (!(s->flags & M_REPLYING) && option (OPTSTUFFQUOTED))
58 width = MaxLineLength;
61 if (option (OPTMBOXPANE))
62 width = COLS - SidebarWidth - WrapMargin - ql - 1;
64 width = COLS - WrapMargin - ql - 1;
66 if (!(s->flags & M_REPLYING) && option (OPTSTUFFQUOTED))
72 if (m_strlen(line) == 0) {
73 if (!(s->flags & M_REPLYING) || option (OPTQUOTEEMPTY)) {
75 state_puts(s->prefix,s);
76 for (i=0;i<ql;++i) state_putc('>',s);
77 if (!(s->flags & M_REPLYING) && option(OPTSTUFFQUOTED))
87 for (; oldpos < line + len; pos += width) {
88 /* only search a new position when we're not over
89 * the end of the string w/ pos */
90 if (pos < line + len) {
92 debug_print (4, ("f=f: found space directly at width\n"));
98 debug_print (4, ("f=f: need to search for space\n"));
100 while (pos >= oldpos && *pos != ' ') {
104 debug_print (4, ("f=f: no space found while searching "
105 "to left; going right\n"));
107 while (pos < line + len && *pos && *pos != ' ') {
110 debug_print (4, ("f=f: found space at pos %d\n", pos-line));
112 debug_print (4, ("f=f: found space while searching to left\n"));
119 debug_print (4, ("f=f: line completely fits on screen\n"));
122 state_puts (s->prefix, s);
124 for (i = 0; i < ql; ++i)
126 if (!(s->flags & M_REPLYING) && option (OPTSTUFFQUOTED) &&
127 (ql > 0 || s->prefix))
129 state_puts (oldpos, s);
130 /* fprintf(stderr,"print_flowed_line: `%s'\n",oldpos); */
131 if (pos < line + len)
133 state_putc ('\n', s);
138 int rfc3676_handler (BODY * a, STATE * s) {
139 int bytes = a->length;
140 char buf[LONG_STRING];
141 char *curline = p_new(char, 1);
143 unsigned int curline_len = 1,
144 quotelevel = 0, newql = 0;
145 int buf_off, buf_len;
146 int delsp = 0, fixed = 0;
150 /* respect DelSP of RfC3676 only with f=f parts */
151 if ((t = (char*) mutt_get_parameter ("delsp", a->parameter))) {
152 delsp = m_strlen(t) == 3 && ascii_strncasecmp (t, "yes", 3) == 0;
156 debug_print (2, ("f=f: DelSp: %s\n", delsp ? "yes" : "no"));
158 while (bytes > 0 && fgets (buf, sizeof (buf), s->fpin)) {
160 buf_len = m_strlen(buf);
163 newql = get_quote_level (buf);
165 /* a change of quoting level in a paragraph - shouldn't happen,
166 * but has to be handled - see RFC 3676, sec. 4.5.
168 if (newql != quotelevel && curline && *curline) {
169 print_flowed_line (curline, s, quotelevel);
175 /* XXX - If a line is longer than buf (shouldn't happen), it is split.
176 * This will almost always cause an unintended line break, and
177 * possibly a change in quoting level. But that's better than not
178 * displaying it at all.
180 if ((t = strrchr (buf, '\r')) || (t = strrchr (buf, '\n'))) {
186 /* respect space-stuffing */
187 if (buf[buf_off] == ' ')
190 /* for DelSp=yes, we need to strip one SP prior to CRLF
191 * which may make the line look like fixed although it wasn't
192 * so keep this in mind for later processing */
193 fixed = buf_len == 0 || buf[buf_len - 1] != ' ' ||
194 (strcmp(buf + buf_off, "-- ") == 0);
196 if (delsp && buf_len >= 1 && buf[buf_len-1] == ' ')
197 buf[--buf_len] = '\0';
199 /* we're here when last space removed 'cause of DelSp was
200 * the last space and there isn't more -> done */
201 if ((buf_len - buf_off) < 0) {
202 print_flowed_line (curline, s, quotelevel);
208 /* signature separator also flushes the previous paragraph */
209 if (strcmp(buf + buf_off, "-- ") == 0 && curline && *curline) {
210 print_flowed_line (curline, s, quotelevel);
215 p_realloc(&curline, curline_len + buf_len - buf_off);
216 strcpy (curline + curline_len - 1, buf + buf_off);
217 curline_len += buf_len - buf_off;
219 /* if this was a fixed line the paragraph is finished */
221 print_flowed_line (curline, s, quotelevel);
231 void rfc3676_space_stuff (HEADER* hdr) {
235 unsigned char c = '\0';
237 FILE* in = NULL, *out = NULL;
238 char buf[LONG_STRING];
239 char tmpfile[_POSIX_PATH_MAX];
241 if (!hdr || !hdr->content || !hdr->content->filename)
244 debug_print (2, ("f=f: postprocess %s\n", hdr->content->filename));
245 if ((in = safe_fopen (hdr->content->filename, "r")) == NULL)
247 mutt_mktemp (tmpfile);
248 if ((out = safe_fopen (tmpfile, "w+")) == NULL) {
253 while (fgets (buf, sizeof (buf), in)) {
254 if (ascii_strncmp ("From ", buf, 4) == 0 || buf[0] == ' ') {
263 debug_print (4, ("f=f: line %d needs space-stuffing: '%s'\n",
273 mutt_set_mtime (hdr->content->filename, tmpfile);
274 unlink (hdr->content->filename);
275 str_replace (&hdr->content->filename, tmpfile);