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);
106 if ((strlen(storename)>0) && (storename[0] != ';')) {
107 while(storename[strlen(storename)-1] == ';') {
108 storename[strlen(storename)-1] = 0;
117 /* display_vcard() calls this after parsing the textual vCard into
118 * our 'struct vCard' data object.
120 * Set 'full' to nonzero to display the full card, otherwise it will only
121 * show a summary line.
123 * This code is a bit ugly, so perhaps an explanation is due: we do this
124 * in two passes through the vCard fields. On the first pass, we process
125 * fields we understand, and then render them in a pretty fashion at the
126 * end. Then we make a second pass, outputting all the fields we don't
127 * understand in a simple two-column name/value format.
129 void display_parsed_vcard(struct vCard *v, int full) {
135 char *thisname, *thisvalue;
136 char firsttoken[SIZ];
139 char displayname[SIZ];
143 strcpy(displayname, "");
149 name = vcard_get_prop(v, "fn", 1, 0, 0);
153 else if (name = vcard_get_prop(v, "n", 1, 0, 0), name != NULL) {
163 wprintf("<TABLE bgcolor=#888888>");
164 for (pass=1; pass<=2; ++pass) {
166 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
168 thisname = strdup(v->prop[i].name);
169 extract_token(firsttoken, thisname, 0, ';');
171 for (j=0; j<num_tokens(thisname, ';'); ++j) {
172 extract_token(buf, thisname, j, ';');
173 if (!strcasecmp(buf, "encoding=quoted-printable")) {
175 remove_token(thisname, j, ';');
177 if (!strcasecmp(buf, "encoding=base64")) {
179 remove_token(thisname, j, ';');
184 thisvalue = malloc(strlen(v->prop[i].value) + 50);
185 j = CtdlDecodeQuotedPrintable(
186 thisvalue, v->prop[i].value,
187 strlen(v->prop[i].value) );
191 thisvalue = malloc(strlen(v->prop[i].value) + 50);
193 thisvalue, v->prop[i].value,
194 strlen(v->prop[i].value) );
197 thisvalue = strdup(v->prop[i].value);
200 /*** Various fields we may encounter ***/
202 /* N is name, but only if there's no FN already there */
203 if (!strcasecmp(firsttoken, "n")) {
204 if (strlen(displayname) == 0) {
205 strcpy(displayname, thisvalue);
209 /* FN (full name) is a true 'display name' field */
210 else if (!strcasecmp(firsttoken, "fn")) {
211 strcpy(displayname, thisvalue);
214 else if (!strcasecmp(firsttoken, "email")) {
215 if (strlen(mailto) > 0) strcat(mailto, "<br />");
217 "<A HREF=\"/display_enter"
218 "?force_room=_MAIL_&recp=");
219 urlesc(&mailto[strlen(mailto)], thisvalue);
220 strcat(mailto, "\">");
221 urlesc(&mailto[strlen(mailto)], thisvalue);
222 strcat(mailto, "</A>");
224 else if (!strcasecmp(firsttoken, "tel")) {
225 if (strlen(phone) > 0) strcat(phone, "<br />");
226 strcat(phone, thisvalue);
227 for (j=0; j<num_tokens(thisname, ';'); ++j) {
228 extract_token(buf, thisname, j, ';');
229 if (!strcasecmp(buf, "tel"))
231 else if (!strcasecmp(buf, "work"))
232 strcat(phone, " (work)");
233 else if (!strcasecmp(buf, "home"))
234 strcat(phone, " (home)");
235 else if (!strcasecmp(buf, "cell"))
236 strcat(phone, " (cell)");
244 else if (!strcasecmp(firsttoken, "adr")) {
246 wprintf("<TR><TD>Address:</TD><TD>");
247 for (j=0; j<num_tokens(thisvalue, ';'); ++j) {
248 extract_token(buf, thisvalue, j, ';');
249 if (strlen(buf) > 0) {
254 wprintf("</TD></TR>\n");
257 else if (!strcasecmp(firsttoken, "version")) {
260 else if (!strcasecmp(firsttoken, "rev")) {
263 else if (!strcasecmp(firsttoken, "label")) {
270 wprintf("</TD><TD>");
272 wprintf("</TD></TR>\n");
281 wprintf("<TR BGCOLOR=\"#AAAAAA\">"
282 "<TD COLSPAN=2 BGCOLOR=\"#FFFFFF\">"
283 "<IMG ALIGN=CENTER SRC=\"/static/vcard.gif\">"
284 "<FONT SIZE=+1><B>");
285 escputs(displayname);
286 wprintf("</B></FONT></TD></TR>\n");
288 if (strlen(phone) > 0)
289 wprintf("<TR><TD>Telephone:</TD><TD>%s</TD></TR>\n", phone);
290 if (strlen(mailto) > 0)
291 wprintf("<TR><TD>E-mail:</TD><TD>%s</TD></TR>\n", mailto);
296 wprintf("</TABLE>\n");
302 * Display a textual vCard
303 * (Converts to a vCard object and then calls the actual display function)
304 * Set 'full' to nonzero to display the whole card instead of a one-liner.
305 * Or, if "storename" is non-NULL, just store the person's name in that
306 * buffer instead of displaying the card at all.
308 void display_vcard(char *vcard_source, char alpha, int full, char *storename) {
314 v = vcard_load(vcard_source);
315 if (v == NULL) return;
317 name = vcard_get_prop(v, "n", 1, 0, 0);
323 if (storename != NULL) {
324 fetchname_parsed_vcard(v, storename);
326 else if ( (alpha == 0)
327 || ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha)) )
328 || ((!isalpha(alpha)) && (!isalpha(this_alpha))) ) {
329 display_parsed_vcard(v, full);
339 * I wanna SEE that message!
341 void read_message(long msgnum) {
343 char mime_partnum[SIZ];
344 char mime_filename[SIZ];
345 char mime_content_type[SIZ];
346 char mime_disposition[SIZ];
358 char vcard_partnum[SIZ];
359 char cal_partnum[SIZ];
360 char *part_source = NULL;
365 strcpy(reply_to, "");
366 strcpy(vcard_partnum, "");
367 strcpy(cal_partnum, "");
368 strcpy(mime_http, "");
370 serv_printf("MSG4 %ld", msgnum);
373 wprintf("<STRONG>ERROR:</STRONG> %s<br />\n", &buf[4]);
377 /* begin everythingamundo table */
378 wprintf("<center><table width=99%% border=1 cellspacing=0 "
379 "cellpadding=0><TR><TD>\n");
381 /* begin message header table */
382 wprintf("<TABLE WIDTH=100%% BORDER=0 CELLSPACING=0 "
383 "CELLPADDING=1 BGCOLOR=\"#CCCCCC\"><TR><TD>\n");
385 wprintf("<SPAN CLASS=\"message_header\">");
386 strcpy(m_subject, "");
388 while (serv_gets(buf), strcasecmp(buf, "text")) {
389 if (!strcmp(buf, "000")) {
390 wprintf("<I>unexpected end of message</I><br /><br />\n");
391 wprintf("</SPAN>\n");
394 if (!strncasecmp(buf, "nhdr=yes", 8))
398 if (!strncasecmp(buf, "type=", 5))
399 format_type = atoi(&buf[5]);
400 if (!strncasecmp(buf, "from=", 5)) {
401 strcpy(from, &buf[5]);
402 wprintf("from <A HREF=\"/showuser&who=");
408 if (!strncasecmp(buf, "subj=", 5))
409 strcpy(m_subject, &buf[5]);
410 if ((!strncasecmp(buf, "hnod=", 5))
411 && (strcasecmp(&buf[5], serv_info.serv_humannode)))
412 wprintf("(%s) ", &buf[5]);
413 if ((!strncasecmp(buf, "room=", 5))
414 && (strcasecmp(&buf[5], WC->wc_roomname))
415 && (strlen(&buf[5])>0) )
416 wprintf("in %s> ", &buf[5]);
417 if (!strncasecmp(buf, "rfca=", 5)) {
418 strcpy(rfca, &buf[5]);
424 if (!strncasecmp(buf, "node=", 5)) {
425 strcpy(node, &buf[5]);
426 if ( ((WC->room_flags & QR_NETWORK)
427 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
428 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
431 wprintf("@%s ", &buf[5]);
434 if (!strncasecmp(buf, "rcpt=", 5))
435 wprintf("to %s ", &buf[5]);
436 if (!strncasecmp(buf, "time=", 5)) {
437 fmt_date(now, atol(&buf[5]));
441 if (!strncasecmp(buf, "part=", 5)) {
442 extract(mime_filename, &buf[5], 1);
443 extract(mime_partnum, &buf[5], 2);
444 extract(mime_disposition, &buf[5], 3);
445 extract(mime_content_type, &buf[5], 4);
446 mime_length = extract_int(&buf[5], 5);
448 if (!strcasecmp(mime_disposition, "attachment")) {
449 snprintf(&mime_http[strlen(mime_http)],
450 (sizeof(mime_http) - strlen(mime_http) - 1),
451 "<A HREF=\"/output_mimepart?"
452 "msgnum=%ld&partnum=%s\" "
453 "TARGET=\"wc.%ld.%s\">"
454 "<IMG SRC=\"/static/attachment.gif\" "
455 "BORDER=0 ALIGN=MIDDLE>\n"
456 "Part %s: %s (%s, %d bytes)</A><br />\n",
457 msgnum, mime_partnum,
458 msgnum, mime_partnum,
459 mime_partnum, mime_filename,
460 mime_content_type, mime_length);
463 if ((!strcasecmp(mime_disposition, "inline"))
464 && (!strncasecmp(mime_content_type, "image/", 6)) ){
465 snprintf(&mime_http[strlen(mime_http)],
466 (sizeof(mime_http) - strlen(mime_http) - 1),
467 "<IMG SRC=\"/output_mimepart?"
468 "msgnum=%ld&partnum=%s\">",
469 msgnum, mime_partnum);
472 /*** begin handler prep ***/
473 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
474 strcpy(vcard_partnum, mime_partnum);
477 if (!strcasecmp(mime_content_type, "text/calendar")) {
478 strcpy(cal_partnum, mime_partnum);
481 /*** end handler prep ***/
487 /* Generate a reply-to address */
488 if (strlen(rfca) > 0) {
489 strcpy(reply_to, rfca);
492 if ( (strlen(node) > 0)
493 && (strcasecmp(node, serv_info.serv_nodename))
494 && (strcasecmp(node, serv_info.serv_humannode)) ) {
495 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
499 snprintf(reply_to, sizeof(reply_to), "%s", from);
508 if (strlen(m_subject) > 0) {
510 "<SPAN CLASS=\"message_subject\">"
517 /* start msg buttons */
518 wprintf("<TD ALIGN=RIGHT>\n");
519 wprintf("<FORM METHOD=\"POST\" ACTION=\"/do_stuff_to_one_msg\">\n");
520 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%ld\">\n",
522 wprintf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"");
526 if (!strncasecmp(m_subject, "Re:", 2)) {
527 wprintf("<INPUT TYPE=\"hidden\" NAME=\"subject\" VALUE=\"");
531 else if (strlen(m_subject) > 0) {
532 wprintf("<INPUT TYPE=\"hidden\" NAME=\"subject\" VALUE=\"Re: ");
537 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");
539 if (WC->is_room_aide) {
540 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"
541 "<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\""
542 "onClick=\"return confirm('Delete this message?');\">\n");
546 "</TD></TR></TABLE>\n");
549 wprintf("<TABLE BORDER=0 WIDTH=100%% BGCOLOR=#FFFFFF "
550 "CELLPADDING=1 CELLSPACING=0><TR><TD>");
553 * Learn the content type
555 strcpy(mime_content_type, "text/plain");
556 while (serv_gets(buf), (strlen(buf) > 0)) {
557 if (!strcmp(buf, "000")) {
558 wprintf("<I>unexpected end of message</I><br /><br />\n");
561 if (!strncasecmp(buf, "Content-type: ", 14)) {
562 safestrncpy(mime_content_type, &buf[14],
563 sizeof(mime_content_type));
567 /* Messages in legacy Citadel variformat get handled thusly... */
568 if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
569 fmout(NULL, "JUSTIFY");
572 /* Boring old 80-column fixed format text gets handled this way... */
573 else if (!strcasecmp(mime_content_type, "text/plain")) {
574 while (serv_gets(buf), strcmp(buf, "000")) {
575 if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
576 if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
577 while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
578 buf[strlen(buf) - 1] = 0;
580 ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
581 wprintf("<BLOCKQUOTE>");
583 } else if ((bq == 1) &&
584 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
585 wprintf("</BLOCKQUOTE>");
591 wprintf("</TT><br />\n");
593 wprintf("</I><br />");
596 else /* HTML is fun, but we've got to strip it first */
597 if (!strcasecmp(mime_content_type, "text/html")) {
601 /* Unknown weirdness */
603 wprintf("I don't know how to display %s<br />\n",
605 while (serv_gets(buf), strcmp(buf, "000")) { }
609 /* Afterwards, offer links to download attachments 'n' such */
610 if (strlen(mime_http) > 0) {
611 wprintf("%s", mime_http);
614 /* Handler for vCard parts */
615 if (strlen(vcard_partnum) > 0) {
616 part_source = load_mimepart(msgnum, vcard_partnum);
617 if (part_source != NULL) {
619 /* If it's my vCard I can edit it */
620 if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
621 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
622 wprintf("<A HREF=\"/edit_vcard?"
623 "msgnum=%ld&partnum=%s\">",
624 msgnum, vcard_partnum);
625 wprintf("(edit)</A>");
628 /* In all cases, display the full card */
629 display_vcard(part_source, 0, 1, NULL);
633 /* Handler for calendar parts */
634 if (strlen(cal_partnum) > 0) {
635 part_source = load_mimepart(msgnum, cal_partnum);
636 if (part_source != NULL) {
637 cal_process_attachment(part_source,
638 msgnum, cal_partnum);
648 wprintf("</TD></TR></TABLE>\n");
650 /* end everythingamundo table */
651 wprintf("</TD></TR></TABLE></center><br />\n");
655 void summarize_message(long msgnum, int is_new) {
666 memset(&summ, 0, sizeof(summ));
667 strcpy(summ.subj, "(no subject)");
669 sprintf(buf, "MSG0 %ld|3", msgnum); /* ask for headers only with no MIME */
672 if (buf[0] != '1') return;
674 while (serv_gets(buf), strcmp(buf, "000")) {
675 if (!strncasecmp(buf, "from=", 5)) {
676 strcpy(summ.from, &buf[5]);
678 if (!strncasecmp(buf, "subj=", 5)) {
679 strcpy(summ.subj, &buf[5]);
681 if (!strncasecmp(buf, "rfca=", 5)) {
682 strcat(summ.from, " <");
683 strcat(summ.from, &buf[5]);
684 strcat(summ.from, ">");
687 if (!strncasecmp(buf, "node=", 5)) {
688 if ( ((WC->room_flags & QR_NETWORK)
689 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
690 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
692 strcat(summ.from, " @ ");
693 strcat(summ.from, &buf[5]);
697 if (!strncasecmp(buf, "rcpt=", 5)) {
698 strcpy(summ.to, &buf[5]);
701 if (!strncasecmp(buf, "time=", 5)) {
702 fmt_date(summ.date, atol(&buf[5]));
707 if (is_new) wprintf("<B>");
708 wprintf("<A HREF=\"/readfwd?startmsg=%ld"
709 "&maxmsgs=1&summary=0\">",
713 if (is_new) wprintf("</B>");
714 wprintf("</TD><TD>");
715 if (is_new) wprintf("<B>");
717 if (is_new) wprintf("</B>");
718 wprintf(" </TD><TD>");
719 if (is_new) wprintf("<B>");
721 if (is_new) wprintf("</B>");
724 "<INPUT TYPE=\"checkbox\" NAME=\"msg_%ld\" VALUE=\"yes\">"
732 void display_addressbook(long msgnum, char alpha) {
734 char mime_partnum[SIZ];
735 char mime_filename[SIZ];
736 char mime_content_type[SIZ];
737 char mime_disposition[SIZ];
739 char vcard_partnum[SIZ];
740 char *vcard_source = NULL;
750 memset(&summ, 0, sizeof(summ));
751 strcpy(summ.subj, "(no subject)");
753 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
756 if (buf[0] != '1') return;
758 while (serv_gets(buf), strcmp(buf, "000")) {
759 if (!strncasecmp(buf, "part=", 5)) {
760 extract(mime_filename, &buf[5], 1);
761 extract(mime_partnum, &buf[5], 2);
762 extract(mime_disposition, &buf[5], 3);
763 extract(mime_content_type, &buf[5], 4);
764 mime_length = extract_int(&buf[5], 5);
766 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
767 strcpy(vcard_partnum, mime_partnum);
773 if (strlen(vcard_partnum) > 0) {
774 vcard_source = load_mimepart(msgnum, vcard_partnum);
775 if (vcard_source != NULL) {
777 /* Display the summary line */
778 display_vcard(vcard_source, alpha, 0, NULL);
780 /* If it's my vCard I can edit it */
781 if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
782 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
783 wprintf("<A HREF=\"/edit_vcard?"
784 "msgnum=%ld&partnum=%s\">",
785 msgnum, vcard_partnum);
786 wprintf("(edit)</A>");
797 /* If it's an old "Firstname Lastname" style record, try to
800 void lastfirst_firstlast(char *namebuf) {
805 if (namebuf == NULL) return;
806 if (strchr(namebuf, ';') != NULL) return;
808 i = num_tokens(namebuf, ' ');
811 extract_token(lastname, namebuf, i-1, ' ');
812 remove_token(namebuf, i-1, ' ');
813 strcpy(firstname, namebuf);
814 sprintf(namebuf, "%s; %s", lastname, firstname);
818 void fetch_ab_name(long msgnum, char *namebuf) {
820 char mime_partnum[SIZ];
821 char mime_filename[SIZ];
822 char mime_content_type[SIZ];
823 char mime_disposition[SIZ];
825 char vcard_partnum[SIZ];
826 char *vcard_source = NULL;
837 if (namebuf == NULL) return;
840 memset(&summ, 0, sizeof(summ));
841 strcpy(summ.subj, "(no subject)");
843 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
846 if (buf[0] != '1') return;
848 while (serv_gets(buf), strcmp(buf, "000")) {
849 if (!strncasecmp(buf, "part=", 5)) {
850 extract(mime_filename, &buf[5], 1);
851 extract(mime_partnum, &buf[5], 2);
852 extract(mime_disposition, &buf[5], 3);
853 extract(mime_content_type, &buf[5], 4);
854 mime_length = extract_int(&buf[5], 5);
856 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
857 strcpy(vcard_partnum, mime_partnum);
863 if (strlen(vcard_partnum) > 0) {
864 vcard_source = load_mimepart(msgnum, vcard_partnum);
865 if (vcard_source != NULL) {
867 /* Grab the name off the card */
868 display_vcard(vcard_source, 0, 0, namebuf);
874 lastfirst_firstlast(namebuf);
876 for (i=0; i<strlen(namebuf); ++i) {
877 if (namebuf[i] != ';') return;
879 strcpy(namebuf, "(no name)");
885 * Record compare function for sorting address book indices
887 int abcmp(const void *ab1, const void *ab2) {
889 (((const struct addrbookent *)ab1)->ab_name),
890 (((const struct addrbookent *)ab2)->ab_name)
896 * Helper function for do_addrbook_view()
897 * Converts a name into a three-letter tab label
899 void nametab(char *tabbuf, char *name) {
900 stresc(tabbuf, name, 0, 0);
901 tabbuf[0] = toupper(tabbuf[0]);
902 tabbuf[1] = tolower(tabbuf[1]);
903 tabbuf[2] = tolower(tabbuf[2]);
909 * Render the address book using info we gathered during the scan
911 void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
915 static int NAMESPERPAGE = 60;
919 char tabfirst_label[SIZ];
921 char tablast_label[SIZ];
924 wprintf("<I>This address book is empty.</I>\n");
929 qsort(addrbook, num_ab, sizeof(struct addrbookent), abcmp);
932 num_pages = num_ab / NAMESPERPAGE;
934 page = atoi(bstr("page"));
937 for (i=0; i<=num_pages; ++i) {
939 wprintf("<A HREF=\"/readfwd?page=%d\">", i);
944 tabfirst = i * NAMESPERPAGE;
945 tablast = tabfirst + NAMESPERPAGE - 1;
946 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
947 nametab(tabfirst_label, addrbook[tabfirst].ab_name);
948 nametab(tablast_label, addrbook[tablast].ab_name);
949 wprintf("[%s - %s]",
950 tabfirst_label, tablast_label
961 wprintf("<TABLE border=0 cellspacing=0 "
962 "cellpadding=3 width=100%%>\n"
965 for (i=0; i<num_ab; ++i) {
967 if ((i / NAMESPERPAGE) == page) {
969 if ((displayed % 4) == 0) {
974 wprintf("<TR BGCOLOR=\"#%s\">",
975 (bg ? "DDDDDD" : "FFFFFF")
981 wprintf("<A HREF=\"/readfwd?startmsg=%ld&is_singlecard=1",
982 addrbook[i].ab_msgnum);
983 wprintf("&maxmsgs=1&summary=0&alpha=%s\">", bstr("alpha"));
984 escputs(addrbook[i].ab_name);
985 wprintf("</A></TD>\n");
990 wprintf("</TR></TABLE>\n");
996 * load message pointers from the server
998 int load_msg_ptrs(char *servcmd)
1005 maxload = sizeof(WC->msgarr) / sizeof(long) ;
1008 if (buf[0] != '1') {
1009 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1012 while (serv_gets(buf), strcmp(buf, "000")) {
1013 if (nummsgs < maxload) {
1014 WC->msgarr[nummsgs] = atol(buf);
1023 * command loop for reading messages
1025 void readloop(char *oper)
1035 int num_displayed = 0;
1037 int is_addressbook = 0;
1038 int is_singlecard = 0;
1039 int is_calendar = 0;
1042 int remaining_messages;
1044 int lowest_displayed = (-1);
1045 int highest_displayed = 0;
1046 long pn_previous = 0L;
1047 long pn_current = 0L;
1050 struct addrbookent *addrbook = NULL;
1053 startmsg = atol(bstr("startmsg"));
1054 maxmsgs = atoi(bstr("maxmsgs"));
1055 is_summary = atoi(bstr("summary"));
1056 if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
1058 output_headers(1, 1, 1, 0, 0, 0, 0);
1060 /* When in summary mode, always show ALL messages instead of just
1061 * new or old. Otherwise, show what the user asked for.
1063 if (!strcmp(oper, "readnew")) {
1064 strcpy(cmd, "MSGS NEW");
1066 else if (!strcmp(oper, "readold")) {
1067 strcpy(cmd, "MSGS OLD");
1070 strcpy(cmd, "MSGS ALL");
1073 if ((WC->wc_view == VIEW_MAILBOX) && (maxmsgs > 1)) {
1075 strcpy(cmd, "MSGS ALL");
1078 if ((WC->wc_view == VIEW_ADDRESSBOOK) && (maxmsgs > 1)) {
1080 strcpy(cmd, "MSGS ALL");
1085 strcpy(cmd, "MSGS ALL");
1088 /* Are we doing a summary view? If so, we need to know old messages
1089 * and new messages, so we can do that pretty boldface thing for the
1092 strcpy(old_msgs, "");
1096 if (buf[0] == '2') {
1097 strcpy(old_msgs, &buf[4]);
1101 is_singlecard = atoi(bstr("is_singlecard"));
1103 if (WC->wc_view == VIEW_CALENDAR) { /* calendar */
1105 strcpy(cmd, "MSGS ALL");
1108 if (WC->wc_view == VIEW_TASKS) { /* tasks */
1110 strcpy(cmd, "MSGS ALL");
1113 if (WC->wc_view == VIEW_NOTES) { /* notes */
1115 strcpy(cmd, "MSGS ALL");
1119 nummsgs = load_msg_ptrs(cmd);
1122 if ((!is_tasks) && (!is_calendar) && (!is_notes)) {
1123 if (!strcmp(oper, "readnew")) {
1124 wprintf("<EM>No new messages.</EM>\n");
1125 } else if (!strcmp(oper, "readold")) {
1126 wprintf("<EM>No old messages.</EM>\n");
1128 wprintf("<EM>No messages here.</EM>\n");
1135 if (startmsg == 0L) startmsg = WC->msgarr[0];
1136 remaining_messages = 0;
1138 for (a = 0; a < nummsgs; ++a) {
1139 if (WC->msgarr[a] >= startmsg) {
1140 ++remaining_messages;
1144 wprintf("<FORM NAME=\"msgomatic\" "
1145 "METHOD=\"POST\" ACTION=\"/do_stuff_to_msgs\">\n");
1148 "<center><table border=0 cellspacing=0 "
1149 "cellpadding=0 width=99%%>\n"
1151 "<TD><I>Subject</I></TD>"
1152 "<TD><I>Sender</I></TD>"
1153 "<TD><I>Date</I></TD>"
1159 for (a = 0; a < nummsgs; ++a) {
1160 if ((WC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
1162 /* Are you a new message, or an old message? */
1165 if (is_msg_in_mset(old_msgs, WC->msgarr[a])) {
1173 /* Learn which msgs "Prev" & "Next" buttons go to */
1174 pn_current = WC->msgarr[a];
1175 if (a > 0) pn_previous = WC->msgarr[a-1];
1176 if (a < (nummsgs-1)) pn_next = WC->msgarr[a+1];
1178 /* If a tabular view, set up the line */
1181 wprintf("<TR BGCOLOR=\"#%s\">",
1182 (bg ? "DDDDDD" : "FFFFFF")
1186 /* Display the message */
1188 summarize_message(WC->msgarr[a], is_new);
1190 else if (is_addressbook) {
1191 fetch_ab_name(WC->msgarr[a], buf);
1193 addrbook = realloc(addrbook,
1194 (sizeof(struct addrbookent) * num_ab) );
1195 safestrncpy(addrbook[num_ab-1].ab_name, buf,
1196 sizeof(addrbook[num_ab-1].ab_name));
1197 addrbook[num_ab-1].ab_msgnum = WC->msgarr[a];
1199 else if (is_calendar) {
1200 display_calendar(WC->msgarr[a]);
1202 else if (is_tasks) {
1203 display_task(WC->msgarr[a]);
1205 else if (is_notes) {
1206 display_note(WC->msgarr[a]);
1209 read_message(WC->msgarr[a]);
1212 /* If a tabular view, finish the line */
1217 if (lowest_displayed < 0) lowest_displayed = a;
1218 highest_displayed = a;
1221 --remaining_messages;
1226 wprintf("</table></center>\n");
1229 /* Bump these because although we're thinking in zero base, the user
1230 * is a drooling idiot and is thinking in one base.
1233 ++highest_displayed;
1235 /* If we're only looking at one message, do a prev/next thing */
1236 if (num_displayed == 1) {
1237 if ((!is_tasks) && (!is_calendar) && (!is_addressbook) && (!is_notes) && (!is_singlecard)) {
1240 "<TABLE BORDER=0 WIDTH=99%% BGCOLOR=\"#DDDDDD\"><TR><TD>"
1241 "Reading #%d of %d messages.</TD>\n"
1242 "<TD ALIGN=RIGHT><FONT SIZE=+1>",
1243 lowest_displayed, nummsgs);
1246 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1247 "VALUE=\"Delete selected\">\n");
1250 if (pn_previous > 0L) {
1251 wprintf("<A HREF=\"/%s"
1261 wprintf("<A HREF=\"/%s"
1270 wprintf("<A HREF=\"/%s?startmsg=%ld"
1271 "&maxmsgs=999999&summary=1\">"
1281 * If we're not currently looking at ALL requested
1282 * messages, then display the selector bar
1284 if (num_displayed > 1) {
1285 if ((!is_tasks) && (!is_calendar) && (!is_addressbook)
1286 && (!is_notes) && (!is_singlecard)) {
1288 wprintf("Reading #", lowest_displayed, highest_displayed);
1290 wprintf("<SELECT NAME=\"whichones\" SIZE=\"1\" "
1291 "OnChange=\"location.href=msgomatic.whichones.options"
1292 "[selectedIndex].value\">\n");
1294 for (b=0; b<nummsgs; b = b + maxmsgs) {
1297 if (hi > nummsgs) hi = nummsgs;
1298 wprintf("<OPTION %s VALUE="
1303 "%d-%d</OPTION> \n",
1304 ((WC->msgarr[b] == startmsg) ? "SELECTED" : ""),
1311 wprintf("<OPTION VALUE=\"/%s?startmsg=%ld"
1312 "&maxmsgs=999999&summary=%d\">"
1316 WC->msgarr[0], is_summary);
1318 wprintf("<OPTION VALUE=\"/%s?startmsg=%ld"
1319 "&maxmsgs=999999&summary=1\">"
1325 wprintf("</SELECT> of %d messages.", nummsgs);
1328 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1329 "VALUE=\"Delete selected\">\n");
1334 if (is_summary) wprintf("</FORM>\n");
1338 do_tasks_view(); /* Render the task list */
1342 do_calendar_view(); /* Render the calendar */
1345 if (is_addressbook) {
1346 do_addrbook_view(addrbook, num_ab); /* Render the address book */
1350 if (addrbook != NULL) free(addrbook);
1355 * Back end for post_message() ... this is where the actual message
1356 * gets transmitted to the server.
1358 void post_mime_to_server(void) {
1360 int is_multipart = 0;
1362 struct wc_attachment *att;
1364 size_t encoded_length;
1366 /* If there are attachments, we have to do multipart/mixed */
1367 if (WC->first_attachment != NULL) {
1372 sprintf(boundary, "---Citadel-Multipart-%s-%04x%04x---",
1373 serv_info.serv_fqdn,
1378 /* Remember, serv_printf() appends an extra newline */
1379 serv_printf("Content-type: multipart/mixed; "
1380 "boundary=\"%s\"\n", boundary);
1381 serv_printf("This is a multipart message in MIME format.\n");
1382 serv_printf("--%s", boundary);
1385 serv_puts("Content-type: text/html");
1387 serv_puts("<HTML><BODY>\n");
1388 text_to_server(bstr("msgtext"), 0);
1389 serv_puts("</BODY></HTML>\n");
1394 /* Add in the attachments */
1395 for (att = WC->first_attachment; att!=NULL; att=att->next) {
1397 encoded_length = ((att->length * 150) / 100);
1398 encoded = malloc(encoded_length);
1399 if (encoded == NULL) break;
1400 CtdlEncodeBase64(encoded, att->data, att->length);
1402 serv_printf("--%s", boundary);
1403 serv_printf("Content-type: %s", att->content_type);
1404 serv_printf("Content-disposition: attachment; "
1405 "filename=\"%s\"", att->filename);
1406 serv_puts("Content-transfer-encoding: base64");
1408 serv_write(encoded, strlen(encoded));
1413 serv_printf("--%s--", boundary);
1421 * Post message (or don't post message)
1423 * Note regarding the "dont_post" variable:
1424 * A random value (actually, it's just a timestamp) is inserted as a hidden
1425 * field called "postseq" when the display_enter page is generated. This
1426 * value is checked when posting, using the static variable dont_post. If a
1427 * user attempts to post twice using the same dont_post value, the message is
1428 * discarded. This prevents the accidental double-saving of the same message
1429 * if the user happens to click the browser "back" button.
1431 void post_message(void)
1434 static long dont_post = (-1L);
1435 struct wc_attachment *att, *aptr;
1437 if (WC->upload_length > 0) {
1439 /* There's an attachment. Save it to this struct... */
1440 att = malloc(sizeof(struct wc_attachment));
1441 memset(att, 0, sizeof(struct wc_attachment));
1442 att->length = WC->upload_length;
1443 strcpy(att->content_type, WC->upload_content_type);
1444 strcpy(att->filename, WC->upload_filename);
1447 /* And add it to the list. */
1448 if (WC->first_attachment == NULL) {
1449 WC->first_attachment = att;
1452 aptr = WC->first_attachment;
1453 while (aptr->next != NULL) aptr = aptr->next;
1457 /* Netscape sends a simple filename, which is what we want,
1458 * but Satan's browser sends an entire pathname. Reduce
1459 * the path to just a filename if we need to.
1461 while (num_tokens(att->filename, '/') > 1) {
1462 remove_token(att->filename, 0, '/');
1464 while (num_tokens(att->filename, '\\') > 1) {
1465 remove_token(att->filename, 0, '\\');
1468 /* Transfer control of this memory from the upload struct
1469 * to the attachment struct.
1471 att->data = WC->upload;
1472 WC->upload_length = 0;
1478 if (!strcasecmp(bstr("sc"), "Cancel")) {
1479 sprintf(WC->ImportantMessage,
1480 "Cancelled. Message was not posted.");
1481 } else if (!strcasecmp(bstr("attach"), "Add")) {
1484 } else if (atol(bstr("postseq")) == dont_post) {
1485 sprintf(WC->ImportantMessage,
1486 "Automatically cancelled because you have already "
1487 "saved this message.");
1489 sprintf(buf, "ENT0 1|%s|0|4|%s",
1494 if (buf[0] == '4') {
1495 post_mime_to_server();
1496 if (strlen(bstr("recp")) > 0) {
1497 sprintf(WC->ImportantMessage, "Message has been sent.\n");
1500 sprintf(WC->ImportantMessage, "Message has been posted.\n");
1502 dont_post = atol(bstr("postseq"));
1504 sprintf(WC->ImportantMessage,
1509 free_attachments(WC);
1510 readloop("readnew");
1517 * display the message entry screen
1519 void display_enter(void)
1523 struct wc_attachment *att;
1525 if (strlen(bstr("force_room")) > 0) {
1526 gotoroom(bstr("force_room"));
1529 /* Are we perhaps in an address book view? If so, then an "enter
1530 * message" command really means "add new entry."
1532 if (WC->wc_view == VIEW_ADDRESSBOOK) {
1533 do_edit_vcard(-1, "", "");
1537 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1538 /* Are we perhaps in a calendar view? If so, then an "enter
1539 * message" command really means "add new calendar item."
1541 if (WC->wc_view == VIEW_CALENDAR) {
1542 display_edit_event();
1546 /* Are we perhaps in a tasks view? If so, then an "enter
1547 * message" command really means "add new task."
1549 if (WC->wc_view == VIEW_TASKS) {
1550 display_edit_task();
1555 /* Otherwise proceed normally. Do a custom room banner with no navbar... */
1556 output_headers(1, 1, 2, 0, 0, 0, 0);
1557 wprintf("<div id=\"banner\">\n");
1558 embed_room_banner(NULL, navbar_none);
1559 wprintf("</div>\n");
1560 wprintf("<div id=\"content\">\n");
1562 sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
1566 if (!strncmp(buf, "570", 3)) {
1567 if (strlen(bstr("recp")) > 0) {
1568 svprintf("RECPERROR", WCS_STRING,
1569 "<SPAN CLASS=\"errormsg\">%s</SPAN><br />\n",
1573 do_template("prompt_for_recipient");
1576 if (buf[0] != '2') {
1577 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1583 strcat(&buf[strlen(buf)], " <I>from</I> ");
1584 stresc(&buf[strlen(buf)], WC->wc_username, 1, 1);
1585 if (strlen(bstr("recp")) > 0) {
1586 strcat(&buf[strlen(buf)], " <I>to</I> ");
1587 stresc(&buf[strlen(buf)], bstr("recp"), 1, 1);
1589 strcat(&buf[strlen(buf)], " <I>in</I> ");
1590 stresc(&buf[strlen(buf)], WC->wc_roomname, 1, 1);
1592 /* begin message entry screen */
1593 wprintf("<div style=\"position:absolute; left:1%%; width:96%%; height:100%%\">\n");
1595 wprintf("<form enctype=\"multipart/form-data\" "
1596 "method=\"POST\" action=\"/post\" "
1597 "name=\"enterform\""
1598 "onSubmit=\"return submitForm();\""
1600 wprintf("<input type=\"hidden\" name=\"recp\" value=\"%s\">\n",
1602 wprintf("<input type=\"hidden\" name=\"postseq\" value=\"%ld\">\n",
1605 wprintf("%s<br>\n", buf); /* header bar */
1606 wprintf("<img src=\"static/enter.gif\" align=middle alt=\" \">");
1607 /* "onLoad=\"document.enterform.msgtext.focus();\" " */
1608 wprintf("<font size=-1>Subject (optional):</font>"
1609 "<input type=\"text\" name=\"subject\" value=\"");
1610 escputs(bstr("subject"));
1611 wprintf("\" size=40 maxlength=70>"
1615 wprintf("<input type=\"submit\" name=\"sc\" value=\"");
1616 if (strlen(bstr("recp")) > 0) {
1617 wprintf("Send message");
1619 wprintf("Post message");
1622 "<input type=\"submit\" name=\"sc\" value=\"Cancel\">\n");
1624 /* begin richedit box */
1625 wprintf("<div style=\"position:absolute; left:0%%; width:100%%; top:15%; height:80%%\">\n");
1627 wprintf("<script type=\"text/javascript\" "
1628 "src=\"static/richtext.js\"></script>\n"
1629 "<script type=\"text/javascript\">\n"
1630 "function submitForm() { \n"
1631 " updateRTE('msgtext'); \n"
1635 "initRTE(\"static/\", \"static/\", \"\"); \n"
1637 "<noscript>JAVAscript MUST BE ENABLED.</noscript> \n"
1638 "<script type=\"text/javascript\"> \n"
1639 "writeRichText('msgtext', '");
1640 msgescputs(bstr("msgtext"));
1641 wprintf("', '100%%', '80%%', true, false); \n"
1643 wprintf("</div>\n"); /* end richedit box */
1645 /* Here comes the "do attachments" section on the bottom */
1646 wprintf("<div style=\"position:absolute; bottom:0px; left:0px; width:100%%\">\n");
1648 /* Enumerate any attachments which are already in place... */
1649 for (att = WC->first_attachment; att != NULL; att = att->next) {
1650 wprintf("<IMG SRC=\"/static/attachment.gif\" "
1651 "BORDER=0 ALIGN=MIDDLE> Attachment: ");
1652 escputs(att->filename);
1653 wprintf(" (%s, %d bytes)<br />\n",
1654 att->content_type, att->length);
1657 /* Now offer the ability to attach additional files... */
1658 wprintf(" "
1659 "Attach file: <input NAME=\"attachfile\" "
1660 "SIZE=48 TYPE=\"file\">\n "
1661 "<input type=\"submit\" name=\"attach\" value=\"Add\">\n");
1663 wprintf("</div>\n"); /* end attachments section */
1665 wprintf("</FORM>\n");
1667 wprintf("</div>\n");
1668 DONE: wDumpContent(1);
1678 void delete_msg(void)
1683 msgid = atol(bstr("msgid"));
1685 output_headers(1, 1, 1, 0, 0, 0, 0);
1687 sprintf(buf, "DELE %ld", msgid);
1690 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1699 * Confirm move of a message
1701 void confirm_move_msg(void)
1707 msgid = atol(bstr("msgid"));
1709 output_headers(1, 1, 1, 0, 0, 0, 0);
1711 wprintf("<center><table width=99%% border=0 bgcolor=\"#444455\"><tr><td>");
1712 wprintf("<font size=+1 color=\"#ffffff\"");
1713 wprintf("<b>Confirm move of message</b>\n");
1714 wprintf("</font></td></tr></table></center>\n");
1716 wprintf("<CENTER>");
1718 wprintf("Move this message to:<br />\n");
1720 wprintf("<FORM METHOD=\"POST\" ACTION=\"/move_msg\">\n");
1721 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
1725 wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1728 if (buf[0] == '1') {
1729 while (serv_gets(buf), strcmp(buf, "000")) {
1730 extract(targ, buf, 0);
1731 wprintf("<OPTION>");
1736 wprintf("</SELECT>\n");
1737 wprintf("<br />\n");
1739 wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
1741 wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
1742 wprintf("</FORM></CENTER>\n");
1744 wprintf("</CENTER>\n");
1755 msgid = atol(bstr("msgid"));
1757 output_headers(1, 1, 1, 0, 0, 0, 0);
1759 if (!strcasecmp(bstr("yesno"), "Move")) {
1760 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1763 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1765 wprintf("<EM>Message not moved.</EM><br />\n");
1772 * This gets called when a user selects Reply/Move/Del etc. on *one* message.
1774 void do_stuff_to_one_msg(void) {
1777 msg_oper = bstr("msg_oper");
1779 if (!strcasecmp(msg_oper, "Delete")) {
1780 delete_msg(); /* It's already been confirmed using JS */
1783 if (!strcasecmp(msg_oper, "Move")) {
1787 if (!strcasecmp(msg_oper, "Reply")) {
1788 display_enter(); /* recp and subject already set */
1792 /* should never get here. FIXME: display an error */
1798 * This gets called when a user selects multiple messages in a summary
1799 * list and then clicks to perform a transformation of some sort on them
1800 * (such as deleting them).
1802 void do_stuff_to_msgs(void) {
1807 struct stuff_t *next;
1811 struct stuff_t *stuff = NULL;
1812 struct stuff_t *ptr;
1815 serv_puts("MSGS ALL");
1818 if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
1819 ptr = malloc(sizeof(struct stuff_t));
1820 ptr->msgnum = atol(buf);
1825 strcpy(sc, bstr("sc"));
1827 while (stuff != NULL) {
1829 sprintf(buf, "msg_%ld", stuff->msgnum);
1830 if (!strcasecmp(bstr(buf), "yes")) {
1832 if (!strcasecmp(sc, "Delete selected")) {
1833 serv_printf("DELE %ld", stuff->msgnum);
1844 readloop("readfwd");