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>
29 #include <lib-ui/curses.h>
36 #include "lib/debug.h"
40 static int get_quote_level (char *line)
44 for (quoted = 0; line[quoted] == '>'; quoted++);
48 static void print_flowed_line (char *line, STATE * s, int ql) {
51 int len = m_strlen(line);
54 if (MaxLineLength > 0) {
55 width = MaxLineLength - WrapMargin - ql - 1;
56 if (!(s->flags & M_REPLYING) && option (OPTSTUFFQUOTED))
59 width = MaxLineLength;
62 if (option (OPTMBOXPANE))
63 width = COLS - SidebarWidth - WrapMargin - ql - 1;
65 width = COLS - WrapMargin - ql - 1;
67 if (!(s->flags & M_REPLYING) && option (OPTSTUFFQUOTED))
73 if (m_strlen(line) == 0) {
74 if (!(s->flags & M_REPLYING) || option (OPTQUOTEEMPTY)) {
76 state_puts(s->prefix,s);
77 for (i=0;i<ql;++i) state_putc('>',s);
78 if (!(s->flags & M_REPLYING) && option(OPTSTUFFQUOTED))
88 for (; oldpos < line + len; pos += width) {
89 /* only search a new position when we're not over
90 * the end of the string w/ pos */
91 if (pos < line + len) {
93 debug_print (4, ("f=f: found space directly at width\n"));
99 debug_print (4, ("f=f: need to search for space\n"));
101 while (pos >= oldpos && *pos != ' ') {
105 debug_print (4, ("f=f: no space found while searching "
106 "to left; going right\n"));
108 while (pos < line + len && *pos && *pos != ' ') {
111 debug_print (4, ("f=f: found space at pos %d\n", pos-line));
113 debug_print (4, ("f=f: found space while searching to left\n"));
120 debug_print (4, ("f=f: line completely fits on screen\n"));
123 state_puts (s->prefix, s);
125 for (i = 0; i < ql; ++i)
127 if (!(s->flags & M_REPLYING) && option (OPTSTUFFQUOTED) &&
128 (ql > 0 || s->prefix))
130 state_puts (oldpos, s);
131 /* fprintf(stderr,"print_flowed_line: `%s'\n",oldpos); */
132 if (pos < line + len)
134 state_putc ('\n', s);
139 int rfc3676_handler (BODY * a, STATE * s) {
140 int bytes = a->length;
141 char buf[LONG_STRING];
142 char *curline = p_new(char, 1);
144 unsigned int curline_len = 1,
145 quotelevel = 0, newql = 0;
146 int buf_off, buf_len;
147 int delsp = 0, fixed = 0;
151 /* respect DelSP of RfC3676 only with f=f parts */
152 if ((t = (char*) mutt_get_parameter ("delsp", a->parameter))) {
153 delsp = m_strlen(t) == 3 && ascii_strncasecmp (t, "yes", 3) == 0;
157 debug_print (2, ("f=f: DelSp: %s\n", delsp ? "yes" : "no"));
159 while (bytes > 0 && fgets (buf, sizeof (buf), s->fpin)) {
161 buf_len = m_strlen(buf);
164 newql = get_quote_level (buf);
166 /* a change of quoting level in a paragraph - shouldn't happen,
167 * but has to be handled - see RFC 3676, sec. 4.5.
169 if (newql != quotelevel && curline && *curline) {
170 print_flowed_line (curline, s, quotelevel);
176 /* XXX - If a line is longer than buf (shouldn't happen), it is split.
177 * This will almost always cause an unintended line break, and
178 * possibly a change in quoting level. But that's better than not
179 * displaying it at all.
181 if ((t = strrchr (buf, '\r')) || (t = strrchr (buf, '\n'))) {
187 /* respect space-stuffing */
188 if (buf[buf_off] == ' ')
191 /* for DelSp=yes, we need to strip one SP prior to CRLF
192 * which may make the line look like fixed although it wasn't
193 * so keep this in mind for later processing */
194 fixed = buf_len == 0 || buf[buf_len - 1] != ' ' ||
195 (strcmp(buf + buf_off, "-- ") == 0);
197 if (delsp && buf_len >= 1 && buf[buf_len-1] == ' ')
198 buf[--buf_len] = '\0';
200 /* we're here when last space removed 'cause of DelSp was
201 * the last space and there isn't more -> done */
202 if ((buf_len - buf_off) < 0) {
203 print_flowed_line (curline, s, quotelevel);
209 /* signature separator also flushes the previous paragraph */
210 if (strcmp(buf + buf_off, "-- ") == 0 && curline && *curline) {
211 print_flowed_line (curline, s, quotelevel);
216 p_realloc(&curline, curline_len + buf_len - buf_off);
217 strcpy (curline + curline_len - 1, buf + buf_off);
218 curline_len += buf_len - buf_off;
220 /* if this was a fixed line the paragraph is finished */
222 print_flowed_line (curline, s, quotelevel);
232 void rfc3676_space_stuff (HEADER* hdr) {
236 unsigned char c = '\0';
238 FILE* in = NULL, *out = NULL;
239 char buf[LONG_STRING];
240 char tmpfile[_POSIX_PATH_MAX];
242 if (!hdr || !hdr->content || !hdr->content->filename)
245 debug_print (2, ("f=f: postprocess %s\n", hdr->content->filename));
246 if ((in = safe_fopen (hdr->content->filename, "r")) == NULL)
248 mutt_mktemp (tmpfile);
249 if ((out = safe_fopen (tmpfile, "w+")) == NULL) {
254 while (fgets (buf, sizeof (buf), in)) {
255 if (ascii_strncmp ("From ", buf, 4) == 0 || buf[0] == ' ') {
264 debug_print (4, ("f=f: line %d needs space-stuffing: '%s'\n",
274 mutt_set_mtime (hdr->content->filename, tmpfile);
275 unlink (hdr->content->filename);
276 m_strreplace(&hdr->content->filename, tmpfile);