* Began some hacks for vCard processing
[citadel.git] / webcit / messages.c
1 /*
2  * $Id$
3  *
4  * Functions which deal with the fetching and displaying of messages.
5  *
6  */
7
8 #include <ctype.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <sys/types.h>
15 #include <sys/wait.h>
16 #include <sys/socket.h>
17 #include <limits.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <string.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include "webcit.h"
27
28
29 /*
30  * Look for URL's embedded in a buffer and make them linkable.  We use a
31  * target window in order to keep the BBS session in its own window.
32  */
33 void url(buf)
34 char buf[];
35 {
36
37         int pos;
38         int start, end;
39         char ench;
40         char urlbuf[SIZ];
41         char outbuf[1024];
42
43         start = (-1);
44         end = strlen(buf);
45         ench = 0;
46
47         for (pos = 0; pos < strlen(buf); ++pos) {
48                 if (!strncasecmp(&buf[pos], "http://", 7))
49                         start = pos;
50                 if (!strncasecmp(&buf[pos], "ftp://", 6))
51                         start = pos;
52         }
53
54         if (start < 0)
55                 return;
56
57         if ((start > 0) && (buf[start - 1] == '<'))
58                 ench = '>';
59         if ((start > 0) && (buf[start - 1] == '['))
60                 ench = ']';
61         if ((start > 0) && (buf[start - 1] == '('))
62                 ench = ')';
63         if ((start > 0) && (buf[start - 1] == '{'))
64                 ench = '}';
65
66         for (pos = strlen(buf); pos > start; --pos) {
67                 if ((buf[pos] == ' ') || (buf[pos] == ench))
68                         end = pos;
69         }
70
71         strncpy(urlbuf, &buf[start], end - start);
72         urlbuf[end - start] = 0;
73
74
75         strncpy(outbuf, buf, start);
76         sprintf(&outbuf[start], "%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
77                 LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
78         strcat(outbuf, &buf[end]);
79         if ( strlen(outbuf) < 250 )
80                 strcpy(buf, outbuf);
81 }
82
83
84 void read_message(long msgnum, int is_summary) {
85         char buf[SIZ];
86         char mime_partnum[SIZ];
87         char mime_filename[SIZ];
88         char mime_content_type[SIZ];
89         char mime_disposition[SIZ];
90         int mime_length;
91         char *mime_http = NULL;
92         char m_subject[SIZ];
93         char from[SIZ];
94         char node[SIZ];
95         char rfca[SIZ];
96         char reply_to[512];
97         char now[SIZ];
98         int format_type = 0;
99         int nhdr = 0;
100         int bq = 0;
101         char vcard_partnum[SIZ];
102         char *vcard_source = NULL;
103
104         strcpy(from, "");
105         strcpy(node, "");
106         strcpy(rfca, "");
107         strcpy(reply_to, "");
108         strcpy(vcard_partnum, "");
109
110         sprintf(buf, "MSG0 %ld", msgnum);
111         serv_puts(buf);
112         serv_gets(buf);
113         if (buf[0] != '1') {
114                 wprintf("<STRONG>ERROR:</STRONG> %s<BR>\n", &buf[4]);
115                 return;
116         }
117         wprintf("<TABLE WIDTH=100%% BORDER=0 CELLSPACING=0 CELLPADDING=1 BGCOLOR=CCCCCC><TR><TD>\n");
118         wprintf("<FONT ");
119         if (!is_summary) wprintf("SIZE=+1 ");
120         wprintf("COLOR=\"000000\"> ");
121         strcpy(m_subject, "");
122
123         while (serv_gets(buf), strncasecmp(buf, "text", 4)) {
124                 if (!strncasecmp(buf, "nhdr=yes", 8))
125                         nhdr = 1;
126                 if (nhdr == 1)
127                         buf[0] = '_';
128                 if (!strncasecmp(buf, "type=", 5))
129                         format_type = atoi(&buf[5]);
130                 if (!strncasecmp(buf, "from=", 5)) {
131                         strcpy(from, &buf[5]);
132                         wprintf("from <A HREF=\"/showuser&who=");
133                         urlescputs(from);
134                         wprintf("\">");
135                         escputs(from);
136                         wprintf("</A> ");
137                 }
138                 if (!strncasecmp(buf, "subj=", 5))
139                         strcpy(m_subject, &buf[5]);
140                 if ((!strncasecmp(buf, "hnod=", 5))
141                     && (strcasecmp(&buf[5], serv_info.serv_humannode)))
142                         wprintf("(%s) ", &buf[5]);
143                 if ((!strncasecmp(buf, "room=", 5))
144                     && (strcasecmp(&buf[5], WC->wc_roomname)))
145                         wprintf("in %s> ", &buf[5]);
146                 if (!strncasecmp(buf, "rfca=", 5)) {
147                         strcpy(rfca, &buf[5]);
148                         wprintf("&lt;");
149                         escputs(rfca);
150                         wprintf("&gt; ");
151                 }
152
153                 if (!strncasecmp(buf, "node=", 5)) {
154                         if ( ((WC->room_flags & QR_NETWORK)
155                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
156                         && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
157                         && (strlen(rfca)==0)
158                         ) {
159                                 wprintf("@%s ", &buf[5]);
160                         }
161                 }
162                 if (!strncasecmp(buf, "rcpt=", 5))
163                         wprintf("to %s ", &buf[5]);
164                 if (!strncasecmp(buf, "time=", 5)) {
165                         fmt_date(now, atol(&buf[5]));
166                         wprintf("%s ", now);
167                 }
168
169                 if (!strncasecmp(buf, "part=", 5)) {
170                         extract(mime_filename, &buf[5], 1);
171                         extract(mime_partnum, &buf[5], 2);
172                         extract(mime_disposition, &buf[5], 3);
173                         extract(mime_content_type, &buf[5], 4);
174                         mime_length = extract_int(&buf[5], 5);
175
176                         if (!strcasecmp(mime_disposition, "attachment")) {
177                                 if (mime_http == NULL) {
178                                         mime_http = malloc(512);
179                                         strcpy(mime_http, "");
180                                 }
181                                 else {
182                                         mime_http = realloc(mime_http,
183                                                 strlen(mime_http) + 512);
184                                 }
185                                 sprintf(&mime_http[strlen(mime_http)],
186                                         "<A HREF=\"/output_mimepart?"
187                                         "msgnum=%ld&partnum=%s\" "
188                                         "TARGET=\"wc.%ld.%s\">"
189                                         "<IMG SRC=\"/static/attachment.gif\" "
190                                         "BORDER=0 ALIGN=MIDDLE>\n"
191                                         "Part %s: %s (%s, %d bytes)</A><BR>\n",
192                                         msgnum, mime_partnum,
193                                         msgnum, mime_partnum,
194                                         mime_partnum, mime_filename,
195                                         mime_content_type, mime_length);
196                         }
197
198                         if ((!strcasecmp(mime_disposition, "inline"))
199                            && (!strncasecmp(mime_content_type, "image/", 6)) ){
200                                 if (mime_http == NULL) {
201                                         mime_http = malloc(512);
202                                         strcpy(mime_http, "");
203                                 }
204                                 else {
205                                         mime_http = realloc(mime_http,
206                                                 strlen(mime_http) + 512);
207                                 }
208                                 sprintf(&mime_http[strlen(mime_http)],
209                                         "<IMG SRC=\"/output_mimepart?"
210                                         "msgnum=%ld&partnum=%s\">",
211                                         msgnum, mime_partnum);
212                         }
213
214                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
215                                 strcpy(vcard_partnum, mime_partnum);
216                         }
217
218                 }
219
220         }
221
222
223         /* Generate a reply-to address */
224         if (strlen(rfca) > 0) {
225                 strcpy(reply_to, rfca);
226         }
227         else {
228                 if (strlen(node) > 0) {
229                         snprintf(reply_to, sizeof(reply_to), "%s @ %s",
230                                 from, node);
231                 }
232                 else {
233                         snprintf(reply_to, sizeof(reply_to), "%s", from);
234                 }
235         }
236
237         if (nhdr == 1)
238                 wprintf("****");
239         wprintf("</FONT></TD>");
240
241         /* begin right-hand toolbar */
242         wprintf("<TD ALIGN=RIGHT>\n"
243                 "<TABLE BORDER=0><TR>\n");
244
245         if (is_summary) {
246                 wprintf("<TD BGCOLOR=\"AAAADD\">"
247                         "<A HREF=\"/readfwd?startmsg=%ld", msgnum);
248                 wprintf("&maxmsgs=1&summary=0\">Read</A>"
249                         "</TD>\n", msgnum);
250         }
251
252         wprintf("<TD BGCOLOR=\"AAAADD\">"
253                 "<A HREF=\"/display_enter?recp=");
254         urlescputs(reply_to);
255         wprintf("\"><FONT SIZE=-1>Reply</FONT></A>"
256                 "</TD>\n", msgnum);
257
258         if (WC->is_room_aide) {
259                 wprintf("<TD BGCOLOR=\"AAAADD\">"
260                         "<A HREF=\"/confirm_move_msg"
261                         "&msgid=%ld"
262                         "\"><FONT SIZE=-1>Move</FONT></A>"
263                         "</TD>\n", msgnum);
264
265                 wprintf("<TD BGCOLOR=\"AAAADD\">"
266                         "<A HREF=\"/delete_msg"
267                         "&msgid=%ld\""
268                         "onClick=\"return confirm('Delete this message?');\""
269                         "><FONT SIZE=-1>Del</FONT></A>"
270                         "</TD>\n", msgnum);
271         }
272
273         wprintf("</TR></TABLE>\n"
274                 "</TD>\n");
275
276         /* end right-hand toolbar */
277
278
279         if (strlen(m_subject) > 0) {
280                 wprintf("<TR><TD><FONT COLOR=\"FFFFFF\">"
281                         "Subject: %s</FONT>"
282                         "</TD><TD>&nbsp;</TD></TR>\n", m_subject);
283         }
284
285         wprintf("</TR></TABLE>\n");
286
287         if (is_summary) {
288                 while (serv_gets(buf), strcmp(buf, "000")) ;
289                 return;
290         }
291
292         if (format_type == 0) {
293                 fmout(NULL);
294         } else {
295                 while (serv_gets(buf), strcmp(buf, "000")) {
296                         while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
297                                 buf[strlen(buf) - 1] = 0;
298                         if ((bq == 0) &&
299                             ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
300                                 wprintf("<FONT COLOR=\"000044\"><I>");
301                                 bq = 1;
302                         } else if ((bq == 1) &&
303                                    (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
304                                 wprintf("</FONT></I>");
305                                 bq = 0;
306                         }
307                         wprintf("<TT>");
308                         url(buf);
309                         escputs(buf);
310                         wprintf("</TT><BR>\n");
311                 }
312         }
313         wprintf("</I><BR>");
314
315         if (mime_http != NULL) {
316                 wprintf("%s", mime_http);
317                 free(mime_http);
318         }
319
320         if (strlen(vcard_partnum) > 0) {
321                 vcard_source = load_mimepart(msgnum, vcard_partnum);
322                 if (vcard_source != NULL) {
323                         wprintf("vcard object length = %d<BR>\n",
324                                 strlen(vcard_source));
325                         free(vcard_source);
326                 }
327         }
328
329 }
330
331
332
333 /* 
334  * load message pointers from the server
335  */
336 int load_msg_ptrs(servcmd)
337 char *servcmd;
338 {
339         char buf[SIZ];
340         int nummsgs;
341
342         nummsgs = 0;
343         serv_puts(servcmd);
344         serv_gets(buf);
345         if (buf[0] != '1') {
346                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
347                 return (nummsgs);
348         }
349         while (serv_gets(buf), strcmp(buf, "000")) {
350                 WC->msgarr[nummsgs] = atol(buf);
351                 ++nummsgs;
352         }
353         return (nummsgs);
354 }
355
356
357 /*
358  * command loop for reading messages
359  */
360 void readloop(char *oper)
361 {
362         char cmd[SIZ];
363         int a, b;
364         int nummsgs;
365         long startmsg;
366         int maxmsgs;
367         int num_displayed = 0;
368         int is_summary = 0;
369         int remaining_messages;
370         int lo, hi;
371         int lowest_displayed = 0;
372         int highest_displayed = 0;
373
374         startmsg = atol(bstr("startmsg"));
375         maxmsgs = atoi(bstr("maxmsgs"));
376         is_summary = atoi(bstr("summary"));
377         if (maxmsgs == 0) maxmsgs = 20;
378
379         output_headers(1);
380
381         if (!strcmp(oper, "readnew")) {
382                 strcpy(cmd, "MSGS NEW");
383         } else if (!strcmp(oper, "readold")) {
384                 strcpy(cmd, "MSGS OLD");
385         } else {
386                 strcpy(cmd, "MSGS ALL");
387         }
388
389         nummsgs = load_msg_ptrs(cmd);
390         if (nummsgs == 0) {
391                 if (!strcmp(oper, "readnew")) {
392                         wprintf("<EM>No new messages in this room.</EM>\n");
393                 } else if (!strcmp(oper, "readold")) {
394                         wprintf("<EM>No old messages in this room.</EM>\n");
395                 } else {
396                         wprintf("<EM>This room is empty.</EM>\n");
397                 }
398                 goto DONE;
399         }
400
401         if (startmsg == 0L) startmsg = WC->msgarr[0];
402         remaining_messages = 0;
403
404         for (a = 0; a < nummsgs; ++a) {
405                 if (WC->msgarr[a] >= startmsg) {
406                         ++remaining_messages;
407                 }
408         }
409
410         for (a = 0; ( (a < nummsgs) && (num_displayed < maxmsgs) ) ; ++a) {
411                 if (WC->msgarr[a] >= startmsg) {
412
413                         read_message(WC->msgarr[a], is_summary);
414                         if (lowest_displayed == 0) lowest_displayed = a;
415                         highest_displayed = a;
416                         if (is_summary) wprintf("<BR>");
417
418                         ++num_displayed;
419                         --remaining_messages;
420                 }
421         }
422
423         /* Bump these because although we're thinking in zero base, the user
424          * is a drooling idiot and is thinking in one base.
425          */
426         ++lowest_displayed;
427         ++highest_displayed;
428
429         /*
430          * If we're not currently looking at ALL requested
431          * messages, then display the selector bar
432          */
433         if (num_displayed < nummsgs) {
434
435                 wprintf("<CENTER>"
436                         "<TABLE BORDER=0 WIDTH=100%% BGCOLOR=DDDDDD><TR><TD>"
437                         "You are reading #%d-%d of %d messages.</TD>\n"
438                         "<TD ALIGN=RIGHT><FONT SIZE=+1>",
439                         lowest_displayed, highest_displayed, nummsgs);
440
441                 for (b=0; b<nummsgs; b = b + maxmsgs) {
442                 lo = b+1;
443                 hi = b+maxmsgs+1;
444                 if (hi > nummsgs) hi = nummsgs;
445                         if (WC->msgarr[b] != startmsg) {
446                                 wprintf("<A HREF=\"/%s"
447                                         "?startmsg=%ld"
448                                         "&maxmsgs=%d"
449                                         "&summary=%d\">"
450                                         "%d-%d</A> \n",
451                                                 oper,
452                                                 WC->msgarr[b],
453                                                 maxmsgs,
454                                                 is_summary,
455                                                 lo, hi);
456                         }
457                         else {
458                                 wprintf("%d-%d \n", lo, hi);
459                         }
460
461                 }
462                 wprintf("<A HREF=\"/%s?startmsg=%ld"
463                         "&maxmsgs=999999&summary=%d\">"
464                         "ALL"
465                         "</A>&nbsp;&nbsp;&nbsp;",
466                         oper,
467                         WC->msgarr[0], is_summary);
468                 wprintf("</TD></TR></TABLE></CENTER><HR>\n");
469         }
470
471 DONE:   wDumpContent(1);
472 }
473
474
475
476
477 /*
478  * Post message (or don't post message)
479  *
480  * Note regarding the "dont_post" variable:
481  * A random value (actually, it's just a timestamp) is inserted as a hidden
482  * field called "postseq" when the display_enter page is generated.  This
483  * value is checked when posting, using the static variable dont_post.  If a
484  * user attempts to post twice using the same dont_post value, the message is
485  * discarded.  This prevents the accidental double-saving of the same message
486  * if the user happens to click the browser "back" button.
487  */
488 void post_message(void)
489 {
490         char buf[SIZ];
491         static long dont_post = (-1L);
492
493         output_headers(1);
494
495         wprintf("<FONT FACE=\"Arial,Helvetica,sans-serif\">");
496         strcpy(buf, bstr("sc"));
497         if (strcasecmp(buf, "Save message")) {
498                 wprintf("Cancelled.  Message was not posted.<BR>\n");
499         } else if (atol(bstr("postseq")) == dont_post) {
500                 wprintf("Automatically cancelled because you have already "
501                         "saved this message.<BR>\n");
502         } else {
503                 sprintf(buf, "ENT0 1|%s|0|0", bstr("recp"));
504                 serv_puts(buf);
505                 serv_gets(buf);
506                 if (buf[0] == '4') {
507                         text_to_server(bstr("msgtext"));
508                         serv_puts("000");
509                         wprintf("Message has been posted.<BR>\n");
510                         dont_post = atol(bstr("postseq"));
511                 } else {
512                         wprintf("<EM>%s</EM><BR>\n", &buf[4]);
513                 }
514         }
515
516         wDumpContent(1);
517 }
518
519
520
521
522 /*
523  * display the message entry screen
524  */
525 void display_enter(void)
526 {
527         char buf[SIZ];
528         long now;
529         struct tm *tm;
530
531         output_headers(1);
532
533         wprintf("<FONT FACE=\"Arial,Helvetica,sans-serif\">");
534
535         sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
536         serv_puts(buf);
537         serv_gets(buf);
538
539         if (!strncmp(buf, "570", 3)) {
540                 if (strlen(bstr("recp")) > 0) {
541                         wprintf("<EM>%s</EM><BR>\n", &buf[4]);
542                 }
543                 do_template("prompt_for_recipient.html");
544                 goto DONE;
545         }
546         if (buf[0] != '2') {
547                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
548                 goto DONE;
549         }
550
551         now = time(NULL);
552         tm = (struct tm *) localtime(&now);
553         strcpy(buf, (char *) asctime(tm));
554         buf[strlen(buf) - 1] = 0;
555         strcpy(&buf[16], &buf[19]);
556         wprintf("</CENTER><FONT COLOR=\"440000\">\n"
557                 "<IMG SRC=\"static/enter.gif\" ALIGN=MIDDLE ALT=\" \" "
558                 "onLoad=\"document.enterform.msgtext.focus();\" >");
559         wprintf("<B> %s ", &buf[4]);
560         wprintf("from %s ", WC->wc_username);
561         if (strlen(bstr("recp")) > 0)
562                 wprintf("to %s ", bstr("recp"));
563         wprintf("in %s&gt; ", WC->wc_roomname);
564         wprintf("</B></FONT><BR><CENTER>\n");
565
566         wprintf("<FORM METHOD=\"POST\" ACTION=\"/post\" "
567                 "NAME=\"enterform\">\n");
568         wprintf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"%s\">\n",
569                 bstr("recp"));
570         wprintf("<INPUT TYPE=\"hidden\" NAME=\"postseq\" VALUE=\"%ld\">\n",
571                 now);
572         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save message\">"
573                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\"><BR>\n");
574
575         wprintf("<TEXTAREA NAME=\"msgtext\" wrap=soft ROWS=30 COLS=80 "
576                 "WIDTH=80></TEXTAREA><P>\n");
577
578         wprintf("</FORM></CENTER>\n");
579 DONE:   wDumpContent(1);
580         wprintf("</FONT>");
581 }
582
583
584
585
586
587
588
589
590 void delete_msg(void)
591 {
592         long msgid;
593         char buf[SIZ];
594
595         msgid = atol(bstr("msgid"));
596
597         output_headers(1);
598
599         sprintf(buf, "DELE %ld", msgid);
600         serv_puts(buf);
601         serv_gets(buf);
602         wprintf("<EM>%s</EM><BR>\n", &buf[4]);
603
604         wDumpContent(1);
605 }
606
607
608
609
610 /*
611  * Confirm move of a message
612  */
613 void confirm_move_msg(void)
614 {
615         long msgid;
616         char buf[SIZ];
617         char targ[SIZ];
618
619         msgid = atol(bstr("msgid"));
620
621         output_headers(1);
622
623         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=770000><TR><TD>");
624         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
625         wprintf("<B>Confirm move of message</B>\n");
626         wprintf("</FONT></TD></TR></TABLE>\n");
627
628         wprintf("<CENTER>");
629
630         wprintf("Please select the room to which you would like this message moved:<BR>\n");
631
632         wprintf("<FORM METHOD=\"POST\" ACTION=\"/move_msg\">\n");
633         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
634                 bstr("msgid"));
635
636
637         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
638         serv_puts("LKRA");
639         serv_gets(buf);
640         if (buf[0] == '1') {
641                 while (serv_gets(buf), strcmp(buf, "000")) {
642                         extract(targ, buf, 0);
643                         wprintf("<OPTION>");
644                         escputs(targ);
645                         wprintf("\n");
646                 }
647         }
648         wprintf("</SELECT>\n");
649         wprintf("<BR>\n");
650
651         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
652         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
653         wprintf("</FORM></CENTER>\n");
654
655         wprintf("</CENTER>\n");
656         wDumpContent(1);
657 }
658
659
660
661 void move_msg(void)
662 {
663         long msgid;
664         char buf[SIZ];
665
666         msgid = atol(bstr("msgid"));
667
668         output_headers(1);
669
670         if (!strcasecmp(bstr("yesno"), "Move")) {
671                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
672                 serv_puts(buf);
673                 serv_gets(buf);
674                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
675         } else {
676                 wprintf("<EM>Message not deleted.</EM><BR>\n");
677         }
678
679         wDumpContent(1);
680 }
681
682
683