]> code.citadel.org Git - citadel.git/blob - webcit/messages.c
* Moved a bunch of other global variables to per-session variables
[citadel.git] / webcit / messages.c
1
2
3
4
5 #include <ctype.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <signal.h>
11 #include <sys/types.h>
12 #include <sys/wait.h>
13 #include <sys/socket.h>
14 #include <sys/time.h>
15 #include <limits.h>
16 #include <netinet/in.h>
17 #include <netdb.h>
18 #include <string.h>
19 #include <pwd.h>
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <pthread.h>
23 #include <signal.h>
24 #include "webcit.h"
25
26
27
28
29
30 /*
31  * Look for URL's embedded in a buffer and make them linkable.  We use a
32  * target window in order to keep the BBS session in its own window.
33  */
34 void url(buf)
35 char buf[];
36 {
37
38         int pos;
39         int start, end;
40         char ench;
41         char urlbuf[256];
42         char outbuf[1024];
43
44         start = (-1);
45         end = strlen(buf);
46         ench = 0;
47
48         for (pos = 0; pos < strlen(buf); ++pos) {
49                 if (!strncasecmp(&buf[pos], "http://", 7))
50                         start = pos;
51                 if (!strncasecmp(&buf[pos], "ftp://", 6))
52                         start = pos;
53         }
54
55         if (start < 0)
56                 return;
57
58         if ((start > 0) && (buf[start - 1] == '<'))
59                 ench = '>';
60         if ((start > 0) && (buf[start - 1] == '['))
61                 ench = ']';
62         if ((start > 0) && (buf[start - 1] == '('))
63                 ench = ')';
64         if ((start > 0) && (buf[start - 1] == '{'))
65                 ench = '}';
66
67         for (pos = strlen(buf); pos > start; --pos) {
68                 if ((buf[pos] == ' ') || (buf[pos] == ench))
69                         end = pos;
70         }
71
72         strncpy(urlbuf, &buf[start], end - start);
73         urlbuf[end - start] = 0;
74
75
76         strncpy(outbuf, buf, start);
77         sprintf(&outbuf[start], "%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
78                 LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
79         strcat(outbuf, &buf[end]);
80         if ( strlen(outbuf) < 250 )
81                 strcpy(buf, outbuf);
82 }
83
84
85 void read_message(msgnum, oper)
86 long msgnum;
87 char *oper;
88 {
89         char buf[256];
90         char m_subject[256];
91         char from[256];
92         long now;
93         struct tm *tm;
94         int format_type = 0;
95         int nhdr = 0;
96         int bq = 0;
97
98         sprintf(buf, "MSG0 %ld", msgnum);
99         serv_puts(buf);
100         serv_gets(buf);
101         if (buf[0] != '1') {
102                 wprintf("<STRONG>ERROR:</STRONG> %s<BR>\n", &buf[4]);
103                 return;
104         }
105         wprintf("<TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=0 BGCOLOR=000077><TR><TD>\n");
106         wprintf("<FONT FACE=\"Arial,Helvetica,sans-serif\" SIZE=+1 COLOR=\"FFFF00\"> ");
107         strcpy(m_subject, "");
108
109         strcpy(WC->reply_to, "nobody...xxxxx");
110         while (serv_gets(buf), strncasecmp(buf, "text", 4)) {
111                 if (!strncasecmp(buf, "nhdr=yes", 8))
112                         nhdr = 1;
113                 if (nhdr == 1)
114                         buf[0] = '_';
115                 if (!strncasecmp(buf, "type=", 5))
116                         format_type = atoi(&buf[5]);
117                 if (!strncasecmp(buf, "from=", 5)) {
118                         wprintf("from %s ", &buf[5]);
119                         strcpy(from, &buf[5]);
120                 }
121                 if (!strncasecmp(buf, "path=", 5))
122                         strcpy(WC->reply_to, &buf[5]);
123                 if (!strncasecmp(buf, "subj=", 5))
124                         strcpy(m_subject, &buf[5]);
125                 if ((!strncasecmp(buf, "hnod=", 5))
126                     && (strcasecmp(&buf[5], serv_info.serv_humannode)))
127                         wprintf("(%s) ", &buf[5]);
128                 if ((!strncasecmp(buf, "room=", 5))
129                     && (strcasecmp(&buf[5], WC->wc_roomname)))
130                         wprintf("in %s> ", &buf[5]);
131
132                 if (!strncasecmp(buf, "node=", 5)) {
133                         if ((WC->room_flags & QR_NETWORK)
134                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
135                         && (strcasecmp(&buf[5], serv_info.serv_fqdn))))) {
136                                 wprintf("@%s ", &buf[5]);
137                         }
138                         if ((!strcasecmp(&buf[5], serv_info.serv_nodename))
139                         || (!strcasecmp(&buf[5], serv_info.serv_fqdn))) {
140                                 strcpy(WC->reply_to, from);
141                         } else if (haschar(&buf[5], '.') == 0) {
142                                 sprintf(WC->reply_to, "%s @ %s", from, &buf[5]);
143                         }
144                 }
145                 if (!strncasecmp(buf, "rcpt=", 5))
146                         wprintf("to %s ", &buf[5]);
147                 if (!strncasecmp(buf, "time=", 5)) {
148                         now = atol(&buf[5]);
149                         tm = (struct tm *) localtime(&now);
150                         strcpy(buf, (char *) asctime(tm));
151                         buf[strlen(buf) - 1] = 0;
152                         strcpy(&buf[16], &buf[19]);
153                         wprintf("%s ", &buf[4]);
154                 }
155         }
156
157         if (nhdr == 1)
158                 wprintf("****");
159         wprintf("</FONT></TD>");
160
161         if (WC->is_room_aide) {
162                 wprintf("<TD ALIGN=RIGHT NOWRAP><FONT FACE=\"Arial,Helvetica,sans-serif\" COLOR=\"FFFF00\"><B>");
163
164                 wprintf("<A HREF=\"/confirm_move_msg");
165                 wprintf("&msgid=%ld", msgnum);
166                 wprintf("\">Move</A>");
167
168                 wprintf("&nbsp;&nbsp;");
169
170                 wprintf("<A HREF=\"/confirm_delete_msg");
171                 wprintf("&msgid=%ld", msgnum);
172                 wprintf("\">Del</A>");
173
174                 wprintf("</B></FONT></TD>");
175         }
176         wprintf("</TR></TABLE>\n");
177
178         if (strlen(m_subject) > 0) {
179                 wprintf("Subject: %s<BR>\n", m_subject);
180         }
181         if (format_type == 0) {
182                 fmout(NULL);
183         } else {
184                 while (serv_gets(buf), strcmp(buf, "000")) {
185                         while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
186                                 buf[strlen(buf) - 1] = 0;
187                         if ((bq == 0) &&
188                             ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
189                                 wprintf("<FONT FACE=\"Arial,Helvetica,sans-serif\" COLOR=\"000044\"><I>");
190                                 bq = 1;
191                         } else if ((bq == 1) &&
192                                    (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
193                                 wprintf("</FONT></I>");
194                                 bq = 0;
195                         }
196                         wprintf("<TT>");
197                         url(buf);
198                         escputs(buf);
199                         wprintf("</TT><BR>\n");
200                 }
201         }
202         wprintf("</I><BR>");
203 }
204
205
206
207 /* 
208  * load message pointers from the server
209  */
210 int load_msg_ptrs(servcmd)
211 char *servcmd;
212 {
213         char buf[256];
214         int nummsgs;
215
216         nummsgs = 0;
217         serv_puts(servcmd);
218         serv_gets(buf);
219         if (buf[0] != '1') {
220                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
221                 return (nummsgs);
222         }
223         while (serv_gets(buf), strcmp(buf, "000")) {
224                 WC->msgarr[nummsgs] = atol(buf);
225                 ++nummsgs;
226         }
227         return (nummsgs);
228 }
229
230
231 /*
232  * command loop for reading messages
233  */
234 void readloop(char *oper)
235 {
236         char cmd[256];
237         int a;
238         int nummsgs;
239
240         output_headers(1);
241
242         wprintf("<FONT FACE=\"Arial,Helvetica,sans-serif\"><CENTER><B>%s - ", WC->wc_roomname);
243         if (!strcmp(oper, "readnew")) {
244                 strcpy(cmd, "MSGS NEW");
245                 wprintf("new messages");
246         } else if (!strcmp(oper, "readold")) {
247                 strcpy(cmd, "MSGS OLD");
248                 wprintf("old messages");
249         } else {
250                 strcpy(cmd, "MSGS ALL");
251                 wprintf("all messages");
252         }
253         wprintf("</B></CENTER><BR>\n");
254
255         nummsgs = load_msg_ptrs(cmd);
256         if (nummsgs == 0) {
257                 if (!strcmp(oper, "readnew")) {
258                         wprintf("<EM>No new messages in this room.</EM>\n");
259                 } else if (!strcmp(oper, "readold")) {
260                         wprintf("<EM>No old messages in this room.</EM>\n");
261                 } else {
262                         wprintf("<EM>This room is empty.</EM>\n");
263                 }
264                 goto DONE;
265         }
266         for (a = 0; a < nummsgs; ++a) {
267                 read_message(WC->msgarr[a], oper);
268         }
269
270       DONE:wDumpContent(1);
271 }
272
273
274
275
276 /*
277  * Post message (or don't post message)
278  *
279  * Note regarding the "dont_post" variable:
280  * A random value (actually, it's just a timestamp) is inserted as a hidden
281  * field called "postseq" when the display_enter page is generated.  This
282  * value is checked when posting, using the static variable dont_post.  If a
283  * user attempts to post twice using the same dont_post value, the message is
284  * discarded.  This prevents the accidental double-saving of the same message
285  * if the user happens to click the browser "back" button.
286  */
287 void post_message(void)
288 {
289         char buf[256];
290         static long dont_post = (-1L);
291
292         output_headers(1);
293
294         wprintf("<FONT FACE=\"Arial,Helvetica,sans-serif\">");
295         strcpy(buf, bstr("sc"));
296         if (strcasecmp(buf, "Save message")) {
297                 wprintf("Cancelled.  Message was not posted.<BR>\n");
298         } else if (atol(bstr("postseq")) == dont_post) {
299                 wprintf("Automatically cancelled because you have already "
300                         "saved this message.<BR>\n");
301         } else {
302                 sprintf(buf, "ENT0 1|%s|0|0", bstr("recp"));
303                 serv_puts(buf);
304                 serv_gets(buf);
305                 if (buf[0] == '4') {
306                         text_to_server(bstr("msgtext"));
307                         serv_puts("000");
308                         wprintf("Message has been posted.<BR>\n");
309                         dont_post = atol(bstr("postseq"));
310                 } else {
311                         wprintf("<EM>%s</EM><BR>\n", &buf[4]);
312                 }
313         }
314
315         wDumpContent(1);
316 }
317
318
319
320
321
322
323
324
325 /*
326  * prompt for a recipient (to be called from display_enter() only)
327  */
328 void prompt_for_recipient()
329 {
330
331         wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=007700><TR><TD>");
332         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
333         wprintf("<B>Send private e-mail</B>\n");
334         wprintf("</FONT></TD></TR></TABLE>\n");
335
336         wprintf("<CENTER>");
337         wprintf("<FORM METHOD=\"POST\" ACTION=\"/display_enter\">\n");
338         wprintf("Enter recipient: ");
339         wprintf("<INPUT TYPE=\"text\" NAME=\"recp\" MAXLENGTH=\"64\"><BR>\n");
340         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Enter message\">");
341         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
342         wprintf("</FORM></CENTER>\n");
343 }
344
345
346
347 /*
348  * display the message entry screen
349  */
350 void display_enter(void)
351 {
352         char buf[256];
353         long now;
354         struct tm *tm;
355
356         output_headers(1);
357
358         wprintf("<FACE=\"Arial,Helvetica,sans-serif\">");
359
360         sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
361         serv_puts(buf);
362         serv_gets(buf);
363
364         if (!strncmp(buf, "570", 3)) {
365                 if (strlen(bstr("recp")) > 0) {
366                         wprintf("<EM>%s</EM><BR>\n", &buf[4]);
367                 }
368                 prompt_for_recipient();
369                 goto DONE;
370         }
371         if (buf[0] != '2') {
372                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
373                 goto DONE;
374         }
375         wprintf("<CENTER>Enter message below.  Messages are formatted to\n");
376         wprintf("the <EM>reader's</EM> screen width.  To defeat the\n");
377         wprintf("formatting, indent a line at least one space.  \n");
378         wprintf("<BR>");
379
380         now = time(NULL);
381         tm = (struct tm *) localtime(&now);
382         strcpy(buf, (char *) asctime(tm));
383         buf[strlen(buf) - 1] = 0;
384         strcpy(&buf[16], &buf[19]);
385         wprintf("</CENTER><FONT COLOR=\"440000\"><B> %s ", &buf[4]);
386         wprintf("from %s ", WC->wc_username);
387         if (strlen(bstr("recp")) > 0)
388                 wprintf("to %s ", bstr("recp"));
389         wprintf("in %s&gt; ", WC->wc_roomname);
390         wprintf("</B></FONT><BR><CENTER>\n");
391
392         wprintf("<FORM METHOD=\"POST\" ACTION=\"/post\">\n");
393         wprintf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"%s\">\n",
394                 bstr("recp"));
395         wprintf("<INPUT TYPE=\"hidden\" NAME=\"postseq\" VALUE=\"%ld\">\n",
396                 now);
397         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save message\">"
398                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\"><BR>\n");
399
400         wprintf("<TEXTAREA NAME=\"msgtext\" wrap=soft ROWS=30 COLS=80 "
401                 "WIDTH=80></TEXTAREA><P>\n");
402
403         wprintf("</FORM></CENTER>\n");
404 DONE:   wDumpContent(1);
405         wprintf("</FONT>");
406 }
407
408
409
410
411
412
413
414
415 /*
416  * Confirm deletion of a message
417  */
418 void confirm_delete_msg(void)
419 {
420         long msgid;
421
422         msgid = atol(bstr("msgid"));
423
424         output_headers(1);
425
426         wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=770000><TR><TD>");
427         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
428         wprintf("<B>Confirm deletion of message</B>\n");
429         wprintf("</FONT></TD></TR></TABLE>\n");
430
431         wprintf("<CENTER>");
432
433         wprintf("Are you sure you want to delete this message? <BR>\n");
434
435         wprintf("<FORM METHOD=\"POST\" ACTION=\"/delete_msg\">\n");
436         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
437                 bstr("msgid"));
438         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Yes\">");
439         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"No\">");
440         wprintf("</FORM></CENTER>\n");
441
442         wprintf("</CENTER>\n");
443         wDumpContent(1);
444 }
445
446
447
448 void delete_msg(void)
449 {
450         long msgid;
451         char buf[256];
452
453         msgid = atol(bstr("msgid"));
454
455         output_headers(1);
456
457         if (!strcasecmp(bstr("yesno"), "Yes")) {
458                 sprintf(buf, "DELE %ld", msgid);
459                 serv_puts(buf);
460                 serv_gets(buf);
461                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
462         } else {
463                 wprintf("<EM>Message not deleted.</EM><BR>\n");
464         }
465
466         wDumpContent(1);
467 }
468
469
470
471
472 /*
473  * Confirm move of a message
474  */
475 void confirm_move_msg(void)
476 {
477         long msgid;
478         char buf[256];
479         char targ[256];
480
481         msgid = atol(bstr("msgid"));
482
483         output_headers(1);
484
485         wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=770000><TR><TD>");
486         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
487         wprintf("<B>Confirm move of message</B>\n");
488         wprintf("</FONT></TD></TR></TABLE>\n");
489
490         wprintf("<CENTER>");
491
492         wprintf("Please select the room to which you would like this message moved:<BR>\n");
493
494         wprintf("<FORM METHOD=\"POST\" ACTION=\"/move_msg\">\n");
495         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
496                 bstr("msgid"));
497
498
499         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
500         serv_puts("LKRA");
501         serv_gets(buf);
502         if (buf[0] == '1') {
503                 while (serv_gets(buf), strcmp(buf, "000")) {
504                         extract(targ, buf, 0);
505                         wprintf("<OPTION>");
506                         escputs(targ);
507                         wprintf("\n");
508                 }
509         }
510         wprintf("</SELECT>\n");
511         wprintf("<BR>\n");
512
513         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
514         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
515         wprintf("</FORM></CENTER>\n");
516
517         wprintf("</CENTER>\n");
518         wDumpContent(1);
519 }
520
521
522
523 void move_msg(void)
524 {
525         long msgid;
526         char buf[256];
527
528         msgid = atol(bstr("msgid"));
529
530         output_headers(1);
531
532         if (!strcasecmp(bstr("yesno"), "Move")) {
533                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
534                 serv_puts(buf);
535                 serv_gets(buf);
536                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
537         } else {
538                 wprintf("<EM>Message not deleted.</EM><BR>\n");
539         }
540
541         wDumpContent(1);
542 }