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.
12 #include <lib-lib/lib-lib.h>
16 #include <lib-ui/lib-ui.h>
23 static int get_quote_level (char *line)
27 for (quoted = 0; line[quoted] == '>'; quoted++);
31 static void print_flowed_line (char *line, STATE * s, int ql) {
34 int len = m_strlen(line);
37 if (MaxLineLength > 0) {
38 width = MaxLineLength - WrapMargin - ql - 1;
39 if (!(s->flags & M_REPLYING) && option (OPTSTUFFQUOTED))
42 width = MaxLineLength;
45 width = getmaxx(main_w) - WrapMargin - ql - 1;
47 if (!(s->flags & M_REPLYING) && option (OPTSTUFFQUOTED))
50 width = getmaxx(main_w);
53 if (m_strlen(line) == 0) {
54 if (!(s->flags & M_REPLYING) || option (OPTQUOTEEMPTY)) {
56 state_puts(s->prefix,s);
57 for (i=0;i<ql;++i) state_putc('>',s);
58 if (!(s->flags & M_REPLYING) && option(OPTSTUFFQUOTED))
68 for (; oldpos < line + len; pos += width) {
69 /* only search a new position when we're not over
70 * the end of the string w/ pos */
71 if (pos < line + len) {
79 while (pos >= oldpos && *pos != ' ') {
84 while (pos < line + len && *pos && *pos != ' ') {
94 state_puts (s->prefix, s);
96 for (i = 0; i < ql; ++i)
98 if (!(s->flags & M_REPLYING) && option (OPTSTUFFQUOTED) &&
99 (ql > 0 || s->prefix))
101 state_puts (oldpos, s);
102 /* fprintf(stderr,"print_flowed_line: `%s'\n",oldpos); */
103 if (pos < line + len)
105 state_putc ('\n', s);
110 int rfc3676_handler (BODY * a, STATE * s) {
111 int bytes = a->length;
112 char buf[LONG_STRING];
113 char *curline = p_new(char, 1);
115 unsigned int curline_len = 1,
116 quotelevel = 0, newql = 0;
117 int buf_off, buf_len;
118 int delsp = 0, fixed = 0;
122 /* respect DelSP of RfC3676 only with f=f parts */
123 if ((t = parameter_getval(a->parameter, "delsp"))) {
124 delsp = m_strlen(t) == 3 && ascii_strncasecmp (t, "yes", 3) == 0;
129 while (bytes > 0 && fgets (buf, sizeof (buf), s->fpin)) {
131 buf_len = m_strlen(buf);
134 newql = get_quote_level (buf);
136 /* a change of quoting level in a paragraph - shouldn't happen,
137 * but has to be handled - see RFC 3676, sec. 4.5.
139 if (newql != quotelevel && curline && *curline) {
140 print_flowed_line (curline, s, quotelevel);
146 /* XXX - If a line is longer than buf (shouldn't happen), it is split.
147 * This will almost always cause an unintended line break, and
148 * possibly a change in quoting level. But that's better than not
149 * displaying it at all.
151 if ((t = strrchr (buf, '\r')) || (t = strrchr (buf, '\n'))) {
157 /* respect space-stuffing */
158 if (buf[buf_off] == ' ')
161 /* for DelSp=yes, we need to strip one SP prior to CRLF
162 * which may make the line look like fixed although it wasn't
163 * so keep this in mind for later processing */
164 fixed = buf_len == 0 || buf[buf_len - 1] != ' ' ||
165 (m_strcmp(buf + buf_off, "-- ") == 0);
167 if (delsp && buf_len >= 1 && buf[buf_len-1] == ' ')
168 buf[--buf_len] = '\0';
170 /* we're here when last space removed 'cause of DelSp was
171 * the last space and there isn't more -> done */
172 if ((buf_len - buf_off) < 0) {
173 print_flowed_line (curline, s, quotelevel);
179 /* signature separator also flushes the previous paragraph */
180 if (m_strcmp(buf + buf_off, "-- ") == 0 && curline && *curline) {
181 print_flowed_line (curline, s, quotelevel);
186 p_realloc(&curline, curline_len + buf_len - buf_off);
187 m_strcpy(curline + curline_len - 1, buf_len - buf_off + 1, buf + buf_off);
188 curline_len += buf_len - buf_off;
190 /* if this was a fixed line the paragraph is finished */
192 print_flowed_line (curline, s, quotelevel);
202 /* sets mtime of 'to' to mtime of 'from' */
203 static void mutt_set_mtime (const char* from, const char* to) {
207 if (stat (from, &st) != -1) {
208 utim.actime = st.st_mtime;
209 utim.modtime = st.st_mtime;
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 out = m_tempfile(tmpf, sizeof(tmpf), NONULL(mod_core.tmpdir), NULL);
231 while (fgets(buf, sizeof (buf), in)) {
232 if (m_strncmp("From ", buf, 4) == 0 || buf[0] == ' ') {
239 mutt_set_mtime (hdr->content->filename, tmpf);
240 unlink (hdr->content->filename);
241 m_strreplace(&hdr->content->filename, tmpf);