]> code.citadel.org Git - citadel.git/blob - webcit/messages.c
*** empty log message ***
[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 "webcit.h"
9 #include "vcard.h"
10 #include "webserver.h"
11
12
13 /* Address book entry (keep it short and sweet, it's just a quickie lookup
14  * which we can use to get to the real meat and bones later)
15  */
16 struct addrbookent {
17         char ab_name[64];
18         long ab_msgnum;
19 };
20
21
22
23 #ifdef HAVE_ICONV
24 /* Handle subjects with RFC2047 encoding, such as:
25  * =?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?=
26  */
27 void utf8ify_rfc822_string(char *buf) {
28         char *start, *end;
29         char newbuf[1024];
30         char charset[128];
31         char encoding[16];
32         char istr[1024];
33         iconv_t ic = (iconv_t)(-1) ;
34         char *ibuf;                   /* Buffer of characters to be converted */
35         char *obuf;                   /* Buffer for converted characters      */
36         size_t ibuflen;               /* Length of input buffer               */
37         size_t obuflen;               /* Length of output buffer              */
38         char *isav;                   /* Saved pointer to input buffer        */
39         char *osav;                   /* Saved pointer to output buffer       */
40
41         while (start=strstr(buf, "=?"), end=strstr(buf, "?="),
42                 ((start != NULL) && (end != NULL) && (end > start)) )
43         {
44                 extract_token(charset, start, 1, '?', sizeof charset);
45                 extract_token(encoding, start, 2, '?', sizeof encoding);
46                 extract_token(istr, start, 3, '?', sizeof istr);
47
48                 /*strcpy(start, "");
49                 ++end;
50                 ++end;*/
51
52                 ibuf = malloc(1024);
53                 isav = ibuf;
54                 if (!strcasecmp(encoding, "B")) {       /* base64 */
55                         ibuflen = CtdlDecodeBase64(ibuf, istr, strlen(istr));
56                 }
57                 else if (!strcasecmp(encoding, "Q")) {  /* quoted-printable */
58                         ibuflen = CtdlDecodeQuotedPrintable(ibuf, istr, strlen(istr));
59                 }
60                 else {
61                         strcpy(ibuf, istr);             /* huh? */
62                         ibuflen = strlen(istr);
63                 }
64
65                 ic = iconv_open("UTF-8", charset);
66                 if (ic != (iconv_t)(-1) ) {
67                         obuf = malloc(1024);
68                         obuflen = 1024;
69                         obuf = (char *) malloc(obuflen);
70                         osav = obuf;
71                         iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
72                         osav[1024-obuflen] = 0;
73
74                         end = start;
75                         end++;
76                         strcpy(start, "");
77                         remove_token(end, 0, '?');
78                         remove_token(end, 0, '?');
79                         remove_token(end, 0, '?');
80                         remove_token(end, 0, '?');
81                         strcpy(end, &end[1]);
82
83                         snprintf(newbuf, sizeof newbuf, "%s%s%s", buf, osav, end);
84                         strcpy(buf, newbuf);
85                         free(osav);
86                         iconv_close(ic);
87                 }
88                 else {
89                         snprintf(newbuf, sizeof newbuf, "%s(unreadable)%s", buf, end);
90                         strcpy(buf, newbuf);
91                 }
92
93                 free(isav);
94         }
95
96 }
97 #endif
98
99
100 /*
101  * Look for URL's embedded in a buffer and make them linkable.  We use a
102  * target window in order to keep the BBS session in its own window.
103  */
104 void url(buf)
105 char buf[];
106 {
107
108         int pos;
109         int start, end;
110         char ench;
111         char urlbuf[SIZ];
112         char outbuf[1024];
113
114         start = (-1);
115         end = strlen(buf);
116         ench = 0;
117
118         for (pos = 0; pos < strlen(buf); ++pos) {
119                 if (!strncasecmp(&buf[pos], "http://", 7))
120                         start = pos;
121                 if (!strncasecmp(&buf[pos], "ftp://", 6))
122                         start = pos;
123         }
124
125         if (start < 0)
126                 return;
127
128         if ((start > 0) && (buf[start - 1] == '<'))
129                 ench = '>';
130         if ((start > 0) && (buf[start - 1] == '['))
131                 ench = ']';
132         if ((start > 0) && (buf[start - 1] == '('))
133                 ench = ')';
134         if ((start > 0) && (buf[start - 1] == '{'))
135                 ench = '}';
136
137         for (pos = strlen(buf); pos > start; --pos) {
138                 if ((buf[pos] == ' ') || (buf[pos] == ench))
139                         end = pos;
140         }
141
142         strncpy(urlbuf, &buf[start], end - start);
143         urlbuf[end - start] = 0;
144
145         strncpy(outbuf, buf, start);
146         sprintf(&outbuf[start], "%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
147                 LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
148         strcat(outbuf, &buf[end]);
149         if ( strlen(outbuf) < 250 )
150                 strcpy(buf, outbuf);
151 }
152
153
154 /*
155  * Turn a vCard "n" (name) field into something displayable.
156  */
157 void vcard_n_prettyize(char *name)
158 {
159         char *original_name;
160         int i;
161
162         original_name = strdup(name);
163         for (i=0; i<5; ++i) {
164                 if (strlen(original_name) > 0) {
165                         if (original_name[strlen(original_name)-1] == ' ') {
166                                 original_name[strlen(original_name)-1] = 0;
167                         }
168                         if (original_name[strlen(original_name)-1] == ';') {
169                                 original_name[strlen(original_name)-1] = 0;
170                         }
171                 }
172         }
173         strcpy(name, "");
174         for (i=0; i<strlen(original_name); ++i) {
175                 if (original_name[i] == ';') {
176                         strcat(name, ", ");
177                 }
178                 else {
179                         name[strlen(name)+1] = 0;
180                         name[strlen(name)] = original_name[i];
181                 }
182         }
183         free(original_name);
184 }
185
186
187
188
189 /* display_vcard() calls this after parsing the textual vCard into
190  * our 'struct vCard' data object.
191  * This gets called instead of display_parsed_vcard() if we are only looking
192  * to extract the person's name instead of displaying the card.
193  */
194 void fetchname_parsed_vcard(struct vCard *v, char *storename) {
195         char *name;
196
197         strcpy(storename, "");
198
199         name = vcard_get_prop(v, "n", 1, 0, 0);
200         if (name != NULL) {
201                 strcpy(storename, name);
202                 /* vcard_n_prettyize(storename); */
203         }
204
205 }
206
207
208
209 /* display_vcard() calls this after parsing the textual vCard into
210  * our 'struct vCard' data object.
211  *
212  * Set 'full' to nonzero to display the full card, otherwise it will only
213  * show a summary line.
214  *
215  * This code is a bit ugly, so perhaps an explanation is due: we do this
216  * in two passes through the vCard fields.  On the first pass, we process
217  * fields we understand, and then render them in a pretty fashion at the
218  * end.  Then we make a second pass, outputting all the fields we don't
219  * understand in a simple two-column name/value format.
220  */
221 void display_parsed_vcard(struct vCard *v, int full) {
222         int i, j;
223         char buf[SIZ];
224         char *name;
225         int is_qp = 0;
226         int is_b64 = 0;
227         char *thisname, *thisvalue;
228         char firsttoken[SIZ];
229         int pass;
230
231         char displayname[SIZ];
232         char title[SIZ];
233         char org[SIZ];
234         char phone[SIZ];
235         char mailto[SIZ];
236
237         strcpy(displayname, "");
238         strcpy(phone, "");
239         strcpy(mailto, "");
240         strcpy(title, "");
241         strcpy(org, "");
242
243         if (!full) {
244                 wprintf("<TD>");
245                 name = vcard_get_prop(v, "fn", 1, 0, 0);
246                 if (name != NULL) {
247                         escputs(name);
248                 }
249                 else if (name = vcard_get_prop(v, "n", 1, 0, 0), name != NULL) {
250                         strcpy(displayname, name);
251                         vcard_n_prettyize(displayname);
252                         escputs(displayname);
253                 }
254                 else {
255                         wprintf("&nbsp;");
256                 }
257                 wprintf("</TD>");
258                 return;
259         }
260
261         wprintf("<div align=center><table bgcolor=#aaaaaa width=50%%>");
262         for (pass=1; pass<=2; ++pass) {
263
264                 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
265
266                         thisname = strdup(v->prop[i].name);
267                         extract_token(firsttoken, thisname, 0, ';', sizeof firsttoken);
268         
269                         for (j=0; j<num_tokens(thisname, ';'); ++j) {
270                                 extract_token(buf, thisname, j, ';', sizeof buf);
271                                 if (!strcasecmp(buf, "encoding=quoted-printable")) {
272                                         is_qp = 1;
273                                         remove_token(thisname, j, ';');
274                                 }
275                                 if (!strcasecmp(buf, "encoding=base64")) {
276                                         is_b64 = 1;
277                                         remove_token(thisname, j, ';');
278                                 }
279                         }
280         
281                         if (is_qp) {
282                                 thisvalue = malloc(strlen(v->prop[i].value) + 50);
283                                 j = CtdlDecodeQuotedPrintable(
284                                         thisvalue, v->prop[i].value,
285                                         strlen(v->prop[i].value) );
286                                 thisvalue[j] = 0;
287                         }
288                         else if (is_b64) {
289                                 thisvalue = malloc(strlen(v->prop[i].value) + 50);
290                                 CtdlDecodeBase64(
291                                         thisvalue, v->prop[i].value,
292                                         strlen(v->prop[i].value) );
293                         }
294                         else {
295                                 thisvalue = strdup(v->prop[i].value);
296                         }
297         
298                         /*** Various fields we may encounter ***/
299         
300                         /* N is name, but only if there's no FN already there */
301                         if (!strcasecmp(firsttoken, "n")) {
302                                 if (strlen(displayname) == 0) {
303                                         strcpy(displayname, thisvalue);
304                                         vcard_n_prettyize(displayname);
305                                 }
306                         }
307         
308                         /* FN (full name) is a true 'display name' field */
309                         else if (!strcasecmp(firsttoken, "fn")) {
310                                 strcpy(displayname, thisvalue);
311                         }
312
313                         /* title */
314                         else if (!strcasecmp(firsttoken, "title")) {
315                                 strcpy(title, thisvalue);
316                         }
317         
318                         /* organization */
319                         else if (!strcasecmp(firsttoken, "org")) {
320                                 strcpy(org, thisvalue);
321                         }
322         
323                         else if (!strcasecmp(firsttoken, "email")) {
324                                 if (strlen(mailto) > 0) strcat(mailto, "<br />");
325                                 strcat(mailto,
326                                         "<A HREF=\"/display_enter"
327                                         "?force_room=_MAIL_?recp=");
328                                 urlesc(&mailto[strlen(mailto)], thisvalue);
329                                 strcat(mailto, "\">");
330                                 urlesc(&mailto[strlen(mailto)], thisvalue);
331                                 strcat(mailto, "</A>");
332                         }
333                         else if (!strcasecmp(firsttoken, "tel")) {
334                                 if (strlen(phone) > 0) strcat(phone, "<br />");
335                                 strcat(phone, thisvalue);
336                                 for (j=0; j<num_tokens(thisname, ';'); ++j) {
337                                         extract_token(buf, thisname, j, ';', sizeof buf);
338                                         if (!strcasecmp(buf, "tel"))
339                                                 strcat(phone, "");
340                                         else if (!strcasecmp(buf, "work"))
341                                                 strcat(phone, _(" (work)"));
342                                         else if (!strcasecmp(buf, "home"))
343                                                 strcat(phone, _(" (home)"));
344                                         else if (!strcasecmp(buf, "cell"))
345                                                 strcat(phone, _(" (cell)"));
346                                         else {
347                                                 strcat(phone, " (");
348                                                 strcat(phone, buf);
349                                                 strcat(phone, ")");
350                                         }
351                                 }
352                         }
353                         else if (!strcasecmp(firsttoken, "adr")) {
354                                 if (pass == 2) {
355                                         wprintf("<TR><TD>");
356                                         wprintf(_("Address:"));
357                                         wprintf("</TD><TD>");
358                                         for (j=0; j<num_tokens(thisvalue, ';'); ++j) {
359                                                 extract_token(buf, thisvalue, j, ';', sizeof buf);
360                                                 if (strlen(buf) > 0) {
361                                                         escputs(buf);
362                                                         if (j<3) wprintf("<br />");
363                                                         else wprintf(" ");
364                                                 }
365                                         }
366                                         wprintf("</TD></TR>\n");
367                                 }
368                         }
369                         else if (!strcasecmp(firsttoken, "version")) {
370                                 /* ignore */
371                         }
372                         else if (!strcasecmp(firsttoken, "rev")) {
373                                 /* ignore */
374                         }
375                         else if (!strcasecmp(firsttoken, "label")) {
376                                 /* ignore */
377                         }
378                         else {
379
380                                 /*** Don't show extra fields.  They're ugly.
381                                 if (pass == 2) {
382                                         wprintf("<TR><TD>");
383                                         escputs(thisname);
384                                         wprintf("</TD><TD>");
385                                         escputs(thisvalue);
386                                         wprintf("</TD></TR>\n");
387                                 }
388                                 ***/
389                         }
390         
391                         free(thisname);
392                         free(thisvalue);
393                 }
394         
395                 if (pass == 1) {
396                         wprintf("<TR BGCOLOR=\"#AAAAAA\">"
397                         "<TD COLSPAN=2 BGCOLOR=\"#FFFFFF\">"
398                         "<IMG ALIGN=CENTER SRC=\"/static/viewcontacts_48x.gif\">"
399                         "<FONT SIZE=+1><B>");
400                         escputs(displayname);
401                         wprintf("</B></FONT>");
402                         if (strlen(title) > 0) {
403                                 wprintf("<div align=right>");
404                                 escputs(title);
405                                 wprintf("</div>");
406                         }
407                         if (strlen(org) > 0) {
408                                 wprintf("<div align=right>");
409                                 escputs(org);
410                                 wprintf("</div>");
411                         }
412                         wprintf("</TD></TR>\n");
413                 
414                         if (strlen(phone) > 0) {
415                                 wprintf("<tr><td>");
416                                 wprintf(_("Telephone:"));
417                                 wprintf("</td><td>%s</td></tr>\n", phone);
418                         }
419                         if (strlen(mailto) > 0) {
420                                 wprintf("<tr><td>");
421                                 wprintf(_("E-mail:"));
422                                 wprintf("</td><td>%s</td></tr>\n", mailto);
423                         }
424                 }
425
426         }
427
428         wprintf("</table></div>\n");
429 }
430
431
432
433 /*
434  * Display a textual vCard
435  * (Converts to a vCard object and then calls the actual display function)
436  * Set 'full' to nonzero to display the whole card instead of a one-liner.
437  * Or, if "storename" is non-NULL, just store the person's name in that
438  * buffer instead of displaying the card at all.
439  */
440 void display_vcard(char *vcard_source, char alpha, int full, char *storename) {
441         struct vCard *v;
442         char *name;
443         char buf[SIZ];
444         char this_alpha = 0;
445
446         v = vcard_load(vcard_source);
447         if (v == NULL) return;
448
449         name = vcard_get_prop(v, "n", 1, 0, 0);
450         if (name != NULL) {
451                 strcpy(buf, name);
452                 this_alpha = buf[0];
453         }
454
455         if (storename != NULL) {
456                 fetchname_parsed_vcard(v, storename);
457         }
458         else if (       (alpha == 0)
459                         || ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha)) )
460                         || ((!isalpha(alpha)) && (!isalpha(this_alpha)))
461                 ) {
462                 display_parsed_vcard(v, full);
463         }
464
465         vcard_free(v);
466 }
467
468
469 /*
470  * I wanna SEE that message!
471  */
472 void read_message(long msgnum, int printable_view) {
473         char buf[SIZ];
474         char mime_partnum[256];
475         char mime_filename[256];
476         char mime_content_type[256];
477         char mime_charset[256];
478         char mime_disposition[256];
479         int mime_length;
480         char mime_http[SIZ];
481         char m_subject[256];
482         char m_cc[1024];
483         char from[256];
484         char node[256];
485         char rfca[256];
486         char reply_to[512];
487         char reply_all[4096];
488         char now[64];
489         int format_type = 0;
490         int nhdr = 0;
491         int bq = 0;
492         int i = 0;
493         char vcard_partnum[256];
494         char cal_partnum[256];
495         char *part_source = NULL;
496 #ifdef HAVE_ICONV
497         iconv_t ic = (iconv_t)(-1) ;
498         char *ibuf;                   /* Buffer of characters to be converted */
499         char *obuf;                   /* Buffer for converted characters      */
500         size_t ibuflen;               /* Length of input buffer               */
501         size_t obuflen;               /* Length of output buffer              */
502         char *osav;                   /* Saved pointer to output buffer       */
503 #endif
504
505         strcpy(from, "");
506         strcpy(node, "");
507         strcpy(rfca, "");
508         strcpy(reply_to, "");
509         strcpy(reply_all, "");
510         strcpy(vcard_partnum, "");
511         strcpy(cal_partnum, "");
512         strcpy(mime_http, "");
513         strcpy(mime_content_type, "text/plain");
514         strcpy(mime_charset, "us-ascii");
515
516         serv_printf("MSG4 %ld", msgnum);
517         serv_getln(buf, sizeof buf);
518         if (buf[0] != '1') {
519                 wprintf("<STRONG>");
520                 wprintf(_("ERROR:"));
521                 wprintf("</STRONG> %s<br />\n", &buf[4]);
522                 return;
523         }
524
525         /* begin everythingamundo table */
526         if (!printable_view) {
527                 wprintf("<div id=\"fix_scrollbar_bug\">\n");
528                 wprintf("<table width=100%% border=1 cellspacing=0 "
529                         "cellpadding=0><TR><TD>\n");
530         }
531
532         /* begin message header table */
533         wprintf("<TABLE WIDTH=100%% BORDER=0 CELLSPACING=0 "
534                 "CELLPADDING=1 BGCOLOR=\"#CCCCCC\"><TR><TD>\n");
535
536         wprintf("<SPAN CLASS=\"message_header\">");
537         strcpy(m_subject, "");
538         strcpy(m_cc, "");
539
540         while (serv_getln(buf, sizeof buf), strcasecmp(buf, "text")) {
541                 if (!strcmp(buf, "000")) {
542                         wprintf("<I>");
543                         wprintf(_("unexpected end of message"));
544                         wprintf("</I><br /><br />\n");
545                         wprintf("</SPAN>\n");
546                         return;
547                 }
548                 if (!strncasecmp(buf, "nhdr=yes", 8))
549                         nhdr = 1;
550                 if (nhdr == 1)
551                         buf[0] = '_';
552                 if (!strncasecmp(buf, "type=", 5))
553                         format_type = atoi(&buf[5]);
554                 if (!strncasecmp(buf, "from=", 5)) {
555                         strcpy(from, &buf[5]);
556                         wprintf(_("from "));
557                         wprintf("<A HREF=\"/showuser?who=");
558 #ifdef HAVE_ICONV
559                         utf8ify_rfc822_string(from);
560 #endif
561                         urlescputs(from);
562                         wprintf("\">");
563                         escputs(from);
564                         wprintf("</A> ");
565                 }
566                 if (!strncasecmp(buf, "subj=", 5)) {
567                         safestrncpy(m_subject, &buf[5], sizeof m_subject);
568                 }
569                 if (!strncasecmp(buf, "cccc=", 5)) {
570                         safestrncpy(m_cc, &buf[5], sizeof m_cc);
571                         if (strlen(reply_all) > 0) {
572                                 strcat(reply_all, ", ");
573                         }
574                         safestrncpy(&reply_all[strlen(reply_all)], &buf[5],
575                                 (sizeof reply_all - strlen(reply_all)) );
576                 }
577                 if ((!strncasecmp(buf, "hnod=", 5))
578                     && (strcasecmp(&buf[5], serv_info.serv_humannode))) {
579                         wprintf("(%s) ", &buf[5]);
580                 }
581                 if ((!strncasecmp(buf, "room=", 5))
582                     && (strcasecmp(&buf[5], WC->wc_roomname))
583                     && (strlen(&buf[5])>0) ) {
584                         wprintf(_("in "));
585                         wprintf("%s&gt; ", &buf[5]);
586                 }
587                 if (!strncasecmp(buf, "rfca=", 5)) {
588                         strcpy(rfca, &buf[5]);
589                         wprintf("&lt;");
590                         escputs(rfca);
591                         wprintf("&gt; ");
592                 }
593
594                 if (!strncasecmp(buf, "node=", 5)) {
595                         strcpy(node, &buf[5]);
596                         if ( ((WC->room_flags & QR_NETWORK)
597                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
598                         && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
599                         && (strlen(rfca)==0)
600                         ) {
601                                 wprintf("@%s ", &buf[5]);
602                         }
603                 }
604                 if (!strncasecmp(buf, "rcpt=", 5)) {
605                         wprintf(_("to "));
606                         escputs(&buf[5]);
607                         wprintf(" ");
608                         if (strlen(reply_all) > 0) {
609                                 strcat(reply_all, ", ");
610                         }
611                         safestrncpy(&reply_all[strlen(reply_all)], &buf[5],
612                                 (sizeof reply_all - strlen(reply_all)) );
613                 }
614                 if (!strncasecmp(buf, "time=", 5)) {
615                         fmt_date(now, atol(&buf[5]), 0);
616                         wprintf("%s ", now);
617                 }
618
619                 if (!strncasecmp(buf, "part=", 5)) {
620                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
621                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
622                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
623                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
624                         mime_length = extract_int(&buf[5], 5);
625
626                         if ((!strcasecmp(mime_disposition, "inline"))
627                            && (!strncasecmp(mime_content_type, "image/", 6)) ){
628                                 snprintf(&mime_http[strlen(mime_http)],
629                                         (sizeof(mime_http) - strlen(mime_http) - 1),
630                                         "<IMG SRC=\"/mimepart/%ld/%s/%s\">",
631                                         msgnum, mime_partnum, mime_filename);
632                         }
633                         else if ( (!strcasecmp(mime_disposition, "attachment")) 
634                              || (!strcasecmp(mime_disposition, "inline")) ) {
635                                 snprintf(&mime_http[strlen(mime_http)],
636                                         (sizeof(mime_http) - strlen(mime_http) - 1),
637                                         "<A HREF=\"/mimepart/%ld/%s/%s\" "
638                                         "TARGET=\"wc.%ld.%s\">"
639                                         "<IMG SRC=\"/static/diskette_24x.gif\" "
640                                         "BORDER=0 ALIGN=MIDDLE>\n"
641                                         "Part %s: %s (%s, %d bytes)</A><br />\n",
642                                         msgnum, mime_partnum, mime_filename,
643                                         msgnum, mime_partnum,
644                                         mime_partnum, mime_filename,
645                                         mime_content_type, mime_length);
646                         }
647
648                         /*** begin handler prep ***/
649                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
650                                 strcpy(vcard_partnum, mime_partnum);
651                         }
652
653                         if (!strcasecmp(mime_content_type, "text/calendar")) {
654                                 strcpy(cal_partnum, mime_partnum);
655                         }
656
657                         /*** end handler prep ***/
658
659                 }
660
661         }
662
663         /* Generate a reply-to address */
664         if (strlen(rfca) > 0) {
665                 strcpy(reply_to, rfca);
666         }
667         else {
668                 if ( (strlen(node) > 0)
669                    && (strcasecmp(node, serv_info.serv_nodename))
670                    && (strcasecmp(node, serv_info.serv_humannode)) ) {
671                         snprintf(reply_to, sizeof(reply_to), "%s @ %s",
672                                 from, node);
673                 }
674                 else {
675                         snprintf(reply_to, sizeof(reply_to), "%s", from);
676                 }
677         }
678
679         if (nhdr == 1) {
680                 wprintf("****");
681         }
682
683         wprintf("</SPAN>");
684 #ifdef HAVE_ICONV
685         utf8ify_rfc822_string(m_cc);
686         utf8ify_rfc822_string(m_subject);
687 #endif
688         if (strlen(m_cc) > 0) {
689                 wprintf("<br />"
690                         "<SPAN CLASS=\"message_subject\">");
691                 wprintf(_("CC:"));
692                 wprintf(" ");
693                 escputs(m_cc);
694                 wprintf("</SPAN>");
695         }
696         if (strlen(m_subject) > 0) {
697                 wprintf("<br />"
698                         "<SPAN CLASS=\"message_subject\">");
699                 wprintf(_("Subject:"));
700                 wprintf(" ");
701                 escputs(m_subject);
702                 wprintf("</SPAN>");
703         }
704         wprintf("</TD>\n");
705
706         /* start msg buttons */
707         if (!printable_view) {
708                 wprintf("<td align=right>\n");
709
710                 /* Reply */
711                 wprintf("<a href=\"/display_enter?recp=");
712                 urlescputs(reply_to);
713                 wprintf("?subject=");
714                 if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%20");
715                 urlescputs(m_subject);
716                 wprintf("\">[%s]</a> ", _("Reply"));
717
718                 /* ReplyAll */
719                 if (WC->wc_view == VIEW_MAILBOX) {
720                         wprintf("<a href=\"/display_enter?recp=");
721                         urlescputs(reply_to);
722                         wprintf("?cc=");
723                         urlescputs(reply_all);
724                         wprintf("?subject=");
725                         if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%20");
726                         urlescputs(m_subject);
727                         wprintf("\">[%s]</a> ", _("ReplyAll"));
728                 }
729
730                 /* Forward */
731                 if (WC->wc_view == VIEW_MAILBOX) {
732                         wprintf("<a href=\"/display_enter?pullquote=%ld?subject=", msgnum);
733                         if (strncasecmp(m_subject, "Fwd:", 4)) wprintf("Fwd:%20");
734                         urlescputs(m_subject);
735                         wprintf("\">[%s]</a> ", _("Forward"));
736                 }
737
738                 if (WC->is_room_aide)  {
739                         /* Move */
740                         wprintf("<a href=\"/confirm_move_msg?msgid=%ld\">[%s]</a> ",
741                                 msgnum, _("Move"));
742         
743                         /* Delete */
744                         wprintf("<a href=\"/delete_msg?msgid=%ld\" "
745                                 "onClick=\"return confirm('%s');\">"
746                                 "[%s]</a> ", msgnum, _("Delete this message?"), _("Delete")
747                         );
748                 }
749
750                 wprintf("<a href=\"#\" onClick=\"window.open('/printmsg?msgnum=%ld', 'print%ld', 'toolbar=no,location=no,directories=no,copyhistory=no,status=yes,scrollbars=yes,resizable=yes,width=600,height=400'); \" >"
751                         "[%s]</a>", msgnum, msgnum, _("Print"));
752
753                 wprintf("</td>");
754         }
755
756         wprintf("</TR></TABLE>\n");
757
758         /* Begin body */
759         wprintf("<TABLE BORDER=0 WIDTH=100%% BGCOLOR=#FFFFFF "
760                 "CELLPADDING=1 CELLSPACING=0><TR><TD>");
761
762         /* 
763          * Learn the content type
764          */
765         strcpy(mime_content_type, "text/plain");
766         while (serv_getln(buf, sizeof buf), (strlen(buf) > 0)) {
767                 if (!strcmp(buf, "000")) {
768                         wprintf("<I>");
769                         wprintf(_("unexpected end of message"));
770                         wprintf("</I><br /><br />\n");
771                         goto ENDBODY;
772                 }
773                 if (!strncasecmp(buf, "Content-type: ", 14)) {
774                         safestrncpy(mime_content_type, &buf[14],
775                                 sizeof(mime_content_type));
776                         for (i=0; i<strlen(mime_content_type); ++i) {
777                                 if (!strncasecmp(&mime_content_type[i], "charset=", 8)) {
778                                         safestrncpy(mime_charset, &mime_content_type[i+8],
779                                                 sizeof mime_charset);
780                                 }
781                         }
782                         for (i=0; i<strlen(mime_content_type); ++i) {
783                                 if (mime_content_type[i] == ';') {
784                                         mime_content_type[i] = 0;
785                                 }
786                         }
787                 }
788         }
789
790         /* Set up a character set conversion if we need to (and if we can) */
791 #ifdef HAVE_ICONV
792         if ( (strcasecmp(mime_charset, "us-ascii"))
793            && (strcasecmp(mime_charset, "UTF-8")) ) {
794                 ic = iconv_open("UTF-8", mime_charset);
795                 if (ic == (iconv_t)(-1) ) {
796                         lprintf(5, "iconv_open() failed: %s\n", strerror(errno));
797                 }
798         }
799 #endif
800
801         /* Messages in legacy Citadel variformat get handled thusly... */
802         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
803                 fmout("JUSTIFY");
804         }
805
806         /* Boring old 80-column fixed format text gets handled this way... */
807         else if ( (!strcasecmp(mime_content_type, "text/plain"))
808                 || (!strcasecmp(mime_content_type, "text")) ) {
809                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
810                         if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
811                         if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
812
813 #ifdef HAVE_ICONV
814                         if (ic != (iconv_t)(-1) ) {
815                                 ibuf = buf;
816                                 ibuflen = strlen(ibuf);
817                                 obuflen = SIZ;
818                                 obuf = (char *) malloc(obuflen);
819                                 osav = obuf;
820                                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
821                                 osav[SIZ-obuflen] = 0;
822                                 safestrncpy(buf, osav, sizeof buf);
823                                 free(osav);
824                         }
825 #endif
826
827                         while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
828                                 buf[strlen(buf) - 1] = 0;
829                         if ((bq == 0) &&
830                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
831                                 wprintf("<BLOCKQUOTE>");
832                                 bq = 1;
833                         } else if ((bq == 1) &&
834                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
835                                 wprintf("</BLOCKQUOTE>");
836                                 bq = 0;
837                         }
838                         wprintf("<TT>");
839                         url(buf);
840                         escputs(buf);
841                         wprintf("</TT><br />\n");
842                 }
843                 wprintf("</I><br />");
844         }
845
846         else /* HTML is fun, but we've got to strip it first */
847         if (!strcasecmp(mime_content_type, "text/html")) {
848                 output_html(mime_charset);
849         }
850
851         /* Unknown weirdness */
852         else {
853                 wprintf(_("I don't know how to display %s"), mime_content_type);
854                 wprintf("<br />\n", mime_content_type);
855                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) { }
856         }
857
858         /* Afterwards, offer links to download attachments 'n' such */
859         if (strlen(mime_http) > 0) {
860                 wprintf("%s", mime_http);
861         }
862
863         /* Handler for vCard parts */
864         if (strlen(vcard_partnum) > 0) {
865                 part_source = load_mimepart(msgnum, vcard_partnum);
866                 if (part_source != NULL) {
867
868                         /* If it's my vCard I can edit it */
869                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
870                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
871                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
872                         ) {
873                                 wprintf("<A HREF=\"/edit_vcard?"
874                                         "msgnum=%ld?partnum=%s\">",
875                                         msgnum, vcard_partnum);
876                                 wprintf("[%s]</A>", _("edit"));
877                         }
878
879                         /* In all cases, display the full card */
880                         display_vcard(part_source, 0, 1, NULL);
881                 }
882         }
883
884         /* Handler for calendar parts */
885         if (strlen(cal_partnum) > 0) {
886                 part_source = load_mimepart(msgnum, cal_partnum);
887                 if (part_source != NULL) {
888                         cal_process_attachment(part_source,
889                                                 msgnum, cal_partnum);
890                 }
891         }
892
893         if (part_source) {
894                 free(part_source);
895                 part_source = NULL;
896         }
897
898 ENDBODY:
899         wprintf("</TD></TR></TABLE>\n");
900
901         /* end everythingamundo table */
902         if (!printable_view) {
903                 wprintf("</TD></TR></TABLE>\n");
904                 wprintf("</div><br />\n");
905         }
906
907 #ifdef HAVE_ICONV
908         if (ic != (iconv_t)(-1) ) {
909                 iconv_close(ic);
910         }
911 #endif
912 }
913
914
915
916 /*
917  * Unadorned HTML output of an individual message, suitable
918  * for placing in a hidden iframe, for printing, or whatever
919  */
920 void embed_message(void) {
921         long msgnum = 0L;
922
923         msgnum = atol(bstr("msgnum"));
924         begin_ajax_response();
925         read_message(msgnum, 0);
926         end_ajax_response();
927 }
928
929
930 /*
931  * Printable view of a message
932  */
933 void print_message(void) {
934         long msgnum = 0L;
935
936         msgnum = atol(bstr("msgnum"));
937         output_headers(0, 0, 0, 0, 0, 0);
938
939         wprintf("Content-type: text/html\r\n"
940                 "Server: %s\r\n"
941                 "Connection: close\r\n",
942                 SERVER);
943         begin_burst();
944
945         wprintf("\r\n\r\n<html>\n"
946                 "<head><title>Printable view</title></head>\n"
947                 "<body onLoad=\" window.print(); window.close(); \">\n"
948         );
949         
950         read_message(msgnum, 1);
951
952         wprintf("\n</body></html>\n\n");
953         wDumpContent(0);
954 }
955
956
957
958 /*
959  * Read message in simple, JavaScript-embeddable form for 'forward'
960  * or 'reply quoted' operations.
961  *
962  * NOTE: it is VITALLY IMPORTANT that we output no single-quotes or linebreaks
963  *       in this function.  Doing so would throw a JavaScript error in the
964  *       'supplied text' argument to the editor.
965  */
966 void pullquote_message(long msgnum, int forward_attachments) {
967         char buf[SIZ];
968         char mime_partnum[256];
969         char mime_filename[256];
970         char mime_content_type[256];
971         char mime_charset[256];
972         char mime_disposition[256];
973         int mime_length;
974         char mime_http[SIZ];
975         char *attachments = NULL;
976         int num_attachments = 0;
977         struct wc_attachment *att, *aptr;
978         char m_subject[256];
979         char from[256];
980         char node[256];
981         char rfca[256];
982         char reply_to[512];
983         char now[256];
984         int format_type = 0;
985         int nhdr = 0;
986         int bq = 0;
987         int i = 0;
988 #ifdef HAVE_ICONV
989         iconv_t ic = (iconv_t)(-1) ;
990         char *ibuf;                   /* Buffer of characters to be converted */
991         char *obuf;                   /* Buffer for converted characters      */
992         size_t ibuflen;               /* Length of input buffer               */
993         size_t obuflen;               /* Length of output buffer              */
994         char *osav;                   /* Saved pointer to output buffer       */
995 #endif
996
997         strcpy(from, "");
998         strcpy(node, "");
999         strcpy(rfca, "");
1000         strcpy(reply_to, "");
1001         strcpy(mime_http, "");
1002         strcpy(mime_content_type, "text/plain");
1003         strcpy(mime_charset, "us-ascii");
1004
1005         serv_printf("MSG4 %ld", msgnum);
1006         serv_getln(buf, sizeof buf);
1007         if (buf[0] != '1') {
1008                 wprintf(_("ERROR:"));
1009                 wprintf("%s<br />", &buf[4]);
1010                 return;
1011         }
1012
1013         strcpy(m_subject, "");
1014
1015         while (serv_getln(buf, sizeof buf), strcasecmp(buf, "text")) {
1016                 if (!strcmp(buf, "000")) {
1017                         wprintf(_("unexpected end of message"));
1018                         return;
1019                 }
1020                 if (!strncasecmp(buf, "nhdr=yes", 8))
1021                         nhdr = 1;
1022                 if (nhdr == 1)
1023                         buf[0] = '_';
1024                 if (!strncasecmp(buf, "type=", 5))
1025                         format_type = atoi(&buf[5]);
1026                 if (!strncasecmp(buf, "from=", 5)) {
1027                         strcpy(from, &buf[5]);
1028                         wprintf(_("from "));
1029 #ifdef HAVE_ICONV
1030                         utf8ify_rfc822_string(from);
1031 #endif
1032                         msgescputs(from);
1033                 }
1034                 if (!strncasecmp(buf, "subj=", 5)) {
1035                         strcpy(m_subject, &buf[5]);
1036                 }
1037                 if ((!strncasecmp(buf, "hnod=", 5))
1038                     && (strcasecmp(&buf[5], serv_info.serv_humannode))) {
1039                         wprintf("(%s) ", &buf[5]);
1040                 }
1041                 if ((!strncasecmp(buf, "room=", 5))
1042                     && (strcasecmp(&buf[5], WC->wc_roomname))
1043                     && (strlen(&buf[5])>0) ) {
1044                         wprintf(_("in "));
1045                         wprintf("%s&gt; ", &buf[5]);
1046                 }
1047                 if (!strncasecmp(buf, "rfca=", 5)) {
1048                         strcpy(rfca, &buf[5]);
1049                         wprintf("&lt;");
1050                         msgescputs(rfca);
1051                         wprintf("&gt; ");
1052                 }
1053
1054                 if (!strncasecmp(buf, "node=", 5)) {
1055                         strcpy(node, &buf[5]);
1056                         if ( ((WC->room_flags & QR_NETWORK)
1057                         || ((strcasecmp(&buf[5], serv_info.serv_nodename)
1058                         && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
1059                         && (strlen(rfca)==0)
1060                         ) {
1061                                 wprintf("@%s ", &buf[5]);
1062                         }
1063                 }
1064                 if (!strncasecmp(buf, "rcpt=", 5)) {
1065                         wprintf(_("to "));
1066                         wprintf("%s ", &buf[5]);
1067                 }
1068                 if (!strncasecmp(buf, "time=", 5)) {
1069                         fmt_date(now, atol(&buf[5]), 0);
1070                         wprintf("%s ", now);
1071                 }
1072
1073                 /*
1074                  * Save attachment info for later.  We can't start downloading them
1075                  * yet because we're in the middle of a server transaction.
1076                  */
1077                 if (!strncasecmp(buf, "part=", 5)) {
1078                         ++num_attachments;
1079                         attachments = realloc(attachments, (num_attachments * 1024));
1080                         strcat(attachments, &buf[5]);
1081                         strcat(attachments, "\n");
1082                 }
1083
1084         }
1085
1086         wprintf("<br>");
1087
1088 #ifdef HAVE_ICONV
1089         utf8ify_rfc822_string(m_subject);
1090 #endif
1091         if (strlen(m_subject) > 0) {
1092                 wprintf(_("Subject:"));
1093                 wprintf(" ");
1094                 msgescputs(m_subject);
1095                 wprintf("<br />");
1096         }
1097
1098         /*
1099          * Begin body
1100          */
1101         wprintf("<br>");
1102
1103         /* 
1104          * Learn the content type
1105          */
1106         strcpy(mime_content_type, "text/plain");
1107         while (serv_getln(buf, sizeof buf), (strlen(buf) > 0)) {
1108                 if (!strcmp(buf, "000")) {
1109                         wprintf(_("unexpected end of message"));
1110                         goto ENDBODY;
1111                 }
1112                 if (!strncasecmp(buf, "Content-type: ", 14)) {
1113                         safestrncpy(mime_content_type, &buf[14],
1114                                 sizeof(mime_content_type));
1115                         for (i=0; i<strlen(mime_content_type); ++i) {
1116                                 if (!strncasecmp(&mime_content_type[i], "charset=", 8)) {
1117                                         safestrncpy(mime_charset, &mime_content_type[i+8],
1118                                                 sizeof mime_charset);
1119                                 }
1120                         }
1121                         for (i=0; i<strlen(mime_content_type); ++i) {
1122                                 if (mime_content_type[i] == ';') {
1123                                         mime_content_type[i] = 0;
1124                                 }
1125                         }
1126                 }
1127         }
1128
1129         /* Set up a character set conversion if we need to (and if we can) */
1130 #ifdef HAVE_ICONV
1131         if ( (strcasecmp(mime_charset, "us-ascii"))
1132            && (strcasecmp(mime_charset, "UTF-8")) ) {
1133                 ic = iconv_open("UTF-8", mime_charset);
1134                 if (ic == (iconv_t)(-1) ) {
1135                         lprintf(5, "iconv_open() failed: %s\n", strerror(errno));
1136                 }
1137         }
1138 #endif
1139
1140         /* Messages in legacy Citadel variformat get handled thusly... */
1141         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
1142                 pullquote_fmout();
1143         }
1144
1145         /* Boring old 80-column fixed format text gets handled this way... */
1146         else if (!strcasecmp(mime_content_type, "text/plain")) {
1147                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1148                         if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
1149                         if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
1150
1151 #ifdef HAVE_ICONV
1152                         if (ic != (iconv_t)(-1) ) {
1153                                 ibuf = buf;
1154                                 ibuflen = strlen(ibuf);
1155                                 obuflen = SIZ;
1156                                 obuf = (char *) malloc(obuflen);
1157                                 osav = obuf;
1158                                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
1159                                 osav[SIZ-obuflen] = 0;
1160                                 safestrncpy(buf, osav, sizeof buf);
1161                                 free(osav);
1162                         }
1163 #endif
1164
1165                         while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
1166                                 buf[strlen(buf) - 1] = 0;
1167                         if ((bq == 0) &&
1168                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
1169                                 wprintf("<BLOCKQUOTE>");
1170                                 bq = 1;
1171                         } else if ((bq == 1) &&
1172                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
1173                                 wprintf("</BLOCKQUOTE>");
1174                                 bq = 0;
1175                         }
1176                         wprintf("<TT>");
1177                         url(buf);
1178                         msgescputs(buf);
1179                         wprintf("</TT><br />");
1180                 }
1181                 wprintf("</I><br />");
1182         }
1183
1184         /* HTML just gets escaped and stuffed back into the editor */
1185         else if (!strcasecmp(mime_content_type, "text/html")) {
1186                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1187                         msgescputs(buf);
1188                 }
1189         }
1190
1191         /* Unknown weirdness ... don't know how to handle this content type */
1192         else {
1193                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) { }
1194         }
1195
1196 ENDBODY:
1197         /* end of body handler */
1198
1199         /*
1200          * If there were attachments, we have to download them and insert them
1201          * into the attachment chain for the forwarded message we are composing.
1202          */
1203         if (num_attachments) {
1204                 for (i=0; i<num_attachments; ++i) {
1205                         extract_token(buf, attachments, i, '\n', sizeof buf);
1206                         extract_token(mime_filename, buf, 1, '|', sizeof mime_filename);
1207                         extract_token(mime_partnum, buf, 2, '|', sizeof mime_partnum);
1208                         extract_token(mime_disposition, buf, 3, '|', sizeof mime_disposition);
1209                         extract_token(mime_content_type, buf, 4, '|', sizeof mime_content_type);
1210                         mime_length = extract_int(buf, 5);
1211
1212                         /*
1213                          * tracing  ... uncomment if necessary
1214                          *
1215                         lprintf(9, "fwd filename: %s\n", mime_filename);
1216                         lprintf(9, "fwd partnum : %s\n", mime_partnum);
1217                         lprintf(9, "fwd conttype: %s\n", mime_content_type);
1218                         lprintf(9, "fwd dispose : %s\n", mime_disposition);
1219                         lprintf(9, "fwd length  : %d\n", mime_length);
1220                          */
1221
1222                         if ( (!strcasecmp(mime_disposition, "inline"))
1223                            || (!strcasecmp(mime_disposition, "attachment")) ) {
1224                 
1225                                 /* Create an attachment struct from this mime part... */
1226                                 att = malloc(sizeof(struct wc_attachment));
1227                                 memset(att, 0, sizeof(struct wc_attachment));
1228                                 att->length = mime_length;
1229                                 strcpy(att->content_type, mime_content_type);
1230                                 strcpy(att->filename, mime_filename);
1231                                 att->next = NULL;
1232                                 att->data = load_mimepart(msgnum, mime_partnum);
1233                 
1234                                 /* And add it to the list. */
1235                                 if (WC->first_attachment == NULL) {
1236                                         WC->first_attachment = att;
1237                                 }
1238                                 else {
1239                                         aptr = WC->first_attachment;
1240                                         while (aptr->next != NULL) aptr = aptr->next;
1241                                         aptr->next = att;
1242                                 }
1243                         }
1244
1245                 }
1246                 free(attachments);
1247         }
1248
1249 #ifdef HAVE_ICONV
1250         if (ic != (iconv_t)(-1) ) {
1251                 iconv_close(ic);
1252         }
1253 #endif
1254 }
1255
1256
1257
1258
1259
1260
1261 void display_summarized(int num) {
1262         char datebuf[64];
1263
1264         wprintf("<TD>");
1265         if (WC->summ[num].is_new) wprintf("<B>");
1266         escputs(WC->summ[num].subj);
1267         if (WC->summ[num].is_new) wprintf("</B>");
1268         wprintf("</TD><TD>");
1269         if (WC->summ[num].is_new) wprintf("<B>");
1270         escputs(WC->summ[num].from);
1271         if (WC->summ[num].is_new) wprintf("</B>");
1272         wprintf(" </TD><TD>");
1273         if (WC->summ[num].is_new) wprintf("<B>");
1274         fmt_date(datebuf, WC->summ[num].date, 1);       /* brief */
1275         escputs(datebuf);
1276         if (WC->summ[num].is_new) wprintf("</B>");
1277         wprintf(" </TD>");
1278         wprintf("<TD>"
1279                 "<INPUT TYPE=\"checkbox\" NAME=\"msg_%ld\" VALUE=\"yes\">"
1280                 "</TD>\n",
1281                 WC->summ[num].msgnum
1282         );
1283 }
1284
1285
1286
1287
1288
1289 void display_addressbook(long msgnum, char alpha) {
1290         char buf[SIZ];
1291         char mime_partnum[SIZ];
1292         char mime_filename[SIZ];
1293         char mime_content_type[SIZ];
1294         char mime_disposition[SIZ];
1295         int mime_length;
1296         char vcard_partnum[SIZ];
1297         char *vcard_source = NULL;
1298         struct message_summary summ;
1299
1300         memset(&summ, 0, sizeof(summ));
1301         safestrncpy(summ.subj, _("(no subject)"), sizeof summ.subj);
1302
1303         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
1304         serv_puts(buf);
1305         serv_getln(buf, sizeof buf);
1306         if (buf[0] != '1') return;
1307
1308         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1309                 if (!strncasecmp(buf, "part=", 5)) {
1310                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
1311                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
1312                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
1313                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
1314                         mime_length = extract_int(&buf[5], 5);
1315
1316                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
1317                                 strcpy(vcard_partnum, mime_partnum);
1318                         }
1319
1320                 }
1321         }
1322
1323         if (strlen(vcard_partnum) > 0) {
1324                 vcard_source = load_mimepart(msgnum, vcard_partnum);
1325                 if (vcard_source != NULL) {
1326
1327                         /* Display the summary line */
1328                         display_vcard(vcard_source, alpha, 0, NULL);
1329
1330                         /* If it's my vCard I can edit it */
1331                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
1332                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
1333                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
1334                         ) {
1335                                 wprintf("<A HREF=\"/edit_vcard?"
1336                                         "msgnum=%ld?partnum=%s\">",
1337                                         msgnum, vcard_partnum);
1338                                 wprintf("[%s]</A>", _("edit"));
1339                         }
1340
1341                         free(vcard_source);
1342                 }
1343         }
1344
1345 }
1346
1347
1348
1349 /* If it's an old "Firstname Lastname" style record, try to
1350  * convert it.
1351  */
1352 void lastfirst_firstlast(char *namebuf) {
1353         char firstname[SIZ];
1354         char lastname[SIZ];
1355         int i;
1356
1357         if (namebuf == NULL) return;
1358         if (strchr(namebuf, ';') != NULL) return;
1359
1360         i = num_tokens(namebuf, ' ');
1361         if (i < 2) return;
1362
1363         extract_token(lastname, namebuf, i-1, ' ', sizeof lastname);
1364         remove_token(namebuf, i-1, ' ');
1365         strcpy(firstname, namebuf);
1366         sprintf(namebuf, "%s; %s", lastname, firstname);
1367 }
1368
1369
1370 void fetch_ab_name(long msgnum, char *namebuf) {
1371         char buf[SIZ];
1372         char mime_partnum[SIZ];
1373         char mime_filename[SIZ];
1374         char mime_content_type[SIZ];
1375         char mime_disposition[SIZ];
1376         int mime_length;
1377         char vcard_partnum[SIZ];
1378         char *vcard_source = NULL;
1379         int i;
1380         struct message_summary summ;
1381
1382         if (namebuf == NULL) return;
1383         strcpy(namebuf, "");
1384
1385         memset(&summ, 0, sizeof(summ));
1386         safestrncpy(summ.subj, "(no subject)", sizeof summ.subj);
1387
1388         sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
1389         serv_puts(buf);
1390         serv_getln(buf, sizeof buf);
1391         if (buf[0] != '1') return;
1392
1393         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1394                 if (!strncasecmp(buf, "part=", 5)) {
1395                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
1396                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
1397                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
1398                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
1399                         mime_length = extract_int(&buf[5], 5);
1400
1401                         if (!strcasecmp(mime_content_type, "text/x-vcard")) {
1402                                 strcpy(vcard_partnum, mime_partnum);
1403                         }
1404
1405                 }
1406         }
1407
1408         if (strlen(vcard_partnum) > 0) {
1409                 vcard_source = load_mimepart(msgnum, vcard_partnum);
1410                 if (vcard_source != NULL) {
1411
1412                         /* Grab the name off the card */
1413                         display_vcard(vcard_source, 0, 0, namebuf);
1414
1415                         free(vcard_source);
1416                 }
1417         }
1418
1419         lastfirst_firstlast(namebuf);
1420         striplt(namebuf);
1421         for (i=0; i<strlen(namebuf); ++i) {
1422                 if (namebuf[i] != ';') return;
1423         }
1424         strcpy(namebuf, _("(no name)"));
1425 }
1426
1427
1428
1429 /*
1430  * Record compare function for sorting address book indices
1431  */
1432 int abcmp(const void *ab1, const void *ab2) {
1433         return(strcasecmp(
1434                 (((const struct addrbookent *)ab1)->ab_name),
1435                 (((const struct addrbookent *)ab2)->ab_name)
1436         ));
1437 }
1438
1439
1440 /*
1441  * Helper function for do_addrbook_view()
1442  * Converts a name into a three-letter tab label
1443  */
1444 void nametab(char *tabbuf, char *name) {
1445         stresc(tabbuf, name, 0, 0);
1446         tabbuf[0] = toupper(tabbuf[0]);
1447         tabbuf[1] = tolower(tabbuf[1]);
1448         tabbuf[2] = tolower(tabbuf[2]);
1449         tabbuf[3] = 0;
1450 }
1451
1452
1453 /*
1454  * Render the address book using info we gathered during the scan
1455  */
1456 void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
1457         int i = 0;
1458         int displayed = 0;
1459         int bg = 0;
1460         static int NAMESPERPAGE = 60;
1461         int num_pages = 0;
1462         int page = 0;
1463         int tabfirst = 0;
1464         char tabfirst_label[SIZ];
1465         int tablast = 0;
1466         char tablast_label[SIZ];
1467
1468         if (num_ab == 0) {
1469                 wprintf("<br /><br /><br /><div align=\"center\"><i>");
1470                 wprintf(_("This address book is empty."));
1471                 wprintf("</i></div>\n");
1472                 return;
1473         }
1474
1475         if (num_ab > 1) {
1476                 qsort(addrbook, num_ab, sizeof(struct addrbookent), abcmp);
1477         }
1478
1479         num_pages = num_ab / NAMESPERPAGE;
1480
1481         page = atoi(bstr("page"));
1482
1483         wprintf("Page: ");
1484         for (i=0; i<=num_pages; ++i) {
1485                 if (i != page) {
1486                         wprintf("<A HREF=\"/readfwd?page=%d\">", i);
1487                 }
1488                 else {
1489                         wprintf("<B>");
1490                 }
1491                 tabfirst = i * NAMESPERPAGE;
1492                 tablast = tabfirst + NAMESPERPAGE - 1;
1493                 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
1494                 nametab(tabfirst_label, addrbook[tabfirst].ab_name);
1495                 nametab(tablast_label, addrbook[tablast].ab_name);
1496                 wprintf("[%s&nbsp;-&nbsp;%s]",
1497                         tabfirst_label, tablast_label
1498                 );
1499                 if (i != page) {
1500                         wprintf("</A>\n");
1501                 }
1502                 else {
1503                         wprintf("</B>\n");
1504                 }
1505         }
1506         wprintf("<br />\n");
1507
1508         wprintf("<TABLE border=0 cellspacing=0 "
1509                 "cellpadding=3 width=100%%>\n"
1510         );
1511
1512         for (i=0; i<num_ab; ++i) {
1513
1514                 if ((i / NAMESPERPAGE) == page) {
1515
1516                         if ((displayed % 4) == 0) {
1517                                 if (displayed > 0) {
1518                                         wprintf("</TR>\n");
1519                                 }
1520                                 bg = 1 - bg;
1521                                 wprintf("<TR BGCOLOR=\"#%s\">",
1522                                         (bg ? "DDDDDD" : "FFFFFF")
1523                                 );
1524                         }
1525         
1526                         wprintf("<TD>");
1527         
1528                         wprintf("<A HREF=\"/readfwd?startmsg=%ld&is_singlecard=1",
1529                                 addrbook[i].ab_msgnum);
1530                         wprintf("?maxmsgs=1?summary=0?alpha=%s\">", bstr("alpha"));
1531                         vcard_n_prettyize(addrbook[i].ab_name);
1532                         escputs(addrbook[i].ab_name);
1533                         wprintf("</A></TD>\n");
1534                         ++displayed;
1535                 }
1536         }
1537
1538         wprintf("</TR></TABLE>\n");
1539 }
1540
1541
1542
1543 /* 
1544  * load message pointers from the server
1545  */
1546 int load_msg_ptrs(char *servcmd, int with_headers)
1547 {
1548         char buf[1024];
1549         time_t datestamp;
1550         char displayname[128];
1551         char nodename[128];
1552         char inetaddr[128];
1553         char subject[256];
1554         int nummsgs;
1555         int maxload = 0;
1556
1557         int num_summ_alloc = 0;
1558
1559         if (with_headers) {
1560                 if (WC->num_summ != 0) {
1561                         free(WC->summ);
1562                         WC->num_summ = 0;
1563                 }
1564         }
1565         num_summ_alloc = 100;
1566         WC->num_summ = 0;
1567         WC->summ = malloc(num_summ_alloc * sizeof(struct message_summary));
1568
1569         nummsgs = 0;
1570         maxload = sizeof(WC->msgarr) / sizeof(long) ;
1571         serv_puts(servcmd);
1572         serv_getln(buf, sizeof buf);
1573         if (buf[0] != '1') {
1574                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1575                 return (nummsgs);
1576         }
1577         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1578                 if (nummsgs < maxload) {
1579                         WC->msgarr[nummsgs] = extract_long(buf, 0);
1580                         datestamp = extract_long(buf, 1);
1581                         extract_token(displayname, buf, 2, '|', sizeof displayname);
1582                         extract_token(nodename, buf, 3, '|', sizeof nodename);
1583                         extract_token(inetaddr, buf, 4, '|', sizeof inetaddr);
1584                         extract_token(subject, buf, 5, '|', sizeof subject);
1585                         ++nummsgs;
1586
1587                         if (with_headers) {
1588                                 if (nummsgs > num_summ_alloc) {
1589                                         num_summ_alloc *= 2;
1590                                         WC->summ = realloc(WC->summ, num_summ_alloc * sizeof(struct message_summary));
1591                                 }
1592                                 ++WC->num_summ;
1593
1594                                 memset(&WC->summ[nummsgs-1], 0, sizeof(struct message_summary));
1595                                 WC->summ[nummsgs-1].msgnum = WC->msgarr[nummsgs-1];
1596                                 safestrncpy(WC->summ[nummsgs-1].subj, _("(no subject)"), sizeof WC->summ[nummsgs-1].subj);
1597                                 if (strlen(displayname) > 0) {
1598                                         safestrncpy(WC->summ[nummsgs-1].from, displayname, sizeof WC->summ[nummsgs-1].from);
1599                                 }
1600                                 if (strlen(subject) > 0) {
1601                                 safestrncpy(WC->summ[nummsgs-1].subj, subject,
1602                                         sizeof WC->summ[nummsgs-1].subj);
1603                                 }
1604 #ifdef HAVE_ICONV
1605                                 /* Handle subjects with RFC2047 encoding */
1606                                 utf8ify_rfc822_string(WC->summ[nummsgs-1].subj);
1607 #endif
1608                                 if (strlen(WC->summ[nummsgs-1].subj) > 75) {
1609                                         strcpy(&WC->summ[nummsgs-1].subj[72], "...");
1610                                 }
1611
1612                                 if (strlen(nodename) > 0) {
1613                                         if ( ((WC->room_flags & QR_NETWORK)
1614                                            || ((strcasecmp(nodename, serv_info.serv_nodename)
1615                                            && (strcasecmp(nodename, serv_info.serv_fqdn)))))
1616                                         ) {
1617                                                 strcat(WC->summ[nummsgs-1].from, " @ ");
1618                                                 strcat(WC->summ[nummsgs-1].from, nodename);
1619                                         }
1620                                 }
1621
1622                                 WC->summ[nummsgs-1].date = datestamp;
1623         
1624 #ifdef HAVE_ICONV
1625                                 /* Handle senders with RFC2047 encoding */
1626                                 utf8ify_rfc822_string(WC->summ[nummsgs-1].from);
1627 #endif
1628                                 if (strlen(WC->summ[nummsgs-1].from) > 25) {
1629                                         strcpy(&WC->summ[nummsgs-1].from[22], "...");
1630                                 }
1631                         }
1632                 }
1633         }
1634         return (nummsgs);
1635 }
1636
1637  
1638 int summcmp_subj(const void *s1, const void *s2) {
1639         struct message_summary *summ1;
1640         struct message_summary *summ2;
1641         
1642         summ1 = (struct message_summary *)s1;
1643         summ2 = (struct message_summary *)s2;
1644         return strcasecmp(summ1->subj, summ2->subj);
1645 }
1646
1647 int summcmp_rsubj(const void *s1, const void *s2) {
1648         struct message_summary *summ1;
1649         struct message_summary *summ2;
1650         
1651         summ1 = (struct message_summary *)s1;
1652         summ2 = (struct message_summary *)s2;
1653         return strcasecmp(summ2->subj, summ1->subj);
1654 }
1655
1656 int summcmp_sender(const void *s1, const void *s2) {
1657         struct message_summary *summ1;
1658         struct message_summary *summ2;
1659         
1660         summ1 = (struct message_summary *)s1;
1661         summ2 = (struct message_summary *)s2;
1662         return strcasecmp(summ1->from, summ2->from);
1663 }
1664
1665 int summcmp_rsender(const void *s1, const void *s2) {
1666         struct message_summary *summ1;
1667         struct message_summary *summ2;
1668         
1669         summ1 = (struct message_summary *)s1;
1670         summ2 = (struct message_summary *)s2;
1671         return strcasecmp(summ2->from, summ1->from);
1672 }
1673
1674 int summcmp_date(const void *s1, const void *s2) {
1675         struct message_summary *summ1;
1676         struct message_summary *summ2;
1677         
1678         summ1 = (struct message_summary *)s1;
1679         summ2 = (struct message_summary *)s2;
1680
1681         if (summ1->date < summ2->date) return -1;
1682         else if (summ1->date > summ2->date) return +1;
1683         else return 0;
1684 }
1685
1686 int summcmp_rdate(const void *s1, const void *s2) {
1687         struct message_summary *summ1;
1688         struct message_summary *summ2;
1689         
1690         summ1 = (struct message_summary *)s1;
1691         summ2 = (struct message_summary *)s2;
1692
1693         if (summ1->date < summ2->date) return +1;
1694         else if (summ1->date > summ2->date) return -1;
1695         else return 0;
1696 }
1697
1698 /*
1699  * command loop for reading messages
1700  */
1701 void readloop(char *oper)
1702 {
1703         char cmd[SIZ];
1704         char buf[SIZ];
1705         char old_msgs[SIZ];
1706         int a, b;
1707         int nummsgs;
1708         long startmsg;
1709         int maxmsgs;
1710         int num_displayed = 0;
1711         int is_summary = 0;
1712         int is_addressbook = 0;
1713         int is_singlecard = 0;
1714         int is_calendar = 0;
1715         int is_tasks = 0;
1716         int is_notes = 0;
1717         int remaining_messages;
1718         int lo, hi;
1719         int lowest_displayed = (-1);
1720         int highest_displayed = 0;
1721         long pn_previous = 0L;
1722         long pn_current = 0L;
1723         long pn_next = 0L;
1724         int bg = 0;
1725         struct addrbookent *addrbook = NULL;
1726         int num_ab = 0;
1727         char *sortby = NULL;
1728         char sortpref_name[128];
1729         char sortpref_value[128];
1730         char *subjsort_button;
1731         char *sendsort_button;
1732         char *datesort_button;
1733
1734         startmsg = atol(bstr("startmsg"));
1735         maxmsgs = atoi(bstr("maxmsgs"));
1736         is_summary = atoi(bstr("summary"));
1737         if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
1738
1739         snprintf(sortpref_name, sizeof sortpref_name, "sort %s", WC->wc_roomname);
1740         get_preference(sortpref_name, sortpref_value, sizeof sortpref_value);
1741
1742         sortby = bstr("sortby");
1743         if ( (strlen(sortby) > 0) && (strcasecmp(sortby, sortpref_value)) ) {
1744                 set_preference(sortpref_name, sortby, 1);
1745         }
1746         if (strlen(sortby) == 0) sortby = sortpref_value;
1747         if (strlen(sortby) == 0) sortby = "msgid";
1748
1749         output_headers(1, 1, 1, 0, 0, 0);
1750
1751         /* When in summary mode, always show ALL messages instead of just
1752          * new or old.  Otherwise, show what the user asked for.
1753          */
1754         if (!strcmp(oper, "readnew")) {
1755                 strcpy(cmd, "MSGS NEW");
1756         }
1757         else if (!strcmp(oper, "readold")) {
1758                 strcpy(cmd, "MSGS OLD");
1759         }
1760         else {
1761                 strcpy(cmd, "MSGS ALL");
1762         }
1763
1764         if ((WC->wc_view == VIEW_MAILBOX) && (maxmsgs > 1)) {
1765                 is_summary = 1;
1766                 strcpy(cmd, "MSGS ALL");
1767         }
1768
1769         if ((WC->wc_view == VIEW_ADDRESSBOOK) && (maxmsgs > 1)) {
1770                 is_addressbook = 1;
1771                 strcpy(cmd, "MSGS ALL");
1772                 maxmsgs = 9999999;
1773         }
1774
1775         if (is_summary) {
1776                 strcpy(cmd, "MSGS ALL|||1");    /* fetch header summary */
1777                 startmsg = 1;
1778                 maxmsgs = 9999999;
1779         }
1780
1781         /* Are we doing a summary view?  If so, we need to know old messages
1782          * and new messages, so we can do that pretty boldface thing for the
1783          * new messages.
1784          */
1785         strcpy(old_msgs, "");
1786         if (is_summary) {
1787                 serv_puts("GTSN");
1788                 serv_getln(buf, sizeof buf);
1789                 if (buf[0] == '2') {
1790                         strcpy(old_msgs, &buf[4]);
1791                 }
1792         }
1793
1794         is_singlecard = atoi(bstr("is_singlecard"));
1795
1796         if (WC->wc_view == VIEW_CALENDAR) {             /* calendar */
1797                 is_calendar = 1;
1798                 strcpy(cmd, "MSGS ALL");
1799                 maxmsgs = 32767;
1800         }
1801         if (WC->wc_view == VIEW_TASKS) {                /* tasks */
1802                 is_tasks = 1;
1803                 strcpy(cmd, "MSGS ALL");
1804                 maxmsgs = 32767;
1805         }
1806         if (WC->wc_view == VIEW_NOTES) {                /* notes */
1807                 is_notes = 1;
1808                 strcpy(cmd, "MSGS ALL");
1809                 maxmsgs = 32767;
1810         }
1811
1812         nummsgs = load_msg_ptrs(cmd, is_summary);
1813         if (nummsgs == 0) {
1814
1815                 if ((!is_tasks) && (!is_calendar) && (!is_notes) && (!is_addressbook)) {
1816                         wprintf("<em>");
1817                         if (!strcmp(oper, "readnew")) {
1818                                 wprintf(_("No new messages."));
1819                         } else if (!strcmp(oper, "readold")) {
1820                                 wprintf(_("No old messages."));
1821                         } else {
1822                                 wprintf(_("No messages here."));
1823                         }
1824                         wprintf("</em>\n");
1825                 }
1826
1827                 goto DONE;
1828         }
1829
1830         if (is_summary) {
1831                 for (a = 0; a < nummsgs; ++a) {
1832                         /* Are you a new message, or an old message? */
1833                         if (is_summary) {
1834                                 if (is_msg_in_mset(old_msgs, WC->msgarr[a])) {
1835                                         WC->summ[a].is_new = 0;
1836                                 }
1837                                 else {
1838                                         WC->summ[a].is_new = 1;
1839                                 }
1840                         }
1841                 }
1842         }
1843
1844         if (startmsg == 0L) startmsg = WC->msgarr[0];
1845         remaining_messages = 0;
1846
1847         for (a = 0; a < nummsgs; ++a) {
1848                 if (WC->msgarr[a] >= startmsg) {
1849                         ++remaining_messages;
1850                 }
1851         }
1852
1853         if (is_summary) {
1854                 if (!strcasecmp(sortby, "subject")) {
1855                         qsort(WC->summ, WC->num_summ,
1856                                 sizeof(struct message_summary), summcmp_subj);
1857                 }
1858                 else if (!strcasecmp(sortby, "rsubject")) {
1859                         qsort(WC->summ, WC->num_summ,
1860                                 sizeof(struct message_summary), summcmp_rsubj);
1861                 }
1862                 else if (!strcasecmp(sortby, "sender")) {
1863                         qsort(WC->summ, WC->num_summ,
1864                                 sizeof(struct message_summary), summcmp_sender);
1865                 }
1866                 else if (!strcasecmp(sortby, "rsender")) {
1867                         qsort(WC->summ, WC->num_summ,
1868                                 sizeof(struct message_summary), summcmp_rsender);
1869                 }
1870                 else if (!strcasecmp(sortby, "date")) {
1871                         qsort(WC->summ, WC->num_summ,
1872                                 sizeof(struct message_summary), summcmp_date);
1873                 }
1874                 else if (!strcasecmp(sortby, "rdate")) {
1875                         qsort(WC->summ, WC->num_summ,
1876                                 sizeof(struct message_summary), summcmp_rdate);
1877                 }
1878         }
1879
1880         if (!strcasecmp(sortby, "subject")) {
1881                 subjsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=rsubject\"><img border=\"0\" src=\"/static/down_pointer.gif\" /></a>" ;
1882         }
1883         else if (!strcasecmp(sortby, "rsubject")) {
1884                 subjsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=subject\"><img border=\"0\" src=\"/static/up_pointer.gif\" /></a>" ;
1885         }
1886         else {
1887                 subjsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=subject\"><img border=\"0\" src=\"/static/sort_none.gif\" /></a>" ;
1888         }
1889
1890         if (!strcasecmp(sortby, "sender")) {
1891                 sendsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=rsender\"><img border=\"0\" src=\"/static/down_pointer.gif\" /></a>" ;
1892         }
1893         else if (!strcasecmp(sortby, "rsender")) {
1894                 sendsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=sender\"><img border=\"0\" src=\"/static/up_pointer.gif\" /></a>" ;
1895         }
1896         else {
1897                 sendsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=sender\"><img border=\"0\" src=\"/static/sort_none.gif\" /></a>" ;
1898         }
1899
1900         if (!strcasecmp(sortby, "date")) {
1901                 datesort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=rdate\"><img border=\"0\" src=\"/static/down_pointer.gif\" /></a>" ;
1902         }
1903         else if (!strcasecmp(sortby, "rdate")) {
1904                 datesort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=date\"><img border=\"0\" src=\"/static/up_pointer.gif\" /></a>" ;
1905         }
1906         else {
1907                 datesort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=date\"><img border=\"0\" src=\"/static/sort_none.gif\" /></a>" ;
1908         }
1909
1910         if (is_summary) {
1911                 wprintf("</div>");              /* end of 'content' div */
1912
1913                 wprintf("<div id=\"message_list\">"
1914
1915                         "<div id=\"fix_scrollbar_bug\">\n"
1916
1917                         "<form name=\"msgomatic\" "
1918                         "method=\"POST\" action=\"/do_stuff_to_msgs\">\n"
1919
1920                         "<table border=0 cellspacing=0 "
1921                         "cellpadding=0 width=100%%>\n"
1922                         "<TR>"
1923                         "<TD align=center><b><i>%s</i></b> %s</TD>"
1924                         "<TD align=center><b><i>%s</i></b> %s</TD>"
1925                         "<TD align=center><b><i>%s</i></b> %s</TD>"
1926                         "<TD><INPUT TYPE=\"submit\" NAME=\"delete_button\" "
1927                         "STYLE=\"font-family: Bitstream Vera Sans,Arial,Helvetica,sans-serif;"
1928                         " font-size: 6pt;\" "
1929                         "VALUE=\"%s\"></TD>"
1930                         "</TR>\n"
1931                         ,
1932                         _("Subject"),   subjsort_button,
1933                         _("Sender"),    sendsort_button,
1934                         _("Date"),      datesort_button,
1935                         _("Delete")
1936                 );
1937         }
1938
1939         for (a = 0; a < nummsgs; ++a) {
1940                 if ((WC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
1941
1942                         /* Learn which msgs "Prev" & "Next" buttons go to */
1943                         pn_current = WC->msgarr[a];
1944                         if (a > 0) pn_previous = WC->msgarr[a-1];
1945                         if (a < (nummsgs-1)) pn_next = WC->msgarr[a+1];
1946
1947                         /* If a tabular view, set up the line */
1948                         if (is_summary) {
1949                                 bg = 1 - bg;
1950                                 wprintf("<tr bgcolor=\"#%s\" ",
1951                                         (bg ? "DDDDDD" : "FFFFFF")
1952                                 );
1953
1954                                 wprintf("onClick=\" new Ajax.Updater('preview_pane', '/msg', { method: 'get', parameters: 'msgnum=%ld' } ); \" ", WC->summ[a].msgnum);
1955
1956                                 wprintf(">");
1957                         }
1958
1959                         /* Display the message */
1960                         if (is_summary) {
1961                                 display_summarized(a);
1962                         }
1963                         else if (is_addressbook) {
1964                                 fetch_ab_name(WC->msgarr[a], buf);
1965                                 ++num_ab;
1966                                 addrbook = realloc(addrbook,
1967                                         (sizeof(struct addrbookent) * num_ab) );
1968                                 safestrncpy(addrbook[num_ab-1].ab_name, buf,
1969                                         sizeof(addrbook[num_ab-1].ab_name));
1970                                 addrbook[num_ab-1].ab_msgnum = WC->msgarr[a];
1971                         }
1972                         else if (is_calendar) {
1973                                 display_calendar(WC->msgarr[a]);
1974                         }
1975                         else if (is_tasks) {
1976                                 display_task(WC->msgarr[a]);
1977                         }
1978                         else if (is_notes) {
1979                                 display_note(WC->msgarr[a]);
1980                         }
1981                         else {
1982                                 read_message(WC->msgarr[a], 0);
1983                         }
1984
1985                         /* If a tabular view, finish the line */
1986                         if (is_summary) {
1987                                 wprintf("</TR>\n");
1988                         }
1989
1990                         if (lowest_displayed < 0) lowest_displayed = a;
1991                         highest_displayed = a;
1992
1993                         ++num_displayed;
1994                         --remaining_messages;
1995                 }
1996         }
1997
1998         if (is_summary) {
1999                 wprintf("</table></form>"
2000                         "</div>\n");                    /* end of 'fix_scrollbar_bug' div */
2001                 wprintf("</div>");                      /* end of 'message_list' div */
2002
2003                 wprintf("<div id=\"ml_slider\"></div>");        /* slider */
2004
2005                 wprintf("<div id=\"preview_pane\">");   /* The preview pane will initially be empty */
2006         }
2007
2008         /* Bump these because although we're thinking in zero base, the user
2009          * is a drooling idiot and is thinking in one base.
2010          */
2011         ++lowest_displayed;
2012         ++highest_displayed;
2013
2014         /* If we're only looking at one message, do a prev/next thing */
2015         if (num_displayed == 1) {
2016            if ((!is_tasks) && (!is_calendar) && (!is_addressbook) && (!is_notes) && (!is_singlecard)) {
2017
2018                 wprintf("<div id=\"fix_scrollbar_bug\">"
2019                         "<table border=0 width=100%% bgcolor=\"#dddddd\"><tr><td>");
2020                 wprintf(_("Reading #%d of %d messages."), lowest_displayed, nummsgs);
2021                 wprintf("</TD><TD ALIGN=RIGHT><FONT SIZE=+1>");
2022
2023                 if (pn_previous > 0L) {
2024                         wprintf("<A HREF=\"/%s"
2025                                 "?startmsg=%ld"
2026                                 "?maxmsgs=1"
2027                                 "?summary=0\">"
2028                                 "%s</A> \n",
2029                                         oper,
2030                                         pn_previous,
2031                                         _("Previous"));
2032                 }
2033
2034                 if (pn_next > 0L) {
2035                         wprintf("<A HREF=\"/%s"
2036                                 "?startmsg=%ld"
2037                                 "?maxmsgs=1"
2038                                 "?summary=0\">"
2039                                 "%s</A> \n",
2040                                         oper,
2041                                         pn_next,
2042                                         _("Next"));
2043                 }
2044
2045                 wprintf("<A HREF=\"/%s?startmsg=%ld"
2046                         "?maxmsgs=%d?summary=1\">"
2047                         "%s"
2048                         "</A>",
2049                         oper,
2050                         WC->msgarr[0],
2051                         DEFAULT_MAXMSGS,
2052                         _("Summary")
2053                 );
2054
2055                 wprintf("</td></tr></table></div>\n");
2056             }
2057         }
2058
2059         /*
2060          * If we're not currently looking at ALL requested
2061          * messages, then display the selector bar
2062          */
2063         if (num_displayed > 1) {
2064            if ((!is_tasks) && (!is_calendar) && (!is_addressbook)
2065               && (!is_notes) && (!is_singlecard) && (!is_summary)) {
2066
2067                 wprintf("<form name=\"msgomatic\" "
2068                         "method=\"POST\" action=\"/do_stuff_to_msgs\">\n");
2069
2070                 wprintf(_("Reading #"), lowest_displayed, highest_displayed);
2071
2072                 wprintf("<select name=\"whichones\" size=\"1\" "
2073                         "OnChange=\"location.href=msgomatic.whichones.options"
2074                         "[selectedIndex].value\">\n");
2075
2076                 for (b=0; b<nummsgs; b = b + maxmsgs) {
2077                 lo = b+1;
2078                 hi = b+maxmsgs;
2079                 if (hi > nummsgs) hi = nummsgs;
2080                         wprintf("<option %s value="
2081                                 "\"/%s"
2082                                 "?startmsg=%ld"
2083                                 "?maxmsgs=%d"
2084                                 "?summary=%d\">"
2085                                 "%d-%d</option> \n",
2086                                 ((WC->msgarr[b] == startmsg) ? "selected" : ""),
2087                                 oper,
2088                                 WC->msgarr[b],
2089                                 maxmsgs,
2090                                 is_summary,
2091                                 lo, hi);
2092                 }
2093                 wprintf("<option value=\"/%s?startmsg=%ld"
2094                         "?maxmsgs=9999999?summary=%d\">"
2095                         "ALL"
2096                         "</option> ",
2097                         oper,
2098                         WC->msgarr[0], is_summary);
2099
2100                 wprintf("</select> ");
2101                 wprintf(_("of %d messages."), nummsgs);
2102                 wprintf("</form>\n");
2103             }
2104         }
2105
2106 DONE:
2107         if (is_tasks) {
2108                 do_tasks_view();        /* Render the task list */
2109         }
2110
2111         if (is_calendar) {
2112                 do_calendar_view();     /* Render the calendar */
2113         }
2114
2115         if (is_addressbook) {
2116                 do_addrbook_view(addrbook, num_ab);     /* Render the address book */
2117         }
2118
2119         /* Put the data transfer hidden iframe in a hidden div, to make it *really* hidden */
2120         wprintf("</div>"
2121                 "<div display=\"hidden\">\n"
2122                 "<iframe name=\"msgloader1\" id=\"msgloader1\" width=\"1\"></iframe>\n"
2123         );
2124
2125         /* Note: wDumpContent() will output one additional </div> tag. */
2126         wDumpContent(1);
2127         if (addrbook != NULL) free(addrbook);
2128
2129         /* free the summary */
2130         if (WC->num_summ != 0) {
2131                 WC->num_summ = 0;
2132                 free(WC->summ);
2133         }
2134
2135         /* If we got here via a mailbox view and are reading a single
2136          * message, mark it as "seen." We do this after rendering the web page
2137          * so it doesn't keep the user waiting.
2138          */
2139         if ( (maxmsgs == 1) && (WC->wc_view == VIEW_MAILBOX) ) {
2140                 serv_printf("SEEN %ld|1", startmsg);
2141                 serv_getln(buf, sizeof buf);
2142         }
2143 }
2144
2145
2146 /*
2147  * Back end for post_message() ... this is where the actual message
2148  * gets transmitted to the server.
2149  */
2150 void post_mime_to_server(void) {
2151         char boundary[SIZ];
2152         int is_multipart = 0;
2153         static int seq = 0;
2154         struct wc_attachment *att;
2155         char *encoded;
2156         size_t encoded_length;
2157
2158         /* RFC2045 requires this, and some clients look for it... */
2159         serv_puts("MIME-Version: 1.0");
2160
2161         /* If there are attachments, we have to do multipart/mixed */
2162         if (WC->first_attachment != NULL) {
2163                 is_multipart = 1;
2164         }
2165
2166         if (is_multipart) {
2167                 sprintf(boundary, "---Citadel-Multipart-%s-%04x%04x---",
2168                         serv_info.serv_fqdn,
2169                         getpid(),
2170                         ++seq
2171                 );
2172
2173                 /* Remember, serv_printf() appends an extra newline */
2174                 serv_printf("Content-type: multipart/mixed; "
2175                         "boundary=\"%s\"\n", boundary);
2176                 serv_printf("This is a multipart message in MIME format.\n");
2177                 serv_printf("--%s", boundary);
2178         }
2179
2180         serv_puts("Content-type: text/html; charset=utf-8");
2181         serv_puts("");
2182         serv_puts("<HTML><BODY>\n");
2183         text_to_server(bstr("msgtext"), 0);
2184         serv_puts("</BODY></HTML>\n");
2185         
2186
2187         if (is_multipart) {
2188
2189                 /* Add in the attachments */
2190                 for (att = WC->first_attachment; att!=NULL; att=att->next) {
2191
2192                         encoded_length = ((att->length * 150) / 100);
2193                         encoded = malloc(encoded_length);
2194                         if (encoded == NULL) break;
2195                         CtdlEncodeBase64(encoded, att->data, att->length);
2196
2197                         serv_printf("--%s", boundary);
2198                         serv_printf("Content-type: %s", att->content_type);
2199                         serv_printf("Content-disposition: attachment; "
2200                                 "filename=\"%s\"", att->filename);
2201                         serv_puts("Content-transfer-encoding: base64");
2202                         serv_puts("");
2203                         serv_write(encoded, strlen(encoded));
2204                         serv_puts("");
2205                         serv_puts("");
2206                         free(encoded);
2207                 }
2208                 serv_printf("--%s--", boundary);
2209         }
2210
2211         serv_puts("000");
2212 }
2213
2214
2215 /*
2216  * Post message (or don't post message)
2217  *
2218  * Note regarding the "dont_post" variable:
2219  * A random value (actually, it's just a timestamp) is inserted as a hidden
2220  * field called "postseq" when the display_enter page is generated.  This
2221  * value is checked when posting, using the static variable dont_post.  If a
2222  * user attempts to post twice using the same dont_post value, the message is
2223  * discarded.  This prevents the accidental double-saving of the same message
2224  * if the user happens to click the browser "back" button.
2225  */
2226 void post_message(void)
2227 {
2228         char buf[SIZ];
2229         static long dont_post = (-1L);
2230         struct wc_attachment *att, *aptr;
2231
2232         if (WC->upload_length > 0) {
2233
2234                 /* There's an attachment.  Save it to this struct... */
2235                 att = malloc(sizeof(struct wc_attachment));
2236                 memset(att, 0, sizeof(struct wc_attachment));
2237                 att->length = WC->upload_length;
2238                 strcpy(att->content_type, WC->upload_content_type);
2239                 strcpy(att->filename, WC->upload_filename);
2240                 att->next = NULL;
2241
2242                 /* And add it to the list. */
2243                 if (WC->first_attachment == NULL) {
2244                         WC->first_attachment = att;
2245                 }
2246                 else {
2247                         aptr = WC->first_attachment;
2248                         while (aptr->next != NULL) aptr = aptr->next;
2249                         aptr->next = att;
2250                 }
2251
2252                 /* Mozilla sends a simple filename, which is what we want,
2253                  * but Satan's Browser sends an entire pathname.  Reduce
2254                  * the path to just a filename if we need to.
2255                  */
2256                 while (num_tokens(att->filename, '/') > 1) {
2257                         remove_token(att->filename, 0, '/');
2258                 }
2259                 while (num_tokens(att->filename, '\\') > 1) {
2260                         remove_token(att->filename, 0, '\\');
2261                 }
2262
2263                 /* Transfer control of this memory from the upload struct
2264                  * to the attachment struct.
2265                  */
2266                 att->data = WC->upload;
2267                 WC->upload_length = 0;
2268                 WC->upload = NULL;
2269                 display_enter();
2270                 return;
2271         }
2272
2273         if (strlen(bstr("cancel_button")) > 0) {
2274                 sprintf(WC->ImportantMessage, 
2275                         _("Cancelled.  Message was not posted."));
2276         } else if (strlen(bstr("attach_button")) > 0) {
2277                 display_enter();
2278                 return;
2279         } else if (atol(bstr("postseq")) == dont_post) {
2280                 sprintf(WC->ImportantMessage, 
2281                         _("Automatically cancelled because you have already "
2282                         "saved this message."));
2283         } else {
2284                 sprintf(buf, "ENT0 1|%s|0|4|%s|||%s|%s",
2285                         bstr("recp"),
2286                         bstr("subject"),
2287                         bstr("cc"),
2288                         bstr("bcc")
2289                 );
2290                 serv_puts(buf);
2291                 serv_getln(buf, sizeof buf);
2292                 if (buf[0] == '4') {
2293                         post_mime_to_server();
2294                         if ( (strlen(bstr("recp")) > 0)
2295                            || (strlen(bstr("cc")) > 0)
2296                            || (strlen(bstr("bcc")) > 0)
2297                         ) {
2298                                 sprintf(WC->ImportantMessage, _("Message has been sent.\n"));
2299                         }
2300                         else {
2301                                 sprintf(WC->ImportantMessage, _("Message has been posted.\n"));
2302                         }
2303                         dont_post = atol(bstr("postseq"));
2304                 } else {
2305                         sprintf(WC->ImportantMessage, "%s", &buf[4]);
2306                         display_enter();
2307                         return;
2308                 }
2309         }
2310
2311         free_attachments(WC);
2312         readloop("readnew");
2313 }
2314
2315
2316
2317
2318 /*
2319  * display the message entry screen
2320  */
2321 void display_enter(void)
2322 {
2323         char buf[SIZ];
2324         long now;
2325         struct wc_attachment *att;
2326         int recipient_required = 0;
2327         int recipient_bad = 0;
2328
2329         if (strlen(bstr("force_room")) > 0) {
2330                 gotoroom(bstr("force_room"));
2331         }
2332
2333         /* Are we perhaps in an address book view?  If so, then an "enter
2334          * message" command really means "add new entry."
2335          */
2336         if (WC->wc_view == VIEW_ADDRESSBOOK) {
2337                 do_edit_vcard(-1, "", "");
2338                 return;
2339         }
2340
2341 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
2342         /* Are we perhaps in a calendar view?  If so, then an "enter
2343          * message" command really means "add new calendar item."
2344          */
2345         if (WC->wc_view == VIEW_CALENDAR) {
2346                 display_edit_event();
2347                 return;
2348         }
2349
2350         /* Are we perhaps in a tasks view?  If so, then an "enter
2351          * message" command really means "add new task."
2352          */
2353         if (WC->wc_view == VIEW_TASKS) {
2354                 display_edit_task();
2355                 return;
2356         }
2357 #endif
2358
2359         /*
2360          * Otherwise proceed normally.
2361          * Do a custom room banner with no navbar...
2362          */
2363         output_headers(1, 1, 2, 0, 0, 0);
2364         wprintf("<div id=\"banner\">\n");
2365         embed_room_banner(NULL, navbar_none);
2366         wprintf("</div>\n");
2367         wprintf("<div id=\"content\">\n"
2368                 "<div id=\"fix_scrollbar_bug\">"
2369                 "<table width=100%% border=0 bgcolor=\"#ffffff\"><tr><td>");
2370
2371         /* First test to see whether this is a room that requires recipients to be entered */
2372         serv_puts("ENT0 0");
2373         serv_getln(buf, sizeof buf);
2374         if (!strncmp(buf, "570", 3)) {          /* 570 means that we need a recipient here */
2375                 recipient_required = 1;
2376         }
2377         else if (buf[0] != '2') {               /* Any other error means that we cannot continue */
2378                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
2379                 goto DONE;
2380         }
2381
2382         /* Now check our actual recipients if there are any */
2383         if (recipient_required) {
2384                 sprintf(buf, "ENT0 0|%s|0|0||||%s|%s", bstr("recp"), bstr("cc"), bstr("bcc"));
2385                 serv_puts(buf);
2386                 serv_getln(buf, sizeof buf);
2387
2388                 if (!strncmp(buf, "570", 3)) {  /* 570 means we have an invalid recipient listed */
2389                         if (strlen(bstr("recp")) + strlen(bstr("cc")) + strlen(bstr("bcc")) > 0) {
2390                                 recipient_bad = 1;
2391                         }
2392                 }
2393                 else if (buf[0] != '2') {       /* Any other error means that we cannot continue */
2394                         wprintf("<EM>%s</EM><br />\n", &buf[4]);
2395                         goto DONE;
2396                 }
2397         }
2398
2399         /* If we got this far, we can display the message entry screen. */
2400
2401         now = time(NULL);
2402         fmt_date(buf, now, 0);
2403         strcat(&buf[strlen(buf)], _(" <I>from</I> "));
2404         stresc(&buf[strlen(buf)], WC->wc_username, 1, 1);
2405
2406         /* Don't need this anymore, it's in the input box below
2407         if (strlen(bstr("recp")) > 0) {
2408                 strcat(&buf[strlen(buf)], _(" <I>to</I> "));
2409                 stresc(&buf[strlen(buf)], bstr("recp"), 1, 1);
2410         }
2411         */
2412
2413         strcat(&buf[strlen(buf)], _(" <I>in</I> "));
2414         stresc(&buf[strlen(buf)], WC->wc_roomname, 1, 1);
2415
2416         /* begin message entry screen */
2417         wprintf("<form enctype=\"multipart/form-data\" "
2418                 "method=\"POST\" action=\"/post\" "
2419                 "name=\"enterform\""
2420                 ">\n");
2421         wprintf("<input type=\"hidden\" name=\"postseq\" value=\"%ld\">\n", now);
2422
2423         wprintf("<img src=\"static/newmess3_24x.gif\" align=middle alt=\" \">");
2424         wprintf("%s<br>\n", buf);       /* header bar */
2425
2426         wprintf("<table border=\"0\" width=\"100%%\">\n");
2427         if (recipient_required) {
2428
2429                 wprintf("<tr><td>");
2430                 wprintf("<font size=-1>");
2431                 wprintf(_("To:"));
2432                 wprintf("</font>");
2433                 wprintf("</td><td>"
2434                         "<input autocomplete=\"off\" type=\"text\" name=\"recp\" id=\"recp_id\" value=\"");
2435                 escputs(bstr("recp"));
2436                 wprintf("\" size=50 maxlength=1000 />");
2437                 wprintf("<div class=\"auto_complete\" id=\"recp_name_choices\"></div>");
2438                 wprintf("</td><td></td></tr>\n");
2439
2440                 wprintf("<tr><td>");
2441                 wprintf("<font size=-1>");
2442                 wprintf(_("CC:"));
2443                 wprintf("</font>");
2444                 wprintf("</td><td>"
2445                         "<input autocomplete=\"off\" type=\"text\" name=\"cc\" id=\"cc_id\" value=\"");
2446                 escputs(bstr("cc"));
2447                 wprintf("\" size=50 maxlength=1000 />");
2448                 wprintf("<div class=\"auto_complete\" id=\"cc_name_choices\"></div>");
2449                 wprintf("</td><td></td></tr>\n");
2450
2451                 wprintf("<tr><td>");
2452                 wprintf("<font size=-1>");
2453                 wprintf(_("BCC:"));
2454                 wprintf("</font>");
2455                 wprintf("</td><td>"
2456                         "<input autocomplete=\"off\" type=\"text\" name=\"bcc\" id=\"bcc_id\" value=\"");
2457                 escputs(bstr("bcc"));
2458                 wprintf("\" size=50 maxlength=1000 />");
2459                 wprintf("<div class=\"auto_complete\" id=\"bcc_name_choices\"></div>");
2460                 wprintf("</td><td></td></tr>\n");
2461
2462                 /* Initialize the autocomplete ajax helpers (found in wclib.js) */
2463                 wprintf("<script type=\"text/javascript\">      \n"
2464                         " activate_entmsg_autocompleters();     \n"
2465                         "</script>                              \n"
2466                 );
2467         }
2468
2469         wprintf("<tr><td>");
2470         wprintf("<font size=-1>");
2471         wprintf(_("Subject (optional):"));
2472         wprintf("</font>");
2473         wprintf("</td><td>"
2474                 "<input type=\"text\" name=\"subject\" value=\"");
2475         escputs(bstr("subject"));
2476         wprintf("\" size=50 maxlength=70></td><td>\n");
2477
2478         wprintf("<input type=\"submit\" name=\"send_button\" value=\"");
2479         if (recipient_required) {
2480                 wprintf(_("Send message"));
2481         } else {
2482                 wprintf(_("Post message"));
2483         }
2484         wprintf("\">&nbsp;"
2485                 "<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
2486         wprintf("</td></tr></table>\n");
2487
2488         wprintf("<center>");
2489
2490         wprintf("<textarea name=\"msgtext\" cols=\"80\" rows=\"15\">");
2491
2492         msgescputs(bstr("msgtext"));
2493         if (atol(bstr("pullquote")) > 0L) {
2494                 wprintf("<br><div align=center><i>");
2495                 wprintf(_("--- forwarded message ---"));
2496                 wprintf("</i></div><br>");
2497                 pullquote_message(atol(bstr("pullquote")), 1);
2498         }
2499         wprintf("</textarea>");
2500         wprintf("</center><br />\n");
2501
2502         /*
2503          * The following script embeds the TinyMCE richedit control, and automatically
2504          * transforms the textarea into a richedit textarea.
2505          */
2506         wprintf(
2507                 "<script language=\"javascript\" type=\"text/javascript\" src=\"tiny_mce/tiny_mce.js\"></script>\n"
2508                 "<script language=\"javascript\" type=\"text/javascript\">"
2509                 "tinyMCE.init({"
2510                 "       mode : \"textareas\", width : \"100%%\", browsers : \"msie,gecko\" "
2511                 "});"
2512                 "</script>\n"
2513         );
2514
2515         /* Enumerate any attachments which are already in place... */
2516         wprintf("<img src=\"/static/diskette_24x.gif\" border=0 "
2517                 "align=middle height=16 width=16> Attachments: ");
2518         wprintf("<select name=\"which_attachment\" size=1>");
2519         for (att = WC->first_attachment; att != NULL; att = att->next) {
2520                 wprintf("<option value=\"");
2521                 urlescputs(att->filename);
2522                 wprintf("\">");
2523                 escputs(att->filename);
2524                 /* wprintf(" (%s, %d bytes)",att->content_type,att->length); */
2525                 wprintf("</option>\n");
2526         }
2527         wprintf("</select>");
2528
2529         /* Now offer the ability to attach additional files... */
2530         wprintf("&nbsp;&nbsp;&nbsp;"
2531                 "Attach file: <input NAME=\"attachfile\" "
2532                 "SIZE=16 TYPE=\"file\">\n&nbsp;&nbsp;"
2533                 "<input type=\"submit\" name=\"attach_button\" value=\"%s\">\n", _("Add"));
2534
2535         /* Seth asked for these to be at the top *and* bottom... */
2536         wprintf("<input type=\"submit\" name=\"send_button\" value=\"");
2537         if (recipient_required) {
2538                 wprintf(_("Send message"));
2539         } else {
2540                 wprintf(_("Post message"));
2541         }
2542         wprintf("\">&nbsp;"
2543                 "<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
2544
2545         wprintf("</form>\n");
2546
2547         wprintf("</td></tr></table></div>\n");
2548 DONE:   wDumpContent(1);
2549 }
2550
2551
2552
2553
2554
2555
2556
2557
2558 void delete_msg(void)
2559 {
2560         long msgid;
2561         char buf[SIZ];
2562
2563         msgid = atol(bstr("msgid"));
2564
2565         output_headers(1, 1, 1, 0, 0, 0);
2566
2567         sprintf(buf, "DELE %ld", msgid);
2568         serv_puts(buf);
2569         serv_getln(buf, sizeof buf);
2570         wprintf("<EM>%s</EM><br />\n", &buf[4]);
2571
2572         wDumpContent(1);
2573 }
2574
2575
2576
2577
2578 /*
2579  * Confirm move of a message
2580  */
2581 void confirm_move_msg(void)
2582 {
2583         long msgid;
2584         char buf[SIZ];
2585         char targ[SIZ];
2586
2587         msgid = atol(bstr("msgid"));
2588
2589         output_headers(1, 1, 1, 0, 0, 0);
2590
2591         wprintf("<div id=\"fix_scrollbar_bug\">"
2592                 "<table width=100%% border=0 bgcolor=\"#444455\"><tr><td>");
2593         wprintf("<font size=+1 color=\"#ffffff\"");
2594         wprintf("<b>");
2595         wprintf(_("Confirm move of message"));
2596         wprintf("</b>\n");
2597         wprintf("</font></td></tr></table></div>\n");
2598
2599         wprintf("<CENTER>");
2600
2601         wprintf(_("Move this message to:"));
2602         wprintf("<br />\n");
2603
2604         wprintf("<form METHOD=\"POST\" ACTION=\"/move_msg\">\n");
2605         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
2606                 bstr("msgid"));
2607
2608
2609         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
2610         serv_puts("LKRA");
2611         serv_getln(buf, sizeof buf);
2612         if (buf[0] == '1') {
2613                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
2614                         extract_token(targ, buf, 0, '|', sizeof targ);
2615                         wprintf("<OPTION>");
2616                         escputs(targ);
2617                         wprintf("\n");
2618                 }
2619         }
2620         wprintf("</SELECT>\n");
2621         wprintf("<br />\n");
2622
2623         wprintf("<INPUT TYPE=\"submit\" NAME=\"move_button\" VALUE=\"%s\">", _("Move"));
2624         wprintf("&nbsp;");
2625         wprintf("<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">", _("Cancel"));
2626         wprintf("</form></CENTER>\n");
2627
2628         wprintf("</CENTER>\n");
2629         wDumpContent(1);
2630 }
2631
2632
2633
2634 void move_msg(void)
2635 {
2636         long msgid;
2637         char buf[SIZ];
2638
2639         msgid = atol(bstr("msgid"));
2640
2641         output_headers(1, 1, 1, 0, 0, 0);
2642
2643         if (strlen(bstr("move_button")) > 0) {
2644                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
2645                 serv_puts(buf);
2646                 serv_getln(buf, sizeof buf);
2647                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
2648         } else {
2649                 wprintf("<EM>");
2650                 wprintf(_("The message was not moved."));
2651                 wprintf("</EM><br />\n");
2652         }
2653
2654         wDumpContent(1);
2655 }
2656
2657 /*
2658  * This gets called when a user selects multiple messages in a summary
2659  * list and then clicks to perform a transformation of some sort on them
2660  * (such as deleting them).
2661  */
2662 void do_stuff_to_msgs(void) {
2663         char buf[256];
2664
2665         struct stuff_t {
2666                 struct stuff_t *next;
2667                 long msgnum;
2668         };
2669
2670         struct stuff_t *stuff = NULL;
2671         struct stuff_t *ptr;
2672         int delete_button_pressed = 0;
2673
2674
2675         serv_puts("MSGS ALL");
2676         serv_getln(buf, sizeof buf);
2677
2678         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
2679                 ptr = malloc(sizeof(struct stuff_t));
2680                 ptr->msgnum = atol(buf);
2681                 ptr->next = stuff;
2682                 stuff = ptr;
2683         }
2684
2685         if (strlen(bstr("delete_button")) > 0) {
2686                 delete_button_pressed = 1;
2687         }
2688
2689         while (stuff != NULL) {
2690
2691                 sprintf(buf, "msg_%ld", stuff->msgnum);
2692                 if (!strcasecmp(bstr(buf), "yes")) {
2693
2694                         if (delete_button_pressed) {
2695                                 serv_printf("DELE %ld", stuff->msgnum);
2696                                 serv_getln(buf, sizeof buf);
2697                         }
2698
2699                 }
2700
2701                 ptr = stuff->next;
2702                 free(stuff);
2703                 stuff = ptr;
2704         }
2705
2706         readloop("readfwd");
2707 }