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