New server command DLAT (DownLoad ATtachment) which
[citadel.git] / citadel / msgbase.c
index b6d6a0bc36168cfcd6e1cb440cc7af15d5add2ea..2a6b775c8a0efa1e6e48058793796efcf5b6458c 100644 (file)
@@ -969,6 +969,27 @@ void mime_download(char *name, char *filename, char *partnum, char *disp,
 
 
 
+/*
+ * Callback function for mime parser that outputs a section all at once
+ */
+void mime_spew_section(char *name, char *filename, char *partnum, char *disp,
+                  void *content, char *cbtype, char *cbcharset, size_t length,
+                  char *encoding, void *cbuserdata)
+{
+       int *found_it = (int *)cbuserdata;
+
+       /* ...or if this is not the desired section */
+       if (strcasecmp(CC->download_desired_section, partnum))
+               return;
+
+       *found_it = 1;
+
+       cprintf("%d %d\n", BINARY_FOLLOWS, length);
+       client_write(content, length);
+}
+
+
+
 /*
  * Load a message from disk into memory.
  * This is used by CtdlOutputMsg() and other fetch functions.
@@ -1209,6 +1230,12 @@ void fixed_output(char *name, char *filename, char *partnum, char *disp,
  * The client is elegant and sophisticated and wants to be choosy about
  * MIME content types, so figure out which multipart/alternative part
  * we're going to send.
+ *
+ * We use a system of weights.  When we find a part that matches one of the
+ * MIME types we've declared as preferential, we can store it in ma->chosen_part
+ * and then set ma->chosen_pref to that MIME type's position in our preference
+ * list.  If we then hit another match, we only replace the first match if
+ * the preference value is lower.
  */
 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
                void *content, char *cbtype, char *cbcharset, size_t length,
@@ -1223,8 +1250,12 @@ void choose_preferred(char *name, char *filename, char *partnum, char *disp,
        if (ma->is_ma > 0) {
                for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
                        extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
+                       lprintf(CTDL_DEBUG, "Is <%s> == <%s> ??\n", buf, cbtype);
                        if ( (!strcasecmp(buf, cbtype)) && (!ma->freeze) ) {
-                               safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
+                               if (i < ma->chosen_pref) {
+                                       safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
+                                       ma->chosen_pref = i;
+                               }
                        }
                }
        }
@@ -1482,6 +1513,32 @@ int CtdlOutputPreLoadedMsg(
                return((CC->download_fp != NULL) ? om_ok : om_mime_error);
        }
 
+       /* MT_SPEW_SECTION is like MT_DOWNLOAD except it outputs the whole MIME part
+        * in a single server operation instead of opening a download file.
+        */
+       if (mode == MT_SPEW_SECTION) {
+               if (TheMessage->cm_format_type != FMT_RFC822) {
+                       if (do_proto)
+                               cprintf("%d This is not a MIME message.\n",
+                               ERROR + ILLEGAL_VALUE);
+               } else {
+                       /* Parse the message text component */
+                       int found_it = 0;
+
+                       mptr = TheMessage->cm_fields['M'];
+                       mime_parser(mptr, NULL, *mime_spew_section, NULL, NULL, (void *)&found_it, 0);
+                       /* If section wasn't found, print an error
+                        */
+                       if (!found_it) {
+                               if (do_proto) cprintf(
+                                       "%d Section %s not found.\n",
+                                       ERROR + FILE_NOT_FOUND,
+                                       CC->download_desired_section);
+                       }
+               }
+               return((CC->download_fp != NULL) ? om_ok : om_mime_error);
+       }
+
        /* now for the user-mode message reading loops */
        if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
 
@@ -1584,6 +1641,12 @@ int CtdlOutputPreLoadedMsg(
                                else if (i == 'Y') {
                                        cprintf("CC: %s%s", mptr, nl);
                                }
+                               else if (i == 'P') {
+                                       cprintf("Return-Path: %s%s", mptr, nl);
+                               }
+                               else if (i == 'V') {
+                                       cprintf("Envelope-To: %s%s", mptr, nl);
+                               }
                                else if (i == 'U') {
                                        cprintf("Subject: %s%s", mptr, nl);
                                        subject_found = 1;
@@ -1760,6 +1823,7 @@ START_TEXT:
                if (mode == MT_MIME) {
                        ma.use_fo_hooks = 0;
                        strcpy(ma.chosen_part, "1");
+                       ma.chosen_pref = 9999;
                        mime_parser(mptr, NULL,
                                *choose_preferred, *fixed_output_pre,
                                *fixed_output_post, (void *)&ma, 0);
@@ -1894,6 +1958,23 @@ void cmd_opna(char *cmdbuf)
        CtdlOutputMsg(msgid, MT_DOWNLOAD, 0, 1, 1, NULL);
 }                      
 
+
+/*
+ * Open a component of a MIME message and transmit it all at once
+ */
+void cmd_dlat(char *cmdbuf)
+{
+       long msgid;
+       char desired_section[128];
+
+       msgid = extract_long(cmdbuf, 0);
+       extract_token(desired_section, cmdbuf, 1, '|', sizeof desired_section);
+       safestrncpy(CC->download_desired_section, desired_section,
+               sizeof CC->download_desired_section);
+       CtdlOutputMsg(msgid, MT_SPEW_SECTION, 0, 1, 1, NULL);
+}                      
+
+
 /*
  * Save one or more message pointers into a specified room
  * (Returns 0 for success, nonzero for failure)
@@ -2630,7 +2711,7 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
 /*
  * Convenience function for generating small administrative messages.
  */
-void quickie_message(char *from, char *to, char *room, char *text, 
+void quickie_message(char *from, char *fromaddr, char *to, char *room, char *text, 
                        int format_type, char *subject)
 {
        struct CtdlMessage *msg;
@@ -2641,7 +2722,21 @@ void quickie_message(char *from, char *to, char *room, char *text,
        msg->cm_magic = CTDLMESSAGE_MAGIC;
        msg->cm_anon_type = MES_NORMAL;
        msg->cm_format_type = format_type;
-       msg->cm_fields['A'] = strdup(from);
+
+       if (from != NULL) {
+               msg->cm_fields['A'] = strdup(from);
+       }
+       else if (fromaddr != NULL) {
+               msg->cm_fields['A'] = strdup(fromaddr);
+               if (strchr(msg->cm_fields['A'], '@')) {
+                       *strchr(msg->cm_fields['A'], '@') = 0;
+               }
+       }
+       else {
+               msg->cm_fields['A'] = strdup("Citadel");
+       }
+
+       if (fromaddr != NULL) msg->cm_fields['F'] = strdup(fromaddr);
        if (room != NULL) msg->cm_fields['O'] = strdup(room);
        msg->cm_fields['N'] = strdup(NODENAME);
        if (to != NULL) {