]> code.citadel.org Git - citadel.git/blobdiff - citadel/msgbase.c
* Bugfixes
[citadel.git] / citadel / msgbase.c
index e2735f1e06df0b1afc1d7306f8bcd9035e36f37d..94405b53fc5465d02a5a869fdb6d75bca9eb4cc2 100644 (file)
@@ -284,6 +284,21 @@ int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
 }
 
 
+
+/*
+ * Retrieve the "seen" message list for the current room.
+ */
+void CtdlGetSeen(char *buf) {
+       struct visit vbuf;
+
+       /* Learn about the user and room in question */
+       CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+
+       safestrncpy(buf, vbuf.v_seen, SIZ);
+}
+
+
+
 /*
  * Manipulate the "seen msgs" string.
  */
@@ -300,8 +315,6 @@ void CtdlSetSeen(long target_msgnum, int target_setting) {
        int num_msgs = 0;
 
        /* Learn about the user and room in question */
-       get_mm();
-       getuser(&CC->usersupp, CC->curr_user);
        CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
 
        /* Load the message list */
@@ -373,7 +386,6 @@ void CtdlSetSeen(long target_msgnum, int target_setting) {
  * current room.  (Returns the number of messages processed.)
  */
 int CtdlForEachMessage(int mode, long ref,
-                       int moderation_level,
                        char *content_type,
                        struct CtdlMessage *compare,
                        void (*CallBack) (long, void *),
@@ -414,19 +426,23 @@ int CtdlForEachMessage(int mode, long ref,
         * Now begin the traversal.
         */
        if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
-               GetMetaData(&smi, msglist[a]);
-
-               /* Filter out messages that are moderated below the level
-                * currently being viewed at.
-                */
-               if (smi.meta_mod < moderation_level) {
-                       msglist[a] = 0L;
-               }
 
                /* If the caller is looking for a specific MIME type, filter
                 * out all messages which are not of the type requested.
                 */
                if (content_type != NULL) if (strlen(content_type) > 0) {
+
+                       /* This call to GetMetaData() sits inside this loop
+                        * so that we only do the extra database read per msg
+                        * if we need to.  Doing the extra read all the time
+                        * really kills the server.  If we ever need to use
+                        * metadata for another search criterion, we need to
+                        * move the read somewhere else -- but still be smart
+                        * enough to only do the read if the caller has
+                        * specified something that will need it.
+                        */
+                       GetMetaData(&smi, msglist[a]);
+
                        if (strcasecmp(smi.meta_content_type, content_type)) {
                                msglist[a] = 0L;
                        }
@@ -550,7 +566,6 @@ void cmd_msgs(char *cmdbuf)
        }
 
        CtdlForEachMessage(mode, cm_ref,
-               CC->usersupp.moderation_filter,
                NULL, template, simple_listing, NULL);
        if (template != NULL) CtdlFreeMessage(template);
        cprintf("000\n");
@@ -693,6 +708,26 @@ void list_this_part(char *name, char *filename, char *partnum, char *disp,
                name, filename, partnum, disp, cbtype, (long)length);
 }
 
+/* 
+ * Callback function for multipart prefix
+ */
+void list_this_pref(char *name, char *filename, char *partnum, char *disp,
+                   void *content, char *cbtype, size_t length, char *encoding,
+                   void *cbuserdata)
+{
+       cprintf("pref=%s|%s\n", partnum, cbtype);
+}
+
+/* 
+ * Callback function for multipart sufffix
+ */
+void list_this_suff(char *name, char *filename, char *partnum, char *disp,
+                   void *content, char *cbtype, size_t length, char *encoding,
+                   void *cbuserdata)
+{
+       cprintf("suff=%s|%s\n", partnum, cbtype);
+}
+
 
 /*
  * Callback function for mime parser that opens a section for downloading
@@ -846,7 +881,7 @@ void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
 {
                lprintf(9, "fixed_output_pre() type=<%s>\n", cbtype);   
                if (!strcasecmp(cbtype, "multipart/alternative")) {
-                       ma->is_ma = 1;
+                       ++ma->is_ma;
                        ma->did_print = 0;
                        return;
                }
@@ -861,7 +896,7 @@ void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
 {
                lprintf(9, "fixed_output_post() type=<%s>\n", cbtype);  
                if (!strcasecmp(cbtype, "multipart/alternative")) {
-                       ma->is_ma = 0;
+                       --ma->is_ma;
                        ma->did_print = 0;
                        return;
                }
@@ -877,7 +912,6 @@ void fixed_output(char *name, char *filename, char *partnum, char *disp,
                char *ptr;
                char *wptr;
                size_t wlen;
-               CIT_UBYTE ch = 0;
 
                lprintf(9, "fixed_output() type=<%s>\n", cbtype);       
 
@@ -893,26 +927,20 @@ void fixed_output(char *name, char *filename, char *partnum, char *disp,
        
                if ( (!strcasecmp(cbtype, "text/plain")) 
                   || (strlen(cbtype)==0) ) {
-                       wlen = length;
                        wptr = content;
-                       while (wlen--) {
-                               ch = *wptr++;
-                               /**********
-                               if (ch==10) cprintf("\r\n");
-                               else cprintf("%c", ch);
-                                **********/
-                               cprintf("%c", ch);
+                       if (length > 0) {
+                               client_write(wptr, length);
+                               if (wptr[length-1] != '\n') {
+                                       cprintf("\n");
+                               }
                        }
-                       if (ch != '\n') cprintf("\n");
                }
                else if (!strcasecmp(cbtype, "text/html")) {
                        ptr = html_to_ascii(content, 80, 0);
                        wlen = strlen(ptr);
-                       wptr = ptr;
-                       while (wlen--) {
-                               ch = *wptr++;
-                               if (ch==10) cprintf("\r\n");
-                               else cprintf("%c", ch);
+                       client_write(ptr, wlen);
+                       if (ptr[wlen-1] != '\n') {
+                               cprintf("\n");
                        }
                        phree(ptr);
                }
@@ -922,6 +950,73 @@ 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.
+ */
+void choose_preferred(char *name, char *filename, char *partnum, char *disp,
+               void *content, char *cbtype, size_t length, char *encoding,
+               void *cbuserdata)
+{
+       char buf[SIZ];
+       int i;
+
+       if (ma->is_ma > 0) {
+               for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
+                       extract(buf, CC->preferred_formats, i);
+                       if (!strcasecmp(buf, cbtype)) {
+                               strcpy(ma->chosen_part, partnum);
+                       }
+               }
+       }
+}
+
+/*
+ * Now that we've chosen our preferred part, output it.
+ */
+void output_preferred(char *name, char *filename, char *partnum, char *disp,
+               void *content, char *cbtype, size_t length, char *encoding,
+               void *cbuserdata)
+{
+       int i;
+       char buf[SIZ];
+       int add_newline = 0;
+       char *text_content;
+
+       /* This is not the MIME part you're looking for... */
+       if (strcasecmp(partnum, ma->chosen_part)) return;
+
+       /* If the content-type of this part is in our preferred formats
+        * list, we can simply output it verbatim.
+        */
+       for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
+               extract(buf, CC->preferred_formats, i);
+               if (!strcasecmp(buf, cbtype)) {
+                       /* Yeah!  Go!  W00t!! */
+
+                       text_content = (char *)content;
+                       if (text_content[length-1] != '\n') {
+                               ++add_newline;
+                       }
+
+                       cprintf("Content-type: %s\n", cbtype);
+                       cprintf("Content-length: %d\n",
+                               length + add_newline);
+                       cprintf("Content-transfer-encoding: %s\n", encoding);
+                       cprintf("\n");
+                       client_write(content, length);
+                       if (add_newline) cprintf("\n");
+                       return;
+               }
+       }
+
+       /* No translations required or possible: output as text/plain */
+       cprintf("Content-type: text/plain\n\n");
+       fixed_output(name, filename, partnum, disp, content, cbtype,
+                       length, encoding, cbuserdata);
+}
+
 
 /*
  * Get a message off disk.  (returns om_* values found in msgbase.h)
@@ -961,9 +1056,22 @@ int CtdlOutputMsg(long msg_num,           /* message number (local) to fetch */
         */
 
        /*
-        * Fetch the message from disk
+        * Fetch the message from disk.  We also keep the most recently
+        * read message in memory, in case we want to read it again, or fetch
+        * MIME parts out of it, or whatever.
         */
-       TheMessage = CtdlFetchMessage(msg_num);
+       if ( (CC->cached_msg != NULL) && (CC->cached_msgnum == msg_num) ) {
+               TheMessage = CC->cached_msg;
+       }
+       else {
+               TheMessage = CtdlFetchMessage(msg_num);
+               if (CC->cached_msg != NULL) {
+                       phree(CC->cached_msg); /* FIXME efence? */
+               }
+               CC->cached_msg = TheMessage;
+               CC->cached_msgnum = msg_num;
+       }
+
        if (TheMessage == NULL) {
                if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
                        ERROR, msg_num);
@@ -974,7 +1082,9 @@ int CtdlOutputMsg(long msg_num,            /* message number (local) to fetch */
                        TheMessage, msg_num, mode,
                        headers_only, do_proto, crlf);
 
-       CtdlFreeMessage(TheMessage);
+       /* don't free the memory; we're keeping it in the cache */
+       /* CtdlFreeMessage(TheMessage); */
+
        return(retcode);
 }
 
@@ -1046,25 +1156,23 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
                return((CC->download_fp != NULL) ? om_ok : om_mime_error);
        }
 
+       /* Does the caller want to skip the headers? */
+       if (headers_only == HEADERS_NONE) goto START_TEXT;
+
        /* now for the user-mode message reading loops */
        if (do_proto) cprintf("%d Message %ld:\n", LISTING_FOLLOWS, msg_num);
 
-       /* Tell the client which format type we're using.  If this is a
-        * MIME message, *lie* about it and tell the user it's fixed-format.
-        */
-       if (mode == MT_CITADEL) {
-               if (TheMessage->cm_format_type == FMT_RFC822) {
-                       if (do_proto) cprintf("type=1\n");
-               }
-               else {
-                       if (do_proto) cprintf("type=%d\n",
-                               TheMessage->cm_format_type);
-               }
+       /* Tell the client which format type we're using. */
+       if ( (mode == MT_CITADEL) && (do_proto) ) {
+               cprintf("type=%d\n", TheMessage->cm_format_type);
        }
 
        /* nhdr=yes means that we're only displaying headers, no body */
-       if ((TheMessage->cm_anon_type == MES_ANONONLY) && (mode == MT_CITADEL)) {
-               if (do_proto) cprintf("nhdr=yes\n");
+       if ( (TheMessage->cm_anon_type == MES_ANONONLY)
+           && (mode == MT_CITADEL)
+          && (do_proto)
+          ) {
+               cprintf("nhdr=yes\n");
        }
 
        /* begin header processing loop for Citadel message format */
@@ -1174,8 +1282,8 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
                                else if (i == 'R')
                                        cprintf("To: %s%s", mptr, nl);
                                else if (i == 'T') {
-                                       datestring(datestamp, sizeof datestamp, atol(mptr),
-                                               DATESTRING_RFC822 );
+                                       datestring(datestamp, sizeof datestamp,
+                                               atol(mptr), DATESTRING_RFC822);
                                        cprintf("Date: %s%s", datestamp, nl);
                                }
                        }
@@ -1212,23 +1320,18 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
        }
 
        /* end header processing loop ... at this point, we're in the text */
