2 * seq_save.c -- 1) synchronize sequences
3 * -- 2) save public sequences
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/signals.h>
15 * 1. If sequence is public and folder is readonly,
16 * then change it to be private
17 * 2a. If sequence is public, then add it to the sequences file
18 * in folder (name specified by mh-sequences profile entry).
19 * 2b. If sequence is private, then add it to the
24 seq_save (struct msgs *mp)
27 char flags, *cp, attr[BUFSIZ], seqfile[PATH_MAX];
31 /* check if sequence information has changed */
32 if (!(mp->msgflags & SEQMOD))
34 mp->msgflags &= ~SEQMOD;
37 flags = mp->msgflags; /* record folder flags */
40 * If no mh-sequences file is defined, or if a mh-sequences file
41 * is defined but empty (*mh_seq == '\0'), then pretend folder
42 * is readonly. This will force all sequences to be private.
44 if (mh_seq == NULL || *mh_seq == '\0')
47 snprintf (seqfile, sizeof(seqfile), "%s/%s", mp->foldpath, mh_seq);
49 for (i = 0; mp->msgattrs[i]; i++) {
50 snprintf (attr, sizeof(attr), "atr-%s-%s", mp->msgattrs[i], mp->foldpath);
52 /* get space separated list of sequence ranges */
53 if (!(cp = seq_list(mp, mp->msgattrs[i]))) {
54 context_del (attr); /* delete sequence from context */
58 if (is_readonly(mp) || is_seq_private(mp, i)) {
63 context_replace (attr, cp); /* update sequence in context */
68 context_del (attr); /* delete sequence from context */
72 * Attempt to open file for public sequences.
73 * If that fails (probably because folder is
74 * readonly), then make sequence private.
76 if ((fp = lkfopen (seqfile, "w")) == NULL
77 && (unlink (seqfile) == -1 ||
78 (fp = lkfopen (seqfile, "w")) == NULL)) {
79 admonish (attr, "unable to write");
83 /* block a few signals */
85 sigaddset(&set, SIGHUP);
86 sigaddset(&set, SIGINT);
87 sigaddset(&set, SIGQUIT);
88 sigaddset(&set, SIGTERM);
89 SIGPROCMASK (SIG_BLOCK, &set, &oset);
91 fprintf (fp, "%s: %s\n", mp->msgattrs[i], cp);
96 lkfclose (fp, seqfile);
97 SIGPROCMASK (SIG_SETMASK, &oset, &set); /* reset signal mask */
100 * If folder is not readonly, and we didn't save any
101 * public sequences, then remove that file.
103 if (!is_readonly(mp))
108 * Reset folder flag, since we may be
109 * pretending that folder is readonly.
111 mp->msgflags = flags;