CURL: make log output configurable.
[citadel.git] / citadel / msgbase.c
index f49f1cc649433ff324fe2801cb6863259277e455..f23af01c8d43302e9c8b2f8ec00c1c3f4e5287c2 100644 (file)
@@ -68,6 +68,8 @@ struct addresses_to_be_filed *atbf = NULL;
 /* This temp file holds the queue of operations for AdjRefCount() */
 static FILE *arcfp = NULL;
 
+void AdjRefCountList(long *msgnum, long nmsg, int incr);
+
 /*
  * These are the four-character field headers we use when outputting
  * messages in Citadel format (as opposed to RFC822 format).
@@ -614,9 +616,23 @@ int CtdlForEachMessage(int mode, long ref, char *search_string,
        }
 
        /* Learn about the user and room in question */
+       if (server_shutting_down) {
+               if (need_to_free_re) regfree(&re);
+               return -1;
+       }
        CtdlGetUser(&CC->user, CC->curr_user);
+
+       if (server_shutting_down) {
+               if (need_to_free_re) regfree(&re);
+               return -1;
+       }
        CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
 
+       if (server_shutting_down) {
+               if (need_to_free_re) regfree(&re);
+               return -1;
+       }
+
        /* Load the message list */
        cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
        if (cdbfr == NULL) {
@@ -649,6 +665,11 @@ int CtdlForEachMessage(int mode, long ref, char *search_string,
                         * enough to only do the read if the caller has
                         * specified something that will need it.
                         */
+                       if (server_shutting_down) {
+                               if (need_to_free_re) regfree(&re);
+                               free(msglist);
+                               return -1;
+                       }
                        GetMetaData(&smi, msglist[a]);
 
                        /* if (strcasecmp(smi.meta_content_type, content_type)) { old non-regex way */
@@ -666,6 +687,11 @@ int CtdlForEachMessage(int mode, long ref, char *search_string,
        if (num_msgs > 0) {
                if (compare != NULL) {
                        for (a = 0; a < num_msgs; ++a) {
+                               if (server_shutting_down) {
+                                       if (need_to_free_re) regfree(&re);
+                                       free(msglist);
+                                       return -1;
+                               }
                                msg = CtdlFetchMessage(msglist[a], 1);
                                if (msg != NULL) {
                                        if (CtdlMsgCmp(msg, compare)) {
@@ -728,6 +754,11 @@ int CtdlForEachMessage(int mode, long ref, char *search_string,
         */
        if (num_msgs > 0)
                for (a = 0; a < num_msgs; ++a) {
+                       if (server_shutting_down) {
+                               if (need_to_free_re) regfree(&re);
+                               free(msglist);
+                               return num_processed;
+                       }
                        thismsg = msglist[a];
                        if (mode == MSGS_ALL) {
                                is_seen = 0;
@@ -2763,9 +2794,7 @@ int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newms
 
        /* Bump the reference count for all messages which were merged */
        if (!suppress_refcount_adj) {
-               for (i=0; i<num_msgs_to_be_merged; ++i) {
-                       AdjRefCount(msgs_to_be_merged[i], +1);
-               }
+               AdjRefCountList(msgs_to_be_merged, num_msgs_to_be_merged, +1);
        }
 
        /* Free up memory... */
@@ -3027,7 +3056,6 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
        int qualified_for_journaling = 0;
        CitContext *CCC = MyContext();
        char bounce_to[1024] = "";
-       size_t tmp = 0;
        int rv = 0;
 
        syslog(LOG_DEBUG, "CtdlSubmitMsg() called\n");
@@ -3206,7 +3234,7 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
                for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
                        extract_token(recipient, recps->recp_room, i,
                                      '|', sizeof recipient);
-                       syslog(LOG_DEBUG, "Delivering to room <%s>\n", recipient);
+                       syslog(LOG_DEBUG, "Delivering to room <%s>\n", recipient);///// xxxx
                        CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
                }
 
@@ -3258,6 +3286,7 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
                                        imsg->cm_magic = CTDLMESSAGE_MAGIC;
                                        imsg->cm_anon_type = MES_NORMAL;
                                        imsg->cm_format_type = FMT_RFC822;
+                                       imsg->cm_fields['U'] = strdup("QMSG");
                                        imsg->cm_fields['A'] = strdup("Citadel");
                                        imsg->cm_fields['J'] = strdup("do not journal");
                                        imsg->cm_fields['M'] = instr;   /* imsg owns this memory now */
@@ -3336,29 +3365,41 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,     /* message to save */
         * contain a recipient.
         */
        if ((recps != NULL) && (recps->num_internet > 0)) {
+               StrBuf *SpoolMsg = NewStrBuf();
+               long nTokens;
+
                syslog(LOG_DEBUG, "Generating delivery instructions\n");
-               instr_alloc = 1024;
-               instr = malloc(instr_alloc);
-               snprintf(instr, instr_alloc,
-                        "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
-                        "bounceto|%s\n",
-                        SPOOLMIME, newmsgid, (long)time(NULL),
-                        bounce_to
-                       );
 
-               if (recps->envelope_from != NULL) {
-                       tmp = strlen(instr);
-                       snprintf(&instr[tmp], instr_alloc-tmp, "envelope_from|%s\n", recps->envelope_from);
-               }
+               StrBufPrintf(SpoolMsg,
+                            "Content-type: "SPOOLMIME"\n"
+                            "\n"
+                            "msgid|%ld\n"
+                            "submitted|%ld\n"
+                            "bounceto|%s\n",
+                            newmsgid,
+                            (long)time(NULL),
+                            bounce_to);
 
-               for (i=0; i<num_tokens(recps->recp_internet, '|'); ++i) {
-                       tmp = strlen(instr);
-                       extract_token(recipient, recps->recp_internet, i, '|', sizeof recipient);
-                       if ((tmp + strlen(recipient) + 32) > instr_alloc) {
-                               instr_alloc = instr_alloc * 2;
-                               instr = realloc(instr, instr_alloc);
+               if (recps->envelope_from != NULL) {
+                       StrBufAppendBufPlain(SpoolMsg, HKEY("envelope_from|"), 0);
+                       StrBufAppendBufPlain(SpoolMsg, recps->envelope_from, -1, 0);
+                       StrBufAppendBufPlain(SpoolMsg, HKEY("\n"), 0);
+               }
+               if (recps->sending_room != NULL) {
+                       StrBufAppendBufPlain(SpoolMsg, HKEY("source_room|"), 0);
+                       StrBufAppendBufPlain(SpoolMsg, recps->sending_room, -1, 0);
+                       StrBufAppendBufPlain(SpoolMsg, HKEY("\n"), 0);
+               }
+
+               nTokens = num_tokens(recps->recp_internet, '|');
+               for (i = 0; i < nTokens; i++) {
+                       long len;
+                       len = extract_token(recipient, recps->recp_internet, i, '|', sizeof recipient);
+                       if (len > 0) {
+                               StrBufAppendBufPlain(SpoolMsg, HKEY("remote|"), 0);
+                               StrBufAppendBufPlain(SpoolMsg, recipient, len, 0);
+                               StrBufAppendBufPlain(SpoolMsg, HKEY("|0||\n"), 0);
                        }
-                       snprintf(&instr[tmp], instr_alloc - tmp, "remote|%s|0||\n", recipient);
                }
 
                imsg = malloc(sizeof(struct CtdlMessage));
@@ -3366,9 +3407,10 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,      /* message to save */
                imsg->cm_magic = CTDLMESSAGE_MAGIC;
                imsg->cm_anon_type = MES_NORMAL;
                imsg->cm_format_type = FMT_RFC822;
+               imsg->cm_fields['U'] = strdup("QMSG");
                imsg->cm_fields['A'] = strdup("Citadel");
                imsg->cm_fields['J'] = strdup("do not journal");
-               imsg->cm_fields['M'] = instr;   /* imsg owns this memory now */
+               imsg->cm_fields['M'] = SmashStrBuf(&SpoolMsg);  /* imsg owns this memory now */
                CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
                CtdlFreeMessage(imsg);
        }
@@ -4000,9 +4042,11 @@ struct recptypes *validate_recipients(const char *supplied_recipients,
                                      int Flags) {
        struct recptypes *ret;
        char *recipients = NULL;
+       char *org_recp;
        char this_recp[256];
        char this_recp_cooked[256];
        char append[SIZ];
+       long len;
        int num_recps = 0;
        int i, j;
        int mailtype;
@@ -4032,19 +4076,22 @@ struct recptypes *validate_recipients(const char *supplied_recipients,
         * actually need, but it's healthier for the heap than doing lots of tiny
         * realloc() calls instead.
         */
-
-       ret->errormsg = malloc(strlen(recipients) + 1024);
-       ret->recp_local = malloc(strlen(recipients) + 1024);
-       ret->recp_internet = malloc(strlen(recipients) + 1024);
-       ret->recp_ignet = malloc(strlen(recipients) + 1024);
-       ret->recp_room = malloc(strlen(recipients) + 1024);
-       ret->display_recp = malloc(strlen(recipients) + 1024);
+       len = strlen(recipients) + 1024;
+       ret->errormsg = malloc(len);
+       ret->recp_local = malloc(len);
+       ret->recp_internet = malloc(len);
+       ret->recp_ignet = malloc(len);
+       ret->recp_room = malloc(len);
+       ret->display_recp = malloc(len);
+       ret->recp_orgroom = malloc(len);
+       org_recp = malloc(len);
 
        ret->errormsg[0] = 0;
        ret->recp_local[0] = 0;
        ret->recp_internet[0] = 0;
        ret->recp_ignet[0] = 0;
        ret->recp_room[0] = 0;
+       ret->recp_orgroom[0] = 0;
        ret->display_recp[0] = 0;
 
        ret->recptypes_magic = RECPTYPES_MAGIC;
@@ -4059,7 +4106,6 @@ struct recptypes *validate_recipients(const char *supplied_recipients,
        /* Now start extracting recipients... */
 
        while (!IsEmptyStr(recipients)) {
-
                for (i=0; i<=strlen(recipients); ++i) {
                        if (recipients[i] == '\"') in_quotes = 1 - in_quotes;
                        if ( ( (recipients[i] == ',') && (!in_quotes) ) || (recipients[i] == 0) ) {
@@ -4080,6 +4126,8 @@ struct recptypes *validate_recipients(const char *supplied_recipients,
                        break;
                syslog(LOG_DEBUG, "Evaluating recipient #%d: %s\n", num_recps, this_recp);
                ++num_recps;
+
+               strcpy(org_recp, this_recp);
                mailtype = alias(this_recp);
                mailtype = alias(this_recp);
                mailtype = alias(this_recp);
@@ -4131,6 +4179,12 @@ struct recptypes *validate_recipients(const char *supplied_recipients,
                                                strcat(ret->recp_room, "|");
                                        }
                                        strcat(ret->recp_room, &this_recp_cooked[5]);
+
+                                       if (!IsEmptyStr(ret->recp_orgroom)) {
+                                               strcat(ret->recp_orgroom, "|");
+                                       }
+                                       strcat(ret->recp_orgroom, org_recp);
+
                                }
                                        
                                /* Restore room in case something needs it */
@@ -4215,6 +4269,7 @@ struct recptypes *validate_recipients(const char *supplied_recipients,
                        }
                }
        }
+       free(org_recp);
 
        if ((ret->num_local + ret->num_internet + ret->num_ignet +
             ret->num_room + ret->num_error) == 0) {
@@ -4253,9 +4308,11 @@ void free_recipients(struct recptypes *valid) {
        if (valid->recp_internet != NULL)       free(valid->recp_internet);
        if (valid->recp_ignet != NULL)          free(valid->recp_ignet);
        if (valid->recp_room != NULL)           free(valid->recp_room);
+       if (valid->recp_orgroom != NULL)        free(valid->recp_orgroom);
        if (valid->display_recp != NULL)        free(valid->display_recp);
        if (valid->bounce_to != NULL)           free(valid->bounce_to);
        if (valid->envelope_from != NULL)       free(valid->envelope_from);
+       if (valid->sending_room != NULL)        free(valid->sending_room);
        free(valid);
 }
 
@@ -4548,6 +4605,15 @@ void cmd_ent0(char *entargs)
        }
        free(all_recps);
 
+       if ((valid != NULL) && (valid->num_room == 1))
+       {
+               /* posting into an ML room? set the envelope from 
+                * to the actual mail address so others get a valid
+                * reply-to-header.
+                */
+               msg->cm_fields['V'] = strdup(valid->recp_orgroom);
+       }
+
        if (msg != NULL) {
                msgnum = CtdlSubmitMsg(msg, valid, "", QP_EADDR);
                if (verbose_reply)
@@ -4634,32 +4700,51 @@ int CtdlDeleteMessages(char *room_name,         /* which room */
                cdb_free(cdbfr);
        }
        if (num_msgs > 0) {
-               for (i = 0; i < num_msgs; ++i) {
+               int have_contenttype = !IsEmptyStr(content_type);
+               int have_delmsgs = (num_dmsgnums == 0) || (dmsgnums == NULL);
+               int have_more_del = 1;
+
+               num_msgs = sort_msglist(msglist, num_msgs);
+               if (num_dmsgnums > 1)
+                       num_dmsgnums = sort_msglist(dmsgnums, num_dmsgnums);
+/*
+               {
+                       StrBuf *dbg = NewStrBuf();
+                       for (i = 0; i < num_dmsgnums; i++)
+                               StrBufAppendPrintf(dbg, ", %ld", dmsgnums[i]);
+                       syslog(LOG_DEBUG, "Deleting before: %s", ChrPtr(dbg));
+                       FreeStrBuf(&dbg);
+               }
+*/
+               i = 0; j = 0;
+               while ((i < num_msgs) && (have_more_del)) {
                        delete_this = 0x00;
 
+
                        /* Set/clear a bit for each criterion */
 
                        /* 0 messages in the list or a null list means that we are
                         * interested in deleting any messages which meet the other criteria.
                         */
-                       if ((num_dmsgnums == 0) || (dmsgnums == NULL)) {
+                       if (have_delmsgs) {
                                delete_this |= 0x01;
                        }
                        else {
-                               for (j=0; j<num_dmsgnums; ++j) {
-                                       if (msglist[i] == dmsgnums[j]) {
-                                               delete_this |= 0x01;
-                                       }
+                               while ((i < num_msgs) && (msglist[i] < dmsgnums[j])) i++;
+                               if (msglist[i] == dmsgnums[j]) {
+                                       delete_this |= 0x01;
                                }
+                               j++;
+                               have_more_del = (j < num_dmsgnums);
                        }
 
-                       if (IsEmptyStr(content_type)) {
-                               delete_this |= 0x02;
-                       } else {
+                       if (have_contenttype) {
                                GetMetaData(&smi, msglist[i]);
                                if (regexec(&re, smi.meta_content_type, 1, &pm, 0) == 0) {
                                        delete_this |= 0x02;
                                }
+                       } else {
+                               delete_this |= 0x02;
                        }
 
                        /* Delete message only if all bits are set */
@@ -4667,8 +4752,17 @@ int CtdlDeleteMessages(char *room_name,          /* which room */
                                dellist[num_deleted++] = msglist[i];
                                msglist[i] = 0L;
                        }
+                       i++;
                }
-
+/*
+               {
+                       StrBuf *dbg = NewStrBuf();
+                       for (i = 0; i < num_deleted; i++)
+                               StrBufAppendPrintf(dbg, ", %ld", dellist[i]);
+                       syslog(LOG_DEBUG, "Deleting: %s", ChrPtr(dbg));
+                       FreeStrBuf(&dbg);
+               }
+*/
                num_msgs = sort_msglist(msglist, num_msgs);
                cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
                          msglist, (int)(num_msgs * sizeof(long)));
@@ -4688,11 +4782,12 @@ int CtdlDeleteMessages(char *room_name,         /* which room */
         * and we don't want that happening during an S_ROOMS critical
         * section.
         */
-       if (num_deleted) for (i=0; i<num_deleted; ++i) {
+       if (num_deleted) {
+               for (i=0; i<num_deleted; ++i) {
                        PerformDeleteHooks(qrbuf.QRname, dellist[i]);
-                       AdjRefCount(dellist[i], -1);
                }
-
+               AdjRefCountList(dellist, num_deleted, -1);
+       }
        /* Now free the memory we used, and go away. */
        if (msglist != NULL) free(msglist);
        if (dellist != NULL) free(dellist);
@@ -4973,6 +5068,57 @@ void AdjRefCount(long msgnum, int incr)
        return;
 }
 
+void AdjRefCountList(long *msgnum, long nmsg, int incr)
+{
+       long i, the_size, offset;
+       struct arcq *new_arcq;
+       int rv = 0;
+
+       syslog(LOG_DEBUG, "AdjRefCountList() msg %ld ref count delta %+d\n", nmsg, incr);
+
+       begin_critical_section(S_SUPPMSGMAIN);
+       if (arcfp == NULL) {
+               arcfp = fopen(file_arcq, "ab+");
+               chown(file_arcq, CTDLUID, (-1));
+               chmod(file_arcq, 0600);
+       }
+       end_critical_section(S_SUPPMSGMAIN);
+
+       /*
+        * If we can't open the queue, perform the operation synchronously.
+        */
+       if (arcfp == NULL) {
+               for (i = 0; i < nmsg; i++)
+                       TDAP_AdjRefCount(msgnum[i], incr);
+               return;
+       }
+
+       the_size = sizeof(struct arcq) * nmsg;
+       new_arcq = malloc(the_size);
+       for (i = 0; i < nmsg; i++) {
+               new_arcq[i].arcq_msgnum = msgnum[i];
+               new_arcq[i].arcq_delta = incr;
+       }
+       rv = 0;
+       offset = 0;
+       while ((rv >= 0) && (offset < the_size))
+       {
+               rv = fwrite(new_arcq + offset, 1, the_size - offset, arcfp);
+               if (rv == -1) {
+                       syslog(LOG_EMERG, "Couldn't write Refcount Queue File %s: %s\n",
+                              file_arcq,
+                              strerror(errno));
+               }
+               else {
+                       offset += rv;
+               }
+       }
+       free(new_arcq);
+       fflush(arcfp);
+
+       return;
+}
+
 
 /*
  * TDAP_ProcessAdjRefCountQueue()