]> diplodocus.org Git - nmh/blob - sbr/seq_add.c
8b51e31e78d96374f30a7bd92269cbd55dc87f4e
[nmh] / sbr / seq_add.c
1 /* seq_add.c -- add message(s) to a sequence
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "error.h"
10
11
12 /*
13 * Add all the SELECTED messages to a (possibly new) sequence.
14 *
15 * If public == 1, make sequence public.
16 * If public == 0, make sequence private.
17 * If public == -1, leave the public/private bit alone for existing
18 * sequences. For new sequences, set this bit based
19 * on its readonly status.
20 *
21 * If error, return 0, else return 1.
22 */
23
24 int
25 seq_addsel (struct msgs *mp, char *cp, int public, int zero)
26 {
27 unsigned int i;
28 int msgnum;
29
30 if (!seq_nameok (cp))
31 return 0;
32
33 /*
34 * We keep mp->curmsg and "cur" sequence in sync.
35 * See seq_list() and seq_init().
36 */
37 if (!strcmp (current,cp))
38 mp->curmsg = mp->hghsel;
39
40 /*
41 * Get the number for this sequence
42 */
43 bool new_seq = true;
44 for (i = 0; i < svector_size (mp->msgattrs); i++) {
45 if (!strcmp (svector_at (mp->msgattrs, i), cp)) {
46 new_seq = false;
47 break;
48 }
49 }
50
51 /*
52 * If this is a new sequence, add a slot for it
53 */
54 if (new_seq) {
55 if (!(svector_push_back (mp->msgattrs, strdup (cp)))) {
56 inform("strdup failed");
57 return 0;
58 }
59 }
60
61 /*
62 * If sequence is new, or zero flag is set, then first
63 * clear the bit for this sequence from all messages.
64 */
65 if ((new_seq || zero) && mp->nummsg > 0) {
66 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
67 clear_sequence (mp, i, msgnum);
68 }
69
70 /*
71 * Now flip on the bit for this sequence
72 * for all selected messages.
73 */
74 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
75 if (is_selected (mp, msgnum))
76 add_sequence (mp, i, msgnum);
77
78 /*
79 * Set the public/private bit for this sequence.
80 */
81 if (public == 1)
82 make_seq_public (mp, i);
83 else if (public == 0)
84 make_seq_private (mp, i);
85 else if (new_seq) {
86 /*
87 * If public == -1, then only set the
88 * public/private bit for new sequences.
89 */
90 if (is_readonly (mp))
91 make_seq_private (mp, i);
92 else
93 make_seq_public (mp, i);
94 }
95
96 mp->msgflags |= SEQMOD;
97 return 1;
98 }
99
100
101 /*
102 * Add a message to a (possibly new) sequence.
103 *
104 * If public == 1, make sequence public.
105 * If public == 0, make sequence private.
106 * If public == -1, leave the public/private bit alone for existing
107 * sequences. For new sequences, set this bit based
108 * on its readonly status.
109 *
110 * If error, return 0, else return 1.
111 */
112
113 int
114 seq_addmsg (struct msgs *mp, char *cp, int msgnum, int public, int zero)
115 {
116 unsigned int i;
117 int j;
118
119 if (!seq_nameok (cp))
120 return 0;
121
122 /*
123 * keep mp->curmsg and msgattrs["cur"] in sync - see seq_list()
124 */
125 if (!strcmp (current,cp))
126 mp->curmsg = msgnum;
127
128 /*
129 * Get the number for this sequence
130 */
131 bool new_seq = true;
132 for (i = 0; i < svector_size (mp->msgattrs); i++) {
133 if (!strcmp (svector_at (mp->msgattrs, i), cp)) {
134 new_seq = false;
135 break;
136 }
137 }
138
139 /*
140 * If this is a new sequence, add a slot for it
141 */
142 if (new_seq) {
143 if (!(svector_push_back (mp->msgattrs, strdup (cp)))) {
144 inform("strdup failed");
145 return 0;
146 }
147 }
148
149 /*
150 * If sequence is new, or zero flag is set, then first
151 * clear the bit for this sequence from all messages.
152 */
153 if ((new_seq || zero) && mp->nummsg > 0) {
154 for (j = mp->lowmsg; j <= mp->hghmsg; j++)
155 clear_sequence (mp, i, j);
156 }
157
158 /*
159 * Now flip on the bit for this sequence
160 * for this particular message.
161 */
162 add_sequence (mp, i, msgnum);
163
164 /*
165 * Set the public/private bit for this sequence.
166 */
167 if (public == 1)
168 make_seq_public (mp, i);
169 else if (public == 0)
170 make_seq_private (mp, i);
171 else if (new_seq) {
172 /*
173 * If public == -1, then only set the
174 * public/private bit for new sequences.
175 */
176 if (is_readonly (mp))
177 make_seq_private (mp, i);
178 else
179 make_seq_public (mp, i);
180 }
181
182 mp->msgflags |= SEQMOD;
183 return 1;
184 }