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