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("<div id=\"fix_scrollbar_bug\">\n");
379 wprintf("<table width=100%% border=1 cellspacing=0 "
380 "cellpadding=0><TR><TD>\n");
382 /* begin message header table */
383 wprintf("<TABLE WIDTH=100%% BORDER=0 CELLSPACING=0 "
384 "CELLPADDING=1 BGCOLOR=\"#CCCCCC\"><TR><TD>\n");
386 wprintf("<SPAN CLASS=\"message_header\">");
387 strcpy(m_subject, "");
389 while (serv_gets(buf), strcasecmp(buf, "text")) {
390 if (!strcmp(buf, "000")) {
391 wprintf("<I>unexpected end of message</I><br /><br />\n");
392 wprintf("</SPAN>\n");
395 if (!strncasecmp(buf, "nhdr=yes", 8))
399 if (!strncasecmp(buf, "type=", 5))
400 format_type = atoi(&buf[5]);
401 if (!strncasecmp(buf, "from=", 5)) {
402 strcpy(from, &buf[5]);
403 wprintf("from <A HREF=\"/showuser&who=");
409 if (!strncasecmp(buf, "subj=", 5))
410 strcpy(m_subject, &buf[5]);
411 if ((!strncasecmp(buf, "hnod=", 5))
412 && (strcasecmp(&buf[5], serv_info.serv_humannode)))
413 wprintf("(%s) ", &buf[5]);
414 if ((!strncasecmp(buf, "room=", 5))
415 && (strcasecmp(&buf[5], WC->wc_roomname))
416 && (strlen(&buf[5])>0) )
417 wprintf("in %s> ", &buf[5]);
418 if (!strncasecmp(buf, "rfca=", 5)) {
419 strcpy(rfca, &buf[5]);
425 if (!strncasecmp(buf, "node=", 5)) {
426 strcpy(node, &buf[5]);
427 if ( ((WC->room_flags & QR_NETWORK)
428 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
429 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
432 wprintf("@%s ", &buf[5]);
435 if (!strncasecmp(buf, "rcpt=", 5))
436 wprintf("to %s ", &buf[5]);
437 if (!strncasecmp(buf, "time=", 5)) {
438 fmt_date(now, atol(&buf[5]));
442 if (!strncasecmp(buf, "part=", 5)) {
443 extract(mime_filename, &buf[5], 1);
444 extract(mime_partnum, &buf[5], 2);
445 extract(mime_disposition, &buf[5], 3);
446 extract(mime_content_type, &buf[5], 4);
447 mime_length = extract_int(&buf[5], 5);
449 if (!strcasecmp(mime_disposition, "attachment")) {
450 snprintf(&mime_http[strlen(mime_http)],
451 (sizeof(mime_http) - strlen(mime_http) - 1),
452 "<A HREF=\"/output_mimepart?"
453 "msgnum=%ld&partnum=%s\" "
454 "TARGET=\"wc.%ld.%s\">"
455 "<IMG SRC=\"/static/attachment.gif\" "
456 "BORDER=0 ALIGN=MIDDLE>\n"
457 "Part %s: %s (%s, %d bytes)</A><br />\n",
458 msgnum, mime_partnum,
459 msgnum, mime_partnum,
460 mime_partnum, mime_filename,
461 mime_content_type, mime_length);
464 if ((!strcasecmp(mime_disposition, "inline"))
465 && (!strncasecmp(mime_content_type, "image/", 6)) ){
466 snprintf(&mime_http[strlen(mime_http)],
467 (sizeof(mime_http) - strlen(mime_http) - 1),
468 "<IMG SRC=\"/output_mimepart?"
469 "msgnum=%ld&partnum=%s\">",
470 msgnum, mime_partnum);
473 /*** begin handler prep ***/
474 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
475 strcpy(vcard_partnum, mime_partnum);
478 if (!strcasecmp(mime_content_type, "text/calendar")) {
479 strcpy(cal_partnum, mime_partnum);
482 /*** end handler prep ***/
488 /* Generate a reply-to address */
489 if (strlen(rfca) > 0) {
490 strcpy(reply_to, rfca);
493 if ( (strlen(node) > 0)
494 && (strcasecmp(node, serv_info.serv_nodename))
495 && (strcasecmp(node, serv_info.serv_humannode)) ) {
496 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
500 snprintf(reply_to, sizeof(reply_to), "%s", from);
509 if (strlen(m_subject) > 0) {
511 "<SPAN CLASS=\"message_subject\">"
518 /* start msg buttons */
519 wprintf("<TD ALIGN=RIGHT>\n");
522 wprintf("<a href=\"/display_enter?recp=");
523 urlescputs(reply_to);
524 wprintf("&subject=");
525 if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%20");
526 urlescputs(m_subject);
527 wprintf("\">Reply</a> ");
529 if (WC->is_room_aide) {
532 wprintf("<a href=\"/confirm_move_msg?msgid=%ld\">Move </a>",
536 wprintf("<a href=\"/delete_msg?msgid=%ld\" "
537 "onClick=\"return confirm('Delete this message?');\">"
538 "Delete</a>", msgnum);
542 wprintf("</TD></TR></TABLE>\n");
545 wprintf("<TABLE BORDER=0 WIDTH=100%% BGCOLOR=#FFFFFF "
546 "CELLPADDING=1 CELLSPACING=0><TR><TD>");
549 * Learn the content type
551 strcpy(mime_content_type, "text/plain");
552 while (serv_gets(buf), (strlen(buf) > 0)) {
553 if (!strcmp(buf, "000")) {
554 wprintf("<I>unexpected end of message</I><br /><br />\n");
557 if (!strncasecmp(buf, "Content-type: ", 14)) {
558 safestrncpy(mime_content_type, &buf[14],
559 sizeof(mime_content_type));
563 /* Messages in legacy Citadel variformat get handled thusly... */
564 if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
565 fmout(NULL, "JUSTIFY");
568 /* Boring old 80-column fixed format text gets handled this way... */
569 else if (!strcasecmp(mime_content_type, "text/plain")) {
570 while (serv_gets(buf), strcmp(buf, "000")) {
571 if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
572 if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
573 while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
574 buf[strlen(buf) - 1] = 0;
576 ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) || (!strncmp(buf, " :-)", 4)))) {
577 wprintf("<BLOCKQUOTE>");
579 } else if ((bq == 1) &&
580 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) && (strncmp(buf, " :-)", 4))) {
581 wprintf("</BLOCKQUOTE>");
587 wprintf("</TT><br />\n");
589 wprintf("</I><br />");
592 else /* HTML is fun, but we've got to strip it first */
593 if (!strcasecmp(mime_content_type, "text/html")) {
597 /* Unknown weirdness */
599 wprintf("I don't know how to display %s<br />\n",
601 while (serv_gets(buf), strcmp(buf, "000")) { }
605 /* Afterwards, offer links to download attachments 'n' such */
606 if (strlen(mime_http) > 0) {
607 wprintf("%s", mime_http);
610 /* Handler for vCard parts */
611 if (strlen(vcard_partnum) > 0) {
612 part_source = load_mimepart(msgnum, vcard_partnum);
613 if (part_source != NULL) {
615 /* If it's my vCard I can edit it */
616 if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
617 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
618 wprintf("<A HREF=\"/edit_vcard?"
619 "msgnum=%ld&partnum=%s\">",
620 msgnum, vcard_partnum);
621 wprintf("(edit)</A>");
624 /* In all cases, display the full card */
625 display_vcard(part_source, 0, 1, NULL);
629 /* Handler for calendar parts */
630 if (strlen(cal_partnum) > 0) {
631 part_source = load_mimepart(msgnum, cal_partnum);
632 if (part_source != NULL) {
633 cal_process_attachment(part_source,
634 msgnum, cal_partnum);
644 wprintf("</TD></TR></TABLE>\n");
646 /* end everythingamundo table */
647 wprintf("</TD></TR></TABLE>\n");
648 wprintf("</div><br />\n");
652 void summarize_message(long msgnum, int is_new) {
663 memset(&summ, 0, sizeof(summ));
664 strcpy(summ.subj, "(no subject)");
666 sprintf(buf, "MSG0 %ld|3", msgnum); /* ask for headers only with no MIME */
669 if (buf[0] != '1') return;
671 while (serv_gets(buf), strcmp(buf, "000")) {
672 if (!strncasecmp(buf, "from=", 5)) {
673 strcpy(summ.from, &buf[5]);
675 if (!strncasecmp(buf, "subj=", 5)) {
676 strcpy(summ.subj, &buf[5]);
678 if (!strncasecmp(buf, "rfca=", 5)) {
679 strcat(summ.from, " <");
680 strcat(summ.from, &buf[5]);
681 strcat(summ.from, ">");
684 if (!strncasecmp(buf, "node=", 5)) {
685 if ( ((WC->room_flags & QR_NETWORK)
686 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
687 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
689 strcat(summ.from, " @ ");
690 strcat(summ.from, &buf[5]);
694 if (!strncasecmp(buf, "rcpt=", 5)) {
695 strcpy(summ.to, &buf[5]);
698 if (!strncasecmp(buf, "time=", 5)) {
699 fmt_date(summ.date, atol(&buf[5]));
704 if (is_new) wprintf("<B>");
705 wprintf("<A HREF=\"/readfwd?startmsg=%ld"
706 "&maxmsgs=1&summary=0\">",
710 if (is_new) wprintf("</B>");
711 wprintf("</TD><TD>");
712 if (is_new) wprintf("<B>");
714 if (is_new) wprintf("</B>");
715 wprintf(" </TD><TD>");
716 if (is_new) wprintf("<B>");
718 if (is_new) wprintf("</B>");
721 "<INPUT TYPE=\"checkbox\" NAME=\"msg_%ld\" VALUE=\"yes\">"
729 void display_addressbook(long msgnum, char alpha) {
731 char mime_partnum[SIZ];
732 char mime_filename[SIZ];
733 char mime_content_type[SIZ];
734 char mime_disposition[SIZ];
736 char vcard_partnum[SIZ];
737 char *vcard_source = NULL;
747 memset(&summ, 0, sizeof(summ));
748 strcpy(summ.subj, "(no subject)");
750 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
753 if (buf[0] != '1') return;
755 while (serv_gets(buf), strcmp(buf, "000")) {
756 if (!strncasecmp(buf, "part=", 5)) {
757 extract(mime_filename, &buf[5], 1);
758 extract(mime_partnum, &buf[5], 2);
759 extract(mime_disposition, &buf[5], 3);
760 extract(mime_content_type, &buf[5], 4);
761 mime_length = extract_int(&buf[5], 5);
763 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
764 strcpy(vcard_partnum, mime_partnum);
770 if (strlen(vcard_partnum) > 0) {
771 vcard_source = load_mimepart(msgnum, vcard_partnum);
772 if (vcard_source != NULL) {
774 /* Display the summary line */
775 display_vcard(vcard_source, alpha, 0, NULL);
777 /* If it's my vCard I can edit it */
778 if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
779 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
780 wprintf("<A HREF=\"/edit_vcard?"
781 "msgnum=%ld&partnum=%s\">",
782 msgnum, vcard_partnum);
783 wprintf("(edit)</A>");
794 /* If it's an old "Firstname Lastname" style record, try to
797 void lastfirst_firstlast(char *namebuf) {
802 if (namebuf == NULL) return;
803 if (strchr(namebuf, ';') != NULL) return;
805 i = num_tokens(namebuf, ' ');
808 extract_token(lastname, namebuf, i-1, ' ');
809 remove_token(namebuf, i-1, ' ');
810 strcpy(firstname, namebuf);
811 sprintf(namebuf, "%s; %s", lastname, firstname);
815 void fetch_ab_name(long msgnum, char *namebuf) {
817 char mime_partnum[SIZ];
818 char mime_filename[SIZ];
819 char mime_content_type[SIZ];
820 char mime_disposition[SIZ];
822 char vcard_partnum[SIZ];
823 char *vcard_source = NULL;
834 if (namebuf == NULL) return;
837 memset(&summ, 0, sizeof(summ));
838 strcpy(summ.subj, "(no subject)");
840 sprintf(buf, "MSG0 %ld|1", msgnum); /* ask for headers only */
843 if (buf[0] != '1') return;
845 while (serv_gets(buf), strcmp(buf, "000")) {
846 if (!strncasecmp(buf, "part=", 5)) {
847 extract(mime_filename, &buf[5], 1);
848 extract(mime_partnum, &buf[5], 2);
849 extract(mime_disposition, &buf[5], 3);
850 extract(mime_content_type, &buf[5], 4);
851 mime_length = extract_int(&buf[5], 5);
853 if (!strcasecmp(mime_content_type, "text/x-vcard")) {
854 strcpy(vcard_partnum, mime_partnum);
860 if (strlen(vcard_partnum) > 0) {
861 vcard_source = load_mimepart(msgnum, vcard_partnum);
862 if (vcard_source != NULL) {
864 /* Grab the name off the card */
865 display_vcard(vcard_source, 0, 0, namebuf);
871 lastfirst_firstlast(namebuf);
873 for (i=0; i<strlen(namebuf); ++i) {
874 if (namebuf[i] != ';') return;
876 strcpy(namebuf, "(no name)");
882 * Record compare function for sorting address book indices
884 int abcmp(const void *ab1, const void *ab2) {
886 (((const struct addrbookent *)ab1)->ab_name),
887 (((const struct addrbookent *)ab2)->ab_name)
893 * Helper function for do_addrbook_view()
894 * Converts a name into a three-letter tab label
896 void nametab(char *tabbuf, char *name) {
897 stresc(tabbuf, name, 0, 0);
898 tabbuf[0] = toupper(tabbuf[0]);
899 tabbuf[1] = tolower(tabbuf[1]);
900 tabbuf[2] = tolower(tabbuf[2]);
906 * Render the address book using info we gathered during the scan
908 void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
912 static int NAMESPERPAGE = 60;
916 char tabfirst_label[SIZ];
918 char tablast_label[SIZ];
921 wprintf("<I>This address book is empty.</I>\n");
926 qsort(addrbook, num_ab, sizeof(struct addrbookent), abcmp);
929 num_pages = num_ab / NAMESPERPAGE;
931 page = atoi(bstr("page"));
934 for (i=0; i<=num_pages; ++i) {
936 wprintf("<A HREF=\"/readfwd?page=%d\">", i);
941 tabfirst = i * NAMESPERPAGE;
942 tablast = tabfirst + NAMESPERPAGE - 1;
943 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
944 nametab(tabfirst_label, addrbook[tabfirst].ab_name);
945 nametab(tablast_label, addrbook[tablast].ab_name);
946 wprintf("[%s - %s]",
947 tabfirst_label, tablast_label
958 wprintf("<TABLE border=0 cellspacing=0 "
959 "cellpadding=3 width=100%%>\n"
962 for (i=0; i<num_ab; ++i) {
964 if ((i / NAMESPERPAGE) == page) {
966 if ((displayed % 4) == 0) {
971 wprintf("<TR BGCOLOR=\"#%s\">",
972 (bg ? "DDDDDD" : "FFFFFF")
978 wprintf("<A HREF=\"/readfwd?startmsg=%ld&is_singlecard=1",
979 addrbook[i].ab_msgnum);
980 wprintf("&maxmsgs=1&summary=0&alpha=%s\">", bstr("alpha"));
981 escputs(addrbook[i].ab_name);
982 wprintf("</A></TD>\n");
987 wprintf("</TR></TABLE>\n");
993 * load message pointers from the server
995 int load_msg_ptrs(char *servcmd)
1002 maxload = sizeof(WC->msgarr) / sizeof(long) ;
1005 if (buf[0] != '1') {
1006 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1009 while (serv_gets(buf), strcmp(buf, "000")) {
1010 if (nummsgs < maxload) {
1011 WC->msgarr[nummsgs] = atol(buf);
1020 * command loop for reading messages
1022 void readloop(char *oper)
1032 int num_displayed = 0;
1034 int is_addressbook = 0;
1035 int is_singlecard = 0;
1036 int is_calendar = 0;
1039 int remaining_messages;
1041 int lowest_displayed = (-1);
1042 int highest_displayed = 0;
1043 long pn_previous = 0L;
1044 long pn_current = 0L;
1047 struct addrbookent *addrbook = NULL;
1050 startmsg = atol(bstr("startmsg"));
1051 maxmsgs = atoi(bstr("maxmsgs"));
1052 is_summary = atoi(bstr("summary"));
1053 if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
1055 output_headers(1, 1, 1, 0, 0, 0, 0);
1057 /* When in summary mode, always show ALL messages instead of just
1058 * new or old. Otherwise, show what the user asked for.
1060 if (!strcmp(oper, "readnew")) {
1061 strcpy(cmd, "MSGS NEW");
1063 else if (!strcmp(oper, "readold")) {
1064 strcpy(cmd, "MSGS OLD");
1067 strcpy(cmd, "MSGS ALL");
1070 if ((WC->wc_view == VIEW_MAILBOX) && (maxmsgs > 1)) {
1072 strcpy(cmd, "MSGS ALL");
1075 if ((WC->wc_view == VIEW_ADDRESSBOOK) && (maxmsgs > 1)) {
1077 strcpy(cmd, "MSGS ALL");
1082 strcpy(cmd, "MSGS ALL");
1085 /* Are we doing a summary view? If so, we need to know old messages
1086 * and new messages, so we can do that pretty boldface thing for the
1089 strcpy(old_msgs, "");
1093 if (buf[0] == '2') {
1094 strcpy(old_msgs, &buf[4]);
1098 is_singlecard = atoi(bstr("is_singlecard"));
1100 if (WC->wc_view == VIEW_CALENDAR) { /* calendar */
1102 strcpy(cmd, "MSGS ALL");
1105 if (WC->wc_view == VIEW_TASKS) { /* tasks */
1107 strcpy(cmd, "MSGS ALL");
1110 if (WC->wc_view == VIEW_NOTES) { /* notes */
1112 strcpy(cmd, "MSGS ALL");
1116 nummsgs = load_msg_ptrs(cmd);
1119 if ((!is_tasks) && (!is_calendar) && (!is_notes)) {
1120 if (!strcmp(oper, "readnew")) {
1121 wprintf("<EM>No new messages.</EM>\n");
1122 } else if (!strcmp(oper, "readold")) {
1123 wprintf("<EM>No old messages.</EM>\n");
1125 wprintf("<EM>No messages here.</EM>\n");
1132 if (startmsg == 0L) startmsg = WC->msgarr[0];
1133 remaining_messages = 0;
1135 for (a = 0; a < nummsgs; ++a) {
1136 if (WC->msgarr[a] >= startmsg) {
1137 ++remaining_messages;
1141 wprintf("<form name=\"msgomatic\" "
1142 "METHOD=\"POST\" ACTION=\"/do_stuff_to_msgs\">\n");
1144 wprintf("<div id=\"fix_scrollbar_bug\">"
1145 "<table border=0 cellspacing=0 "
1146 "cellpadding=0 width=100%%>\n"
1148 "<TD><I>Subject</I></TD>"
1149 "<TD><I>Sender</I></TD>"
1150 "<TD><I>Date</I></TD>"
1156 for (a = 0; a < nummsgs; ++a) {
1157 if ((WC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
1159 /* Are you a new message, or an old message? */
1162 if (is_msg_in_mset(old_msgs, WC->msgarr[a])) {
1170 /* Learn which msgs "Prev" & "Next" buttons go to */
1171 pn_current = WC->msgarr[a];
1172 if (a > 0) pn_previous = WC->msgarr[a-1];
1173 if (a < (nummsgs-1)) pn_next = WC->msgarr[a+1];
1175 /* If a tabular view, set up the line */
1178 wprintf("<TR BGCOLOR=\"#%s\">",
1179 (bg ? "DDDDDD" : "FFFFFF")
1183 /* Display the message */
1185 summarize_message(WC->msgarr[a], is_new);
1187 else if (is_addressbook) {
1188 fetch_ab_name(WC->msgarr[a], buf);
1190 addrbook = realloc(addrbook,
1191 (sizeof(struct addrbookent) * num_ab) );
1192 safestrncpy(addrbook[num_ab-1].ab_name, buf,
1193 sizeof(addrbook[num_ab-1].ab_name));
1194 addrbook[num_ab-1].ab_msgnum = WC->msgarr[a];
1196 else if (is_calendar) {
1197 display_calendar(WC->msgarr[a]);
1199 else if (is_tasks) {
1200 display_task(WC->msgarr[a]);
1202 else if (is_notes) {
1203 display_note(WC->msgarr[a]);
1206 read_message(WC->msgarr[a]);
1209 /* If a tabular view, finish the line */
1214 if (lowest_displayed < 0) lowest_displayed = a;
1215 highest_displayed = a;
1218 --remaining_messages;
1223 wprintf("</table></div>\n");
1226 /* Bump these because although we're thinking in zero base, the user
1227 * is a drooling idiot and is thinking in one base.
1230 ++highest_displayed;
1232 /* If we're only looking at one message, do a prev/next thing */
1233 if (num_displayed == 1) {
1234 if ((!is_tasks) && (!is_calendar) && (!is_addressbook) && (!is_notes) && (!is_singlecard)) {
1236 wprintf("<div id=\"fix_scrollbar_bug\">"
1237 "<table border=0 width=100%% bgcolor=\"#dddddd\"><tr><td>"
1238 "Reading #%d of %d messages.</TD>\n"
1239 "<TD ALIGN=RIGHT><FONT SIZE=+1>",
1240 lowest_displayed, nummsgs);
1243 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1244 "VALUE=\"Delete selected\">\n");
1247 if (pn_previous > 0L) {
1248 wprintf("<A HREF=\"/%s"
1258 wprintf("<A HREF=\"/%s"
1267 wprintf("<A HREF=\"/%s?startmsg=%ld"
1268 "&maxmsgs=999999&summary=1\">"
1274 wprintf("</td></tr></table></div>\n");
1279 * If we're not currently looking at ALL requested
1280 * messages, then display the selector bar
1282 if (num_displayed > 1) {
1283 if ((!is_tasks) && (!is_calendar) && (!is_addressbook)
1284 && (!is_notes) && (!is_singlecard)) {
1286 wprintf("Reading #", lowest_displayed, highest_displayed);
1288 wprintf("<SELECT NAME=\"whichones\" SIZE=\"1\" "
1289 "OnChange=\"location.href=msgomatic.whichones.options"
1290 "[selectedIndex].value\">\n");
1292 for (b=0; b<nummsgs; b = b + maxmsgs) {
1295 if (hi > nummsgs) hi = nummsgs;
1296 wprintf("<OPTION %s VALUE="
1301 "%d-%d</OPTION> \n",
1302 ((WC->msgarr[b] == startmsg) ? "SELECTED" : ""),
1309 wprintf("<OPTION VALUE=\"/%s?startmsg=%ld"
1310 "&maxmsgs=999999&summary=%d\">"
1314 WC->msgarr[0], is_summary);
1316 wprintf("</SELECT> of %d messages.", nummsgs);
1319 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
1320 "VALUE=\"Delete selected\">\n");
1325 wprintf("</form>\n");
1329 do_tasks_view(); /* Render the task list */
1333 do_calendar_view(); /* Render the calendar */
1336 if (is_addressbook) {
1337 do_addrbook_view(addrbook, num_ab); /* Render the address book */
1341 if (addrbook != NULL) free(addrbook);
1346 * Back end for post_message() ... this is where the actual message
1347 * gets transmitted to the server.
1349 void post_mime_to_server(void) {
1351 int is_multipart = 0;
1353 struct wc_attachment *att;
1355 size_t encoded_length;
1357 /* If there are attachments, we have to do multipart/mixed */
1358 if (WC->first_attachment != NULL) {
1363 sprintf(boundary, "---Citadel-Multipart-%s-%04x%04x---",
1364 serv_info.serv_fqdn,
1369 /* Remember, serv_printf() appends an extra newline */
1370 serv_printf("Content-type: multipart/mixed; "
1371 "boundary=\"%s\"\n", boundary);
1372 serv_printf("This is a multipart message in MIME format.\n");
1373 serv_printf("--%s", boundary);
1376 serv_puts("Content-type: text/html");
1378 serv_puts("<HTML><BODY>\n");
1379 text_to_server(bstr("msgtext"), 0);
1380 serv_puts("</BODY></HTML>\n");
1385 /* Add in the attachments */
1386 for (att = WC->first_attachment; att!=NULL; att=att->next) {
1388 encoded_length = ((att->length * 150) / 100);
1389 encoded = malloc(encoded_length);
1390 if (encoded == NULL) break;
1391 CtdlEncodeBase64(encoded, att->data, att->length);
1393 serv_printf("--%s", boundary);
1394 serv_printf("Content-type: %s", att->content_type);
1395 serv_printf("Content-disposition: attachment; "
1396 "filename=\"%s\"", att->filename);
1397 serv_puts("Content-transfer-encoding: base64");
1399 serv_write(encoded, strlen(encoded));
1404 serv_printf("--%s--", boundary);
1412 * Post message (or don't post message)
1414 * Note regarding the "dont_post" variable:
1415 * A random value (actually, it's just a timestamp) is inserted as a hidden
1416 * field called "postseq" when the display_enter page is generated. This
1417 * value is checked when posting, using the static variable dont_post. If a
1418 * user attempts to post twice using the same dont_post value, the message is
1419 * discarded. This prevents the accidental double-saving of the same message
1420 * if the user happens to click the browser "back" button.
1422 void post_message(void)
1425 static long dont_post = (-1L);
1426 struct wc_attachment *att, *aptr;
1428 if (WC->upload_length > 0) {
1430 /* There's an attachment. Save it to this struct... */
1431 att = malloc(sizeof(struct wc_attachment));
1432 memset(att, 0, sizeof(struct wc_attachment));
1433 att->length = WC->upload_length;
1434 strcpy(att->content_type, WC->upload_content_type);
1435 strcpy(att->filename, WC->upload_filename);
1438 /* And add it to the list. */
1439 if (WC->first_attachment == NULL) {
1440 WC->first_attachment = att;
1443 aptr = WC->first_attachment;
1444 while (aptr->next != NULL) aptr = aptr->next;
1448 /* Netscape sends a simple filename, which is what we want,
1449 * but Satan's browser sends an entire pathname. Reduce
1450 * the path to just a filename if we need to.
1452 while (num_tokens(att->filename, '/') > 1) {
1453 remove_token(att->filename, 0, '/');
1455 while (num_tokens(att->filename, '\\') > 1) {
1456 remove_token(att->filename, 0, '\\');
1459 /* Transfer control of this memory from the upload struct
1460 * to the attachment struct.
1462 att->data = WC->upload;
1463 WC->upload_length = 0;
1469 if (!strcasecmp(bstr("sc"), "Cancel")) {
1470 sprintf(WC->ImportantMessage,
1471 "Cancelled. Message was not posted.");
1472 } else if (!strcasecmp(bstr("attach"), "Add")) {
1475 } else if (atol(bstr("postseq")) == dont_post) {
1476 sprintf(WC->ImportantMessage,
1477 "Automatically cancelled because you have already "
1478 "saved this message.");
1480 sprintf(buf, "ENT0 1|%s|0|4|%s",
1485 if (buf[0] == '4') {
1486 post_mime_to_server();
1487 if (strlen(bstr("recp")) > 0) {
1488 sprintf(WC->ImportantMessage, "Message has been sent.\n");
1491 sprintf(WC->ImportantMessage, "Message has been posted.\n");
1493 dont_post = atol(bstr("postseq"));
1495 sprintf(WC->ImportantMessage,
1500 free_attachments(WC);
1501 readloop("readnew");
1508 * display the message entry screen
1510 void display_enter(void)
1514 struct wc_attachment *att;
1516 if (strlen(bstr("force_room")) > 0) {
1517 gotoroom(bstr("force_room"));
1520 /* Are we perhaps in an address book view? If so, then an "enter
1521 * message" command really means "add new entry."
1523 if (WC->wc_view == VIEW_ADDRESSBOOK) {
1524 do_edit_vcard(-1, "", "");
1528 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
1529 /* Are we perhaps in a calendar view? If so, then an "enter
1530 * message" command really means "add new calendar item."
1532 if (WC->wc_view == VIEW_CALENDAR) {
1533 display_edit_event();
1537 /* Are we perhaps in a tasks view? If so, then an "enter
1538 * message" command really means "add new task."
1540 if (WC->wc_view == VIEW_TASKS) {
1541 display_edit_task();
1546 /* Otherwise proceed normally. Do a custom room banner with no navbar... */
1547 output_headers(1, 1, 2, 0, 0, 0, 0);
1548 wprintf("<div id=\"banner\">\n");
1549 embed_room_banner(NULL, navbar_none);
1550 wprintf("</div>\n");
1551 wprintf("<div id=\"content\">\n"
1552 "<div id=\"fix_scrollbar_bug\">"
1553 "<table width=100%% border=0 bgcolor=\"#ffffff\"><tr><td>");
1555 sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
1559 if (!strncmp(buf, "570", 3)) {
1560 if (strlen(bstr("recp")) > 0) {
1561 svprintf("RECPERROR", WCS_STRING,
1562 "<SPAN CLASS=\"errormsg\">%s</SPAN><br />\n",
1566 do_template("prompt_for_recipient");
1569 if (buf[0] != '2') {
1570 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1576 strcat(&buf[strlen(buf)], " <I>from</I> ");
1577 stresc(&buf[strlen(buf)], WC->wc_username, 1, 1);
1578 if (strlen(bstr("recp")) > 0) {
1579 strcat(&buf[strlen(buf)], " <I>to</I> ");
1580 stresc(&buf[strlen(buf)], bstr("recp"), 1, 1);
1582 strcat(&buf[strlen(buf)], " <I>in</I> ");
1583 stresc(&buf[strlen(buf)], WC->wc_roomname, 1, 1);
1585 /* begin message entry screen */
1586 // wprintf("<div style=\"position:absolute; left:1%%; width:96%%; height:100%%\">\n");
1588 wprintf("<form enctype=\"multipart/form-data\" "
1589 "method=\"POST\" action=\"/post\" "
1590 "name=\"enterform\""
1591 "onSubmit=\"return submitForm();\""
1593 wprintf("<input type=\"hidden\" name=\"recp\" value=\"%s\">\n",
1595 wprintf("<input type=\"hidden\" name=\"postseq\" value=\"%ld\">\n",
1598 wprintf("%s<br>\n", buf); /* header bar */
1599 wprintf("<img src=\"static/enter.gif\" align=middle alt=\" \">");
1600 /* "onLoad=\"document.enterform.msgtext.focus();\" " */
1601 wprintf("<font size=-1>Subject (optional):</font>"
1602 "<input type=\"text\" name=\"subject\" value=\"");
1603 escputs(bstr("subject"));
1604 wprintf("\" size=40 maxlength=70>"
1608 wprintf("<input type=\"submit\" name=\"sc\" value=\"");
1609 if (strlen(bstr("recp")) > 0) {
1610 wprintf("Send message");
1612 wprintf("Post message");
1615 "<input type=\"submit\" name=\"sc\" value=\"Cancel\">\n");
1617 wprintf("<center><script type=\"text/javascript\" "
1618 "src=\"static/richtext.js\"></script>\n"
1619 "<script type=\"text/javascript\">\n"
1620 "function submitForm() { \n"
1621 " updateRTE('msgtext'); \n"
1625 "initRTE(\"static/\", \"static/\", \"\"); \n"
1627 "<noscript>JavaScript must be enabled.</noscript> \n"
1628 "<script type=\"text/javascript\"> \n"
1629 "writeRichText('msgtext', '");
1630 msgescputs(bstr("msgtext"));
1631 wprintf("', '96%%', '200', true, false); \n"
1632 "</script></center><br />\n");
1634 /* Enumerate any attachments which are already in place... */
1635 wprintf("<img src=\"/static/attachment.gif\" border=0 "
1636 "align=middle height=16 width=16> Attachments: ");
1637 wprintf("<select name=\"which_attachment\" size=1>");
1638 for (att = WC->first_attachment; att != NULL; att = att->next) {
1639 wprintf("<option value=\"");
1640 urlescputs(att->filename);
1642 escputs(att->filename);
1643 /* wprintf(" (%s, %d bytes)",att->content_type,att->length); */
1644 wprintf("</option>\n");
1646 wprintf("</select>");
1648 /* Now offer the ability to attach additional files... */
1649 wprintf(" "
1650 "Attach file: <input NAME=\"attachfile\" "
1651 "SIZE=16 TYPE=\"file\">\n "
1652 "<input type=\"submit\" name=\"attach\" value=\"Add\">\n");
1654 wprintf("</form>\n");
1656 wprintf("</td></tr></table></div>\n");
1657 DONE: wDumpContent(1);
1667 void delete_msg(void)
1672 msgid = atol(bstr("msgid"));
1674 output_headers(1, 1, 1, 0, 0, 0, 0);
1676 sprintf(buf, "DELE %ld", msgid);
1679 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1688 * Confirm move of a message
1690 void confirm_move_msg(void)
1696 msgid = atol(bstr("msgid"));
1698 output_headers(1, 1, 1, 0, 0, 0, 0);
1700 wprintf("<div id=\"fix_scrollbar_bug\">"
1701 "<table width=100%% border=0 bgcolor=\"#444455\"><tr><td>");
1702 wprintf("<font size=+1 color=\"#ffffff\"");
1703 wprintf("<b>Confirm move of message</b>\n");
1704 wprintf("</font></td></tr></table></div>\n");
1706 wprintf("<CENTER>");
1708 wprintf("Move this message to:<br />\n");
1710 wprintf("<form METHOD=\"POST\" ACTION=\"/move_msg\">\n");
1711 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n",
1715 wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1718 if (buf[0] == '1') {
1719 while (serv_gets(buf), strcmp(buf, "000")) {
1720 extract(targ, buf, 0);
1721 wprintf("<OPTION>");
1726 wprintf("</SELECT>\n");
1727 wprintf("<br />\n");
1729 wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Move\">");
1731 wprintf("<INPUT TYPE=\"submit\" NAME=\"yesno\" VALUE=\"Cancel\">");
1732 wprintf("</form></CENTER>\n");
1734 wprintf("</CENTER>\n");
1745 msgid = atol(bstr("msgid"));
1747 output_headers(1, 1, 1, 0, 0, 0, 0);
1749 if (!strcasecmp(bstr("yesno"), "Move")) {
1750 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1753 wprintf("<EM>%s</EM><br />\n", &buf[4]);
1755 wprintf("<EM>Message not moved.</EM><br />\n");
1762 * This gets called when a user selects multiple messages in a summary
1763 * list and then clicks to perform a transformation of some sort on them
1764 * (such as deleting them).
1766 void do_stuff_to_msgs(void) {
1771 struct stuff_t *next;
1775 struct stuff_t *stuff = NULL;
1776 struct stuff_t *ptr;
1779 serv_puts("MSGS ALL");
1782 if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
1783 ptr = malloc(sizeof(struct stuff_t));
1784 ptr->msgnum = atol(buf);
1789 strcpy(sc, bstr("sc"));
1791 while (stuff != NULL) {
1793 sprintf(buf, "msg_%ld", stuff->msgnum);
1794 if (!strcasecmp(bstr(buf), "yes")) {
1796 if (!strcasecmp(sc, "Delete selected")) {
1797 serv_printf("DELE %ld", stuff->msgnum);
1808 readloop("readfwd");