html2html.c

00001 /*
00002  * $Id: html2html.c 5147 2007-05-08 15:36:22Z ajc $
00003  */
00010 #include "webcit.h"
00011 #include "vcard.h"
00012 #include "webserver.h"
00013 
00014 
00020 void stripquotes(char *s)
00021 {
00022         int len;
00023 
00024         if (!s) return;
00025 
00026         len = strlen(s);
00027         if (len < 2) return;
00028 
00029         if ( ( (s[0] == '\"') && (s[len-1] == '\"') ) || ( (s[0] == '\'') && (s[len-1] == '\'') ) ) {
00030                 s[len-1] = 0;
00031                 strcpy(s, &s[1]);
00032         }
00033 }
00034 
00035 
00043 void extract_charset_from_meta(char *charset, char *meta_http_equiv, char *meta_content)
00044 {
00045         char *ptr;
00046         char buf[64];
00047 
00048         if (!charset) return;
00049         if (!meta_http_equiv) return;
00050         if (!meta_content) return;
00051 
00052 
00053         if (strcasecmp(meta_http_equiv, "Content-type")) return;
00054 
00055         ptr = strchr(meta_content, ';');
00056         if (!ptr) return;
00057 
00058         safestrncpy(buf, ++ptr, sizeof buf);
00059         striplt(buf);
00060         if (!strncasecmp(buf, "charset=", 8)) {
00061                 strcpy(charset, &buf[8]);
00062 
00063                 /*
00064                  * The brain-damaged webmail program in Microsoft Exchange declares
00065                  * a charset of "unicode" when they really mean "UTF-8".  GNU iconv
00066                  * treats "unicode" as an alias for "UTF-16" so we have to manually
00067                  * fix this here, otherwise messages generated in Exchange webmail
00068                  * show up as a big pile of weird characters.
00069                  */
00070                 if (!strcasecmp(charset, "unicode")) {
00071                         strcpy(charset, "UTF-8");
00072                 }
00073 
00074         }
00075 }
00076 
00077 
00078 
00085 void output_html(char *supplied_charset, int treat_as_wiki) {
00086         char buf[SIZ];
00087         char *msg;
00088         char *ptr;
00089         char *msgstart;
00090         char *msgend;
00091         char *converted_msg;
00092         size_t converted_alloc = 0;
00093         int buffer_length = 1;
00094         int line_length = 0;
00095         int content_length = 0;
00096         int output_length = 0;
00097         char new_window[SIZ];
00098         int brak = 0;
00099         int alevel = 0;
00100         int i;
00101         int linklen;
00102         char charset[128];
00103 #ifdef HAVE_ICONV
00104         iconv_t ic = (iconv_t)(-1) ;
00105         char *ibuf;                   
00106         char *obuf;                   
00107         size_t ibuflen;               
00108         size_t obuflen;               
00109         char *osav;                   
00110 #endif
00111 
00112         safestrncpy(charset, supplied_charset, sizeof charset);
00113         msg = strdup("");
00114         sprintf(new_window, "<a target=\"%s\" href=", TARGET);
00115 
00116         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
00117                 line_length = strlen(buf);
00118                 buffer_length = content_length + line_length + 2;
00119                 ptr = realloc(msg, buffer_length);
00120                 if (ptr == NULL) {
00121                         wprintf("<b>");
00122                         wprintf(_("realloc() error! couldn't get %d bytes: %s"),
00123                                 buffer_length + 1,
00124                                 strerror(errno));
00125                         wprintf("</b><br /><br />\n");
00126                         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
00128                         }
00129                         free(msg);
00130                         return;
00131                 }
00132                 msg = ptr;
00133                 strcpy(&msg[content_length], buf);
00134                 content_length += line_length;
00135                 strcpy(&msg[content_length], "\n");
00136                 content_length += 1;
00137         }
00138 
00140         ptr = msg;
00141         msgstart = msg;
00142         msgend = &msg[content_length];
00143 
00144         while (ptr < msgend) {
00145 
00147                 ptr = strchr(ptr, '<');
00148                 if ((ptr == NULL) || (ptr >= msgend)) break;
00149                 ++ptr;
00150                 if ((ptr == NULL) || (ptr >= msgend)) break;
00151 
00158                 if (!strncasecmp(ptr, "META", 4)) {
00159 
00160                         char *meta_start;
00161                         char *meta_end;
00162                         int meta_length;
00163                         char *meta;
00164                         char *meta_http_equiv;
00165                         char *meta_content;
00166                         char *spaceptr;
00167 
00168                         meta_start = &ptr[4];
00169                         meta_end = strchr(ptr, '>');
00170                         if ((meta_end != NULL) && (meta_end <= msgend)) {
00171                                 meta_length = meta_end - meta_start + 1;
00172                                 meta = malloc(meta_length + 1);
00173                                 safestrncpy(meta, meta_start, meta_length);
00174                                 meta[meta_length] = 0;
00175                                 striplt(meta);
00176                                 if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
00177                                         meta_http_equiv = strdup(&meta[11]);
00178                                         spaceptr = strchr(meta_http_equiv, ' ');
00179                                         if (spaceptr != NULL) {
00180                                                 *spaceptr = 0;
00181                                                 meta_content = strdup(++spaceptr);
00182                                                 if (!strncasecmp(meta_content, "content=", 8)) {
00183                                                         strcpy(meta_content, &meta_content[8]);
00184                                                         stripquotes(meta_http_equiv);
00185                                                         stripquotes(meta_content);
00186                                                         extract_charset_from_meta(charset,
00187                                                                 meta_http_equiv, meta_content);
00188                                                 }
00189                                                 free(meta_content);
00190                                         }
00191                                         free(meta_http_equiv);
00192                                 }
00193                                 free(meta);
00194                         }
00195                 }
00196 
00201                 if ( (!strncasecmp(ptr, "HTML", 4))
00202                    ||(!strncasecmp(ptr, "HEAD", 4))
00203                    ||(!strncasecmp(ptr, "/HEAD", 5))
00204                    ||(!strncasecmp(ptr, "BODY", 4)) ) {
00205                         ptr = strchr(ptr, '>');
00206                         if ((ptr == NULL) || (ptr >= msgend)) break;
00207                         ++ptr;
00208                         if ((ptr == NULL) || (ptr >= msgend)) break;
00209                         msgstart = ptr;
00210                 }
00211 
00216                 if ( (!strncasecmp(ptr, "/HTML", 5))
00217                    ||(!strncasecmp(ptr, "/BODY", 5)) ) {
00218                         --ptr;
00219                         msgend = ptr;
00220                         strcpy(ptr, "");
00221                         
00222                 }
00223 
00224                 ++ptr;
00225         }
00226         if (msgstart > msg) {
00227                 strcpy(msg, msgstart);
00228         }
00229 
00231 #ifdef HAVE_ICONV
00232         if ( (strcasecmp(charset, "us-ascii"))
00233            && (strcasecmp(charset, "UTF-8"))
00234            && (strcasecmp(charset, ""))
00235         ) {
00236                 lprintf(9, "Converting %s to UTF-8\n", charset);
00237                 ic = ctdl_iconv_open("UTF-8", charset);
00238                 if (ic == (iconv_t)(-1) ) {
00239                         lprintf(5, "%s:%d iconv_open() failed: %s\n",
00240                                 __FILE__, __LINE__, strerror(errno));
00241                 }
00242         }
00243         if (ic != (iconv_t)(-1) ) {
00244                 ibuf = msg;
00245                 ibuflen = content_length;
00246                 obuflen = content_length + (content_length / 2) ;
00247                 obuf = (char *) malloc(obuflen);
00248                 osav = obuf;
00249                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
00250                 content_length = content_length + (content_length / 2) - obuflen;
00251                 osav[content_length] = 0;
00252                 free(msg);
00253                 msg = osav;
00254                 iconv_close(ic);
00255         }
00256 #endif
00257 
00267         converted_alloc = content_length + 8192;
00268         converted_msg = malloc(converted_alloc);
00269         if (converted_msg == NULL) {
00270                 wprintf("Error %d: %s<br />%s:%s", errno, strerror(errno), __FILE__, __LINE__);
00271                 goto BAIL;
00272         }
00273 
00274         strcpy(converted_msg, "");
00275         ptr = msg;
00276         msgend = strchr(msg, 0);
00277         while (ptr < msgend) {
00278 
00285                 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
00286                         content_length += 64;
00287                         if (content_length >= converted_alloc) {
00288                                 converted_alloc += 8192;
00289                                 converted_msg = realloc(converted_msg, converted_alloc);
00290                                 if (converted_msg == NULL) {
00291                                         abort();
00292                                 }
00293                         }
00294                         sprintf(&converted_msg[output_length],
00295                                 "<a href=\"display_enter"
00296                                 "?force_room=_MAIL_&recp=");
00297                         output_length += 47;
00298                         ptr = &ptr[16];
00299                         ++alevel;
00300                 }
00302                 else if (!strncasecmp(ptr, "<a href=\"", 9)) {
00303                         ++alevel;
00304                         if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
00305                              &&  ((strchr(ptr, '/') < strchr(ptr, '>'))) 
00306                              ) {
00307                                 /* open external links to new window */
00308                                 content_length += 64;
00309                                 if (content_length >= converted_alloc) {
00310                                         converted_alloc += 8192;
00311                                         converted_msg = realloc(converted_msg, converted_alloc);
00312                                         if (converted_msg == NULL) {
00313                                                 abort();
00314                                         }
00315                                 }
00316                                 sprintf(&converted_msg[output_length], new_window);
00317                                 output_length += strlen(new_window);
00318                                 ptr = &ptr[8];
00319                         }
00320                         else if ( (treat_as_wiki) && (strncasecmp(ptr, "<a href=\"wiki?", 14)) ) {
00321                                 content_length += 64;
00322                                 if (content_length >= converted_alloc) {
00323                                         converted_alloc += 8192;
00324                                         converted_msg = realloc(converted_msg, converted_alloc);
00325                                         if (converted_msg == NULL) {
00326                                                 abort();
00327                                         }
00328                                 }
00329                                 sprintf(&converted_msg[output_length], "<a href=\"wiki?page=");
00330                                 output_length += 19;
00331                                 ptr = &ptr[9];
00332                         }
00333                         else {
00334                                 sprintf(&converted_msg[output_length], "<a href=\"");
00335                                 output_length += 9;
00336                                 ptr = &ptr[9];
00337                         }
00338                 }
00343                 else if ( (brak == 0) && (alevel == 0)
00344                      && (!strncasecmp(ptr, "http://", 7))) {
00345                                 linklen = 0;
00347                                 for (i=0; i<=strlen(ptr); ++i) {
00348                                         if ((ptr[i]==0)
00349                                            ||(isspace(ptr[i]))
00350                                            ||(ptr[i]==10)
00351                                            ||(ptr[i]==13)
00352                                            ||(ptr[i]=='(')
00353                                            ||(ptr[i]==')')
00354                                            ||(ptr[i]=='<')
00355                                            ||(ptr[i]=='>')
00356                                            ||(ptr[i]=='[')
00357                                            ||(ptr[i]==']')
00358                                         ) linklen = i;
00359                                         if (linklen > 0) break;
00360                                 }
00361                                 if (linklen > 0) {
00362                                         content_length += (32 + linklen);
00363                                         if (content_length >= converted_alloc) {
00364                                                 converted_alloc += 8192;
00365                                                 converted_msg = realloc(converted_msg, converted_alloc);
00366                                                 if (converted_msg == NULL) {
00367                                                         abort();
00368                                                 }
00369                                         }
00370                                         sprintf(&converted_msg[output_length], new_window);
00371                                         output_length += strlen(new_window);
00372                                         converted_msg[output_length] = '\"';
00373                                         converted_msg[++output_length] = 0;
00374                                         for (i=0; i<linklen; ++i) {
00375                                                 converted_msg[output_length] = ptr[i];
00376                                                 converted_msg[++output_length] = 0;
00377                                         }
00378                                         sprintf(&converted_msg[output_length], "\">");
00379                                         output_length += 2;
00380                                         for (i=0; i<linklen; ++i) {
00381                                                 converted_msg[output_length] = *ptr++;
00382                                                 converted_msg[++output_length] = 0;
00383                                         }
00384                                         sprintf(&converted_msg[output_length], "</A>");
00385                                         output_length += 4;
00386                                 }
00387                 }
00388                 else {
00394                         if (*ptr == '<') ++brak;
00395                         if (*ptr == '>') --brak;
00396                         if (!strncasecmp(ptr, "</A>", 3)) --alevel;
00397                         converted_msg[output_length] = *ptr++;
00398                         converted_msg[++output_length] = 0;
00399                 }
00400         }
00401 
00407         client_write(converted_msg, output_length);
00408 
00409 BAIL:   
00410         wprintf("<br /><br />\n");
00411 
00413         if (converted_msg != NULL) free(converted_msg);
00414         if (msg != NULL) free(msg);
00415 }
00416 

Generated on Wed Jun 20 23:13:08 2007 for webcit by  doxygen 1.5.2