]> code.citadel.org Git - citadel.git/blobdiff - citadel/msgbase.c
* IMAP date strings. More stupidity.
[citadel.git] / citadel / msgbase.c
index b4ed04e1f26fad003dfeb477b0cbb07de4d17d53..2ef26071149cd0a162bebd24d2f2440b9f9c591e 100644 (file)
@@ -1,4 +1,9 @@
-/* $Id$ */
+/* 
+ * $Id$
+ *
+ * Implements the message store.
+ *
+ */
 
 #include "sysdep.h"
 #include <stdlib.h>
@@ -80,17 +85,18 @@ void remove_any_whitespace_to_the_left_or_right_of_at_symbol(char *name)
 {
        int i;
 
-       for (i = 0; i < strlen(name); ++i)
+stov:  for (i = 0; i < strlen(name); ++i) {
                if (name[i] == '@') {
                        if (i > 0)
                                if (isspace(name[i - 1])) {
                                        strcpy(&name[i - 1], &name[i]);
-                                       i = 0;
+                                       goto stov; /* start over */
                                }
                        while (isspace(name[i + 1])) {
                                strcpy(&name[i + 1], &name[i + 2]);
                        }
                }
+       }
 }
 
 
@@ -187,6 +193,7 @@ GETSN:              do {
                        while (bbb[strlen(bbb) - 1] == '_')
                                bbb[strlen(bbb) - 1] = 0;
                        sprintf(name, &aaa[4], bbb);
+                       lprintf(9, "returning MES_INTERNET\n");
                        return (MES_INTERNET);
                }
                if (!strncmp(aaa, "bin", 3)) {
@@ -203,10 +210,12 @@ GETSN:            do {
                        while (bbb[0] == ' ')
                                strcpy(bbb, &bbb[1]);
                        sprintf(name, "%s @%s", aaa, bbb);
+                       lprintf(9, "returning MES_BINARY\n");
                        return (MES_BINARY);
                }
                return (MES_ERROR);
        }
+       lprintf(9, "returning MES_LOCAL\n");
        return (MES_LOCAL);
 }
 
@@ -222,7 +231,7 @@ void get_mm(void)
 
 
 
-void simple_listing(long msgnum)
+void simple_listing(long msgnum, void *userdata)
 {
        cprintf("%ld\n", msgnum);
 }
@@ -262,12 +271,14 @@ 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))
+                       void (*CallBack) (long, void *),
+                       void *userdata)
 {
 
        int a;
@@ -275,6 +286,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;
@@ -292,22 +304,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);
 
@@ -347,12 +366,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, userdata);
+                               ++num_processed;
                        }
                }
        phree(msglist);         /* Clean up */
+       return num_processed;
 }
 
 
@@ -416,7 +438,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, NULL);
        if (template != NULL) CtdlFreeMessage(template);
        cprintf("000\n");
 }
