allow quote / reply date & time format to be set using %D{format}.
[claws.git] / src / enriched.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <glib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <ctype.h>
24
25 #include "enriched.h"
26 #include "codeconv.h"
27 #include "utils.h"
28
29 #define ERTFBUFSIZE     8192
30
31
32 static ERTFState ertf_read_line         (ERTFParser     *parser);
33 static void ertf_append_char            (ERTFParser     *parser,
34                                          gchar           ch);
35 static void ertf_append_str             (ERTFParser     *parser,
36                                          const gchar    *str,
37                                          gint            len);
38
39 static ERTFState ertf_parse_tag         (ERTFParser     *parser);
40 static void ertf_get_parenthesis        (ERTFParser     *parser, 
41                                          gchar          *buf, 
42                                          gint            len);
43
44 ERTFParser *ertf_parser_new(FILE *fp, CodeConverter *conv)
45 {
46         ERTFParser *parser;
47
48         g_return_val_if_fail(fp   != NULL, NULL);
49         g_return_val_if_fail(conv != NULL, NULL);
50
51         parser             = g_new0(ERTFParser, 1);
52         parser->fp         = fp;
53         parser->conv       = conv;
54         parser->str        = g_string_new(NULL);
55         parser->buf        = g_string_new(NULL);
56         parser->bufp       = parser->buf->str;
57         parser->newline    = TRUE;
58         parser->empty_line = TRUE;
59         parser->space      = FALSE;
60         parser->pre        = FALSE;
61
62         return parser;
63 }
64
65 void ertf_parser_destroy(ERTFParser *parser)
66 {
67         g_string_free(parser->str, TRUE);
68         g_string_free(parser->buf, TRUE);
69         g_free(parser);
70 }
71
72 gchar *ertf_parse(ERTFParser *parser)
73 {
74         parser->state = ERTF_NORMAL;
75         g_string_truncate(parser->str, 0);
76
77         if (*parser->bufp == '\0') {
78                 g_string_truncate(parser->buf, 0);
79                 parser->bufp = parser->buf->str;
80                 if (ertf_read_line(parser) == ERTF_EOF)
81                         return NULL;
82         }
83         
84         while (*parser->bufp != '\0') {
85                 switch (*parser->bufp) {
86                         case '<':
87                                 if (parser->str->len == 0)
88                                         ertf_parse_tag(parser);
89                                 else
90                                         return parser->str->str;
91                                 break;
92                         case '\n':
93                         case '\r':      
94                                 if (parser->bufp[0] == '\r' && parser->bufp[1] == '\n') 
95                                                 parser->bufp++;
96
97                                 if (!parser->pre) {
98                                         /* When not pre (not <nofill>), 1 CRLF = SPACE, N>1 CRLF = N-1 CRLF*/
99                                         if (!parser->newline) {
100                                                 parser->newline = TRUE;
101                                                 parser->space = TRUE;
102                                         parser->bufp++;
103                                         break;
104                                         }
105
106                                 }
107                         default:
108                                 ertf_append_char(parser, *parser->bufp++);
109                 }
110         }
111         
112         return parser->str->str;
113 }
114
115 static ERTFState ertf_read_line(ERTFParser *parser)
116 {
117         gchar buf[ERTFBUFSIZE];
118         gchar buf2[ERTFBUFSIZE];
119         gint index;
120
121         if (fgets(buf, sizeof(buf), parser->fp) == NULL) {
122                 parser->state = ERTF_EOF;
123                 return ERTF_EOF;
124         }
125
126         if (conv_convert(parser->conv, buf2, sizeof(buf2), buf) < 0) {
127                 g_warning("ertf_read_line(): code conversion failed\n");
128
129                 index = parser->bufp - parser->buf->str;
130
131                 g_string_append(parser->buf, buf);
132
133                 parser->bufp = parser->buf->str + index;
134
135                 return ERTF_ERR;
136         }
137
138         index = parser->bufp - parser->buf->str;
139
140         g_string_append(parser->buf, buf2);
141
142         parser->bufp = parser->buf->str + index;
143
144         return ERTF_NORMAL;
145 }
146
147 static void ertf_append_char(ERTFParser *parser, gchar ch)
148 {
149         GString *str = parser->str;
150
151         if (!parser->pre && parser->space) {
152                 if (ch != '\n')
153                         g_string_append_c(str, ' ');
154                 parser->space = FALSE;
155         }
156
157         g_string_append_c(str, ch);
158
159         parser->empty_line = FALSE;
160         if (ch == '\n') {
161                 if (parser->newline)
162                         parser->empty_line = TRUE;
163                 else
164                         parser->newline = TRUE;
165         } else
166                 parser->newline = FALSE;
167 }
168
169 static void ertf_append_str(ERTFParser *parser, const gchar *str, gint len)
170 {
171         GString *string = parser->str;
172
173         if (!parser->pre && parser->space) {
174                 g_string_append_c(string, ' ');
175                 parser->space = FALSE;
176         }
177
178         if (len == 0) return;
179         if (len < 0)
180                 g_string_append(string, str);
181         else {
182                 gchar *s;
183                 Xstrndup_a(s, str, len, return);
184                 g_string_append(string, s);
185         }
186
187         parser->empty_line = FALSE;
188         if (string->len > 0 && string->str[string->len - 1] == '\n') {
189                 parser->newline = TRUE;
190                 if (string->len > 1 && string->str[string->len - 2] == '\n')
191                         parser->empty_line = TRUE;
192         } else
193                 parser->newline = FALSE;
194 }
195
196 static ERTFState ertf_parse_tag(ERTFParser *parser)
197 {
198         gchar buf[ERTFBUFSIZE];
199         gchar *p;
200         
201         ertf_get_parenthesis (parser, buf, sizeof(buf));
202         
203         for (p = buf; *p != '\0'; p++) {
204                 if (isspace (*p)) {
205                         *p = '\0';
206                         break;
207                 }
208         }
209
210         parser->state = ERTF_UNKNOWN;
211         if (buf[0] == '\0') return parser->state;
212
213         g_strdown (buf);
214
215         if (!strcmp(buf, "nofill")) {
216                 parser->pre   = TRUE;
217                 parser->state = ERTF_NOFILL;
218         }
219         else if (!strcmp(buf, "/nofill")) {
220                 parser->pre   = FALSE;
221                 parser->state = ERTF_NORMAL;
222         }
223         
224         return parser->state;
225 }
226                 
227 static void ertf_get_parenthesis(ERTFParser *parser, gchar *buf, gint len)
228 {
229         gchar *p;
230
231         buf[0] = '\0';
232         g_return_if_fail(*parser->bufp == '<');
233
234         /* ignore params */
235         if (!g_strncasecmp(parser->bufp, "<param>", 4)) {
236                 parser->bufp += 7;
237                 while ((p = strstr(parser->bufp, "</param>")) == NULL)
238                         if (ertf_read_line(parser) == ERTF_EOF) return;
239                 parser->bufp = p + 8;
240                 return;
241         }
242         parser->bufp++;
243         while ((p = strchr(parser->bufp, '>')) == NULL)
244                 if (ertf_read_line(parser) == ERTF_EOF) return;
245
246         strncpy2(buf, parser->bufp, MIN(p - parser->bufp + 1, len));
247         parser->bufp = p + 1;
248 }
249