]> code.citadel.org Git - citadel.git/blobdiff - citadel/msgbase.c
* Bounds checking in CtdlDirectoryLookup()
[citadel.git] / citadel / msgbase.c
index cf73c74f956d3facef92bc534d6a36e6411676fe..a5035ce27644915ec79a25e61efc2b63c945d477 100644 (file)
@@ -142,7 +142,13 @@ int alias(char *name)
        striplt(name);
        remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
 
-       fp = fopen("network/mail.aliases", "r");
+       fp = fopen(
+#ifndef HAVE_ETG_DIR
+                          "network/"
+#else
+                          ETC_DIR
+#endif
+                          "mail.aliases", "r");
        if (fp == NULL) {
                fp = fopen("/dev/null", "r");
        }
@@ -168,7 +174,7 @@ int alias(char *name)
        fclose(fp);
 
        /* Hit the Global Address Book */
-       if (CtdlDirectoryLookup(aaa, name) == 0) {
+       if (CtdlDirectoryLookup(aaa, name, sizeof aaa) == 0) {
                strcpy(name, aaa);
        }
 
@@ -237,7 +243,13 @@ void get_mm(void)
 {
        FILE *fp;
 
-       fp = fopen("citadel.control", "r");
+       fp = fopen(
+#ifndef HAVE_RUN_DIR
+                          "."
+#else
+                          RUN_DIR
+#endif
+                          "/citadel.control", "r");
        if (fp == NULL) {
                lprintf(CTDL_CRIT, "Cannot open citadel.control: %s\n",
                        strerror(errno));
@@ -248,7 +260,9 @@ void get_mm(void)
 }
 
 
-
+/*
+ * Back end for the MSGS command: output message number only.
+ */
 void simple_listing(long msgnum, void *userdata)
 {
        cprintf("%ld\n", msgnum);
@@ -256,6 +270,32 @@ void simple_listing(long msgnum, void *userdata)
 
 
 
+/*
+ * Back end for the MSGS command: output header summary.
+ */
+void headers_listing(long msgnum, void *userdata)
+{
+       struct CtdlMessage *msg;
+
+       msg = CtdlFetchMessage(msgnum, 0);
+       if (msg < 0L) {
+               cprintf("%ld|0|||||\n", msgnum);
+               return;
+       }
+
+       cprintf("%ld|%s|%s|%s|%s|%s|\n",
+               msgnum,
+               (msg->cm_fields['T'] ? msg->cm_fields['T'] : "0"),
+               (msg->cm_fields['A'] ? msg->cm_fields['A'] : ""),
+               (msg->cm_fields['N'] ? msg->cm_fields['N'] : ""),
+               (msg->cm_fields['F'] ? msg->cm_fields['F'] : ""),
+               (msg->cm_fields['U'] ? msg->cm_fields['U'] : "")
+       );
+       CtdlFreeMessage(msg);
+}
+
+
+
 /* Determine if a given message matches the fields in a message template.
  * Return 0 for a successful match.
  */
@@ -306,13 +346,16 @@ void CtdlGetSeen(char *buf, int which_set) {
 /*
  * Manipulate the "seen msgs" string (or other message set strings)
  */
-void CtdlSetSeen(long target_msgnum, int target_setting, int which_set) {
+void CtdlSetSeen(long target_msgnum, int target_setting, int which_set,
+               struct ctdluser *which_user, struct ctdlroom *which_room) {
        struct cdbdata *cdbfr;
-       int i;
+       int i, j;
        int is_seen = 0;
-       int was_seen = 1;
+       int was_seen = 0;
        long lo = (-1L);
        long hi = (-1L);
+       long t = (-1L);
+       int trimming = 0;
        struct visit vbuf;
        long *msglist;
        int num_msgs = 0;
@@ -321,12 +364,16 @@ void CtdlSetSeen(long target_msgnum, int target_setting, int which_set) {
        int num_sets;
        int s;
        char setstr[SIZ], lostr[SIZ], histr[SIZ];
+       size_t tmp;
 
        lprintf(CTDL_DEBUG, "CtdlSetSeen(%ld, %d, %d)\n",
                target_msgnum, target_setting, which_set);
 
        /* Learn about the user and room in question */
-       CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
+       CtdlGetRelationship(&vbuf,
+               ((which_user != NULL) ? which_user : &CC->user),
+               ((which_room != NULL) ? which_room : &CC->room)
+       );
 
        /* Load the message list */
        cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
@@ -381,6 +428,8 @@ void CtdlSetSeen(long target_msgnum, int target_setting, int which_set) {
 
        /* Now translate the array of booleans back into a sequence set */
        strcpy(vset, "");
+       lo = (-1L);
+       hi = (-1L);
 
        for (i=0; i<num_msgs; ++i) {
                is_seen = 0;
@@ -389,34 +438,46 @@ void CtdlSetSeen(long target_msgnum, int target_setting, int which_set) {
                        is_seen = target_setting;
                }
                else {
-                       if (is_set[i]) {
-                               is_seen = 1;
-                       }
+                       is_seen = is_set[i];
                }
 
-               if (is_seen == 1) {
+               if (is_seen) {
                        if (lo < 0L) lo = msglist[i];
                        hi = msglist[i];
                }
+
                if (  ((is_seen == 0) && (was_seen == 1))
                   || ((is_seen == 1) && (i == num_msgs-1)) ) {
-                       size_t tmp;
 
-                       if ( (strlen(vset) + 20) > sizeof vset) {
-                               strcpy(vset, &vset[20]);
-                               vset[0] = '*';
+                       /* begin trim-o-matic code */
+                       j=9;
+                       trimming = 0;
+                       while ( (strlen(vset) + 20) > sizeof vset) {
+                               remove_token(vset, 0, ',');
+                               trimming = 1;
+                               if (j--) break; /* loop no more than 9 times */
+                       }
+                       if ( (trimming) && (which_set == ctdlsetseen_seen) ) {
+                               t = atol(vset);
+                               if (t<2) t=2;
+                               --t;
+                               snprintf(lostr, sizeof lostr,
+                                       "1:%ld,%s", t, vset);
+                               safestrncpy(vset, lostr, sizeof vset);
                        }
+                       /* end trim-o-matic code */
+
                        tmp = strlen(vset);
                        if (tmp > 0) {
                                strcat(vset, ",");
-                               tmp++;
+                               ++tmp;
                        }
                        if (lo == hi) {
-                               snprintf(&vset[tmp], sizeof vset - tmp,
+                               snprintf(&vset[tmp], (sizeof vset) - tmp,
                                         "%ld", lo);
                        }
                        else {
-                               snprintf(&vset[tmp], sizeof vset - tmp,
+                               snprintf(&vset[tmp], (sizeof vset) - tmp,
                                         "%ld:%ld", lo, hi);
                        }
                        lo = (-1L);
@@ -439,7 +500,10 @@ void CtdlSetSeen(long target_msgnum, int target_setting, int which_set) {
 
        /* lprintf(CTDL_DEBUG, " after optimize: %s\n", vset); */
        free(msglist);
-       CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
+       CtdlSetRelationship(&vbuf,
+               ((which_user != NULL) ? which_user : &CC->user),
+               ((which_room != NULL) ? which_room : &CC->room)
+       );
 }
 
 
@@ -463,7 +527,7 @@ int CtdlForEachMessage(int mode, long ref,
        long thismsg;
        struct MetaData smi;
        struct CtdlMessage *msg;
-       int is_seen;
+       int is_seen = 0;
        long lastold = 0L;
        int printed_lastold = 0;
 
@@ -538,8 +602,14 @@ int CtdlForEachMessage(int mode, long ref,
        if (num_msgs > 0)
                for (a = 0; a < num_msgs; ++a) {
                        thismsg = msglist[a];
-                       is_seen = is_msg_in_sequence_set(vbuf.v_seen, thismsg);
-                       if (is_seen) lastold = thismsg;
+                       if (mode == MSGS_ALL) {
+                               is_seen = 0;
+                       }
+                       else {
+                               is_seen = is_msg_in_sequence_set(
+                                                       vbuf.v_seen, thismsg);
+                               if (is_seen) lastold = thismsg;
+                       }
                        if ((thismsg > 0L)
                            && (
 
@@ -583,10 +653,12 @@ void cmd_msgs(char *cmdbuf)
        int i;
        int with_template = 0;
        struct CtdlMessage *template = NULL;
+       int with_headers = 0;
 
        extract_token(which, cmdbuf, 0, '|', sizeof which);
        cm_ref = extract_int(cmdbuf, 1);
        with_template = extract_int(cmdbuf, 2);
+       with_headers = extract_int(cmdbuf, 3);
 
        mode = MSGS_ALL;
        strcat(which, "   ");
@@ -626,11 +698,16 @@ void cmd_msgs(char *cmdbuf)
                buffer_output();
        }
        else {
-               cprintf("%d Message list...\n", LISTING_FOLLOWS);
+               cprintf("%d  \n", LISTING_FOLLOWS);
        }
 
-       CtdlForEachMessage(mode, cm_ref,
-               NULL, template, simple_listing, NULL);
+       CtdlForEachMessage(mode,
+                       cm_ref,
+                       NULL,
+                       template,
+                       (with_headers ? headers_listing : simple_listing),
+                       NULL
+       );
        if (template != NULL) CtdlFreeMessage(template);
        cprintf("000\n");
 }
@@ -765,7 +842,7 @@ void memfmout(
  * Callback function for mime parser that simply lists the part
  */
 void list_this_part(char *name, char *filename, char *partnum, char *disp,
-                   void *content, char *cbtype, size_t length, char *encoding,
+                   void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
                    void *cbuserdata)
 {
 
@@ -777,7 +854,7 @@ void list_this_part(char *name, char *filename, char *partnum, char *disp,
  * 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 *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
                    void *cbuserdata)
 {
        cprintf("pref=%s|%s\n", partnum, cbtype);
@@ -787,7 +864,7 @@ void list_this_pref(char *name, char *filename, char *partnum, char *disp,
  * 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 *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
                    void *cbuserdata)
 {
        cprintf("suff=%s|%s\n", partnum, cbtype);
@@ -798,8 +875,8 @@ void list_this_suff(char *name, char *filename, char *partnum, char *disp,
  * Callback function for mime parser that opens a section for downloading
  */
 void mime_download(char *name, char *filename, char *partnum, char *disp,
-                  void *content, char *cbtype, size_t length, char *encoding,
-                  void *cbuserdata)
+                  void *content, char *cbtype, char *cbcharset, size_t length,
+                  char *encoding, void *cbuserdata)
 {
 
        /* Silently go away if there's already a download open... */
@@ -957,7 +1034,7 @@ void CtdlFreeMessage(struct CtdlMessage *msg)
  *
  */
 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
-               void *content, char *cbtype, size_t length, char *encoding,
+               void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
                void *cbuserdata)
 {
        struct ma_info *ma;
@@ -975,7 +1052,7 @@ void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
  * Post callback function for multipart/alternative
  */
 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
-               void *content, char *cbtype, size_t length, char *encoding,
+               void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
                void *cbuserdata)
 {
        struct ma_info *ma;
@@ -993,7 +1070,7 @@ void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
  * Inline callback function for mime parser that wants to display text
  */
 void fixed_output(char *name, char *filename, char *partnum, char *disp,
-               void *content, char *cbtype, size_t length, char *encoding,
+               void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
                void *cbuserdata)
        {
                char *ptr;
@@ -1046,7 +1123,7 @@ void fixed_output(char *name, char *filename, char *partnum, char *disp,
  * 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 *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
                void *cbuserdata)
 {
        char buf[1024];
@@ -1069,7 +1146,7 @@ void choose_preferred(char *name, char *filename, char *partnum, char *disp,
  * 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 *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
                void *cbuserdata)
 {
        int i;
@@ -1096,8 +1173,11 @@ void output_preferred(char *name, char *filename, char *partnum, char *disp,
                                ++add_newline;
                        }
 
-                       cprintf("Content-type: %s\n", cbtype);
-                       cprintf("Content-length: %d\n",
+                       cprintf("Content-type: %s", cbtype);
+                       if (strlen(cbcharset) > 0) {
+                               cprintf("; charset=%s", cbcharset);
+                       }
+                       cprintf("\nContent-length: %d\n",
                                (int)(length + add_newline) );
                        if (strlen(encoding) > 0) {
                                cprintf("Content-transfer-encoding: %s\n", encoding);
@@ -1114,7 +1194,7 @@ void output_preferred(char *name, char *filename, char *partnum, char *disp,
 
        /* No translations required or possible: output as text/plain */
        cprintf("Content-type: text/plain\n\n");
-       fixed_output(name, filename, partnum, disp, content, cbtype,
+       fixed_output(name, filename, partnum, disp, content, cbtype, cbcharset,
                        length, encoding, cbuserdata);
 }
 
@@ -1919,7 +1999,7 @@ void serialize_message(struct ser_ret *ret,               /* return values */
  */
 void check_repl(long msgnum, void *userdata) {
        lprintf(CTDL_DEBUG, "check_repl() replacing message %ld\n", msgnum);
-       CtdlDeleteMessages(CC->room.QRname, msgnum, "");
+       CtdlDeleteMessages(CC->room.QRname, msgnum, "", 0);
 }
 
 
@@ -2071,7 +2151,8 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
 
        lprintf(CTDL_DEBUG, "Final selection: %s\n", actual_rm);
        if (strcasecmp(actual_rm, CC->room.QRname)) {
-               getroom(&CC->room, actual_rm);
+               /* getroom(&CC->room, actual_rm); */
+               usergoto(actual_rm, 0, 1, NULL, NULL);
        }
 
        /*
@@ -2160,7 +2241,7 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
        }
 
        /* Bump this user's messages posted counter. */
-       lprintf(CTDL_DEBUG, "Updating user (FIXME defer this)\n");
+       lprintf(CTDL_DEBUG, "Updating user\n");
        lgetuser(&CC->user, CC->curr_user);
        CC->user.posted = CC->user.posted + 1;
        lputuser(&CC->user);
@@ -2216,8 +2297,13 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,      /* message to save */
                serialize_message(&smr, msg);
                if (smr.len > 0) {
                        snprintf(submit_filename, sizeof submit_filename,
-                               "./network/spoolin/netmail.%04lx.%04x.%04x",
-                               (long) getpid(), CC->cs_pid, ++seqnum);
+#ifndef HAVE_SPOOL_DIR
+                                        "."
+#else
+                                        SPOOL_DIR
+#endif
+                                        "/network/spoolin/netmail.%04lx.%04x.%04x",
+                                        (long) getpid(), CC->cs_pid, ++seqnum);
                        network_fp = fopen(submit_filename, "wb+");
                        if (network_fp != NULL) {
                                fwrite(smr.ser, smr.len, 1, network_fp);
@@ -2235,7 +2321,8 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
        /* Go back to the room we started from */
        lprintf(CTDL_DEBUG, "Returning to original room %s\n", hold_rm);
        if (strcasecmp(hold_rm, CC->room.QRname))
-               getroom(&CC->room, hold_rm);
+               /* getroom(&CC->room, hold_rm); */
+               usergoto(hold_rm, 0, 1, NULL, NULL);
 
        /* For internet mail, generate delivery instructions.
         * Yes, this is recursive.  Deal with it.  Infinite recursion does
@@ -2895,8 +2982,9 @@ void cmd_ent0(char *entargs)
  * (returns the actual number of messages deleted)
  */
 int CtdlDeleteMessages(char *room_name,                /* which room */
-                      long dmsgnum,            /* or "0" for any */
-                      char *content_type       /* or "" for any */
+                       long dmsgnum,           /* or "0" for any */
+                       char *content_type,     /* or "" for any */
+                       int deferred            /* let TDAP sweep it later */
 )
 {
 
@@ -2910,8 +2998,8 @@ int CtdlDeleteMessages(char *room_name,           /* which room */
        int delete_this;
        struct MetaData smi;
 
-       lprintf(CTDL_DEBUG, "CtdlDeleteMessages(%s, %ld, %s)\n",
-               room_name, dmsgnum, content_type);
+       lprintf(CTDL_DEBUG, "CtdlDeleteMessages(%s, %ld, %s, %d)\n",
+               room_name, dmsgnum, content_type, deferred);
 
        /* get room record, obtaining a lock... */
        if (lgetroom(&qrbuf, room_name) != 0) {
@@ -2962,6 +3050,20 @@ int CtdlDeleteMessages(char *room_name,          /* which room */
        }
        lputroom(&qrbuf);
 
+       /*
+        * If the delete operation is "deferred" (and technically, any delete
+        * operation not performed by THE DREADED AUTO-PURGER ought to be
+        * a deferred delete) then we save a pointer to the message in the
+        * DELETED_MSGS_ROOM.  This will cause the reference count to remain
+        * at least 1, which will save the user from having to synchronously
+        * wait for various disk-intensive operations to complete.
+        */
+       if ( (deferred) && (num_deleted) ) {
+               for (i=0; i<num_deleted; ++i) {
+                       CtdlCopyMsgToRoom(dellist[i], DELETED_MSGS_ROOM);
+               }
+       }
+
        /* Go through the messages we pulled out of the index, and decrement
         * their reference counts by 1.  If this is the only room the message
         * was in, the reference count will reach zero and the message will
@@ -3016,7 +3118,7 @@ void cmd_dele(char *delstr)
        }
        delnum = extract_long(delstr, 0);
 
-       num_deleted = CtdlDeleteMessages(CC->room.QRname, delnum, "");
+       num_deleted = CtdlDeleteMessages(CC->room.QRname, delnum, "", 1);
 
        if (num_deleted) {
                cprintf("%d %d message%s deleted.\n", CIT_OK,
@@ -3109,7 +3211,7 @@ void cmd_move(char *args)
         * if this is a 'move' rather than a 'copy' operation.
         */
        if (is_copy == 0) {
-               CtdlDeleteMessages(CC->room.QRname, num, "");
+               CtdlDeleteMessages(CC->room.QRname, num, "", 0);
        }
 
        cprintf("%d Message %s.\n", CIT_OK, (is_copy ? "copied" : "moved") );
@@ -3194,7 +3296,9 @@ void AdjRefCount(long msgnum, int incr)
                lprintf(CTDL_DEBUG, "Deleting message <%ld>\n", msgnum);
 
                /* Remove from fulltext index */
-               ft_index_message(msgnum, 0);
+               if (config.c_enable_fulltext) {
+                       ft_index_message(msgnum, 0);
+               }
 
                /* Remove from message base */
                delnum = msgnum;
@@ -3318,7 +3422,8 @@ void CtdlWriteObject(char *req_room,              /* Room to stuff it in */
         */
        if (is_unique) {
                lprintf(CTDL_DEBUG, "Deleted %d other msgs of this type\n",
-                       CtdlDeleteMessages(roomname, 0L, content_type));
+                       CtdlDeleteMessages(roomname, 0L, content_type, 0)
+               );
        }
        /* Now write the data */
        CtdlSubmitMsg(msg, NULL, roomname);