@@ -468,7 +492,8 @@ void do_help_subst(char *buffer)
 void memfmout(
        int width,              /* screen width to use */
        char *mptr,             /* where are we going to get our text from? */
-       char subst)             /* nonzero if we should do substitutions */
+       char subst,             /* nonzero if we should do substitutions */
+       char *nl)               /* string to terminate lines with */
 {
        int a, b, c;
        int real = 0;
@@ -507,7 +532,7 @@ FMTA:       if (subst) {
        if (((ch == 13) || (ch == 10)) && (old != 13) && (old != 10))
                ch = 32;
        if (((old == 13) || (old == 10)) && (isspace(real))) {
-               cprintf("\n");
+               cprintf("%s", nl);
                c = 1;
        }
        if (ch > 126)
@@ -515,7 +540,7 @@ FMTA:       if (subst) {
 
        if (ch > 32) {
                if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
-                       cprintf("\n%s", aaa);
+                       cprintf("%s%s", nl, aaa);
                        c = strlen(aaa);
                        aaa[0] = 0;
                }
@@ -525,7 +550,7 @@ FMTA:       if (subst) {
        }
        if (ch == 32) {
                if ((strlen(aaa) + c) > (width - 5)) {
-                       cprintf("\n");
+                       cprintf("%s", nl);
                        c = 1;
                }
                cprintf("%s ", aaa);
@@ -535,14 +560,14 @@ FMTA:     if (subst) {
                goto FMTA;
        }
        if ((ch == 13) || (ch == 10)) {
-               cprintf("%s\n", aaa);
+               cprintf("%s%s", aaa, nl);
                c = 1;
                strcpy(aaa, "");
                goto FMTA;
        }
        goto FMTA;
 
-FMTEND:        cprintf("%s\n", aaa);
+FMTEND:        cprintf("%s%s", aaa, nl);
 }
 
 
@@ -942,8 +967,8 @@ int CtdlOutputMsg(long msg_num,             /* message number (local) to fetch */
                                else if (i == 'R')
                                        cprintf("To: %s%s", mptr, nl);
                                else if (i == 'T') {
-                                       generate_rfc822_datestamp(datestamp,
-                                                               atol(mptr) );
+                                       datestring(datestamp, atol(mptr),
+                                               DATESTRING_RFC822 );
                                        cprintf("Date: %s%s", datestamp, nl);
                                }
                        }
@@ -959,7 +984,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) {
@@ -1049,7 +1081,7 @@ int CtdlOutputMsg(long msg_num,           /* message number (local) to fetch */
         * message to the reader's screen width.
         */
        if (TheMessage->cm_format_type == FMT_CITADEL) {
-               memfmout(80, mptr, 0);
+               memfmout(80, mptr, 0, nl);
        }
 
        /* If the message on disk is format 4 (MIME), we've gotta hand it
@@ -1160,7 +1192,7 @@ void cmd_opna(char *cmdbuf)
 {
        long msgid;
 
-       CtdlAllocUserData(SYM_DESIRED_SECTION, 64);
+       CtdlAllocUserData(SYM_DESIRED_SECTION, 256);
 
        msgid = extract_long(cmdbuf, 0);
        extract(desired_section, cmdbuf, 1);
@@ -1300,19 +1332,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);
        }
        
@@ -1374,7 +1406,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;
@@ -1402,7 +1434,7 @@ void serialize_message(struct ser_ret *ret,               /* return values */
 /*
  * Back end for the ReplicationChecks() function
  */
-void check_repl(long msgnum) {
+void check_repl(long msgnum, void *userdata) {
        struct CtdlMessage *msg;
        time_t timestamp = (-1L);
 
@@ -1422,7 +1454,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, "");
 }
 
 
@@ -1452,7 +1484,8 @@ 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, NULL);
 
        /* If a newer message exists with the same Extended ID, abort
         * this save.
@@ -1475,8 +1508,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 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];
@@ -1493,9 +1525,11 @@ long CtdlSaveMsg(struct CtdlMessage *msg,        /* message to save */
        static int seqnum = 1;
        struct CtdlMessage *imsg;
        char *instr;
+       int mailtype;
 
        lprintf(9, "CtdlSaveMsg() called\n");
        if (is_valid_message(msg) == 0) return(-1);     /* self check */
+       mailtype = supplied_mailtype;
 
        /* If this message has no timestamp, we take the liberty of
         * giving it one, right now.
@@ -1539,6 +1573,7 @@ long CtdlSaveMsg(struct CtdlMessage *msg, /* message to save */
                           == hostalias_localhost) {
                                recipient[a] = 0;
                                lprintf(7, "Changed to <%s>\n", recipient);
+                               mailtype = MES_LOCAL;
                        }
                }
        }
@@ -1632,7 +1667,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 &");
@@ -1659,7 +1694,10 @@ long CtdlSaveMsg(struct CtdlMessage *msg,        /* message to save */
         * is no local sender; it would otherwise go to the Trashcan).
         */
        if ((!CC->internal_pgm) || (strlen(recipient) == 0)) {
-               CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0);
+               if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0) != 0) {
+                       lprintf(3, "ERROR saving message pointer!\n");
+                       CtdlSaveMsgPointerInRoom(AIDEROOM, newmsgid, 0);
+               }
        }
 
        /* For internet mail, drop a copy in the outbound queue room */
@@ -1682,6 +1720,11 @@ long CtdlSaveMsg(struct CtdlMessage *msg,        /* message to save */
                        MailboxName(actual_rm, &userbuf, MAILROOM);
                        CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0);
                }
+               else {
+                       lprintf(9, "No user <%s>, saving in %s> instead\n",
+                               recipient, AIDEROOM);
+                       CtdlSaveMsgPointerInRoom(AIDEROOM, newmsgid, 0);
+               }
        }
 
        /* Perform "after save" hooks */
@@ -1714,7 +1757,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);
        }
 
@@ -1742,7 +1785,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);
 }
@@ -2040,7 +2083,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;
@@ -2145,7 +2188,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);
 }
@@ -2157,7 +2200,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 */
 )
 {
 
@@ -2196,7 +2239,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]);
@@ -2247,7 +2290,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,
@@ -2298,7 +2341,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") );
 }
@@ -2486,7 +2529,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);
 }
 
@@ -2495,7 +2538,7 @@ void CtdlWriteObject(char *req_room,              /* Room to stuff it in */
 
 
 
-void CtdlGetSysConfigBackend(long msgnum) {
+void CtdlGetSysConfigBackend(long msgnum, void *userdata) {
        config_msgnum = msgnum;
 }
 
@@ -2517,8 +2560,8 @@ 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,
-               CtdlGetSysConfigBackend);
+       CtdlForEachMessage(MSGS_LAST, 1, (-127), sysconfname, NULL,
+               CtdlGetSysConfigBackend, NULL);
        msgnum = config_msgnum;
        end_critical_section(S_CONFIG);