HUGE PATCH. This moves all of mime_parser.c and all
[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 <libcitadel.h>
32 #include "citadel.h"
33 #include "server.h"
34 #include "control.h"
35 #include "sysdep_decls.h"
36 #include "support.h"
37 #include "config.h"
38 #include "msgbase.h"
39 #include "room_ops.h"
40 #include "html.h"
41  
42
43 /*
44  * Convert HTML to plain text.
45  *
46  * inputmsg      = pointer to raw HTML message
47  * screenwidth   = desired output screenwidth
48  * do_citaformat = set to 1 to indent newlines with spaces
49  */
50 char *html_to_ascii(char *inputmsg, int msglen, int screenwidth, int do_citaformat) {
51         char inbuf[SIZ];
52         int inbuf_len = 0;
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 styletag = 0;       /* STYLE tag nesting level */
64         int styletag_start = 0;
65         int bytes_processed = 0;
66         char nl[128];
67
68         strcpy(nl, "\n");
69         inptr = inputmsg;
70         strcpy(inbuf, "");
71         strcpy(outbuf, "");
72         if (msglen == 0) msglen = strlen(inputmsg);
73
74         outptr_buffer_size = strlen(inptr) + SIZ;
75         outptr = malloc(outptr_buffer_size);
76         if (outptr == NULL) return NULL;
77         strcpy(outptr, "");
78         output_len = 0;
79
80         do {
81                 /* Fill the input buffer */
82                 inbuf_len = strlen(inbuf);
83                 if ( (done_reading == 0) && (inbuf_len < (SIZ-128)) ) {
84
85                         ch = *inptr++;
86                         if (ch != 0) {
87                                 inbuf[inbuf_len++] = ch;
88                                 inbuf[inbuf_len] = 0;
89                         } 
90                         else {
91                                 done_reading = 1;
92                         }
93
94                         ++bytes_processed;
95                         if (bytes_processed > msglen) {
96                                 done_reading = 1;
97                         }
98
99                 }
100
101                 /* Do some parsing */
102                 if (!IsEmptyStr(inbuf)) {
103
104
105                     /* Fold in all the spacing */
106                     for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
107                         if (inbuf[i]==10) inbuf[i]=32;
108                         if (inbuf[i]==13) inbuf[i]=32;
109                         if (inbuf[i]==9) inbuf[i]=32;
110                         /*** we like foreign characters now.
111                         if ((inbuf[i]<32) || (inbuf[i]>126)) {
112                                 inbuf[i] = '?';
113                         } */
114                     }
115                     for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
116                         while ((inbuf[i]==32)&&(inbuf[i+1]==32))
117                                 strcpy(&inbuf[i], &inbuf[i+1]);
118                     }
119
120                     for (i=0; !IsEmptyStr(&inbuf[i]); ++i) {
121
122                         ch = inbuf[i];
123
124                         if (ch == '<') {
125                                 ++nest;
126                                 strcpy(tag, "");
127                         }
128
129                         else if (ch == '>') {   /* We have a tag. */
130                                 if (nest > 0) --nest;
131
132                                 /* Unqualify the tag (truncate at first space) */
133                                 if (strchr(tag, ' ') != NULL) {
134                                         strcpy(strchr(tag, ' '), "");
135                                 }
136                                 
137                                 if (!strcasecmp(tag, "P")) {
138                                         strcat(outbuf, nl);
139                                         strcat(outbuf, nl);
140                                 }
141
142                                 if (!strcasecmp(tag, "/DIV")) {
143                                         strcat(outbuf, nl);
144                                         strcat(outbuf, nl);
145                                 }
146
147                                 if (!strcasecmp(tag, "LI")) {
148                                         strcat(outbuf, nl);
149                                         strcat(outbuf, " * ");
150                                 }
151
152                                 else if (!strcasecmp(tag, "/UL")) {
153                                         strcat(outbuf, nl);
154                                         strcat(outbuf, nl);
155                                 }
156
157                                 else if (!strcasecmp(tag, "H1")) {
158                                         strcat(outbuf, nl);
159                                         strcat(outbuf, nl);
160                                 }
161
162                                 else if (!strcasecmp(tag, "H2")) {
163                                         strcat(outbuf, nl);
164                                         strcat(outbuf, nl);
165                                 }
166
167                                 else if (!strcasecmp(tag, "H3")) {
168                                         strcat(outbuf, nl);
169                                         strcat(outbuf, nl);
170                                 }
171
172                                 else if (!strcasecmp(tag, "H4")) {
173                                         strcat(outbuf, nl);
174                                         strcat(outbuf, nl);
175                                 }
176
177                                 else if (!strcasecmp(tag, "/H1")) {
178                                         strcat(outbuf, nl);
179                                 }
180
181                                 else if (!strcasecmp(tag, "/H2")) {
182                                         strcat(outbuf, nl);
183                                 }
184
185                                 else if (!strcasecmp(tag, "/H3")) {
186                                         strcat(outbuf, nl);
187                                 }
188
189                                 else if (!strcasecmp(tag, "/H4")) {
190                                         strcat(outbuf, nl);
191                                 }
192
193                                 else if (!strcasecmp(tag, "HR")) {
194                                         strcat(outbuf, nl);
195                                         strcat(outbuf, " ");
196                                         for (j=0; j<screenwidth-2; ++j)
197                                                 strcat(outbuf, "-");
198                                         strcat(outbuf, nl);
199                                 }
200
201                                 else if (!strcasecmp(tag, "BR")) {
202                                         strcat(outbuf, nl);
203                                 }
204
205                                 else if (!strcasecmp(tag, "TR")) {
206                                         strcat(outbuf, nl);
207                                 }
208
209                                 else if (!strcasecmp(tag, "/TABLE")) {
210                                         strcat(outbuf, nl);
211                                 }
212
213                                 else if (!strcasecmp(tag, "BLOCKQUOTE")) {
214                                         ++blockquote;
215                                         strcpy(nl, "\n");
216                                         for (j=0; j<blockquote; ++j) strcat(nl, ">");
217                                         strcat(outbuf, nl);
218                                 }
219
220                                 else if (!strcasecmp(tag, "/BLOCKQUOTE")) {
221                                         strcat(outbuf, "\n");
222                                         --blockquote;
223                                         strcpy(nl, "\n");
224                                         for (j=0; j<blockquote; ++j) strcat(nl, ">");
225                                         strcat(outbuf, nl);
226                                 }
227
228                                 else if (!strcasecmp(tag, "STYLE")) {
229                                         ++styletag;
230                                         if (styletag == 1) {
231                                                 styletag_start = strlen(outbuf);
232                                         }
233                                 }
234
235                                 else if (!strcasecmp(tag, "/STYLE")) {
236                                         --styletag;
237                                         if (styletag == 0) {
238                                                 outbuf[styletag_start] = 0;
239                                         }
240                                 }
241
242                         }
243
244                         else if ((nest > 0) && (strlen(tag)<(sizeof(tag)-1))) {
245                                 tag[strlen(tag)+1] = 0;
246                                 tag[strlen(tag)] = ch;
247                         }
248                                 
249                         else if (!nest) {
250                                 outbuf[strlen(outbuf)+1] = 0;
251                                 outbuf[strlen(outbuf)] = ch;
252                         }
253                     }
254                     strcpy(inbuf, &inbuf[i]);
255                 }
256
257                 /* Convert &; tags to the forbidden characters */
258                 if (!IsEmptyStr(outbuf)) for (i=0; !IsEmptyStr(&outbuf[i]); ++i) {
259
260                         /* Character entity references */
261                         if (!strncasecmp(&outbuf[i], "&nbsp;", 6)) {
262                                 outbuf[i] = ' ';
263                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
264                         }
265
266                         if (!strncasecmp(&outbuf[i], "&ensp;", 6)) {
267                                 outbuf[i] = ' ';
268                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
269                         }
270
271                         if (!strncasecmp(&outbuf[i], "&emsp;", 6)) {
272                                 outbuf[i] = ' ';
273                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
274                         }
275
276                         if (!strncasecmp(&outbuf[i], "&thinsp;", 8)) {
277                                 outbuf[i] = ' ';
278                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
279                         }
280
281                         else if (!strncasecmp(&outbuf[i], "&lt;", 4)) {
282                                 outbuf[i] = '<';
283                                 strcpy(&outbuf[i+1], &outbuf[i+4]);
284                         }
285
286                         else if (!strncasecmp(&outbuf[i], "&gt;", 4)) {
287                                 outbuf[i] = '>';
288                                 strcpy(&outbuf[i+1], &outbuf[i+4]);
289                         }
290
291                         else if (!strncasecmp(&outbuf[i], "&amp;", 5)) {
292                                 strcpy(&outbuf[i+1], &outbuf[i+5]);
293                         }
294
295                         else if (!strncasecmp(&outbuf[i], "&quot;", 6)) {
296                                 outbuf[i] = '\"';
297                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
298                         }
299
300                         else if (!strncasecmp(&outbuf[i], "&lsquo;", 7)) {
301                                 outbuf[i] = '`';
302                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
303                         }
304
305                         else if (!strncasecmp(&outbuf[i], "&rsquo;", 7)) {
306                                 outbuf[i] = '\'';
307                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
308                         }
309
310                         else if (!strncasecmp(&outbuf[i], "&copy;", 6)) {
311                                 outbuf[i] = '(';
312                                 outbuf[i+1] = 'c';
313                                 outbuf[i+2] = ')';
314                                 strcpy(&outbuf[i+3], &outbuf[i+6]);
315                         }
316
317                         else if (!strncasecmp(&outbuf[i], "&bull;", 6)) {
318                                 outbuf[i] = ' ';
319                                 outbuf[i+1] = '*';
320                                 outbuf[i+2] = ' ';
321                                 strcpy(&outbuf[i+3], &outbuf[i+6]);
322                         }
323
324                         else if (!strncasecmp(&outbuf[i], "&hellip;", 8)) {
325                                 outbuf[i] = '.';
326                                 outbuf[i+1] = '.';
327                                 outbuf[i+2] = '.';
328                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
329                         }
330
331                         else if (!strncasecmp(&outbuf[i], "&trade;", 7)) {
332                                 outbuf[i] = '(';
333                                 outbuf[i+1] = 't';
334                                 outbuf[i+2] = 'm';
335                                 outbuf[i+3] = ')';
336                                 strcpy(&outbuf[i+4], &outbuf[i+7]);
337                         }
338
339                         else if (!strncasecmp(&outbuf[i], "&reg;", 5)) {
340                                 outbuf[i] = '(';
341                                 outbuf[i+1] = 'r';
342                                 outbuf[i+2] = ')';
343                                 strcpy(&outbuf[i+3], &outbuf[i+5]);
344                         }
345
346                         else if (!strncasecmp(&outbuf[i], "&frac14;", 8)) {
347                                 outbuf[i] = '1';
348                                 outbuf[i+1] = '/';
349                                 outbuf[i+2] = '4';
350                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
351                         }
352
353                         else if (!strncasecmp(&outbuf[i], "&frac12;", 8)) {
354                                 outbuf[i] = '1';
355                                 outbuf[i+1] = '/';
356                                 outbuf[i+2] = '2';
357                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
358                         }
359
360                         else if (!strncasecmp(&outbuf[i], "&frac34;", 8)) {
361                                 outbuf[i] = '3';
362                                 outbuf[i+1] = '/';
363                                 outbuf[i+2] = '4';
364                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
365                         }
366
367                         else if (!strncasecmp(&outbuf[i], "&ndash;", 7)) {
368                                 outbuf[i] = '-';
369                                 outbuf[i+1] = '-';
370                                 strcpy(&outbuf[i+2], &outbuf[i+7]);
371                         }
372
373                         else if (!strncasecmp(&outbuf[i], "&mdash;", 7)) {
374                                 outbuf[i] = '-';
375                                 outbuf[i+1] = '-';
376                                 outbuf[i+2] = '-';
377                                 strcpy(&outbuf[i+3], &outbuf[i+7]);
378                         }
379
380                         else if (!strncmp(&outbuf[i], "&Ccedil;", 8)) {
381                                 outbuf[i] = 'C';
382                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
383                         }
384
385                         else if (!strncasecmp(&outbuf[i], "&ccedil;", 8)) {
386                                 outbuf[i] = 'c';
387                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
388                         }
389
390                         else if (!strncmp(&outbuf[i], "&Egrave;", 8)) {
391                                 outbuf[i] = 'E';
392                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
393                         }
394
395                         else if (!strncasecmp(&outbuf[i], "&egrave;", 8)) {
396                                 outbuf[i] = 'e';
397                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
398                         }
399
400                         else if (!strncmp(&outbuf[i], "&Ecirc;", 7)) {
401                                 outbuf[i] = 'E';
402                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
403                         }
404
405                         else if (!strncasecmp(&outbuf[i], "&ecirc;", 7)) {
406                                 outbuf[i] = 'e';
407                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
408                         }
409
410                         else if (!strncmp(&outbuf[i], "&Eacute;", 8)) {
411                                 outbuf[i] = 'E';
412                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
413                         }
414
415                         else if (!strncasecmp(&outbuf[i], "&eacute;", 8)) {
416                                 outbuf[i] = 'e';
417                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
418                         }
419
420                         else if (!strncmp(&outbuf[i], "&Agrave;", 8)) {
421                                 outbuf[i] = 'A';
422                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
423                         }
424
425                         else if (!strncasecmp(&outbuf[i], "&agrave;", 8)) {
426                                 outbuf[i] = 'a';
427                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
428                         }
429
430                         else if (!strncasecmp(&outbuf[i], "&ldquo;", 7)) {
431                                 outbuf[i] = '\"';
432                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
433                         }
434
435                         else if (!strncasecmp(&outbuf[i], "&rdquo;", 7)) {
436                                 outbuf[i] = '\"';
437                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
438                         }
439
440                         /* two-digit decimal equivalents */
441                         else if ((!strncmp(&outbuf[i], "&#", 2))
442                               && (outbuf[i+4] == ';') ) {
443                                 scanch = 0;
444                                 sscanf(&outbuf[i+2], "%02d", &scanch);
445                                 outbuf[i] = scanch;
446                                 strcpy(&outbuf[i+1], &outbuf[i+5]);
447                         }
448
449                         /* three-digit decimal equivalents */
450                         else if ((!strncmp(&outbuf[i], "&#", 2))
451                               && (outbuf[i+5] == ';') ) {
452                                 scanch = 0;
453                                 sscanf(&outbuf[i+2], "%03d", &scanch);
454                                 outbuf[i] = scanch;
455                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
456                         }
457
458                 }
459
460                 /* Make sure the output buffer is big enough */
461                 if ((output_len + strlen(outbuf) + SIZ) > outptr_buffer_size) {
462                         outptr_buffer_size += SIZ;
463                         outptr = realloc(outptr, outptr_buffer_size);
464                         if (outptr == NULL) {
465                                 abort();
466                         }
467                 }
468
469                 /* Output any lines terminated with hard line breaks */
470                 do {
471                         did_out = 0;
472                         if (strlen(outbuf) > 0) {
473                             for (i = 0; i<strlen(outbuf); ++i) {
474                                 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
475
476                                         strncpy(&outptr[output_len], outbuf, i+1);
477                                         output_len += (i+1);
478
479                                         if (do_citaformat) {
480                                                 strcpy(&outptr[output_len], " ");
481                                                 ++output_len;
482                                         }
483
484                                         strcpy(outbuf, &outbuf[i+1]);
485                                         i = 0;
486                                         did_out = 1;
487                                 }
488                         }
489                     }
490                 } while (did_out);
491
492                 /* Add soft line breaks */
493                 if (strlen(outbuf) > (screenwidth - 2 )) {
494                         rb = (-1);
495                         for (i=0; i<(screenwidth-2); ++i) {
496                                 if (outbuf[i]==32) rb = i;
497                         }
498                         if (rb>=0) {
499                                 strncpy(&outptr[output_len], outbuf, rb);
500                                 output_len += rb;
501                                 strcpy(&outptr[output_len], nl);
502                                 output_len += strlen(nl);
503                                 if (do_citaformat) {
504                                         strcpy(&outptr[output_len], " ");
505                                         ++output_len;
506                                 }
507                                 strcpy(outbuf, &outbuf[rb+1]);
508                         } else {
509                                 strncpy(&outptr[output_len], outbuf,
510                                         screenwidth-2);
511                                 output_len += (screenwidth-2);
512                                 strcpy(&outptr[output_len], nl);
513                                 output_len += strlen(nl);
514                                 if (do_citaformat) {
515                                         strcpy(&outptr[output_len], " ");
516                                         ++output_len;
517                                 }
518                                 strcpy(outbuf, &outbuf[screenwidth-2]);
519                         }
520                 }
521
522         } while (done_reading == 0);
523
524         strcpy(&outptr[output_len], outbuf);
525         output_len += strlen(outbuf);
526
527         /* Strip leading/trailing whitespace.  We can't do this with
528          * striplt() because it uses too many strlen()'s
529          */
530         while ((output_len > 0) && (isspace(outptr[0]))) {
531                 strcpy(outptr, &outptr[1]);
532                 --output_len;
533         }
534         while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
535                 outptr[output_len-1] = 0;
536                 --output_len;
537         }
538
539         if (outptr[output_len-1] != '\n') {
540                 strcat(outptr, "\n");
541                 ++output_len;
542         }
543
544         return outptr;
545
546 }