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