-
+START_TEXT:
        mptr = TheMessage->cm_fields['M'];
 
        /* Tell the client about the MIME parts in this message */
        if (TheMessage->cm_format_type == FMT_RFC822) {
-               if (mode == MT_CITADEL) {
+               if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
                        mime_parser(mptr, NULL,
-                               *list_this_part, NULL, NULL,
+                               *list_this_part,
+                               *list_this_pref,
+                               *list_this_suff,
                                NULL, 0);
                }
-               else if (mode == MT_MIME) {     /* list parts only */
-                       mime_parser(mptr, NULL,
-                               *list_this_part, NULL, NULL,
-                               NULL, 0);
-                       if (do_proto) cprintf("000\n");
-                       return(om_ok);
-               }
                else if (mode == MT_RFC822) {   /* unparsed RFC822 dump */
                        /* FIXME ... we have to put some code in here to avoid
                         * printing duplicate header information when both
@@ -1245,14 +1348,15 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
                }
        }
 
-       if (headers_only) {
+       if (headers_only == HEADERS_ONLY) {
                if (do_proto) cprintf("000\n");
                return(om_ok);
        }
 
        /* signify start of msg text */
-       if (mode == MT_CITADEL)
+       if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
                if (do_proto) cprintf("text\n");
+       }
        if (mode == MT_RFC822) {
                if (TheMessage->cm_fields['U'] == NULL) {
                        cprintf("Subject: (no subject)%s", nl);
@@ -1265,6 +1369,9 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
         * what message transfer format is in use.
         */
        if (TheMessage->cm_format_type == FMT_FIXED) {
+               if (mode == MT_MIME) {
+                       cprintf("Content-type: text/plain\n\n");
+               }
                strcpy(buf, "");
                while (ch = *mptr++, ch > 0) {
                        if (ch == 13)
@@ -1289,6 +1396,9 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
         * message to the reader's screen width.
         */
        if (TheMessage->cm_format_type == FMT_CITADEL) {
+               if (mode == MT_MIME) {
+                       cprintf("Content-type: text/x-citadel-variformat\n\n");
+               }
                memfmout(80, mptr, 0, nl);
        }
 
@@ -1300,9 +1410,20 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
        if (TheMessage->cm_format_type == FMT_RFC822) {
                CtdlAllocUserData(SYM_MA_INFO, sizeof(struct ma_info));
                memset(ma, 0, sizeof(struct ma_info));
-               mime_parser(mptr, NULL,
-                       *fixed_output, *fixed_output_pre, *fixed_output_post,
-                       NULL, 0);
+
+               if (mode == MT_MIME) {
+                       strcpy(ma->chosen_part, "1");
+                       mime_parser(mptr, NULL,
+                               *choose_preferred, *fixed_output_pre,
+                               *fixed_output_post, NULL, 0);
+                       mime_parser(mptr, NULL,
+                               *output_preferred, NULL, NULL, NULL, 0);
+               }
+               else {
+                       mime_parser(mptr, NULL,
+                               *fixed_output, *fixed_output_pre,
+                               *fixed_output_post, NULL, 0);
+               }
        }
 
        /* now we're done */
@@ -1318,7 +1439,7 @@ int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage,
 void cmd_msg0(char *cmdbuf)
 {
        long msgid;
-       int headers_only = 0;
+       int headers_only = HEADERS_ALL;
 
        msgid = extract_long(cmdbuf, 0);
        headers_only = extract_int(cmdbuf, 1);
@@ -1334,7 +1455,7 @@ void cmd_msg0(char *cmdbuf)
 void cmd_msg2(char *cmdbuf)
 {
        long msgid;
-       int headers_only = 0;
+       int headers_only = HEADERS_ALL;
 
        msgid = extract_long(cmdbuf, 0);
        headers_only = extract_int(cmdbuf, 1);
@@ -1384,7 +1505,7 @@ void cmd_msg3(char *cmdbuf)
 
 
 /* 
- * display a message (mode 4 - MIME) (FIXME ... still evolving, not complete)
+ * Display a message using MIME content types
  */
 void cmd_msg4(char *cmdbuf)
 {
@@ -1394,6 +1515,19 @@ void cmd_msg4(char *cmdbuf)
        CtdlOutputMsg(msgid, MT_MIME, 0, 1, 0);
 }
 
+
+
+/* 
+ * Client tells us its preferred message format(s)
+ */
+void cmd_msgp(char *cmdbuf)
+{
+       safestrncpy(CC->preferred_formats, cmdbuf,
+                       sizeof(CC->preferred_formats));
+       cprintf("%d ok\n", CIT_OK);
+}
+
+
 /*
  * Open a component of a MIME message as a download file 
  */
@@ -1691,8 +1825,7 @@ int ReplicationChecks(struct CtdlMessage *msg) {
        memset(template, 0, sizeof(struct CtdlMessage));
        template->cm_fields['E'] = strdoop(msg->cm_fields['E']);
 
-       CtdlForEachMessage(MSGS_ALL, 0L, (-127), NULL, template,
-               check_repl, NULL);
+       CtdlForEachMessage(MSGS_ALL, 0L, NULL, template, check_repl, NULL);
 
        /* If a newer message exists with the same Extended ID, abort
         * this save.
@@ -1763,7 +1896,12 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,      /* message to save */
                }
        }
 
-       strcpy(force_room, force);
+       if (force == NULL) {
+               strcpy(force_room, "");
+       }
+       else {
+               strcpy(force_room, force);
+       }
 
        /* Learn about what's inside, because it's what's inside that counts */
        lprintf(9, "Learning what's inside\n");
@@ -1868,7 +2006,7 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
        if ((!CC->internal_pgm) || (recps == NULL)) {
                if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0) != 0) {
                        lprintf(3, "ERROR saving message pointer!\n");
-                       CtdlSaveMsgPointerInRoom(AIDEROOM, newmsgid, 0);
+                       CtdlSaveMsgPointerInRoom(config.c_aideroom, newmsgid, 0);
                }
        }
 
@@ -1905,10 +2043,11 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,     /* message to save */
                if (getuser(&userbuf, recipient) == 0) {
                        MailboxName(actual_rm, sizeof actual_rm, &userbuf, MAILROOM);
                        CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0);
+                       BumpNewMailCounter(userbuf.usernum);
                }
                else {
                        lprintf(9, "No user <%s>\n", recipient);
-                       CtdlSaveMsgPointerInRoom(AIDEROOM, newmsgid, 0);
+                       CtdlSaveMsgPointerInRoom(config.c_aideroom, newmsgid, 0);
                }
        }
 
@@ -2002,25 +2141,32 @@ 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 *to, char *room, char *text, 
+                       int format_type, char *subject)
 {
        struct CtdlMessage *msg;
+       struct recptypes *recp = NULL;
 
        msg = mallok(sizeof(struct CtdlMessage));
        memset(msg, 0, sizeof(struct CtdlMessage));
        msg->cm_magic = CTDLMESSAGE_MAGIC;
        msg->cm_anon_type = MES_NORMAL;
-       msg->cm_format_type = 0;
+       msg->cm_format_type = format_type;
        msg->cm_fields['A'] = strdoop(from);
-       msg->cm_fields['O'] = strdoop(room);
+       if (room != NULL) msg->cm_fields['O'] = strdoop(room);
        msg->cm_fields['N'] = strdoop(NODENAME);
-       if (to != NULL)
+       if (to != NULL) {
                msg->cm_fields['R'] = strdoop(to);
+               recp = validate_recipients(to);
+       }
+       if (subject != NULL) {
+               msg->cm_fields['U'] = strdoop(subject);
+       }
        msg->cm_fields['M'] = strdoop(text);
 
-       CtdlSubmitMsg(msg, NULL, room);
+       CtdlSubmitMsg(msg, recp, room);
        CtdlFreeMessage(msg);
-       syslog(LOG_NOTICE, text);
+       if (recp != NULL) phree(recp);
 }
 
 
@@ -2284,7 +2430,7 @@ struct recptypes *validate_recipients(char *recipients) {
                        case MES_LOCAL:
                                if (!strcasecmp(this_recp, "sysop")) {
                                        ++ret->num_room;
-                                       strcpy(this_recp, AIDEROOM);
+                                       strcpy(this_recp, config.c_aideroom);
                                        if (strlen(ret->recp_room) > 0) {
                                                strcat(ret->recp_room, "|");
                                        }
@@ -2428,7 +2574,7 @@ void cmd_ent0(char *entargs)
                memset(CC->fake_postname, 0, sizeof(CC->fake_postname) );
                safestrncpy(CC->fake_postname, newusername,
                        sizeof(CC->fake_postname) );
-               cprintf("%d ok\n", OK);
+               cprintf("%d ok\n", CIT_OK);
                return;
        }
        CC->cs_flags |= CS_POSTING;
@@ -2490,7 +2636,7 @@ void cmd_ent0(char *entargs)
         * success without creating the message.
         */
        if (post == 0) {
-               cprintf("%d %s\n", OK,
+               cprintf("%d %s\n", CIT_OK,
                        ((valid != NULL) ? valid->display_recp : "") );
                phree(valid);
                return;
@@ -2653,7 +2799,7 @@ void cmd_dele(char *delstr)
        num_deleted = CtdlDeleteMessages(CC->quickroom.QRname, delnum, "");
 
        if (num_deleted) {
-               cprintf("%d %d message%s deleted.\n", OK,
+               cprintf("%d %d message%s deleted.\n", CIT_OK,
                        num_deleted, ((num_deleted != 1) ? "s" : ""));
        } else {
                cprintf("%d Message %ld not found.\n", ERROR, delnum);
@@ -2727,7 +2873,7 @@ void cmd_move(char *args)
                CtdlDeleteMessages(CC->quickroom.QRname, num, "");
        }
 
-       cprintf("%d Message %s.\n", OK, (is_copy ? "copied" : "moved") );
+       cprintf("%d Message %s.\n", CIT_OK, (is_copy ? "copied" : "moved") );
 }
 
 
@@ -2905,7 +3051,7 @@ void CtdlWriteObject(char *req_room,              /* Room to stuff it in */
        if (getroom(&qrbuf, roomname) != 0) {
                create_room(roomname, 
                        ( (is_mailbox != NULL) ? 5 : 3 ),
-                       "", 0, 1);
+                       "", 0, 1, 0);
        }
        /* If the caller specified this object as unique, delete all
         * other objects of this type that are currently in the room.
@@ -2946,7 +3092,7 @@ char *CtdlGetSysConfig(char *sysconfname) {
        /* We want the last (and probably only) config in this room */
        begin_critical_section(S_CONFIG);
        config_msgnum = (-1L);
-       CtdlForEachMessage(MSGS_LAST, 1, (-127), sysconfname, NULL,
+       CtdlForEachMessage(MSGS_LAST, 1, sysconfname, NULL,
                CtdlGetSysConfigBackend, NULL);
        msgnum = config_msgnum;
        end_critical_section(S_CONFIG);