Fetchinfo: slightly improve prefs, add hints
[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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
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 "utils.h"
27
28 #define ERTFBUFSIZE     8192
29
30
31 static ERTFState ertf_read_line         (ERTFParser     *parser);
32 static void ertf_append_char            (ERTFParser     *parser,
33                                          gchar           ch);
34
35 static ERTFState ertf_parse_tag         (ERTFParser     *parser);
36 static void ertf_get_parenthesis        (ERTFParser     *parser, 
37                                          gchar          *buf, 
38                                          gint            len);
39
40 ERTFParser *ertf_parser_new(FILE *fp, CodeConverter *conv)
41 {
42         ERTFParser *parser;
43
44         cm_return_val_if_fail(fp   != NULL, NULL);
45         cm_return_val_if_fail(conv != NULL, NULL);
46
47         parser             = g_new0(ERTFParser, 1);
48         parser->fp         = fp;
49         parser->conv       = conv;
50         parser->str        = g_string_new(NULL);
51         parser->buf        = g_string_new(NULL);
52         parser->bufp       = parser->buf->str;
53         parser->newline    = TRUE;
54         parser->empty_line = TRUE;
55         parser->space      = FALSE;
56         parser->pre        = FALSE;
57
58         return parser;
59 }
60
61 void ertf_parser_destroy(ERTFParser *parser)
62 {
63         g_string_free(parser->str, TRUE);
64         g_string_free(parser->buf, TRUE);
65         g_free(parser);
66 }
67
68 gchar *ertf_parse(ERTFParser *parser)
69 {
70         parser->state = ERTF_NORMAL;
71         g_string_truncate(parser->str, 0);
72
73         if (*parser->bufp == '\0') {
74                 g_string_truncate(parser->buf, 0);
75                 parser->bufp = parser->buf->str;
76                 if (ertf_read_line(parser) == ERTF_EOF)
77                         return NULL;
78         }
79         
80         while (*parser->bufp != '\0') {
81                 switch (*parser->bufp) {
82                         case '<':
83                                 if (parser->str->len == 0)
84                                         ertf_parse_tag(parser);
85                                 else
86                                         return parser->str->str;
87                                 break;
88                         case '\n':
89                         case '\r':      
90                                 if (parser->bufp[0] == '\r' && parser->bufp[1] == '\n') 
91                                                 parser->bufp++;
92
93                                 if (!parser->pre) {
94                                         /* When not pre (not <nofill>), 1 CRLF = SPACE, N>1 CRLF = N-1 CRLF*/
95                                         if (!parser->newline) {
96                                                 parser->newline = TRUE;
97                                                 parser->space = TRUE;
98                                         parser->bufp++;
99                                         break;
100                                         }
101
102                                 }
103                         default:
104                                 ertf_append_char(parser, *parser->bufp++);
105                 }
106         }
107         
108         return parser->str->str;
109 }
110
111 static ERTFState ertf_read_line(ERTFParser *parser)
112 {
113         gchar buf[ERTFBUFSIZE];
114         gchar buf2[ERTFBUFSIZE];
115         gint index;
116
117         if (fgets(buf, sizeof(buf), parser->fp) == NULL) {
118                 parser->state = ERTF_EOF;
119                 return ERTF_EOF;
120         }
121
122         if (conv_convert(parser->conv, buf2, sizeof(buf2), buf) < 0) {
123                 g_warning("ertf_read_line(): code conversion failed\n");
124
125                 index = parser->bufp - parser->buf->str;
126
127                 g_string_append(parser->buf, buf);
128
129                 parser->bufp = parser->buf->str + index;
130
131                 return ERTF_ERR;
132         }
133
134         index = parser->bufp - parser->buf->str;
135
136         g_string_append(parser->buf, buf2);
137
138         parser->bufp = parser->buf->str + index;
139
140         return ERTF_NORMAL;
141 }
142
143 static void ertf_append_char(ERTFParser *parser, gchar ch)
144 {
145         GString *str = parser->str;
146
147         if (!parser->pre && parser->space) {
148                 if (ch != '\n')
149                         g_string_append_c(str, ' ');
150                 parser->space = FALSE;
151         }
152
153         g_string_append_c(str, ch);
154
155         parser->empty_line = FALSE;
156         if (ch == '\n') {
157                 if (parser->newline)
158                         parser->empty_line = TRUE;
159                 else
160                         parser->newline = TRUE;
161         } else
162                 parser->newline = FALSE;
163 }
164
165 static ERTFState ertf_parse_tag(ERTFParser *parser)
166 {
167         gchar buf[ERTFBUFSIZE];
168         gchar *p;
169         gchar *down;
170         
171         ertf_get_parenthesis (parser, buf, sizeof(buf));
172         
173         for (p = buf; *p != '\0'; p++) {
174                 if (isspace (*(guchar *)p)) {
175                         *p = '\0';
176                         break;
177                 }
178         }
179
180         parser->state = ERTF_UNKNOWN;
181         if (buf[0] == '\0') return parser->state;
182
183         down = g_utf8_strdown (buf, -1);
184
185         if (!strcmp(down, "nofill")) {
186                 parser->pre   = TRUE;
187                 parser->state = ERTF_NOFILL;
188         }
189         else if (!strcmp(down, "/nofill")) {
190                 parser->pre   = FALSE;
191                 parser->state = ERTF_NORMAL;
192         }
193         g_free(down);
194         return parser->state;
195 }
196                 
197 static void ertf_get_parenthesis(ERTFParser *parser, gchar *buf, gint len)
198 {
199         gchar *p;
200
201         buf[0] = '\0';
202         cm_return_if_fail(*parser->bufp == '<');
203
204         /* ignore params */
205         if (!g_ascii_strncasecmp(parser->bufp, "<param>", 4)) {
206                 parser->bufp += 7;
207                 while ((p = strstr(parser->bufp, "</param>")) == NULL)
208                         if (ertf_read_line(parser) == ERTF_EOF) return;
209                 parser->bufp = p + 8;
210                 return;
211         }
212         parser->bufp++;
213         while ((p = strchr(parser->bufp, '>')) == NULL)
214                 if (ertf_read_line(parser) == ERTF_EOF) return;
215
216         strncpy2(buf, parser->bufp, MIN(p - parser->bufp + 1, len));
217         parser->bufp = p + 1;
218 }
219