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