]> code.citadel.org Git - citadel.git/blob - citadel/html.c
Lots of LDAP changes.
[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; !IsEmptyStr(&inbuf[i]); ++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; !IsEmptyStr(&inbuf[i]); ++i) {
113                         while ((inbuf[i]==32)&&(inbuf[i+1]==32))
114                                 strcpy(&inbuf[i], &inbuf[i+1]);
115                     }
116
117                     for (i=0; !IsEmptyStr(&inbuf[i]); ++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; !IsEmptyStr(&outbuf[i]); ++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], "&bull;", 6)) {
315                                 outbuf[i] = ' ';
316                                 outbuf[i+1] = '*';
317                                 outbuf[i+2] = ' ';
318                                 strcpy(&outbuf[i+3], &outbuf[i+6]);
319                         }
320
321                         else if (!strncasecmp(&outbuf[i], "&hellip;", 8)) {
322                                 outbuf[i] = '.';
323                                 outbuf[i+1] = '.';
324                                 outbuf[i+2] = '.';
325                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
326                         }
327
328                         else if (!strncasecmp(&outbuf[i], "&trade;", 7)) {
329                                 outbuf[i] = '(';
330                                 outbuf[i+1] = 't';
331                                 outbuf[i+2] = 'm';
332                                 outbuf[i+3] = ')';
333                                 strcpy(&outbuf[i+4], &outbuf[i+7]);
334                         }
335
336                         else if (!strncasecmp(&outbuf[i], "&reg;", 5)) {
337                                 outbuf[i] = '(';
338                                 outbuf[i+1] = 'r';
339                                 outbuf[i+2] = ')';
340                                 strcpy(&outbuf[i+3], &outbuf[i+5]);
341                         }
342
343                         else if (!strncasecmp(&outbuf[i], "&frac14;", 8)) {
344                                 outbuf[i] = '1';
345                                 outbuf[i+1] = '/';
346                                 outbuf[i+2] = '4';
347                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
348                         }
349
350                         else if (!strncasecmp(&outbuf[i], "&frac12;", 8)) {
351                                 outbuf[i] = '1';
352                                 outbuf[i+1] = '/';
353                                 outbuf[i+2] = '2';
354                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
355                         }
356
357                         else if (!strncasecmp(&outbuf[i], "&frac34;", 8)) {
358                                 outbuf[i] = '3';
359                                 outbuf[i+1] = '/';
360                                 outbuf[i+2] = '4';
361                                 strcpy(&outbuf[i+3], &outbuf[i+8]);
362                         }
363
364                         else if (!strncasecmp(&outbuf[i], "&ndash;", 7)) {
365                                 outbuf[i] = '-';
366                                 outbuf[i+1] = '-';
367                                 strcpy(&outbuf[i+2], &outbuf[i+7]);
368                         }
369
370                         else if (!strncasecmp(&outbuf[i], "&mdash;", 7)) {
371                                 outbuf[i] = '-';
372                                 outbuf[i+1] = '-';
373                                 outbuf[i+2] = '-';
374                                 strcpy(&outbuf[i+3], &outbuf[i+7]);
375                         }
376
377                         else if (!strncmp(&outbuf[i], "&Ccedil;", 8)) {
378                                 outbuf[i] = 'C';
379                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
380                         }
381
382                         else if (!strncasecmp(&outbuf[i], "&ccedil;", 8)) {
383                                 outbuf[i] = 'c';
384                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
385                         }
386
387                         else if (!strncmp(&outbuf[i], "&Egrave;", 8)) {
388                                 outbuf[i] = 'E';
389                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
390                         }
391
392                         else if (!strncasecmp(&outbuf[i], "&egrave;", 8)) {
393                                 outbuf[i] = 'e';
394                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
395                         }
396
397                         else if (!strncmp(&outbuf[i], "&Ecirc;", 7)) {
398                                 outbuf[i] = 'E';
399                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
400                         }
401
402                         else if (!strncasecmp(&outbuf[i], "&ecirc;", 7)) {
403                                 outbuf[i] = 'e';
404                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
405                         }
406
407                         else if (!strncmp(&outbuf[i], "&Eacute;", 8)) {
408                                 outbuf[i] = 'E';
409                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
410                         }
411
412                         else if (!strncasecmp(&outbuf[i], "&eacute;", 8)) {
413                                 outbuf[i] = 'e';
414                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
415                         }
416
417                         else if (!strncmp(&outbuf[i], "&Agrave;", 8)) {
418                                 outbuf[i] = 'A';
419                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
420                         }
421
422                         else if (!strncasecmp(&outbuf[i], "&agrave;", 8)) {
423                                 outbuf[i] = 'a';
424                                 strcpy(&outbuf[i+1], &outbuf[i+8]);
425                         }
426
427                         else if (!strncasecmp(&outbuf[i], "&ldquo;", 7)) {
428                                 outbuf[i] = '\"';
429                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
430                         }
431
432                         else if (!strncasecmp(&outbuf[i], "&rdquo;", 7)) {
433                                 outbuf[i] = '\"';
434                                 strcpy(&outbuf[i+1], &outbuf[i+7]);
435                         }
436
437                         /* two-digit decimal equivalents */
438                         else if ((!strncmp(&outbuf[i], "&#", 2))
439                               && (outbuf[i+4] == ';') ) {
440                                 scanch = 0;
441                                 sscanf(&outbuf[i+2], "%02d", &scanch);
442                                 outbuf[i] = scanch;
443                                 strcpy(&outbuf[i+1], &outbuf[i+5]);
444                         }
445
446                         /* three-digit decimal equivalents */
447                         else if ((!strncmp(&outbuf[i], "&#", 2))
448                               && (outbuf[i+5] == ';') ) {
449                                 scanch = 0;
450                                 sscanf(&outbuf[i+2], "%03d", &scanch);
451                                 outbuf[i] = scanch;
452                                 strcpy(&outbuf[i+1], &outbuf[i+6]);
453                         }
454
455                 }
456
457                 /* Make sure the output buffer is big enough */
458                 if ((output_len + strlen(outbuf) + SIZ) > outptr_buffer_size) {
459                         outptr_buffer_size += SIZ;
460                         outptr = realloc(outptr, outptr_buffer_size);
461                         if (outptr == NULL) {
462                                 abort();
463                         }
464                 }
465
466                 /* Output any lines terminated with hard line breaks */
467                 do {
468                         did_out = 0;
469                         if (strlen(outbuf) > 0) {
470                             for (i = 0; i<strlen(outbuf); ++i) {
471                                 if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
472
473                                         strncpy(&outptr[output_len], outbuf, i+1);
474                                         output_len += (i+1);
475
476                                         if (do_citaformat) {
477                                                 strcpy(&outptr[output_len], " ");
478                                                 ++output_len;
479                                         }
480
481                                         strcpy(outbuf, &outbuf[i+1]);
482                                         i = 0;
483                                         did_out = 1;
484                                 }
485                         }
486                     }
487                 } while (did_out);
488
489                 /* Add soft line breaks */
490                 if (strlen(outbuf) > (screenwidth - 2 )) {
491                         rb = (-1);
492                         for (i=0; i<(screenwidth-2); ++i) {
493                                 if (outbuf[i]==32) rb = i;
494                         }
495                         if (rb>=0) {
496                                 strncpy(&outptr[output_len], outbuf, rb);
497                                 output_len += rb;
498                                 strcpy(&outptr[output_len], nl);
499                                 output_len += strlen(nl);
500                                 if (do_citaformat) {
501                                         strcpy(&outptr[output_len], " ");
502                                         ++output_len;
503                                 }
504                                 strcpy(outbuf, &outbuf[rb+1]);
505                         } else {
506                                 strncpy(&outptr[output_len], outbuf,
507                                         screenwidth-2);
508                                 output_len += (screenwidth-2);
509                                 strcpy(&outptr[output_len], nl);
510                                 output_len += strlen(nl);
511                                 if (do_citaformat) {
512                                         strcpy(&outptr[output_len], " ");
513                                         ++output_len;
514                                 }
515                                 strcpy(outbuf, &outbuf[screenwidth-2]);
516                         }
517                 }
518
519         } while (done_reading == 0);
520
521         strcpy(&outptr[output_len], outbuf);
522         output_len += strlen(outbuf);
523
524         /* Strip leading/trailing whitespace.  We can't do this with
525          * striplt() because it uses too many strlen()'s
526          */
527         while ((output_len > 0) && (isspace(outptr[0]))) {
528                 strcpy(outptr, &outptr[1]);
529                 --output_len;
530         }
531         while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
532                 outptr[output_len-1] = 0;
533                 --output_len;
534         }
535
536         if (outptr[output_len-1] != '\n') {
537                 strcat(outptr, "\n");
538                 ++output_len;
539         }
540
541         return outptr;
542
543 }