-
-
-/*
- * Check ndbm/db file(s) to see if the Message-Id of this
- * message matches the Message-Id of a previous message,
- * so we can discard it. If it doesn't match, we add the
- * Message-Id of this message to the ndbm/db file.
- */
-static int
-suppress_duplicates (int fd, char *file)
-{
- int fd1, lockfd, state, result;
- char *cp, buf[BUFSIZ], name[NAMESZ];
- datum key, value;
- DBM *db;
- FILE *in;
-
- if ((fd1 = dup (fd)) == -1)
- return -1;
- if (!(in = fdopen (fd1, "r"))) {
- close (fd1);
- return -1;
- }
- rewind (in);
-
- for (state = FLD;;) {
- state = m_getfld (state, name, buf, sizeof(buf), in);
- switch (state) {
- case FLD:
- case FLDPLUS:
- case FLDEOF:
- /* Search for the message ID */
- if (mh_strcasecmp (name, "Message-ID")) {
- while (state == FLDPLUS)
- state = m_getfld (state, name, buf, sizeof(buf), in);
- continue;
- }
-
- cp = add (buf, NULL);
- while (state == FLDPLUS) {
- state = m_getfld (state, name, buf, sizeof(buf), in);
- cp = add (buf, cp);
- }
- key.dptr = trimcpy (cp);
- key.dsize = strlen (key.dptr) + 1;
- free (cp);
- cp = key.dptr;
-
- if (!(db = dbm_open (file, O_RDWR | O_CREAT, 0600))) {
- advise (file, "unable to perform dbm_open on");
- free (cp);
- fclose (in);
- return -1;
- }
- /*
- * Since it is difficult to portable
- * lock a ndbm file, we will open and
- * lock the Maildelivery file instead.
- * This will fail if your Maildelivery
- * file doesn't exist.
- */
- if ((lockfd = lkopen(file, O_RDWR, 0)) == -1) {
- advise (file, "unable to perform file locking on");
- free (cp);
- fclose (in);
- return -1;
- }
- value = dbm_fetch (db, key);
- if (value.dptr) {
- if (verbose)
- verbose_printf ("Message-ID: %s\n already received on %s",
- cp, value.dptr);
- result = DONE;
- } else {
- value.dptr = ddate + sizeof("Delivery-Date:");
- value.dsize = strlen(value.dptr) + 1;
- if (dbm_store (db, key, value, DBM_INSERT))
- advise (file, "possibly corrupt file");
- result = 0;
- }
-
- dbm_close (db);
- lkclose(lockfd, file);
- free (cp);
- fclose (in);
- return result;
- break;
-
- case BODY:
- case BODYEOF:
- case FILEEOF:
- break;
-
- case LENERR:
- case FMTERR:
- default:
- break;
- }
-
- break;
- }
-
- fclose (in);
- return 0;
-}