/** * mh_read_sequences - Read a set of MH sequences * @param mhs Existing sequences * @param path File to read from * @retval 0 Success * @retval -1 Error */ int mh_read_sequences(struct MhSequences *mhs, const char *path) { int line = 1; char *buf = NULL; size_t sz = 0; MhSeqFlags flags; int first, last, rc = 0; char pathname[PATH_MAX]; snprintf(pathname, sizeof(pathname), "%s/.mh_sequences", path); FILE *fp = fopen(pathname, "r"); if (!fp) return 0; /* yes, ask callers to silently ignore the error */ while ((buf = mutt_file_read_line(buf, &sz, fp, &line, 0))) { char *t = strtok(buf, " \t:"); if (!t) continue; if (mutt_str_strcmp(t, C_MhSeqUnseen) == 0) flags = MH_SEQ_UNSEEN; else if (mutt_str_strcmp(t, C_MhSeqFlagged) == 0) flags = MH_SEQ_FLAGGED; else if (mutt_str_strcmp(t, C_MhSeqReplied) == 0) flags = MH_SEQ_REPLIED; else /* unknown sequence */ continue; while ((t = strtok(NULL, " \t:"))) { if (mh_read_token(t, &first, &last) < 0) { mhs_free_sequences(mhs); rc = -1; goto out; } for (; first <= last; first++) mhs_set(mhs, first, flags); } } rc = 0; out: FREE(&buf); mutt_file_fclose(&fp); return rc; }
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); } } safe_free ((void **) &buff); safe_fclose (&fp); }
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); } safe_free ((void **) &tmpfname); }
/** * mh_update_sequences - Update sequence numbers * @param m Mailbox * * XXX we don't currently remove deleted messages from sequences we don't know. * Should we? */ void mh_update_sequences(struct Mailbox *m) { char sequences[PATH_MAX]; char *tmpfname = NULL; char *buf = NULL; char *p = NULL; size_t s; int l = 0; int i; int unseen = 0; int flagged = 0; int replied = 0; char seq_unseen[256]; char seq_replied[256]; char seq_flagged[256]; struct MhSequences mhs = { 0 }; snprintf(seq_unseen, sizeof(seq_unseen), "%s:", NONULL(C_MhSeqUnseen)); snprintf(seq_replied, sizeof(seq_replied), "%s:", NONULL(C_MhSeqReplied)); snprintf(seq_flagged, sizeof(seq_flagged), "%s:", NONULL(C_MhSeqFlagged)); FILE *fp_new = NULL; if (mh_mkstemp(m, &fp_new, &tmpfname) != 0) { /* error message? */ return; } snprintf(sequences, sizeof(sequences), "%s/.mh_sequences", m->path); /* first, copy unknown sequences */ FILE *fp_old = fopen(sequences, "r"); if (fp_old) { while ((buf = mutt_file_read_line(buf, &s, fp_old, &l, 0))) { if (mutt_str_startswith(buf, seq_unseen, CASE_MATCH) || mutt_str_startswith(buf, seq_flagged, CASE_MATCH) || mutt_str_startswith(buf, seq_replied, CASE_MATCH)) continue; fprintf(fp_new, "%s\n", buf); } } mutt_file_fclose(&fp_old); /* now, update our unseen, flagged, and replied sequences */ for (l = 0; l < m->msg_count; l++) { if (m->emails[l]->deleted) continue; p = strrchr(m->emails[l]->path, '/'); if (p) p++; else p = m->emails[l]->path; if (mutt_str_atoi(p, &i) < 0) continue; if (!m->emails[l]->read) { mhs_set(&mhs, i, MH_SEQ_UNSEEN); unseen++; } if (m->emails[l]->flagged) { mhs_set(&mhs, i, MH_SEQ_FLAGGED); flagged++; } if (m->emails[l]->replied) { mhs_set(&mhs, i, MH_SEQ_REPLIED); replied++; } } /* write out the new sequences */ if (unseen) mhs_write_one_sequence(fp_new, &mhs, MH_SEQ_UNSEEN, NONULL(C_MhSeqUnseen)); if (flagged) mhs_write_one_sequence(fp_new, &mhs, MH_SEQ_FLAGGED, NONULL(C_MhSeqFlagged)); if (replied) mhs_write_one_sequence(fp_new, &mhs, MH_SEQ_REPLIED, NONULL(C_MhSeqReplied)); mhs_free_sequences(&mhs); /* try to commit the changes - no guarantee here */ mutt_file_fclose(&fp_new); unlink(sequences); if (mutt_file_safe_rename(tmpfname, sequences) != 0) { /* report an error? */ unlink(tmpfname); } FREE(&tmpfname); }