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