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>
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 = m_strlen(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 (m_strlen(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) {
97 while (pos >= oldpos && *pos != ' ') {
102 while (pos < line + len && *pos && *pos != ' ') {
112 state_puts (s->prefix, s);
114 for (i = 0; i < ql; ++i)
116 if (!(s->flags & M_REPLYING) && option (OPTSTUFFQUOTED) &&
117 (ql > 0 || s->prefix))
119 state_puts (oldpos, s);
120 /* fprintf(stderr,"print_flowed_line: `%s'\n",oldpos); */
121 if (pos < line + len)
123 state_putc ('\n', s);
128 int rfc3676_handler (BODY * a, STATE * s) {
129 int bytes = a->length;
130 char buf[LONG_STRING];
131 char *curline = p_new(char, 1);
133 unsigned int curline_len = 1,
134 quotelevel = 0, newql = 0;
135 int buf_off, buf_len;
136 int delsp = 0, fixed = 0;
140 /* respect DelSP of RfC3676 only with f=f parts */
141 if ((t = parameter_getval(a->parameter, "delsp"))) {
142 delsp = m_strlen(t) == 3 && ascii_strncasecmp (t, "yes", 3) == 0;
147 while (bytes > 0 && fgets (buf, sizeof (buf), s->fpin)) {
149 buf_len = m_strlen(buf);
152 newql = get_quote_level (buf);
154 /* a change of quoting level in a paragraph - shouldn't happen,
155 * but has to be handled - see RFC 3676, sec. 4.5.
157 if (newql != quotelevel && curline && *curline) {
158 print_flowed_line (curline, s, quotelevel);
164 /* XXX - If a line is longer than buf (shouldn't happen), it is split.
165 * This will almost always cause an unintended line break, and
166 * possibly a change in quoting level. But that's better than not
167 * displaying it at all.
169 if ((t = strrchr (buf, '\r')) || (t = strrchr (buf, '\n'))) {
175 /* respect space-stuffing */
176 if (buf[buf_off] == ' ')
179 /* for DelSp=yes, we need to strip one SP prior to CRLF
180 * which may make the line look like fixed although it wasn't
181 * so keep this in mind for later processing */
182 fixed = buf_len == 0 || buf[buf_len - 1] != ' ' ||
183 (strcmp(buf + buf_off, "-- ") == 0);
185 if (delsp && buf_len >= 1 && buf[buf_len-1] == ' ')
186 buf[--buf_len] = '\0';
188 /* we're here when last space removed 'cause of DelSp was
189 * the last space and there isn't more -> done */
190 if ((buf_len - buf_off) < 0) {
191 print_flowed_line (curline, s, quotelevel);
197 /* signature separator also flushes the previous paragraph */
198 if (strcmp(buf + buf_off, "-- ") == 0 && curline && *curline) {
199 print_flowed_line (curline, s, quotelevel);
204 p_realloc(&curline, curline_len + buf_len - buf_off);
205 strcpy (curline + curline_len - 1, buf + buf_off);
206 curline_len += buf_len - buf_off;
208 /* if this was a fixed line the paragraph is finished */
210 print_flowed_line (curline, s, quotelevel);
220 void rfc3676_space_stuff (HEADER* hdr) {
221 FILE* in = NULL, *out = NULL;
222 char buf[LONG_STRING];
223 char tmpf[_POSIX_PATH_MAX];
225 if (!hdr || !hdr->content || !hdr->content->filename)
228 if ((in = safe_fopen (hdr->content->filename, "r")) == NULL)
231 if ((out = safe_fopen (tmpf, "w+")) == NULL) {
236 while (fgets (buf, sizeof (buf), in)) {
237 if (ascii_strncmp ("From ", buf, 4) == 0 || buf[0] == ' ') {
244 mutt_set_mtime (hdr->content->filename, tmpf);
245 unlink (hdr->content->filename);
246 m_strreplace(&hdr->content->filename, tmpf);