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