]> code.citadel.org Git - citadel.git/blobdiff - citadel/msgbase.c
* Issue 'cancel' messages for vCard when a user is deleted.
[citadel.git] / citadel / msgbase.c
index a0c0a857e97a6b9b87dcea211ef56add76274215..7c18b943cf5e789b154cd5d991fb2b53f54c9d69 100644 (file)
@@ -8,9 +8,6 @@
 #include <ctype.h>
 #include <string.h>
 #include <syslog.h>
-#ifdef HAVE_PTHREAD_H
-#include <pthread.h>
-#endif
 #include <limits.h>
 #include "citadel.h"
 #include "server.h"
@@ -59,7 +56,7 @@ char *msgkeys[] = {
        "path",
        "",
        "rcpt",
-       "",
+       "spec",
        "time",
        "subj",
        "",
@@ -101,8 +98,6 @@ int alias(char *name)
        int a, b;
        char aaa[300], bbb[300];
 
-       lprintf(9, "alias() called for <%s>\n", name);
-
        remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
 
        fp = fopen("network/mail.aliases", "r");
@@ -632,10 +627,8 @@ struct CtdlMessage *CtdlFetchMessage(long msgnum)
        CIT_UBYTE field_header;
        size_t field_length;
 
-
        dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
        if (dmsgtext == NULL) {
-               lprintf(9, "CtdlFetchMessage(%ld) failed.\n");
                return NULL;
        }
        mptr = dmsgtext->ptr;
@@ -677,6 +670,10 @@ struct CtdlMessage *CtdlFetchMessage(long msgnum)
 
        cdb_free(dmsgtext);
 
+       /* Always make sure there's something in the msg text field */
+       if (ret->cm_fields['M'] == NULL)
+               ret->cm_fields['M'] = strdoop("<no text>\n");
+
        /* Perform "before read" hooks (aborting if any return nonzero) */
        if (PerformMessageHooks(ret, EVT_BEFOREREAD) > 0) {
                CtdlFreeMessage(ret);
@@ -748,6 +745,7 @@ void output_message(char *msgid, int mode, int headers_only)
        /*                                       */
 
        msg_num = atol(msgid);
+       safestrncpy(mid, msgid, sizeof mid);
 
        if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
                cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
@@ -1097,6 +1095,7 @@ void cmd_opna(char *cmdbuf)
 /*
  * Save a message pointer into a specified room
  * (Returns 0 for success, nonzero for failure)
+ * roomname may be NULL to use the current room
  */
 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid, int flags) {
        int i;
@@ -1120,6 +1119,26 @@ int CtdlSaveMsgPointerInRoom(char *roomname, long msgid, int flags) {
                if (msg == NULL) return(ERROR + ILLEGAL_VALUE);
        }
 
+       /* Perform replication checks if necessary */
+       if ( (flags & SM_DO_REPL_CHECK) && (msg != NULL) ) {
+
+               if (getroom(&CC->quickroom,
+                  ((roomname != NULL) ? roomname : CC->quickroom.QRname) )
+                  != 0) {
+                       lprintf(9, "No such room <%s>\n", roomname);
+                       if (msg != NULL) CtdlFreeMessage(msg);
+                       return(ERROR + ROOM_NOT_FOUND);
+               }
+
+               if (ReplicationChecks(msg) != 0) {
+                       getroom(&CC->quickroom, hold_rm);
+                       if (msg != NULL) CtdlFreeMessage(msg);
+                       lprintf(9, "Did replication, and newer exists\n");
+                       return(0);
+               }
+       }
+
+       /* Now the regular stuff */
        if (lgetroom(&CC->quickroom,
           ((roomname != NULL) ? roomname : CC->quickroom.QRname) )
           != 0) {
@@ -1155,17 +1174,6 @@ int CtdlSaveMsgPointerInRoom(char *roomname, long msgid, int flags) {
                }
        }
 
-       /* Perform replication checks if necessary */
-       if ( (flags & SM_DO_REPL_CHECK) && (msg != NULL) ) {
-               if (ReplicationChecks(msg) != 0) {
-                       lputroom(&CC->quickroom);       /* unlock the room */
-                       getroom(&CC->quickroom, hold_rm);
-                       if (msg != NULL) CtdlFreeMessage(msg);
-                       lprintf(9, "Did replication, and newer exists\n");
-                       return(0);
-               }
-       }
-
         /* Now add the new message */
         ++num_msgs;
         msglist = reallok(msglist,
@@ -1198,7 +1206,6 @@ int CtdlSaveMsgPointerInRoom(char *roomname, long msgid, int flags) {
        AdjRefCount(msgid, +1);
 
        /* Return success. */
-       lprintf(9, "CtdlSaveMsgPointerInRoom() succeeded\n");
        if (msg != NULL) CtdlFreeMessage(msg);
         return (0);
 }
@@ -1281,19 +1288,13 @@ void serialize_message(struct ser_ret *ret,             /* return values */
        int i;
        static char *forder = FORDER;
 
-       lprintf(9, "serialize_message() called\n");
-
        if (is_valid_message(msg) == 0) return;         /* self check */
 
-       lprintf(9, "magic number check OK.\n");
-
        ret->len = 3;
        for (i=0; i<26; ++i) if (msg->cm_fields[(int)forder[i]] != NULL)
                ret->len = ret->len +
                        strlen(msg->cm_fields[(int)forder[i]]) + 2;
 
-       lprintf(9, "message is %d bytes\n", ret->len);
-
        lprintf(9, "calling malloc\n");
        ret->ser = mallok(ret->len);
        if (ret->ser == NULL) {
@@ -1306,7 +1307,6 @@ void serialize_message(struct ser_ret *ret,               /* return values */
        ret->ser[2] = msg->cm_format_type;
        wlen = 3;
 
-       lprintf(9, "stuff\n");
        for (i=0; i<26; ++i) if (msg->cm_fields[(int)forder[i]] != NULL) {
                ret->ser[wlen++] = (char)forder[i];
                strcpy(&ret->ser[wlen], msg->cm_fields[(int)forder[i]]);
@@ -1314,7 +1314,6 @@ void serialize_message(struct ser_ret *ret,               /* return values */
        }
        if (ret->len != wlen) lprintf(3, "ERROR: len=%d wlen=%d\n",
                ret->len, wlen);
-       lprintf(9, "done serializing\n");
 
        return;
 }
@@ -1384,7 +1383,7 @@ int ReplicationChecks(struct CtdlMessage *msg) {
                }
 
        CtdlFreeMessage(template);
-       lprintf(9, "Returning %d\n", abort_this);
+       lprintf(9, "ReplicationChecks() returning %d\n", abort_this);
        return(abort_this);
 }
 
@@ -1475,6 +1474,26 @@ void CtdlSaveMsg(struct CtdlMessage *msg,        /* message to save */
                }
        }
 
+       /* Goto the correct room */
+       strcpy(hold_rm, CC->quickroom.QRname);
+       strcpy(actual_rm, CC->quickroom.QRname);
+
+       /* If the user is a twit, move to the twit room for posting */
+       if (TWITDETECT) {
+               if (CC->usersupp.axlevel == 2) {
+                       strcpy(hold_rm, actual_rm);
+                       strcpy(actual_rm, config.c_twitroom);
+               }
+       }
+
+       /* ...or if this message is destined for Aide> then go there. */
+       if (strlen(force_room) > 0) {
+               strcpy(actual_rm, force_room);
+       }
+
+       if (strcasecmp(actual_rm, CC->quickroom.QRname))
+               getroom(&CC->quickroom, actual_rm);
+
        /* Perform "before save" hooks (aborting if any return nonzero) */
        if (PerformMessageHooks(msg, EVT_BEFORESAVE) > 0) return;
 
@@ -1512,24 +1531,12 @@ void CtdlSaveMsg(struct CtdlMessage *msg,       /* message to save */
 
        /* Now figure out where to store the pointers */
 
-       strcpy(actual_rm, CC->quickroom.QRname);
 
        /* If this is being done by the networker delivering a private
         * message, we want to BYPASS saving the sender's copy (because there
         * is no local sender; it would otherwise go to the Trashcan).
         */
        if ((!CC->internal_pgm) || (strlen(recipient) == 0)) {
-               /* If the user is a twit, move to the twit room for posting */
-               if (TWITDETECT) {
-                       if (CC->usersupp.axlevel == 2) {
-                               strcpy(hold_rm, actual_rm);
-                               strcpy(actual_rm, config.c_twitroom);
-                       }
-               }
-               /* ...or if this message is destined for Aide> then go there. */
-               if (strlen(force_room) > 0) {
-                       strcpy(actual_rm, force_room);
-               }
                CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0);
        }
 
@@ -1550,6 +1557,10 @@ void CtdlSaveMsg(struct CtdlMessage *msg,        /* message to save */
 
        /* Perform "after save" hooks */
        PerformMessageHooks(msg, EVT_AFTERSAVE);
+
+       /* */
+       if (strcasecmp(hold_rm, CC->quickroom.QRname))
+               getroom(&CC->quickroom, hold_rm);
 }
 
 
@@ -1598,7 +1609,7 @@ struct CtdlMessage *make_message(
        char buf[256];
        size_t message_len = 0;
        size_t buffer_len = 0;
-       char *ptr;
+       char *ptr, *append;
        struct CtdlMessage *msg;
 
        msg = mallok(sizeof(struct CtdlMessage));
@@ -1664,10 +1675,12 @@ struct CtdlMessage *make_message(
        }
 
        /* read in the lines of message text one by one */
+       append = NULL;
        while (client_gets(buf), strcmp(buf, "000")) {
 
                /* augment the buffer if we have to */
                if ((message_len + strlen(buf) + 2) > buffer_len) {
+                       lprintf(9, "realloking\n");
                        ptr = reallok(msg->cm_fields['M'], (buffer_len * 2) );
                        if (ptr == NULL) {      /* flush if can't allocate */
                                while (client_gets(buf), strcmp(buf, "000")) ;;
@@ -1675,11 +1688,16 @@ struct CtdlMessage *make_message(
                        } else {
                                buffer_len = (buffer_len * 2);
                                msg->cm_fields['M'] = ptr;
+                               append = NULL;
+                               lprintf(9, "buffer_len is %d\n", buffer_len);
                        }
                }
 
-               strcat(msg->cm_fields['M'], buf);
-               strcat(msg->cm_fields['M'], "\n");
+               if (append == NULL) append = msg->cm_fields['M'];
+               while (strlen(append) > 0) ++append;
+               strcpy(append, buf);
+               strcat(append, "\n");
+               message_len = message_len + strlen(buf) + 1;
 
                /* if we've hit the max msg length, flush the rest */
                if (message_len >= config.c_maxmsglen) {
@@ -1852,6 +1870,7 @@ void cmd_ent3(char *entargs)
        char recp[256];
        int a;
        int e = 0;
+       int valid_msg = 1;
        unsigned char ch, which_field;
        struct usersupp tempUS;
        long msglen;
@@ -1925,6 +1944,7 @@ void cmd_ent3(char *entargs)
 
        while (msglen > 0) {
                client_read(&which_field, 1);
+               if (!isalpha(which_field)) valid_msg = 0;
                --msglen;
                tempbuf[0] = 0;
                do {
@@ -1934,11 +1954,12 @@ void cmd_ent3(char *entargs)
                        tempbuf[a+1] = 0;
                        tempbuf[a] = ch;
                } while ( (ch != 0) && (msglen > 0) );
-               msg->cm_fields[which_field] = strdoop(tempbuf);
+               if (valid_msg)
+                       msg->cm_fields[which_field] = strdoop(tempbuf);
        }
 
        msg->cm_flags = CM_SKIP_HOOKS;
-       CtdlSaveMsg(msg, recp, "", e, 0);
+       if (valid_msg) CtdlSaveMsg(msg, recp, "", e, 0);
        CtdlFreeMessage(msg);
        phree(tempbuf);
 }