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