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