* templatized the "prompt for recipient" screen
[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
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[SIZ];
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[SIZ];
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[SIZ];
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  * display the message entry screen
507  */
508 void display_enter(void)
509 {
510         char buf[SIZ];
511         long now;
512         struct tm *tm;
513
514         output_headers(1);
515
516         wprintf("<FONT FACE=\"Arial,Helvetica,sans-serif\">");
517
518         sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
519         serv_puts(buf);
520         serv_gets(buf);
521
522         if (!strncmp(buf, "570", 3)) {
523                 if (strlen(bstr("recp")) > 0) {
524                         wprintf("<EM>%s</EM><BR>\n", &buf[4]);
525                 }
526                 do_template("prompt_for_recipient.html");
527                 goto DONE;
528         }
529         if (buf[0] != '2') {
530                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
531                 goto DONE;
532         }
533
534         now = time(NULL);
535         tm = (struct tm *) localtime(&now);
536         strcpy(buf, (char *) asctime(tm));
537         buf[strlen(buf) - 1] = 0;
538         strcpy(&buf[16], &buf[19]);
539         wprintf("</CENTER><FONT COLOR=\"440000\">\n"
540                 "<IMG SRC=\"static/enter.gif\" ALIGN=MIDDLE ALT=\" \" "
541                 "onLoad=\"document.enterform.msgtext.focus();\" >");
542         wprintf("<B> %s ", &buf[4]);
543         wprintf("from %s ", WC->wc_username);
544         if (strlen(bstr("recp")) > 0)
545                 wprintf("to %s ", bstr("recp"));
546         wprintf("in %s&gt; ", WC->wc_roomname);
547         wprintf("</B></FONT><BR><CENTER>\n");
548
549         wprintf("<FORM METHOD=\"POST\" ACTION=\"/post\" "
550                 "NAME=\"enterform\">\n");
551         wprintf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"%s\">\n",
552                 bstr("recp"));
553         wprintf("<INPUT TYPE=\"hidden\" NAME=\"postseq\" VALUE=\"%ld\">\n",
554                 now);
555         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save message\">"
556                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\"><BR>\n");
557
558         wprintf("<TEXTAREA NAME=\"msgtext\" wrap=soft ROWS=30 COLS=80 "
559                 "WIDTH=80></TEXTAREA><P>\n");
560
561         wprintf("</FORM></CENTER>\n");
562 DONE:   wDumpContent(1);
563         wprintf("</FONT>");
564 }
565
566
567
568
569
570
571
572
573 void delete_msg(void)
574 {
575         long msgid;
576         char buf[SIZ];
577
578         msgid = atol(bstr("msgid"));
579
580         output_headers(1);
581
582         sprintf(buf, "DELE %ld", msgid);
583         serv_puts(buf);
584         serv_gets(buf);
585         wprintf("<EM>%s</EM><BR>\n", &buf[4]);
586
587         wDumpContent(1);
588 }
589
590
591
592
593 /*
594  * Confirm move of a message
595  */
596 void confirm_move_msg(void)
597 {
598         long msgid;
599         char buf[SIZ];
600         char targ[SIZ];
601
602         msgid = atol(bstr("msgid"));
603
604         output_headers(1);
605
606         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=770000><TR><TD>");
607         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
608         wprintf("<B>Confirm move of message</B>\n");
609         wprintf("</FONT></TD></TR></TABLE>\n");
610
611         wprintf("<CENTER>");
612
613         wprintf("Please select the room to which you would like this message moved:<BR>\n");
614
615         wprintf("<FORM METHOD=\"POST\" ACTION=\"/move_msg\">\n");
616         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
617                 bstr("msgid"));
618
619
620         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
621         serv_puts("LKRA");
622         serv_gets(buf);
623         if (buf[0] == '1') {
624                 while (serv_gets(buf), strcmp(buf, "000")) {
625                         extract(targ, buf, 0);
626                         wprintf("<OPTION>");
627                         escputs(targ);
628                         wprintf("\n");
629                 }
630         }
631         wprintf("</SELECT>\n");
632         wprintf("<BR>\n");
633
634         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
635         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
636         wprintf("</FORM></CENTER>\n");
637
638         wprintf("</CENTER>\n");
639         wDumpContent(1);
640 }
641
642
643
644 void move_msg(void)
645 {
646         long msgid;
647         char buf[SIZ];
648
649         msgid = atol(bstr("msgid"));
650
651         output_headers(1);
652
653         if (!strcasecmp(bstr("yesno"), "Move")) {
654                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
655                 serv_puts(buf);
656                 serv_gets(buf);
657                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
658         } else {
659                 wprintf("<EM>Message not deleted.</EM><BR>\n");
660         }
661
662         wDumpContent(1);
663 }
664
665
666