* msgbase.c: when a summary mode message list is requested, and the room
[citadel.git] / citadel / html.c
1 /*
2  * $Id$
3  *
4  * Functions which handle translation between HTML and plain text
5  * Copyright (c) 2000-2005 by Art Cancro and others.   This program is
6  * released under the terms of the GNU General Public License.
7  */
8
9 #include "sysdep.h"
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15
16 #if TIME_WITH_SYS_TIME
17 # include <sys/time.h>
18 # include <time.h>
19 #else
20 # if HAVE_SYS_TIME_H
21 #  include <sys/time.h>
22 # else
23 #  include <time.h>
24 # endif
25 #endif
26
27 #include <ctype.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <limits.h>
31 #include "citadel.h"
32 #include "server.h"
33 #include "serv_extensions.h"
34 #include "control.h"
35 #include "sysdep_decls.h"
36 #include "support.h"
37 #include "config.h"
38 #include "msgbase.h"
39 #include "tools.h"
40 #include "room_ops.h"
41 #include "html.h"
42  
43
44 /*
45  * Convert HTML to plain text.
46  *
47  * inputmsg      = pointer to raw HTML message
48  * screenwidth   = desired output screenwidth
49  * do_citaformat = set to 1 to indent newlines with spaces
50  */
51 char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaformat) {
52         char inbuf[SIZ];
53         char outbuf[SIZ];
54         char tag[1024];
55         int done_reading = 0;
56         char *inptr;
57         char *outptr;
58         size_t outptr_buffer_size;
59         size_t output_len = 0;
60         int i, j, ch, did_out, rb, scanch;
61         int nest = 0;           /* Bracket nesting level */
62         int blockquote = 0;     /* BLOCKQUOTE nesting level */
63         int bytes_processed = 0;
64
65         inptr = inputmsg;
66         strcpy(inbuf, "");
67         strcpy(outbuf, "");
68         if (msglen == 0) msglen = strlen(inputmsg);
69
70         outptr_buffer_size = strlen(inptr) + SIZ;
71         outptr = malloc(outptr_buffer_size);
72         if (outptr == NULL) return NULL;
73         strcpy(outptr, "");
74         output_len = 0;
75
76         do {
77                 /* Fill the input buffer */
78                 if ( (done_reading == 0) && (strlen(inbuf) < (SIZ-128)) ) {
79
80                         ch = *inptr++;
81                         if (ch != 0) {
82                                 inbuf[strlen(inbuf)+1] = 0;
83                                 inbuf[strlen(inbuf)] = ch;
84                         } 
85                         else {
86                                 done_reading = 1;
87                         }
88
89                         ++bytes_processed;
90                         if (bytes_processed > msglen) {
91                                 done_reading = 1;
92                         }
93
94                 }
95
96                 /* Do some parsing */
97                 if (strlen(inbuf)>0) {
98
99                     /* Fold in all the spacing */
100                     for (i=0; i<strlen(inbuf); ++i) {
101                         if (inbuf[i]==10) inbuf[i]=32;
102                         if (inbuf[i]==13) inbuf[i]=32;
103                         if (inbuf[i]==9) inbuf[i]=32;
104                         /*** we like foreign characters now.
105                         if ((inbuf[i]<32) || (inbuf[i]>126)) {
106                                 inbuf[i] = '?';
107                         } */
108                     }
109                     for (i=0; i<strlen(inbuf); ++i) {
110                         while ((inbuf[i]==32)&&(inbuf[i+1]==32))
111                                 strcpy(&inbuf[i], &inbuf[i+1]);
112                     }
113
114                     for (i=0; i<strlen(inbuf); ++i) {
115
116                         ch = inbuf[i];
117
118                         if (ch == '<') {
119                                 ++nest;
120                                 strcpy(tag, "");
121                         }
122
123                         else if (ch == '>') {   /* We have a tag. */
124                                 if (nest > 0) --nest;
125
126                                 /* Unqualify the tag (truncate at first space) */
127                                 if (strchr(tag, ' ') != NULL) {
128                                         strcpy(strchr(tag, ' '), "");
129                                 }
130                                 
131                                 if (!strcasecmp(tag, "P")) {
132                                         strcat(outbuf, "\n\n");
133                                 }
134
135                                 if (!strcasecmp(tag, "/DIV")) {
136                                         strcat(outbuf, "\n\n");
137                                 }
138
139                                 if (!strcasecmp(tag, "LI")) {
140                                         strcat(outbuf, "\n * ");
141                                 }
142
143                                 else if (!strcasecmp(tag, "/UL")) {
144                                         strcat(outbuf, "\n\n");
145                                 }
146
147                                 else if (!strcasecmp(tag, "H1")) {
148                                         strcat(outbuf, "\n\n");
149                                 }
150
151                                 else if (!strcasecmp(tag, "H2")) {
152                                         strcat(outbuf, "\n\n");
153                                 }
154
155                                 else if (!strcasecmp(tag, "H3")) {
156                                         strcat(outbuf, "\n\n");
157                                 }
158
159                                 else if (!strcasecmp(tag, "H4")) {
160                                         strcat(outbuf, "\n\n");
161                                 }
162
163                                 else if (!strcasecmp(tag, "/H1")) {
164                                         strcat(outbuf, "\n");
165                                 }
166
167                                 else if (!strcasecmp(tag, "/H2")) {
168                                         strcat(outbuf, "\n");
169                                 }
170
171                                 else if (!strcasecmp(tag, "/H3")) {
172                                         strcat(outbuf, "\n");
173                                 }
174
175                                 else if (!strcasecmp(tag, "/H4")) {
176                                         strcat(outbuf, "\n");
177                                 }
178
179                                 else if (!strcasecmp(tag, "HR")) {
180                                         strcat(outbuf, "\n ");
181                                         for (j=0; j<screenwidth-2; ++j)
182                                                 strcat(outbuf, "-");
183                                         strcat(outbuf, "\n");
184                                 }
185
186                                 else if (!strcasecmp(tag, "BR")) {
187                                         strcat(outbuf, "\n");
188                                 }
189
190                                 else if (!strcasecmp(tag, "TR")) {
191                                         strcat(outbuf, "\n");
192                                 }
193
194                                 else if (!strcasecmp(tag, "/TABLE")) {
195                                         strcat(outbuf, "\n");
196                                 }
197
198                                 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
199                                         strcat(outbuf, "\n\n <<\n");
200                                         ++blockquote;
201                                 }
202
203                                 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
204                                         strcat(outbuf, "\n >>\n\n");
205                                         --blockquote;
206                                 }
207
208                         }
209
210                         else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
211                                 tag[strlen(tag)+1] = 0;
212                                 tag[strlen(tag)] = ch;
213                         }
214                                 
215                         else if (!nest) {
216                                 outbuf[strlen(outbuf)+1] = 0;
217                                 outbuf[strlen(outbuf)] = ch;
218                         }
219                     }
220                     strcpy(inbuf, &inbuf[i]);
221                 }
222
223                 /* Convert &; tags to the forbidden characters */
224                 if (strlen(outbuf)>0) for (i=0; i<strlen(outbuf); ++i) {
225
226                         /* Character entity references */
227                         if (!strncasecmp(&outbuf[i], "&nbsp;", 6)) {
228                                 outbuf[i] = ' ';
229                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
230                         }
231
232                         if (!strncasecmp(&outbuf[i], "&ensp;", 6)) {
233                                 outbuf[i] = ' ';
234                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
235                         }
236
237                         if (!strncasecmp(&outbuf[i], "&emsp;", 6)) {
238                                 outbuf[i] = ' ';
239                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
240                         }
241
242                         if (!strncasecmp(&outbuf[i], "&thinsp;", 8)) {
243                                 outbuf[i] = ' ';
244                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
245                         }
246
247                         else if (!strncasecmp(&outbuf[i], "&lt;", 4)) {
248                                 outbuf[i] = '<';
249                                 strcpy(&outbuf[i+1], &outbuf[i+4]);
250                         }
251
252                         else if (!strncasecmp(&outbuf[i], "&gt;", 4)) {
253                                 outbuf[i] = '>';
254                                 strcpy(&outbuf[i+1], &outbuf[i+4]);
255                         }
256
257                         else if (!strncasecmp(&outbuf[i], "&amp;", 5)) {
258                                 strcpy(&outbuf[i+1], &outbuf[i+5]);
259                         }
260
261                         else if (!strncasecmp(&outbuf[i], "&quot;", 6)) {
262                                 outbuf[i] = '\"';
263                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
264                         }
265
266                         else if (!strncasecmp(&outbuf[i], "&lsquo;", 7)) {
267                                 outbuf[i] = '`';
268                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
269                         }
270
271                         else if (!strncasecmp(&outbuf[i], "&rsquo;", 7)) {
272                                 outbuf[i] = '\'';
273                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
274                         }
275
276                         else if (!strncasecmp(&outbuf[i], "&copy;", 6)) {
277                                 outbuf[i] = '(';
278                                 outbuf[i+1] = 'c';
279                                 outbuf[i+2] = ')';
280                                 strcpy(&outbuf[i+3], &outbuf[i+6]);
281                         }
282
283                         else if (!strncasecmp(&outbuf[i], "&hellip;", 8)) {
284                                 outbuf[i] = '.';
285                                 outbuf[i+1] = '.';
286                                 outbuf[i+2] = '.';
287                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
288                         }
289
290                         else if (!strncasecmp(&outbuf[i], "&trade;", 7)) {
291                                 outbuf[i] = '(';
292                                 outbuf[i+1] = 't';
293                                 outbuf[i+2] = 'm';
294                                 outbuf[i+3] = ')';
295                                 strcpy(&outbuf[i+4], &outbuf[i+7]);
296                         }
297
298                         else if (!strncasecmp(&outbuf[i], "&reg;", 5)) {
299                                 outbuf[i] = '(';
300                                 outbuf[i+1] = 'r';
301                                 outbuf[i+2] = ')';
302                                 strcpy(&outbuf[i+3], &outbuf[i+5]);
303                         }
304
305                         else if (!strncasecmp(&outbuf[i], "&frac14;", 8)) {
306                                 outbuf[i] = '1';
307                                 outbuf[i+1] = '/';
308                                 outbuf[i+2] = '4';
309                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
310                         }
311
312                         else if (!strncasecmp(&outbuf[i], "&frac12;", 8)) {
313                                 outbuf[i] = '1';
314                                 outbuf[i+1] = '/';
315                                 outbuf[i+2] = '2';
316                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
317                         }
318
319                         else if (!strncasecmp(&outbuf[i], "&frac34;", 8)) {
320                                 outbuf[i] = '3';
321                                 outbuf[i+1] = '/';
322                                 outbuf[i+2] = '4';
323                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
324                         }
325
326                         else if (!strncasecmp(&outbuf[i], "&ndash;", 7)) {
327                                 outbuf[i] = '-';
328                                 outbuf[i+1] = '-';
329                                 strcpy(&outbuf[i+2], &outbuf[i+7]);
330                         }
331
332                         else if (!strncasecmp(&outbuf[i], "&mdash;", 7)) {
333                                 outbuf[i] = '-';
334                                 outbuf[i+1] = '-';
335                                 outbuf[i+2] = '-';
336                                 strcpy(&outbuf[i+3], &outbuf[i+7]);
337                         }
338
339                         /* two-digit decimal equivalents */
340                         else if ((!strncmp(&outbuf[i], "&#", 2))
341                               && (outbuf[i+4] == ';') ) {
342                                 scanch = 0;
343                                 sscanf(&outbuf[i+2], "%02d", &scanch);
344                                 outbuf[i] = scanch;
345                                 strcpy(&outbuf[i+1], &outbuf[i+5]);
346                         }
347
348                         /* three-digit decimal equivalents */
349                         else if ((!strncmp(&outbuf[i], "&#", 2))
350                               && (outbuf[i+5] == ';') ) {
351                                 scanch = 0;
352                                 sscanf(&outbuf[i+2], "%03d", &scanch);
353                                 outbuf[i] = scanch;
354                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
355                         }
356
357                 }
358
359                 /* Make sure the output buffer is big enough */
360                 if ((output_len + strlen(outbuf) + SIZ)
361                    > outptr_buffer_size) {
362                         outptr_buffer_size += SIZ;
363                         outptr = realloc(outptr, outptr_buffer_size);
364                 }
365
366                 /* Output any lines terminated with hard line breaks */
367                 do {
368                         did_out = 0;
369                         if (strlen(outbuf)>0) {
370                             for (i = 0; i<strlen(outbuf); ++i) {
371                                 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
372
373                                         strncpy(&outptr[output_len],
374                                                 outbuf, i+1);
375                                         output_len += (i+1);
376
377                                         if (do_citaformat) {
378                                                 strcpy(&outptr[output_len],
379                                                         " ");
380                                                 ++output_len;
381                                         }
382
383                                         strcpy(outbuf, &outbuf[i+1]);
384                                         i = 0;
385                                         did_out = 1;
386                                 }
387                         }
388                     }
389                 } while (did_out);
390
391                 /* Add soft line breaks */
392                 if (strlen(outbuf) > (screenwidth - 2 )) {
393                         rb = (-1);
394                         for (i=0; i<(screenwidth-2); ++i) {
395                                 if (outbuf[i]==32) rb = i;
396                         }
397                         if (rb>=0) {
398                                 strncpy(&outptr[output_len], outbuf, rb);
399                                 output_len += rb;
400                                 strcpy(&outptr[output_len], "\n");
401                                 output_len += 1;
402                                 if (do_citaformat) {
403                                         strcpy(&outptr[output_len], " ");
404                                         ++output_len;
405                                 }
406                                 strcpy(outbuf, &outbuf[rb+1]);
407                         } else {
408                                 strncpy(&outptr[output_len], outbuf,
409                                         screenwidth-2);
410                                 output_len += (screenwidth-2);
411                                 strcpy(&outptr[output_len], "\n");
412                                 output_len += 1;
413                                 if (do_citaformat) {
414                                         strcpy(&outptr[output_len], " ");
415                                         ++output_len;
416                                 }
417                                 strcpy(outbuf, &outbuf[screenwidth-2]);
418                         }
419                 }
420
421         } while (done_reading == 0);
422
423         strcpy(&outptr[output_len], outbuf);
424         output_len += strlen(outbuf);
425
426         /* Strip leading/trailing whitespace.  We can't do this with
427          * striplt() because it uses too many strlen()'s
428          */
429         while ((output_len > 0) && (isspace(outptr[0]))) {
430                 strcpy(outptr, &outptr[1]);
431                 --output_len;
432         }
433         while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
434                 outptr[output_len-1] = 0;
435                 --output_len;
436         }
437
438         if (outptr[output_len-1] != '\n') {
439                 strcat(outptr, "\n");
440                 ++output_len;
441         }
442
443         return outptr;
444
445 }