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