2428675db3e7f91915b5cf62ee4717e4b4e0b14f
[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 #include "webserver.h"
29
30
31 /*
32  * Look for URL's embedded in a buffer and make them linkable.  We use a
33  * target window in order to keep the BBS session in its own window.
34  */
35 void url(buf)
36 char buf[];
37 {
38
39         int pos;
40         int start, end;
41         char ench;
42         char urlbuf[SIZ];
43         char outbuf[1024];
44
45         start = (-1);
46         end = strlen(buf);
47         ench = 0;
48
49         for (pos = 0; pos < strlen(buf); ++pos) {
50                 if (!strncasecmp(&buf[pos], "http://", 7))
51                         start = pos;
52                 if (!strncasecmp(&buf[pos], "ftp://", 6))
53                         start = pos;
54         }
55
56         if (start < 0)
57                 return;
58
59         if ((start > 0) && (buf[start - 1] == '<'))
60                 ench = '>';
61         if ((start > 0) && (buf[start - 1] == '['))
62                 ench = ']';
63         if ((start > 0) && (buf[start - 1] == '('))
64                 ench = ')';
65         if ((start > 0) && (buf[start - 1] == '{'))
66                 ench = '}';
67
68         for (pos = strlen(buf); pos > start; --pos) {
69                 if ((buf[pos] == ' ') || (buf[pos] == ench))
70                         end = pos;
71         }
72
73         strncpy(urlbuf, &buf[start], end - start);
74         urlbuf[end - start] = 0;
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  * I wanna SEE that message!
200  */
201 void read_message(long msgnum) {
202         char buf[SIZ];
203         char mime_partnum[SIZ];
204         char mime_filename[SIZ];
205         char mime_content_type[SIZ];
206         char mime_disposition[SIZ];
207         int mime_length;
208         char *mime_http = NULL;
209         char m_subject[SIZ];
210         char from[SIZ];
211         char node[SIZ];
212         char rfca[SIZ];
213         char reply_to[512];
214         char now[SIZ];
215         int format_type = 0;
216         int nhdr = 0;
217         int bq = 0;
218         char vcard_partnum[SIZ];
219         char cal_partnum[SIZ];
220         char *part_source = NULL;
221
222         strcpy(from, "");
223         strcpy(node, "");
224         strcpy(rfca, "");
225         strcpy(reply_to, "");
226         strcpy(vcard_partnum, "");
227         strcpy(cal_partnum, "");
228
229         serv_printf("MSG4 %ld", msgnum);
230         serv_gets(buf);
231         if (buf[0] != '1') {
232                 wprintf("<STRONG>ERROR:</STRONG> %s<BR>\n", &buf[4]);
233                 return;
234         }
235
236         wprintf("<TABLE WIDTH=100%% BORDER=0 CELLSPACING=0 "
237                 "CELLPADDING=1 BGCOLOR=CCCCCC><TR><TD>\n");
238
239         wprintf("<FONT ");
240         wprintf("SIZE=+1 ");
241         wprintf("COLOR=\"000000\"> ");
242         strcpy(m_subject, "");
243
244         while (serv_gets(buf), strcasecmp(buf, "text")) {
245                 if (!strncasecmp(buf, "nhdr=yes", 8))
246                         nhdr = 1;
247                 if (nhdr == 1)
248                         buf[0] = '_';
249                 if (!strncasecmp(buf, "type=", 5))
250                         format_type = atoi(&buf[5]);
251                 if (!strncasecmp(buf, "from=", 5)) {
252                         strcpy(from, &buf[5]);
253                         wprintf("from <A HREF=\"/showuser&who=");
254                         urlescputs(from);
255                         wprintf("\">");
256                         escputs(from);
257                         wprintf("</A> ");
258                 }
259                 if (!strncasecmp(buf, "subj=", 5))
260                         strcpy(m_subject, &buf[5]);
261                 if ((!strncasecmp(buf, "hnod=", 5))
262                     && (strcasecmp(&buf[5], serv_info.serv_humannode)))
263                         wprintf("(%s) ", &buf[5]);
264                 if ((!strncasecmp(buf, "room=", 5))
265                     && (strcasecmp(&buf[5], WC->wc_roomname)))
266                         wprintf("in %s> ", &buf[5]);
267                 if (!strncasecmp(buf, "rfca=", 5)) {
268                         strcpy(rfca, &buf[5]);
269                         wprintf("&lt;");
270                         escputs(rfca);
271                         wprintf("&gt; ");
272                 }
273
274                 if (!strncasecmp(buf, "node=", 5)) {
275                         if ( ((WC->room_flags & QR_NETWORK)
276                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
277                         && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
278                         && (strlen(rfca)==0)
279                         ) {
280                                 wprintf("@%s ", &buf[5]);
281                         }
282                 }
283                 if (!strncasecmp(buf, "rcpt=", 5))
284                         wprintf("to %s ", &buf[5]);
285                 if (!strncasecmp(buf, "time=", 5)) {
286                         fmt_date(now, atol(&buf[5]));
287                         wprintf("%s ", now);
288                 }
289
290                 if (!strncasecmp(buf, "part=", 5)) {
291                         extract(mime_filename, &buf[5], 1);
292                         extract(mime_partnum, &buf[5], 2);
293                         extract(mime_disposition, &buf[5], 3);
294                         extract(mime_content_type, &buf[5], 4);
295                         mime_length = extract_int(&buf[5], 5);
296
297                         if (!strcasecmp(mime_disposition, "attachment")) {
298                                 if (mime_http == NULL) {
299                                         mime_http = malloc(512);
300                                         strcpy(mime_http, "");
301                                 }
302                                 else {
303                                         mime_http = realloc(mime_http,
304                                                 strlen(mime_http) + 512);
305                                 }
306                                 sprintf(&mime_http[strlen(mime_http)],
307                                         "<A HREF=\"/output_mimepart?"
308                                         "msgnum=%ld&partnum=%s\" "
309                                         "TARGET=\"wc.%ld.%s\">"
310                                         "<IMG SRC=\"/static/attachment.gif\" "
311                                         "BORDER=0 ALIGN=MIDDLE>\n"
312                                         "Part %s: %s (%s, %d bytes)</A><BR>\n",
313                                         msgnum, mime_partnum,
314                                         msgnum, mime_partnum,
315                                         mime_partnum, mime_filename,
316                                         mime_content_type, mime_length);
317                         }
318
319                         if ((!strcasecmp(mime_disposition, "inline"))
320                            && (!strncasecmp(mime_content_type, "image/", 6)) ){
321                                 if (mime_http == NULL) {
322                                         mime_http = malloc(512);
323                                         strcpy(mime_http, "");
324                                 }
325                                 else {
326                                         mime_http = realloc(mime_http,
327                                                 strlen(mime_http) + 512);
328                                 }
329                                 sprintf(&mime_http[strlen(mime_http)],
330                                         "<IMG SRC=\"/output_mimepart?"
331                                         "msgnum=%ld&partnum=%s\">",
332                                         msgnum, mime_partnum);
333                         }
334
335                         /*** begin handler prep ***/
336                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
337                                 strcpy(vcard_partnum, mime_partnum);
338                         }
339
340                         if (!strcasecmp(mime_content_type, "text/calendar")) {
341                                 strcpy(cal_partnum, mime_partnum);
342                         }
343
344                         /*** end handler prep ***/
345
346                 }
347
348         }
349
350
351         /* Generate a reply-to address */
352         if (strlen(rfca) > 0) {
353                 strcpy(reply_to, rfca);
354         }
355         else {
356                 if (strlen(node) > 0) {
357                         snprintf(reply_to, sizeof(reply_to), "%s @ %s",
358                                 from, node);
359                 }
360                 else {
361                         snprintf(reply_to, sizeof(reply_to), "%s", from);
362                 }
363         }
364
365         if (nhdr == 1) {
366                 wprintf("****");
367         }
368
369         wprintf("</FONT></TD>");
370
371         wprintf("<TD ALIGN=RIGHT>\n"
372                 "<TABLE BORDER=0><TR>\n");
373
374         wprintf("<TD BGCOLOR=\"AAAADD\">"
375                 "<A HREF=\"/readfwd?startmsg=%ld", msgnum);
376         wprintf("&maxmsgs=1&summary=0\">Read</A>"
377                 "</TD>\n", msgnum);
378
379         wprintf("<TD BGCOLOR=\"AAAADD\">"
380                 "<A HREF=\"/display_enter?recp=");
381         urlescputs(reply_to);
382         wprintf("\"><FONT SIZE=-1>Reply</FONT></A>"
383                 "</TD>\n", msgnum);
384
385         if (WC->is_room_aide) {
386                 wprintf("<TD BGCOLOR=\"AAAADD\">"
387                         "<A HREF=\"/confirm_move_msg"
388                         "&msgid=%ld"
389                         "\"><FONT SIZE=-1>Move</FONT></A>"
390                         "</TD>\n", msgnum);
391
392                 wprintf("<TD BGCOLOR=\"AAAADD\">"
393                         "<A HREF=\"/delete_msg"
394                         "&msgid=%ld\""
395                         "onClick=\"return confirm('Delete this message?');\""
396                         "><FONT SIZE=-1>Del</FONT></A>"
397                         "</TD>\n", msgnum);
398         }
399
400         wprintf("</TR></TABLE>\n"
401                 "</TD>\n");
402
403         if (strlen(m_subject) > 0) {
404                 wprintf("<TR><TD><FONT COLOR=\"0000FF\">"
405                         "Subject: %s</FONT>"
406                         "</TD><TD>&nbsp;</TD></TR>\n", m_subject);
407         }
408
409         wprintf("</TR></TABLE>\n");
410
411         /* 
412          * Learn the content type
413          */
414         strcpy(mime_content_type, "text/plain");
415         while (serv_gets(buf), (strlen(buf) > 0)) {
416                 if (!strncasecmp(buf, "Content-type: ", 14)) {
417                         safestrncpy(mime_content_type, &buf[14],
418                                 sizeof(mime_content_type));
419                 }
420         }
421
422         /* Messages in legacy Citadel variformat get handled thusly... */
423         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
424                 fmout(NULL);
425         }
426
427         /* Boring old 80-column fixed format text gets handled this way... */
428         else if (!strcasecmp(mime_content_type, "text/plain")) {
429                 while (serv_gets(buf), strcmp(buf, "000")) {
430                         while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
431                                 buf[strlen(buf) - 1] = 0;
432                         if ((bq == 0) &&
433                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
434                                 wprintf("<FONT COLOR=\"000044\"><I>");
435                                 bq = 1;
436                         } else if ((bq == 1) &&
437                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
438                                 wprintf("</FONT></I>");
439                                 bq = 0;
440                         }
441                         wprintf("<TT>");
442                         url(buf);
443                         escputs(buf);
444                         wprintf("</TT><BR>\n");
445                 }
446                 wprintf("</I><BR>");
447         }
448
449         else /* HTML is fun, but we've got to strip it first */
450         if (!strcasecmp(mime_content_type, "text/html")) {
451                 output_html();
452         }
453
454         /* Unknown weirdness */
455         else {
456                 wprintf("I don't know how to display %s<BR>\n",
457                         mime_content_type);
458                 while (serv_gets(buf), strcmp(buf, "000")) { }
459         }
460
461
462         /* Afterwards, offer links to download attachments 'n' such */
463         if (mime_http != NULL) {
464                 wprintf("%s", mime_http);
465                 free(mime_http);
466         }
467
468         /* Handler for vCard parts */
469         if (strlen(vcard_partnum) > 0) {
470                 part_source = load_mimepart(msgnum, vcard_partnum);
471                 if (part_source != NULL) {
472
473                         /* If it's my vCard I can edit it */
474                         if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
475                            || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
476                                 wprintf("<A HREF=\"/edit_vcard?"
477                                         "msgnum=%ld&partnum=%s\">",
478                                         msgnum, vcard_partnum);
479                                 wprintf("(edit)</A>");
480                         }
481
482                         /* In all cases, display the full card */
483                         display_vcard(part_source, 0, 1);
484                 }
485         }
486
487         /* Handler for calendar parts */
488         if (strlen(cal_partnum) > 0) {
489                 part_source = load_mimepart(msgnum, cal_partnum);
490                 if (part_source != NULL) {
491                         cal_process_attachment(part_source);
492                 }
493         }
494
495         if (part_source) {
496                 free(part_source);
497                 part_source = NULL;
498         }
499
500 }
501
502
503 void summarize_message(long msgnum) {
504         char buf[SIZ];
505
506         struct {
507                 char date[SIZ];
508                 char from[SIZ];
509                 char to[SIZ];
510                 char subj[SIZ];
511                 int hasattachments;
512         } summ;
513
514         memset(&summ, 0, sizeof(summ));
515         strcpy(summ.subj, "(no subject)");
516
517         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
518         serv_puts(buf);
519         serv_gets(buf);
520         if (buf[0] != '1') return;
521
522         while (serv_gets(buf), strcmp(buf, "000")) {
523                 if (!strncasecmp(buf, "from=", 5)) {
524                         strcpy(summ.from, &buf[5]);
525                 }
526                 if (!strncasecmp(buf, "subj=", 5)) {
527                         strcpy(summ.subj, &buf[5]);
528                 }
529                 if (!strncasecmp(buf, "rfca=", 5)) {
530                         strcat(summ.from, " <");
531                         strcat(summ.from, &buf[5]);
532                         strcat(summ.from, ">");
533                 }
534
535                 if (!strncasecmp(buf, "node=", 5)) {
536                         if ( ((WC->room_flags & QR_NETWORK)
537                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
538                         && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
539                         ) {
540                                 strcat(summ.from, " @ ");
541                                 strcat(summ.from, &buf[5]);
542                         }
543                 }
544
545                 if (!strncasecmp(buf, "rcpt=", 5)) {
546                         strcpy(summ.to, &buf[5]);
547                 }
548
549                 if (!strncasecmp(buf, "time=", 5)) {
550                         fmt_date(summ.date, atol(&buf[5]));
551                 }
552         }
553
554         wprintf("<TD><A HREF=\"/readfwd?startmsg=%ld"
555                 "&maxmsgs=1&summary=0\">", 
556                 msgnum);
557         escputs(summ.subj);
558         wprintf("</A></TD><TD>");
559         escputs(summ.from);
560         wprintf(" </TD><TD>");
561         escputs(summ.date);
562         wprintf(" </TD>");
563         wprintf("<TD>"
564                 "<INPUT TYPE=\"checkbox\" NAME=\"msg_%ld\" VALUE=\"yes\">"
565                 "</TD>\n");
566
567         return;
568 }
569
570
571
572 void display_addressbook(long msgnum, char alpha) {
573         char buf[SIZ];
574         char mime_partnum[SIZ];
575         char mime_filename[SIZ];
576         char mime_content_type[SIZ];
577         char mime_disposition[SIZ];
578         int mime_length;
579         char vcard_partnum[SIZ];
580         char *vcard_source = NULL;
581
582         struct {
583                 char date[SIZ];
584                 char from[SIZ];
585                 char to[SIZ];
586                 char subj[SIZ];
587                 int hasattachments;
588         } summ;
589
590         memset(&summ, 0, sizeof(summ));
591         strcpy(summ.subj, "(no subject)");
592
593         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
594         serv_puts(buf);
595         serv_gets(buf);
596         if (buf[0] != '1') return;
597
598         while (serv_gets(buf), strcmp(buf, "000")) {
599                 if (!strncasecmp(buf, "part=", 5)) {
600                         extract(mime_filename, &buf[5], 1);
601                         extract(mime_partnum, &buf[5], 2);
602                         extract(mime_disposition, &buf[5], 3);
603                         extract(mime_content_type, &buf[5], 4);
604                         mime_length = extract_int(&buf[5], 5);
605
606                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
607                                 strcpy(vcard_partnum, mime_partnum);
608                         }
609
610                 }
611         }
612
613         if (strlen(vcard_partnum) > 0) {
614                 vcard_source = load_mimepart(msgnum, vcard_partnum);
615                 if (vcard_source != NULL) {
616
617                         /* Display the summary line */
618                         display_vcard(vcard_source, alpha, 0);
619
620                         /* If it's my vCard I can edit it */
621                         if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
622                            || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
623                                 wprintf("<A HREF=\"/edit_vcard?"
624                                         "msgnum=%ld&partnum=%s\">",
625                                         msgnum, vcard_partnum);
626                                 wprintf("(edit)</A>");
627                         }
628
629                         free(vcard_source);
630                 }
631         }
632
633 }
634
635
636
637 /* 
638  * load message pointers from the server
639  */
640 int load_msg_ptrs(servcmd)
641 char *servcmd;
642 {
643         char buf[SIZ];
644         int nummsgs;
645
646         nummsgs = 0;
647         serv_puts(servcmd);
648         serv_gets(buf);
649         if (buf[0] != '1') {
650                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
651                 return (nummsgs);
652         }
653         while (serv_gets(buf), strcmp(buf, "000")) {
654                 WC->msgarr[nummsgs] = atol(buf);
655                 ++nummsgs;
656         }
657         return (nummsgs);
658 }
659
660
661 /*
662  * command loop for reading messages
663  */
664 void readloop(char *oper)
665 {
666         char cmd[SIZ];
667         char buf[SIZ];
668         int a, b, i;
669         int nummsgs;
670         long startmsg;
671         int maxmsgs;
672         int num_displayed = 0;
673         int is_summary = 0;
674         int is_addressbook = 0;
675         int is_calendar = 0;
676         int is_tasks = 0;
677         int remaining_messages;
678         int lo, hi;
679         int lowest_displayed = (-1);
680         int highest_displayed = 0;
681         long pn_previous = 0L;
682         long pn_current = 0L;
683         long pn_next = 0L;
684         int bg = 0;
685         char alpha = 0;
686
687         startmsg = atol(bstr("startmsg"));
688         maxmsgs = atoi(bstr("maxmsgs"));
689         is_summary = atoi(bstr("summary"));
690         if (maxmsgs == 0) maxmsgs = 20;
691
692         output_headers(1);
693
694         if (!strcmp(oper, "readnew")) {
695                 strcpy(cmd, "MSGS NEW");
696         } else if (!strcmp(oper, "readold")) {
697                 strcpy(cmd, "MSGS OLD");
698         } else {
699                 strcpy(cmd, "MSGS ALL");
700         }
701
702         /* FIXME put in the correct constant #defs */
703         if ((WC->wc_view == 1) && (maxmsgs > 1)) {
704                 is_summary = 1;
705                 strcpy(cmd, "MSGS ALL");
706                 maxmsgs = 32767;
707         }
708         if ((WC->wc_view == 2) && (maxmsgs > 1)) {
709                 is_addressbook = 1;
710                 strcpy(cmd, "MSGS ALL");
711                 maxmsgs = 32767;
712                 if (bstr("alpha") == NULL) {
713                         alpha = 'A';
714                 }
715                 else {
716                         strcpy(buf, bstr("alpha"));
717                         alpha = buf[0];
718                 }
719
720                 for (i='A'; i<='Z'; ++i) {
721                         if (i == alpha) wprintf("<FONT SIZE=+2>"
722                                                 "%c</FONT>\n", i);
723                         else {
724                                 wprintf("<A HREF=\"/readfwd?alpha=%c\">"
725                                         "%c</A>\n", i, i);
726                         }
727                         wprintf("&nbsp;");
728                 }
729                 if (!isalpha(alpha)) wprintf("<FONT SIZE=+2>(other)</FONT>\n");
730                 else wprintf("<A HREF=\"/readfwd?alpha=1\">(other)</A>\n");
731                 wprintf("<HR width=100%%>\n");
732         }
733         if (WC->wc_view == 3) {         /* calendar */
734                 is_calendar = 1;
735                 strcpy(cmd, "MSGS ALL");
736                 maxmsgs = 32767;
737         }
738         if (WC->wc_view == 4) {         /* tasks */
739                 is_tasks = 1;
740                 strcpy(cmd, "MSGS ALL");
741                 maxmsgs = 32767;
742                 wprintf("<UL>");
743         }
744
745         nummsgs = load_msg_ptrs(cmd);
746         if (nummsgs == 0) {
747
748                 if ((!is_tasks) && (!is_calendar)) {
749                         if (!strcmp(oper, "readnew")) {
750                                 wprintf("<EM>No new messages in this room.</EM>\n");
751                         } else if (!strcmp(oper, "readold")) {
752                                 wprintf("<EM>No old messages in this room.</EM>\n");
753                         } else {
754                                 wprintf("<EM>This room is empty.</EM>\n");
755                         }
756                 }
757
758                 goto DONE;
759         }
760
761         if (startmsg == 0L) startmsg = WC->msgarr[0];
762         remaining_messages = 0;
763
764         for (a = 0; a < nummsgs; ++a) {
765                 if (WC->msgarr[a] >= startmsg) {
766                         ++remaining_messages;
767                 }
768         }
769
770         if (is_summary) {
771                 wprintf("<FORM METHOD=\"POST\" ACTION=\"/do_stuff_to_msgs\">\n"
772                         "<TABLE border=0 cellspacing=0 "
773                         "cellpadding=0 width=100%%>\n"
774                         "<TR>"
775                         "<TD><I>Subject</I></TD>"
776                         "<TD><I>Sender</I></TD>"
777                         "<TD><I>Date</I></TD>"
778                         "<TD></TD>"
779                         "</TR>\n"
780                 );
781         }
782
783         if (is_addressbook) {
784                 wprintf("<TABLE border=0 cellspacing=0 "
785                         "cellpadding=0 width=100%%>\n"
786                 );
787         }
788
789         for (a = 0; a < nummsgs; ++a) {
790                 if ((WC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
791
792                         /* Learn which msgs "Prev" & "Next" buttons go to */
793                         pn_current = WC->msgarr[a];
794                         if (a > 0) pn_previous = WC->msgarr[a-1];
795                         if (a < (nummsgs-1)) pn_next = WC->msgarr[a+1];
796
797                         /* If a tabular view, set up the line */
798                         if ( (is_summary) || (is_addressbook) ) {
799                                 bg = 1 - bg;
800                                 wprintf("<TR BGCOLOR=%s>",
801                                         (bg ? "DDDDDD" : "FFFFFF")
802                                 );
803                         }
804
805                         /* Display the message */
806                         if (is_summary) {
807                                 summarize_message(WC->msgarr[a]);
808                         }
809                         else if (is_addressbook) {
810                                 display_addressbook(WC->msgarr[a], alpha);
811                         }
812                         else if (is_calendar) {
813                                 display_calendar(WC->msgarr[a]);
814                         }
815                         else if (is_tasks) {
816                                 display_task(WC->msgarr[a]);
817                         }
818                         else {
819                                 read_message(WC->msgarr[a]);
820                         }
821
822                         /* If a tabular view, finish the line */
823                         if ( (is_summary) || (is_addressbook) ) {
824                                 wprintf("</TR>\n");
825                         }
826
827                         if (lowest_displayed < 0) lowest_displayed = a;
828                         highest_displayed = a;
829
830                         ++num_displayed;
831                         --remaining_messages;
832                 }
833         }
834
835         if (is_summary) {
836                 wprintf("</TABLE>\n");
837         }
838
839         if (is_addressbook) {
840                 wprintf("</TABLE>\n");
841         }
842
843         if (is_tasks) {
844                 wprintf("</UL>\n");
845         }
846
847         /* Bump these because although we're thinking in zero base, the user
848          * is a drooling idiot and is thinking in one base.
849          */
850         ++lowest_displayed;
851         ++highest_displayed;
852
853         /* If we're only looking at one message, do a prev/next thing */
854         if (num_displayed == 1) {
855            if ((!is_tasks) && (!is_calendar)) {
856
857                 wprintf("<CENTER>"
858                         "<TABLE BORDER=0 WIDTH=100%% BGCOLOR=DDDDDD><TR><TD>"
859                         "Reading #%d of %d messages.</TD>\n"
860                         "<TD ALIGN=RIGHT><FONT SIZE=+1>",
861                         lowest_displayed, nummsgs);
862
863                 if (is_summary) {
864                         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
865                                 "VALUE=\"Delete selected\">\n");
866                 }
867
868                 if (pn_previous > 0L) {
869                         wprintf("<A HREF=\"/%s"
870                                 "?startmsg=%ld"
871                                 "&maxmsgs=1"
872                                 "&summary=0\">"
873                                 "Previous</A> \n",
874                                         oper,
875                                         pn_previous );
876                 }
877
878                 if (pn_next > 0L) {
879                         wprintf("<A HREF=\"/%s"
880                                 "?startmsg=%ld"
881                                 "&maxmsgs=1"
882                                 "&summary=0\">"
883                                 "Next</A> \n",
884                                         oper,
885                                         pn_next );
886                 }
887
888                 wprintf("<A HREF=\"/%s?startmsg=%ld"
889                         "&maxmsgs=999999&summary=1\">"
890                         "Summary"
891                         "</A>",
892                         oper,
893                         WC->msgarr[0]);
894
895                 wprintf("</TD></TR></TABLE></CENTER>\n");
896             }
897         }
898
899
900         /*
901          * If we're not currently looking at ALL requested
902          * messages, then display the selector bar
903          */
904         if (num_displayed > 1) {
905            if ((!is_tasks) && (!is_calendar)) {
906                 wprintf("<CENTER>"
907                         "<TABLE BORDER=0 WIDTH=100%% BGCOLOR=DDDDDD><TR><TD>"
908                         "Reading #%d-%d of %d messages.</TD>\n"
909                         "<TD ALIGN=RIGHT><FONT SIZE=+1>",
910                         lowest_displayed, highest_displayed, nummsgs);
911
912                 if (is_summary) {
913                         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
914                                 "VALUE=\"Delete selected\">\n");
915                 }
916
917
918                 for (b=0; b<nummsgs; b = b + maxmsgs) {
919                 lo = b+1;
920                 hi = b+maxmsgs+1;
921                 if (hi > nummsgs) hi = nummsgs;
922                         if (WC->msgarr[b] != startmsg) {
923                                 wprintf("<A HREF=\"/%s"
924                                         "?startmsg=%ld"
925                                         "&maxmsgs=%d"
926                                         "&summary=%d\">"
927                                         "%d-%d</A> \n",
928                                                 oper,
929                                                 WC->msgarr[b],
930                                                 maxmsgs,
931                                                 is_summary,
932                                                 lo, hi);
933                         }
934                         else {
935                                 wprintf("%d-%d \n", lo, hi);
936                         }
937
938                 }
939                 wprintf("<A HREF=\"/%s?startmsg=%ld"
940                         "&maxmsgs=999999&summary=%d\">"
941                         "ALL"
942                         "</A> ",
943                         oper,
944                         WC->msgarr[0], is_summary);
945
946                 wprintf("<A HREF=\"/%s?startmsg=%ld"
947                         "&maxmsgs=999999&summary=1\">"
948                         "Summary"
949                         "</A>",
950                         oper,
951                         WC->msgarr[0]);
952
953                 wprintf("</TD></TR></TABLE></CENTER>\n");
954             }
955         }
956         if (is_summary) wprintf("</FORM>\n");
957
958 DONE:
959         if (is_tasks) {
960                 wprintf("<A HREF=\"/display_edit_task?msgnum=0\">"
961                         "Add new task</A>\n"
962                 );
963         }
964
965         if (is_calendar) {
966                 do_calendar_view();     /* Render the calendar */
967         }
968
969         wDumpContent(1);
970 }
971
972
973
974
975 /*
976  * Post message (or don't post message)
977  *
978  * Note regarding the "dont_post" variable:
979  * A random value (actually, it's just a timestamp) is inserted as a hidden
980  * field called "postseq" when the display_enter page is generated.  This
981  * value is checked when posting, using the static variable dont_post.  If a
982  * user attempts to post twice using the same dont_post value, the message is
983  * discarded.  This prevents the accidental double-saving of the same message
984  * if the user happens to click the browser "back" button.
985  */
986 void post_message(void)
987 {
988         char buf[SIZ];
989         static long dont_post = (-1L);
990
991         output_headers(1);
992
993         wprintf("<FONT FACE=\"Arial,Helvetica,sans-serif\">");
994         strcpy(buf, bstr("sc"));
995         if (strcasecmp(buf, "Save message")) {
996                 wprintf("Cancelled.  Message was not posted.<BR>\n");
997         } else if (atol(bstr("postseq")) == dont_post) {
998                 wprintf("Automatically cancelled because you have already "
999                         "saved this message.<BR>\n");
1000         } else {
1001                 sprintf(buf, "ENT0 1|%s|0|4|%s",
1002                         bstr("recp"),
1003                         bstr("subject") );
1004                 serv_puts(buf);
1005                 serv_gets(buf);
1006                 if (buf[0] == '4') {
1007                         serv_puts("Content-type: text/html");
1008                         serv_puts("");
1009                         text_to_server(bstr("msgtext"), 1);
1010                         serv_puts("000");
1011                         wprintf("Message has been posted.<BR>\n");
1012                         dont_post = atol(bstr("postseq"));
1013                 } else {
1014                         wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1015                 }
1016         }
1017
1018         wDumpContent(1);
1019 }
1020
1021
1022
1023
1024 /*
1025  * display the message entry screen
1026  */
1027 void display_enter(void)
1028 {
1029         char buf[SIZ];
1030         long now;
1031         struct tm *tm;
1032
1033         output_headers(1);
1034
1035         wprintf("<FONT FACE=\"Arial,Helvetica,sans-serif\">");
1036
1037         sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
1038         serv_puts(buf);
1039         serv_gets(buf);
1040
1041         if (!strncmp(buf, "570", 3)) {
1042                 if (strlen(bstr("recp")) > 0) {
1043                         wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1044                 }
1045                 do_template("prompt_for_recipient");
1046                 goto DONE;
1047         }
1048         if (buf[0] != '2') {
1049                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1050                 goto DONE;
1051         }
1052
1053         now = time(NULL);
1054         tm = (struct tm *) localtime(&now);
1055         strcpy(buf, (char *) asctime(tm));
1056         buf[strlen(buf) - 1] = 0;
1057         strcpy(&buf[16], &buf[19]);
1058         wprintf("</CENTER><FONT COLOR=\"440000\">\n"
1059                 "<IMG SRC=\"static/enter.gif\" ALIGN=MIDDLE ALT=\" \" "
1060                 "onLoad=\"document.enterform.msgtext.focus();\" >");
1061         wprintf("<B> %s ", &buf[4]);
1062         wprintf("from %s ", WC->wc_username);
1063         if (strlen(bstr("recp")) > 0)
1064                 wprintf("to %s ", bstr("recp"));
1065         wprintf("in %s&gt; ", WC->wc_roomname);
1066         wprintf("</B></FONT><BR><CENTER>\n");
1067
1068         wprintf("<FORM METHOD=\"POST\" ACTION=\"/post\" "
1069                 "NAME=\"enterform\">\n");
1070         wprintf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"%s\">\n",
1071                 bstr("recp"));
1072         wprintf("<INPUT TYPE=\"hidden\" NAME=\"postseq\" VALUE=\"%ld\">\n",
1073                 now);
1074         wprintf("<FONT SIZE=-1>Subject (optional):</FONT>"
1075                 "<INPUT TYPE=\"text\" NAME=\"subject\" MAXLENGTH=70>"
1076                 "&nbsp;&nbsp;&nbsp;"
1077                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save message\">"
1078                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\"><BR>\n");
1079
1080         wprintf("<TEXTAREA NAME=\"msgtext\" wrap=soft ROWS=30 COLS=80 "
1081                 "WIDTH=80></TEXTAREA><P>\n");
1082
1083         wprintf("</FORM></CENTER>\n");
1084 DONE:   wDumpContent(1);
1085         wprintf("</FONT>");
1086 }
1087
1088
1089
1090
1091
1092
1093
1094
1095 void delete_msg(void)
1096 {
1097         long msgid;
1098         char buf[SIZ];
1099
1100         msgid = atol(bstr("msgid"));
1101
1102         output_headers(1);
1103
1104         sprintf(buf, "DELE %ld", msgid);
1105         serv_puts(buf);
1106         serv_gets(buf);
1107         wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1108
1109         wDumpContent(1);
1110 }
1111
1112
1113
1114
1115 /*
1116  * Confirm move of a message
1117  */
1118 void confirm_move_msg(void)
1119 {
1120         long msgid;
1121         char buf[SIZ];
1122         char targ[SIZ];
1123
1124         msgid = atol(bstr("msgid"));
1125
1126         output_headers(1);
1127
1128         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=770000><TR><TD>");
1129         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
1130         wprintf("<B>Confirm move of message</B>\n");
1131         wprintf("</FONT></TD></TR></TABLE>\n");
1132
1133         wprintf("<CENTER>");
1134
1135         wprintf("Please select the room to which you would like this message moved:<BR>\n");
1136
1137         wprintf("<FORM METHOD=\"POST\" ACTION=\"/move_msg\">\n");
1138         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
1139                 bstr("msgid"));
1140
1141
1142         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1143         serv_puts("LKRA");
1144         serv_gets(buf);
1145         if (buf[0] == '1') {
1146                 while (serv_gets(buf), strcmp(buf, "000")) {
1147                         extract(targ, buf, 0);
1148                         wprintf("<OPTION>");
1149                         escputs(targ);
1150                         wprintf("\n");
1151                 }
1152         }
1153         wprintf("</SELECT>\n");
1154         wprintf("<BR>\n");
1155
1156         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
1157         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
1158         wprintf("</FORM></CENTER>\n");
1159
1160         wprintf("</CENTER>\n");
1161         wDumpContent(1);
1162 }
1163
1164
1165
1166 void move_msg(void)
1167 {
1168         long msgid;
1169         char buf[SIZ];
1170
1171         msgid = atol(bstr("msgid"));
1172
1173         output_headers(1);
1174
1175         if (!strcasecmp(bstr("yesno"), "Move")) {
1176                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1177                 serv_puts(buf);
1178                 serv_gets(buf);
1179                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1180         } else {
1181                 wprintf("<EM>Message not moved.</EM><BR>\n");
1182         }
1183
1184         wDumpContent(1);
1185 }
1186
1187
1188
1189 void do_stuff_to_msgs(void) {
1190         char buf[SIZ];
1191         char sc[SIZ];
1192
1193         struct stuff_t {
1194                 struct stuff_t *next;
1195                 long msgnum;
1196         };
1197
1198         struct stuff_t *stuff = NULL;
1199         struct stuff_t *ptr;
1200
1201
1202         serv_puts("MSGS ALL");
1203         serv_gets(buf);
1204
1205         if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
1206                 ptr = malloc(sizeof(struct stuff_t));
1207                 ptr->msgnum = atol(buf);
1208                 ptr->next = stuff;
1209                 stuff = ptr;
1210         }
1211
1212         strcpy(sc, bstr("sc"));
1213
1214         while (stuff != NULL) {
1215
1216                 sprintf(buf, "msg_%ld", stuff->msgnum);
1217                 if (!strcasecmp(bstr(buf), "yes")) {
1218
1219                         if (!strcasecmp(sc, "Delete selected")) {
1220                                 serv_printf("DELE %ld", stuff->msgnum);
1221                                 serv_gets(buf);
1222                         }
1223
1224                 }
1225
1226                 ptr = stuff->next;
1227                 free(stuff);
1228                 stuff = ptr;
1229         }
1230
1231         readloop("readfwd");
1232 }