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/lib-lib.h>
25 #include <lib-ui/curses.h>
32 static int get_quote_level (char *line)
36 for (quoted = 0; line[quoted] == '>'; quoted++);
40 static void print_flowed_line (char *line, STATE * s, int ql) {
43 int len = m_strlen(line);
46 if (MaxLineLength > 0) {
47 width = MaxLineLength - WrapMargin - ql - 1;
48 if (!(s->flags & M_REPLYING) && option (OPTSTUFFQUOTED))
51 width = MaxLineLength;
54 if (option (OPTMBOXPANE))
55 width = COLS - SidebarWidth - WrapMargin - ql - 1;
57 width = COLS - WrapMargin - ql - 1;
59 if (!(s->flags & M_REPLYING) && option (OPTSTUFFQUOTED))
65 if (m_strlen(line) == 0) {
66 if (!(s->flags & M_REPLYING) || option (OPTQUOTEEMPTY)) {
68 state_puts(s->prefix,s);
69 for (i=0;i<ql;++i) state_putc('>',s);
70 if (!(s->flags & M_REPLYING) && option(OPTSTUFFQUOTED))
80 for (; oldpos < line + len; pos += width) {
81 /* only search a new position when we're not over
82 * the end of the string w/ pos */
83 if (pos < line + len) {
91 while (pos >= oldpos && *pos != ' ') {
96 while (pos < line + len && *pos && *pos != ' ') {
106 state_puts (s->prefix, s);
108 for (i = 0; i < ql; ++i)
110 if (!(s->flags & M_REPLYING) && option (OPTSTUFFQUOTED) &&
111 (ql > 0 || s->prefix))
113 state_puts (oldpos, s);
114 /* fprintf(stderr,"print_flowed_line: `%s'\n",oldpos); */
115 if (pos < line + len)
117 state_putc ('\n', s);
122 int rfc3676_handler (BODY * a, STATE * s) {
123 int bytes = a->length;
124 char buf[LONG_STRING];
125 char *curline = p_new(char, 1);
127 unsigned int curline_len = 1,
128 quotelevel = 0, newql = 0;
129 int buf_off, buf_len;
130 int delsp = 0, fixed = 0;
134 /* respect DelSP of RfC3676 only with f=f parts */
135 if ((t = parameter_getval(a->parameter, "delsp"))) {
136 delsp = m_strlen(t) == 3 && ascii_strncasecmp (t, "yes", 3) == 0;
141 while (bytes > 0 && fgets (buf, sizeof (buf), s->fpin)) {
143 buf_len = m_strlen(buf);
146 newql = get_quote_level (buf);
148 /* a change of quoting level in a paragraph - shouldn't happen,
149 * but has to be handled - see RFC 3676, sec. 4.5.
151 if (newql != quotelevel && curline && *curline) {
152 print_flowed_line (curline, s, quotelevel);
158 /* XXX - If a line is longer than buf (shouldn't happen), it is split.
159 * This will almost always cause an unintended line break, and
160 * possibly a change in quoting level. But that's better than not
161 * displaying it at all.
163 if ((t = strrchr (buf, '\r')) || (t = strrchr (buf, '\n'))) {
169 /* respect space-stuffing */
170 if (buf[buf_off] == ' ')
173 /* for DelSp=yes, we need to strip one SP prior to CRLF
174 * which may make the line look like fixed although it wasn't
175 * so keep this in mind for later processing */
176 fixed = buf_len == 0 || buf[buf_len - 1] != ' ' ||
177 (strcmp(buf + buf_off, "-- ") == 0);
179 if (delsp && buf_len >= 1 && buf[buf_len-1] == ' ')
180 buf[--buf_len] = '\0';
182 /* we're here when last space removed 'cause of DelSp was
183 * the last space and there isn't more -> done */
184 if ((buf_len - buf_off) < 0) {
185 print_flowed_line (curline, s, quotelevel);
191 /* signature separator also flushes the previous paragraph */
192 if (strcmp(buf + buf_off, "-- ") == 0 && curline && *curline) {
193 print_flowed_line (curline, s, quotelevel);
198 p_realloc(&curline, curline_len + buf_len - buf_off);
199 strcpy (curline + curline_len - 1, buf + buf_off);
200 curline_len += buf_len - buf_off;
202 /* if this was a fixed line the paragraph is finished */
204 print_flowed_line (curline, s, quotelevel);
214 void rfc3676_space_stuff (HEADER* hdr) {
215 FILE* in = NULL, *out = NULL;
216 char buf[LONG_STRING];
217 char tmpf[_POSIX_PATH_MAX];
219 if (!hdr || !hdr->content || !hdr->content->filename)
222 if ((in = safe_fopen (hdr->content->filename, "r")) == NULL)
225 if ((out = safe_fopen (tmpf, "w+")) == NULL) {
230 while (fgets (buf, sizeof (buf), in)) {
231 if (ascii_strncmp ("From ", buf, 4) == 0 || buf[0] == ' ') {
238 mutt_set_mtime (hdr->content->filename, tmpf);
239 unlink (hdr->content->filename);
240 m_strreplace(&hdr->content->filename, tmpf);