Removed an unused parameter from CtdlSubmitMsg(). Why was it even there?
[citadel.git] / citadel / msgbase.c
index 0b4ed5c593049f39d707f86733092cc144bdb69c..bf18da7447b0e2323e4cfbc74aad76235ad4f876 100644 (file)
@@ -61,7 +61,7 @@ char *msgkeys[] = {
        "rep2", // K -> eReplyTo
        "list", // L -> eListID
        "text", // M -> eMesageText
-       NULL,   // N (formerly used as eNodeName)
+       "locl", // N -> eOrigLocal
        "room", // O -> eOriginalRoom
        "path", // P -> eMessagePath
        NULL,   // Q
@@ -76,6 +76,7 @@ char *msgkeys[] = {
        NULL    // Z
 };
 
+
 HashList *msgKeyLookup = NULL;
 
 int GetFieldFromMnemonic(eMsgField *f, const char* c)
@@ -101,6 +102,7 @@ void FillMsgKeyLookupTable(void)
        }
 }
 
+
 eMsgField FieldOrder[]  = {
 /* Important fields */
        emessageId   ,
@@ -111,6 +113,7 @@ eMsgField FieldOrder[]  = {
        eOriginalRoom,
        eRecipient   ,
 /* Semi-important fields */
+       eOrigLocal   ,
        eBig_message ,
        eExclusiveID ,
        eWeferences  ,
@@ -149,6 +152,9 @@ void CM_SetField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long
        if (Msg->cm_fields[which] != NULL) {
                free (Msg->cm_fields[which]);
        }
+       if (length < 0) {                       // You can set the length to -1 to have CM_SetField measure it for you
+               length = strlen(buf);
+       }
        Msg->cm_fields[which] = malloc(length + 1);
        memcpy(Msg->cm_fields[which], buf, length);
        Msg->cm_fields[which][length] = '\0';
@@ -256,7 +262,12 @@ void CM_SetAsField(struct CtdlMessage *Msg, eMsgField which, char **buf, long le
 
        Msg->cm_fields[which] = *buf;
        *buf = NULL;
-       Msg->cm_lengths[which] = length;
+       if (length < 0) {                       // You can set the length to -1 to have CM_SetField measure it for you
+               Msg->cm_lengths[which] = strlen(Msg->cm_fields[which]);
+       }
+       else {
+               Msg->cm_lengths[which] = length;
+       }
 }
 
 
@@ -756,7 +767,7 @@ int CtdlForEachMessage(int mode, long ref, char *search_string,
                                        free(msglist);
                                        return -1;
                                }
-                               msg = CtdlFetchMessage(msglist[a], 1, 1);
+                               msg = CtdlFetchMessage(msglist[a], 1);
                                if (msg != NULL) {
                                        if (CtdlMsgCmp(msg, compare)) {
                                                msglist[a] = 0L;
@@ -1163,7 +1174,7 @@ struct CtdlMessage *CtdlDeserializeMessage(long msgnum, int with_body, const cha
  * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
  *       using the CM_Free(); function.
  */
-struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body, int run_msg_hooks)
+struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body)
 {
        struct cdbdata *dmsgtext;
        struct CtdlMessage *ret = NULL;
@@ -1204,12 +1215,6 @@ struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body, int run_msg_hoo
                CM_SetField(ret, eMesageText, HKEY("\r\n\r\n (no text)\r\n"));
        }
 
-       /* Perform "before read" hooks (aborting if any return nonzero) */
-       if (run_msg_hooks && (PerformMessageHooks(ret, NULL, EVT_BEFOREREAD) > 0)) {
-               CM_Free(ret);
-               return NULL;
-       }
-
        return (ret);
 }
 
@@ -1596,10 +1601,10 @@ int CtdlOutputMsg(long msg_num,         /* message number (local) to fetch */
         * request that we don't even bother loading the body into memory.
         */
        if (headers_only == HEADERS_FAST) {
-               TheMessage = CtdlFetchMessage(msg_num, 0, 1);
+               TheMessage = CtdlFetchMessage(msg_num, 0);
        }
        else {
-               TheMessage = CtdlFetchMessage(msg_num, 1, 1);
+               TheMessage = CtdlFetchMessage(msg_num, 1);
        }
 
        if (TheMessage == NULL) {
@@ -1852,6 +1857,7 @@ void OutputRFC822MsgHeaders(
                        case eSuppressIdx:
                        case eExtnotify:
                        case eVltMsgNum:
+                       case eOrigLocal:
                                /* these don't map to mime message headers. */
                                break;
                        }
@@ -2034,11 +2040,11 @@ int CtdlOutputPreLoadedMsg(
         * using functions that are bounds-checked, and therefore we can
         * make them substantially smaller than SIZ.
         */
-       char suser[100];
-       char luser[100];
-       char fuser[100];
-       char snode[100];
-       char mid[100];
+       char suser[1024];
+       char luser[1024];
+       char fuser[1024];
+       char snode[1024];
+       char mid[1024];
 
        syslog(LOG_DEBUG, "msgbase: CtdlOutputPreLoadedMsg(TheMessage=%s, %d, %d, %d, %d",
                   ((TheMessage == NULL) ? "NULL" : "not null"),
@@ -2141,6 +2147,7 @@ int CtdlOutputPreLoadedMsg(
        strcpy(suser, "");
        strcpy(luser, "");
        strcpy(fuser, "");
+       strcpy(snode, "");
        if (mode == MT_RFC822) 
                OutputRFC822MsgHeaders(
                        TheMessage,
@@ -2394,7 +2401,7 @@ int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newms
                                msg = supplied_msg;
                        }
                        else {
-                               msg = CtdlFetchMessage(msgid, 0, 1);
+                               msg = CtdlFetchMessage(msgid, 0);
                        }
        
                        if (msg != NULL) {
@@ -2453,8 +2460,6 @@ int CtdlSaveMsgPointerInRoom(char *roomname, long msgid,
 }
 
 
-
-
 /*
  * Message base operation to save a new message to the message store
  * (returns new message number)
@@ -2474,8 +2479,7 @@ long CtdlSaveThisMessage(struct CtdlMessage *msg, long msgid, int Reply) {
         * If the message is big, set its body aside for storage elsewhere
         * and we hide the message body from the serializer
         */
-       if (!CM_IsEmpty(msg, eMesageText) && msg->cm_lengths[eMesageText] > BIGMSG)
-       {
+       if (!CM_IsEmpty(msg, eMesageText) && msg->cm_lengths[eMesageText] > BIGMSG) {
                is_bigmsg = 1;
                holdM = msg->cm_fields[eMesageText];
                msg->cm_fields[eMesageText] = NULL;
@@ -2505,8 +2509,7 @@ long CtdlSaveThisMessage(struct CtdlMessage *msg, long msgid, int Reply) {
        }
 
        /* Write our little bundle of joy into the message base */
-       retval = cdb_store(CDB_MSGMAIN, &msgid, (int)sizeof(long),
-                          smr.ser, smr.len);
+       retval = cdb_store(CDB_MSGMAIN, &msgid, (int)sizeof(long), smr.ser, smr.len);
        if (retval < 0) {
                syslog(LOG_ERR, "msgbase: can't store message %ld: %ld", msgid, retval);
        }
@@ -2530,6 +2533,7 @@ long CtdlSaveThisMessage(struct CtdlMessage *msg, long msgid, int Reply) {
        return(retval);
 }
 
+
 long send_message(struct CtdlMessage *msg) {
        long newmsgid;
        long retval;
@@ -2563,7 +2567,6 @@ long send_message(struct CtdlMessage *msg) {
 }
 
 
-
 /*
  * Serialize a struct CtdlMessage into the format used on disk and network.
  * 
@@ -2605,9 +2608,8 @@ void CtdlSerializeMessage(struct ser_ret *ret,            /* return values */
        ret->ser[2] = msg->cm_format_type;
        wlen = 3;
 
-       for (i=0; i < NDiskFields; ++i)
-               if (msg->cm_fields[FieldOrder[i]] != NULL)
-               {
+       for (i=0; i < NDiskFields; ++i) {
+               if (msg->cm_fields[FieldOrder[i]] != NULL) {
                        ret->ser[wlen++] = (char)FieldOrder[i];
 
                        memcpy(&ret->ser[wlen],
@@ -2616,6 +2618,7 @@ void CtdlSerializeMessage(struct ser_ret *ret,            /* return values */
 
                        wlen = wlen + msg->cm_lengths[FieldOrder[i]] + 1;
                }
+       }
 
        if (ret->len != wlen) {
                syslog(LOG_ERR, "msgbase: ERROR; len=%ld wlen=%ld", (long)ret->len, (long)wlen);
@@ -2651,16 +2654,13 @@ void ReplicationChecks(struct CtdlMessage *msg) {
 }
 
 
-
 /*
  * Save a message to disk and submit it into the delivery system.
  */
 long CtdlSubmitMsg(struct CtdlMessage *msg,    /* message to save */
                   recptypes *recps,            /* recipients (if mail) */
-                  const char *force,           /* force a particular room? */
-                  int flags                    /* should the message be exported clean? */
-       )
-{
+                  const char *force            /* force a particular room? */
+) {
        char hold_rm[ROOMNAMELEN];
        char actual_rm[ROOMNAMELEN];
        char force_room[ROOMNAMELEN];
@@ -2777,7 +2777,7 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
         * If this message has no O (room) field, generate one.
         */
        if (CM_IsEmpty(msg, eOriginalRoom) && !IsEmptyStr(CC->room.QRname)) {
-               CM_SetField(msg, eOriginalRoom, CC->room.QRname, strlen(CC->room.QRname));
+               CM_SetField(msg, eOriginalRoom, CC->room.QRname, -1);
        }
 
        /* Perform "before save" hooks (aborting if any return nonzero) */
@@ -2805,8 +2805,7 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
        memset(&smi, 0, sizeof(struct MetaData));
        smi.meta_msgnum = newmsgid;
        smi.meta_refcount = 0;
-       safestrncpy(smi.meta_content_type, content_type,
-                   sizeof smi.meta_content_type);
+       safestrncpy(smi.meta_content_type, content_type, sizeof smi.meta_content_type);
 
        /*
         * Measure how big this message will be when rendered as RFC822.
@@ -2850,13 +2849,13 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,     /* message to save */
        }
 
        /* If other rooms are specified, drop them there too. */
-       if ((recps != NULL) && (recps->num_room > 0))
+       if ((recps != NULL) && (recps->num_room > 0)) {
                for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
-                       extract_token(recipient, recps->recp_room, i,
-                                     '|', sizeof recipient);
+                       extract_token(recipient, recps->recp_room, i, '|', sizeof recipient);
                        syslog(LOG_DEBUG, "msgbase: delivering to room <%s>", recipient);
                        CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
                }
+       }
 
        /* Bump this user's messages posted counter. */
        syslog(LOG_DEBUG, "msgbase: updating user");
@@ -2865,12 +2864,11 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,     /* message to save */
        CtdlPutCurrentUserLock();
 
        /* Decide where bounces need to be delivered */
-       if ((recps != NULL) && (recps->bounce_to == NULL))
-       {
+       if ((recps != NULL) && (recps->bounce_to == NULL)) {
                if (CC->logged_in) {
                        strcpy(bounce_to, CC->user.fullname);
                }
-               else {
+               else if (!IsEmptyStr(msg->cm_fields[eAuthor])){
                        strcpy(bounce_to, msg->cm_fields[eAuthor]);
                }
                recps->bounce_to = bounce_to;
@@ -2878,20 +2876,17 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,     /* message to save */
                
        CM_SetFieldLONG(msg, eVltMsgNum, newmsgid);
 
-
        /* If this is private, local mail, make a copy in the
         * recipient's mailbox and bump the reference count.
         */
-       if ((recps != NULL) && (recps->num_local > 0))
-       {
+       if ((recps != NULL) && (recps->num_local > 0)) {
                char *pch;
                int ntokens;
 
                pch = recps->recp_local;
                recps->recp_local = recipient;
                ntokens = num_tokens(pch, '|');
-               for (i=0; i<ntokens; ++i)
-               {
+               for (i=0; i<ntokens; ++i) {
                        extract_token(recipient, pch, i, '|', sizeof recipient);
                        syslog(LOG_DEBUG, "msgbase: delivering private local mail to <%s>", recipient);
                        if (CtdlGetUser(&userbuf, recipient) == 0) {
@@ -2916,8 +2911,9 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
 
        /* Go back to the room we started from */
        syslog(LOG_DEBUG, "msgbase: returning to original room %s", hold_rm);
-       if (strcasecmp(hold_rm, CC->room.QRname))
+       if (strcasecmp(hold_rm, CC->room.QRname)) {
                CtdlUserGoto(hold_rm, 0, 1, NULL, NULL, NULL, NULL);
+       }
 
        /*
         * Any addresses to harvest for someone's address book?
@@ -2927,10 +2923,8 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,      /* message to save */
        }
 
        if (collected_addresses != NULL) {
-               aptr = (struct addresses_to_be_filed *)
-                       malloc(sizeof(struct addresses_to_be_filed));
-               CtdlMailboxName(actual_rm, sizeof actual_rm,
-                               &CC->user, USERCONTACTSROOM);
+               aptr = (struct addresses_to_be_filed *) malloc(sizeof(struct addresses_to_be_filed));
+               CtdlMailboxName(actual_rm, sizeof actual_rm, &CC->user, USERCONTACTSROOM);
                aptr->roomname = strdup(actual_rm);
                aptr->collected_addresses = collected_addresses;
                begin_critical_section(S_ATBF);
@@ -3000,11 +2994,11 @@ long quickie_message(const char *from,
        msg->cm_format_type = format_type;
 
        if (!IsEmptyStr(from)) {
-               CM_SetField(msg, eAuthor, from, strlen(from));
+               CM_SetField(msg, eAuthor, from, -1);
        }
        else if (!IsEmptyStr(fromaddr)) {
                char *pAt;
-               CM_SetField(msg, eAuthor, fromaddr, strlen(fromaddr));
+               CM_SetField(msg, eAuthor, fromaddr, -1);
                pAt = strchr(msg->cm_fields[eAuthor], '@');
                if (pAt != NULL) {
                        CM_CutFieldAt(msg, eAuthor, pAt - msg->cm_fields[eAuthor]);
@@ -3014,20 +3008,20 @@ long quickie_message(const char *from,
                msg->cm_fields[eAuthor] = strdup("Citadel");
        }
 
-       if (!IsEmptyStr(fromaddr)) CM_SetField(msg, erFc822Addr, fromaddr, strlen(fromaddr));
-       if (!IsEmptyStr(room)) CM_SetField(msg, eOriginalRoom, room, strlen(room));
+       if (!IsEmptyStr(fromaddr)) CM_SetField(msg, erFc822Addr, fromaddr, -1);
+       if (!IsEmptyStr(room)) CM_SetField(msg, eOriginalRoom, room, -1);
        if (!IsEmptyStr(to)) {
-               CM_SetField(msg, eRecipient, to, strlen(to));
+               CM_SetField(msg, eRecipient, to, -1);
                recp = validate_recipients(to, NULL, 0);
        }
        if (!IsEmptyStr(subject)) {
-               CM_SetField(msg, eMsgSubject, subject, strlen(subject));
+               CM_SetField(msg, eMsgSubject, subject, -1);
        }
        if (!IsEmptyStr(text)) {
-               CM_SetField(msg, eMesageText, text, strlen(text));
+               CM_SetField(msg, eMesageText, text, -1);
        }
 
-       long msgnum = CtdlSubmitMsg(msg, recp, room, 0);
+       long msgnum = CtdlSubmitMsg(msg, recp, room);
        CM_Free(msg);
        if (recp != NULL) free_recipients(recp);
        return msgnum;
@@ -3043,8 +3037,7 @@ StrBuf *CtdlReadMessageBodyBuf(char *terminator,  /* token signalling EOT */
                               StrBuf *exist,           /* if non-null, append to it;
                                                           exist is ALWAYS freed  */
                               int crlf                 /* CRLF newlines instead of LF */
-       ) 
-{
+) {
        StrBuf *Message;
        StrBuf *LineBuf;
        int flushing = 0;
@@ -3121,6 +3114,7 @@ char *CtdlReadMessageBody(char *terminator,       /* token signalling EOT */
                return SmashStrBuf(&Message);
 }
 
+
 struct CtdlMessage *CtdlMakeMessage(
         struct ctdluser *author,        /* author's user structure */
         char *recipient,                /* NULL if it's not mail */
@@ -3134,8 +3128,7 @@ struct CtdlMessage *CtdlMakeMessage(
        char *supplied_euid,            /* ...or NULL if this is irrelevant */
         char *preformatted_text,        /* ...or NULL to read text from client */
        char *references                /* Thread references */
-)
-{
+) {
        return CtdlMakeMessageLen(
                author, /* author's user structure */
                recipient,              /* NULL if it's not mail */
@@ -3161,6 +3154,7 @@ struct CtdlMessage *CtdlMakeMessage(
 
 }
 
+
 /*
  * Build a binary message to be saved on disk.
  * (NOTE: if you supply 'preformatted_text', the buffer you give it
@@ -3168,7 +3162,6 @@ struct CtdlMessage *CtdlMakeMessage(
  * responsible for managing that memory -- it will be freed along with
  * the rest of the fields when CM_Free() is called.)
  */
-
 struct CtdlMessage *CtdlMakeMessageLen(
        struct ctdluser *author,        /* author's user structure */
        char *recipient,                /* NULL if it's not mail */
@@ -3191,10 +3184,7 @@ struct CtdlMessage *CtdlMakeMessageLen(
        long textlen,
        char *references,               /* Thread references */
        long reflen
-       )
-{
-       /* Don't confuse the poor folks if it's not routed mail. * /
-          char dest_node[256] = "";*/
+) {
        long blen;
        char buf[1024];
        struct CtdlMessage *msg;
@@ -3215,7 +3205,7 @@ struct CtdlMessage *CtdlMakeMessageLen(
                CM_SetField(msg, eMessagePath, my_email, myelen);
        }
        else if (!IsEmptyStr(author->fullname)) {
-               CM_SetField(msg, eMessagePath, author->fullname, strlen(author->fullname));
+               CM_SetField(msg, eMessagePath, author->fullname, -1);
        }
        convert_spaces_to_underscores(msg->cm_fields[eMessagePath]);
 
@@ -3234,10 +3224,10 @@ struct CtdlMessage *CtdlMakeMessageLen(
 
        if (!!IsEmptyStr(CC->room.QRname)) {
                if (CC->room.QRflags & QR_MAILBOX) {            /* room */
-                       CM_SetField(msg, eOriginalRoom, &CC->room.QRname[11], strlen(&CC->room.QRname[11]));
+                       CM_SetField(msg, eOriginalRoom, &CC->room.QRname[11], -1);
                }
                else {
-                       CM_SetField(msg, eOriginalRoom, CC->room.QRname, strlen(CC->room.QRname));
+                       CM_SetField(msg, eOriginalRoom, CC->room.QRname, -1);
                }
        }
 
@@ -3252,7 +3242,7 @@ struct CtdlMessage *CtdlMakeMessageLen(
                CM_SetField(msg, erFc822Addr, my_email, myelen);
        }
        else if ( (author == &CC->user) && (!IsEmptyStr(CC->cs_inet_email)) ) {
-               CM_SetField(msg, erFc822Addr, CC->cs_inet_email, strlen(CC->cs_inet_email));
+               CM_SetField(msg, erFc822Addr, CC->cs_inet_email, -1);
        }
 
        if (subject != NULL) {
@@ -3301,18 +3291,15 @@ struct CtdlMessage *CtdlMakeMessageLen(
 }
 
 
-
-
 /*
  * API function to delete messages which match a set of criteria
  * (returns the actual number of messages deleted)
  */
-int CtdlDeleteMessages(const char *room_name,          /* which room */
-                      long *dmsgnums,          /* array of msg numbers to be deleted */
-                      int num_dmsgnums,        /* number of msgs to be deleted, or 0 for "any" */
-                      char *content_type       /* or "" for any.  regular expressions expected. */
-       )
-{
+int CtdlDeleteMessages(const char *room_name,  // which room
+                      long *dmsgnums,          // array of msg numbers to be deleted
+                      int num_dmsgnums,        // number of msgs to be deleted, or 0 for "any"
+                      char *content_type       // or "" for any.  regular expressions expected.
+) {
        struct ctdlroom qrbuf;
        struct cdbdata *cdbfr;
        long *msglist = NULL;
@@ -3559,8 +3546,7 @@ void CtdlWriteObject(char *req_room,                      /* Room to stuff it in */
                     int is_binary,                     /* Is encoding necessary? */
                     int is_unique,                     /* Del others of this type? */
                     unsigned int flags                 /* Internal save flags */
-       )
-{
+) {
        struct ctdlroom qrbuf;
        char roomname[ROOMNAMELEN];
        struct CtdlMessage *msg;
@@ -3606,8 +3592,8 @@ void CtdlWriteObject(char *req_room,                      /* Room to stuff it in */
        msg->cm_magic = CTDLMESSAGE_MAGIC;
        msg->cm_anon_type = MES_NORMAL;
        msg->cm_format_type = 4;
-       CM_SetField(msg, eAuthor, CC->user.fullname, strlen(CC->user.fullname));
-       CM_SetField(msg, eOriginalRoom, req_room, strlen(req_room));
+       CM_SetField(msg, eAuthor, CC->user.fullname, -1);
+       CM_SetField(msg, eOriginalRoom, req_room, -1);
        msg->cm_flags = flags;
        
        CM_SetAsFieldSB(msg, eMesageText, &encoded_message);
@@ -3625,7 +3611,7 @@ void CtdlWriteObject(char *req_room,                      /* Room to stuff it in */
                        );
        }
        /* Now write the data */
-       CtdlSubmitMsg(msg, NULL, roomname, 0);
+       CtdlSubmitMsg(msg, NULL, roomname);
        CM_Free(msg);
 }