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