* auth.c: shrink the margins on the login screen so the user sees something
[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                 ) {
330                 display_parsed_vcard(v, full);
331         }
332
333         vcard_free(v);
334 }
335
336
337
338
339 /*
340  * I wanna SEE that message!
341  */
342 void read_message(long msgnum) {
343         char buf[SIZ];
344         char mime_partnum[SIZ];
345         char mime_filename[SIZ];
346         char mime_content_type[SIZ];
347         char mime_disposition[SIZ];
348         int mime_length;
349         char mime_http[SIZ];
350         char m_subject[SIZ];
351         char from[SIZ];
352         char node[SIZ];
353         char rfca[SIZ];
354         char reply_to[512];
355         char now[SIZ];
356         int format_type = 0;
357         int nhdr = 0;
358         int bq = 0;
359         char vcard_partnum[SIZ];
360         char cal_partnum[SIZ];
361         char *part_source = NULL;
362
363         strcpy(from, "");
364         strcpy(node, "");
365         strcpy(rfca, "");
366         strcpy(reply_to, "");
367         strcpy(vcard_partnum, "");
368         strcpy(cal_partnum, "");
369         strcpy(mime_http, "");
370
371         serv_printf("MSG4 %ld", msgnum);
372         serv_gets(buf);
373         if (buf[0] != '1') {
374                 wprintf("<STRONG>ERROR:</STRONG> %s<br />\n", &buf[4]);
375                 return;
376         }
377
378         /* begin everythingamundo table */
379         wprintf("<div id=\"fix_scrollbar_bug\">\n");
380         wprintf("<table width=100%% border=1 cellspacing=0 "
381                 "cellpadding=0><TR><TD>\n");
382
383         /* begin message header table */
384         wprintf("<TABLE WIDTH=100%% BORDER=0 CELLSPACING=0 "
385                 "CELLPADDING=1 BGCOLOR=\"#CCCCCC\"><TR><TD>\n");
386
387         wprintf("<SPAN CLASS=\"message_header\">");
388         strcpy(m_subject, "");
389
390         while (serv_gets(buf), strcasecmp(buf, "text")) {
391                 if (!strcmp(buf, "000")) {
392                         wprintf("<I>unexpected end of message</I><br /><br />\n");
393                         wprintf("</SPAN>\n");
394                         return;
395                 }
396                 if (!strncasecmp(buf, "nhdr=yes", 8))
397                         nhdr = 1;
398                 if (nhdr == 1)
399                         buf[0] = '_';
400                 if (!strncasecmp(buf, "type=", 5))
401                         format_type = atoi(&buf[5]);
402                 if (!strncasecmp(buf, "from=", 5)) {
403                         strcpy(from, &buf[5]);
404                         wprintf("from <A HREF=\"/showuser&who=");
405                         urlescputs(from);
406                         wprintf("\">");
407                         escputs(from);
408                         wprintf("</A> ");
409                 }
410                 if (!strncasecmp(buf, "subj=", 5))
411                         strcpy(m_subject, &buf[5]);
412                 if ((!strncasecmp(buf, "hnod=", 5))
413                     && (strcasecmp(&buf[5], serv_info.serv_humannode)))
414                         wprintf("(%s) ", &buf[5]);
415                 if ((!strncasecmp(buf, "room=", 5))
416                     && (strcasecmp(&buf[5], WC->wc_roomname))
417                     && (strlen(&buf[5])>0) )
418                         wprintf("in %s> ", &buf[5]);
419                 if (!strncasecmp(buf, "rfca=", 5)) {
420                         strcpy(rfca, &buf[5]);
421                         wprintf("&lt;");
422                         escputs(rfca);
423                         wprintf("&gt; ");
424                 }
425
426                 if (!strncasecmp(buf, "node=", 5)) {
427                         strcpy(node, &buf[5]);
428                         if ( ((WC->room_flags & QR_NETWORK)
429                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
430                         && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
431                         && (strlen(rfca)==0)
432                         ) {
433                                 wprintf("@%s ", &buf[5]);
434                         }
435                 }
436                 if (!strncasecmp(buf, "rcpt=", 5))
437                         wprintf("to %s ", &buf[5]);
438                 if (!strncasecmp(buf, "time=", 5)) {
439                         fmt_date(now, atol(&buf[5]));
440                         wprintf("%s ", now);
441                 }
442
443                 if (!strncasecmp(buf, "part=", 5)) {
444                         extract(mime_filename, &buf[5], 1);
445                         extract(mime_partnum, &buf[5], 2);
446                         extract(mime_disposition, &buf[5], 3);
447                         extract(mime_content_type, &buf[5], 4);
448                         mime_length = extract_int(&buf[5], 5);
449
450                         if (!strcasecmp(mime_disposition, "attachment")) {
451                                 snprintf(&mime_http[strlen(mime_http)],
452                                         (sizeof(mime_http) - strlen(mime_http) - 1),
453                                         "<A HREF=\"/output_mimepart?"
454                                         "msgnum=%ld&partnum=%s\" "
455                                         "TARGET=\"wc.%ld.%s\">"
456                                         "<IMG SRC=\"/static/attachment.gif\" "
457                                         "BORDER=0 ALIGN=MIDDLE>\n"
458                                         "Part %s: %s (%s, %d bytes)</A><br />\n",
459                                         msgnum, mime_partnum,
460                                         msgnum, mime_partnum,
461                                         mime_partnum, mime_filename,
462                                         mime_content_type, mime_length);
463                         }
464
465                         if ((!strcasecmp(mime_disposition, "inline"))
466                            && (!strncasecmp(mime_content_type, "image/", 6)) ){
467                                 snprintf(&mime_http[strlen(mime_http)],
468                                         (sizeof(mime_http) - strlen(mime_http) - 1),
469                                         "<IMG SRC=\"/output_mimepart?"
470                                         "msgnum=%ld&partnum=%s\">",
471                                         msgnum, mime_partnum);
472                         }
473
474                         /*** begin handler prep ***/
475                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
476                                 strcpy(vcard_partnum, mime_partnum);
477                         }
478
479                         if (!strcasecmp(mime_content_type, "text/calendar")) {
480                                 strcpy(cal_partnum, mime_partnum);
481                         }
482
483                         /*** end handler prep ***/
484
485                 }
486
487         }
488
489         /* Generate a reply-to address */
490         if (strlen(rfca) > 0) {
491                 strcpy(reply_to, rfca);
492         }
493         else {
494                 if ( (strlen(node) > 0)
495                    && (strcasecmp(node, serv_info.serv_nodename))
496                    && (strcasecmp(node, serv_info.serv_humannode)) ) {
497                         snprintf(reply_to, sizeof(reply_to), "%s @ %s",
498                                 from, node);
499                 }
500                 else {
501                         snprintf(reply_to, sizeof(reply_to), "%s", from);
502                 }
503         }
504
505         if (nhdr == 1) {
506                 wprintf("****");
507         }
508
509         wprintf("</SPAN>");
510         if (strlen(m_subject) > 0) {
511                 wprintf("<br />"
512                         "<SPAN CLASS=\"message_subject\">"
513                         "Subject: %s"
514                         "</SPAN>", m_subject
515                 );
516         }
517         wprintf("</TD>\n");
518
519         /* start msg buttons */
520         wprintf("<TD ALIGN=RIGHT>\n");
521
522         /* Reply */
523         wprintf("<a href=\"/display_enter?recp=");
524         urlescputs(reply_to);
525         wprintf("&subject=");
526         if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%20");
527         urlescputs(m_subject);
528         wprintf("\">Reply</a> ");
529
530         if (WC->is_room_aide)  {
531         
532                 /* Move */
533                 wprintf("<a href=\"/confirm_move_msg?msgid=%ld\">Move </a>",
534                         msgnum);
535
536                 /* Delete */
537                 wprintf("<a href=\"/delete_msg?msgid=%ld\" "
538                         "onClick=\"return confirm('Delete this message?');\">"
539                         "Delete</a>", msgnum);
540                         
541         }
542
543         wprintf("</TD></TR></TABLE>\n");
544
545         /* Begin body */
546         wprintf("<TABLE BORDER=0 WIDTH=100%% BGCOLOR=#FFFFFF "
547                 "CELLPADDING=1 CELLSPACING=0><TR><TD>");
548
549         /* 
550          * Learn the content type
551          */
552         strcpy(mime_content_type, "text/plain");
553         while (serv_gets(buf), (strlen(buf) > 0)) {
554                 if (!strcmp(buf, "000")) {
555                         wprintf("<I>unexpected end of message</I><br /><br />\n");
556                         goto ENDBODY;
557                 }
558                 if (!strncasecmp(buf, "Content-type: ", 14)) {
559                         safestrncpy(mime_content_type, &buf[14],
560                                 sizeof(mime_content_type));
561                 }
562         }
563
564         /* Messages in legacy Citadel variformat get handled thusly... */
565         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
566                 fmout(NULL, "JUSTIFY");
567         }
568
569         /* Boring old 80-column fixed format text gets handled this way... */
570         else if (!strcasecmp(mime_content_type, "text/plain")) {
571                 while (serv_gets(buf), strcmp(buf, "000")) {
572                         if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
573                         if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
574                         while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
575                                 buf[strlen(buf) - 1] = 0;
576                         if ((bq == 0) &&
577                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
578                                 wprintf("<BLOCKQUOTE>");
579                                 bq = 1;
580                         } else if ((bq == 1) &&
581                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
582                                 wprintf("</BLOCKQUOTE>");
583                                 bq = 0;
584                         }
585                         wprintf("<TT>");
586                         url(buf);
587                         escputs(buf);
588                         wprintf("</TT><br />\n");
589                 }
590                 wprintf("</I><br />");
591         }
592
593         else /* HTML is fun, but we've got to strip it first */
594         if (!strcasecmp(mime_content_type, "text/html")) {
595                 output_html();
596         }
597
598         /* Unknown weirdness */
599         else {
600                 wprintf("I don't know how to display %s<br />\n",
601                         mime_content_type);
602                 while (serv_gets(buf), strcmp(buf, "000")) { }
603         }
604
605
606         /* Afterwards, offer links to download attachments 'n' such */
607         if (strlen(mime_http) > 0) {
608                 wprintf("%s", mime_http);
609         }
610
611         /* Handler for vCard parts */
612         if (strlen(vcard_partnum) > 0) {
613                 part_source = load_mimepart(msgnum, vcard_partnum);
614                 if (part_source != NULL) {
615
616                         /* If it's my vCard I can edit it */
617                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
618                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
619                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
620                         ) {
621                                 wprintf("<A HREF=\"/edit_vcard?"
622                                         "msgnum=%ld&partnum=%s\">",
623                                         msgnum, vcard_partnum);
624                                 wprintf("[edit]</A>");
625                         }
626
627                         /* In all cases, display the full card */
628                         display_vcard(part_source, 0, 1, NULL);
629                 }
630         }
631
632         /* Handler for calendar parts */
633         if (strlen(cal_partnum) > 0) {
634                 part_source = load_mimepart(msgnum, cal_partnum);
635                 if (part_source != NULL) {
636                         cal_process_attachment(part_source,
637                                                 msgnum, cal_partnum);
638                 }
639         }
640
641         if (part_source) {
642                 free(part_source);
643                 part_source = NULL;
644         }
645
646 ENDBODY:
647         wprintf("</TD></TR></TABLE>\n");
648
649         /* end everythingamundo table */
650         wprintf("</TD></TR></TABLE>\n");
651         wprintf("</div><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                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
784                         ) {
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 id=\"fix_scrollbar_bug\">"
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 id=\"fix_scrollbar_bug\">"
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("</SELECT> of %d messages.", nummsgs);
1322
1323                 if (is_summary) {
1324                         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1325                                 "VALUE=\"Delete selected\">\n");
1326                 }
1327
1328             }
1329         }
1330         wprintf("</form>\n");
1331
1332 DONE:
1333         if (is_tasks) {
1334                 do_tasks_view();        /* Render the task list */
1335         }
1336
1337         if (is_calendar) {
1338                 do_calendar_view();     /* Render the calendar */
1339         }
1340
1341         if (is_addressbook) {
1342                 do_addrbook_view(addrbook, num_ab);     /* Render the address book */
1343         }
1344
1345         wDumpContent(1);
1346         if (addrbook != NULL) free(addrbook);
1347 }
1348
1349
1350 /*
1351  * Back end for post_message() ... this is where the actual message
1352  * gets transmitted to the server.
1353  */
1354 void post_mime_to_server(void) {
1355         char boundary[SIZ];
1356         int is_multipart = 0;
1357         static int seq = 0;
1358         struct wc_attachment *att;
1359         char *encoded;
1360         size_t encoded_length;
1361
1362         /* If there are attachments, we have to do multipart/mixed */
1363         if (WC->first_attachment != NULL) {
1364                 is_multipart = 1;
1365         }
1366
1367         if (is_multipart) {
1368                 sprintf(boundary, "---Citadel-Multipart-%s-%04x%04x---",
1369                         serv_info.serv_fqdn,
1370                         getpid(),
1371                         ++seq
1372                 );
1373
1374                 /* Remember, serv_printf() appends an extra newline */
1375                 serv_printf("Content-type: multipart/mixed; "
1376                         "boundary=\"%s\"\n", boundary);
1377                 serv_printf("This is a multipart message in MIME format.\n");
1378                 serv_printf("--%s", boundary);
1379         }
1380
1381         serv_puts("Content-type: text/html");
1382         serv_puts("");
1383         serv_puts("<HTML><BODY>\n");
1384         text_to_server(bstr("msgtext"), 0);
1385         serv_puts("</BODY></HTML>\n");
1386         
1387
1388         if (is_multipart) {
1389
1390                 /* Add in the attachments */
1391                 for (att = WC->first_attachment; att!=NULL; att=att->next) {
1392
1393                         encoded_length = ((att->length * 150) / 100);
1394                         encoded = malloc(encoded_length);
1395                         if (encoded == NULL) break;
1396                         CtdlEncodeBase64(encoded, att->data, att->length);
1397
1398                         serv_printf("--%s", boundary);
1399                         serv_printf("Content-type: %s", att->content_type);
1400                         serv_printf("Content-disposition: attachment; "
1401                                 "filename=\"%s\"", att->filename);
1402                         serv_puts("Content-transfer-encoding: base64");
1403                         serv_puts("");
1404                         serv_write(encoded, strlen(encoded));
1405                         serv_puts("");
1406                         serv_puts("");
1407                         free(encoded);
1408                 }
1409                 serv_printf("--%s--", boundary);
1410         }
1411
1412         serv_puts("000");
1413 }
1414
1415
1416 /*
1417  * Post message (or don't post message)
1418  *
1419  * Note regarding the "dont_post" variable:
1420  * A random value (actually, it's just a timestamp) is inserted as a hidden
1421  * field called "postseq" when the display_enter page is generated.  This
1422  * value is checked when posting, using the static variable dont_post.  If a
1423  * user attempts to post twice using the same dont_post value, the message is
1424  * discarded.  This prevents the accidental double-saving of the same message
1425  * if the user happens to click the browser "back" button.
1426  */
1427 void post_message(void)
1428 {
1429         char buf[SIZ];
1430         static long dont_post = (-1L);
1431         struct wc_attachment *att, *aptr;
1432
1433         if (WC->upload_length > 0) {
1434
1435                 /* There's an attachment.  Save it to this struct... */
1436                 att = malloc(sizeof(struct wc_attachment));
1437                 memset(att, 0, sizeof(struct wc_attachment));
1438                 att->length = WC->upload_length;
1439                 strcpy(att->content_type, WC->upload_content_type);
1440                 strcpy(att->filename, WC->upload_filename);
1441                 att->next = NULL;
1442
1443                 /* And add it to the list. */
1444                 if (WC->first_attachment == NULL) {
1445                         WC->first_attachment = att;
1446                 }
1447                 else {
1448                         aptr = WC->first_attachment;
1449                         while (aptr->next != NULL) aptr = aptr->next;
1450                         aptr->next = att;
1451                 }
1452
1453                 /* Netscape sends a simple filename, which is what we want,
1454                  * but Satan's browser sends an entire pathname.  Reduce
1455                  * the path to just a filename if we need to.
1456                  */
1457                 while (num_tokens(att->filename, '/') > 1) {
1458                         remove_token(att->filename, 0, '/');
1459                 }
1460                 while (num_tokens(att->filename, '\\') > 1) {
1461                         remove_token(att->filename, 0, '\\');
1462                 }
1463
1464                 /* Transfer control of this memory from the upload struct
1465                  * to the attachment struct.
1466                  */
1467                 att->data = WC->upload;
1468                 WC->upload_length = 0;
1469                 WC->upload = NULL;
1470                 display_enter();
1471                 return;
1472         }
1473
1474         if (!strcasecmp(bstr("sc"), "Cancel")) {
1475                 sprintf(WC->ImportantMessage, 
1476                         "Cancelled.  Message was not posted.");
1477         } else if (!strcasecmp(bstr("attach"), "Add")) {
1478                 display_enter();
1479                 return;
1480         } else if (atol(bstr("postseq")) == dont_post) {
1481                 sprintf(WC->ImportantMessage, 
1482                         "Automatically cancelled because you have already "
1483                         "saved this message.");
1484         } else {
1485                 sprintf(buf, "ENT0 1|%s|0|4|%s",
1486                         bstr("recp"),
1487                         bstr("subject") );
1488                 serv_puts(buf);
1489                 serv_gets(buf);
1490                 if (buf[0] == '4') {
1491                         post_mime_to_server();
1492                         if (strlen(bstr("recp")) > 0) {
1493                                 sprintf(WC->ImportantMessage, "Message has been sent.\n");
1494                         }
1495                         else {
1496                                 sprintf(WC->ImportantMessage, "Message has been posted.\n");
1497                         }
1498                         dont_post = atol(bstr("postseq"));
1499                 } else {
1500                         sprintf(WC->ImportantMessage, 
1501                                 "%s", &buf[4]);
1502                 }
1503         }
1504
1505         free_attachments(WC);
1506         readloop("readnew");
1507 }
1508
1509
1510
1511
1512 /*
1513  * display the message entry screen
1514  */
1515 void display_enter(void)
1516 {
1517         char buf[SIZ];
1518         long now;
1519         struct wc_attachment *att;
1520
1521         if (strlen(bstr("force_room")) > 0) {
1522                 gotoroom(bstr("force_room"));
1523         }
1524
1525         /* Are we perhaps in an address book view?  If so, then an "enter
1526          * message" command really means "add new entry."
1527          */
1528         if (WC->wc_view == VIEW_ADDRESSBOOK) {
1529                 do_edit_vcard(-1, "", "");
1530                 return;
1531         }
1532
1533 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1534         /* Are we perhaps in a calendar view?  If so, then an "enter
1535          * message" command really means "add new calendar item."
1536          */
1537         if (WC->wc_view == VIEW_CALENDAR) {
1538                 display_edit_event();
1539                 return;
1540         }
1541
1542         /* Are we perhaps in a tasks view?  If so, then an "enter
1543          * message" command really means "add new task."
1544          */
1545         if (WC->wc_view == VIEW_TASKS) {
1546                 display_edit_task();
1547                 return;
1548         }
1549 #endif
1550
1551         /* Otherwise proceed normally.  Do a custom room banner with no navbar... */
1552         output_headers(1, 1, 2, 0, 0, 0, 0);
1553         wprintf("<div id=\"banner\">\n");
1554         embed_room_banner(NULL, navbar_none);
1555         wprintf("</div>\n");
1556         wprintf("<div id=\"content\">\n"
1557                 "<div id=\"fix_scrollbar_bug\">"
1558                 "<table width=100%% border=0 bgcolor=\"#ffffff\"><tr><td>");
1559
1560         sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
1561         serv_puts(buf);
1562         serv_gets(buf);
1563
1564         if (!strncmp(buf, "570", 3)) {
1565                 if (strlen(bstr("recp")) > 0) {
1566                         svprintf("RECPERROR", WCS_STRING,
1567                                 "<SPAN CLASS=\"errormsg\">%s</SPAN><br />\n",
1568                                 &buf[4]
1569                         );
1570                 }
1571                 do_template("prompt_for_recipient");
1572                 goto DONE;
1573         }
1574         if (buf[0] != '2') {
1575                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1576                 goto DONE;
1577         }
1578
1579         now = time(NULL);
1580         fmt_date(buf, now);
1581         strcat(&buf[strlen(buf)], " <I>from</I> ");
1582         stresc(&buf[strlen(buf)], WC->wc_username, 1, 1);
1583         if (strlen(bstr("recp")) > 0) {
1584                 strcat(&buf[strlen(buf)], " <I>to</I> ");
1585                 stresc(&buf[strlen(buf)], bstr("recp"), 1, 1);
1586         }
1587         strcat(&buf[strlen(buf)], " <I>in</I> ");
1588         stresc(&buf[strlen(buf)], WC->wc_roomname, 1, 1);
1589
1590         /* begin message entry screen */
1591         // wprintf("<div style=\"position:absolute; left:1%%; width:96%%; height:100%%\">\n");
1592
1593         wprintf("<form enctype=\"multipart/form-data\" "
1594                 "method=\"POST\" action=\"/post\" "
1595                 "name=\"enterform\""
1596                 "onSubmit=\"return submitForm();\""
1597                 ">\n");
1598         wprintf("<input type=\"hidden\" name=\"recp\" value=\"%s\">\n",
1599                 bstr("recp"));
1600         wprintf("<input type=\"hidden\" name=\"postseq\" value=\"%ld\">\n",
1601                 now);
1602
1603         wprintf("%s<br>\n", buf);       /* header bar */
1604         wprintf("<img src=\"static/enter.gif\" align=middle alt=\" \">");
1605                 /* "onLoad=\"document.enterform.msgtext.focus();\" " */
1606         wprintf("<font size=-1>Subject (optional):</font>"
1607                 "<input type=\"text\" name=\"subject\" value=\"");
1608         escputs(bstr("subject"));
1609         wprintf("\" size=40 maxlength=70>"
1610                 "&nbsp;"
1611         );
1612
1613         wprintf("<input type=\"submit\" name=\"sc\" value=\"");
1614         if (strlen(bstr("recp")) > 0) {
1615                 wprintf("Send message");
1616         } else {
1617                 wprintf("Post message");
1618         }
1619         wprintf("\">&nbsp;"
1620                 "<input type=\"submit\" name=\"sc\" value=\"Cancel\">\n");
1621
1622         wprintf("<center><script type=\"text/javascript\" "
1623                 "src=\"static/richtext.js\"></script>\n"
1624                 "<script type=\"text/javascript\">\n"
1625                 "function submitForm() { \n"
1626                 "  updateRTE('msgtext'); \n"
1627                 "  return true; \n"
1628                 "} \n"
1629                 "  \n"
1630                 "initRTE(\"static/\", \"static/\", \"\"); \n"
1631                 "</script> \n"
1632                 "<noscript>JavaScript must be enabled.</noscript> \n"
1633                 "<script type=\"text/javascript\"> \n"
1634                 "writeRichText('msgtext', '");
1635         msgescputs(bstr("msgtext"));
1636         wprintf("', '96%%', '200', true, false); \n"
1637                 "</script></center><br />\n");
1638
1639         /* Enumerate any attachments which are already in place... */
1640         wprintf("<img src=\"/static/attachment.gif\" border=0 "
1641                 "align=middle height=16 width=16> Attachments: ");
1642         wprintf("<select name=\"which_attachment\" size=1>");
1643         for (att = WC->first_attachment; att != NULL; att = att->next) {
1644                 wprintf("<option value=\"");
1645                 urlescputs(att->filename);
1646                 wprintf("\">");
1647                 escputs(att->filename);
1648                 /* wprintf(" (%s, %d bytes)",att->content_type,att->length); */
1649                 wprintf("</option>\n");
1650         }
1651         wprintf("</select>");
1652
1653         /* Now offer the ability to attach additional files... */
1654         wprintf("&nbsp;&nbsp;&nbsp;"
1655                 "Attach file: <input NAME=\"attachfile\" "
1656                 "SIZE=16 TYPE=\"file\">\n&nbsp;&nbsp;"
1657                 "<input type=\"submit\" name=\"attach\" value=\"Add\">\n");
1658
1659         wprintf("</form>\n");
1660
1661         wprintf("</td></tr></table></div>\n");
1662 DONE:   wDumpContent(1);
1663 }
1664
1665
1666
1667
1668
1669
1670
1671
1672 void delete_msg(void)
1673 {
1674         long msgid;
1675         char buf[SIZ];
1676
1677         msgid = atol(bstr("msgid"));
1678
1679         output_headers(1, 1, 1, 0, 0, 0, 0);
1680
1681         sprintf(buf, "DELE %ld", msgid);
1682         serv_puts(buf);
1683         serv_gets(buf);
1684         wprintf("<EM>%s</EM><br />\n", &buf[4]);
1685
1686         wDumpContent(1);
1687 }
1688
1689
1690
1691
1692 /*
1693  * Confirm move of a message
1694  */
1695 void confirm_move_msg(void)
1696 {
1697         long msgid;
1698         char buf[SIZ];
1699         char targ[SIZ];
1700
1701         msgid = atol(bstr("msgid"));
1702
1703         output_headers(1, 1, 1, 0, 0, 0, 0);
1704
1705         wprintf("<div id=\"fix_scrollbar_bug\">"
1706                 "<table width=100%% border=0 bgcolor=\"#444455\"><tr><td>");
1707         wprintf("<font size=+1 color=\"#ffffff\"");
1708         wprintf("<b>Confirm move of message</b>\n");
1709         wprintf("</font></td></tr></table></div>\n");
1710
1711         wprintf("<CENTER>");
1712
1713         wprintf("Move this message to:<br />\n");
1714
1715         wprintf("<form METHOD=\"POST\" ACTION=\"/move_msg\">\n");
1716         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
1717                 bstr("msgid"));
1718
1719
1720         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1721         serv_puts("LKRA");
1722         serv_gets(buf);
1723         if (buf[0] == '1') {
1724                 while (serv_gets(buf), strcmp(buf, "000")) {
1725                         extract(targ, buf, 0);
1726                         wprintf("<OPTION>");
1727                         escputs(targ);
1728                         wprintf("\n");
1729                 }
1730         }
1731         wprintf("</SELECT>\n");
1732         wprintf("<br />\n");
1733
1734         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
1735         wprintf("&nbsp;");
1736         wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
1737         wprintf("</form></CENTER>\n");
1738
1739         wprintf("</CENTER>\n");
1740         wDumpContent(1);
1741 }
1742
1743
1744
1745 void move_msg(void)
1746 {
1747         long msgid;
1748         char buf[SIZ];
1749
1750         msgid = atol(bstr("msgid"));
1751
1752         output_headers(1, 1, 1, 0, 0, 0, 0);
1753
1754         if (!strcasecmp(bstr("yesno"), "Move")) {
1755                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1756                 serv_puts(buf);
1757                 serv_gets(buf);
1758                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1759         } else {
1760                 wprintf("<EM>Message not moved.</EM><br />\n");
1761         }
1762
1763         wDumpContent(1);
1764 }
1765
1766 /*
1767  * This gets called when a user selects multiple messages in a summary
1768  * list and then clicks to perform a transformation of some sort on them
1769  * (such as deleting them).
1770  */
1771 void do_stuff_to_msgs(void) {
1772         char buf[SIZ];
1773         char sc[SIZ];
1774
1775         struct stuff_t {
1776                 struct stuff_t *next;
1777                 long msgnum;
1778         };
1779
1780         struct stuff_t *stuff = NULL;
1781         struct stuff_t *ptr;
1782
1783
1784         serv_puts("MSGS ALL");
1785         serv_gets(buf);
1786
1787         if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
1788                 ptr = malloc(sizeof(struct stuff_t));
1789                 ptr->msgnum = atol(buf);
1790                 ptr->next = stuff;
1791                 stuff = ptr;
1792         }
1793
1794         strcpy(sc, bstr("sc"));
1795
1796         while (stuff != NULL) {
1797
1798                 sprintf(buf, "msg_%ld", stuff->msgnum);
1799                 if (!strcasecmp(bstr(buf), "yes")) {
1800
1801                         if (!strcasecmp(sc, "Delete selected")) {
1802                                 serv_printf("DELE %ld", stuff->msgnum);
1803                                 serv_gets(buf);
1804                         }
1805
1806                 }
1807
1808                 ptr = stuff->next;
1809                 free(stuff);
1810                 stuff = ptr;
1811         }
1812
1813         readloop("readfwd");
1814 }