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