* Room info popup now uses Scriptactulous appear and fade effects
[citadel.git] / webcit / messages.c
index 9e05d552d03a1d2d606ad32ba43519e5faf9b4fa..cfcf8ff94d3f2af60e6ccf9dc034fd03a48f24a8 100644 (file)
@@ -32,7 +32,7 @@ struct addrbookent {
 /**
  * \brief      Wrapper around iconv_open()
  *             Our version adds aliases for non-standard Microsoft charsets
- *              such as 'MS950', aliasing them to names like 'CP950'
+ *           such as 'MS950', aliasing them to names like 'CP950'
  *
  * \param      tocode          Target encoding
  * \param      fromcode        Source encoding
@@ -127,7 +127,18 @@ void utf8ify_rfc822_string(char *buf) {
                        ibuflen = CtdlDecodeBase64(ibuf, istr, strlen(istr));
                }
                else if (!strcasecmp(encoding, "Q")) {  /**< quoted-printable */
-                       ibuflen = CtdlDecodeQuotedPrintable(ibuf, istr, strlen(istr));
+                       size_t len;
+                       long pos;
+                       
+                       len = strlen(istr);
+                       pos = 0;
+                       while (pos < len)
+                       {
+                               if (istr[pos] == '_') istr[pos] = ' ';
+                               pos++;
+                       }
+
+                       ibuflen = CtdlDecodeQuotedPrintable(ibuf, istr, len);
                }
                else {
                        strcpy(ibuf, istr);             /**< unknown encoding */
@@ -243,13 +254,10 @@ void url(char *buf)
 
        int pos;
        int start, end;
-       char ench;
        char urlbuf[SIZ];
        char outbuf[1024];
-
        start = (-1);
        end = strlen(buf);
-       ench = 0;
 
        for (pos = 0; pos < strlen(buf); ++pos) {
                if (!strncasecmp(&buf[pos], "http://", 7))
@@ -261,18 +269,24 @@ void url(char *buf)
        if (start < 0)
                return;
 
-       if ((start > 0) && (buf[start - 1] == '<'))
-               ench = '>';
-       if ((start > 0) && (buf[start - 1] == '['))
-               ench = ']';
-       if ((start > 0) && (buf[start - 1] == '('))
-               ench = ')';
-       if ((start > 0) && (buf[start - 1] == '{'))
-               ench = '}';
-
        for (pos = strlen(buf); pos > start; --pos) {
-               if ((buf[pos] == ' ') || (buf[pos] == ench))
+               if (  (!isprint(buf[pos]))
+                  || (isspace(buf[pos]))
+                  || (buf[pos] == '{')
+                  || (buf[pos] == '}')
+                  || (buf[pos] == '|')
+                  || (buf[pos] == '\\')
+                  || (buf[pos] == '^')
+                  || (buf[pos] == '[')
+                  || (buf[pos] == ']')
+                  || (buf[pos] == '`')
+                  || (buf[pos] == '<')
+                  || (buf[pos] == '>')
+                  || (buf[pos] == '(')
+                  || (buf[pos] == ')')
+               ) {
                        end = pos;
+               }
        }
 
        strncpy(urlbuf, &buf[start], end - start);
@@ -620,6 +634,12 @@ void display_vcard(char *vcard_source, char alpha, int full, char *storename) {
 }
 
 
+struct attach_link {
+       char partnum[32];
+       char html[1024];
+};
+
+
 /**
  * \brief I wanna SEE that message!  
  * \param msgnum the citadel number of the message to display
@@ -634,7 +654,8 @@ void read_message(long msgnum, int printable_view, char *section) {
        char mime_charset[256];
        char mime_disposition[256];
        int mime_length;
-       char mime_http[SIZ];
+       struct attach_link *attach_links = NULL;
+       int num_attach_links = 0;
        char mime_submessages[256];
        char m_subject[256];
        char m_cc[1024];
@@ -651,6 +672,7 @@ void read_message(long msgnum, int printable_view, char *section) {
        char vcard_partnum[256];
        char cal_partnum[256];
        char *part_source = NULL;
+       char msg4_partnum[32];
 #ifdef HAVE_ICONV
        iconv_t ic = (iconv_t)(-1) ;
        char *ibuf;                /**< Buffer of characters to be converted */
@@ -667,7 +689,6 @@ void read_message(long msgnum, int printable_view, char *section) {
        strcpy(reply_all, "");
        strcpy(vcard_partnum, "");
        strcpy(cal_partnum, "");
-       strcpy(mime_http, "");
        strcpy(mime_content_type, "text/plain");
        strcpy(mime_charset, "us-ascii");
        strcpy(mime_submessages, "");
@@ -793,15 +814,24 @@ void read_message(long msgnum, int printable_view, char *section) {
                        }
                        else if ((!strcasecmp(mime_disposition, "inline"))
                           && (!strncasecmp(mime_content_type, "image/", 6)) ){
-                               snprintf(&mime_http[strlen(mime_http)],
-                                       (sizeof(mime_http) - strlen(mime_http) - 1),
+                               ++num_attach_links;
+                               attach_links = realloc(attach_links,
+                                       (num_attach_links*sizeof(struct attach_link)));
+                               safestrncpy(attach_links[num_attach_links-1].partnum, mime_partnum, 32);
+                               snprintf(attach_links[num_attach_links-1].html, 1024,
                                        "<img src=\"mimepart/%ld/%s/%s\">",
                                        msgnum, mime_partnum, mime_filename);
                        }
-                       else if ( (!strcasecmp(mime_disposition, "attachment")) 
-                            || (!strcasecmp(mime_disposition, "inline")) ) {
-                               snprintf(&mime_http[strlen(mime_http)],
-                                       (sizeof(mime_http) - strlen(mime_http) - 1),
+                       else if ( ( (!strcasecmp(mime_disposition, "attachment")) 
+                            || (!strcasecmp(mime_disposition, "inline"))
+                            || (!strcasecmp(mime_disposition, ""))
+                            ) && (strlen(mime_content_type) > 0)
+                       ) {
+                               ++num_attach_links;
+                               attach_links = realloc(attach_links,
+                                       (num_attach_links*sizeof(struct attach_link)));
+                               safestrncpy(attach_links[num_attach_links-1].partnum, mime_partnum, 32);
+                               snprintf(attach_links[num_attach_links-1].html, 1024,
                                        "<img src=\"static/diskette_24x.gif\" "
                                        "border=0 align=middle>\n"
                                        "%s (%s, %d bytes) [ "
@@ -821,7 +851,8 @@ void read_message(long msgnum, int printable_view, char *section) {
                        }
 
                        /** begin handler prep ***/
-                       if (!strcasecmp(mime_content_type, "text/x-vcard")) {
+                       if (  (!strcasecmp(mime_content_type, "text/x-vcard"))
+                          || (!strcasecmp(mime_content_type, "text/vcard")) ) {
                                strcpy(vcard_partnum, mime_partnum);
                        }
 
@@ -837,7 +868,12 @@ void read_message(long msgnum, int printable_view, char *section) {
 
        /** Generate a reply-to address */
        if (strlen(rfca) > 0) {
-               strcpy(reply_to, rfca);
+               if (strlen(from) > 0) {
+                       snprintf(reply_to, sizeof(reply_to), "%s <%s>", from, rfca);
+               }
+               else {
+                       strcpy(reply_to, rfca);
+               }
        }
        else {
                if ( (strlen(node) > 0)
@@ -939,7 +975,7 @@ void read_message(long msgnum, int printable_view, char *section) {
                }
 
                /** If this is one of my own rooms, or if I'm an Aide or Room Aide, I can move/delete */
-               if ( (WC->is_room_aide) || (WC->is_mailbox) ) {
+               if ( (WC->is_room_aide) || (WC->is_mailbox) || (WC->room_flags2 & QR2_COLLABDEL) ) {
                        /** Move */
                        wprintf("<a href=\"confirm_move_msg?msgid=%ld\">[%s]</a> ",
                                msgnum, _("Move"));
@@ -980,9 +1016,13 @@ void read_message(long msgnum, int printable_view, char *section) {
                        wprintf("</i><br /><br />\n");
                        goto ENDBODY;
                }
-               if (!strncasecmp(buf, "Content-type: ", 14)) {
-                       safestrncpy(mime_content_type, &buf[14],
-                               sizeof(mime_content_type));
+               if (!strncasecmp(buf, "X-Citadel-MSG4-Partnum:", 23)) {
+                       safestrncpy(msg4_partnum, &buf[23], sizeof(msg4_partnum));
+                       striplt(msg4_partnum);
+               }
+               if (!strncasecmp(buf, "Content-type:", 13)) {
+                       safestrncpy(mime_content_type, &buf[13], sizeof(mime_content_type));
+                       striplt(mime_content_type);
                        for (i=0; i<strlen(mime_content_type); ++i) {
                                if (!strncasecmp(&mime_content_type[i], "charset=", 8)) {
                                        safestrncpy(mime_charset, &mime_content_type[i+8],
@@ -1087,8 +1127,12 @@ void read_message(long msgnum, int printable_view, char *section) {
 
 
        /** Afterwards, offer links to download attachments 'n' such */
-       if ( (strlen(mime_http) > 0) && (!section[0]) ) {
-               wprintf("%s", mime_http);
+       if ( (num_attach_links > 0) && (!section[0]) ) {
+               for (i=0; i<num_attach_links; ++i) {
+                       if (strcasecmp(attach_links[i].partnum, msg4_partnum)) {
+                               wprintf("%s", attach_links[i].html);
+                       }
+               }
        }
 
        /** Handler for vCard parts */
@@ -1135,6 +1179,10 @@ ENDBODY:
                wprintf("</div><br />\n");
        }
 
+       if (num_attach_links > 0) {
+               free(attach_links);
+       }
+
 #ifdef HAVE_ICONV
        if (ic != (iconv_t)(-1) ) {
                iconv_close(ic);
@@ -1223,7 +1271,7 @@ void display_headers(char *msgnum_as_string) {
 
 /**
  * \brief Read message in simple, JavaScript-embeddable form for 'forward'
- *        or 'reply quoted' operations.
+ *     or 'reply quoted' operations.
  *
  * NOTE: it is VITALLY IMPORTANT that we output no single-quotes or linebreaks
  *       in this function.  Doing so would throw a JavaScript error in the
@@ -1609,7 +1657,8 @@ void display_addressbook(long msgnum, char alpha) {
                        extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
                        mime_length = extract_int(&buf[5], 5);
 
-                       if (!strcasecmp(mime_content_type, "text/x-vcard")) {
+                       if (  (!strcasecmp(mime_content_type, "text/x-vcard"))
+                          || (!strcasecmp(mime_content_type, "text/vcard")) ) {
                                strcpy(vcard_partnum, mime_partnum);
                        }
 
@@ -1686,7 +1735,7 @@ void fetch_ab_name(long msgnum, char *namebuf) {
        memset(&summ, 0, sizeof(summ));
        safestrncpy(summ.subj, "(no subject)", sizeof summ.subj);
 
-       sprintf(buf, "MSG0 %ld|1", msgnum);     /** ask for headers only */
+       sprintf(buf, "MSG0 %ld|0", msgnum);     /** unfortunately we need the mime info now */
        serv_puts(buf);
        serv_getln(buf, sizeof buf);
        if (buf[0] != '1') return;
@@ -1699,7 +1748,8 @@ void fetch_ab_name(long msgnum, char *namebuf) {
                        extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
                        mime_length = extract_int(&buf[5], 5);
 
-                       if (!strcasecmp(mime_content_type, "text/x-vcard")) {
+                       if (  (!strcasecmp(mime_content_type, "text/x-vcard"))
+                          || (!strcasecmp(mime_content_type, "text/vcard")) ) {
                                strcpy(vcard_partnum, mime_partnum);
                        }
 
@@ -2580,7 +2630,7 @@ void post_mime_to_server(void) {
        }
 
        if (is_multipart) {
-               sprintf(boundary, "=_Citadel_Multipart_%s_%04x%04x",
+               sprintf(boundary, "Citadel--Multipart--%s--%04x--%04x",
                        serv_info.serv_fqdn,
                        getpid(),
                        ++seq
@@ -2646,13 +2696,17 @@ void post_message(void)
        static long dont_post = (-1L);
        struct wc_attachment *att, *aptr;
        int is_anonymous = 0;
+       char *display_name;
 
-       if (!strcasecmp(bstr("is_anonymous"), "yes")) {
+       display_name = bstr("display_name");
+       if (!strcmp(display_name, "__ANONYMOUS__")) {
+               display_name = "";
                is_anonymous = 1;
        }
 
        if (WC->upload_length > 0) {
 
+               lprintf(9, "%s:%d: we are uploading %d bytes\n", __FILE__, __LINE__, WC->upload_length);
                /** There's an attachment.  Save it to this struct... */
                att = malloc(sizeof(struct wc_attachment));
                memset(att, 0, sizeof(struct wc_attachment));
@@ -2706,13 +2760,15 @@ void post_message(void)
                        "saved this message."));
        } else {
                rfc2047encode(encoded_subject, sizeof encoded_subject, bstr("subject"));
-               sprintf(buf, "ENT0 1|%s|%d|4|%s|||%s|%s|%s",
+               sprintf(buf, "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s",
                        bstr("recp"),
                        is_anonymous,
                        encoded_subject,
+                       display_name,
                        bstr("cc"),
                        bstr("bcc"),
-                       bstr("wikipage")
+                       bstr("wikipage"),
+                       bstr("my_email_addr")
                );
                serv_puts(buf);
                serv_getln(buf, sizeof buf);
@@ -2729,6 +2785,7 @@ void post_message(void)
                        }
                        dont_post = atol(bstr("postseq"));
                } else {
+                       lprintf(9, "%s:%d: server post error: %s\n", __FILE__, __LINE__, buf);
                        sprintf(WC->ImportantMessage, "%s", &buf[4]);
                        display_enter();
                        return;
@@ -2770,6 +2827,7 @@ void display_enter(void)
        char buf[SIZ];
        char ebuf[SIZ];
        long now;
+       char *display_name;
        struct wc_attachment *att;
        int recipient_required = 0;
        int recipient_bad = 0;
@@ -2777,11 +2835,15 @@ void display_enter(void)
        int is_anonymous = 0;
        long existing_page = (-1L);
 
+       now = time(NULL);
+
        if (strlen(bstr("force_room")) > 0) {
                gotoroom(bstr("force_room"));
        }
 
-       if (!strcasecmp(bstr("is_anonymous"), "yes")) {
+       display_name = bstr("display_name");
+       if (!strcmp(display_name, "__ANONYMOUS__")) {
+               display_name = "";
                is_anonymous = 1;
        }
 
@@ -2840,7 +2902,10 @@ void display_enter(void)
 
        /** Now check our actual recipients if there are any */
        if (recipient_required) {
-               sprintf(buf, "ENT0 0|%s|%d|0||||%s|%s|%s", bstr("recp"), is_anonymous,
+               sprintf(buf, "ENT0 0|%s|%d|0||%s||%s|%s|%s",
+                       bstr("recp"),
+                       is_anonymous,
+                       display_name,
                        bstr("cc"), bstr("bcc"), bstr("wikipage"));
                serv_puts(buf);
                serv_getln(buf, sizeof buf);
@@ -2858,21 +2923,6 @@ void display_enter(void)
 
        /** If we got this far, we can display the message entry screen. */
 
-       now = time(NULL);
-       fmt_date(buf, now, 0);
-       strcat(&buf[strlen(buf)], _(" <I>from</I> "));
-       stresc(&buf[strlen(buf)], WC->wc_fullname, 1, 1);
-
-       /* Don't need this anymore, it's in the input box below
-       if (strlen(bstr("recp")) > 0) {
-               strcat(&buf[strlen(buf)], _(" <I>to</I> "));
-               stresc(&buf[strlen(buf)], bstr("recp"), 1, 1);
-       }
-       */
-
-       strcat(&buf[strlen(buf)], _(" <I>in</I> "));
-       stresc(&buf[strlen(buf)], WC->wc_roomname, 1, 1);
-
        /** begin message entry screen */
        wprintf("<form "
                "enctype=\"multipart/form-data\" "
@@ -2887,15 +2937,62 @@ void display_enter(void)
        }
        wprintf("<input type=\"hidden\" name=\"return_to\" value=\"%s\">\n", bstr("return_to"));
 
+       /** header bar */
+
        wprintf("<img src=\"static/newmess3_24x.gif\" align=middle alt=\" \">");
-       wprintf("%s\n", buf);   /** header bar */
+       fmt_date(buf, now, 0);
+       wprintf("%s ", buf);
+
+       wprintf(_(" <I>from</I> "));
+
+       /* Allow the user to select any of his valid screen names */
+
+       wprintf("<select name=\"display_name\" size=1>\n");
+
+       serv_puts("GVSN");
+       serv_getln(buf, sizeof buf);
+       if (buf[0] == '1') {
+               while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
+                       wprintf("<option %s value=\"",
+                               ((!strcasecmp(bstr("display_name"), buf)) ? "selected" : "")
+                       );
+                       escputs(buf);
+                       wprintf("\">");
+                       escputs(buf);
+                       wprintf("</option>\n");
+               }
+       }
+
        if (WC->room_flags & QR_ANONOPT) {
-               wprintf("&nbsp;"
-                       "<input type=\"checkbox\" name=\"is_anonymous\" value=\"yes\" %s>",
-                               (is_anonymous ? "checked" : "")
+               wprintf("<option %s value=\"__ANONYMOUS__\">%s</option>\n",
+                       ((!strcasecmp(bstr("__ANONYMOUS__"), WC->wc_fullname)) ? "selected" : ""),
+                       _("Anonymous")
                );
-               wprintf("Anonymous");
        }
+
+       wprintf("</select>\n");
+
+       /* If this is an email (not a post), allow the user to select any of his
+        * valid email addresses.
+        */
+       if (recipient_required) {
+               serv_puts("GVEA");
+               serv_getln(buf, sizeof buf);
+               if (buf[0] == '1') {
+                       wprintf("<select name=\"my_email_addr\" size=1>\n");
+                       while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
+                               wprintf("<option value=\"");
+                               escputs(buf);
+                               wprintf("\">&lt;");
+                               escputs(buf);
+                               wprintf("&gt;</option>\n");
+                       }
+                       wprintf("</select>\n");
+               }
+       }
+
+       wprintf(_(" <I>in</I> "));
+       escputs(WC->wc_roomname);
        wprintf("<br>\n");      /** header bar */
 
        wprintf("<table border=\"0\" width=\"100%%\">\n");
@@ -2910,6 +3007,17 @@ void display_enter(void)
                escputs(bstr("recp"));
                wprintf("\" size=50 maxlength=1000 />");
                wprintf("<div class=\"auto_complete\" id=\"recp_name_choices\"></div>");
+
+               /** Pop open an address book -- begin **/
+
+               wprintf(
+                       "<a href=\"javascript:PopOpenAddressBook();\" title=\"FIXME\">"
+                       "<img border=0 width=16 height=16 src=\"static/viewcontacts_16x.gif\">"
+                       "</a>"
+               );
+
+               /** Pop open an address book -- end **/
+
                wprintf("</td><td></td></tr>\n");
 
                wprintf("<tr><td>");
@@ -3089,6 +3197,23 @@ void display_enter(void)
        wprintf("</form>\n");
 
        wprintf("</td></tr></table></div>\n");
+
+       /* Close the main div, now open a new one, hidden initially, for address book popups.
+        * Remember: the popup div will be closed by wDumpContent, which will think it's merely
+        * closing the main div.  FIXME put this in its own function so we can use it from the
+        * calendar too.
+        */
+       wprintf("</div><div id=\"address_book_popup\" style=\"display:none;\">");
+       svprintf("BOXTITLE", WCS_STRING,  _("Contacts") );
+       do_template("beginbox");
+       wprintf("<div id=\"address_book_inner_div\"></div>");
+       wprintf("<div align=center><p class=\"close_popup\" "
+               "onclick=\"javascript:Effect.Fade('address_book_popup', { duration: 0.5 });\" "
+               ">");
+               wprintf(_("Close window"));
+               wprintf("</p></div>");
+       do_template("endbox");
+
 DONE:  wDumpContent(1);
 }