* Started changing some of the top-level tables from 100% width to 99%
[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         /* begin everythingamundo table */
372         wprintf("<center><table width=99%% border=1 cellspacing=0 "
373                 "cellpadding=0><TR><TD>\n");
374
375         /* begin message header table */
376         wprintf("<TABLE WIDTH=100%% BORDER=0 CELLSPACING=0 "
377                 "CELLPADDING=1 BGCOLOR=\"#CCCCCC\"><TR><TD>\n");
378
379         wprintf("<SPAN CLASS=\"message_header\">");
380         strcpy(m_subject, "");
381
382         while (serv_gets(buf), strcasecmp(buf, "text")) {
383                 if (!strcmp(buf, "000")) {
384                         wprintf("<I>unexpected end of message</I><br /><br />\n");
385                         wprintf("</SPAN>\n");
386                         return;
387                 }
388                 if (!strncasecmp(buf, "nhdr=yes", 8))
389                         nhdr = 1;
390                 if (nhdr == 1)
391                         buf[0] = '_';
392                 if (!strncasecmp(buf, "type=", 5))
393                         format_type = atoi(&buf[5]);
394                 if (!strncasecmp(buf, "from=", 5)) {
395                         strcpy(from, &buf[5]);
396                         wprintf("from <A HREF=\"/showuser&who=");
397                         urlescputs(from);
398                         wprintf("\">");
399                         escputs(from);
400                         wprintf("</A> ");
401                 }
402                 if (!strncasecmp(buf, "subj=", 5))
403                         strcpy(m_subject, &buf[5]);
404                 if ((!strncasecmp(buf, "hnod=", 5))
405                     && (strcasecmp(&buf[5], serv_info.serv_humannode)))
406                         wprintf("(%s) ", &buf[5]);
407                 if ((!strncasecmp(buf, "room=", 5))
408                     && (strcasecmp(&buf[5], WC->wc_roomname))
409                     && (strlen(&buf[5])>0) )
410                         wprintf("in %s> ", &buf[5]);
411                 if (!strncasecmp(buf, "rfca=", 5)) {
412                         strcpy(rfca, &buf[5]);
413                         wprintf("&lt;");
414                         escputs(rfca);
415                         wprintf("&gt; ");
416                 }
417
418                 if (!strncasecmp(buf, "node=", 5)) {
419                         strcpy(node, &buf[5]);
420                         if ( ((WC->room_flags & QR_NETWORK)
421                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
422                         && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
423                         && (strlen(rfca)==0)
424                         ) {
425                                 wprintf("@%s ", &buf[5]);
426                         }
427                 }
428                 if (!strncasecmp(buf, "rcpt=", 5))
429                         wprintf("to %s ", &buf[5]);
430                 if (!strncasecmp(buf, "time=", 5)) {
431                         fmt_date(now, atol(&buf[5]));
432                         wprintf("%s ", now);
433                 }
434
435                 if (!strncasecmp(buf, "part=", 5)) {
436                         extract(mime_filename, &buf[5], 1);
437                         extract(mime_partnum, &buf[5], 2);
438                         extract(mime_disposition, &buf[5], 3);
439                         extract(mime_content_type, &buf[5], 4);
440                         mime_length = extract_int(&buf[5], 5);
441
442                         if (!strcasecmp(mime_disposition, "attachment")) {
443                                 snprintf(&mime_http[strlen(mime_http)],
444                                         (sizeof(mime_http) - strlen(mime_http) - 1),
445                                         "<A HREF=\"/output_mimepart?"
446                                         "msgnum=%ld&partnum=%s\" "
447                                         "TARGET=\"wc.%ld.%s\">"
448                                         "<IMG SRC=\"/static/attachment.gif\" "
449                                         "BORDER=0 ALIGN=MIDDLE>\n"
450                                         "Part %s: %s (%s, %d bytes)</A><br />\n",
451                                         msgnum, mime_partnum,
452                                         msgnum, mime_partnum,
453                                         mime_partnum, mime_filename,
454                                         mime_content_type, mime_length);
455                         }
456
457                         if ((!strcasecmp(mime_disposition, "inline"))
458                            && (!strncasecmp(mime_content_type, "image/", 6)) ){
459                                 snprintf(&mime_http[strlen(mime_http)],
460                                         (sizeof(mime_http) - strlen(mime_http) - 1),
461                                         "<IMG SRC=\"/output_mimepart?"
462                                         "msgnum=%ld&partnum=%s\">",
463                                         msgnum, mime_partnum);
464                         }
465
466                         /*** begin handler prep ***/
467                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
468                                 strcpy(vcard_partnum, mime_partnum);
469                         }
470
471                         if (!strcasecmp(mime_content_type, "text/calendar")) {
472                                 strcpy(cal_partnum, mime_partnum);
473                         }
474
475                         /*** end handler prep ***/
476
477                 }
478
479         }
480
481         /* Generate a reply-to address */
482         if (strlen(rfca) > 0) {
483                 strcpy(reply_to, rfca);
484         }
485         else {
486                 if ( (strlen(node) > 0)
487                    && (strcasecmp(node, serv_info.serv_nodename))
488                    && (strcasecmp(node, serv_info.serv_humannode)) ) {
489                         snprintf(reply_to, sizeof(reply_to), "%s @ %s",
490                                 from, node);
491                 }
492                 else {
493                         snprintf(reply_to, sizeof(reply_to), "%s", from);
494                 }
495         }
496
497         if (nhdr == 1) {
498                 wprintf("****");
499         }
500
501         wprintf("</SPAN>");
502         if (strlen(m_subject) > 0) {
503                 wprintf("<br />"
504                         "<SPAN CLASS=\"message_subject\">"
505                         "Subject: %s"
506                         "</SPAN>", m_subject
507                 );
508         }
509         wprintf("</TD>\n");
510
511         /* start msg buttons */
512         wprintf("<TD ALIGN=RIGHT>\n");
513         wprintf("<FORM METHOD=\"POST\" ACTION=\"/do_stuff_to_one_msg\">\n");
514         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%ld\">\n",
515                 msgnum);
516         wprintf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"");
517         escputs(reply_to);
518         wprintf("\">\n");
519
520         if (!strncasecmp(m_subject, "Re:", 2)) {
521                 wprintf("<INPUT TYPE=\"hidden\" NAME=\"subject\" VALUE=\"");
522                 escputs(m_subject);
523                 wprintf("\">\n");
524         }
525         else if (strlen(m_subject) > 0) {
526                 wprintf("<INPUT TYPE=\"hidden\" NAME=\"subject\" VALUE=\"Re: ");
527                 escputs(m_subject);
528                 wprintf("\">\n");
529         }
530
531         wprintf("<INPUT TYPE=\"submit\" NAME=\"msg_oper\" STYLE=\"font-family: Bitstream Vera Sans,Arial,Helvetica,sans-serif; font-size: 7pt; background: blue; color: #FFFFFF;\" VALUE=\"Reply\">\n");
532
533         if (WC->is_room_aide)  {
534                 wprintf("<INPUT TYPE=\"submit\" NAME=\"msg_oper\" STYLE=\"font-family: Bitstream Vera Sans,Arial,Helvetica,sans-serif; font-size: 7pt; background: blue; color: #FFFFFF;\"VALUE=\"Move\">\n"
535                         "<INPUT TYPE=\"submit\" NAME=\"msg_oper\" STYLE=\"font-family: Bitstream Vera Sans,Arial,Helvetica,sans-serif; font-size: 7pt; background: blue; color: #FFFFFF;\" VALUE=\"Delete\""
536                         "onClick=\"return confirm('Delete this message?');\">\n");
537         }
538
539         wprintf("</FORM>\n"
540                 "</TD></TR></TABLE>\n");
541
542         /* Begin body */
543         wprintf("<TABLE BORDER=0 WIDTH=100%% BGCOLOR=#FFFFFF "
544                 "CELLPADDING=1 CELLSPACING=0><TR><TD>");
545
546         /* 
547          * Learn the content type
548          */
549         strcpy(mime_content_type, "text/plain");
550         while (serv_gets(buf), (strlen(buf) > 0)) {
551                 if (!strcmp(buf, "000")) {
552                         wprintf("<I>unexpected end of message</I><br /><br />\n");
553                         goto ENDBODY;
554                 }
555                 if (!strncasecmp(buf, "Content-type: ", 14)) {
556                         safestrncpy(mime_content_type, &buf[14],
557                                 sizeof(mime_content_type));
558                 }
559         }
560
561         /* Messages in legacy Citadel variformat get handled thusly... */
562         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
563                 fmout(NULL, "JUSTIFY");
564         }
565
566         /* Boring old 80-column fixed format text gets handled this way... */
567         else if (!strcasecmp(mime_content_type, "text/plain")) {
568                 while (serv_gets(buf), strcmp(buf, "000")) {
569                         if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
570                         if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
571                         while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
572                                 buf[strlen(buf) - 1] = 0;
573                         if ((bq == 0) &&
574                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
575                                 wprintf("<BLOCKQUOTE>");
576                                 bq = 1;
577                         } else if ((bq == 1) &&
578                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
579                                 wprintf("</BLOCKQUOTE>");
580                                 bq = 0;
581                         }
582                         wprintf("<TT>");
583                         url(buf);
584                         escputs(buf);
585                         wprintf("</TT><br />\n");
586                 }
587                 wprintf("</I><br />");
588         }
589
590         else /* HTML is fun, but we've got to strip it first */
591         if (!strcasecmp(mime_content_type, "text/html")) {
592                 output_html();
593         }
594
595         /* Unknown weirdness */
596         else {
597                 wprintf("I don't know how to display %s<br />\n",
598                         mime_content_type);
599                 while (serv_gets(buf), strcmp(buf, "000")) { }
600         }
601
602
603         /* Afterwards, offer links to download attachments 'n' such */
604         if (strlen(mime_http) > 0) {
605                 wprintf("%s", mime_http);
606         }
607
608         /* Handler for vCard parts */
609         if (strlen(vcard_partnum) > 0) {
610                 part_source = load_mimepart(msgnum, vcard_partnum);
611                 if (part_source != NULL) {
612
613                         /* If it's my vCard I can edit it */
614                         if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
615                            || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
616                                 wprintf("<A HREF=\"/edit_vcard?"
617                                         "msgnum=%ld&partnum=%s\">",
618                                         msgnum, vcard_partnum);
619                                 wprintf("(edit)</A>");
620                         }
621
622                         /* In all cases, display the full card */
623                         display_vcard(part_source, 0, 1, NULL);
624                 }
625         }
626
627         /* Handler for calendar parts */
628         if (strlen(cal_partnum) > 0) {
629                 part_source = load_mimepart(msgnum, cal_partnum);
630                 if (part_source != NULL) {
631                         cal_process_attachment(part_source,
632                                                 msgnum, cal_partnum);
633                 }
634         }
635
636         if (part_source) {
637                 free(part_source);
638                 part_source = NULL;
639         }
640
641 ENDBODY:
642         wprintf("</TD></TR></TABLE>\n");
643
644         /* end everythingamundo table */
645         wprintf("</TD></TR></TABLE></center><br />\n");
646 }
647
648
649 void summarize_message(long msgnum, int is_new) {
650         char buf[SIZ];
651
652         struct {
653                 char date[SIZ];
654                 char from[SIZ];
655                 char to[SIZ];
656                 char subj[SIZ];
657                 int hasattachments;
658         } summ;
659
660         memset(&summ, 0, sizeof(summ));
661         strcpy(summ.subj, "(no subject)");
662
663         sprintf(buf, "MSG0 %ld|3", msgnum);     /* ask for headers only with no MIME */
664         serv_puts(buf);
665         serv_gets(buf);
666         if (buf[0] != '1') return;
667
668         while (serv_gets(buf), strcmp(buf, "000")) {
669                 if (!strncasecmp(buf, "from=", 5)) {
670                         strcpy(summ.from, &buf[5]);
671                 }
672                 if (!strncasecmp(buf, "subj=", 5)) {
673                         strcpy(summ.subj, &buf[5]);
674                 }
675                 if (!strncasecmp(buf, "rfca=", 5)) {
676                         strcat(summ.from, " <");
677                         strcat(summ.from, &buf[5]);
678                         strcat(summ.from, ">");
679                 }
680
681                 if (!strncasecmp(buf, "node=", 5)) {
682                         if ( ((WC->room_flags & QR_NETWORK)
683                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
684                         && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
685                         ) {
686                                 strcat(summ.from, " @ ");
687                                 strcat(summ.from, &buf[5]);
688                         }
689                 }
690
691                 if (!strncasecmp(buf, "rcpt=", 5)) {
692                         strcpy(summ.to, &buf[5]);
693                 }
694
695                 if (!strncasecmp(buf, "time=", 5)) {
696                         fmt_date(summ.date, atol(&buf[5]));
697                 }
698         }
699
700         wprintf("<TD>");
701         if (is_new) wprintf("<B>");
702         wprintf("<A HREF=\"/readfwd?startmsg=%ld"
703                 "&maxmsgs=1&summary=0\">", 
704                 msgnum);
705         escputs(summ.subj);
706         wprintf("</A>");
707         if (is_new) wprintf("</B>");
708         wprintf("</TD><TD>");
709         if (is_new) wprintf("<B>");
710         escputs(summ.from);
711         if (is_new) wprintf("</B>");
712         wprintf(" </TD><TD>");
713         if (is_new) wprintf("<B>");
714         escputs(summ.date);
715         if (is_new) wprintf("</B>");
716         wprintf(" </TD>");
717         wprintf("<TD>"
718                 "<INPUT TYPE=\"checkbox\" NAME=\"msg_%ld\" VALUE=\"yes\">"
719                 "</TD>\n");
720
721         return;
722 }
723
724
725
726 void display_addressbook(long msgnum, char alpha) {
727         char buf[SIZ];
728         char mime_partnum[SIZ];
729         char mime_filename[SIZ];
730         char mime_content_type[SIZ];
731         char mime_disposition[SIZ];
732         int mime_length;
733         char vcard_partnum[SIZ];
734         char *vcard_source = NULL;
735
736         struct {
737                 char date[SIZ];
738                 char from[SIZ];
739                 char to[SIZ];
740                 char subj[SIZ];
741                 int hasattachments;
742         } summ;
743
744         memset(&summ, 0, sizeof(summ));
745         strcpy(summ.subj, "(no subject)");
746
747         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
748         serv_puts(buf);
749         serv_gets(buf);
750         if (buf[0] != '1') return;
751
752         while (serv_gets(buf), strcmp(buf, "000")) {
753                 if (!strncasecmp(buf, "part=", 5)) {
754                         extract(mime_filename, &buf[5], 1);
755                         extract(mime_partnum, &buf[5], 2);
756                         extract(mime_disposition, &buf[5], 3);
757                         extract(mime_content_type, &buf[5], 4);
758                         mime_length = extract_int(&buf[5], 5);
759
760                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
761                                 strcpy(vcard_partnum, mime_partnum);
762                         }
763
764                 }
765         }
766
767         if (strlen(vcard_partnum) > 0) {
768                 vcard_source = load_mimepart(msgnum, vcard_partnum);
769                 if (vcard_source != NULL) {
770
771                         /* Display the summary line */
772                         display_vcard(vcard_source, alpha, 0, NULL);
773
774                         /* If it's my vCard I can edit it */
775                         if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
776                            || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
777                                 wprintf("<A HREF=\"/edit_vcard?"
778                                         "msgnum=%ld&partnum=%s\">",
779                                         msgnum, vcard_partnum);
780                                 wprintf("(edit)</A>");
781                         }
782
783                         free(vcard_source);
784                 }
785         }
786
787 }
788
789
790
791 /* If it's an old "Firstname Lastname" style record, try to
792  * convert it.
793  */
794 void lastfirst_firstlast(char *namebuf) {
795         char firstname[SIZ];
796         char lastname[SIZ];
797         int i;
798
799         if (namebuf == NULL) return;
800         if (strchr(namebuf, ';') != NULL) return;
801
802         i = num_tokens(namebuf, ' ');
803         if (i < 2) return;
804
805         extract_token(lastname, namebuf, i-1, ' ');
806         remove_token(namebuf, i-1, ' ');
807         strcpy(firstname, namebuf);
808         sprintf(namebuf, "%s; %s", lastname, firstname);
809 }
810
811
812 void fetch_ab_name(long msgnum, char *namebuf) {
813         char buf[SIZ];
814         char mime_partnum[SIZ];
815         char mime_filename[SIZ];
816         char mime_content_type[SIZ];
817         char mime_disposition[SIZ];
818         int mime_length;
819         char vcard_partnum[SIZ];
820         char *vcard_source = NULL;
821         int i;
822
823         struct {
824                 char date[SIZ];
825                 char from[SIZ];
826                 char to[SIZ];
827                 char subj[SIZ];
828                 int hasattachments;
829         } summ;
830
831         if (namebuf == NULL) return;
832         strcpy(namebuf, "");
833
834         memset(&summ, 0, sizeof(summ));
835         strcpy(summ.subj, "(no subject)");
836
837         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
838         serv_puts(buf);
839         serv_gets(buf);
840         if (buf[0] != '1') return;
841
842         while (serv_gets(buf), strcmp(buf, "000")) {
843                 if (!strncasecmp(buf, "part=", 5)) {
844                         extract(mime_filename, &buf[5], 1);
845                         extract(mime_partnum, &buf[5], 2);
846                         extract(mime_disposition, &buf[5], 3);
847                         extract(mime_content_type, &buf[5], 4);
848                         mime_length = extract_int(&buf[5], 5);
849
850                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
851                                 strcpy(vcard_partnum, mime_partnum);
852                         }
853
854                 }
855         }
856
857         if (strlen(vcard_partnum) > 0) {
858                 vcard_source = load_mimepart(msgnum, vcard_partnum);
859                 if (vcard_source != NULL) {
860
861                         /* Grab the name off the card */
862                         display_vcard(vcard_source, 0, 0, namebuf);
863
864                         free(vcard_source);
865                 }
866         }
867
868         lastfirst_firstlast(namebuf);
869         striplt(namebuf);
870         for (i=0; i<strlen(namebuf); ++i) {
871                 if (namebuf[i] != ';') return;
872         }
873         strcpy(namebuf, "(no name)");
874 }
875
876
877
878 /*
879  * Record compare function for sorting address book indices
880  */
881 int abcmp(const void *ab1, const void *ab2) {
882         return(strcasecmp(
883                 (((const struct addrbookent *)ab1)->ab_name),
884                 (((const struct addrbookent *)ab2)->ab_name)
885         ));
886 }
887
888
889 /*
890  * Helper function for do_addrbook_view()
891  * Converts a name into a three-letter tab label
892  */
893 void nametab(char *tabbuf, char *name) {
894         stresc(tabbuf, name, 0, 0);
895         tabbuf[0] = toupper(tabbuf[0]);
896         tabbuf[1] = tolower(tabbuf[1]);
897         tabbuf[2] = tolower(tabbuf[2]);
898         tabbuf[3] = 0;
899 }
900
901
902 /*
903  * Render the address book using info we gathered during the scan
904  */
905 void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
906         int i = 0;
907         int displayed = 0;
908         int bg = 0;
909         static int NAMESPERPAGE = 60;
910         int num_pages = 0;
911         int page = 0;
912         int tabfirst = 0;
913         char tabfirst_label[SIZ];
914         int tablast = 0;
915         char tablast_label[SIZ];
916
917         if (num_ab == 0) {
918                 wprintf("<I>This address book is empty.</I>\n");
919                 return;
920         }
921
922         if (num_ab > 1) {
923                 qsort(addrbook, num_ab, sizeof(struct addrbookent), abcmp);
924         }
925
926         num_pages = num_ab / NAMESPERPAGE;
927
928         page = atoi(bstr("page"));
929
930         wprintf("Page: ");
931         for (i=0; i<=num_pages; ++i) {
932                 if (i != page) {
933                         wprintf("<A HREF=\"/readfwd?page=%d\">", i);
934                 }
935                 else {
936                         wprintf("<B>");
937                 }
938                 tabfirst = i * NAMESPERPAGE;
939                 tablast = tabfirst + NAMESPERPAGE - 1;
940                 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
941                 nametab(tabfirst_label, addrbook[tabfirst].ab_name);
942                 nametab(tablast_label, addrbook[tablast].ab_name);
943                 wprintf("[%s&nbsp;-&nbsp;%s]",
944                         tabfirst_label, tablast_label
945                 );
946                 if (i != page) {
947                         wprintf("</A>\n");
948                 }
949                 else {
950                         wprintf("</B>\n");
951                 }
952         }
953         wprintf("<br />\n");
954
955         wprintf("<TABLE border=0 cellspacing=0 "
956                 "cellpadding=3 width=100%%>\n"
957         );
958
959         for (i=0; i<num_ab; ++i) {
960
961                 if ((i / NAMESPERPAGE) == page) {
962
963                         if ((displayed % 4) == 0) {
964                                 if (displayed > 0) {
965                                         wprintf("</TR>\n");
966                                 }
967                                 bg = 1 - bg;
968                                 wprintf("<TR BGCOLOR=\"#%s\">",
969                                         (bg ? "DDDDDD" : "FFFFFF")
970                                 );
971                         }
972         
973                         wprintf("<TD>");
974         
975                         wprintf("<A HREF=\"/readfwd?startmsg=%ld&is_singlecard=1",
976                                 addrbook[i].ab_msgnum);
977                         wprintf("&maxmsgs=1&summary=0&alpha=%s\">", bstr("alpha"));
978                         escputs(addrbook[i].ab_name);
979                         wprintf("</A></TD>\n");
980                         ++displayed;
981                 }
982         }
983
984         wprintf("</TR></TABLE>\n");
985 }
986
987
988
989 /* 
990  * load message pointers from the server
991  */
992 int load_msg_ptrs(char *servcmd)
993 {
994         char buf[SIZ];
995         int nummsgs;
996         int maxload = 0;
997
998         nummsgs = 0;
999         maxload = sizeof(WC->msgarr) / sizeof(long) ;
1000         serv_puts(servcmd);
1001         serv_gets(buf);
1002         if (buf[0] != '1') {
1003                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1004                 return (nummsgs);
1005         }
1006         while (serv_gets(buf), strcmp(buf, "000")) {
1007                 if (nummsgs < maxload) {
1008                         WC->msgarr[nummsgs] = atol(buf);
1009                         ++nummsgs;
1010                 }
1011         }
1012         return (nummsgs);
1013 }
1014
1015
1016 /*
1017  * command loop for reading messages
1018  */
1019 void readloop(char *oper)
1020 {
1021         char cmd[SIZ];
1022         char buf[SIZ];
1023         char old_msgs[SIZ];
1024         int is_new = 0;
1025         int a, b;
1026         int nummsgs;
1027         long startmsg;
1028         int maxmsgs;
1029         int num_displayed = 0;
1030         int is_summary = 0;
1031         int is_addressbook = 0;
1032         int is_singlecard = 0;
1033         int is_calendar = 0;
1034         int is_tasks = 0;
1035         int is_notes = 0;
1036         int remaining_messages;
1037         int lo, hi;
1038         int lowest_displayed = (-1);
1039         int highest_displayed = 0;
1040         long pn_previous = 0L;
1041         long pn_current = 0L;
1042         long pn_next = 0L;
1043         int bg = 0;
1044         struct addrbookent *addrbook = NULL;
1045         int num_ab = 0;
1046
1047         startmsg = atol(bstr("startmsg"));
1048         maxmsgs = atoi(bstr("maxmsgs"));
1049         is_summary = atoi(bstr("summary"));
1050         if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
1051
1052         output_headers(1, 1, 1, 0, 0, 0, 0);
1053
1054         /* When in summary mode, always show ALL messages instead of just
1055          * new or old.  Otherwise, show what the user asked for.
1056          */
1057         if (!strcmp(oper, "readnew")) {
1058                 strcpy(cmd, "MSGS NEW");
1059         }
1060         else if (!strcmp(oper, "readold")) {
1061                 strcpy(cmd, "MSGS OLD");
1062         }
1063         else {
1064                 strcpy(cmd, "MSGS ALL");
1065         }
1066
1067         if ((WC->wc_view == VIEW_MAILBOX) && (maxmsgs > 1)) {
1068                 is_summary = 1;
1069                 strcpy(cmd, "MSGS ALL");
1070         }
1071
1072         if ((WC->wc_view == VIEW_ADDRESSBOOK) && (maxmsgs > 1)) {
1073                 is_addressbook = 1;
1074                 strcpy(cmd, "MSGS ALL");
1075                 maxmsgs = 32767;
1076         }
1077
1078         if (is_summary) {
1079                 strcpy(cmd, "MSGS ALL");
1080         }
1081
1082         /* Are we doing a summary view?  If so, we need to know old messages
1083          * and new messages, so we can do that pretty boldface thing for the
1084          * new messages.
1085          */
1086         strcpy(old_msgs, "");
1087         if (is_summary) {
1088                 serv_puts("GTSN");
1089                 serv_gets(buf);
1090                 if (buf[0] == '2') {
1091                         strcpy(old_msgs, &buf[4]);
1092                 }
1093         }
1094
1095         is_singlecard = atoi(bstr("is_singlecard"));
1096
1097         if (WC->wc_view == VIEW_CALENDAR) {             /* calendar */
1098                 is_calendar = 1;
1099                 strcpy(cmd, "MSGS ALL");
1100                 maxmsgs = 32767;
1101         }
1102         if (WC->wc_view == VIEW_TASKS) {                /* tasks */
1103                 is_tasks = 1;
1104                 strcpy(cmd, "MSGS ALL");
1105                 maxmsgs = 32767;
1106         }
1107         if (WC->wc_view == VIEW_NOTES) {                /* notes */
1108                 is_notes = 1;
1109                 strcpy(cmd, "MSGS ALL");
1110                 maxmsgs = 32767;
1111         }
1112
1113         nummsgs = load_msg_ptrs(cmd);
1114         if (nummsgs == 0) {
1115
1116                 if ((!is_tasks) && (!is_calendar) && (!is_notes)) {
1117                         if (!strcmp(oper, "readnew")) {
1118                                 wprintf("<EM>No new messages.</EM>\n");
1119                         } else if (!strcmp(oper, "readold")) {
1120                                 wprintf("<EM>No old messages.</EM>\n");
1121                         } else {
1122                                 wprintf("<EM>No messages here.</EM>\n");
1123                         }
1124                 }
1125
1126                 goto DONE;
1127         }
1128
1129         if (startmsg == 0L) startmsg = WC->msgarr[0];
1130         remaining_messages = 0;
1131
1132         for (a = 0; a < nummsgs; ++a) {
1133                 if (WC->msgarr[a] >= startmsg) {
1134                         ++remaining_messages;
1135                 }
1136         }
1137
1138         wprintf("<FORM NAME=\"msgomatic\" "
1139                 "METHOD=\"POST\" ACTION=\"/do_stuff_to_msgs\">\n");
1140         if (is_summary) {
1141                 wprintf(
1142                         "<TABLE border=0 cellspacing=0 "
1143                         "cellpadding=0 width=100%%>\n"
1144                         "<TR>"
1145                         "<TD><I>Subject</I></TD>"
1146                         "<TD><I>Sender</I></TD>"
1147                         "<TD><I>Date</I></TD>"
1148                         "<TD></TD>"
1149                         "</TR>\n"
1150                 );
1151         }
1152
1153         for (a = 0; a < nummsgs; ++a) {
1154                 if ((WC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
1155
1156                         /* Are you a new message, or an old message? */
1157                         is_new = 0;
1158                         if (is_summary) {
1159                                 if (is_msg_in_mset(old_msgs, WC->msgarr[a])) {
1160                                         is_new = 0;
1161                                 }
1162                                 else {
1163                                         is_new = 1;
1164                                 }
1165                         }
1166
1167                         /* Learn which msgs "Prev" & "Next" buttons go to */
1168                         pn_current = WC->msgarr[a];
1169                         if (a > 0) pn_previous = WC->msgarr[a-1];
1170                         if (a < (nummsgs-1)) pn_next = WC->msgarr[a+1];
1171
1172                         /* If a tabular view, set up the line */
1173                         if (is_summary) {
1174                                 bg = 1 - bg;
1175                                 wprintf("<TR BGCOLOR=\"#%s\">",
1176                                         (bg ? "DDDDDD" : "FFFFFF")
1177                                 );
1178                         }
1179
1180                         /* Display the message */
1181                         if (is_summary) {
1182                                 summarize_message(WC->msgarr[a], is_new);
1183                         }
1184                         else if (is_addressbook) {
1185                                 fetch_ab_name(WC->msgarr[a], buf);
1186                                 ++num_ab;
1187                                 addrbook = realloc(addrbook,
1188                                         (sizeof(struct addrbookent) * num_ab) );
1189                                 safestrncpy(addrbook[num_ab-1].ab_name, buf,
1190                                         sizeof(addrbook[num_ab-1].ab_name));
1191                                 addrbook[num_ab-1].ab_msgnum = WC->msgarr[a];
1192                         }
1193                         else if (is_calendar) {
1194                                 display_calendar(WC->msgarr[a]);
1195                         }
1196                         else if (is_tasks) {
1197                                 display_task(WC->msgarr[a]);
1198                         }
1199                         else if (is_notes) {
1200                                 display_note(WC->msgarr[a]);
1201                         }
1202                         else {
1203                                 read_message(WC->msgarr[a]);
1204                         }
1205
1206                         /* If a tabular view, finish the line */
1207                         if (is_summary) {
1208                                 wprintf("</TR>\n");
1209                         }
1210
1211                         if (lowest_displayed < 0) lowest_displayed = a;
1212                         highest_displayed = a;
1213
1214                         ++num_displayed;
1215                         --remaining_messages;
1216                 }
1217         }
1218
1219         if (is_summary) {
1220                 wprintf("</TABLE>\n");
1221         }
1222
1223         /* Bump these because although we're thinking in zero base, the user
1224          * is a drooling idiot and is thinking in one base.
1225          */
1226         ++lowest_displayed;
1227         ++highest_displayed;
1228
1229         /* If we're only looking at one message, do a prev/next thing */
1230         if (num_displayed == 1) {
1231            if ((!is_tasks) && (!is_calendar) && (!is_addressbook) && (!is_notes) && (!is_singlecard)) {
1232
1233                 wprintf("<CENTER>"
1234                         "<TABLE BORDER=0 WIDTH=100%% BGCOLOR=\"#DDDDDD\"><TR><TD>"
1235                         "Reading #%d of %d messages.</TD>\n"
1236                         "<TD ALIGN=RIGHT><FONT SIZE=+1>",
1237                         lowest_displayed, nummsgs);
1238
1239                 if (is_summary) {
1240                         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1241                                 "VALUE=\"Delete selected\">\n");
1242                 }
1243
1244                 if (pn_previous > 0L) {
1245                         wprintf("<A HREF=\"/%s"
1246                                 "?startmsg=%ld"
1247                                 "&maxmsgs=1"
1248                                 "&summary=0\">"
1249                                 "Previous</A> \n",
1250                                         oper,
1251                                         pn_previous );
1252                 }
1253
1254                 if (pn_next > 0L) {
1255                         wprintf("<A HREF=\"/%s"
1256                                 "?startmsg=%ld"
1257                                 "&maxmsgs=1"
1258                                 "&summary=0\">"
1259                                 "Next</A> \n",
1260                                         oper,
1261                                         pn_next );
1262                 }
1263
1264                 wprintf("<A HREF=\"/%s?startmsg=%ld"
1265                         "&maxmsgs=999999&summary=1\">"
1266                         "Summary"
1267                         "</A>",
1268                         oper,
1269                         WC->msgarr[0]);
1270
1271             }
1272         }
1273
1274         /*
1275          * If we're not currently looking at ALL requested
1276          * messages, then display the selector bar
1277          */
1278         if (num_displayed > 1) {
1279            if ((!is_tasks) && (!is_calendar) && (!is_addressbook)
1280               && (!is_notes) && (!is_singlecard)) {
1281
1282                 wprintf("Reading #", lowest_displayed, highest_displayed);
1283
1284                 wprintf("<SELECT NAME=\"whichones\" SIZE=\"1\" "
1285                         "OnChange=\"location.href=msgomatic.whichones.options"
1286                         "[selectedIndex].value\">\n");
1287
1288                 for (b=0; b<nummsgs; b = b + maxmsgs) {
1289                 lo = b+1;
1290                 hi = b+maxmsgs;
1291                 if (hi > nummsgs) hi = nummsgs;
1292                         wprintf("<OPTION %s VALUE="
1293                                 "\"/%s"
1294                                 "?startmsg=%ld"
1295                                 "&maxmsgs=%d"
1296                                 "&summary=%d\">"
1297                                 "%d-%d</OPTION> \n",
1298                                 ((WC->msgarr[b] == startmsg) ? "SELECTED" : ""),
1299                                 oper,
1300                                 WC->msgarr[b],
1301                                 maxmsgs,
1302                                 is_summary,
1303                                 lo, hi);
1304                 }
1305                 wprintf("<OPTION VALUE=\"/%s?startmsg=%ld"
1306                         "&maxmsgs=999999&summary=%d\">"
1307                         "ALL"
1308                         "</OPTION> ",
1309                         oper,
1310                         WC->msgarr[0], is_summary);
1311
1312                 wprintf("<OPTION VALUE=\"/%s?startmsg=%ld"
1313                         "&maxmsgs=999999&summary=1\">"
1314                         "Summary"
1315                         "</OPTION>",
1316                         oper,
1317                         WC->msgarr[0]);
1318
1319                 wprintf("</SELECT> of %d messages.", nummsgs);
1320
1321                 if (is_summary) {
1322                         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1323                                 "VALUE=\"Delete selected\">\n");
1324                 }
1325
1326             }
1327         }
1328         if (is_summary) wprintf("</FORM>\n");
1329
1330 DONE:
1331         if (is_tasks) {
1332                 do_tasks_view();        /* Render the task list */
1333         }
1334
1335         if (is_calendar) {
1336                 do_calendar_view();     /* Render the calendar */
1337         }
1338
1339         if (is_addressbook) {
1340                 do_addrbook_view(addrbook, num_ab);     /* Render the address book */
1341         }
1342
1343         wDumpContent(1);
1344         if (addrbook != NULL) free(addrbook);
1345 }
1346
1347
1348 /*
1349  * Back end for post_message() ... this is where the actual message
1350  * gets transmitted to the server.
1351  */
1352 void post_mime_to_server(void) {
1353         char boundary[SIZ];
1354         int is_multipart = 0;
1355         static int seq = 0;
1356         struct wc_attachment *att;
1357         char *encoded;
1358         size_t encoded_length;
1359
1360         /* If there are attachments, we have to do multipart/mixed */
1361         if (WC->first_attachment != NULL) {
1362                 is_multipart = 1;
1363         }
1364
1365         if (is_multipart) {
1366                 sprintf(boundary, "---Citadel-Multipart-%s-%04x%04x---",
1367                         serv_info.serv_fqdn,
1368                         getpid(),
1369                         ++seq
1370                 );
1371
1372                 /* Remember, serv_printf() appends an extra newline */
1373                 serv_printf("Content-type: multipart/mixed; "
1374                         "boundary=\"%s\"\n", boundary);
1375                 serv_printf("This is a multipart message in MIME format.\n");
1376                 serv_printf("--%s", boundary);
1377         }
1378
1379         serv_puts("Content-type: text/html");
1380         serv_puts("");
1381         serv_puts("<HTML><BODY>\n");
1382         text_to_server(bstr("msgtext"), 0);
1383         serv_puts("</BODY></HTML>\n");
1384         
1385
1386         if (is_multipart) {
1387
1388                 /* Add in the attachments */
1389                 for (att = WC->first_attachment; att!=NULL; att=att->next) {
1390
1391                         encoded_length = ((att->length * 150) / 100);
1392                         encoded = malloc(encoded_length);
1393                         if (encoded == NULL) break;
1394                         CtdlEncodeBase64(encoded, att->data, att->length);
1395
1396                         serv_printf("--%s", boundary);
1397                         serv_printf("Content-type: %s", att->content_type);
1398                         serv_printf("Content-disposition: attachment; "
1399                                 "filename=\"%s\"", att->filename);
1400                         serv_puts("Content-transfer-encoding: base64");
1401                         serv_puts("");
1402                         serv_write(encoded, strlen(encoded));
1403                         serv_puts("");
1404                         serv_puts("");
1405                         free(encoded);
1406                 }
1407                 serv_printf("--%s--", boundary);
1408         }
1409
1410         serv_puts("000");
1411 }
1412
1413
1414 /*
1415  * Post message (or don't post message)
1416  *
1417  * Note regarding the "dont_post" variable:
1418  * A random value (actually, it's just a timestamp) is inserted as a hidden
1419  * field called "postseq" when the display_enter page is generated.  This
1420  * value is checked when posting, using the static variable dont_post.  If a
1421  * user attempts to post twice using the same dont_post value, the message is
1422  * discarded.  This prevents the accidental double-saving of the same message
1423  * if the user happens to click the browser "back" button.
1424  */
1425 void post_message(void)
1426 {
1427         char buf[SIZ];
1428         static long dont_post = (-1L);
1429         struct wc_attachment *att, *aptr;
1430
1431         if (WC->upload_length > 0) {
1432
1433                 /* There's an attachment.  Save it to this struct... */
1434                 att = malloc(sizeof(struct wc_attachment));
1435                 memset(att, 0, sizeof(struct wc_attachment));
1436                 att->length = WC->upload_length;
1437                 strcpy(att->content_type, WC->upload_content_type);
1438                 strcpy(att->filename, WC->upload_filename);
1439                 att->next = NULL;
1440
1441                 /* And add it to the list. */
1442                 if (WC->first_attachment == NULL) {
1443                         WC->first_attachment = att;
1444                 }
1445                 else {
1446                         aptr = WC->first_attachment;
1447                         while (aptr->next != NULL) aptr = aptr->next;
1448                         aptr->next = att;
1449                 }
1450
1451                 /* Netscape sends a simple filename, which is what we want,
1452                  * but Satan's browser sends an entire pathname.  Reduce
1453                  * the path to just a filename if we need to.
1454                  */
1455                 while (num_tokens(att->filename, '/') > 1) {
1456                         remove_token(att->filename, 0, '/');
1457                 }
1458                 while (num_tokens(att->filename, '\\') > 1) {
1459                         remove_token(att->filename, 0, '\\');
1460                 }
1461
1462                 /* Transfer control of this memory from the upload struct
1463                  * to the attachment struct.
1464                  */
1465                 att->data = WC->upload;
1466                 WC->upload_length = 0;
1467                 WC->upload = NULL;
1468                 display_enter();
1469                 return;
1470         }
1471
1472         if (!strcasecmp(bstr("sc"), "Cancel")) {
1473                 sprintf(WC->ImportantMessage, 
1474                         "Cancelled.  Message was not posted.");
1475         } else if (!strcasecmp(bstr("attach"), "Add")) {
1476                 display_enter();
1477                 return;
1478         } else if (atol(bstr("postseq")) == dont_post) {
1479                 sprintf(WC->ImportantMessage, 
1480                         "Automatically cancelled because you have already "
1481                         "saved this message.");
1482         } else {
1483                 sprintf(buf, "ENT0 1|%s|0|4|%s",
1484                         bstr("recp"),
1485                         bstr("subject") );
1486                 serv_puts(buf);
1487                 serv_gets(buf);
1488                 if (buf[0] == '4') {
1489                         post_mime_to_server();
1490                         sprintf(WC->ImportantMessage, 
1491                                 "Message has been posted.\n");
1492                         dont_post = atol(bstr("postseq"));
1493                 } else {
1494                         sprintf(WC->ImportantMessage, 
1495                                 "%s", &buf[4]);
1496                 }
1497         }
1498
1499         free_attachments(WC);
1500         readloop("readnew");
1501 }
1502
1503
1504
1505
1506 /*
1507  * display the message entry screen
1508  */
1509 void display_enter(void)
1510 {
1511         char buf[SIZ];
1512         long now;
1513         struct wc_attachment *att;
1514
1515         if (strlen(bstr("force_room")) > 0) {
1516                 gotoroom(bstr("force_room"));
1517         }
1518
1519         /* Are we perhaps in an address book view?  If so, then an "enter
1520          * message" command really means "add new entry."
1521          */
1522         if (WC->wc_view == VIEW_ADDRESSBOOK) {
1523                 do_edit_vcard(-1, "", "");
1524                 return;
1525         }
1526
1527 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1528         /* Are we perhaps in a calendar view?  If so, then an "enter
1529          * message" command really means "add new calendar item."
1530          */
1531         if (WC->wc_view == VIEW_CALENDAR) {
1532                 display_edit_event();
1533                 return;
1534         }
1535
1536         /* Are we perhaps in a tasks view?  If so, then an "enter
1537          * message" command really means "add new task."
1538          */
1539         if (WC->wc_view == VIEW_TASKS) {
1540                 display_edit_task();
1541                 return;
1542         }
1543 #endif
1544
1545         /* Otherwise proceed normally.  Do a custom room banner with no navbar... */
1546         output_headers(1, 1, 2, 0, 0, 0, 0);
1547         wprintf("<div id=\"banner\">\n");
1548         embed_room_banner(NULL, navbar_none);
1549         wprintf("</div>\n");
1550         wprintf("<div id=\"content\">\n");
1551
1552         sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
1553         serv_puts(buf);
1554         serv_gets(buf);
1555
1556         if (!strncmp(buf, "570", 3)) {
1557                 if (strlen(bstr("recp")) > 0) {
1558                         svprintf("RECPERROR", WCS_STRING,
1559                                 "<SPAN CLASS=\"errormsg\">%s</SPAN><br />\n",
1560                                 &buf[4]
1561                         );
1562                 }
1563                 do_template("prompt_for_recipient");
1564                 goto DONE;
1565         }
1566         if (buf[0] != '2') {
1567                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1568                 goto DONE;
1569         }
1570
1571         now = time(NULL);
1572         fmt_date(buf, now);
1573         strcat(&buf[strlen(buf)], " <I>from</I> ");
1574         stresc(&buf[strlen(buf)], WC->wc_username, 1, 1);
1575         if (strlen(bstr("recp")) > 0) {
1576                 strcat(&buf[strlen(buf)], " <I>to</I> ");
1577                 stresc(&buf[strlen(buf)], bstr("recp"), 1, 1);
1578         }
1579         strcat(&buf[strlen(buf)], " <I>in</I> ");
1580         stresc(&buf[strlen(buf)], WC->wc_roomname, 1, 1);
1581
1582         /* begin message entry screen */
1583         wprintf("<div style=\"position:absolute; left:1%%; width:96%%; height:100%%\">\n");
1584
1585         wprintf("<form enctype=\"multipart/form-data\" "
1586                 "method=\"POST\" action=\"/post\" "
1587                 "name=\"enterform\""
1588                 "onSubmit=\"return submitForm();\""
1589                 ">\n");
1590         wprintf("<input type=\"hidden\" name=\"recp\" value=\"%s\">\n",
1591                 bstr("recp"));
1592         wprintf("<input type=\"hidden\" name=\"postseq\" value=\"%ld\">\n",
1593                 now);
1594
1595         wprintf("%s<br>\n", buf);       /* header bar */
1596         wprintf("<img src=\"static/enter.gif\" align=middle alt=\" \">");
1597                 /* "onLoad=\"document.enterform.msgtext.focus();\" " */
1598         wprintf("<font size=-1>Subject (optional):</font>"
1599                 "<input type=\"text\" name=\"subject\" value=\"");
1600         escputs(bstr("subject"));
1601         wprintf("\" size=40 maxlength=70>"
1602                 "&nbsp;"
1603         );
1604
1605         wprintf("<input type=\"submit\" name=\"sc\" value=\"Save message\">"
1606                 "&nbsp;"
1607                 "<input type=\"submit\" name=\"sc\" value=\"Cancel\">\n");
1608
1609         /* begin richedit box */
1610         wprintf("<div style=\"position:absolute; left:0%%; width:100%%; top:15%; height:80%%\">\n");
1611
1612         wprintf("<script type=\"text/javascript\" "
1613                 "src=\"static/richtext.js\"></script>\n"
1614                 "<script type=\"text/javascript\">\n"
1615                 "function submitForm() { \n"
1616                 "  updateRTE('msgtext'); \n"
1617                 "  return true; \n"
1618                 "} \n"
1619                 "  \n"
1620                 "initRTE(\"static/\", \"static/\", \"\"); \n"
1621                 "</script> \n"
1622                 "<noscript>JAVAscript MUST BE ENABLED.</noscript> \n"
1623                 "<script type=\"text/javascript\"> \n"
1624                 "writeRichText('msgtext', '");
1625         msgescputs(bstr("msgtext"));
1626         wprintf("', '100%%', '80%%', true, false); \n"
1627                 "</script> \n");
1628         wprintf("</div>\n");    /* end richedit box */
1629
1630         /* Here comes the "do attachments" section on the bottom */
1631         wprintf("<div style=\"position:absolute; bottom:0px; left:0px; width:100%%\">\n");
1632
1633         /* Enumerate any attachments which are already in place... */
1634         for (att = WC->first_attachment; att != NULL; att = att->next) {
1635                 wprintf("<IMG SRC=\"/static/attachment.gif\" "
1636                         "BORDER=0 ALIGN=MIDDLE> Attachment: ");
1637                 escputs(att->filename);
1638                 wprintf(" (%s, %d bytes)<br />\n",
1639                         att->content_type, att->length);
1640         }
1641
1642         /* Now offer the ability to attach additional files... */
1643         wprintf("&nbsp;&nbsp;&nbsp;"
1644                 "Attach file: <input NAME=\"attachfile\" "
1645                 "SIZE=48 TYPE=\"file\">\n&nbsp;&nbsp;"
1646                 "<input type=\"submit\" name=\"attach\" value=\"Add\">\n");
1647
1648         wprintf("</div>\n");    /* end attachments section */
1649
1650         wprintf("</FORM>\n");
1651
1652         wprintf("</div>\n");
1653 DONE:   wDumpContent(1);
1654 }
1655
1656
1657
1658
1659
1660
1661
1662
1663 void delete_msg(void)
1664 {
1665         long msgid;
1666         char buf[SIZ];
1667
1668         msgid = atol(bstr("msgid"));
1669
1670         output_headers(1, 1, 1, 0, 0, 0, 0);
1671
1672         sprintf(buf, "DELE %ld", msgid);
1673         serv_puts(buf);
1674         serv_gets(buf);
1675         wprintf("<EM>%s</EM><br />\n", &buf[4]);
1676
1677         wDumpContent(1);
1678 }
1679
1680
1681
1682
1683 /*
1684  * Confirm move of a message
1685  */
1686 void confirm_move_msg(void)
1687 {
1688         long msgid;
1689         char buf[SIZ];
1690         char targ[SIZ];
1691
1692         msgid = atol(bstr("msgid"));
1693
1694         output_headers(1, 1, 1, 0, 0, 0, 0);
1695
1696         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
1697         wprintf("<FONT SIZE=+1 COLOR=\"#FFFFFF\"");
1698         wprintf("<B>Confirm move of message</B>\n");
1699         wprintf("</FONT></TD></TR></TABLE>\n");
1700
1701         wprintf("<CENTER>");
1702
1703         wprintf("Move this message to:<br />\n");
1704
1705         wprintf("<FORM METHOD=\"POST\" ACTION=\"/move_msg\">\n");
1706         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
1707                 bstr("msgid"));
1708
1709
1710         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1711         serv_puts("LKRA");
1712         serv_gets(buf);
1713         if (buf[0] == '1') {
1714                 while (serv_gets(buf), strcmp(buf, "000")) {
1715                         extract(targ, buf, 0);
1716                         wprintf("<OPTION>");
1717                         escputs(targ);
1718                         wprintf("\n");
1719                 }
1720         }
1721         wprintf("</SELECT>\n");
1722         wprintf("<br />\n");
1723
1724         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
1725         wprintf("&nbsp;");
1726         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
1727         wprintf("</FORM></CENTER>\n");
1728
1729         wprintf("</CENTER>\n");
1730         wDumpContent(1);
1731 }
1732
1733
1734
1735 void move_msg(void)
1736 {
1737         long msgid;
1738         char buf[SIZ];
1739
1740         msgid = atol(bstr("msgid"));
1741
1742         output_headers(1, 1, 1, 0, 0, 0, 0);
1743
1744         if (!strcasecmp(bstr("yesno"), "Move")) {
1745                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1746                 serv_puts(buf);
1747                 serv_gets(buf);
1748                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1749         } else {
1750                 wprintf("<EM>Message not moved.</EM><br />\n");
1751         }
1752
1753         wDumpContent(1);
1754 }
1755
1756 /*
1757  * This gets called when a user selects Reply/Move/Del etc. on *one* message.
1758  */
1759 void do_stuff_to_one_msg(void) {
1760         char *msg_oper;
1761
1762         msg_oper = bstr("msg_oper");
1763
1764         if (!strcasecmp(msg_oper, "Delete")) {
1765                 delete_msg();   /* It's already been confirmed using JS */
1766                 return;
1767         }
1768         if (!strcasecmp(msg_oper, "Move")) {
1769                 confirm_move_msg();
1770                 return;
1771         }
1772         if (!strcasecmp(msg_oper, "Reply")) {
1773                 display_enter();        /* recp and subject already set */
1774                 return;
1775         }
1776
1777         /* should never get here.  FIXME: display an error */
1778
1779 }
1780
1781
1782 /*
1783  * This gets called when a user selects multiple messages in a summary
1784  * list and then clicks to perform a transformation of some sort on them
1785  * (such as deleting them).
1786  */
1787 void do_stuff_to_msgs(void) {
1788         char buf[SIZ];
1789         char sc[SIZ];
1790
1791         struct stuff_t {
1792                 struct stuff_t *next;
1793                 long msgnum;
1794         };
1795
1796         struct stuff_t *stuff = NULL;
1797         struct stuff_t *ptr;
1798
1799
1800         serv_puts("MSGS ALL");
1801         serv_gets(buf);
1802
1803         if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
1804                 ptr = malloc(sizeof(struct stuff_t));
1805                 ptr->msgnum = atol(buf);
1806                 ptr->next = stuff;
1807                 stuff = ptr;
1808         }
1809
1810         strcpy(sc, bstr("sc"));
1811
1812         while (stuff != NULL) {
1813
1814                 sprintf(buf, "msg_%ld", stuff->msgnum);
1815                 if (!strcasecmp(bstr(buf), "yes")) {
1816
1817                         if (!strcasecmp(sc, "Delete selected")) {
1818                                 serv_printf("DELE %ld", stuff->msgnum);
1819                                 serv_gets(buf);
1820                         }
1821
1822                 }
1823
1824                 ptr = stuff->next;
1825                 free(stuff);
1826                 stuff = ptr;
1827         }
1828
1829         readloop("readfwd");
1830 }