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