33eeedf01b6172290493d68a51fcad20a5937f4d
[citadel.git] / webcit / html2html.c
1 /*
2  * Output an HTML message, modifying it slightly to make sure it plays nice
3  * with the rest of our web framework.
4  *
5  * Copyright (c) 2005-2010 by the citadel.org team
6  *
7  * This program is open source software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  */
20
21 #include "webcit.h"
22 #include "webserver.h"
23
24
25 /*
26  * Strip surrounding single or double quotes from a string.
27  */
28 void stripquotes(char *s)
29 {
30         int len;
31
32         if (!s) return;
33
34         len = strlen(s);
35         if (len < 2) return;
36
37         if ( ( (s[0] == '\"') && (s[len-1] == '\"') ) || ( (s[0] == '\'') && (s[len-1] == '\'') ) ) {
38                 s[len-1] = 0;
39                 strcpy(s, &s[1]);
40         }
41 }
42
43
44 /*
45  * Check to see if a META tag has overridden the declared MIME character set.
46  *
47  * charset              Character set name (left unchanged if we don't do anything)
48  * meta_http_equiv      Content of the "http-equiv" portion of the META tag
49  * meta_content         Content of the "content" portion of the META tag
50  */
51 void extract_charset_from_meta(char *charset, char *meta_http_equiv, char *meta_content)
52 {
53         char *ptr;
54         char buf[64];
55
56         if (!charset) return;
57         if (!meta_http_equiv) return;
58         if (!meta_content) return;
59
60
61         if (strcasecmp(meta_http_equiv, "Content-type")) return;
62
63         ptr = strchr(meta_content, ';');
64         if (!ptr) return;
65
66         safestrncpy(buf, ++ptr, sizeof buf);
67         striplt(buf);
68         if (!strncasecmp(buf, "charset=", 8)) {
69                 strcpy(charset, &buf[8]);
70
71                 /*
72                  * The brain-damaged webmail program in Microsoft Exchange declares
73                  * a charset of "unicode" when they really mean "UTF-8".  GNU iconv
74                  * treats "unicode" as an alias for "UTF-16" so we have to manually
75                  * fix this here, otherwise messages generated in Exchange webmail
76                  * show up as a big pile of weird characters.
77                  */
78                 if (!strcasecmp(charset, "unicode")) {
79                         strcpy(charset, "UTF-8");
80                 }
81
82                 /* Remove wandering punctuation */
83                 if ((ptr=strchr(charset, '\"'))) *ptr = 0;
84                 striplt(charset);
85         }
86 }
87
88
89
90 /*
91  * Sanitize and enhance an HTML message for display.
92  * Also convert weird character sets to UTF-8 if necessary.
93  * Also fixup img src="cid:..." type inline images to fetch the image
94  *
95  */
96 void output_html(const char *supplied_charset, int treat_as_wiki, int msgnum, StrBuf *Source, StrBuf *Target) {
97         char buf[SIZ];
98         char *msg;
99         char *ptr;
100         char *msgstart;
101         char *msgend;
102         StrBuf *converted_msg;
103         int buffer_length = 1;
104         int line_length = 0;
105         int content_length = 0;
106         char new_window[SIZ];
107         int brak = 0;
108         int alevel = 0;
109         int scriptlevel = 0;
110         int script_start_pos = (-1);
111         int i;
112         int linklen;
113         char charset[128];
114         StrBuf *BodyArea = NULL;
115 #ifdef HAVE_ICONV
116         iconv_t ic = (iconv_t)(-1) ;
117         char *ibuf;                   /* Buffer of characters to be converted */
118         char *obuf;                   /* Buffer for converted characters      */
119         size_t ibuflen;               /* Length of input buffer               */
120         size_t obuflen;               /* Length of output buffer              */
121         char *osav;                   /* Saved pointer to output buffer       */
122 #endif
123         if (Target == NULL)
124                 Target = WC->WBuf;
125
126         safestrncpy(charset, supplied_charset, sizeof charset);
127         msg = strdup("");
128         sprintf(new_window, "<a target=\"%s\" href=", TARGET);
129
130         if (Source == NULL) while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
131                 line_length = strlen(buf);
132                 buffer_length = content_length + line_length + 2;
133                 ptr = realloc(msg, buffer_length);
134                 if (ptr == NULL) {
135                         StrBufAppendPrintf(Target, "<b>");
136                         StrBufAppendPrintf(Target, _("realloc() error! couldn't get %d bytes: %s"),
137                                         buffer_length + 1,
138                                         strerror(errno));
139                         StrBufAppendPrintf(Target, "</b><br><br>\n");
140                         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
141                                 /** flush */
142                         }
143                         free(msg);
144                         return;
145                 }
146                 msg = ptr;
147                 strcpy(&msg[content_length], buf);
148                 content_length += line_length;
149                 strcpy(&msg[content_length], "\n");
150                 content_length += 1;
151         }
152         else {
153                 content_length = StrLength(Source);
154                 free(msg);
155                 msg = (char*) ChrPtr(Source);/* TODO: remove cast */
156                 buffer_length = content_length;
157         }
158
159         /** Do a first pass to isolate the message body */
160         ptr = msg + 1;
161         msgstart = msg;
162         msgend = &msg[content_length];
163
164         while (ptr < msgend) {
165
166                 /** Advance to next tag */
167                 ptr = strchr(ptr, '<');
168                 if ((ptr == NULL) || (ptr >= msgend)) break;
169                 ++ptr;
170                 if ((ptr == NULL) || (ptr >= msgend)) break;
171
172                 /*
173                  *  Look for META tags.  Some messages (particularly in
174                  *  Asian locales) illegally declare a message's character
175                  *  set in the HTML instead of in the MIME headers.  This
176                  *  is wrong but we have to work around it anyway.
177                  */
178                 if (!strncasecmp(ptr, "META", 4)) {
179
180                         char *meta_start;
181                         char *meta_end;
182                         int meta_length;
183                         char *meta;
184                         char *meta_http_equiv;
185                         char *meta_content;
186                         char *spaceptr;
187
188                         meta_start = &ptr[4];
189                         meta_end = strchr(ptr, '>');
190                         if ((meta_end != NULL) && (meta_end <= msgend)) {
191                                 meta_length = meta_end - meta_start + 1;
192                                 meta = malloc(meta_length + 1);
193                                 safestrncpy(meta, meta_start, meta_length);
194                                 meta[meta_length] = 0;
195                                 striplt(meta);
196                                 if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
197                                         meta_http_equiv = strdup(&meta[11]);
198                                         spaceptr = strchr(meta_http_equiv, ' ');
199                                         if (spaceptr != NULL) {
200                                                 *spaceptr = 0;
201                                                 meta_content = strdup(++spaceptr);
202                                                 if (!strncasecmp(meta_content, "content=", 8)) {
203                                                         strcpy(meta_content, &meta_content[8]);
204                                                         stripquotes(meta_http_equiv);
205                                                         stripquotes(meta_content);
206                                                         extract_charset_from_meta(charset,
207                                                                         meta_http_equiv, meta_content);
208                                                 }
209                                                 free(meta_content);
210                                         }
211                                         free(meta_http_equiv);
212                                 }
213                                 free(meta);
214                         }
215                 }
216
217                 /*
218                  * Any of these tags cause everything up to and including
219                  * the tag to be removed.
220                  */     
221                 if ( (!strncasecmp(ptr, "HTML", 4))
222                                 ||(!strncasecmp(ptr, "HEAD", 4))
223                                 ||(!strncasecmp(ptr, "/HEAD", 5))
224                                 ||(!strncasecmp(ptr, "BODY", 4)) ) {
225                         char *pBody = NULL;
226
227                         if (!strncasecmp(ptr, "BODY", 4)) {
228                                 pBody = ptr;
229                         }
230                         ptr = strchr(ptr, '>');
231                         if ((ptr == NULL) || (ptr >= msgend)) break;
232                         if ((pBody != NULL) && (ptr - pBody > 4)) {
233                                 char* src;
234                                 char *cid_start, *cid_end;
235
236                                 *ptr = '\0';
237                                 pBody += 4; 
238                                 while ((isspace(*pBody)) && (pBody < ptr))
239                                         pBody ++;
240                                 BodyArea = NewStrBufPlain(NULL,  ptr - pBody);
241
242                                 if (pBody < ptr) {
243                                         src = strstr(pBody, "cid:");
244                                         if (src) {
245                                                 cid_start = src + 4;
246                                                 cid_end = cid_start;
247                                                 while ((*cid_end != '"') && 
248                                                                 !isspace(*cid_end) &&
249                                                                 (cid_end < ptr))
250                                                         cid_end ++;
251
252                                                 /* copy tag and attributes up to src="cid: */
253                                                 StrBufAppendBufPlain(BodyArea, pBody, src - pBody, 0);
254
255                                                 /* add in /webcit/mimepart/<msgno>/CID/ 
256                                                    trailing / stops dumb URL filters getting excited */
257                                                 StrBufAppendPrintf(BodyArea,
258                                                                 "/webcit/mimepart/%d/",msgnum);
259                                                 StrBufAppendBufPlain(BodyArea, cid_start, cid_end - cid_start, 0);
260
261                                                 if (ptr - cid_end > 0)
262                                                         StrBufAppendBufPlain(BodyArea, 
263                                                                         cid_end + 1, 
264                                                                         ptr - cid_end, 0);
265                                         }
266                                         else 
267                                                 StrBufAppendBufPlain(BodyArea, pBody, ptr - pBody, 0);
268                                 }
269                                 *ptr = '>';
270                         }
271                         ++ptr;
272                         if ((ptr == NULL) || (ptr >= msgend)) break;
273                         msgstart = ptr;
274                 }
275
276                 /*
277                  * Any of these tags cause everything including and following
278                  * the tag to be removed.
279                  */
280                 if ( (!strncasecmp(ptr, "/HTML", 5))
281                                 ||(!strncasecmp(ptr, "/BODY", 5)) ) {
282                         --ptr;
283                         msgend = ptr;
284                         strcpy(ptr, "");
285
286                 }
287
288                 ++ptr;
289         }
290         if (msgstart > msg) {
291                 strcpy(msg, msgstart);
292         }
293
294         /* Now go through the message, parsing tags as necessary. */
295         converted_msg = NewStrBufPlain(NULL, content_length + 8192);
296
297
298         /** Convert foreign character sets to UTF-8 if necessary. */
299 #ifdef HAVE_ICONV
300         if ( (strcasecmp(charset, "us-ascii"))
301                         && (strcasecmp(charset, "UTF-8"))
302                         && (strcasecmp(charset, ""))
303            ) {
304                 lprintf(9, "Converting %s to UTF-8\n", charset);
305                 ctdl_iconv_open("UTF-8", charset, &ic);
306                 if (ic == (iconv_t)(-1) ) {
307                         lprintf(5, "%s:%d iconv_open() failed: %s\n",
308                                         __FILE__, __LINE__, strerror(errno));
309                 }
310         }
311         if  (Source == NULL) {
312                 if (ic != (iconv_t)(-1) ) {
313                         ibuf = msg;
314                         ibuflen = content_length;
315                         obuflen = content_length + (content_length / 2) ;
316                         obuf = (char *) malloc(obuflen);
317                         osav = obuf;
318                         iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
319                         content_length = content_length + (content_length / 2) - obuflen;
320                         osav[content_length] = 0;
321                         free(msg);
322                         msg = osav;
323                         iconv_close(ic);
324                 }
325         }
326         else {
327                 if (ic != (iconv_t)(-1) ) {
328                         StrBuf *Buf = NewStrBufPlain(NULL, StrLength(Source) + 8096);;
329                         StrBufConvert(Source, Buf, &ic);
330                         FreeStrBuf(&Buf);
331                         iconv_close(ic);
332                         msg = (char*)ChrPtr(Source); /* TODO: get rid of this. */
333                 }
334         }
335
336 #endif
337
338         /*
339          *      At this point, the message has been stripped down to
340          *      only the content inside the <BODY></BODY> tags, and has
341          *      been converted to UTF-8 if it was originally in a foreign
342          *      character set.  The text is also guaranteed to be null
343          *      terminated now.
344          */
345
346         if (converted_msg == NULL) {
347                 StrBufAppendPrintf(Target, "Error %d: %s<br>%s:%d", errno, strerror(errno), __FILE__, __LINE__);
348                 goto BAIL;
349         }
350
351         if (BodyArea != NULL) {
352                 StrBufAppendBufPlain(converted_msg, HKEY("<table "), 0);  
353                 StrBufAppendBuf(converted_msg, BodyArea, 0);
354                 StrBufAppendBufPlain(converted_msg, HKEY(" width=\"100%\"><tr><td>"), 0);
355         }
356         ptr = msg;
357         msgend = strchr(msg, 0);
358         while (ptr < msgend) {
359
360                 /** Try to sanitize the html of any rogue scripts */
361                 if (!strncasecmp(ptr, "<script", 7)) {
362                         if (scriptlevel == 0) {
363                                 script_start_pos = StrLength(converted_msg);
364                         }
365                         ++scriptlevel;
366                 }
367                 if (!strncasecmp(ptr, "</script", 8)) {
368                         --scriptlevel;
369                 }
370
371                 /**
372                  * Change mailto: links to WebCit mail, by replacing the
373                  * link with one that points back to our mail room.  Due to
374                  * the way we parse URL's, it'll even handle mailto: links
375                  * that have "?subject=" in them.
376                  */
377                 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
378                         content_length += 64;
379                         StrBufAppendPrintf(converted_msg,
380                                         "<a href=\"display_enter?force_room=_MAIL_?recp=");
381                         ptr = &ptr[16];
382                         ++alevel;
383                         ++brak;
384                 }
385                 /** Make external links open in a separate window */
386                 else if (!strncasecmp(ptr, "<a href=\"", 9)) {
387                         ++alevel;
388                         ++brak;
389                         if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
390                                         &&  ((strchr(ptr, '/') < strchr(ptr, '>'))) 
391                            ) {
392                                 /* open external links to new window */
393                                 StrBufAppendPrintf(converted_msg, new_window);
394                                 ptr = &ptr[8];
395                         }
396                         else if ( (treat_as_wiki) && (strncasecmp(ptr, "<a href=\"wiki?", 14)) ) {
397                                 content_length += 64;
398                                 StrBufAppendPrintf(converted_msg, "<a href=\"wiki?page=");
399                                 ptr = &ptr[9];
400                         }
401                         else {
402                                 StrBufAppendPrintf(converted_msg, "<a href=\"");
403                                 ptr = &ptr[9];
404                         }
405                 }
406                 /** Fixup <img src="cid:... ...> to fetch the mime part */
407                 else if (!strncasecmp(ptr, "<img ", 5)) {
408                         char *cid_start, *cid_end;
409                         char* tag_end=strchr(ptr,'>');
410                         char* src;
411                         /* FIXME - handle this situation (maybe someone opened an <img cid... 
412                          * and then ended the message)
413                          */
414                         if (!tag_end) {
415                                 lprintf(9, "tag_end is null and ptr is:\n");
416                                 lprintf(9, "%s\n", ptr);
417                                 lprintf(9, "Theoretical bytes remaining: %d\n", msgend - ptr);
418                         }
419
420                         src=strstr(ptr, "src=\"cid:");
421                         ++brak;
422
423                         if (src
424                             && isspace(*(src-1))
425                                 && tag_end
426                                 && (cid_start=strchr(src,':'))
427                                 && (cid_end=strchr(cid_start,'"'))
428                                 && (cid_end < tag_end)
429                         ) {
430                                 /* copy tag and attributes up to src="cid: */
431                                 StrBufAppendBufPlain(converted_msg, ptr, src - ptr, 0);
432                                 cid_start++;
433
434                                 /* add in /webcit/mimepart/<msgno>/CID/ 
435                                    trailing / stops dumb URL filters getting excited */
436                                 StrBufAppendPrintf(converted_msg,
437                                                 " src=\"/webcit/mimepart/%d/",msgnum);
438                                 StrBufAppendBufPlain(converted_msg, cid_start, cid_end - cid_start, 0);
439                                 StrBufAppendBufPlain(converted_msg, "/\"", -1, 0);
440
441                                 ptr = cid_end+1;
442                         }
443                         StrBufAppendBufPlain(converted_msg, ptr, tag_end - ptr, 0);
444                         ptr = tag_end;
445                 }
446
447                 /**
448                  * Turn anything that looks like a URL into a real link, as long
449                  * as it's not inside a tag already
450                  */
451                 else if ( (brak == 0) && (alevel == 0)
452                      && (!strncasecmp(ptr, "http://", 7))) {
453                                 /** Find the end of the link */
454                                 int strlenptr;
455                                 linklen = 0;
456                                 
457                                 strlenptr = strlen(ptr);
458                                 for (i=0; i<=strlenptr; ++i) {
459                                         if ((ptr[i]==0)
460                                            ||(isspace(ptr[i]))
461                                            ||(ptr[i]==10)
462                                            ||(ptr[i]==13)
463                                            ||(ptr[i]=='(')
464                                            ||(ptr[i]==')')
465                                            ||(ptr[i]=='<')
466                                            ||(ptr[i]=='>')
467                                            ||(ptr[i]=='[')
468                                            ||(ptr[i]==']')
469                                            ||(ptr[i]=='"')
470                                            ||(ptr[i]=='\'')
471                                         ) linklen = i;
472                                         /* did s.b. send us an entity? */
473                                         if (ptr[i] == '&') {
474                                                 if ((ptr[i+2] ==';') ||
475                                                     (ptr[i+3] ==';') ||
476                                                     (ptr[i+5] ==';') ||
477                                                     (ptr[i+6] ==';') ||
478                                                     (ptr[i+7] ==';'))
479                                                         linklen = i;
480                                         }
481                                         if (linklen > 0) break;
482                                 }
483                                 if (linklen > 0) {
484                                         char *ltreviewptr;
485                                         char *nbspreviewptr;
486                                         char linkedchar;
487                                         int len;
488                                         
489                                         len = linklen;
490                                         linkedchar = ptr[len];
491                                         ptr[len] = '\0';
492                                         /* spot for some subject strings tinymce tends to give us. */
493                                         ltreviewptr = strchr(ptr, '<');
494                                         if (ltreviewptr != NULL) {
495                                                 *ltreviewptr = '\0';
496                                                 linklen = ltreviewptr - ptr;
497                                         }
498
499                                         nbspreviewptr = strstr(ptr, "&nbsp;");
500                                         if (nbspreviewptr != NULL) {
501                                                 /* nbspreviewptr = '\0'; */
502                                                 linklen = nbspreviewptr - ptr;
503                                         }
504                                         if (ltreviewptr != 0)
505                                                 *ltreviewptr = '<';
506
507                                         ptr[len] = linkedchar;
508
509                                         content_length += (32 + linklen);
510                                         StrBufAppendPrintf(converted_msg, "%s\"", new_window);
511                                         StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
512                                         StrBufAppendPrintf(converted_msg, "\">");
513                                         StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
514                                         ptr += linklen;
515                                         StrBufAppendPrintf(converted_msg, "</A>");
516                                 }
517                 }
518                 else {
519                         StrBufAppendBufPlain(converted_msg, ptr, 1, 0);
520                         ptr++;
521                 }
522
523
524                 if ((ptr >= msg) && (ptr <= msgend)) {
525                         /*
526                          * We need to know when we're inside a tag,
527                          * so we don't turn things that look like URL's into
528                          * links, when they're already links - or image sources.
529                          */
530                         if ((ptr > msg) && (*(ptr-1) == '<')) {
531                                 ++brak;
532                         }
533                         if ((ptr > msg) && (*(ptr-1) == '>')) {
534                                 --brak;
535                                 if ((scriptlevel == 0) && (script_start_pos >= 0)) {
536                                         StrBufCutRight(converted_msg, StrLength(converted_msg) - script_start_pos);
537                                         script_start_pos = (-1);
538                                 }
539                         }
540                         if (!strncasecmp(ptr, "</A>", 3)) --alevel;
541                 }
542         }
543
544         if (BodyArea != NULL) {
545                 StrBufAppendBufPlain(converted_msg, HKEY("</td></tr></table>"), 0);  
546                 FreeStrBuf(&BodyArea);
547         }
548
549         /**     uncomment these two lines to override conversion        */
550         /**     memcpy(converted_msg, msg, content_length);             */
551         /**     output_length = content_length;                         */
552
553         /** Output our big pile of markup */
554         StrBufAppendBuf(Target, converted_msg, 0);
555
556 BAIL:   /** A little trailing vertical whitespace... */
557         StrBufAppendPrintf(Target, "<br><br>\n");
558
559         /** Now give back the memory */
560         FreeStrBuf(&converted_msg);
561         if ((msg != NULL) && (Source == NULL)) free(msg);
562 }
563
564
565
566
567
568
569 /*
570  * Look for URL's embedded in a buffer and make them linkable.  We use a
571  * target window in order to keep the Citadel session in its own window.
572  */
573 void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf)
574 {
575         int len, UrlLen, Offset, TrailerLen;
576         const char *start, *end, *pos;
577         
578         FlushStrBuf(Target);
579
580         start = NULL;
581         len = StrLength(Source);
582         end = ChrPtr(Source) + len;
583         for (pos = ChrPtr(Source); (pos < end) && (start == NULL); ++pos) {
584                 if (!strncasecmp(pos, "http://", 7))
585                         start = pos;
586                 else if (!strncasecmp(pos, "ftp://", 6))
587                         start = pos;
588         }
589
590         if (start == NULL) {
591                 StrBufAppendBuf(Target, Source, 0);
592                 return;
593         }
594         FlushStrBuf(WrkBuf);
595
596         for (pos = ChrPtr(Source) + len; pos > start; --pos) {
597                 if (  (!isprint(*pos))
598                    || (isspace(*pos))
599                    || (*pos == '{')
600                    || (*pos == '}')
601                    || (*pos == '|')
602                    || (*pos == '\\')
603                    || (*pos == '^')
604                    || (*pos == '[')
605                    || (*pos == ']')
606                    || (*pos == '`')
607                    || (*pos == '<')
608                    || (*pos == '>')
609                    || (*pos == '(')
610                    || (*pos == ')')
611                 ) {
612                         end = pos;
613                 }
614         }
615         
616         UrlLen = end - start;
617         StrBufAppendBufPlain(WrkBuf, start, UrlLen, 0);
618
619         Offset = start - ChrPtr(Source);
620         if (Offset != 0)
621                 StrBufAppendBufPlain(Target, ChrPtr(Source), Offset, 0);
622         StrBufAppendPrintf(Target, "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
623                            LB, QU, ChrPtr(WrkBuf), QU, QU, TARGET, 
624                            QU, RB, ChrPtr(WrkBuf), LB, RB);
625
626         TrailerLen = StrLength(Source) - (end - ChrPtr(Source));
627         if (TrailerLen > 0)
628                 StrBufAppendBufPlain(Target, end, TrailerLen, 0);
629 }
630
631
632 void url(char *buf, size_t bufsize)
633 {
634         int len, UrlLen, Offset, TrailerLen, outpos;
635         char *start, *end, *pos;
636         char urlbuf[SIZ];
637         char outbuf[SIZ];
638
639         start = NULL;
640         len = strlen(buf);
641         if (len > bufsize) {
642                 lprintf(1, "URL: content longer than buffer!");
643                 return;
644         }
645         end = buf + len;
646         for (pos = buf; (pos < end) && (start == NULL); ++pos) {
647                 if (!strncasecmp(pos, "http://", 7))
648                         start = pos;
649                 if (!strncasecmp(pos, "ftp://", 6))
650                         start = pos;
651         }
652
653         if (start == NULL)
654                 return;
655
656         for (pos = buf+len; pos > start; --pos) {
657                 if (  (!isprint(*pos))
658                    || (isspace(*pos))
659                    || (*pos == '{')
660                    || (*pos == '}')
661                    || (*pos == '|')
662                    || (*pos == '\\')
663                    || (*pos == '^')
664                    || (*pos == '[')
665                    || (*pos == ']')
666                    || (*pos == '`')
667                    || (*pos == '<')
668                    || (*pos == '>')
669                    || (*pos == '(')
670                    || (*pos == ')')
671                 ) {
672                         end = pos;
673                 }
674         }
675         
676         UrlLen = end - start;
677         if (UrlLen > sizeof(urlbuf)){
678                 lprintf(1, "URL: content longer than buffer!");
679                 return;
680         }
681         memcpy(urlbuf, start, UrlLen);
682         urlbuf[UrlLen] = '\0';
683
684         Offset = start - buf;
685         if ((Offset != 0) && (Offset < sizeof(outbuf)))
686                 memcpy(outbuf, buf, Offset);
687         outpos = snprintf(&outbuf[Offset], sizeof(outbuf) - Offset,  
688                           "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
689                           LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
690         if (outpos >= sizeof(outbuf) - Offset) {
691                 lprintf(1, "URL: content longer than buffer!");
692                 return;
693         }
694
695         TrailerLen = len - (end - start);
696         if (TrailerLen > 0)
697                 memcpy(outbuf + Offset + outpos, end, TrailerLen);
698         if (Offset + outpos + TrailerLen > bufsize) {
699                 lprintf(1, "URL: content longer than buffer!");
700                 return;
701         }
702         memcpy (buf, outbuf, Offset + outpos + TrailerLen);
703         *(buf + Offset + outpos + TrailerLen) = '\0';
704 }
705