4 * Functions which deal with the fetching and displaying of messages.
14 #include <sys/types.h>
16 #include <sys/socket.h>
18 #include <netinet/in.h>
28 #include "webserver.h"
31 /* Address book entry (keep it short and sweet, it's just a quickie lookup
32 * which we can use to get to the real meat and bones later)
41 * Look for URL's embedded in a buffer and make them linkable. We use a
42 * target window in order to keep the BBS session in its own window.
58 for (pos = 0; pos < strlen(buf); ++pos) {
59 if (!strncasecmp(&buf[pos], "http://", 7))
61 if (!strncasecmp(&buf[pos], "ftp://", 6))
68 if ((start > 0) && (buf[start - 1] == '<'))
70 if ((start > 0) && (buf[start - 1] == '['))
72 if ((start > 0) && (buf[start - 1] == '('))
74 if ((start > 0) && (buf[start - 1] == '{'))
77 for (pos = strlen(buf); pos > start; --pos) {
78 if ((buf[pos] == ' ') || (buf[pos] == ench))
82 strncpy(urlbuf, &buf[start], end - start);
83 urlbuf[end - start] = 0;
85 strncpy(outbuf, buf, start);
86 sprintf(&outbuf[start], "%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
87 LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
88 strcat(outbuf, &buf[end]);
89 if ( strlen(outbuf) < 250 )
94 /* display_vcard() calls this after parsing the textual vCard into
95 * our 'struct vCard' data object.
96 * This gets called instead of display_parsed_vcard() if we are only looking
97 * to extract the person's name instead of displaying the card.
99 void fetchname_parsed_vcard(struct vCard *v, char *storename) {
102 strcpy(storename, "");
103 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
104 if (!strcasecmp(v->prop[i].name, "n")) {
105 strcpy(storename, v->prop[i].value);
112 /* display_vcard() calls this after parsing the textual vCard into
113 * our 'struct vCard' data object.
115 * Set 'full' to nonzero to display the full card, otherwise it will only
116 * show a summary line.
118 * This code is a bit ugly, so perhaps an explanation is due: we do this
119 * in two passes through the vCard fields. On the first pass, we process
120 * fields we understand, and then render them in a pretty fashion at the
121 * end. Then we make a second pass, outputting all the fields we don't
122 * understand in a simple two-column name/value format.
124 void display_parsed_vcard(struct vCard *v, int full) {
130 char *thisname, *thisvalue;
131 char firsttoken[SIZ];
134 char displayname[SIZ];
138 strcpy(displayname, "");
144 name = vcard_get_prop(v, "fn", 1, 0, 0);
145 if (name == NULL) name = vcard_get_prop(v, "n", 1, 0, 0);
157 wprintf("<TABLE bgcolor=#888888>");
158 for (pass=1; pass<=2; ++pass) {
160 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
162 thisname = strdup(v->prop[i].name);
163 extract_token(firsttoken, thisname, 0, ';');
165 for (j=0; j<num_tokens(thisname, ';'); ++j) {
166 extract_token(buf, thisname, j, ';');
167 if (!strcasecmp(buf, "encoding=quoted-printable")) {
169 remove_token(thisname, j, ';');
171 if (!strcasecmp(buf, "encoding=base64")) {
173 remove_token(thisname, j, ';');
178 thisvalue = malloc(strlen(v->prop[i].value) + 50);
179 j = CtdlDecodeQuotedPrintable(
180 thisvalue, v->prop[i].value,
181 strlen(v->prop[i].value) );
185 thisvalue = malloc(strlen(v->prop[i].value) + 50);
187 thisvalue, v->prop[i].value,
188 strlen(v->prop[i].value) );
191 thisvalue = strdup(v->prop[i].value);
194 /*** Various fields we may encounter ***/
196 /* N is name, but only if there's no FN already there */
197 if (!strcasecmp(firsttoken, "n")) {
198 if (strlen(displayname) == 0) {
199 strcpy(displayname, thisvalue);
203 /* FN (full name) is a true 'display name' field */
204 else if (!strcasecmp(firsttoken, "fn")) {
205 strcpy(displayname, thisvalue);
208 else if (!strcasecmp(firsttoken, "email")) {
209 if (strlen(mailto) > 0) strcat(mailto, "<BR>");
211 "<A HREF=\"/display_enter"
212 "?force_room=_MAIL_&recp=");
213 urlesc(&mailto[strlen(mailto)], thisvalue);
214 strcat(mailto, "\">");
215 urlesc(&mailto[strlen(mailto)], thisvalue);
216 strcat(mailto, "</A>");
218 else if (!strcasecmp(firsttoken, "tel")) {
219 if (strlen(phone) > 0) strcat(phone, "<BR>");
220 strcat(phone, thisvalue);
221 for (j=0; j<num_tokens(thisname, ';'); ++j) {
222 extract_token(buf, thisname, j, ';');
223 if (!strcasecmp(buf, "tel"))
225 else if (!strcasecmp(buf, "work"))
226 strcat(phone, " (work)");
227 else if (!strcasecmp(buf, "home"))
228 strcat(phone, " (home)");
229 else if (!strcasecmp(buf, "cell"))
230 strcat(phone, " (cell)");
238 else if (!strcasecmp(firsttoken, "adr")) {
240 wprintf("<TR><TD>Address:</TD><TD>");
241 for (j=0; j<num_tokens(thisvalue, ';'); ++j) {
242 extract_token(buf, thisvalue, j, ';');
243 if (strlen(buf) > 0) {
248 wprintf("</TD></TR>\n");
251 else if (!strcasecmp(firsttoken, "version")) {
254 else if (!strcasecmp(firsttoken, "rev")) {
257 else if (!strcasecmp(firsttoken, "label")) {
264 wprintf("</TD><TD>");
266 wprintf("</TD></TR>\n");
275 wprintf("<TR BGCOLOR=\"#AAAAAA\">"
276 "<TD COLSPAN=2 BGCOLOR=\"#FFFFFF\">"
277 "<IMG ALIGN=CENTER SRC=\"/static/vcard.gif\">"
278 "<FONT SIZE=+1><B>");
279 escputs(displayname);
280 wprintf("</B></FONT></TD></TR>\n");
282 if (strlen(phone) > 0)
283 wprintf("<TR><TD>Telephone:</TD><TD>%s</TD></TR>\n", phone);
284 if (strlen(mailto) > 0)
285 wprintf("<TR><TD>E-mail:</TD><TD>%s</TD></TR>\n", mailto);
290 wprintf("</TABLE>\n");
296 * Display a textual vCard
297 * (Converts to a vCard object and then calls the actual display function)
298 * Set 'full' to nonzero to display the whole card instead of a one-liner.
299 * Or, if "storename" is non-NULL, just store the person's name in that
300 * buffer instead of displaying the card at all.
302 void display_vcard(char *vcard_source, char alpha, int full, char *storename) {
308 v = vcard_load(vcard_source);
309 if (v == NULL) return;
311 name = vcard_get_prop(v, "n", 1, 0, 0);
317 if (storename != NULL) {
318 fetchname_parsed_vcard(v, storename);
320 else if ( (alpha == 0)
321 || ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha)) )
322 || ((!isalpha(alpha)) && (!isalpha(this_alpha))) ) {
323 display_parsed_vcard(v, full);
333 * I wanna SEE that message!
335 void read_message(long msgnum) {
337 char mime_partnum[SIZ];
338 char mime_filename[SIZ];
339 char mime_content_type[SIZ];
340 char mime_disposition[SIZ];
352 char vcard_partnum[SIZ];
353 char cal_partnum[SIZ];
354 char *part_source = NULL;
359 strcpy(reply_to, "");
360 strcpy(vcard_partnum, "");
361 strcpy(cal_partnum, "");
362 strcpy(mime_http, "");
364 serv_printf("MSG4 %ld", msgnum);
367 wprintf("<STRONG>ERROR:</STRONG> %s<BR>\n", &buf[4]);
371 /* begin everythingamundo table */
372 wprintf("<table width=100% border=1 cellspacing=0 "
373 "cellpadding=0><TR><TD>\n");
375 /* begin message header table */
376 wprintf("<TABLE WIDTH=100%% BORDER=0 CELLSPACING=0 "
377 "CELLPADDING=1 BGCOLOR=\"#CCCCCC\"><TR><TD>\n");
379 wprintf("<SPAN CLASS=\"message_header\">");
380 strcpy(m_subject, "");
382 while (serv_gets(buf), strcasecmp(buf, "text")) {
383 if (!strcmp(buf, "000")) {
384 wprintf("<I>unexpected end of message</I><BR><BR>\n");
385 wprintf("</SPAN>\n");
388 if (!strncasecmp(buf, "nhdr=yes", 8))
392 if (!strncasecmp(buf, "type=", 5))
393 format_type = atoi(&buf[5]);
394 if (!strncasecmp(buf, "from=", 5)) {
395 strcpy(from, &buf[5]);
396 wprintf("from <A HREF=\"/showuser&who=");
402 if (!strncasecmp(buf, "subj=", 5))
403 strcpy(m_subject, &buf[5]);
404 if ((!strncasecmp(buf, "hnod=", 5))
405 && (strcasecmp(&buf[5], serv_info.serv_humannode)))
406 wprintf("(%s) ", &buf[5]);
407 if ((!strncasecmp(buf, "room=", 5))
408 && (strcasecmp(&buf[5], WC->wc_roomname))
409 && (strlen(&buf[5])>0) )
410 wprintf("in %s> ", &buf[5]);
411 if (!strncasecmp(buf, "rfca=", 5)) {
412 strcpy(rfca, &buf[5]);
418 if (!strncasecmp(buf, "node=", 5)) {
419 strcpy(node, &buf[5]);
420 if ( ((WC->room_flags & QR_NETWORK)
421 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
422 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
425 wprintf("@%s ", &buf[5]);
428 if (!strncasecmp(buf, "rcpt=", 5))
429 wprintf("to %s ", &buf[5]);
430 if (!strncasecmp(buf, "time=", 5)) {
431 fmt_date(now, atol(&buf[5]));
435 if (!strncasecmp(buf, "part=", 5)) {
436 extract(mime_filename, &buf[5], 1);
437 extract(mime_partnum, &buf[5], 2);
438 extract(mime_disposition, &buf[5], 3);
439 extract(mime_content_type, &buf[5], 4);
440 mime_length = extract_int(&buf[5], 5);
442 if (!strcasecmp(mime_disposition, "attachment")) {
443 snprintf(&mime_http[strlen(mime_http)],
444 (sizeof(mime_http) - strlen(mime_http) - 1),
445 "<A HREF=\"/output_mimepart?"
446 "msgnum=%ld&partnum=%s\" "
447 "TARGET=\"wc.%ld.%s\">"
448 "<IMG SRC=\"/static/attachment.gif\" "
449 "BORDER=0 ALIGN=MIDDLE>\n"
450 "Part %s: %s (%s, %d bytes)</A><BR>\n",
451 msgnum, mime_partnum,
452 msgnum, mime_partnum,
453 mime_partnum, mime_filename,
454 mime_content_type, mime_length);
457 if ((!strcasecmp(mime_disposition, "inline"))
458 && (!strncasecmp(mime_content_type, "image/", 6)) ){
459 snprintf(&mime_http[strlen(mime_http)],
460 (sizeof(mime_http) - strlen(mime_http) - 1),
461 "<IMG SRC=\"/output_mimepart?"
462 "msgnum=%ld&partnum=%s\">",
463 msgnum, mime_partnum);
466 /*** begin handler prep ***/
467 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
468 strcpy(vcard_partnum, mime_partnum);
471 if (!strcasecmp(mime_content_type, "text/calendar")) {
472 strcpy(cal_partnum, mime_partnum);
475 /*** end handler prep ***/
481 /* Generate a reply-to address */
482 if (strlen(rfca) > 0) {
483 strcpy(reply_to, rfca);
486 if ( (strlen(node) > 0)
487 && (strcasecmp(node, serv_info.serv_nodename))
488 && (strcasecmp(node, serv_info.serv_humannode)) ) {
489 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
493 snprintf(reply_to, sizeof(reply_to), "%s", from);
502 if (strlen(m_subject) > 0) {
504 "<SPAN CLASS=\"message_subject\">"
511 /* start msg buttons */
512 wprintf("<TD ALIGN=RIGHT>\n");
513 wprintf("<FORM METHOD=\"POST\" ACTION=\"/do_stuff_to_one_msg\">\n");
514 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%ld\">\n",
516 wprintf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"");
517 urlescputs(reply_to);
520 if (!strncasecmp(m_subject, "Re:", 2)) {
521 wprintf("<INPUT TYPE=\"hidden\" NAME=\"subject\" VALUE=\"");
525 else if (strlen(m_subject) > 0) {
526 wprintf("<INPUT TYPE=\"hidden\" NAME=\"subject\" VALUE=\"Re: ");
531 wprintf("<INPUT TYPE=\"submit\" NAME=\"msg_oper\" STYLE=\"font-family: Bitstream Vera Sans,Arial,Helvetica,sans-serif; font-size: 7pt; background: blue; color: #FFFFFF;\" VALUE=\"Reply\">\n");
533 if (WC->is_room_aide) {
534 wprintf("<INPUT TYPE=\"submit\" NAME=\"msg_oper\" STYLE=\"font-family: Bitstream Vera Sans,Arial,Helvetica,sans-serif; font-size: 7pt; background: blue; color: #FFFFFF;\"VALUE=\"Move\">\n"
535 "<INPUT TYPE=\"submit\" NAME=\"msg_oper\" STYLE=\"font-family: Bitstream Vera Sans,Arial,Helvetica,sans-serif; font-size: 7pt; background: blue; color: #FFFFFF;\" VALUE=\"Delete\""
536 "onClick=\"return confirm('Delete this message?');\">\n");
540 "</TD></TR></TABLE>\n");
543 wprintf("<TABLE BORDER=0 WIDTH=100%% BGCOLOR=#FFFFFF "
544 "CELLPADDING=1 CELLSPACING=0><TR><TD>");
547 * Learn the content type
549 strcpy(mime_content_type, "text/plain");
550 while (serv_gets(buf), (strlen(buf) > 0)) {
551 if (!strcmp(buf, "000")) {
552 wprintf("<I>unexpected end of message</I><BR><BR>\n");
555 if (!strncasecmp(buf, "Content-type: ", 14)) {
556 safestrncpy(mime_content_type, &buf[14],
557 sizeof(mime_content_type));
561 /* Messages in legacy Citadel variformat get handled thusly... */
562 if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
563 fmout(NULL, "JUSTIFY");
566 /* Boring old 80-column fixed format text gets handled this way... */
567 else if (!strcasecmp(mime_content_type, "text/plain")) {
568 while (serv_gets(buf), strcmp(buf, "000")) {
569 if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
570 if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
571 while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
572 buf[strlen(buf) - 1] = 0;
574 ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
575 wprintf("<BLOCKQUOTE>");
577 } else if ((bq == 1) &&
578 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
579 wprintf("</BLOCKQUOTE>");
585 wprintf("</TT><BR>\n");
590 else /* HTML is fun, but we've got to strip it first */
591 if (!strcasecmp(mime_content_type, "text/html")) {
595 /* Unknown weirdness */
597 wprintf("I don't know how to display %s<BR>\n",
599 while (serv_gets(buf), strcmp(buf, "000")) { }
603 /* Afterwards, offer links to download attachments 'n' such */
604 if (strlen(mime_http) > 0) {
605 wprintf("%s", mime_http);
608 /* Handler for vCard parts */
609 if (strlen(vcard_partnum) > 0) {
610 part_source = load_mimepart(msgnum, vcard_partnum);
611 if (part_source != NULL) {
613 /* If it's my vCard I can edit it */
614 if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
615 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
616 wprintf("<A HREF=\"/edit_vcard?"
617 "msgnum=%ld&partnum=%s\">",
618 msgnum, vcard_partnum);
619 wprintf("(edit)</A>");
622 /* In all cases, display the full card */
623 display_vcard(part_source, 0, 1, NULL);
627 /* Handler for calendar parts */
628 if (strlen(cal_partnum) > 0) {
629 part_source = load_mimepart(msgnum, cal_partnum);
630 if (part_source != NULL) {
631 cal_process_attachment(part_source,
632 msgnum, cal_partnum);
642 wprintf("</TD></TR></TABLE>\n");
644 /* end everythingamundo table */
645 wprintf("</TD></TR></TABLE><BR>\n");
649 void summarize_message(long msgnum, int is_new) {
660 memset(&summ, 0, sizeof(summ));
661 strcpy(summ.subj, "(no subject)");
663 sprintf(buf, "MSG0 %ld|3", msgnum); /* ask for headers only with no MIME */
666 if (buf[0] != '1') return;
668 while (serv_gets(buf), strcmp(buf, "000")) {
669 if (!strncasecmp(buf, "from=", 5)) {
670 strcpy(summ.from, &buf[5]);
672 if (!strncasecmp(buf, "subj=", 5)) {
673 strcpy(summ.subj, &buf[5]);
675 if (!strncasecmp(buf, "rfca=", 5)) {
676 strcat(summ.from, " <");
677 strcat(summ.from, &buf[5]);
678 strcat(summ.from, ">");
681 if (!strncasecmp(buf, "node=", 5)) {
682 if ( ((WC->room_flags & QR_NETWORK)
683 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
684 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
686 strcat(summ.from, " @ ");
687 strcat(summ.from, &buf[5]);
691 if (!strncasecmp(buf, "rcpt=", 5)) {
692 strcpy(summ.to, &buf[5]);
695 if (!strncasecmp(buf, "time=", 5)) {
696 fmt_date(summ.date, atol(&buf[5]));
701 if (is_new) wprintf("<B>");
702 wprintf("<A HREF=\"/readfwd?startmsg=%ld"
703 "&maxmsgs=1&summary=0\">",
707 if (is_new) wprintf("</B>");
708 wprintf("</TD><TD>");
709 if (is_new) wprintf("<B>");
711 if (is_new) wprintf("</B>");
712 wprintf(" </TD><TD>");
713 if (is_new) wprintf("<B>");
715 if (is_new) wprintf("</B>");
718 "<INPUT TYPE=\"checkbox\" NAME=\"msg_%ld\" VALUE=\"yes\">"
726 void display_addressbook(long msgnum, char alpha) {
728 char mime_partnum[SIZ];
729 char mime_filename[SIZ];
730 char mime_content_type[SIZ];
731 char mime_disposition[SIZ];
733 char vcard_partnum[SIZ];
734 char *vcard_source = NULL;
744 memset(&summ, 0, sizeof(summ));
745 strcpy(summ.subj, "(no subject)");
747 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
750 if (buf[0] != '1') return;
752 while (serv_gets(buf), strcmp(buf, "000")) {
753 if (!strncasecmp(buf, "part=", 5)) {
754 extract(mime_filename, &buf[5], 1);
755 extract(mime_partnum, &buf[5], 2);
756 extract(mime_disposition, &buf[5], 3);
757 extract(mime_content_type, &buf[5], 4);
758 mime_length = extract_int(&buf[5], 5);
760 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
761 strcpy(vcard_partnum, mime_partnum);
767 if (strlen(vcard_partnum) > 0) {
768 vcard_source = load_mimepart(msgnum, vcard_partnum);
769 if (vcard_source != NULL) {
771 /* Display the summary line */
772 display_vcard(vcard_source, alpha, 0, NULL);
774 /* If it's my vCard I can edit it */
775 if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
776 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
777 wprintf("<A HREF=\"/edit_vcard?"
778 "msgnum=%ld&partnum=%s\">",
779 msgnum, vcard_partnum);
780 wprintf("(edit)</A>");
791 /* If it's an old "Firstname Lastname" style record, try to
794 void lastfirst_firstlast(char *namebuf) {
799 if (namebuf == NULL) return;
800 if (strchr(namebuf, ';') != NULL) return;
802 i = num_tokens(namebuf, ' ');
805 extract_token(lastname, namebuf, i-1, ' ');
806 remove_token(namebuf, i-1, ' ');
807 strcpy(firstname, namebuf);
808 sprintf(namebuf, "%s; %s", lastname, firstname);
812 void fetch_ab_name(long msgnum, char *namebuf) {
814 char mime_partnum[SIZ];
815 char mime_filename[SIZ];
816 char mime_content_type[SIZ];
817 char mime_disposition[SIZ];
819 char vcard_partnum[SIZ];
820 char *vcard_source = NULL;
831 if (namebuf == NULL) return;
834 memset(&summ, 0, sizeof(summ));
835 strcpy(summ.subj, "(no subject)");
837 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
840 if (buf[0] != '1') return;
842 while (serv_gets(buf), strcmp(buf, "000")) {
843 if (!strncasecmp(buf, "part=", 5)) {
844 extract(mime_filename, &buf[5], 1);
845 extract(mime_partnum, &buf[5], 2);
846 extract(mime_disposition, &buf[5], 3);
847 extract(mime_content_type, &buf[5], 4);
848 mime_length = extract_int(&buf[5], 5);
850 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
851 strcpy(vcard_partnum, mime_partnum);
857 if (strlen(vcard_partnum) > 0) {
858 vcard_source = load_mimepart(msgnum, vcard_partnum);
859 if (vcard_source != NULL) {
861 /* Grab the name off the card */
862 display_vcard(vcard_source, 0, 0, namebuf);
868 lastfirst_firstlast(namebuf);
870 for (i=0; i<strlen(namebuf); ++i) {
871 if (namebuf[i] != ';') return;
873 strcpy(namebuf, "(no name)");
879 * Record compare function for sorting address book indices
881 int abcmp(const void *ab1, const void *ab2) {
883 (((const struct addrbookent *)ab1)->ab_name),
884 (((const struct addrbookent *)ab2)->ab_name)
890 * Helper function for do_addrbook_view()
891 * Converts a name into a three-letter tab label
893 void nametab(char *tabbuf, char *name) {
894 stresc(tabbuf, name, 0, 0);
895 tabbuf[0] = toupper(tabbuf[0]);
896 tabbuf[1] = tolower(tabbuf[1]);
897 tabbuf[2] = tolower(tabbuf[2]);
903 * Render the address book using info we gathered during the scan
905 void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
909 static int NAMESPERPAGE = 60;
913 char tabfirst_label[SIZ];
915 char tablast_label[SIZ];
918 wprintf("<I>This address book is empty.</I>\n");
923 qsort(addrbook, num_ab, sizeof(struct addrbookent), abcmp);
926 num_pages = num_ab / NAMESPERPAGE;
928 page = atoi(bstr("page"));
931 for (i=0; i<=num_pages; ++i) {
933 wprintf("<A HREF=\"/readfwd?page=%d\">", i);
938 tabfirst = i * NAMESPERPAGE;
939 tablast = tabfirst + NAMESPERPAGE - 1;
940 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
941 nametab(tabfirst_label, addrbook[tabfirst].ab_name);
942 nametab(tablast_label, addrbook[tablast].ab_name);
943 wprintf("[%s - %s]",
944 tabfirst_label, tablast_label
955 wprintf("<TABLE border=0 cellspacing=0 "
956 "cellpadding=3 width=100%%>\n"
959 for (i=0; i<num_ab; ++i) {
961 if ((i / NAMESPERPAGE) == page) {
963 if ((displayed % 4) == 0) {
968 wprintf("<TR BGCOLOR=\"#%s\">",
969 (bg ? "DDDDDD" : "FFFFFF")
975 wprintf("<A HREF=\"/readfwd?startmsg=%ld&is_singlecard=1",
976 addrbook[i].ab_msgnum);
977 wprintf("&maxmsgs=1&summary=0&alpha=%s\">", bstr("alpha"));
978 escputs(addrbook[i].ab_name);
979 wprintf("</A></TD>\n");
984 wprintf("</TR></TABLE>\n");
990 * load message pointers from the server
992 int load_msg_ptrs(char *servcmd)
999 maxload = sizeof(WC->msgarr) / sizeof(long) ;
1002 if (buf[0] != '1') {
1003 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1006 while (serv_gets(buf), strcmp(buf, "000")) {
1007 if (nummsgs < maxload) {
1008 WC->msgarr[nummsgs] = atol(buf);
1017 * command loop for reading messages
1019 void readloop(char *oper)
1029 int num_displayed = 0;
1031 int is_addressbook = 0;
1032 int is_singlecard = 0;
1033 int is_calendar = 0;
1035 int remaining_messages;
1037 int lowest_displayed = (-1);
1038 int highest_displayed = 0;
1039 long pn_previous = 0L;
1040 long pn_current = 0L;
1043 struct addrbookent *addrbook = NULL;
1046 startmsg = atol(bstr("startmsg"));
1047 maxmsgs = atoi(bstr("maxmsgs"));
1048 is_summary = atoi(bstr("summary"));
1049 if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
1053 /* When in summary mode, always show ALL messages instead of just
1054 * new or old. Otherwise, show what the user asked for.
1056 if (!strcmp(oper, "readnew")) {
1057 strcpy(cmd, "MSGS NEW");
1059 else if (!strcmp(oper, "readold")) {
1060 strcpy(cmd, "MSGS OLD");
1063 strcpy(cmd, "MSGS ALL");
1066 if ((WC->wc_view == VIEW_MAILBOX) && (maxmsgs > 1)) {
1068 strcpy(cmd, "MSGS ALL");
1071 if ((WC->wc_view == VIEW_ADDRESSBOOK) && (maxmsgs > 1)) {
1073 strcpy(cmd, "MSGS ALL");
1078 strcpy(cmd, "MSGS ALL");
1081 /* Are we doing a summary view? If so, we need to know old messages
1082 * and new messages, so we can do that pretty boldface thing for the
1085 strcpy(old_msgs, "");
1089 if (buf[0] == '2') {
1090 strcpy(old_msgs, &buf[4]);
1094 is_singlecard = atoi(bstr("is_singlecard"));
1096 if (WC->wc_view == VIEW_CALENDAR) { /* calendar */
1098 strcpy(cmd, "MSGS ALL");
1101 if (WC->wc_view == VIEW_TASKS) { /* tasks */
1103 strcpy(cmd, "MSGS ALL");
1107 nummsgs = load_msg_ptrs(cmd);
1110 if ((!is_tasks) && (!is_calendar)) {
1111 if (!strcmp(oper, "readnew")) {
1112 wprintf("<EM>No new messages.</EM>\n");
1113 } else if (!strcmp(oper, "readold")) {
1114 wprintf("<EM>No old messages.</EM>\n");
1116 wprintf("<EM>No messages here.</EM>\n");
1123 if (startmsg == 0L) startmsg = WC->msgarr[0];
1124 remaining_messages = 0;
1126 for (a = 0; a < nummsgs; ++a) {
1127 if (WC->msgarr[a] >= startmsg) {
1128 ++remaining_messages;
1133 wprintf("<FORM METHOD=\"POST\" ACTION=\"/do_stuff_to_msgs\">\n"
1134 "<TABLE border=0 cellspacing=0 "
1135 "cellpadding=0 width=100%%>\n"
1137 "<TD><I>Subject</I></TD>"
1138 "<TD><I>Sender</I></TD>"
1139 "<TD><I>Date</I></TD>"
1145 for (a = 0; a < nummsgs; ++a) {
1146 if ((WC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
1148 /* Are you a new message, or an old message? */
1151 if (is_msg_in_mset(old_msgs, WC->msgarr[a])) {
1159 /* Learn which msgs "Prev" & "Next" buttons go to */
1160 pn_current = WC->msgarr[a];
1161 if (a > 0) pn_previous = WC->msgarr[a-1];
1162 if (a < (nummsgs-1)) pn_next = WC->msgarr[a+1];
1164 /* If a tabular view, set up the line */
1167 wprintf("<TR BGCOLOR=\"#%s\">",
1168 (bg ? "DDDDDD" : "FFFFFF")
1172 /* Display the message */
1174 summarize_message(WC->msgarr[a], is_new);
1176 else if (is_addressbook) {
1177 fetch_ab_name(WC->msgarr[a], buf);
1179 addrbook = realloc(addrbook,
1180 (sizeof(struct addrbookent) * num_ab) );
1181 safestrncpy(addrbook[num_ab-1].ab_name, buf,
1182 sizeof(addrbook[num_ab-1].ab_name));
1183 addrbook[num_ab-1].ab_msgnum = WC->msgarr[a];
1185 else if (is_calendar) {
1186 display_calendar(WC->msgarr[a]);
1188 else if (is_tasks) {
1189 display_task(WC->msgarr[a]);
1192 read_message(WC->msgarr[a]);
1195 /* If a tabular view, finish the line */
1200 if (lowest_displayed < 0) lowest_displayed = a;
1201 highest_displayed = a;
1204 --remaining_messages;
1209 wprintf("</TABLE>\n");
1212 /* Bump these because although we're thinking in zero base, the user
1213 * is a drooling idiot and is thinking in one base.
1216 ++highest_displayed;
1218 /* If we're only looking at one message, do a prev/next thing */
1219 if (num_displayed == 1) {
1220 if ((!is_tasks) && (!is_calendar) && (!is_addressbook) && (!is_singlecard)) {
1223 "<TABLE BORDER=0 WIDTH=100%% BGCOLOR=\"#DDDDDD\"><TR><TD>"
1224 "Reading #%d of %d messages.</TD>\n"
1225 "<TD ALIGN=RIGHT><FONT SIZE=+1>",
1226 lowest_displayed, nummsgs);
1229 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1230 "VALUE=\"Delete selected\">\n");
1233 if (pn_previous > 0L) {
1234 wprintf("<A HREF=\"/%s"
1244 wprintf("<A HREF=\"/%s"
1253 wprintf("<A HREF=\"/%s?startmsg=%ld"
1254 "&maxmsgs=999999&summary=1\">"
1260 wprintf("</TD></TR></TABLE></CENTER>\n");
1265 * If we're not currently looking at ALL requested
1266 * messages, then display the selector bar
1268 if (num_displayed > 1) {
1269 if ((!is_tasks) && (!is_calendar) && (!is_addressbook) && (!is_singlecard)) {
1271 "<TABLE BORDER=0 WIDTH=100%% BGCOLOR=\"#DDDDDD\"><TR><TD>"
1272 "Reading #%d-%d of %d messages.</TD>\n"
1273 "<TD ALIGN=RIGHT><FONT SIZE=+1>",
1274 lowest_displayed, highest_displayed, nummsgs);
1277 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1278 "VALUE=\"Delete selected\">\n");
1282 for (b=0; b<nummsgs; b = b + maxmsgs) {
1285 if (hi > nummsgs) hi = nummsgs;
1286 if (WC->msgarr[b] != startmsg) {
1287 wprintf("<A HREF=\"/%s"
1299 wprintf("%d-%d \n", lo, hi);
1303 wprintf("<A HREF=\"/%s?startmsg=%ld"
1304 "&maxmsgs=999999&summary=%d\">"
1308 WC->msgarr[0], is_summary);
1310 wprintf("<A HREF=\"/%s?startmsg=%ld"
1311 "&maxmsgs=999999&summary=1\">"
1317 wprintf("</TD></TR></TABLE></CENTER>\n");
1320 if (is_summary) wprintf("</FORM>\n");
1324 do_tasks_view(); /* Render the task list */
1328 do_calendar_view(); /* Render the calendar */
1331 if (is_addressbook) {
1332 do_addrbook_view(addrbook, num_ab); /* Render the address book */
1336 if (addrbook != NULL) free(addrbook);
1341 * Back end for post_message() ... this is where the actual message
1342 * gets transmitted to the server.
1344 void post_mime_to_server(void) {
1346 int is_multipart = 0;
1348 struct wc_attachment *att;
1350 size_t encoded_length;
1352 /* If there are attachments, we have to do multipart/mixed */
1353 if (WC->first_attachment != NULL) {
1358 sprintf(boundary, "---Citadel-Multipart-%s-%04x%04x---",
1359 serv_info.serv_fqdn,
1364 /* Remember, serv_printf() appends an extra newline */
1365 serv_printf("Content-type: multipart/mixed; "
1366 "boundary=\"%s\"\n", boundary);
1367 serv_printf("This is a multipart message in MIME format.\n");
1368 serv_printf("--%s", boundary);
1371 serv_puts("Content-type: text/html");
1373 serv_puts("<HTML><BODY>\n");
1374 text_to_server(bstr("msgtext"), 0);
1375 serv_puts("</BODY></HTML>\n");
1380 /* Add in the attachments */
1381 for (att = WC->first_attachment; att!=NULL; att=att->next) {
1383 encoded_length = ((att->length * 150) / 100);
1384 encoded = malloc(encoded_length);
1385 if (encoded == NULL) break;
1386 CtdlEncodeBase64(encoded, att->data, att->length);
1388 serv_printf("--%s", boundary);
1389 serv_printf("Content-type: %s", att->content_type);
1390 serv_printf("Content-disposition: attachment; "
1391 "filename=\"%s\"", att->filename);
1392 serv_puts("Content-transfer-encoding: base64");
1394 serv_write(encoded, strlen(encoded));
1399 serv_printf("--%s--", boundary);
1407 * Post message (or don't post message)
1409 * Note regarding the "dont_post" variable:
1410 * A random value (actually, it's just a timestamp) is inserted as a hidden
1411 * field called "postseq" when the display_enter page is generated. This
1412 * value is checked when posting, using the static variable dont_post. If a
1413 * user attempts to post twice using the same dont_post value, the message is
1414 * discarded. This prevents the accidental double-saving of the same message
1415 * if the user happens to click the browser "back" button.
1417 void post_message(void)
1420 static long dont_post = (-1L);
1421 struct wc_attachment *att, *aptr;
1423 if (WC->upload_length > 0) {
1425 /* There's an attachment. Save it to this struct... */
1426 att = malloc(sizeof(struct wc_attachment));
1427 memset(att, 0, sizeof(struct wc_attachment));
1428 att->length = WC->upload_length;
1429 strcpy(att->content_type, WC->upload_content_type);
1430 strcpy(att->filename, WC->upload_filename);
1433 /* And add it to the list. */
1434 if (WC->first_attachment == NULL) {
1435 WC->first_attachment = att;
1438 aptr = WC->first_attachment;
1439 while (aptr->next != NULL) aptr = aptr->next;
1443 /* Netscape sends a simple filename, which is what we want,
1444 * but Satan's browser sends an entire pathname. Reduce
1445 * the path to just a filename if we need to.
1447 while (num_tokens(att->filename, '/') > 1) {
1448 remove_token(att->filename, 0, '/');
1450 while (num_tokens(att->filename, '\\') > 1) {
1451 remove_token(att->filename, 0, '\\');
1454 /* Transfer control of this memory from the upload struct
1455 * to the attachment struct.
1457 att->data = WC->upload;
1458 WC->upload_length = 0;
1464 if (!strcasecmp(bstr("sc"), "Cancel")) {
1465 sprintf(WC->ImportantMessage,
1466 "Cancelled. Message was not posted.");
1467 } else if (!strcasecmp(bstr("attach"), "Add")) {
1470 } else if (atol(bstr("postseq")) == dont_post) {
1471 sprintf(WC->ImportantMessage,
1472 "Automatically cancelled because you have already "
1473 "saved this message.");
1475 sprintf(buf, "ENT0 1|%s|0|4|%s",
1480 if (buf[0] == '4') {
1481 post_mime_to_server();
1482 sprintf(WC->ImportantMessage,
1483 "Message has been posted.\n");
1484 dont_post = atol(bstr("postseq"));
1486 sprintf(WC->ImportantMessage,
1491 free_attachments(WC);
1492 readloop("readnew");
1499 * display the message entry screen
1501 void display_enter(void)
1505 struct wc_attachment *att;
1507 if (strlen(bstr("force_room")) > 0) {
1508 gotoroom(bstr("force_room"));
1511 /* Are we perhaps in an address book view? If so, then an "enter
1512 * message" command really means "add new entry."
1514 if (WC->wc_view == VIEW_ADDRESSBOOK) {
1515 do_edit_vcard(-1, "", "");
1519 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1520 /* Are we perhaps in a calendar view? If so, then an "enter
1521 * message" command really means "add new calendar item."
1523 if (WC->wc_view == VIEW_CALENDAR) {
1524 display_edit_event();
1528 /* Are we perhaps in a tasks view? If so, then an "enter
1529 * message" command really means "add new task."
1531 if (WC->wc_view == VIEW_TASKS) {
1532 display_edit_task();
1537 /* Otherwise proceed normally */
1539 sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
1543 if (!strncmp(buf, "570", 3)) {
1544 if (strlen(bstr("recp")) > 0) {
1545 svprintf("RECPERROR", WCS_STRING,
1546 "<SPAN CLASS=\"errormsg\">%s</SPAN><BR>\n",
1550 do_template("prompt_for_recipient");
1553 if (buf[0] != '2') {
1554 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1560 strcat(&buf[strlen(buf)], " <I>from</I> ");
1561 stresc(&buf[strlen(buf)], WC->wc_username, 1, 1);
1562 if (strlen(bstr("recp")) > 0) {
1563 strcat(&buf[strlen(buf)], " <I>to</I> ");
1564 stresc(&buf[strlen(buf)], bstr("recp"), 1, 1);
1566 strcat(&buf[strlen(buf)], " <I>in</I> ");
1567 stresc(&buf[strlen(buf)], WC->wc_roomname, 1, 1);
1568 svprintf("BOXTITLE", WCS_STRING, buf);
1569 do_template("beginbox");
1571 wprintf("<CENTER>\n");
1573 wprintf("<FORM ENCTYPE=\"multipart/form-data\" "
1574 "METHOD=\"POST\" ACTION=\"/post\" "
1575 "NAME=\"enterform\""
1576 "onSubmit=\"return submitForm();\""
1578 wprintf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"%s\">\n",
1580 wprintf("<INPUT TYPE=\"hidden\" NAME=\"postseq\" VALUE=\"%ld\">\n",
1583 wprintf("<TABLE border=0 cellspacing=0 cellpadding=0 width=100%%>\n");
1584 wprintf("<TR><TD ALIGN=LEFT>");
1585 wprintf("<IMG SRC=\"static/enter.gif\" ALIGN=MIDDLE ALT=\" \">");
1586 /* "onLoad=\"document.enterform.msgtext.focus();\" " */
1587 wprintf("<FONT SIZE=-1>Subject (optional):</FONT>"
1588 "<INPUT TYPE=\"text\" NAME=\"subject\" VALUE=\"");
1589 escputs(bstr("subject"));
1590 wprintf("\" SIZE=40 MAXLENGTH=70>"
1595 wprintf("<TD ALIGN=RIGHT>");
1596 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save message\">"
1598 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\"><BR>\n");
1599 wprintf("</TD></TR></TABLE>\n");
1601 wprintf("<SCRIPT language=\"JavaScript\" type=\"text/javascript\" "
1602 "src=\"static/richtext_compressed.js\"></SCRIPT>\n"
1603 "<SCRIPT language=\"JavaScript\" type=\"text/javascript\">\n"
1604 "function submitForm() { \n"
1605 " updateRTE('msgtext'); \n"
1609 "initRTE(\"static/\", \"static/\", \"\"); \n"
1611 "<noscript>JAVASCRIPT MUST BE ENABLED.</noscript> \n"
1612 "<SCRIPT language=\"javascript\" type=\"text/javascript\"> \n"
1613 "writeRichText('msgtext', '");
1614 msgescputs(bstr("msgtext"));
1615 wprintf("', '100%%', 200, true, false); \n"
1619 * Before we had the richedit widget, we did it this way...
1621 wprintf("<TEXTAREA NAME=\"msgtext\" wrap=soft ROWS=25 COLS=80 "
1623 escputs(bstr("msgtext"));
1624 wprintf("</TEXTAREA><BR>\n");
1627 /* Enumerate any attachments which are already in place... */
1628 for (att = WC->first_attachment; att != NULL; att = att->next) {
1629 wprintf("<IMG SRC=\"/static/attachment.gif\" "
1630 "BORDER=0 ALIGN=MIDDLE> Attachment: ");
1631 escputs(att->filename);
1632 wprintf(" (%s, %d bytes)<BR>\n",
1633 att->content_type, att->length);
1636 /* Now offer the ability to attach additional files... */
1637 wprintf(" "
1638 "Attach file: <input NAME=\"attachfile\" "
1639 "SIZE=48 TYPE=\"file\">\n "
1640 "<input type=\"submit\" name=\"attach\" value=\"Add\">\n");
1642 wprintf("</FORM></CENTER>\n");
1643 do_template("endbox");
1644 DONE: wDumpContent(1);
1654 void delete_msg(void)
1659 msgid = atol(bstr("msgid"));
1663 sprintf(buf, "DELE %ld", msgid);
1666 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1675 * Confirm move of a message
1677 void confirm_move_msg(void)
1683 msgid = atol(bstr("msgid"));
1687 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
1688 wprintf("<FONT SIZE=+1 COLOR=\"#FFFFFF\"");
1689 wprintf("<B>Confirm move of message</B>\n");
1690 wprintf("</FONT></TD></TR></TABLE>\n");
1692 wprintf("<CENTER>");
1694 wprintf("Move this message to:<BR>\n");
1696 wprintf("<FORM METHOD=\"POST\" ACTION=\"/move_msg\">\n");
1697 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
1701 wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1704 if (buf[0] == '1') {
1705 while (serv_gets(buf), strcmp(buf, "000")) {
1706 extract(targ, buf, 0);
1707 wprintf("<OPTION>");
1712 wprintf("</SELECT>\n");
1715 wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
1717 wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
1718 wprintf("</FORM></CENTER>\n");
1720 wprintf("</CENTER>\n");
1731 msgid = atol(bstr("msgid"));
1735 if (!strcasecmp(bstr("yesno"), "Move")) {
1736 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1739 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
1741 wprintf("<EM>Message not moved.</EM><BR>\n");
1748 * This gets called when a user selects Reply/Move/Del etc. on *one* message.
1750 void do_stuff_to_one_msg(void) {
1753 msg_oper = bstr("msg_oper");
1755 if (!strcasecmp(msg_oper, "Delete")) {
1756 delete_msg(); /* It's already been confirmed using JS */
1759 if (!strcasecmp(msg_oper, "Move")) {
1763 if (!strcasecmp(msg_oper, "Reply")) {
1764 display_enter(); /* recp and subject already set */
1768 /* should never get here. FIXME: display an error */
1774 * This gets called when a user selects multiple messages in a summary
1775 * list and then clicks to perform a transformation of some sort on them
1776 * (such as deleting them).
1778 void do_stuff_to_msgs(void) {
1783 struct stuff_t *next;
1787 struct stuff_t *stuff = NULL;
1788 struct stuff_t *ptr;
1791 serv_puts("MSGS ALL");
1794 if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
1795 ptr = malloc(sizeof(struct stuff_t));
1796 ptr->msgnum = atol(buf);
1801 strcpy(sc, bstr("sc"));
1803 while (stuff != NULL) {
1805 sprintf(buf, "msg_%ld", stuff->msgnum);
1806 if (!strcasecmp(bstr(buf), "yes")) {
1808 if (!strcasecmp(sc, "Delete selected")) {
1809 serv_printf("DELE %ld", stuff->msgnum);
1820 readloop("readfwd");