]> code.citadel.org Git - citadel.git/blobdiff - citadel/msgbase.c
* Changed the comments at the beginning of each file to a consistent format
[citadel.git] / citadel / msgbase.c
index 4c91cd80777c0c39adb78dbc72e9177f0705805b..086ca77e63b2aeaaf4d4fd49757677e714b630d6 100644 (file)
@@ -1,4 +1,9 @@
-/* $Id$ */
+/* 
+ * $Id$
+ *
+ * Implements the message store.
+ *
+ */
 
 #include "sysdep.h"
 #include <stdlib.h>
@@ -104,13 +109,16 @@ int alias(char *name)
        int a, b;
        char aaa[300], bbb[300];
 
+       TRACE;
        remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
+       TRACE;
 
        fp = fopen("network/mail.aliases", "r");
        if (fp == NULL)
                fp = fopen("/dev/null", "r");
        if (fp == NULL)
                return (MES_ERROR);
+       TRACE;
        strcpy(aaa, "");
        strcpy(bbb, "");
        while (fgets(aaa, sizeof aaa, fp) != NULL) {
@@ -127,6 +135,7 @@ int alias(char *name)
                if (!strcasecmp(name, aaa))
                        strcpy(name, bbb);
        }
+       TRACE;
        fclose(fp);
        lprintf(7, "Mail is being forwarded to %s\n", name);
 
@@ -141,15 +150,18 @@ int alias(char *name)
        }
 
        /* determine local or remote type, see citadel.h */
+       TRACE;
        for (a = 0; a < strlen(name); ++a)
                if (name[a] == '!')
                        return (MES_INTERNET);
+       TRACE;
        for (a = 0; a < strlen(name); ++a)
                if (name[a] == '@')
                        for (b = a; b < strlen(name); ++b)
                                if (name[b] == '.')
                                        return (MES_INTERNET);
        b = 0;
+       TRACE;
        for (a = 0; a < strlen(name); ++a)
                if (name[a] == '@')
                        ++b;
@@ -169,6 +181,7 @@ int alias(char *name)
 GETSN:         do {
                        a = getstring(fp, aaa);
                } while ((a >= 0) && (strcasecmp(aaa, bbb)));
+               TRACE;
                a = getstring(fp, aaa);
                if (!strncmp(aaa, "use ", 4)) {
                        strcpy(bbb, &aaa[4]);
@@ -176,6 +189,7 @@ GETSN:              do {
                        goto GETSN;
                }
                fclose(fp);
+               TRACE;
                if (!strncmp(aaa, "uum", 3)) {
                        strcpy(bbb, name);
                        for (a = 0; a < strlen(bbb); ++a) {
@@ -209,6 +223,7 @@ GETSN:              do {
                }
                return (MES_ERROR);
        }
+       TRACE;
        lprintf(9, "returning MES_LOCAL\n");
        return (MES_LOCAL);
 }
@@ -265,9 +280,10 @@ int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
 
 /*
  * API function to perform an operation for each qualifying message in the
- * current room.
+ * current room.  (Returns the number of messages processed.)
  */
-void CtdlForEachMessage(int mode, long ref,
+int CtdlForEachMessage(int mode, long ref,
+                       int moderation_level,
                        char *content_type,
                        struct CtdlMessage *compare,
                        void (*CallBack) (long msgnum))
@@ -278,6 +294,7 @@ void CtdlForEachMessage(int mode, long ref,
        struct cdbdata *cdbfr;
        long *msglist = NULL;
        int num_msgs = 0;
+       int num_processed = 0;
        long thismsg;
        struct SuppMsgInfo smi;
        struct CtdlMessage *msg;
@@ -295,22 +312,29 @@ void CtdlForEachMessage(int mode, long ref,
                num_msgs = cdbfr->len / sizeof(long);
                cdb_free(cdbfr);
        } else {
-               return;         /* No messages at all?  No further action. */
+               return 0;       /* No messages at all?  No further action. */
        }
 
 
-       /* If the caller is looking for a specific MIME type, then filter
-        * out all messages which are not of the type requested.
-        */
-       if (num_msgs > 0)
-               if (content_type != NULL)
-                       if (strlen(content_type) > 0)
-                               for (a = 0; a < num_msgs; ++a) {
-                                       GetSuppMsgInfo(&smi, msglist[a]);
-                                       if (strcasecmp(smi.smi_content_type, content_type)) {
-                                               msglist[a] = 0L;
-                                       }
-                               }
+       if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
+               GetSuppMsgInfo(&smi, msglist[a]);
+
+               /* Filter out messages that are moderated below the level
+                * currently being viewed at.
+                */
+               if (smi.smi_mod < moderation_level) {
+                       msglist[a] = 0L;
+               }
+
+               /* If the caller is looking for a specific MIME type, filter
+                * out all messages which are not of the type requested.
+                */
+               if (content_type != NULL) if (strlen(content_type) > 0) {
+                       if (strcasecmp(smi.smi_content_type, content_type)) {
+                               msglist[a] = 0L;
+                       }
+               }
+       }
 
        num_msgs = sort_msglist(msglist, num_msgs);
 
@@ -350,12 +374,15 @@ void CtdlForEachMessage(int mode, long ref,
                                       || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
                                   || ((mode == MSGS_FIRST) && (a < ref))
                                || ((mode == MSGS_GT) && (thismsg > ref))
+                               || ((mode == MSGS_EQ) && (thismsg == ref))
                            )
                            ) {
-                               CallBack(thismsg);
+                               if (CallBack) CallBack(thismsg);
+                               ++num_processed;
                        }
                }
        phree(msglist);         /* Clean up */
