/*
* Implements the message store.
*
- * Copyright (c) 1987-2011 by the citadel.org team
+ * Copyright (c) 1987-2012 by the citadel.org team
*
* This program is open source software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
+ * it under the terms of the GNU General Public License version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "sysdep.h"
/* 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).
}
/* 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) {
* 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 */
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)) {
*/
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;
/* 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... */
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");
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);
}
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 */
* 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));
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);
}
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;
* 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;
/* 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) ) {
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);
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 */
}
}
}
+ free(org_recp);
if ((ret->num_local + ret->num_internet + ret->num_ignet +
ret->num_room + ret->num_error) == 0) {
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);
}
}
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)
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 */
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)));
* 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);
struct arcq new_arcq;
int rv = 0;
- syslog(LOG_DEBUG, "AdjRefCount() msg %ld ref count delta %+d\n",
- msgnum, incr
- );
+ syslog(LOG_DEBUG, "AdjRefCount() msg %ld ref count delta %+d\n", msgnum, 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);
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()