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