* Clicking "Enter Message" in a room with an Address Book view opens
[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 /* Address book entry (keep it short and sweet, it's just a quickie lookup
32  * which we can use to get to the real meat and bones later)
33  */
34 struct addrbookent {
35         char ab_name[64];
36         long ab_msgnum;
37 };
38
39
40 /*
41  * Look for URL's embedded in a buffer and make them linkable.  We use a
42  * target window in order to keep the BBS session in its own window.
43  */
44 void url(buf)
45 char buf[];
46 {
47
48         int pos;
49         int start, end;
50         char ench;
51         char urlbuf[SIZ];
52         char outbuf[1024];
53
54         start = (-1);
55         end = strlen(buf);
56         ench = 0;
57
58         for (pos = 0; pos < strlen(buf); ++pos) {
59                 if (!strncasecmp(&buf[pos], "http://", 7))
60                         start = pos;
61                 if (!strncasecmp(&buf[pos], "ftp://", 6))
62                         start = pos;
63         }
64
65         if (start < 0)
66                 return;
67
68         if ((start > 0) && (buf[start - 1] == '<'))
69                 ench = '>';
70         if ((start > 0) && (buf[start - 1] == '['))
71                 ench = ']';
72         if ((start > 0) && (buf[start - 1] == '('))
73                 ench = ')';
74         if ((start > 0) && (buf[start - 1] == '{'))
75                 ench = '}';
76
77         for (pos = strlen(buf); pos > start; --pos) {
78                 if ((buf[pos] == ' ') || (buf[pos] == ench))
79                         end = pos;
80         }
81
82         strncpy(urlbuf, &buf[start], end - start);
83         urlbuf[end - start] = 0;
84
85         strncpy(outbuf, buf, start);
86         sprintf(&outbuf[start], "%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
87                 LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
88         strcat(outbuf, &buf[end]);
89         if ( strlen(outbuf) < 250 )
90                 strcpy(buf, outbuf);
91 }
92
93
94 /* display_vcard() calls this after parsing the textual vCard into
95  * our 'struct vCard' data object.
96  * This gets called instead of display_parsed_vcard() if we are only looking
97  * to extract the person's name instead of displaying the card.
98  */
99 void fetchname_parsed_vcard(struct vCard *v, char *storename) {
100         int i;
101
102         strcpy(storename, "");
103         if (v->numprops) for (i=0; i<(v->numprops); ++i) {
104                 if (!strcasecmp(v->prop[i].name, "n")) {
105                         strcpy(storename, v->prop[i].value);
106                 }
107         }
108 }
109
110
111
112 /* display_vcard() calls this after parsing the textual vCard into
113  * our 'struct vCard' data object.
114  *
115  * Set 'full' to nonzero to display the full card, otherwise it will only
116  * show a summary line.
117  *
118  * This code is a bit ugly, so perhaps an explanation is due: we do this
119  * in two passes through the vCard fields.  On the first pass, we process
120  * fields we understand, and then render them in a pretty fashion at the
121  * end.  Then we make a second pass, outputting all the fields we don't
122  * understand in a simple two-column name/value format.
123  */
124 void display_parsed_vcard(struct vCard *v, int full) {
125         int i, j;
126         char buf[SIZ];
127         char *name;
128         int is_qp = 0;
129         int is_b64 = 0;
130         char *thisname, *thisvalue;
131         char firsttoken[SIZ];
132         int pass;
133
134         char displayname[SIZ];
135         char phone[SIZ];
136         char mailto[SIZ];
137
138         strcpy(displayname, "");
139         strcpy(phone, "");
140         strcpy(mailto, "");
141
142         if (!full) {
143                 wprintf("<TD>");
144                 name = vcard_get_prop(v, "fn", 1, 0, 0);
145                 if (name == NULL) name = vcard_get_prop(v, "n", 1, 0, 0);
146                 if (name != NULL) {
147                         strcpy(buf, name);
148                         escputs(buf);
149                 }
150                 else {
151                         wprintf("&nbsp;");
152                 }
153                 wprintf("</TD>");
154                 return;
155         }
156
157         wprintf("<TABLE bgcolor=#888888>");
158         for (pass=1; pass<=2; ++pass) {
159
160                 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
161
162                         thisname = strdup(v->prop[i].name);
163                         extract_token(firsttoken, thisname, 0, ';');
164         
165                         for (j=0; j<num_tokens(thisname, ';'); ++j) {
166                                 extract_token(buf, thisname, j, ';');
167                                 if (!strcasecmp(buf, "encoding=quoted-printable")) {
168                                         is_qp = 1;
169                                         remove_token(thisname, j, ';');
170                                 }
171                                 if (!strcasecmp(buf, "encoding=base64")) {
172                                         is_b64 = 1;
173                                         remove_token(thisname, j, ';');
174                                 }
175                         }
176         
177                         if (is_qp) {
178                                 thisvalue = malloc(strlen(v->prop[i].value) + 50);
179                                 j = CtdlDecodeQuotedPrintable(
180                                         thisvalue, v->prop[i].value,
181                                         strlen(v->prop[i].value) );
182                                 thisvalue[j] = 0;
183                         }
184                         else if (is_b64) {
185                                 thisvalue = malloc(strlen(v->prop[i].value) + 50);
186                                 CtdlDecodeBase64(
187                                         thisvalue, v->prop[i].value,
188                                         strlen(v->prop[i].value) );
189                         }
190                         else {
191                                 thisvalue = strdup(v->prop[i].value);
192                         }
193         
194                         /*** Various fields we may encounter ***/
195         
196                         /* N is name, but only if there's no FN already there */
197                         if (!strcasecmp(firsttoken, "n")) {
198                                 if (strlen(displayname) == 0) {
199                                         strcpy(displayname, thisvalue);
200                                 }
201                         }
202         
203                         /* FN (full name) is a true 'display name' field */
204                         else if (!strcasecmp(firsttoken, "fn")) {
205                                 strcpy(displayname, thisvalue);
206                         }
207         
208                         else if (!strcasecmp(firsttoken, "email")) {
209                                 if (strlen(mailto) > 0) strcat(mailto, "<BR>");
210                                 strcat(mailto,
211                                         "<A HREF=\"/display_enter"
212                                         "?force_room=_MAIL_&recp=");
213                                 urlesc(&mailto[strlen(mailto)], thisvalue);
214                                 strcat(mailto, "\">");
215                                 urlesc(&mailto[strlen(mailto)], thisvalue);
216                                 strcat(mailto, "</A>");
217                         }
218                         else if (!strcasecmp(firsttoken, "tel")) {
219                                 if (strlen(phone) > 0) strcat(phone, "<BR>");
220                                 strcat(phone, thisvalue);
221                                 for (j=0; j<num_tokens(thisname, ';'); ++j) {
222                                         extract_token(buf, thisname, j, ';');
223                                         if (!strcasecmp(buf, "tel"))
224                                                 strcat(phone, "");
225                                         else if (!strcasecmp(buf, "work"))
226                                                 strcat(phone, " (work)");
227                                         else if (!strcasecmp(buf, "home"))
228                                                 strcat(phone, " (home)");
229                                         else if (!strcasecmp(buf, "cell"))
230                                                 strcat(phone, " (cell)");
231                                         else {
232                                                 strcat(phone, " (");
233                                                 strcat(phone, buf);
234                                                 strcat(phone, ")");
235                                         }
236                                 }
237                         }
238                         else if (!strcasecmp(firsttoken, "adr")) {
239                                 if (pass == 2) {
240                                         wprintf("<TR><TD>Address:</TD><TD>");
241                                         for (j=0; j<num_tokens(thisvalue, ';'); ++j) {
242                                                 extract_token(buf, thisvalue, j, ';');
243                                                 if (strlen(buf) > 0) {
244                                                         escputs(buf);
245                                                         wprintf("<BR>");
246                                                 }
247                                         }
248                                         wprintf("</TD></TR>\n");
249                                 }
250                         }
251                         else if (!strcasecmp(firsttoken, "version")) {
252                                 /* ignore */
253                         }
254                         else if (!strcasecmp(firsttoken, "rev")) {
255                                 /* ignore */
256                         }
257                         else if (!strcasecmp(firsttoken, "label")) {
258                                 /* ignore */
259                         }
260                         else {
261                                 if (pass == 2) {
262                                         wprintf("<TR><TD>");
263                                         escputs(thisname);
264                                         wprintf("</TD><TD>");
265                                         escputs(thisvalue);
266                                         wprintf("</TD></TR>\n");
267                                 }
268                         }
269         
270                         free(thisname);
271                         free(thisvalue);
272                 }
273         
274                 if (pass == 1) {
275                         wprintf("<TR BGCOLOR=\"#AAAAAA\">"
276                         "<TD COLSPAN=2 BGCOLOR=\"#FFFFFF\">"
277                         "<IMG ALIGN=CENTER SRC=\"/static/vcard.gif\">"
278                         "<FONT SIZE=+1><B>");
279                         escputs(displayname);
280                         wprintf("</B></FONT></TD></TR>\n");
281                 
282                         if (strlen(phone) > 0)
283                                 wprintf("<TR><TD>Telephone:</TD><TD>%s</TD></TR>\n", phone);
284                         if (strlen(mailto) > 0)
285                                 wprintf("<TR><TD>E-mail:</TD><TD>%s</TD></TR>\n", mailto);
286                 }
287
288         }
289
290         wprintf("</TABLE>\n");
291 }
292
293
294
295 /*
296  * Display a textual vCard
297  * (Converts to a vCard object and then calls the actual display function)
298  * Set 'full' to nonzero to display the whole card instead of a one-liner.
299  * Or, if "storename" is non-NULL, just store the person's name in that
300  * buffer instead of displaying the card at all.
301  */
302 void display_vcard(char *vcard_source, char alpha, int full, char *storename) {
303         struct vCard *v;
304         char *name;
305         char buf[SIZ];
306         char this_alpha = 0;
307
308         v = vcard_load(vcard_source);
309         if (v == NULL) return;
310
311         name = vcard_get_prop(v, "n", 1, 0, 0);
312         if (name != NULL) {
313                 strcpy(buf, name);
314                 this_alpha = buf[0];
315         }
316
317         if (storename != NULL) {
318                 fetchname_parsed_vcard(v, storename);
319         }
320         else if ( (alpha == 0)
321            || ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha)) )
322            || ((!isalpha(alpha)) && (!isalpha(this_alpha))) ) {
323                 display_parsed_vcard(v, full);
324         }
325
326         vcard_free(v);
327 }
328
329
330
331
332 /*
333  * I wanna SEE that message!
334  */
335 void read_message(long msgnum) {
336         char buf[SIZ];
337         char mime_partnum[SIZ];
338         char mime_filename[SIZ];
339         char mime_content_type[SIZ];
340         char mime_disposition[SIZ];
341         int mime_length;
342         char mime_http[SIZ];
343         char m_subject[SIZ];
344         char from[SIZ];
345         char node[SIZ];
346         char rfca[SIZ];
347         char reply_to[512];
348         char now[SIZ];
349         int format_type = 0;
350         int nhdr = 0;
351         int bq = 0;
352         char vcard_partnum[SIZ];
353         char cal_partnum[SIZ];
354         char *part_source = NULL;
355
356         strcpy(from, "");
357         strcpy(node, "");
358         strcpy(rfca, "");
359         strcpy(reply_to, "");
360         strcpy(vcard_partnum, "");
361         strcpy(cal_partnum, "");
362         strcpy(mime_http, "");
363
364         serv_printf("MSG4 %ld", msgnum);
365         serv_gets(buf);
366         if (buf[0] != '1') {
367                 wprintf("<STRONG>ERROR:</STRONG> %s<BR>\n", &buf[4]);
368                 return;
369         }
370
371         wprintf("<TABLE WIDTH=100%% BORDER=0 CELLSPACING=0 "
372                 "CELLPADDING=1 BGCOLOR=\"#CCCCCC\"><TR><TD>\n");
373
374         wprintf("<SPAN CLASS=\"message_header\">");
375         strcpy(m_subject, "");
376
377         while (serv_gets(buf), strcasecmp(buf, "text")) {
378                 if (!strcmp(buf, "000")) {
379                         wprintf("<I>unexpected end of message</I><BR><BR>\n");
380                         wprintf("</SPAN>\n");
381                         return;
382                 }
383                 if (!strncasecmp(buf, "nhdr=yes", 8))
384                         nhdr = 1;
385                 if (nhdr == 1)
386                         buf[0] = '_';
387                 if (!strncasecmp(buf, "type=", 5))
388                         format_type = atoi(&buf[5]);
389                 if (!strncasecmp(buf, "from=", 5)) {
390                         strcpy(from, &buf[5]);
391                         wprintf("from <A HREF=\"/showuser&who=");
392                         urlescputs(from);
393                         wprintf("\">");
394                         escputs(from);
395                         wprintf("</A> ");
396                 }
397                 if (!strncasecmp(buf, "subj=", 5))
398                         strcpy(m_subject, &buf[5]);
399                 if ((!strncasecmp(buf, "hnod=", 5))
400                     && (strcasecmp(&buf[5], serv_info.serv_humannode)))
401                         wprintf("(%s) ", &buf[5]);
402                 if ((!strncasecmp(buf, "room=", 5))
403                     && (strcasecmp(&buf[5], WC->wc_roomname))
404                     && (strlen(&buf[5])>0) )
405                         wprintf("in %s> ", &buf[5]);
406                 if (!strncasecmp(buf, "rfca=", 5)) {
407                         strcpy(rfca, &buf[5]);
408                         wprintf("&lt;");
409                         escputs(rfca);
410                         wprintf("&gt; ");
411                 }
412
413                 if (!strncasecmp(buf, "node=", 5)) {
414                         strcpy(node, &buf[5]);
415                         if ( ((WC->room_flags & QR_NETWORK)
416                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
417                         && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
418                         && (strlen(rfca)==0)
419                         ) {
420                                 wprintf("@%s ", &buf[5]);
421                         }
422                 }
423                 if (!strncasecmp(buf, "rcpt=", 5))
424                         wprintf("to %s ", &buf[5]);
425                 if (!strncasecmp(buf, "time=", 5)) {
426                         fmt_date(now, atol(&buf[5]));
427                         wprintf("%s ", now);
428                 }
429
430                 if (!strncasecmp(buf, "part=", 5)) {
431                         extract(mime_filename, &buf[5], 1);
432                         extract(mime_partnum, &buf[5], 2);
433                         extract(mime_disposition, &buf[5], 3);
434                         extract(mime_content_type, &buf[5], 4);
435                         mime_length = extract_int(&buf[5], 5);
436
437                         if (!strcasecmp(mime_disposition, "attachment")) {
438                                 snprintf(&mime_http[strlen(mime_http)],
439                                         (sizeof(mime_http) - strlen(mime_http) - 1),
440                                         "<A HREF=\"/output_mimepart?"
441                                         "msgnum=%ld&partnum=%s\" "
442                                         "TARGET=\"wc.%ld.%s\">"
443                                         "<IMG SRC=\"/static/attachment.gif\" "
444                                         "BORDER=0 ALIGN=MIDDLE>\n"
445                                         "Part %s: %s (%s, %d bytes)</A><BR>\n",
446                                         msgnum, mime_partnum,
447                                         msgnum, mime_partnum,
448                                         mime_partnum, mime_filename,
449                                         mime_content_type, mime_length);
450                         }
451
452                         if ((!strcasecmp(mime_disposition, "inline"))
453                            && (!strncasecmp(mime_content_type, "image/", 6)) ){
454                                 snprintf(&mime_http[strlen(mime_http)],
455                                         (sizeof(mime_http) - strlen(mime_http) - 1),
456                                         "<IMG SRC=\"/output_mimepart?"
457                                         "msgnum=%ld&partnum=%s\">",
458                                         msgnum, mime_partnum);
459                         }
460
461                         /*** begin handler prep ***/
462                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
463                                 strcpy(vcard_partnum, mime_partnum);
464                         }
465
466                         if (!strcasecmp(mime_content_type, "text/calendar")) {
467                                 strcpy(cal_partnum, mime_partnum);
468                         }
469
470                         /*** end handler prep ***/
471
472                 }
473
474         }
475
476         /* Generate a reply-to address */
477         if (strlen(rfca) > 0) {
478                 strcpy(reply_to, rfca);
479         }
480         else {
481                 if ( (strlen(node) > 0)
482                    && (strcasecmp(node, serv_info.serv_nodename))
483                    && (strcasecmp(node, serv_info.serv_humannode)) ) {
484                         snprintf(reply_to, sizeof(reply_to), "%s @ %s",
485                                 from, node);
486                 }
487                 else {
488                         snprintf(reply_to, sizeof(reply_to), "%s", from);
489                 }
490         }
491
492         if (nhdr == 1) {
493                 wprintf("****");
494         }
495
496         wprintf("</SPAN></TD>");
497
498         wprintf("<TD ALIGN=RIGHT>\n"
499                 "<TABLE BORDER=0><TR>\n");
500
501         /***  "Read" button is now superfluous
502          ***
503          ***wprintf("<TD BGCOLOR=\"#AAAADD\">"
504          ***    "<A HREF=\"/readfwd?startmsg=%ld", msgnum);
505          ***wprintf("&maxmsgs=1&summary=0\">Read</A>"
506          ***    "</TD>\n", msgnum);
507          ***/
508
509         wprintf("<TD BGCOLOR=\"#AAAADD\">"
510                 "<A HREF=\"/display_enter?recp=");
511         urlescputs(reply_to);
512         wprintf("\"><FONT SIZE=-1>Reply</FONT></A>"
513                 "</TD>\n", msgnum);
514
515         if (WC->is_room_aide) {
516                 wprintf("<TD BGCOLOR=\"#AAAADD\">"
517                         "<A HREF=\"/confirm_move_msg"
518                         "&msgid=%ld"
519                         "\"><FONT SIZE=-1>Move</FONT></A>"
520                         "</TD>\n", msgnum);
521
522                 wprintf("<TD BGCOLOR=\"#AAAADD\">"
523                         "<A HREF=\"/delete_msg"
524                         "&msgid=%ld\""
525                         "onClick=\"return confirm('Delete this message?');\""
526                         "><FONT SIZE=-1>Del</FONT></A>"
527                         "</TD>\n", msgnum);
528         }
529
530         wprintf("</TR></TABLE>\n"
531                 "</TD>\n");
532
533         if (strlen(m_subject) > 0) {
534                 wprintf("<TR><TD>"
535                         "<SPAN CLASS=\"message_subject\">"
536                         "Subject: %s"
537                         "</SPAN>"
538                         "</TD><TD>&nbsp;</TD></TR>\n", m_subject);
539         }
540
541         wprintf("</TR></TABLE>\n");
542
543         /* Begin body */
544         wprintf("<TABLE BORDER=0 WIDTH=100%% BGCOLOR=#FFFFFF "
545                 "CELLPADDING=0 CELLSPACING=0><TR><TD>");
546
547         /* 
548          * Learn the content type
549          */
550         strcpy(mime_content_type, "text/plain");
551         while (serv_gets(buf), (strlen(buf) > 0)) {
552                 if (!strcmp(buf, "000")) {
553                         wprintf("<I>unexpected end of message</I><BR><BR>\n");
554                         goto ENDBODY;
555                 }
556                 if (!strncasecmp(buf, "Content-type: ", 14)) {
557                         safestrncpy(mime_content_type, &buf[14],
558                                 sizeof(mime_content_type));
559                 }
560         }
561
562         /* Messages in legacy Citadel variformat get handled thusly... */
563         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
564                 fmout(NULL, "JUSTIFY");
565         }
566
567         /* Boring old 80-column fixed format text gets handled this way... */
568         else if (!strcasecmp(mime_content_type, "text/plain")) {
569                 while (serv_gets(buf), strcmp(buf, "000")) {
570                         if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
571                         if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
572                         while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
573                                 buf[strlen(buf) - 1] = 0;
574                         if ((bq == 0) &&
575                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
576                                 wprintf("<SPAN CLASS=\"pull_quote\">");
577                                 bq = 1;
578                         } else if ((bq == 1) &&
579                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
580                                 wprintf("</SPAN>");
581                                 bq = 0;
582                         }
583                         wprintf("<TT>");
584                         url(buf);
585                         escputs(buf);
586                         wprintf("</TT><BR>\n");
587                 }
588                 wprintf("</I><BR>");
589         }
590
591         else /* HTML is fun, but we've got to strip it first */
592         if (!strcasecmp(mime_content_type, "text/html")) {
593                 output_html();
594         }
595
596         /* Unknown weirdness */
597         else {
598                 wprintf("I don't know how to display %s<BR>\n",
599                         mime_content_type);
600                 while (serv_gets(buf), strcmp(buf, "000")) { }
601         }
602
603
604         /* Afterwards, offer links to download attachments 'n' such */
605         if (strlen(mime_http) > 0) {
606                 wprintf("%s", mime_http);
607         }
608
609         /* Handler for vCard parts */
610         if (strlen(vcard_partnum) > 0) {
611                 part_source = load_mimepart(msgnum, vcard_partnum);
612                 if (part_source != NULL) {
613
614                         /* If it's my vCard I can edit it */
615                         if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
616                            || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
617                                 wprintf("<A HREF=\"/edit_vcard?"
618                                         "msgnum=%ld&partnum=%s\">",
619                                         msgnum, vcard_partnum);
620                                 wprintf("(edit)</A>");
621                         }
622
623                         /* In all cases, display the full card */
624                         display_vcard(part_source, 0, 1, NULL);
625                 }
626         }
627
628         /* Handler for calendar parts */
629         if (strlen(cal_partnum) > 0) {
630                 part_source = load_mimepart(msgnum, cal_partnum);
631                 if (part_source != NULL) {
632                         cal_process_attachment(part_source,
633                                                 msgnum, cal_partnum);
634                 }
635         }
636
637         if (part_source) {
638                 free(part_source);
639                 part_source = NULL;
640         }
641
642 ENDBODY:
643         wprintf("</TD></TR></TABLE>\n");
644 }
645
646
647 void summarize_message(long msgnum) {
648         char buf[SIZ];
649
650         struct {
651                 char date[SIZ];
652                 char from[SIZ];
653                 char to[SIZ];
654                 char subj[SIZ];
655                 int hasattachments;
656         } summ;
657
658         memset(&summ, 0, sizeof(summ));
659         strcpy(summ.subj, "(no subject)");
660
661         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
662         serv_puts(buf);
663         serv_gets(buf);
664         if (buf[0] != '1') return;
665
666         while (serv_gets(buf), strcmp(buf, "000")) {
667                 if (!strncasecmp(buf, "from=", 5)) {
668                         strcpy(summ.from, &buf[5]);
669                 }
670                 if (!strncasecmp(buf, "subj=", 5)) {
671                         strcpy(summ.subj, &buf[5]);
672                 }
673                 if (!strncasecmp(buf, "rfca=", 5)) {
674                         strcat(summ.from, " <");
675                         strcat(summ.from, &buf[5]);
676                         strcat(summ.from, ">");
677                 }
678
679                 if (!strncasecmp(buf, "node=", 5)) {
680                         if ( ((WC->room_flags & QR_NETWORK)
681                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
682                         && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
683                         ) {
684                                 strcat(summ.from, " @ ");
685                                 strcat(summ.from, &buf[5]);
686                         }
687                 }
688
689                 if (!strncasecmp(buf, "rcpt=", 5)) {
690                         strcpy(summ.to, &buf[5]);
691                 }
692
693                 if (!strncasecmp(buf, "time=", 5)) {
694                         fmt_date(summ.date, atol(&buf[5]));
695                 }
696         }
697
698         wprintf("<TD><A HREF=\"/readfwd?startmsg=%ld"
699                 "&maxmsgs=1&summary=0\">", 
700                 msgnum);
701         escputs(summ.subj);
702         wprintf("</A></TD><TD>");
703         escputs(summ.from);
704         wprintf(" </TD><TD>");
705         escputs(summ.date);
706         wprintf(" </TD>");
707         wprintf("<TD>"
708                 "<INPUT TYPE=\"checkbox\" NAME=\"msg_%ld\" VALUE=\"yes\">"
709                 "</TD>\n");
710
711         return;
712 }
713
714
715
716 void display_addressbook(long msgnum, char alpha) {
717         char buf[SIZ];
718         char mime_partnum[SIZ];
719         char mime_filename[SIZ];
720         char mime_content_type[SIZ];
721         char mime_disposition[SIZ];
722         int mime_length;
723         char vcard_partnum[SIZ];
724         char *vcard_source = NULL;
725
726         struct {
727                 char date[SIZ];
728                 char from[SIZ];
729                 char to[SIZ];
730                 char subj[SIZ];
731                 int hasattachments;
732         } summ;
733
734         memset(&summ, 0, sizeof(summ));
735         strcpy(summ.subj, "(no subject)");
736
737         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
738         serv_puts(buf);
739         serv_gets(buf);
740         if (buf[0] != '1') return;
741
742         while (serv_gets(buf), strcmp(buf, "000")) {
743                 if (!strncasecmp(buf, "part=", 5)) {
744                         extract(mime_filename, &buf[5], 1);
745                         extract(mime_partnum, &buf[5], 2);
746                         extract(mime_disposition, &buf[5], 3);
747                         extract(mime_content_type, &buf[5], 4);
748                         mime_length = extract_int(&buf[5], 5);
749
750                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
751                                 strcpy(vcard_partnum, mime_partnum);
752                         }
753
754                 }
755         }
756
757         if (strlen(vcard_partnum) > 0) {
758                 vcard_source = load_mimepart(msgnum, vcard_partnum);
759                 if (vcard_source != NULL) {
760
761                         /* Display the summary line */
762                         display_vcard(vcard_source, alpha, 0, NULL);
763
764                         /* If it's my vCard I can edit it */
765                         if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
766                            || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
767                                 wprintf("<A HREF=\"/edit_vcard?"
768                                         "msgnum=%ld&partnum=%s\">",
769                                         msgnum, vcard_partnum);
770                                 wprintf("(edit)</A>");
771                         }
772
773                         free(vcard_source);
774                 }
775         }
776
777 }
778
779
780
781 /* If it's an old "Firstname Lastname" style record, try to
782  * convert it.
783  */
784 void lastfirst_firstlast(char *namebuf) {
785         char firstname[SIZ];
786         char lastname[SIZ];
787         int i;
788
789         if (namebuf == NULL) return;
790         if (strchr(namebuf, ';') != NULL) return;
791
792         i = num_tokens(namebuf, ' ');
793         if (i < 2) return;
794
795         extract_token(lastname, namebuf, i-1, ' ');
796         remove_token(namebuf, i-1, ' ');
797         strcpy(firstname, namebuf);
798         sprintf(namebuf, "%s; %s", lastname, firstname);
799 }
800
801
802 void fetch_ab_name(long msgnum, char *namebuf) {
803         char buf[SIZ];
804         char mime_partnum[SIZ];
805         char mime_filename[SIZ];
806         char mime_content_type[SIZ];
807         char mime_disposition[SIZ];
808         int mime_length;
809         char vcard_partnum[SIZ];
810         char *vcard_source = NULL;
811
812         struct {
813                 char date[SIZ];
814                 char from[SIZ];
815                 char to[SIZ];
816                 char subj[SIZ];
817                 int hasattachments;
818         } summ;
819
820         if (namebuf == NULL) return;
821         strcpy(namebuf, "");
822
823         memset(&summ, 0, sizeof(summ));
824         strcpy(summ.subj, "(no subject)");
825
826         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
827         serv_puts(buf);
828         serv_gets(buf);
829         if (buf[0] != '1') return;
830
831         while (serv_gets(buf), strcmp(buf, "000")) {
832                 if (!strncasecmp(buf, "part=", 5)) {
833                         extract(mime_filename, &buf[5], 1);
834                         extract(mime_partnum, &buf[5], 2);
835                         extract(mime_disposition, &buf[5], 3);
836                         extract(mime_content_type, &buf[5], 4);
837                         mime_length = extract_int(&buf[5], 5);
838
839                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
840                                 strcpy(vcard_partnum, mime_partnum);
841                         }
842
843                 }
844         }
845
846         if (strlen(vcard_partnum) > 0) {
847                 vcard_source = load_mimepart(msgnum, vcard_partnum);
848                 if (vcard_source != NULL) {
849
850                         /* Grab the name off the card */
851                         display_vcard(vcard_source, 0, 0, namebuf);
852
853                         free(vcard_source);
854                 }
855         }
856
857         lastfirst_firstlast(namebuf);
858 }
859
860
861
862 /*
863  * Record compare function for sorting address book indices
864  */
865 int abcmp(const void *ab1, const void *ab2) {
866         return(strcasecmp(
867                 (((const struct addrbookent *)ab1)->ab_name),
868                 (((const struct addrbookent *)ab2)->ab_name)
869         ));
870 }
871
872
873 /*
874  * Render the address book using info we gathered during the scan
875  */
876 void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
877         int i = 0;
878         int bg = 0;
879
880         if (num_ab > 1) {
881                 qsort(addrbook, num_ab, sizeof(struct addrbookent), abcmp);
882         }
883
884         wprintf("<TABLE border=0 cellspacing=0 "
885                 "cellpadding=3 width=100%%>\n"
886         );
887
888         for (i=0; i<num_ab; ++i) {
889
890                 if ((i % 4) == 0) {
891                         if (i > 0) {
892                                 wprintf("</TR>\n");
893                         }
894                         bg = 1 - bg;
895                         wprintf("<TR BGCOLOR=\"#%s\">",
896                                 (bg ? "DDDDDD" : "FFFFFF")
897                         );
898                 }
899
900                 wprintf("<TD>");
901                 wprintf("<A HREF=\"/readfwd?startmsg=%ld&is_singlecard=1",
902                         addrbook[i].ab_msgnum);
903                 wprintf("&maxmsgs=1&summary=0&alpha=%s\">", bstr("alpha"));
904                 escputs(addrbook[i].ab_name);
905                 wprintf("</A></TD>\n");
906         }
907
908         wprintf("</TR></TABLE>\n");
909 }
910
911
912
913 /* 
914  * load message pointers from the server
915  */
916 int load_msg_ptrs(char *servcmd)
917 {
918         char buf[SIZ];
919         int nummsgs;
920
921         nummsgs = 0;
922         serv_puts(servcmd);
923         serv_gets(buf);
924         if (buf[0] != '1') {
925                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
926                 return (nummsgs);
927         }
928         while (serv_gets(buf), strcmp(buf, "000")) {
929                 WC->msgarr[nummsgs] = atol(buf);
930                 /* FIXME check for overflow */
931                 ++nummsgs;
932         }
933         return (nummsgs);
934 }
935
936
937 /*
938  * command loop for reading messages
939  */
940 void readloop(char *oper)
941 {
942         char cmd[SIZ];
943         char buf[SIZ];
944         int a, b, i;
945         int nummsgs;
946         long startmsg;
947         int maxmsgs;
948         int num_displayed = 0;
949         int is_summary = 0;
950         int is_addressbook = 0;
951         int is_singlecard = 0;
952         int is_calendar = 0;
953         int is_tasks = 0;
954         int remaining_messages;
955         int lo, hi;
956         int lowest_displayed = (-1);
957         int highest_displayed = 0;
958         long pn_previous = 0L;
959         long pn_current = 0L;
960         long pn_next = 0L;
961         int bg = 0;
962         char alpha = 0;
963         char ab_alpha = 0;
964         struct addrbookent *addrbook = NULL;
965         int num_ab = 0;
966
967         startmsg = atol(bstr("startmsg"));
968         maxmsgs = atoi(bstr("maxmsgs"));
969         is_summary = atoi(bstr("summary"));
970         if (maxmsgs == 0) maxmsgs = 20;
971
972         output_headers(1);
973
974         if (!strcmp(oper, "readnew")) {
975                 strcpy(cmd, "MSGS NEW");
976         } else if (!strcmp(oper, "readold")) {
977                 strcpy(cmd, "MSGS OLD");
978         } else {
979                 strcpy(cmd, "MSGS ALL");
980         }
981
982         /* FIXME put in the correct constant #defs */
983         if ((WC->wc_view == 1) && (maxmsgs > 1)) {
984                 is_summary = 1;
985                 strcpy(cmd, "MSGS ALL");
986                 maxmsgs = 32767;
987         }
988
989         if ((WC->wc_view == 2) && (maxmsgs > 1)) {
990                 is_addressbook = 1;
991                 strcpy(cmd, "MSGS ALL");
992                 maxmsgs = 32767;
993         }
994
995         is_singlecard = atoi(bstr("is_singlecard"));
996
997         /* Display the letter indices across the top */
998         if ((is_addressbook) || (is_singlecard)) {
999                 if (strlen(bstr("alpha")) == 0) {
1000                         alpha = 'a';
1001                 }
1002                 else {
1003                         strcpy(buf, bstr("alpha"));
1004                         alpha = buf[0];
1005                 }
1006
1007                 for (i='1'; i<='z'; ++i) if ((i=='1')||(islower(i))) {
1008                         if ((i != alpha) || (is_singlecard)) {
1009                                 wprintf("<A HREF=\"/readfwd?alpha=%c\">", i);
1010                         }
1011                         if (i == alpha) wprintf("<FONT SIZE=+2>");
1012                         if (isalpha(i)) {
1013                                 wprintf("%c", toupper(i));
1014                         }
1015                         else {
1016                                 wprintf("(other)");
1017                         }
1018                         if (i == alpha) wprintf("</FONT>");
1019                         if ((i != alpha) || (is_singlecard)) {
1020                                 wprintf("</A>\n");
1021                         }
1022                         wprintf("&nbsp;");
1023                 }
1024
1025                 wprintf("<HR width=100%%>\n");
1026         }
1027
1028         if (WC->wc_view == 3) {         /* calendar */
1029                 is_calendar = 1;
1030                 strcpy(cmd, "MSGS ALL");
1031                 maxmsgs = 32767;
1032         }
1033         if (WC->wc_view == 4) {         /* tasks */
1034                 is_tasks = 1;
1035                 strcpy(cmd, "MSGS ALL");
1036                 maxmsgs = 32767;
1037                 wprintf("<UL>");
1038         }
1039
1040         nummsgs = load_msg_ptrs(cmd);
1041         if (nummsgs == 0) {
1042
1043                 if ((!is_tasks) && (!is_calendar)) {
1044                         if (!strcmp(oper, "readnew")) {
1045                                 wprintf("<EM>No new messages in this room.</EM>\n");
1046                         } else if (!strcmp(oper, "readold")) {
1047                                 wprintf("<EM>No old messages in this room.</EM>\n");
1048                         } else {
1049                                 wprintf("<EM>This room is empty.</EM>\n");
1050                         }
1051                 }
1052
1053                 goto DONE;
1054         }
1055
1056         if (startmsg == 0L) startmsg = WC->msgarr[0];
1057         remaining_messages = 0;
1058
1059         for (a = 0; a < nummsgs; ++a) {
1060                 if (WC->msgarr[a] >= startmsg) {
1061                         ++remaining_messages;
1062                 }
1063         }
1064
1065         if (is_summary) {
1066                 wprintf("<FORM METHOD=\"POST\" ACTION=\"/do_stuff_to_msgs\">\n"
1067                         "<TABLE border=0 cellspacing=0 "
1068                         "cellpadding=0 width=100%%>\n"
1069                         "<TR>"
1070                         "<TD><I>Subject</I></TD>"
1071                         "<TD><I>Sender</I></TD>"
1072                         "<TD><I>Date</I></TD>"
1073                         "<TD></TD>"
1074                         "</TR>\n"
1075                 );
1076         }
1077
1078         for (a = 0; a < nummsgs; ++a) {
1079                 if ((WC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
1080
1081                         /* Learn which msgs "Prev" & "Next" buttons go to */
1082                         pn_current = WC->msgarr[a];
1083                         if (a > 0) pn_previous = WC->msgarr[a-1];
1084                         if (a < (nummsgs-1)) pn_next = WC->msgarr[a+1];
1085
1086                         /* If a tabular view, set up the line */
1087                         if (is_summary) {
1088                                 bg = 1 - bg;
1089                                 wprintf("<TR BGCOLOR=\"#%s\">",
1090                                         (bg ? "DDDDDD" : "FFFFFF")
1091                                 );
1092                         }
1093
1094                         /* Display the message */
1095                         if (is_summary) {
1096                                 summarize_message(WC->msgarr[a]);
1097                         }
1098                         else if (is_addressbook) {
1099                                 fetch_ab_name(WC->msgarr[a], buf);
1100                                 if ((strlen(buf) > 0) && (isalpha(buf[0]))) {
1101                                         ab_alpha = tolower(buf[0]);
1102                                 }
1103                                 else {
1104                                         ab_alpha = '1';
1105                                 }
1106                                 if (alpha == ab_alpha) {
1107                                         ++num_ab;
1108                                         addrbook = realloc(addrbook,
1109                                                 (sizeof(struct addrbookent) * num_ab) );
1110                                         safestrncpy(addrbook[num_ab-1].ab_name, buf,
1111                                                 sizeof(addrbook[num_ab-1].ab_name));
1112                                         addrbook[num_ab-1].ab_msgnum = WC->msgarr[a];
1113                                 }
1114                         }
1115                         else if (is_calendar) {
1116                                 display_calendar(WC->msgarr[a]);
1117                         }
1118                         else if (is_tasks) {
1119                                 display_task(WC->msgarr[a]);
1120                         }
1121                         else {
1122                                 read_message(WC->msgarr[a]);
1123                         }
1124
1125                         /* If a tabular view, finish the line */
1126                         if (is_summary) {
1127                                 wprintf("</TR>\n");
1128                         }
1129
1130                         if (lowest_displayed < 0) lowest_displayed = a;
1131                         highest_displayed = a;
1132
1133                         ++num_displayed;
1134                         --remaining_messages;
1135                 }
1136         }
1137
1138         if (is_summary) {
1139                 wprintf("</TABLE>\n");
1140         }
1141
1142         if (is_tasks) {
1143                 wprintf("</UL>\n");
1144         }
1145
1146         /* Bump these because although we're thinking in zero base, the user
1147          * is a drooling idiot and is thinking in one base.
1148          */
1149         ++lowest_displayed;
1150         ++highest_displayed;
1151
1152         /* If we're only looking at one message, do a prev/next thing */
1153         if (num_displayed == 1) {
1154            if ((!is_tasks) && (!is_calendar) && (!is_addressbook) && (!is_singlecard)) {
1155
1156                 wprintf("<CENTER>"
1157                         "<TABLE BORDER=0 WIDTH=100%% BGCOLOR=\"#DDDDDD\"><TR><TD>"
1158                         "Reading #%d of %d messages.</TD>\n"
1159                         "<TD ALIGN=RIGHT><FONT SIZE=+1>",
1160                         lowest_displayed, nummsgs);
1161
1162                 if (is_summary) {
1163                         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1164                                 "VALUE=\"Delete selected\">\n");
1165                 }
1166
1167                 if (pn_previous > 0L) {
1168                         wprintf("<A HREF=\"/%s"
1169                                 "?startmsg=%ld"
1170                                 "&maxmsgs=1"
1171                                 "&summary=0\">"
1172                                 "Previous</A> \n",
1173                                         oper,
1174                                         pn_previous );
1175                 }
1176
1177                 if (pn_next > 0L) {
1178                         wprintf("<A HREF=\"/%s"
1179                                 "?startmsg=%ld"
1180                                 "&maxmsgs=1"
1181                                 "&summary=0\">"
1182                                 "Next</A> \n",
1183                                         oper,
1184                                         pn_next );
1185                 }
1186
1187                 wprintf("<A HREF=\"/%s?startmsg=%ld"
1188                         "&maxmsgs=999999&summary=1\">"
1189                         "Summary"
1190                         "</A>",
1191                         oper,
1192                         WC->msgarr[0]);
1193
1194                 wprintf("</TD></TR></TABLE></CENTER>\n");
1195             }
1196         }
1197
1198
1199         /*
1200          * If we're not currently looking at ALL requested
1201          * messages, then display the selector bar
1202          */
1203         if (num_displayed > 1) {
1204            if ((!is_tasks) && (!is_calendar) && (!is_addressbook) && (!is_singlecard)) {
1205                 wprintf("<CENTER>"
1206                         "<TABLE BORDER=0 WIDTH=100%% BGCOLOR=\"#DDDDDD\"><TR><TD>"
1207                         "Reading #%d-%d of %d messages.</TD>\n"
1208                         "<TD ALIGN=RIGHT><FONT SIZE=+1>",
1209                         lowest_displayed, highest_displayed, nummsgs);
1210
1211                 if (is_summary) {
1212                         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1213                                 "VALUE=\"Delete selected\">\n");
1214                 }
1215
1216
1217                 for (b=0; b<nummsgs; b = b + maxmsgs) {
1218                 lo = b+1;
1219                 hi = b+maxmsgs;
1220                 if (hi > nummsgs) hi = nummsgs;
1221                         if (WC->msgarr[b] != startmsg) {
1222                                 wprintf("<A HREF=\"/%s"
1223                                         "?startmsg=%ld"
1224                                         "&maxmsgs=%d"
1225                                         "&summary=%d\">"
1226                                         "%d-%d</A> \n",
1227                                                 oper,
1228                                                 WC->msgarr[b],
1229                                                 maxmsgs,
1230                                                 is_summary,
1231                                                 lo, hi);
1232                         }
1233                         else {
1234                                 wprintf("%d-%d \n", lo, hi);
1235                         }
1236
1237                 }
1238                 wprintf("<A HREF=\"/%s?startmsg=%ld"
1239                         "&maxmsgs=999999&summary=%d\">"
1240                         "ALL"
1241                         "</A> ",
1242                         oper,
1243                         WC->msgarr[0], is_summary);
1244
1245                 wprintf("<A HREF=\"/%s?startmsg=%ld"
1246                         "&maxmsgs=999999&summary=1\">"
1247                         "Summary"
1248                         "</A>",
1249                         oper,
1250                         WC->msgarr[0]);
1251
1252                 wprintf("</TD></TR></TABLE></CENTER>\n");
1253             }
1254         }
1255         if (is_summary) wprintf("</FORM>\n");
1256
1257 DONE:
1258         if (is_tasks) {
1259                 wprintf("<A HREF=\"/display_edit_task?msgnum=0\">"
1260                         "Add new task</A>\n"
1261                 );
1262         }
1263
1264         if (is_calendar) {
1265                 do_calendar_view();     /* Render the calendar */
1266         }
1267
1268         if (is_addressbook) {
1269                 do_addrbook_view(addrbook, num_ab);     /* Render the address book */
1270         }
1271
1272         wDumpContent(1);
1273         if (addrbook != NULL) free(addrbook);
1274 }
1275
1276
1277 /*
1278  * Back end for post_message() ... this is where the actual message
1279  * gets transmitted to the server.
1280  */
1281 void post_mime_to_server(void) {
1282         char boundary[SIZ];
1283         int is_multipart = 0;
1284         static int seq = 0;
1285         struct wc_attachment *att;
1286         char *encoded;
1287         size_t encoded_length;
1288         
1289         /* If there are attachments, we have to do multipart/mixed */
1290         if (WC->first_attachment != NULL) {
1291                 is_multipart = 1;
1292         }
1293
1294         if (is_multipart) {
1295                 sprintf(boundary, "---Citadel-Multipart-%s-%04x%04x---",
1296                         serv_info.serv_fqdn,
1297                         getpid(),
1298                         ++seq
1299                 );
1300
1301                 /* Remember, serv_printf() appends an extra newline */
1302                 serv_printf("Content-type: multipart/mixed; "
1303                         "boundary=\"%s\"\n", boundary);
1304                 serv_printf("This is a multipart message in MIME format.\n");
1305                 serv_printf("--%s", boundary);
1306         }
1307
1308         serv_puts("Content-type: text/html");
1309         serv_puts("");
1310         text_to_server(bstr("msgtext"), 1);
1311
1312         if (is_multipart) {
1313
1314                 /* Add in the attachments */
1315                 for (att = WC->first_attachment; att!=NULL; att=att->next) {
1316
1317                         encoded_length = ((att->length * 150) / 100);
1318                         encoded = malloc(encoded_length);
1319                         if (encoded == NULL) break;
1320                         CtdlEncodeBase64(encoded, att->data, att->length);
1321
1322                         serv_printf("--%s", boundary);
1323                         serv_printf("Content-type: %s", att->content_type);
1324                         serv_printf("Content-disposition: attachment; "
1325                                 "filename=\"%s\"", att->filename);
1326                         serv_puts("Content-transfer-encoding: base64");
1327                         serv_puts("");
1328                         serv_write(encoded, strlen(encoded));
1329                         serv_puts("");
1330                         serv_puts("");
1331                         free(encoded);
1332                 }
1333                 serv_printf("--%s--", boundary);
1334         }
1335
1336         serv_puts("000");
1337 }
1338
1339
1340 /*
1341  * Post message (or don't post message)
1342  *
1343  * Note regarding the "dont_post" variable:
1344  * A random value (actually, it's just a timestamp) is inserted as a hidden
1345  * field called "postseq" when the display_enter page is generated.  This
1346  * value is checked when posting, using the static variable dont_post.  If a
1347  * user attempts to post twice using the same dont_post value, the message is
1348  * discarded.  This prevents the accidental double-saving of the same message
1349  * if the user happens to click the browser "back" button.
1350  */
1351 void post_message(void)
1352 {
1353         char buf[SIZ];
1354         static long dont_post = (-1L);
1355         struct wc_attachment *att;
1356
1357         if (WC->upload_length > 0) {
1358
1359                 att = malloc(sizeof(struct wc_attachment));
1360                 memset(att, 0, sizeof(struct wc_attachment));
1361                 att->next = WC->first_attachment;
1362                 WC->first_attachment = att;
1363                 att->length = WC->upload_length;
1364                 strcpy(att->content_type, WC->upload_content_type);
1365                 strcpy(att->filename, WC->upload_filename);
1366
1367                 /* Netscape sends a simple filename, which is what we want,
1368                  * but Satan's browser sends an entire pathname.  Reduce
1369                  * the path to just a filename if we need to.
1370                  */
1371                 while (num_tokens(att->filename, '/') > 1) {
1372                         remove_token(att->filename, 0, '/');
1373                 }
1374                 while (num_tokens(att->filename, '\\') > 1) {
1375                         remove_token(att->filename, 0, '\\');
1376                 }
1377
1378                 /* Transfer control of this memory from the upload struct
1379                  * to the attachment struct.
1380                  */
1381                 att->data = WC->upload;
1382                 WC->upload_length = 0;
1383                 WC->upload = NULL;
1384                 display_enter();
1385                 return;
1386         }
1387
1388         if (strcasecmp(bstr("sc"), "Save message")) {
1389                 sprintf(WC->ImportantMessage, 
1390                         "Cancelled.  Message was not posted.");
1391         } else if (atol(bstr("postseq")) == dont_post) {
1392                 sprintf(WC->ImportantMessage, 
1393                         "Automatically cancelled because you have already "
1394                         "saved this message.");
1395         } else {
1396                 sprintf(buf, "ENT0 1|%s|0|4|%s",
1397                         bstr("recp"),
1398                         bstr("subject") );
1399                 serv_puts(buf);
1400                 serv_gets(buf);
1401                 if (buf[0] == '4') {
1402                         post_mime_to_server();
1403                         sprintf(WC->ImportantMessage, 
1404                                 "Message has been posted.\n");
1405                         dont_post = atol(bstr("postseq"));
1406                 } else {
1407                         sprintf(WC->ImportantMessage, 
1408                                 "%s", &buf[4]);
1409                 }
1410         }
1411
1412         free_attachments(WC);
1413         readloop("readnew");
1414 }
1415
1416
1417
1418
1419 /*
1420  * display the message entry screen
1421  */
1422 void display_enter(void)
1423 {
1424         char buf[SIZ];
1425         long now;
1426         struct wc_attachment *att;
1427
1428         if (strlen(bstr("force_room")) > 0) {
1429                 gotoroom(bstr("force_room"), 0);
1430         }
1431
1432         /* Are we perhaps in an address book view?  If so, then an "enter
1433          * message" command really means "add new entry."
1434          */
1435         if (WC->wc_view == 2) {
1436                 do_edit_vcard(-1, "", "");
1437                 return;
1438         }
1439
1440         /* Otherwise proceed normally */
1441         output_headers(1);
1442         sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
1443         serv_puts(buf);
1444         serv_gets(buf);
1445
1446         if (!strncmp(buf, "570", 3)) {
1447                 if (strlen(bstr("recp")) > 0) {
1448                         svprintf("RECPERROR", WCS_STRING,
1449                                 "<SPAN CLASS=\"errormsg\">%s</SPAN><BR>\n",
1450                                 &buf[4]
1451                         );
1452                 }
1453                 do_template("prompt_for_recipient");
1454                 goto DONE;
1455         }
1456         if (buf[0] != '2') {
1457                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1458                 goto DONE;
1459         }
1460
1461         now = time(NULL);
1462         fmt_date(buf, now);
1463         strcat(&buf[strlen(buf)], " <I>from</I> ");
1464         stresc(&buf[strlen(buf)], WC->wc_username, 1);
1465         if (strlen(bstr("recp")) > 0) {
1466                 strcat(&buf[strlen(buf)], " <I>to</I> ");
1467                 stresc(&buf[strlen(buf)], bstr("recp"), 1);
1468         }
1469         strcat(&buf[strlen(buf)], " <I>in</I> ");
1470         stresc(&buf[strlen(buf)], WC->wc_roomname, 1);
1471         svprintf("BOXTITLE", WCS_STRING, buf);
1472         do_template("beginbox");
1473
1474         wprintf("<CENTER>\n");
1475
1476         wprintf("<FORM ENCTYPE=\"multipart/form-data\" "
1477                 "METHOD=\"POST\" ACTION=\"/post\" "
1478                 "NAME=\"enterform\">\n");
1479         wprintf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"%s\">\n",
1480                 bstr("recp"));
1481         wprintf("<INPUT TYPE=\"hidden\" NAME=\"postseq\" VALUE=\"%ld\">\n",
1482                 now);
1483         wprintf("<IMG SRC=\"static/enter.gif\" ALIGN=MIDDLE ALT=\" \" "
1484                 "onLoad=\"document.enterform.msgtext.focus();\" >");
1485         wprintf("<FONT SIZE=-1>Subject (optional):</FONT>"
1486                 "<INPUT TYPE=\"text\" NAME=\"subject\" VALUE=\"");
1487         escputs(bstr("subject"));
1488         wprintf("\" MAXLENGTH=70>"
1489                 "&nbsp;&nbsp;&nbsp;"
1490                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save message\">"
1491                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\"><BR>\n");
1492
1493         wprintf("<TEXTAREA NAME=\"msgtext\" wrap=soft ROWS=25 COLS=80 "
1494                 "WIDTH=80>");
1495         escputs(bstr("msgtext"));
1496         wprintf("</TEXTAREA><BR>\n");
1497
1498         /* Enumerate any attachments which are already in place... */
1499         for (att = WC->first_attachment; att != NULL; att = att->next) {
1500                 wprintf("<IMG SRC=\"/static/attachment.gif\" "
1501                         "BORDER=0 ALIGN=MIDDLE> Attachment: ");
1502                 escputs(att->filename);
1503                 wprintf(" (%s, %d bytes)<BR>\n",
1504                         att->content_type, att->length);
1505         }
1506
1507         /* Now offer the ability to attach additional files... */
1508         wprintf("Attach file: <input NAME=\"attachfile\" "
1509                 "SIZE=48 TYPE=\"file\">\n&nbsp;&nbsp;"
1510                 "<input type=\"submit\" name=\"attach\" value=\"Add\">\n");
1511
1512         wprintf("</FORM></CENTER>\n");
1513         do_template("endbox");
1514 DONE:   wDumpContent(1);
1515 }
1516
1517
1518
1519
1520
1521
1522
1523
1524 void delete_msg(void)
1525 {
1526         long msgid;
1527         char buf[SIZ];
1528
1529         msgid = atol(bstr("msgid"));
1530
1531         output_headers(1);
1532
1533         sprintf(buf, "DELE %ld", msgid);
1534         serv_puts(buf);
1535         serv_gets(buf);
1536         wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1537
1538         wDumpContent(1);
1539 }
1540
1541
1542
1543
1544 /*
1545  * Confirm move of a message
1546  */
1547 void confirm_move_msg(void)
1548 {
1549         long msgid;
1550         char buf[SIZ];
1551         char targ[SIZ];
1552
1553         msgid = atol(bstr("msgid"));
1554
1555         output_headers(1);
1556
1557         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#770000\"><TR><TD>");
1558         wprintf("<FONT SIZE=+1 COLOR=\"#FFFFFF\"");
1559         wprintf("<B>Confirm move of message</B>\n");
1560         wprintf("</FONT></TD></TR></TABLE>\n");
1561
1562         wprintf("<CENTER>");
1563
1564         wprintf("Please select the room to which you would like this message moved:<BR>\n");
1565
1566         wprintf("<FORM METHOD=\"POST\" ACTION=\"/move_msg\">\n");
1567         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
1568                 bstr("msgid"));
1569
1570
1571         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1572         serv_puts("LKRA");
1573         serv_gets(buf);
1574         if (buf[0] == '1') {
1575                 while (serv_gets(buf), strcmp(buf, "000")) {
1576                         extract(targ, buf, 0);
1577                         wprintf("<OPTION>");
1578                         escputs(targ);
1579                         wprintf("\n");
1580                 }
1581         }
1582         wprintf("</SELECT>\n");
1583         wprintf("<BR>\n");
1584
1585         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
1586         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
1587         wprintf("</FORM></CENTER>\n");
1588
1589         wprintf("</CENTER>\n");
1590         wDumpContent(1);
1591 }
1592
1593
1594
1595 void move_msg(void)
1596 {
1597         long msgid;
1598         char buf[SIZ];
1599
1600         msgid = atol(bstr("msgid"));
1601
1602         output_headers(1);
1603
1604         if (!strcasecmp(bstr("yesno"), "Move")) {
1605                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1606                 serv_puts(buf);
1607                 serv_gets(buf);
1608                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1609         } else {
1610                 wprintf("<EM>Message not moved.</EM><BR>\n");
1611         }
1612
1613         wDumpContent(1);
1614 }
1615
1616
1617
1618 void do_stuff_to_msgs(void) {
1619         char buf[SIZ];
1620         char sc[SIZ];
1621
1622         struct stuff_t {
1623                 struct stuff_t *next;
1624                 long msgnum;
1625         };
1626
1627         struct stuff_t *stuff = NULL;
1628         struct stuff_t *ptr;
1629
1630
1631         serv_puts("MSGS ALL");
1632         serv_gets(buf);
1633
1634         if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
1635                 ptr = malloc(sizeof(struct stuff_t));
1636                 ptr->msgnum = atol(buf);
1637                 ptr->next = stuff;
1638                 stuff = ptr;
1639         }
1640
1641         strcpy(sc, bstr("sc"));
1642
1643         while (stuff != NULL) {
1644
1645                 sprintf(buf, "msg_%ld", stuff->msgnum);
1646                 if (!strcasecmp(bstr(buf), "yes")) {
1647
1648                         if (!strcasecmp(sc, "Delete selected")) {
1649                                 serv_printf("DELE %ld", stuff->msgnum);
1650                                 serv_gets(buf);
1651                         }
1652
1653                 }
1654
1655                 ptr = stuff->next;
1656                 free(stuff);
1657                 stuff = ptr;
1658         }
1659
1660         readloop("readfwd");
1661 }