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>
35 static int get_quote_level (char *line)
39 for (quoted = 0; line[quoted] == '>'; quoted++);
43 static void print_flowed_line (char *line, STATE * s, int ql) {
46 int len = m_strlen(line);
49 if (MaxLineLength > 0) {
50 width = MaxLineLength - WrapMargin - ql - 1;
51 if (!(s->flags & M_REPLYING) && option (OPTSTUFFQUOTED))
54 width = MaxLineLength;
57 if (option (OPTMBOXPANE))
58 width = COLS - SidebarWidth - WrapMargin - ql - 1;
60 width = COLS - WrapMargin - ql - 1;
62 if (!(s->flags & M_REPLYING) && option (OPTSTUFFQUOTED))
68 if (m_strlen(line) == 0) {
69 if (!(s->flags & M_REPLYING) || option (OPTQUOTEEMPTY)) {
71 state_puts(s->prefix,s);
72 for (i=0;i<ql;++i) state_putc('>',s);
73 if (!(s->flags & M_REPLYING) && option(OPTSTUFFQUOTED))
83 for (; oldpos < line + len; pos += width) {
84 /* only search a new position when we're not over
85 * the end of the string w/ pos */
86 if (pos < line + len) {
94 while (pos >= oldpos && *pos != ' ') {
99 while (pos < line + len && *pos && *pos != ' ') {
109 state_puts (s->prefix, s);
111 for (i = 0; i < ql; ++i)
113 if (!(s->flags & M_REPLYING) && option (OPTSTUFFQUOTED) &&
114 (ql > 0 || s->prefix))
116 state_puts (oldpos, s);
117 /* fprintf(stderr,"print_flowed_line: `%s'\n",oldpos); */
118 if (pos < line + len)
120 state_putc ('\n', s);
125 int rfc3676_handler (BODY * a, STATE * s) {
126 int bytes = a->length;
127 char buf[LONG_STRING];
128 char *curline = p_new(char, 1);
130 unsigned int curline_len = 1,
131 quotelevel = 0, newql = 0;
132 int buf_off, buf_len;
133 int delsp = 0, fixed = 0;
137 /* respect DelSP of RfC3676 only with f=f parts */
138 if ((t = parameter_getval(a->parameter, "delsp"))) {
139 delsp = m_strlen(t) == 3 && ascii_strncasecmp (t, "yes", 3) == 0;
144 while (bytes > 0 && fgets (buf, sizeof (buf), s->fpin)) {
146 buf_len = m_strlen(buf);
149 newql = get_quote_level (buf);
151 /* a change of quoting level in a paragraph - shouldn't happen,
152 * but has to be handled - see RFC 3676, sec. 4.5.
154 if (newql != quotelevel && curline && *curline) {
155 print_flowed_line (curline, s, quotelevel);
161 /* XXX - If a line is longer than buf (shouldn't happen), it is split.
162 * This will almost always cause an unintended line break, and
163 * possibly a change in quoting level. But that's better than not
164 * displaying it at all.
166 if ((t = strrchr (buf, '\r')) || (t = strrchr (buf, '\n'))) {
172 /* respect space-stuffing */
173 if (buf[buf_off] == ' ')
176 /* for DelSp=yes, we need to strip one SP prior to CRLF
177 * which may make the line look like fixed although it wasn't
178 * so keep this in mind for later processing */
179 fixed = buf_len == 0 || buf[buf_len - 1] != ' ' ||
180 (strcmp(buf + buf_off, "-- ") == 0);
182 if (delsp && buf_len >= 1 && buf[buf_len-1] == ' ')
183 buf[--buf_len] = '\0';
185 /* we're here when last space removed 'cause of DelSp was
186 * the last space and there isn't more -> done */
187 if ((buf_len - buf_off) < 0) {
188 print_flowed_line (curline, s, quotelevel);
194 /* signature separator also flushes the previous paragraph */
195 if (strcmp(buf + buf_off, "-- ") == 0 && curline && *curline) {
196 print_flowed_line (curline, s, quotelevel);
201 p_realloc(&curline, curline_len + buf_len - buf_off);
202 strcpy (curline + curline_len - 1, buf + buf_off);
203 curline_len += buf_len - buf_off;
205 /* if this was a fixed line the paragraph is finished */
207 print_flowed_line (curline, s, quotelevel);
217 void rfc3676_space_stuff (HEADER* hdr) {
218 FILE* in = NULL, *out = NULL;
219 char buf[LONG_STRING];
220 char tmpf[_POSIX_PATH_MAX];
222 if (!hdr || !hdr->content || !hdr->content->filename)
225 if ((in = safe_fopen (hdr->content->filename, "r")) == NULL)
228 if ((out = safe_fopen (tmpf, "w+")) == NULL) {
233 while (fgets (buf, sizeof (buf), in)) {
234 if (ascii_strncmp ("From ", buf, 4) == 0 || buf[0] == ' ') {
241 mutt_set_mtime (hdr->content->filename, tmpf);
242 unlink (hdr->content->filename);
243 m_strreplace(&hdr->content->filename, tmpf);