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