]> code.citadel.org Git - citadel.git/blob - webcit/messages.c
* div align=center
[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 - ",
243                 WC->wc_roomname);
244         if (!strcmp(oper, "readnew")) {
245                 strcpy(cmd, "MSGS NEW");
246                 wprintf("new messages");
247         } else if (!strcmp(oper, "readold")) {
248                 strcpy(cmd, "MSGS OLD");
249                 wprintf("old messages");
250         } else {
251                 strcpy(cmd, "MSGS ALL");
252                 wprintf("all messages");
253         }
254         wprintf("</B></CENTER><BR>\n");
255
256         nummsgs = load_msg_ptrs(cmd);
257         if (nummsgs == 0) {
258                 if (!strcmp(oper, "readnew")) {
259                         wprintf("<EM>No new messages in this room.</EM>\n");
260                 } else if (!strcmp(oper, "readold")) {
261                         wprintf("<EM>No old messages in this room.</EM>\n");
262                 } else {
263                         wprintf("<EM>This room is empty.</EM>\n");
264                 }
265                 goto DONE;
266         }
267         for (a = 0; a < nummsgs; ++a) {
268                 read_message(WC->msgarr[a], oper);
269         }
270
271 DONE:   wDumpContent(1);
272 }
273
274
275
276
277 /*
278  * Post message (or don't post message)
279  *
280  * Note regarding the "dont_post" variable:
281  * A random value (actually, it's just a timestamp) is inserted as a hidden
282  * field called "postseq" when the display_enter page is generated.  This
283  * value is checked when posting, using the static variable dont_post.  If a
284  * user attempts to post twice using the same dont_post value, the message is
285  * discarded.  This prevents the accidental double-saving of the same message
286  * if the user happens to click the browser "back" button.
287  */
288 void post_message(void)
289 {
290         char buf[256];
291         static long dont_post = (-1L);
292
293         output_headers(1);
294
295         wprintf("<FONT FACE=\"Arial,Helvetica,sans-serif\">");
296         strcpy(buf, bstr("sc"));
297         if (strcasecmp(buf, "Save message")) {
298                 wprintf("Cancelled.  Message was not posted.<BR>\n");
299         } else if (atol(bstr("postseq")) == dont_post) {
300                 wprintf("Automatically cancelled because you have already "
301                         "saved this message.<BR>\n");
302         } else {
303                 sprintf(buf, "ENT0 1|%s|0|0", bstr("recp"));
304                 serv_puts(buf);
305                 serv_gets(buf);
306                 if (buf[0] == '4') {
307                         text_to_server(bstr("msgtext"));
308                         serv_puts("000");
309                         wprintf("Message has been posted.<BR>\n");
310                         dont_post = atol(bstr("postseq"));
311                 } else {
312                         wprintf("<EM>%s</EM><BR>\n", &buf[4]);
313                 }
314         }
315
316         wDumpContent(1);
317 }
318
319
320
321
322
323
324
325
326 /*
327  * prompt for a recipient (to be called from display_enter() only)
328  */
329 void prompt_for_recipient()
330 {
331
332         wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=007700><TR><TD>");
333         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
334         wprintf("<B>Send private e-mail</B>\n");
335         wprintf("</FONT></TD></TR></TABLE>\n");
336
337         wprintf("<CENTER>");
338         wprintf("<FORM METHOD=\"POST\" ACTION=\"/display_enter\">\n");
339         wprintf("Enter recipient: ");
340         wprintf("<INPUT TYPE=\"text\" NAME=\"recp\" MAXLENGTH=\"64\"><BR>\n");
341         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Enter message\">");
342         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
343         wprintf("</FORM></CENTER>\n");
344 }
345
346
347
348 /*
349  * display the message entry screen
350  */
351 void display_enter(void)
352 {
353         char buf[256];
354         long now;
355         struct tm *tm;
356
357         output_headers(1);
358
359         wprintf("<FACE=\"Arial,Helvetica,sans-serif\">");
360
361         sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
362         serv_puts(buf);
363         serv_gets(buf);
364
365         if (!strncmp(buf, "570", 3)) {
366                 if (strlen(bstr("recp")) > 0) {
367                         wprintf("<EM>%s</EM><BR>\n", &buf[4]);
368                 }
369                 prompt_for_recipient();
370                 goto DONE;
371         }
372         if (buf[0] != '2') {
373                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
374                 goto DONE;
375         }
376         wprintf("<CENTER>Enter message below.  Messages are formatted to\n");
377         wprintf("the <EM>reader's</EM> screen width.  To defeat the\n");
378         wprintf("formatting, indent a line at least one space.  \n");
379         wprintf("<BR>");
380
381         now = time(NULL);
382         tm = (struct tm *) localtime(&now);
383         strcpy(buf, (char *) asctime(tm));
384         buf[strlen(buf) - 1] = 0;
385         strcpy(&buf[16], &buf[19]);
386         wprintf("</CENTER><FONT COLOR=\"440000\"><B> %s ", &buf[4]);
387         wprintf("from %s ", WC->wc_username);
388         if (strlen(bstr("recp")) > 0)
389                 wprintf("to %s ", bstr("recp"));
390         wprintf("in %s&gt; ", WC->wc_roomname);
391         wprintf("</B></FONT><BR><CENTER>\n");
392
393         wprintf("<FORM METHOD=\"POST\" ACTION=\"/post\">\n");
394         wprintf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"%s\">\n",
395                 bstr("recp"));
396         wprintf("<INPUT TYPE=\"hidden\" NAME=\"postseq\" VALUE=\"%ld\">\n",
397                 now);
398         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save message\">"
399                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\"><BR>\n");
400
401         wprintf("<TEXTAREA NAME=\"msgtext\" wrap=soft ROWS=30 COLS=80 "
402                 "WIDTH=80></TEXTAREA><P>\n");
403
404         wprintf("</FORM></CENTER>\n");
405 DONE:   wDumpContent(1);
406         wprintf("</FONT>");
407 }
408
409
410
411
412
413
414
415
416 /*
417  * Confirm deletion of a message
418  */
419 void confirm_delete_msg(void)
420 {
421         long msgid;
422
423         msgid = atol(bstr("msgid"));
424
425         output_headers(1);
426
427         wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=770000><TR><TD>");
428         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
429         wprintf("<B>Confirm deletion of message</B>\n");
430         wprintf("</FONT></TD></TR></TABLE>\n");
431
432         wprintf("<CENTER>");
433
434         wprintf("Are you sure you want to delete this message? <BR>\n");
435
436         wprintf("<FORM METHOD=\"POST\" ACTION=\"/delete_msg\">\n");
437         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
438                 bstr("msgid"));
439         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Yes\">");
440         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"No\">");
441         wprintf("</FORM></CENTER>\n");
442
443         wprintf("</CENTER>\n");
444         wDumpContent(1);
445 }
446
447
448
449 void delete_msg(void)
450 {
451         long msgid;
452         char buf[256];
453
454         msgid = atol(bstr("msgid"));
455
456         output_headers(1);
457
458         if (!strcasecmp(bstr("yesno"), "Yes")) {
459                 sprintf(buf, "DELE %ld", msgid);
460                 serv_puts(buf);
461                 serv_gets(buf);
462                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
463         } else {
464                 wprintf("<EM>Message not deleted.</EM><BR>\n");
465         }
466
467         wDumpContent(1);
468 }
469
470
471
472
473 /*
474  * Confirm move of a message
475  */
476 void confirm_move_msg(void)
477 {
478         long msgid;
479         char buf[256];
480         char targ[256];
481
482         msgid = atol(bstr("msgid"));
483
484         output_headers(1);
485
486         wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=770000><TR><TD>");
487         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
488         wprintf("<B>Confirm move of message</B>\n");
489         wprintf("</FONT></TD></TR></TABLE>\n");
490
491         wprintf("<CENTER>");
492
493         wprintf("Please select the room to which you would like this message moved:<BR>\n");
494
495         wprintf("<FORM METHOD=\"POST\" ACTION=\"/move_msg\">\n");
496         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
497                 bstr("msgid"));
498
499
500         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
501         serv_puts("LKRA");
502         serv_gets(buf);
503         if (buf[0] == '1') {
504                 while (serv_gets(buf), strcmp(buf, "000")) {
505                         extract(targ, buf, 0);
506                         wprintf("<OPTION>");
507                         escputs(targ);
508                         wprintf("\n");
509                 }
510         }
511         wprintf("</SELECT>\n");
512         wprintf("<BR>\n");
513
514         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
515         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
516         wprintf("</FORM></CENTER>\n");
517
518         wprintf("</CENTER>\n");
519         wDumpContent(1);
520 }
521
522
523
524 void move_msg(void)
525 {
526         long msgid;
527         char buf[256];
528
529         msgid = atol(bstr("msgid"));
530
531         output_headers(1);
532
533         if (!strcasecmp(bstr("yesno"), "Move")) {
534                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
535                 serv_puts(buf);
536                 serv_gets(buf);
537                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
538         } else {
539                 wprintf("<EM>Message not deleted.</EM><BR>\n");
540         }
541
542         wDumpContent(1);
543 }