+       return num_processed;
 }
 
 
@@ -419,7 +446,9 @@ void cmd_msgs(char *cmdbuf)
                cprintf("%d Message list...\n", LISTING_FOLLOWS);
        }
 
-       CtdlForEachMessage(mode, cm_ref, NULL, template, simple_listing);
+       CtdlForEachMessage(mode, cm_ref,
+               CC->usersupp.moderation_filter,
+               NULL, template, simple_listing);
        if (template != NULL) CtdlFreeMessage(template);
        cprintf("000\n");
 }
@@ -963,7 +992,14 @@ int CtdlOutputMsg(long msg_num,            /* message number (local) to fetch */
                if (!strcasecmp(snode, NODENAME)) {
                        strcpy(snode, FQDN);
                }
-               cprintf("Message-ID: <%s@%s>%s", mid, snode, nl);
+
+               /* Construct a fun message id */
+               cprintf("Message-ID: <%s", mid);
+               if (strchr(mid, '@')==NULL) {
+                       cprintf("@%s", snode);
+               }
+               cprintf(">%s", nl);
+
                PerformUserHooks(luser, (-1L), EVT_OUTPUTMSG);
 
                if (strlen(fuser) > 0) {
@@ -1304,19 +1340,19 @@ int CtdlSaveMsgPointerInRoom(char *roomname, long msgid, int flags) {
  *
  */
 long send_message(struct CtdlMessage *msg,     /* pointer to buffer */
-               int generate_id,                /* generate 'I' field? */
                FILE *save_a_copy)              /* save a copy to disk? */
 {
        long newmsgid;
        long retval;
-       char msgidbuf[32];
+       char msgidbuf[256];
         struct ser_ret smr;
 
        /* Get a new message number */
        newmsgid = get_new_message_number();
-       sprintf(msgidbuf, "%ld", newmsgid);
+       sprintf(msgidbuf, "%ld@%s", newmsgid, config.c_fqdn);
 
-       if (generate_id) {
+       /* Generate an ID if we don't have one already */
+       if (msg->cm_fields['I']==NULL) {
                msg->cm_fields['I'] = strdoop(msgidbuf);
        }
        
@@ -1378,7 +1414,7 @@ void serialize_message(struct ser_ret *ret,               /* return values */
                ret->len = ret->len +
                        strlen(msg->cm_fields[(int)forder[i]]) + 2;
 
-       lprintf(9, "calling malloc\n");
+       lprintf(9, "calling malloc(%d)\n", ret->len);
        ret->ser = mallok(ret->len);
        if (ret->ser == NULL) {
                ret->len = 0;
@@ -1426,7 +1462,7 @@ void check_repl(long msgnum) {
        lprintf(9, "older!\n");
 
        /* Existing isn't newer?  Then delete the old one(s). */
-       CtdlDeleteMessages(CC->quickroom.QRname, msgnum, NULL);
+       CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
 }
 
 
@@ -1456,7 +1492,7 @@ int ReplicationChecks(struct CtdlMessage *msg) {
        memset(template, 0, sizeof(struct CtdlMessage));
        template->cm_fields['E'] = strdoop(msg->cm_fields['E']);
 
-       CtdlForEachMessage(MSGS_ALL, 0L, NULL, template, check_repl);
+       CtdlForEachMessage(MSGS_ALL, 0L, (-127), NULL, template, check_repl);
 
        /* If a newer message exists with the same Extended ID, abort
         * this save.
@@ -1479,8 +1515,7 @@ int ReplicationChecks(struct CtdlMessage *msg) {
 long CtdlSaveMsg(struct CtdlMessage *msg,      /* message to save */
                char *rec,                      /* Recipient (mail) */
                char *force,                    /* force a particular room? */
-               int supplied_mailtype,          /* local or remote type */
-               int generate_id)                /* 1 = generate 'I' field */
+               int supplied_mailtype)          /* local or remote type */
 {
        char aaa[100];
        char hold_rm[ROOMNAMELEN];
@@ -1639,7 +1674,7 @@ long CtdlSaveMsg(struct CtdlMessage *msg, /* message to save */
 
        /* Save it to disk */
        lprintf(9, "Saving to disk\n");
-       newmsgid = send_message(msg, generate_id, network_fp);
+       newmsgid = send_message(msg, network_fp);
        if (network_fp != NULL) {
                fclose(network_fp);
                system("exec nohup ./netproc -i >/dev/null 2>&1 &");
@@ -1729,7 +1764,7 @@ long CtdlSaveMsg(struct CtdlMessage *msg, /* message to save */
                imsg->cm_format_type = FMT_RFC822;
                imsg->cm_fields['A'] = strdoop("Citadel");
                imsg->cm_fields['M'] = instr;
-               CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL, 1);
+               CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL);
                CtdlFreeMessage(imsg);
        }
 
@@ -1757,7 +1792,7 @@ void quickie_message(char *from, char *to, char *room, char *text)
                msg->cm_fields['R'] = strdoop(to);
        msg->cm_fields['M'] = strdoop(text);
 
-       CtdlSaveMsg(msg, "", room, MES_LOCAL, 1);
+       CtdlSaveMsg(msg, "", room, MES_LOCAL);
        CtdlFreeMessage(msg);
        syslog(LOG_NOTICE, text);
 }
@@ -2055,7 +2090,7 @@ SKFALL:   b = MES_NORMAL;
                        CC->quickroom.QRname, b, e, format_type, "");
 
        if (msg != NULL)
-               CtdlSaveMsg(msg, buf, (mtsflag ? AIDEROOM : ""), e, 1);
+               CtdlSaveMsg(msg, buf, (mtsflag ? AIDEROOM : ""), e);
                CtdlFreeMessage(msg);
        CC->fake_postname[0] = '\0';
        return;
@@ -2160,7 +2195,7 @@ void cmd_ent3(char *entargs)
        }
 
        msg->cm_flags = CM_SKIP_HOOKS;
-       if (valid_msg) CtdlSaveMsg(msg, recp, "", e, 0);
+       if (valid_msg) CtdlSaveMsg(msg, recp, "", e);
        CtdlFreeMessage(msg);
        phree(tempbuf);
 }
@@ -2172,7 +2207,7 @@ void cmd_ent3(char *entargs)
  */
 int CtdlDeleteMessages(char *room_name,                /* which room */
                       long dmsgnum,            /* or "0" for any */
-                      char *content_type       /* or NULL for any */
+                      char *content_type       /* or "" for any */
 )
 {
 
@@ -2211,7 +2246,7 @@ int CtdlDeleteMessages(char *room_name,           /* which room */
                        if ((dmsgnum == 0L) || (msglist[i] == dmsgnum)) {
                                delete_this |= 0x01;
                        }
-                       if (content_type == NULL) {
+                       if (strlen(content_type) == 0) {
                                delete_this |= 0x02;
                        } else {
                                GetSuppMsgInfo(&smi, msglist[i]);
@@ -2262,7 +2297,7 @@ void cmd_dele(char *delstr)
        }
        delnum = extract_long(delstr, 0);
 
-       num_deleted = CtdlDeleteMessages(CC->quickroom.QRname, delnum, NULL);
+       num_deleted = CtdlDeleteMessages(CC->quickroom.QRname, delnum, "");
 
        if (num_deleted) {
                cprintf("%d %d message%s deleted.\n", OK,
@@ -2313,7 +2348,7 @@ void cmd_move(char *args)
        /* Now delete the message from the source room,
         * if this is a 'move' rather than a 'copy' operation.
         */
-       if (is_copy == 0) CtdlDeleteMessages(CC->quickroom.QRname, num, NULL);
+       if (is_copy == 0) CtdlDeleteMessages(CC->quickroom.QRname, num, "");
 
        cprintf("%d Message %s.\n", OK, (is_copy ? "copied" : "moved") );
 }
@@ -2501,7 +2536,7 @@ void CtdlWriteObject(char *req_room,              /* Room to stuff it in */
                        CtdlDeleteMessages(roomname, 0L, content_type));
        }
        /* Now write the data */
-       CtdlSaveMsg(msg, "", roomname, MES_LOCAL, 1);
+       CtdlSaveMsg(msg, "", roomname, MES_LOCAL);
        CtdlFreeMessage(msg);
 }
 
@@ -2532,7 +2567,7 @@ char *CtdlGetSysConfig(char *sysconfname) {
        /* We want the last (and probably only) config in this room */
        begin_critical_section(S_CONFIG);
        config_msgnum = (-1L);
-       CtdlForEachMessage(MSGS_LAST, 1, sysconfname, NULL,
+       CtdlForEachMessage(MSGS_LAST, 1, (-127), sysconfname, NULL,
                CtdlGetSysConfigBackend);
        msgnum = config_msgnum;
        end_critical_section(S_CONFIG);