]> code.citadel.org Git - citadel.git/blobdiff - webcit/messages.c
* All OS-level includes are now included from webcit.h instead of from
[citadel.git] / webcit / messages.c
index 370454c792a02a3eb3804417b9f1bec73d4da6a0..b79ae635e8813e72694dc1d1a8da88bc23d7282d 100644 (file)
@@ -5,29 +5,6 @@
  *
  */
 
-#include <ctype.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/socket.h>
-#include <limits.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#include <string.h>
-#include <pwd.h>
-#include <errno.h>
-#include <stdarg.h>
-#include <pthread.h>
-#include <signal.h>
-
-#ifdef HAVE_ICONV
-#include <iconv.h>
-#endif
-
 #include "webcit.h"
 #include "vcard.h"
 #include "webserver.h"
@@ -68,9 +45,9 @@ void utf8ify_rfc822_string(char *buf) {
                extract_token(encoding, start, 2, '?', sizeof encoding);
                extract_token(istr, start, 3, '?', sizeof istr);
 
-               strcpy(start, "");
-               ++end;
+               /*strcpy(start, "");
                ++end;
+               ++end;*/
 
                ibuf = malloc(1024);
                isav = ibuf;
@@ -93,6 +70,16 @@ void utf8ify_rfc822_string(char *buf) {
                        osav = obuf;
                        iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
                        osav[1024-obuflen] = 0;
+
+                       end = start;
+                       end++;
+                       strcpy(start, "");
+                       remove_token(end, 0, '?');
+                       remove_token(end, 0, '?');
+                       remove_token(end, 0, '?');
+                       remove_token(end, 0, '?');
+                       strcpy(end, &end[1]);
+
                        snprintf(newbuf, sizeof newbuf, "%s%s%s", buf, osav, end);
                        strcpy(buf, newbuf);
                        free(osav);
@@ -337,7 +324,7 @@ void display_parsed_vcard(struct vCard *v, int full) {
                                if (strlen(mailto) > 0) strcat(mailto, "<br />");
                                strcat(mailto,
                                        "<A HREF=\"/display_enter"
-                                       "?force_room=_MAIL_&recp=");
+                                       "?force_room=_MAIL_?recp=");
                                urlesc(&mailto[strlen(mailto)], thisvalue);
                                strcat(mailto, "\">");
                                urlesc(&mailto[strlen(mailto)], thisvalue);
@@ -476,7 +463,7 @@ void display_vcard(char *vcard_source, char alpha, int full, char *storename) {
 /*
  * I wanna SEE that message!
  */
-void read_message(long msgnum) {
+void read_message(long msgnum, int suppress_buttons) {
        char buf[SIZ];
        char mime_partnum[256];
        char mime_filename[256];
@@ -550,7 +537,7 @@ void read_message(long msgnum) {
                        format_type = atoi(&buf[5]);
                if (!strncasecmp(buf, "from=", 5)) {
                        strcpy(from, &buf[5]);
-                       wprintf("from <A HREF=\"/showuser&who=");
+                       wprintf("from <A HREF=\"/showuser?who=");
 #ifdef HAVE_ICONV
                        utf8ify_rfc822_string(from);
 #endif
@@ -603,7 +590,7 @@ void read_message(long msgnum) {
                                snprintf(&mime_http[strlen(mime_http)],
                                        (sizeof(mime_http) - strlen(mime_http) - 1),
                                        "<A HREF=\"/output_mimepart?"
-                                       "msgnum=%ld&partnum=%s\" "
+                                       "msgnum=%ld?partnum=%s\" "
                                        "TARGET=\"wc.%ld.%s\">"
                                        "<IMG SRC=\"/static/diskette_24x.gif\" "
                                        "BORDER=0 ALIGN=MIDDLE>\n"
@@ -619,7 +606,7 @@ void read_message(long msgnum) {
                                snprintf(&mime_http[strlen(mime_http)],
                                        (sizeof(mime_http) - strlen(mime_http) - 1),
                                        "<IMG SRC=\"/output_mimepart?"
-                                       "msgnum=%ld&partnum=%s\">",
+                                       "msgnum=%ld?partnum=%s\">",
                                        msgnum, mime_partnum);
                        }
 
@@ -672,30 +659,37 @@ void read_message(long msgnum) {
        wprintf("</TD>\n");
 
        /* start msg buttons */
-       wprintf("<TD ALIGN=RIGHT>\n");
-
-       /* Reply */
-       wprintf("<a href=\"/display_enter?recp=");
-       urlescputs(reply_to);
-       wprintf("&subject=");
-       if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%20");
-       urlescputs(m_subject);
-       wprintf("\">[Reply]</a> ");
-
-       if (WC->is_room_aide)  {
+       if (!suppress_buttons) {
+               wprintf("<td align=right>\n");
+
+               /* Reply */
+               wprintf("<a href=\"/display_enter?recp=");
+               urlescputs(reply_to);
+               wprintf("?subject=");
+               if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%20");
+               urlescputs(m_subject);
+               wprintf("\">[Reply]</a> ");
+
+               if (WC->is_room_aide)  {
+               
+                       /* Move */
+                       wprintf("<a href=\"/confirm_move_msg?msgid=%ld\">[Move]</a> ",
+                               msgnum);
        
-               /* Move */
-               wprintf("<a href=\"/confirm_move_msg?msgid=%ld\">[Move]</a> ",
-                       msgnum);
+                       /* Delete */
+                       wprintf("<a href=\"/delete_msg?msgid=%ld\" "
+                               "onClick=\"return confirm('Delete this message?');\">"
+                               "[Delete]</a> ", msgnum);
+                               
+               }
 
-               /* Delete */
-               wprintf("<a href=\"/delete_msg?msgid=%ld\" "
-                       "onClick=\"return confirm('Delete this message?');\">"
-                       "[Delete]</a>", msgnum);
-                       
+               wprintf("<a href=\"/msg?msgnum=%ld?print_it=yes\" target=\"msgloader1\">"
+                       "[Print]</a>", msgnum);
+
+               wprintf("</td>");
        }
 
-       wprintf("</TD></TR></TABLE>\n");
+       wprintf("</TR></TABLE>\n");
 
        /* Begin body */
        wprintf("<TABLE BORDER=0 WIDTH=100%% BGCOLOR=#FFFFFF "
@@ -810,7 +804,7 @@ void read_message(long msgnum) {
                                || (WC->wc_view == VIEW_ADDRESSBOOK)
                        ) {
                                wprintf("<A HREF=\"/edit_vcard?"
-                                       "msgnum=%ld&partnum=%s\">",
+                                       "msgnum=%ld?partnum=%s\">",
                                        msgnum, vcard_partnum);
                                wprintf("[edit]</A>");
                        }
@@ -849,101 +843,89 @@ ENDBODY:
 }
 
 
-void summarize_message(long msgnum, int is_new) {
-       char buf[SIZ];
-
-       struct {
-               char date[SIZ];
-               char from[SIZ];
-               char to[SIZ];
-               char subj[SIZ];
-               int hasattachments;
-       } summ;
 
-       memset(&summ, 0, sizeof(summ));
-       strcpy(summ.subj, "(no subject)");
+/*
+ * Unadorned HTML output of an individual message, suitable
+ * for placing in a hidden iframe, for printing, or whatever
+ */
+void embed_message(void) {
+       long msgnum = 0L;
+       char *sourceiframe;
+       char *targetdiv;
+       char *print_it;
 
-       /* ask for headers only with no MIME */
-       sprintf(buf, "MSG0 %ld|3", msgnum);
-       serv_puts(buf);
-       serv_getln(buf, sizeof buf);
-       if (buf[0] != '1') return;
+       msgnum = atol(bstr("msgnum"));
+       sourceiframe = bstr("sourceiframe");
+       targetdiv = bstr("targetdiv");
+       print_it = bstr("print_it");
 
-       while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
-               if (!strncasecmp(buf, "from=", 5)) {
-                       strcpy(summ.from, &buf[5]);
-               }
-               if (!strncasecmp(buf, "subj=", 5)) {
-                       if (strlen(&buf[5]) > 0) {
-                               strcpy(summ.subj, &buf[5]);
-#ifdef HAVE_ICONV
-                               /* Handle subjects with RFC2047 encoding */
-                               utf8ify_rfc822_string(summ.subj);
-#endif
-                               if (strlen(summ.subj) > 75) {
-                                       strcpy(&summ.subj[72], "...");
-                               }
-                       }
-               }
-               /* if (!strncasecmp(buf, "rfca=", 5)) {
-                       strcat(summ.from, " <");
-                       strcat(summ.from, &buf[5]);
-                       strcat(summ.from, ">");
-               } */
+       output_headers(1, 0, 0, 0, 0, 1, 0);
+       begin_burst();
 
-               if (!strncasecmp(buf, "node=", 5)) {
-                       if ( ((WC->room_flags & QR_NETWORK)
-                       || ((strcasecmp(&buf[5], serv_info.serv_nodename)
-                       && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
-                       ) {
-                               strcat(summ.from, " @ ");
-                               strcat(summ.from, &buf[5]);
-                       }
-               }
+       wprintf("<html><head>");
 
-               if (!strncasecmp(buf, "rcpt=", 5)) {
-                       strcpy(summ.to, &buf[5]);
-               }
+       /* If we're loading into a hidden iframe, chances are the caller told us
+        * about a target div somewhere that we need to copy into when we're done.
+        */
+       if (strlen(targetdiv) > 0) wprintf(
+"                                                                      \n"
+" <script type=\"text/javascript\">                                    \n"
+"      function loaded_now_copy_it() {                                 \n"
+"              parent.document.getElementById(\"%s\").innerHTML = parent.frames['%s'].document.body.innerHTML; \n"
+"      }                                                                                       \n"
+"</script>\n",
+               targetdiv,
+               sourceiframe
+       );
 
-               if (!strncasecmp(buf, "time=", 5)) {
-                       fmt_date(summ.date, atol(&buf[5]), 1);  /* brief */
-               }
+       wprintf("</head>");
+       wprintf("<body");
+       if (strlen(targetdiv) > 0) {
+               wprintf(" onLoad='loaded_now_copy_it();'");
        }
-       
-#ifdef HAVE_ICONV
-       /* Handle senders with RFC2047 encoding */
-       utf8ify_rfc822_string(summ.from);
-#endif
-       if (strlen(summ.from) > 25) {
-               strcpy(&summ.from[22], "...");
+       if (!strcasecmp(print_it, "yes")) {
+               wprintf(" onLoad='window.print();'");
        }
+       wprintf(">\n");
+       read_message(msgnum, (!strcasecmp(print_it, "yes") ? 1 : 0) );
+       wprintf("</body></html>\n");
+       wDumpContent(0);
+}
+
+
+
+
+void display_summarized(int num) {
+       char datebuf[64];
 
        wprintf("<TD>");
-       if (is_new) wprintf("<B>");
-       wprintf("<A HREF=\"/readfwd?startmsg=%ld"
-               "&maxmsgs=1&summary=0\">", 
-               msgnum);
-       escputs(summ.subj);
+       if (WC->summ[num].is_new) wprintf("<B>");
+       wprintf("<A HREF=\"/msg?msgnum=%ld?sourceiframe=msgloader1?targetdiv=preview_pane\" target=\"msgloader1\">",
+               WC->summ[num].msgnum);
+       escputs(WC->summ[num].subj);
        wprintf("</A>");
-       if (is_new) wprintf("</B>");
+       if (WC->summ[num].is_new) wprintf("</B>");
        wprintf("</TD><TD>");
-       if (is_new) wprintf("<B>");
-       escputs(summ.from);
-       if (is_new) wprintf("</B>");
+       if (WC->summ[num].is_new) wprintf("<B>");
+       escputs(WC->summ[num].from);
+       if (WC->summ[num].is_new) wprintf("</B>");
        wprintf(" </TD><TD>");
-       if (is_new) wprintf("<B>");
-       escputs(summ.date);
-       if (is_new) wprintf("</B>");
+       if (WC->summ[num].is_new) wprintf("<B>");
+       fmt_date(datebuf, WC->summ[num].date, 1);       /* brief */
+       escputs(datebuf);
+       if (WC->summ[num].is_new) wprintf("</B>");
        wprintf(" </TD>");
        wprintf("<TD>"
                "<INPUT TYPE=\"checkbox\" NAME=\"msg_%ld\" VALUE=\"yes\">"
-               "</TD>\n");
-
-       return;
+               "</TD>\n",
+               WC->summ[num].msgnum
+       );
 }
 
 
 
+
+
 void display_addressbook(long msgnum, char alpha) {
        char buf[SIZ];
        char mime_partnum[SIZ];
@@ -953,17 +935,10 @@ void display_addressbook(long msgnum, char alpha) {
        int mime_length;
        char vcard_partnum[SIZ];
        char *vcard_source = NULL;
-
-       struct {
-               char date[SIZ];
-               char from[SIZ];
-               char to[SIZ];
-               char subj[SIZ];
-               int hasattachments;
-       } summ;
+       struct message_summary summ;
 
        memset(&summ, 0, sizeof(summ));
-       strcpy(summ.subj, "(no subject)");
+       safestrncpy(summ.subj, "(no subject)", sizeof summ.subj);
 
        sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
        serv_puts(buf);
@@ -998,7 +973,7 @@ void display_addressbook(long msgnum, char alpha) {
                                || (WC->wc_view == VIEW_ADDRESSBOOK)
                        ) {
                                wprintf("<A HREF=\"/edit_vcard?"
-                                       "msgnum=%ld&partnum=%s\">",
+                                       "msgnum=%ld?partnum=%s\">",
                                        msgnum, vcard_partnum);
                                wprintf("[edit]</A>");
                        }
@@ -1042,20 +1017,13 @@ void fetch_ab_name(long msgnum, char *namebuf) {
        char vcard_partnum[SIZ];
        char *vcard_source = NULL;
        int i;
-
-       struct {
-               char date[SIZ];
-               char from[SIZ];
-               char to[SIZ];
-               char subj[SIZ];
-               int hasattachments;
-       } summ;
+       struct message_summary summ;
 
        if (namebuf == NULL) return;
        strcpy(namebuf, "");
 
        memset(&summ, 0, sizeof(summ));
-       strcpy(summ.subj, "(no subject)");
+       safestrncpy(summ.subj, "(no subject)", sizeof summ.subj);
 
        sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
        serv_puts(buf);
@@ -1197,7 +1165,7 @@ void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
        
                        wprintf("<A HREF=\"/readfwd?startmsg=%ld&is_singlecard=1",
                                addrbook[i].ab_msgnum);
-                       wprintf("&maxmsgs=1&summary=0&alpha=%s\">", bstr("alpha"));
+                       wprintf("?maxmsgs=1?summary=0?alpha=%s\">", bstr("alpha"));
                        vcard_n_prettyize(addrbook[i].ab_name);
                        escputs(addrbook[i].ab_name);
                        wprintf("</A></TD>\n");
@@ -1213,12 +1181,29 @@ void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
 /* 
  * load message pointers from the server
  */
-int load_msg_ptrs(char *servcmd)
+int load_msg_ptrs(char *servcmd, int with_headers)
 {
-       char buf[SIZ];
+       char buf[1024];
+       time_t datestamp;
+       char displayname[128];
+       char nodename[128];
+       char inetaddr[128];
+       char subject[256];
        int nummsgs;
        int maxload = 0;
 
+       int num_summ_alloc = 0;
+
+       if (with_headers) {
+               if (WC->num_summ != 0) {
+                       free(WC->summ);
+                       WC->num_summ = 0;
+               }
+       }
+       num_summ_alloc = 100;
+       WC->num_summ = 0;
+       WC->summ = malloc(num_summ_alloc * sizeof(struct message_summary));
+
        nummsgs = 0;
        maxload = sizeof(WC->msgarr) / sizeof(long) ;
        serv_puts(servcmd);
@@ -1229,13 +1214,124 @@ int load_msg_ptrs(char *servcmd)
        }
        while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
                if (nummsgs < maxload) {
-                       WC->msgarr[nummsgs] = atol(buf);
+                       WC->msgarr[nummsgs] = extract_long(buf, 0);
+                       datestamp = extract_long(buf, 1);
+                       extract_token(displayname, buf, 2, '|', sizeof displayname);
+                       extract_token(nodename, buf, 3, '|', sizeof nodename);
+                       extract_token(inetaddr, buf, 4, '|', sizeof inetaddr);
+                       extract_token(subject, buf, 5, '|', sizeof subject);
                        ++nummsgs;
+
+                       if (with_headers) {
+                               if (nummsgs > num_summ_alloc) {
+                                       num_summ_alloc *= 2;
+                                       WC->summ = realloc(WC->summ, num_summ_alloc * sizeof(struct message_summary));
+                               }
+                               ++WC->num_summ;
+
+                               memset(&WC->summ[nummsgs-1], 0, sizeof(struct message_summary));
+                               WC->summ[nummsgs-1].msgnum = WC->msgarr[nummsgs-1];
+                               safestrncpy(WC->summ[nummsgs-1].subj, "(no subject)", sizeof WC->summ[nummsgs-1].subj);
+                               if (strlen(displayname) > 0) {
+                                       safestrncpy(WC->summ[nummsgs-1].from, displayname, sizeof WC->summ[nummsgs-1].from);
+                               }
+                               if (strlen(subject) > 0) {
+                               safestrncpy(WC->summ[nummsgs-1].subj, subject,
+                                       sizeof WC->summ[nummsgs-1].subj);
+                               }
+#ifdef HAVE_ICONV
+                               /* Handle subjects with RFC2047 encoding */
+                               utf8ify_rfc822_string(WC->summ[nummsgs-1].subj);
+#endif
+                               if (strlen(WC->summ[nummsgs-1].subj) > 75) {
+                                       strcpy(&WC->summ[nummsgs-1].subj[72], "...");
+                               }
+
+                               if (strlen(nodename) > 0) {
+                                       if ( ((WC->room_flags & QR_NETWORK)
+                                          || ((strcasecmp(nodename, serv_info.serv_nodename)
+                                          && (strcasecmp(nodename, serv_info.serv_fqdn)))))
+                                       ) {
+                                               strcat(WC->summ[nummsgs-1].from, " @ ");
+                                               strcat(WC->summ[nummsgs-1].from, nodename);
+                                       }
+                               }
+
+                               WC->summ[nummsgs-1].date = datestamp;
+       
+#ifdef HAVE_ICONV
+                               /* Handle senders with RFC2047 encoding */
+                               utf8ify_rfc822_string(WC->summ[nummsgs-1].from);
+#endif
+                               if (strlen(WC->summ[nummsgs-1].from) > 25) {
+                                       strcpy(&WC->summ[nummsgs-1].from[22], "...");
+                               }
+                       }
                }
        }
        return (nummsgs);
 }
 
+int summcmp_subj(const void *s1, const void *s2) {
+       struct message_summary *summ1;
+       struct message_summary *summ2;
+       
+       summ1 = (struct message_summary *)s1;
+       summ2 = (struct message_summary *)s2;
+       return strcasecmp(summ1->subj, summ2->subj);
+}
+
+int summcmp_rsubj(const void *s1, const void *s2) {
+       struct message_summary *summ1;
+       struct message_summary *summ2;
+       
+       summ1 = (struct message_summary *)s1;
+       summ2 = (struct message_summary *)s2;
+       return strcasecmp(summ2->subj, summ1->subj);
+}
+
+int summcmp_sender(const void *s1, const void *s2) {
+       struct message_summary *summ1;
+       struct message_summary *summ2;
+       
+       summ1 = (struct message_summary *)s1;
+       summ2 = (struct message_summary *)s2;
+       return strcasecmp(summ1->from, summ2->from);
+}
+
+int summcmp_rsender(const void *s1, const void *s2) {
+       struct message_summary *summ1;
+       struct message_summary *summ2;
+       
+       summ1 = (struct message_summary *)s1;
+       summ2 = (struct message_summary *)s2;
+       return strcasecmp(summ2->from, summ1->from);
+}
+
+int summcmp_date(const void *s1, const void *s2) {
+       struct message_summary *summ1;
+       struct message_summary *summ2;
+       
+       summ1 = (struct message_summary *)s1;
+       summ2 = (struct message_summary *)s2;
+
+       if (summ1->date < summ2->date) return -1;
+       else if (summ1->date > summ2->date) return +1;
+       else return 0;
+}
+
+int summcmp_rdate(const void *s1, const void *s2) {
+       struct message_summary *summ1;
+       struct message_summary *summ2;
+       
+       summ1 = (struct message_summary *)s1;
+       summ2 = (struct message_summary *)s2;
+
+       if (summ1->date < summ2->date) return +1;
+       else if (summ1->date > summ2->date) return -1;
+       else return 0;
+}
 
 /*
  * command loop for reading messages
@@ -1245,7 +1341,6 @@ void readloop(char *oper)
        char cmd[SIZ];
        char buf[SIZ];
        char old_msgs[SIZ];
-       int is_new = 0;
        int a, b;
        int nummsgs;
        long startmsg;
@@ -1267,12 +1362,28 @@ void readloop(char *oper)
        int bg = 0;
        struct addrbookent *addrbook = NULL;
        int num_ab = 0;
+       char *sortby = NULL;
+       char sortpref_name[128];
+       char sortpref_value[128];
+       char *subjsort_button;
+       char *sendsort_button;
+       char *datesort_button;
 
        startmsg = atol(bstr("startmsg"));
        maxmsgs = atoi(bstr("maxmsgs"));
        is_summary = atoi(bstr("summary"));
        if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
 
+       snprintf(sortpref_name, sizeof sortpref_name, "sort %s", WC->wc_roomname);
+       get_preference(sortpref_name, sortpref_value, sizeof sortpref_value);
+
+       sortby = bstr("sortby");
+       if ( (strlen(sortby) > 0) && (strcasecmp(sortby, sortpref_value)) ) {
+               set_preference(sortpref_name, sortby, 1);
+       }
+       if (strlen(sortby) == 0) sortby = sortpref_value;
+       if (strlen(sortby) == 0) sortby = "msgid";
+
        output_headers(1, 1, 1, 0, 0, 0, 0);
 
        /* When in summary mode, always show ALL messages instead of just
@@ -1296,11 +1407,13 @@ void readloop(char *oper)
        if ((WC->wc_view == VIEW_ADDRESSBOOK) && (maxmsgs > 1)) {
                is_addressbook = 1;
                strcpy(cmd, "MSGS ALL");
-               maxmsgs = 32767;
+               maxmsgs = 9999999;
        }
 
        if (is_summary) {
-               strcpy(cmd, "MSGS ALL");
+               strcpy(cmd, "MSGS ALL|||1");    /* fetch header summary */
+               startmsg = 1;
+               maxmsgs = 9999999;
        }
 
        /* Are we doing a summary view?  If so, we need to know old messages
@@ -1334,7 +1447,7 @@ void readloop(char *oper)
                maxmsgs = 32767;
        }
 
-       nummsgs = load_msg_ptrs(cmd);
+       nummsgs = load_msg_ptrs(cmd, is_summary);
        if (nummsgs == 0) {
 
                if ((!is_tasks) && (!is_calendar) && (!is_notes)) {
@@ -1350,6 +1463,20 @@ void readloop(char *oper)
                goto DONE;
        }
 
+       if (is_summary) {
+               for (a = 0; a < nummsgs; ++a) {
+                       /* Are you a new message, or an old message? */
+                       if (is_summary) {
+                               if (is_msg_in_mset(old_msgs, WC->msgarr[a])) {
+                                       WC->summ[a].is_new = 0;
+                               }
+                               else {
+                                       WC->summ[a].is_new = 1;
+                               }
+                       }
+               }
+       }
+
        if (startmsg == 0L) startmsg = WC->msgarr[0];
        remaining_messages = 0;
 
@@ -1359,35 +1486,94 @@ void readloop(char *oper)
                }
        }
 
-       wprintf("<form name=\"msgomatic\" "
-               "METHOD=\"POST\" ACTION=\"/do_stuff_to_msgs\">\n");
        if (is_summary) {
-               wprintf("<div id=\"fix_scrollbar_bug\">"
+               if (!strcasecmp(sortby, "subject")) {
+                       qsort(WC->summ, WC->num_summ,
+                               sizeof(struct message_summary), summcmp_subj);
+               }
+               else if (!strcasecmp(sortby, "rsubject")) {
+                       qsort(WC->summ, WC->num_summ,
+                               sizeof(struct message_summary), summcmp_rsubj);
+               }
+               else if (!strcasecmp(sortby, "sender")) {
+                       qsort(WC->summ, WC->num_summ,
+                               sizeof(struct message_summary), summcmp_sender);
+               }
+               else if (!strcasecmp(sortby, "rsender")) {
+                       qsort(WC->summ, WC->num_summ,
+                               sizeof(struct message_summary), summcmp_rsender);
+               }
+               else if (!strcasecmp(sortby, "date")) {
+                       qsort(WC->summ, WC->num_summ,
+                               sizeof(struct message_summary), summcmp_date);
+               }
+               else if (!strcasecmp(sortby, "rdate")) {
+                       qsort(WC->summ, WC->num_summ,
+                               sizeof(struct message_summary), summcmp_rdate);
+               }
+       }
+
+       if (!strcasecmp(sortby, "subject")) {
+               subjsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=rsubject\"><img border=\"0\" src=\"/static/down_pointer.gif\" /></a>" ;
+       }
+       else if (!strcasecmp(sortby, "rsubject")) {
+               subjsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=subject\"><img border=\"0\" src=\"/static/up_pointer.gif\" /></a>" ;
+       }
+       else {
+               subjsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=subject\"><img border=\"0\" src=\"/static/sort_none.gif\" /></a>" ;
+       }
+
+       if (!strcasecmp(sortby, "sender")) {
+               sendsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=rsender\"><img border=\"0\" src=\"/static/down_pointer.gif\" /></a>" ;
+       }
+       else if (!strcasecmp(sortby, "rsender")) {
+               sendsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=sender\"><img border=\"0\" src=\"/static/up_pointer.gif\" /></a>" ;
+       }
+       else {
+               sendsort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=sender\"><img border=\"0\" src=\"/static/sort_none.gif\" /></a>" ;
+       }
+
+       if (!strcasecmp(sortby, "date")) {
+               datesort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=rdate\"><img border=\"0\" src=\"/static/down_pointer.gif\" /></a>" ;
+       }
+       else if (!strcasecmp(sortby, "rdate")) {
+               datesort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=date\"><img border=\"0\" src=\"/static/up_pointer.gif\" /></a>" ;
+       }
+       else {
+               datesort_button = "<a href=\"/readfwd?startmsg=1?maxmsgs=9999999?summary=1?sortby=date\"><img border=\"0\" src=\"/static/sort_none.gif\" /></a>" ;
+       }
+
+       if (is_summary) {
+               wprintf("</div>");              /* end of 'content' div */
+
+               wprintf("<div id=\"message_list\">"
+
+                       "<div id=\"fix_scrollbar_bug\">\n"
+
+                       "<form name=\"msgomatic\" "
+                       "method=\"POST\" action=\"/do_stuff_to_msgs\">\n"
+
                        "<table border=0 cellspacing=0 "
                        "cellpadding=0 width=100%%>\n"
                        "<TR>"
-                       "<TD align=center><b><i>Subject</i></b></TD>"
-                       "<TD align=center><b><i>Sender</i></b></TD>"
-                       "<TD align=center><b><i>Date</i></b></TD>"
-                       "<TD></TD>"
+                       "<TD align=center><b><i>Subject</i></b> %s</TD>"
+                       "<TD align=center><b><i>Sender</i></b> %s</TD>"
+                       "<TD align=center><b><i>Date</i></b> %s</TD>"
+                       "<TD><INPUT TYPE=\"submit\" NAME=\"sc\" "
+                       "STYLE=\"font-family: Bitstream Vera Sans,Arial,Helvetica,sans-serif;"
+                       " font-size: 6pt;\" "
+                       "VALUE=\"Delete\"></TD>"
                        "</TR>\n"
+                       ,
+                       subjsort_button,
+                       sendsort_button,
+                       datesort_button
                );
        }
 
        for (a = 0; a < nummsgs; ++a) {
                if ((WC->msgarr[a] >= startmsg) && (num_displayed < maxmsgs)) {
 
-                       /* Are you a new message, or an old message? */
-                       is_new = 0;
-                       if (is_summary) {
-                               if (is_msg_in_mset(old_msgs, WC->msgarr[a])) {
-                                       is_new = 0;
-                               }
-                               else {
-                                       is_new = 1;
-                               }
-                       }
-
                        /* Learn which msgs "Prev" & "Next" buttons go to */
                        pn_current = WC->msgarr[a];
                        if (a > 0) pn_previous = WC->msgarr[a-1];
@@ -1403,7 +1589,7 @@ void readloop(char *oper)
 
                        /* Display the message */
                        if (is_summary) {
-                               summarize_message(WC->msgarr[a], is_new);
+                               display_summarized(a);
                        }
                        else if (is_addressbook) {
                                fetch_ab_name(WC->msgarr[a], buf);
@@ -1424,7 +1610,7 @@ void readloop(char *oper)
                                display_note(WC->msgarr[a]);
                        }
                        else {
-                               read_message(WC->msgarr[a]);
+                               read_message(WC->msgarr[a], 0);
                        }
 
                        /* If a tabular view, finish the line */
@@ -1441,7 +1627,11 @@ void readloop(char *oper)
        }
 
        if (is_summary) {
-               wprintf("</table></div>\n");
+               wprintf("</table></form>"
+                       "</div>\n");                    /* end of 'fix_scrollbar_bug' div */
+               wprintf("</div>");                      /* end of 'message_list' div */
+
+               wprintf("<div id=\"preview_pane\">");   /* The preview pane will initially be empty */
        }
 
        /* Bump these because although we're thinking in zero base, the user
@@ -1460,16 +1650,11 @@ void readloop(char *oper)
                        "<TD ALIGN=RIGHT><FONT SIZE=+1>",
                        lowest_displayed, nummsgs);
 
-               if (is_summary) {
-                       wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
-                               "VALUE=\"Delete selected\">\n");
-               }
-
                if (pn_previous > 0L) {
                        wprintf("<A HREF=\"/%s"
                                "?startmsg=%ld"
-                               "&maxmsgs=1"
-                               "&summary=0\">"
+                               "?maxmsgs=1"
+                               "?summary=0\">"
                                "Previous</A> \n",
                                        oper,
                                        pn_previous );
@@ -1478,15 +1663,15 @@ void readloop(char *oper)
                if (pn_next > 0L) {
                        wprintf("<A HREF=\"/%s"
                                "?startmsg=%ld"
-                               "&maxmsgs=1"
-                               "&summary=0\">"
+                               "?maxmsgs=1"
+                               "?summary=0\">"
                                "Next</A> \n",
                                        oper,
                                        pn_next );
                }
 
                wprintf("<A HREF=\"/%s?startmsg=%ld"
-                       "&maxmsgs=%d&summary=1\">"
+                       "?maxmsgs=%d?summary=1\">"
                        "Summary"
                        "</A>",
                        oper,
@@ -1504,11 +1689,14 @@ void readloop(char *oper)
         */
        if (num_displayed > 1) {
           if ((!is_tasks) && (!is_calendar) && (!is_addressbook)
-             && (!is_notes) && (!is_singlecard)) {
+             && (!is_notes) && (!is_singlecard) && (!is_summary)) {
+
+               wprintf("<form name=\"msgomatic\" "
+                       "method=\"POST\" action=\"/do_stuff_to_msgs\">\n");
 
                wprintf("Reading #", lowest_displayed, highest_displayed);
 
-               wprintf("<SELECT NAME=\"whichones\" SIZE=\"1\" "
+               wprintf("<select name=\"whichones\" size=\"1\" "
                        "OnChange=\"location.href=msgomatic.whichones.options"
                        "[selectedIndex].value\">\n");
 
@@ -1516,36 +1704,30 @@ void readloop(char *oper)
                lo = b+1;
                hi = b+maxmsgs;
                if (hi > nummsgs) hi = nummsgs;
-                       wprintf("<OPTION %s VALUE="
+                       wprintf("<option %s value="
                                "\"/%s"
                                "?startmsg=%ld"
-                               "&maxmsgs=%d"
-                               "&summary=%d\">"
-                               "%d-%d</OPTION> \n",
-                               ((WC->msgarr[b] == startmsg) ? "SELECTED" : ""),
+                               "?maxmsgs=%d"
+                               "?summary=%d\">"
+                               "%d-%d</option> \n",
+                               ((WC->msgarr[b] == startmsg) ? "selected" : ""),
                                oper,
                                WC->msgarr[b],
                                maxmsgs,
                                is_summary,
                                lo, hi);
                }
-               wprintf("<OPTION VALUE=\"/%s?startmsg=%ld"
-                       "&maxmsgs=9999999&summary=%d\">"
+               wprintf("<option value=\"/%s?startmsg=%ld"
+                       "?maxmsgs=9999999?summary=%d\">"
                        "ALL"
-                       "</OPTION> ",
+                       "</option> ",
                        oper,
                        WC->msgarr[0], is_summary);
 
-               wprintf("</SELECT> of %d messages.", nummsgs);
-
-               if (is_summary) {
-                       wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" "
-                               "VALUE=\"Delete selected\">\n");
-               }
-
+               wprintf("</select> of %d messages.", nummsgs);
+               wprintf("</form>\n");
            }
        }
-       wprintf("</form>\n");
 
 DONE:
        if (is_tasks) {
@@ -1560,9 +1742,22 @@ DONE:
                do_addrbook_view(addrbook, num_ab);     /* Render the address book */
        }
 
+       /* Put the data transfer hidden iframe in a hidden div, to make it *really* hidden */
+       wprintf("</div>"
+               "<div display=\"hidden\">\n"
+               "<iframe name=\"msgloader1\" id=\"msgloader1\" width=\"1\"></iframe>\n"
+       );
+
+       /* Note: wDumpContent() will output one additional </div> tag. */
        wDumpContent(1);
        if (addrbook != NULL) free(addrbook);
 
+       /* free the summary */
+       if (WC->num_summ != 0) {
+               WC->num_summ = 0;
+               free(WC->summ);
+       }
+
        /* If we got here via a mailbox view and are reading a single
         * message, mark it as "seen." We do this after rendering the web page
         * so it doesn't keep the user waiting.
@@ -1775,7 +1970,10 @@ void display_enter(void)
        }
 #endif
 
-       /* Otherwise proceed normally.  Do a custom room banner with no navbar... */
+       /*
+        * Otherwise proceed normally.
+`       * Do a custom room banner with no navbar...
+        */
        output_headers(1, 1, 2, 0, 0, 0, 0);
        wprintf("<div id=\"banner\">\n");
        embed_room_banner(NULL, navbar_none);
@@ -2025,7 +2223,7 @@ void do_stuff_to_msgs(void) {
                sprintf(buf, "msg_%ld", stuff->msgnum);
                if (!strcasecmp(bstr(buf), "yes")) {
 
-                       if (!strcasecmp(sc, "Delete selected")) {
+                       if (!strcasecmp(sc, "Delete")) {
                                serv_printf("DELE %ld", stuff->msgnum);
                                serv_getln(buf, sizeof buf);
                        }