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