* do_template() now parses a .wml file for WAP clients, and a .html file
[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 #include "vcard.h"
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[SIZ];
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 /* display_vcard() calls this after parsing the textual vCard into
86  * our 'struct vCard' data object.
87  *
88  * Set 'full' to nonzero to display the full card, otherwise it will only
89  * show a summary line.
90  */
91 void display_parsed_vcard(struct vCard *v, int full) {
92         int i, j;
93         char buf[SIZ];
94         char *name;
95
96         if (!full) {
97                 wprintf("<TD>");
98                 name = vcard_get_prop(v, "n", 1, 0, 0);
99                 if (name != NULL) {
100                         strcpy(buf, name);
101                         escputs(buf);
102                 }
103                 else {
104                         wprintf("&nbsp;");
105                 }
106                 wprintf("</TD>");
107                 return;
108         }
109
110         wprintf("<TABLE bgcolor=#888888>");
111         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
112                 if (!strcasecmp(v->prop[i].name, "n")) {
113                         wprintf("<TR BGCOLOR=#AAAAAA>"
114                         "<TD BGCOLOR=#FFFFFF>"
115                         "<IMG VALIGN=CENTER SRC=\"/static/vcard.gif\"></TD>"
116                         "<TD><FONT SIZE=+1><B>");
117                         escputs(v->prop[i].value);
118                         wprintf("</B></FONT></TD></TR>\n");
119                 }
120                 else if (!strcasecmp(v->prop[i].name, "email;internet")) {
121                         wprintf("<TR><TD>Internet e-mail:</TD>"
122                                 "<TD><A HREF=\"mailto:");
123                         urlescputs(v->prop[i].value);
124                         wprintf("\">");
125                         escputs(v->prop[i].value);
126                         wprintf("</A></TD></TR>\n");
127                 }
128                 else if (!strcasecmp(v->prop[i].name, "adr")) {
129                         wprintf("<TR><TD>Address:</TD><TD>");
130                         for (j=0; j<num_tokens(v->prop[i].value, ';'); ++j) {
131                                 extract_token(buf, v->prop[i].value, j, ';');
132                                 if (strlen(buf) > 0) {
133                                         escputs(buf);
134                                         wprintf("<BR>");
135                                 }
136                         }
137                         wprintf("</TD></TR>\n");
138                 }
139                 else if (!strncasecmp(v->prop[i].name, "tel;", 4)) {
140                         wprintf("<TR><TD>%s telephone:</TD><TD>",
141                                 &v->prop[i].name[4]);
142                         for (j=0; j<num_tokens(v->prop[i].value, ';'); ++j) {
143                                 extract_token(buf, v->prop[i].value, j, ';');
144                                 if (strlen(buf) > 0) {
145                                         escputs(buf);
146                                         wprintf("<BR>");
147                                 }
148                         }
149                         wprintf("</TD></TR>\n");
150                 }
151                 else {
152                         wprintf("<TR><TD>");
153                         escputs(v->prop[i].name);
154                         wprintf("</TD><TD>");
155                         escputs(v->prop[i].value);
156                         wprintf("</TD></TR>\n");
157                 }
158         }
159         wprintf("</TABLE>\n");
160 }
161
162
163
164 /*
165  * Display a textual vCard
166  * (Converts to a vCard object and then calls the actual display function)
167  * Set 'full' to nonzero to display the whole card instead of a one-liner
168  */
169 void display_vcard(char *vcard_source, char alpha, int full) {
170         struct vCard *v;
171         char *name;
172         char buf[SIZ];
173         char this_alpha = 0;
174
175         v = vcard_load(vcard_source);
176         if (v == NULL) return;
177
178         name = vcard_get_prop(v, "n", 1, 0, 0);
179         if (name != NULL) {
180                 strcpy(buf, name);
181                 this_alpha = buf[0];
182         }
183
184         if ( (alpha == 0)
185            || ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha)) )
186            || ((!isalpha(alpha)) && (!isalpha(this_alpha))) ) {
187
188                 display_parsed_vcard(v, full);
189
190         }
191
192         vcard_free(v);
193 }
194
195
196
197
198
199
200 void read_message(long msgnum) {
201         char buf[SIZ];
202         char mime_partnum[SIZ];
203         char mime_filename[SIZ];
204         char mime_content_type[SIZ];
205         char mime_disposition[SIZ];
206         int mime_length;
207         char *mime_http = NULL;
208         char m_subject[SIZ];
209         char from[SIZ];
210         char node[SIZ];
211         char rfca[SIZ];
212         char reply_to[512];
213         char now[SIZ];
214         int format_type = 0;
215         int nhdr = 0;
216         int bq = 0;
217         char vcard_partnum[SIZ];
218         char *vcard_source = NULL;
219
220         strcpy(from, "");
221         strcpy(node, "");
222         strcpy(rfca, "");
223         strcpy(reply_to, "");
224         strcpy(vcard_partnum, "");
225
226         sprintf(buf, "MSG0 %ld", msgnum);
227         serv_puts(buf);
228         serv_gets(buf);
229         if (buf[0] != '1') {
230                 wprintf("<STRONG>ERROR:</STRONG> %s<BR>\n", &buf[4]);
231                 return;
232         }
233
234         wprintf("<TABLE WIDTH=100%% BORDER=0 CELLSPACING=0 "
235                 "CELLPADDING=1 BGCOLOR=CCCCCC><TR><TD>\n");
236
237         wprintf("<FONT ");
238         wprintf("SIZE=+1 ");
239         wprintf("COLOR=\"000000\"> ");
240         strcpy(m_subject, "");
241
242         while (serv_gets(buf), strncasecmp(buf, "text", 4)) {
243                 if (!strncasecmp(buf, "nhdr=yes", 8))
244                         nhdr = 1;
245                 if (nhdr == 1)
246                         buf[0] = '_';
247                 if (!strncasecmp(buf, "type=", 5))
248                         format_type = atoi(&buf[5]);
249                 if (!strncasecmp(buf, "from=", 5)) {
250                         strcpy(from, &buf[5]);
251                         wprintf("from <A HREF=\"/showuser&who=");
252                         urlescputs(from);
253                         wprintf("\">");
254                         escputs(from);
255                         wprintf("</A> ");
256                 }
257                 if (!strncasecmp(buf, "subj=", 5))
258                         strcpy(m_subject, &buf[5]);
259                 if ((!strncasecmp(buf, "hnod=", 5))
260                     && (strcasecmp(&buf[5], serv_info.serv_humannode)))
261                         wprintf("(%s) ", &buf[5]);
262                 if ((!strncasecmp(buf, "room=", 5))
263                     && (strcasecmp(&buf[5], WC->wc_roomname)))
264                         wprintf("in %s> ", &buf[5]);
265                 if (!strncasecmp(buf, "rfca=", 5)) {
266                         strcpy(rfca, &buf[5]);
267                         wprintf("&lt;");
268                         escputs(rfca);
269                         wprintf("&gt; ");
270                 }
271
272                 if (!strncasecmp(buf, "node=", 5)) {
273                         if ( ((WC->room_flags & QR_NETWORK)
274                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
275                         && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
276                         && (strlen(rfca)==0)
277                         ) {
278                                 wprintf("@%s ", &buf[5]);
279                         }
280                 }
281                 if (!strncasecmp(buf, "rcpt=", 5))
282                         wprintf("to %s ", &buf[5]);
283                 if (!strncasecmp(buf, "time=", 5)) {
284                         fmt_date(now, atol(&buf[5]));
285                         wprintf("%s ", now);
286                 }
287
288                 if (!strncasecmp(buf, "part=", 5)) {
289                         extract(mime_filename, &buf[5], 1);
290                         extract(mime_partnum, &buf[5], 2);
291                         extract(mime_disposition, &buf[5], 3);
292                         extract(mime_content_type, &buf[5], 4);
293                         mime_length = extract_int(&buf[5], 5);
294
295                         if (!strcasecmp(mime_disposition, "attachment")) {
296                                 if (mime_http == NULL) {
297                                         mime_http = malloc(512);
298                                         strcpy(mime_http, "");
299                                 }
300                                 else {
301                                         mime_http = realloc(mime_http,
302                                                 strlen(mime_http) + 512);
303                                 }
304                                 sprintf(&mime_http[strlen(mime_http)],
305                                         "<A HREF=\"/output_mimepart?"
306                                         "msgnum=%ld&partnum=%s\" "
307                                         "TARGET=\"wc.%ld.%s\">"
308                                         "<IMG SRC=\"/static/attachment.gif\" "
309                                         "BORDER=0 ALIGN=MIDDLE>\n"
310                                         "Part %s: %s (%s, %d bytes)</A><BR>\n",
311                                         msgnum, mime_partnum,
312                                         msgnum, mime_partnum,
313                                         mime_partnum, mime_filename,
314                                         mime_content_type, mime_length);
315                         }
316
317                         if ((!strcasecmp(mime_disposition, "inline"))
318                            && (!strncasecmp(mime_content_type, "image/", 6)) ){
319                                 if (mime_http == NULL) {
320                                         mime_http = malloc(512);
321                                         strcpy(mime_http, "");
322                                 }
323                                 else {
324                                         mime_http = realloc(mime_http,
325                                                 strlen(mime_http) + 512);
326                                 }
327                                 sprintf(&mime_http[strlen(mime_http)],
328                                         "<IMG SRC=\"/output_mimepart?"
329                                         "msgnum=%ld&partnum=%s\">",
330                                         msgnum, mime_partnum);
331                         }
332
333                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
334                                 strcpy(vcard_partnum, mime_partnum);
335                         }
336
337                 }
338
339         }
340
341
342         /* Generate a reply-to address */
343         if (strlen(rfca) > 0) {
344                 strcpy(reply_to, rfca);
345         }
346         else {
347                 if (strlen(node) > 0) {
348                         snprintf(reply_to, sizeof(reply_to), "%s @ %s",
349                                 from, node);
350                 }
351                 else {
352                         snprintf(reply_to, sizeof(reply_to), "%s", from);
353                 }
354         }
355
356         if (nhdr == 1) {
357                 wprintf("****");
358         }
359
360         wprintf("</FONT></TD>");
361
362         wprintf("<TD ALIGN=RIGHT>\n"
363                 "<TABLE BORDER=0><TR>\n");
364
365         wprintf("<TD BGCOLOR=\"AAAADD\">"
366                 "<A HREF=\"/readfwd?startmsg=%ld", msgnum);
367         wprintf("&maxmsgs=1&summary=0\">Read</A>"
368                 "</TD>\n", msgnum);
369
370         wprintf("<TD BGCOLOR=\"AAAADD\">"
371                 "<A HREF=\"/display_enter?recp=");
372         urlescputs(reply_to);
373         wprintf("\"><FONT SIZE=-1>Reply</FONT></A>"
374                 "</TD>\n", msgnum);
375
376         if (WC->is_room_aide) {
377                 wprintf("<TD BGCOLOR=\"AAAADD\">"
378                         "<A HREF=\"/confirm_move_msg"
379                         "&msgid=%ld"
380                         "\"><FONT SIZE=-1>Move</FONT></A>"
381                         "</TD>\n", msgnum);
382
383                 wprintf("<TD BGCOLOR=\"AAAADD\">"
384                         "<A HREF=\"/delete_msg"
385                         "&msgid=%ld\""
386                         "onClick=\"return confirm('Delete this message?');\""
387                         "><FONT SIZE=-1>Del</FONT></A>"
388                         "</TD>\n", msgnum);
389         }
390
391         wprintf("</TR></TABLE>\n"
392                 "</TD>\n");
393
394         if (strlen(m_subject) > 0) {
395                 wprintf("<TR><TD><FONT COLOR=\"0000FF\">"
396                         "Subject: %s</FONT>"
397                         "</TD><TD>&nbsp;</TD></TR>\n", m_subject);
398         }
399
400         wprintf("</TR></TABLE>\n");
401
402         if (format_type == 0) {
403                 fmout(NULL);
404         } else {
405                 while (serv_gets(buf), strcmp(buf, "000")) {
406                         while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
407                                 buf[strlen(buf) - 1] = 0;
408                         if ((bq == 0) &&
409                             ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
410                                 wprintf("<FONT COLOR=\"000044\"><I>");
411                                 bq = 1;
412                         } else if ((bq == 1) &&
413                                    (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
414                                 wprintf("</FONT></I>");
415                                 bq = 0;
416                         }
417                         wprintf("<TT>");
418                         url(buf);
419                         escputs(buf);
420                         wprintf("</TT><BR>\n");
421                 }
422         }
423         wprintf("</I><BR>");
424
425         if (mime_http != NULL) {
426                 wprintf("%s", mime_http);
427                 free(mime_http);
428         }
429
430         if (strlen(vcard_partnum) > 0) {
431                 vcard_source = load_mimepart(msgnum, vcard_partnum);
432                 if (vcard_source != NULL) {
433
434                         /* If it's my vCard I can edit it */
435                         if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
436                            || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
437                                 wprintf("<A HREF=\"/edit_vcard?"
438                                         "msgnum=%ld&partnum=%s\">",
439                                         msgnum, vcard_partnum);
440                                 wprintf("(edit)</A>");
441                         }
442
443                         /* In all cases, display the full card */
444                         display_vcard(vcard_source, 0, 1);
445                         free(vcard_source);
446                 }
447         }
448
449 }
450
451
452 void summarize_message(long msgnum) {
453         char buf[SIZ];
454
455         struct {
456                 char date[SIZ];
457                 char from[SIZ];
458                 char to[SIZ];
459                 char subj[SIZ];
460                 int hasattachments;
461         } summ;
462
463         memset(&summ, 0, sizeof(summ));
464         strcpy(summ.subj, "(no subject)");
465
466         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
467         serv_puts(buf);
468         serv_gets(buf);
469         if (buf[0] != '1') return;
470
471         while (serv_gets(buf), strcmp(buf, "000")) {
472                 if (!strncasecmp(buf, "from=", 5)) {
473                         strcpy(summ.from, &buf[5]);
474                 }
475                 if (!strncasecmp(buf, "subj=", 5)) {
476                         strcpy(summ.subj, &buf[5]);
477                 }
478                 if (!strncasecmp(buf, "rfca=", 5)) {
479                         strcat(summ.from, " <");
480                         strcat(summ.from, &buf[5]);
481                         strcat(summ.from, ">");
482                 }
483
484                 if (!strncasecmp(buf, "node=", 5)) {
485                         if ( ((WC->room_flags & QR_NETWORK)
486                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
487                         && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
488                         ) {
489                                 strcat(summ.from, " @ ");
490                                 strcat(summ.from, &buf[5]);
491                         }
492                 }
493
494                 if (!strncasecmp(buf, "rcpt=", 5)) {
495                         strcpy(summ.to, &buf[5]);
496                 }
497
498                 if (!strncasecmp(buf, "time=", 5)) {
499                         fmt_date(summ.date, atol(&buf[5]));
500                 }
501         }
502
503         wprintf("<TD><A HREF=\"/readfwd?startmsg=%ld"
504                 "&maxmsgs=1&summary=0\">", 
505                 msgnum);
506         escputs(summ.subj);
507         wprintf("</A></TD><TD>");
508         escputs(summ.from);
509         wprintf(" </TD><TD>");
510         escputs(summ.date);
511         wprintf(" </TD>");
512         wprintf("<TD>"
513                 "<INPUT TYPE=\"checkbox\" NAME=\"msg_%ld\" VALUE=\"yes\">"
514                 "</TD>\n");
515
516         return;
517 }
518
519
520
521 void display_addressbook(long msgnum, char alpha) {
522         char buf[SIZ];
523         char mime_partnum[SIZ];
524         char mime_filename[SIZ];
525         char mime_content_type[SIZ];
526         char mime_disposition[SIZ];
527         int mime_length;
528         char vcard_partnum[SIZ];
529         char *vcard_source = NULL;
530
531         struct {
532                 char date[SIZ];
533                 char from[SIZ];
534                 char to[SIZ];
535                 char subj[SIZ];
536                 int hasattachments;
537         } summ;
538
539         memset(&summ, 0, sizeof(summ));
540         strcpy(summ.subj, "(no subject)");
541
542         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
543         serv_puts(buf);
544         serv_gets(buf);
545         if (buf[0] != '1') return;
546
547         while (serv_gets(buf), strcmp(buf, "000")) {
548                 if (!strncasecmp(buf, "part=", 5)) {
549                         extract(mime_filename, &buf[5], 1);
550                         extract(mime_partnum, &buf[5], 2);
551                         extract(mime_disposition, &buf[5], 3);
552                         extract(mime_content_type, &buf[5], 4);
553                         mime_length = extract_int(&buf[5], 5);
554
555                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
556                                 strcpy(vcard_partnum, mime_partnum);
557                         }
558
559                 }
560         }
561
562         if (strlen(vcard_partnum) > 0) {
563                 vcard_source = load_mimepart(msgnum, vcard_partnum);
564                 if (vcard_source != NULL) {
565
566                         /* Display the summary line */
567                         display_vcard(vcard_source, alpha, 0);
568
569                         /* If it's my vCard I can edit it */
570                         if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
571                            || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
572                                 wprintf("<A HREF=\"/edit_vcard?"
573                                         "msgnum=%ld&partnum=%s\">",
574                                         msgnum, vcard_partnum);
575                                 wprintf("(edit)</A>");
576                         }
577
578                         free(vcard_source);
579                 }
580         }
581
582 }
583
584
585
586 /* 
587  * load message pointers from the server
588  */
589 int load_msg_ptrs(servcmd)
590 char *servcmd;
591 {
592         char buf[SIZ];
593         int nummsgs;
594
595         nummsgs = 0;
596         serv_puts(servcmd);
597         serv_gets(buf);
598         if (buf[0] != '1') {
599                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
600                 return (nummsgs);
601         }
602         while (serv_gets(buf), strcmp(buf, "000")) {
603                 WC->msgarr[nummsgs] = atol(buf);
604                 ++nummsgs;
605         }
606         return (nummsgs);
607 }
608
609
610 /*
611  * command loop for reading messages
612  */
613 void readloop(char *oper)
614 {
615         char cmd[SIZ];
616         char buf[SIZ];
617         int a, b, i;
618         int nummsgs;
619         long startmsg;
620         int maxmsgs;
621         int num_displayed = 0;
622         int is_summary = 0;
623         int is_addressbook = 0;
624         int remaining_messages;
625         int lo, hi;
626         int lowest_displayed = (-1);
627         int highest_displayed = 0;
628         long pn_previous = 0L;
629         long pn_current = 0L;
630         long pn_next = 0L;
631         int bg = 0;
632         char alpha = 0;
633
634         startmsg = atol(bstr("startmsg"));
635         maxmsgs = atoi(bstr("maxmsgs"));
636         is_summary = atoi(bstr("summary"));
637         if (maxmsgs == 0) maxmsgs = 20;
638
639         output_headers(1);
640
641         if (!strcmp(oper, "readnew")) {
642                 strcpy(cmd, "MSGS NEW");
643         } else if (!strcmp(oper, "readold")) {
644                 strcpy(cmd, "MSGS OLD");
645         } else {
646                 strcpy(cmd, "MSGS ALL");
647         }
648
649         /* FIXME put in the correct constant #defs */
650         if ((WC->wc_view == 1) && (maxmsgs > 1)) {
651                 is_summary = 1;
652                 strcpy(cmd, "MSGS ALL");
653                 maxmsgs = 32767;
654         }
655         if ((WC->wc_view == 2) && (maxmsgs > 1)) {
656                 is_addressbook = 1;
657                 strcpy(cmd, "MSGS ALL");
658                 maxmsgs = 32767;
659                 if (bstr("alpha") == NULL) {
660                         alpha = 'A';
661                 }
662                 else {
663                         strcpy(buf, bstr("alpha"));
664                         alpha = buf[0];
665                 }
666
667                 for (i='A'; i<='Z'; ++i) {
668                         if (i == alpha) wprintf("<FONT SIZE=+2>"
669                                                 "%c</FONT>\n", i);
670                         else {
671                                 wprintf("<A HREF=\"/readfwd?alpha=%c\">"
672                                         "%c</A>\n", i, i);
673                         }
674                         wprintf("&nbsp;");
675                 }
676                 if (!isalpha(alpha)) wprintf("<FONT SIZE=+2>(other)</FONT>\n");
677                 else wprintf("<A HREF=\"/readfwd?alpha=1\">(other)</A>\n");
678                 wprintf("<HR width=100%%>\n");
679         }
680
681         nummsgs = load_msg_ptrs(cmd);
682         if (nummsgs == 0) {
683                 if (!strcmp(oper, "readnew")) {
684                         wprintf("<EM>No new messages in this room.</EM>\n");
685                 } else if (!strcmp(oper, "readold")) {
686                         wprintf("<EM>No old messages in this room.</EM>\n");
687                 } else {
688                         wprintf("<EM>This room is empty.</EM>\n");
689                 }
690                 goto DONE;
691         }
692
693         if (startmsg == 0L) startmsg = WC->msgarr[0];
694         remaining_messages = 0;
695
696         for (a = 0; a < nummsgs; ++a) {
697                 if (WC->msgarr[a] >= startmsg) {
698                         ++remaining_messages;
699                 }
700         }
701
702         if (is_summary) {
703                 wprintf("<FORM METHOD=\"POST\" ACTION=\"/do_stuff_to_msgs\">\n"
704                         "<TABLE border=0 cellspacing=0 "
705                         "cellpadding=0 width=100%%>\n"
706                         "<TR>"
707                         "<TD><I>Subject</I></TD>"
708                         "<TD><I>Sender</I></TD>"
709                         "<TD><I>Date</I></TD>"
710                         "<TD></TD>"
711                         "</TR>\n"
712                 );
713         }
714
715         if (is_addressbook) {
716                 wprintf("<TABLE border=0 cellspacing=0 "
717                         "cellpadding=0 width=100%%>\n"
718                 );
719         }
720
721         for (a = 0; a < nummsgs; ++a) {
722                 if ((WC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
723
724                         /* Learn which msgs "Prev" & "Next" buttons go to */
725                         pn_current = WC->msgarr[a];
726                         if (a > 0) pn_previous = WC->msgarr[a-1];
727                         if (a < (nummsgs-1)) pn_next = WC->msgarr[a+1];
728
729                         /* If a tabular view, set up the line */
730                         if ( (is_summary) || (is_addressbook) ) {
731                                 bg = 1 - bg;
732                                 wprintf("<TR BGCOLOR=%s>",
733                                         (bg ? "DDDDDD" : "FFFFFF")
734                                 );
735                         }
736
737                         /* Display the message */
738                         if (is_summary) {
739                                 summarize_message(WC->msgarr[a]);
740                         }
741                         else if (is_addressbook) {
742                                 display_addressbook(WC->msgarr[a], alpha);
743                         }
744                         else {
745                                 read_message(WC->msgarr[a]);
746                         }
747
748                         /* If a tabular view, finish the line */
749                         if ( (is_summary) || (is_addressbook) ) {
750                                 wprintf("</TR>\n");
751                         }
752
753                         if (lowest_displayed < 0) lowest_displayed = a;
754                         highest_displayed = a;
755
756                         ++num_displayed;
757                         --remaining_messages;
758                 }
759         }
760
761         if (is_summary) {
762                 wprintf("</TABLE>\n");
763         }
764
765         if (is_addressbook) {
766                 wprintf("</TABLE>\n");
767         }
768
769         /* Bump these because although we're thinking in zero base, the user
770          * is a drooling idiot and is thinking in one base.
771          */
772         ++lowest_displayed;
773         ++highest_displayed;
774
775         /* If we're only looking at one message, do a prev/next thing */
776         if (num_displayed == 1) {
777
778                 wprintf("<CENTER>"
779                         "<TABLE BORDER=0 WIDTH=100%% BGCOLOR=DDDDDD><TR><TD>"
780                         "Reading #%d of %d messages.</TD>\n"
781                         "<TD ALIGN=RIGHT><FONT SIZE=+1>",
782                         lowest_displayed, nummsgs);
783
784                 if (is_summary) {
785                         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
786                                 "VALUE=\"Delete selected\">\n");
787                 }
788
789                 if (pn_previous > 0L) {
790                         wprintf("<A HREF=\"/%s"
791                                 "?startmsg=%ld"
792                                 "&maxmsgs=1"
793                                 "&summary=0\">"
794                                 "Previous</A> \n",
795                                         oper,
796                                         pn_previous );
797                 }
798
799                 if (pn_next > 0L) {
800                         wprintf("<A HREF=\"/%s"
801                                 "?startmsg=%ld"
802                                 "&maxmsgs=1"
803                                 "&summary=0\">"
804                                 "Next</A> \n",
805                                         oper,
806                                         pn_next );
807                 }
808
809                 wprintf("<A HREF=\"/%s?startmsg=%ld"
810                         "&maxmsgs=999999&summary=1\">"
811                         "Summary"
812                         "</A>",
813                         oper,
814                         WC->msgarr[0]);
815
816                 wprintf("</TD></TR></TABLE></CENTER>\n");
817         }
818
819
820         /*
821          * If we're not currently looking at ALL requested
822          * messages, then display the selector bar
823          */
824         if (num_displayed > 1) {
825                 wprintf("<CENTER>"
826                         "<TABLE BORDER=0 WIDTH=100%% BGCOLOR=DDDDDD><TR><TD>"
827                         "Reading #%d-%d of %d messages.</TD>\n"
828                         "<TD ALIGN=RIGHT><FONT SIZE=+1>",
829                         lowest_displayed, highest_displayed, nummsgs);
830
831                 if (is_summary) {
832                         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
833                                 "VALUE=\"Delete selected\">\n");
834                 }
835
836
837                 for (b=0; b<nummsgs; b = b + maxmsgs) {
838                 lo = b+1;
839                 hi = b+maxmsgs+1;
840                 if (hi > nummsgs) hi = nummsgs;
841                         if (WC->msgarr[b] != startmsg) {
842                                 wprintf("<A HREF=\"/%s"
843                                         "?startmsg=%ld"
844                                         "&maxmsgs=%d"
845                                         "&summary=%d\">"
846                                         "%d-%d</A> \n",
847                                                 oper,
848                                                 WC->msgarr[b],
849                                                 maxmsgs,
850                                                 is_summary,
851                                                 lo, hi);
852                         }
853                         else {
854                                 wprintf("%d-%d \n", lo, hi);
855                         }
856
857                 }
858                 wprintf("<A HREF=\"/%s?startmsg=%ld"
859                         "&maxmsgs=999999&summary=%d\">"
860                         "ALL"
861                         "</A> ",
862                         oper,
863                         WC->msgarr[0], is_summary);
864
865                 wprintf("<A HREF=\"/%s?startmsg=%ld"
866                         "&maxmsgs=999999&summary=1\">"
867                         "Summary"
868                         "</A>",
869                         oper,
870                         WC->msgarr[0]);
871
872                 wprintf("</TD></TR></TABLE></CENTER>\n");
873         }
874         if (is_summary) wprintf("</FORM>\n");
875
876 DONE:   wDumpContent(1);
877 }
878
879
880
881
882 /*
883  * Post message (or don't post message)
884  *
885  * Note regarding the "dont_post" variable:
886  * A random value (actually, it's just a timestamp) is inserted as a hidden
887  * field called "postseq" when the display_enter page is generated.  This
888  * value is checked when posting, using the static variable dont_post.  If a
889  * user attempts to post twice using the same dont_post value, the message is
890  * discarded.  This prevents the accidental double-saving of the same message
891  * if the user happens to click the browser "back" button.
892  */
893 void post_message(void)
894 {
895         char buf[SIZ];
896         static long dont_post = (-1L);
897
898         output_headers(1);
899
900         wprintf("<FONT FACE=\"Arial,Helvetica,sans-serif\">");
901         strcpy(buf, bstr("sc"));
902         if (strcasecmp(buf, "Save message")) {
903                 wprintf("Cancelled.  Message was not posted.<BR>\n");
904         } else if (atol(bstr("postseq")) == dont_post) {
905                 wprintf("Automatically cancelled because you have already "
906                         "saved this message.<BR>\n");
907         } else {
908                 sprintf(buf, "ENT0 1|%s|0|0|%s",
909                         bstr("recp"),
910                         bstr("subject") );
911                 serv_puts(buf);
912                 serv_gets(buf);
913                 if (buf[0] == '4') {
914                         text_to_server(bstr("msgtext"));
915                         serv_puts("000");
916                         wprintf("Message has been posted.<BR>\n");
917                         dont_post = atol(bstr("postseq"));
918                 } else {
919                         wprintf("<EM>%s</EM><BR>\n", &buf[4]);
920                 }
921         }
922
923         wDumpContent(1);
924 }
925
926
927
928
929 /*
930  * display the message entry screen
931  */
932 void display_enter(void)
933 {
934         char buf[SIZ];
935         long now;
936         struct tm *tm;
937
938         output_headers(1);
939
940         wprintf("<FONT FACE=\"Arial,Helvetica,sans-serif\">");
941
942         sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
943         serv_puts(buf);
944         serv_gets(buf);
945
946         if (!strncmp(buf, "570", 3)) {
947                 if (strlen(bstr("recp")) > 0) {
948                         wprintf("<EM>%s</EM><BR>\n", &buf[4]);
949                 }
950                 do_template("prompt_for_recipient");
951                 goto DONE;
952         }
953         if (buf[0] != '2') {
954                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
955                 goto DONE;
956         }
957
958         now = time(NULL);
959         tm = (struct tm *) localtime(&now);
960         strcpy(buf, (char *) asctime(tm));
961         buf[strlen(buf) - 1] = 0;
962         strcpy(&buf[16], &buf[19]);
963         wprintf("</CENTER><FONT COLOR=\"440000\">\n"
964                 "<IMG SRC=\"static/enter.gif\" ALIGN=MIDDLE ALT=\" \" "
965                 "onLoad=\"document.enterform.msgtext.focus();\" >");
966         wprintf("<B> %s ", &buf[4]);
967         wprintf("from %s ", WC->wc_username);
968         if (strlen(bstr("recp")) > 0)
969                 wprintf("to %s ", bstr("recp"));
970         wprintf("in %s&gt; ", WC->wc_roomname);
971         wprintf("</B></FONT><BR><CENTER>\n");
972
973         wprintf("<FORM METHOD=\"POST\" ACTION=\"/post\" "
974                 "NAME=\"enterform\">\n");
975         wprintf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"%s\">\n",
976                 bstr("recp"));
977         wprintf("<INPUT TYPE=\"hidden\" NAME=\"postseq\" VALUE=\"%ld\">\n",
978                 now);
979         wprintf("<FONT SIZE=-1>Subject (optional):</FONT>"
980                 "<INPUT TYPE=\"text\" NAME=\"subject\" MAXLENGTH=70>"
981                 "&nbsp;&nbsp;&nbsp;"
982                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save message\">"
983                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\"><BR>\n");
984
985         wprintf("<TEXTAREA NAME=\"msgtext\" wrap=soft ROWS=30 COLS=80 "
986                 "WIDTH=80></TEXTAREA><P>\n");
987
988         wprintf("</FORM></CENTER>\n");
989 DONE:   wDumpContent(1);
990         wprintf("</FONT>");
991 }
992
993
994
995
996
997
998
999
1000 void delete_msg(void)
1001 {
1002         long msgid;
1003         char buf[SIZ];
1004
1005         msgid = atol(bstr("msgid"));
1006
1007         output_headers(1);
1008
1009         sprintf(buf, "DELE %ld", msgid);
1010         serv_puts(buf);
1011         serv_gets(buf);
1012         wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1013
1014         wDumpContent(1);
1015 }
1016
1017
1018
1019
1020 /*
1021  * Confirm move of a message
1022  */
1023 void confirm_move_msg(void)
1024 {
1025         long msgid;
1026         char buf[SIZ];
1027         char targ[SIZ];
1028
1029         msgid = atol(bstr("msgid"));
1030
1031         output_headers(1);
1032
1033         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=770000><TR><TD>");
1034         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
1035         wprintf("<B>Confirm move of message</B>\n");
1036         wprintf("</FONT></TD></TR></TABLE>\n");
1037
1038         wprintf("<CENTER>");
1039
1040         wprintf("Please select the room to which you would like this message moved:<BR>\n");
1041
1042         wprintf("<FORM METHOD=\"POST\" ACTION=\"/move_msg\">\n");
1043         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
1044                 bstr("msgid"));
1045
1046
1047         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1048         serv_puts("LKRA");
1049         serv_gets(buf);
1050         if (buf[0] == '1') {
1051                 while (serv_gets(buf), strcmp(buf, "000")) {
1052                         extract(targ, buf, 0);
1053                         wprintf("<OPTION>");
1054                         escputs(targ);
1055                         wprintf("\n");
1056                 }
1057         }
1058         wprintf("</SELECT>\n");
1059         wprintf("<BR>\n");
1060
1061         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
1062         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
1063         wprintf("</FORM></CENTER>\n");
1064
1065         wprintf("</CENTER>\n");
1066         wDumpContent(1);
1067 }
1068
1069
1070
1071 void move_msg(void)
1072 {
1073         long msgid;
1074         char buf[SIZ];
1075
1076         msgid = atol(bstr("msgid"));
1077
1078         output_headers(1);
1079
1080         if (!strcasecmp(bstr("yesno"), "Move")) {
1081                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1082                 serv_puts(buf);
1083                 serv_gets(buf);
1084                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1085         } else {
1086                 wprintf("<EM>Message not moved.</EM><BR>\n");
1087         }
1088
1089         wDumpContent(1);
1090 }
1091
1092
1093
1094 void do_stuff_to_msgs(void) {
1095         char buf[SIZ];
1096         char sc[SIZ];
1097
1098         struct stuff_t {
1099                 struct stuff_t *next;
1100                 long msgnum;
1101         };
1102
1103         struct stuff_t *stuff = NULL;
1104         struct stuff_t *ptr;
1105
1106
1107         serv_puts("MSGS ALL");
1108         serv_gets(buf);
1109
1110         if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
1111                 ptr = malloc(sizeof(struct stuff_t));
1112                 ptr->msgnum = atol(buf);
1113                 ptr->next = stuff;
1114                 stuff = ptr;
1115         }
1116
1117         strcpy(sc, bstr("sc"));
1118
1119         while (stuff != NULL) {
1120
1121                 sprintf(buf, "msg_%ld", stuff->msgnum);
1122                 if (!strcasecmp(bstr(buf), "yes")) {
1123
1124                         if (!strcasecmp(sc, "Delete selected")) {
1125                                 serv_printf("DELE %ld", stuff->msgnum);
1126                                 serv_gets(buf);
1127                         }
1128
1129                 }
1130
1131                 ptr = stuff->next;
1132                 free(stuff);
1133                 stuff = ptr;
1134         }
1135
1136         readloop("readfwd");
1137 }