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