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.
24 #include "mutt_curses.h"
33 #include "lib/debug.h"
37 static int get_quote_level (char *line)
41 for (quoted = 0; line[quoted] == '>'; quoted++);
45 static void print_flowed_line (char *line, STATE * s,
47 int* spaces, int space_len) {
50 int len = str_len (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 (str_len (line) == 0) {
73 if (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))
130 if (delsp && spaces && space_len > 0) {
131 /* here, we need to character-wise step through the line
132 * to eliminate all spaces which were trailing due to DelSp */
133 for (i = 0; i < str_len (oldpos); i++) {
134 if (oldpos[i] == ' ' && spaces[&(oldpos[i])-line] != 0) {
135 debug_print (4, ("f=f: DelSp: spaces[%d] forces space removal\n",
139 /* print space at oldpos[i] if it was non-trailing */
140 state_putc (oldpos[i], s);
143 /* for no DelSp, just do whole line as per usual */
144 state_puts (oldpos, s);
145 /* fprintf(stderr,"print_flowed_line: `%s'\n",oldpos); */
146 if (pos < line + len)
148 state_putc ('\n', s);
153 int rfc3676_handler (BODY * a, STATE * s) {
154 int bytes = a->length;
155 char buf[LONG_STRING];
156 char *curline = str_dup ("");
158 unsigned int curline_len = 1, space_len = 1,
159 quotelevel = 0, newql = 0;
160 int buf_off, buf_len;
164 /* respect DelSP of RfC3676 only with f=f parts */
165 if ((t = (char*) mutt_get_parameter ("delsp", a->parameter))) {
166 delsp = str_len (t) == 3 && ascii_strncasecmp (t, "yes", 3) == 0;
170 debug_print (2, ("f=f: DelSp: %s\n", delsp ? "yes" : "no"));
172 while (bytes > 0 && fgets (buf, sizeof (buf), s->fpin)) {
173 buf_len = str_len (buf);
176 newql = get_quote_level (buf);
178 /* a change of quoting level in a paragraph - shouldn't happen,
179 * but has to be handled - see RFC 3676, sec. 4.5.
181 if (newql != quotelevel && curline && *curline) {
182 print_flowed_line (curline, s, quotelevel, delsp, spaces, space_len);
189 /* XXX - If a line is longer than buf (shouldn't happen), it is split.
190 * This will almost always cause an unintended line break, and
191 * possibly a change in quoting level. But that's better than not
192 * displaying it at all.
194 if ((t = strrchr (buf, '\n')) || (t = strrchr (buf, '\r'))) {
199 /* respect space-stuffing */
200 if (buf[buf_off] == ' ')
203 /* signature separator also flushes the previous paragraph */
204 if (strcmp(buf + buf_off, "-- ") == 0 && curline && *curline) {
205 print_flowed_line (curline, s, quotelevel, delsp, spaces, space_len);
211 mem_realloc (&curline, curline_len + buf_len - buf_off);
212 mem_realloc (&spaces, (curline_len + buf_len - buf_off)*sizeof (int));
213 strcpy (curline + curline_len - 1, buf + buf_off);
214 memset (&spaces[space_len], 0, (buf_len - buf_off)*sizeof (int));
215 curline_len += buf_len - buf_off;
216 space_len += buf_len - buf_off;
218 /* if this was a fixed line the paragraph is finished */
219 if (buf_len == 0 || buf[buf_len - 1] != ' ' || strcmp(buf + buf_off, "-- ") == 0) {
220 print_flowed_line (curline, s, quotelevel, delsp, spaces, space_len);
225 /* if last line we appended had a space and we have DelSp=yes,
226 * get a 1 into spaces array at proper position so that
227 * print_flowed_line() can handle it; don't kill the space
228 * right here 'cause we maybe need soft linebreaks to search for break
230 if (delsp && curline && *curline && curline_len-2 >= 0 &&
231 curline[curline_len-2] == ' ') {
232 debug_print (4, ("f=f: DelSp: marking spaces[%d] for later removal\n",
234 spaces[curline_len-2] = 1;
244 void rfc3676_quote_line (STATE* s, char* dst, size_t dstlen,
246 char quote[SHORT_STRING];
247 int offset = 0, i = 0, count = 0;
248 regmatch_t pmatch[1];
252 while (regexec ((regex_t *) QuoteRegexp.rx, &line[offset],
254 offset += pmatch->rm_eo;
257 /* first count number of real quoting characters;
259 * this maybe just plain wrong, but leaving spaces
260 * within quoting characters is what I consider
261 * more plain wrong...
263 for (i = 0; i < offset; i++)
266 /* just make sure we're inside quote althoug we
267 * likely won't have more than SHORT_STRING quote levels... */
268 i = (count > SHORT_STRING-1) ? SHORT_STRING-1 : count;
269 memset (quote, '>', i);
272 debug_print (4, ("f=f: quotelevel = %d, new prefix = '%s'\n",
274 snprintf (dst, dstlen, "%s%s%s", NONULL (s->prefix), NONULL (quote),
278 void rfc3676_space_stuff (HEADER* hdr) {
282 unsigned char c = '\0';
284 FILE* in = NULL, *out = NULL;
285 char buf[LONG_STRING];
286 char tmpfile[_POSIX_PATH_MAX];
288 if (!hdr || !hdr->content || !hdr->content->filename)
291 debug_print (2, ("f=f: postprocess %s\n", hdr->content->filename));
292 if ((in = safe_fopen (hdr->content->filename, "r")) == NULL)
294 mutt_mktemp (tmpfile);
295 if ((out = safe_fopen (tmpfile, "w+")) == NULL) {
300 while (fgets (buf, sizeof (buf), in)) {
301 if (ascii_strncmp ("From ", buf, 4) == 0 || buf[0] == ' ') {
310 debug_print (4, ("f=f: line %d needs space-stuffing: '%s'\n",
319 unlink (hdr->content->filename);
321 str_replace (&hdr->content->filename, tmpfile);