📄 mh.c
字号:
/* * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org> * Copyright (C) 1999-2002 Thomas Roessler <roessler@does-not-exist.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *//* * This file contains routines specific to MH and ``maildir'' style * mailboxes. */#if HAVE_CONFIG_H# include "config.h"#endif#include "mutt.h"#include "mailbox.h"#include "mx.h"#include "copy.h"#include "buffy.h"#include "sort.h"#include "account.h"#if USE_HCACHE#include "hcache.h"#endif#include <sys/stat.h>#include <sys/types.h>#include <dirent.h>#include <limits.h>#include <unistd.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <ctype.h>#include <errno.h>#include <string.h>#include <utime.h>#if HAVE_SYS_TIME_H#include <sys/time.h>#endifstruct maildir{ HEADER *h; char *canon_fname; unsigned header_parsed:1;#ifdef USE_INODESORT ino_t inode;#endif /* USE_INODESORT */ struct maildir *next;};struct mh_sequences{ int max; short *flags;};/* mh_sequences support */#define MH_SEQ_UNSEEN (1 << 0)#define MH_SEQ_REPLIED (1 << 1)#define MH_SEQ_FLAGGED (1 << 2)static void mhs_alloc (struct mh_sequences *mhs, int i){ int j; int newmax; if (i > mhs->max || !mhs->flags) { newmax = i + 128; safe_realloc (&mhs->flags, sizeof (mhs->flags[0]) * (newmax + 1)); for (j = mhs->max + 1; j <= newmax; j++) mhs->flags[j] = 0; mhs->max = newmax; }}static void mhs_free_sequences (struct mh_sequences *mhs){ FREE (&mhs->flags);}static short mhs_check (struct mh_sequences *mhs, int i){ if (!mhs->flags || i > mhs->max) return 0; else return mhs->flags[i];}static short mhs_set (struct mh_sequences *mhs, int i, short f){ mhs_alloc (mhs, i); mhs->flags[i] |= f; return mhs->flags[i];}#if 0/* unused */static short mhs_unset (struct mh_sequences *mhs, int i, short f){ mhs_alloc (mhs, i); mhs->flags[i] &= ~f; return mhs->flags[i];}#endifstatic void mh_read_token (char *t, int *first, int *last){ char *p; if ((p = strchr (t, '-'))) { *p++ = '\0'; *first = atoi (t); *last = atoi (p); } else *first = *last = atoi (t);}static void mh_read_sequences (struct mh_sequences *mhs, const char *path){ FILE *fp; int line = 1; char *buff = NULL; char *t; size_t sz = 0; short f; int first, last; char pathname[_POSIX_PATH_MAX]; snprintf (pathname, sizeof (pathname), "%s/.mh_sequences", path); if (!(fp = fopen (pathname, "r"))) return; while ((buff = mutt_read_line (buff, &sz, fp, &line))) { if (!(t = strtok (buff, " \t:"))) continue; if (!mutt_strcmp (t, MhUnseen)) f = MH_SEQ_UNSEEN; else if (!mutt_strcmp (t, MhFlagged)) f = MH_SEQ_FLAGGED; else if (!mutt_strcmp (t, MhReplied)) f = MH_SEQ_REPLIED; else /* unknown sequence */ continue; while ((t = strtok (NULL, " \t:"))) { mh_read_token (t, &first, &last); for (; first <= last; first++) mhs_set (mhs, first, f); } } FREE (&buff); safe_fclose (&fp);}int mh_buffy (const char *path){ int i, r = 0; struct mh_sequences mhs; memset (&mhs, 0, sizeof (mhs)); mh_read_sequences (&mhs, path); for (i = 0; !r && i <= mhs.max; i++) if (mhs_check (&mhs, i) & MH_SEQ_UNSEEN) r = 1; mhs_free_sequences (&mhs); return r;}static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt){ int fd; char path[_POSIX_PATH_MAX]; FOREVER { snprintf (path, _POSIX_PATH_MAX, "%s/.mutt-%s-%d-%d", dest->path, NONULL (Hostname), (int) getpid (), Counter++); if ((fd = open (path, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1) { if (errno != EEXIST) { mutt_perror (path); return -1; } } else { *tgt = safe_strdup (path); break; } } if ((*fp = fdopen (fd, "w")) == NULL) { FREE (tgt); /* __FREE_CHECKED__ */ close (fd); unlink (path); return (-1); } return 0;}static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs, short f, const char *tag){ int i; int first, last; fprintf (fp, "%s:", tag); first = -1; last = -1; for (i = 0; i <= mhs->max; i++) { if ((mhs_check (mhs, i) & f)) { if (first < 0) first = i; else last = i; } else if (first >= 0) { if (last < 0) fprintf (fp, " %d", first); else fprintf (fp, " %d-%d", first, last); first = -1; last = -1; } } if (first >= 0) { if (last < 0) fprintf (fp, " %d", first); else fprintf (fp, " %d-%d", first, last); } fputc ('\n', fp);}/* XXX - we don't currently remove deleted messages from sequences we don't know. Should we? */void mh_update_sequences (CONTEXT * ctx){ FILE *ofp, *nfp; char sequences[_POSIX_PATH_MAX]; char *tmpfname; char *buff = NULL; char *p; size_t s; int l = 0; int i; int unseen = 0; int flagged = 0; int replied = 0; char seq_unseen[STRING]; char seq_replied[STRING]; char seq_flagged[STRING]; struct mh_sequences mhs; memset (&mhs, 0, sizeof (mhs)); snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen)); snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied)); snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged)); if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0) { /* error message? */ return; } snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path); /* first, copy unknown sequences */ if ((ofp = fopen (sequences, "r"))) { while ((buff = mutt_read_line (buff, &s, ofp, &l))) { if (!mutt_strncmp (buff, seq_unseen, mutt_strlen (seq_unseen))) continue; if (!mutt_strncmp (buff, seq_flagged, mutt_strlen (seq_flagged))) continue; if (!mutt_strncmp (buff, seq_replied, mutt_strlen (seq_replied))) continue; fprintf (nfp, "%s\n", buff); } } safe_fclose (&ofp); /* now, update our unseen, flagged, and replied sequences */ for (l = 0; l < ctx->msgcount; l++) { if (ctx->hdrs[l]->deleted) continue; if ((p = strrchr (ctx->hdrs[l]->path, '/'))) p++; else p = ctx->hdrs[l]->path; i = atoi (p); if (!ctx->hdrs[l]->read) { mhs_set (&mhs, i, MH_SEQ_UNSEEN); unseen++; } if (ctx->hdrs[l]->flagged) { mhs_set (&mhs, i, MH_SEQ_FLAGGED); flagged++; } if (ctx->hdrs[l]->replied) { mhs_set (&mhs, i, MH_SEQ_REPLIED); replied++; } } /* write out the new sequences */ if (unseen) mhs_write_one_sequence (nfp, &mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen)); if (flagged) mhs_write_one_sequence (nfp, &mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged)); if (replied) mhs_write_one_sequence (nfp, &mhs, MH_SEQ_REPLIED, NONULL (MhReplied)); mhs_free_sequences (&mhs); /* try to commit the changes - no guarantee here */ safe_fclose (&nfp); unlink (sequences); if (safe_rename (tmpfname, sequences) != 0) { /* report an error? */ unlink (tmpfname); } FREE (&tmpfname);}static void mh_sequences_add_one (CONTEXT * ctx, int n, short unseen, short flagged, short replied){ short unseen_done = 0; short flagged_done = 0; short replied_done = 0; FILE *ofp = NULL, *nfp = NULL; char *tmpfname; char sequences[_POSIX_PATH_MAX]; char seq_unseen[STRING]; char seq_replied[STRING]; char seq_flagged[STRING]; char *buff = NULL; int line; size_t sz; if (mh_mkstemp (ctx, &nfp, &tmpfname) == -1) return; snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen)); snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied)); snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged)); snprintf (sequences, sizeof (sequences), "%s/.mh_sequences", ctx->path); if ((ofp = fopen (sequences, "r"))) { while ((buff = mutt_read_line (buff, &sz, ofp, &line))) { if (unseen && !strncmp (buff, seq_unseen, mutt_strlen (seq_unseen))) { fprintf (nfp, "%s %d\n", buff, n); unseen_done = 1; } else if (flagged && !strncmp (buff, seq_flagged, mutt_strlen (seq_flagged))) { fprintf (nfp, "%s %d\n", buff, n); flagged_done = 1; } else if (replied && !strncmp (buff, seq_replied, mutt_strlen (seq_replied))) { fprintf (nfp, "%s %d\n", buff, n); replied_done = 1; } else fprintf (nfp, "%s\n", buff); } } safe_fclose (&ofp); FREE (&buff); if (!unseen_done && unseen) fprintf (nfp, "%s: %d\n", NONULL (MhUnseen), n); if (!flagged_done && flagged) fprintf (nfp, "%s: %d\n", NONULL (MhFlagged), n); if (!replied_done && replied) fprintf (nfp, "%s: %d\n", NONULL (MhReplied), n); safe_fclose (&nfp); unlink (sequences); if (safe_rename (tmpfname, sequences) != 0) unlink (tmpfname); FREE (&tmpfname);}static void mh_update_maildir (struct maildir *md, struct mh_sequences *mhs){ int i; short f; char *p; for (; md; md = md->next) { if ((p = strrchr (md->h->path, '/'))) p++; else p = md->h->path; i = atoi (p); f = mhs_check (mhs, i); md->h->read = (f & MH_SEQ_UNSEEN) ? 0 : 1; md->h->flagged = (f & MH_SEQ_FLAGGED) ? 1 : 0; md->h->replied = (f & MH_SEQ_REPLIED) ? 1 : 0; }}/* maildir support */static void maildir_free_entry (struct maildir **md){ if (!md || !*md) return; FREE (&(*md)->canon_fname); if ((*md)->h) mutt_free_header (&(*md)->h); FREE (md); /* __FREE_CHECKED__ */}static void maildir_free_maildir (struct maildir **md){ struct maildir *p, *q; if (!md || !*md) return; for (p = *md; p; p = q) { q = p->next; maildir_free_entry (&p); }}static void maildir_parse_flags (HEADER * h, const char *path){ char *p, *q = NULL; h->flagged = 0; h->read = 0; h->replied = 0; if ((p = strrchr (path, ':')) != NULL && mutt_strncmp (p + 1, "2,", 2) == 0) { p += 3;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -