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