b586fc3798f2f32d6599851b64381b612609830d
[mmh] / sbr / seq_add.c
1
2 /*
3  * seq_add.c -- add message(s) to a sequence
4  *
5  * $Id$
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     int i, msgnum, new_seq = 1;
27
28     if (!seq_nameok (cp))
29         return 0;
30
31     /*
32      * We keep mp->curmsg and "cur" sequence in sync.
33      * See seq_list() and seq_init().
34      */
35     if (!strcmp (current,cp))
36         mp->curmsg = mp->hghsel;
37
38     /*
39      * Get the number for this sequence
40      */
41     for (i = 0; mp->msgattrs[i]; i++) {
42         if (!strcmp (mp->msgattrs[i], cp)) {
43             new_seq = 0;
44             break;
45         }
46     }
47
48     /*
49      * If this is a new sequence, add a slot for it
50      */
51     if (new_seq) {
52         if (i >= NUMATTRS) {
53             advise (NULL, "only %d sequences allowed (no room for %s)!", NUMATTRS, cp);
54             return 0;
55         }
56         if (!(mp->msgattrs[i] = strdup (cp))) {
57             advise (NULL, "strdup failed");
58             return 0;
59         }
60         mp->msgattrs[i + 1] = NULL;
61     }
62
63     /*
64      * If sequence is new, or zero flag is set, then first
65      * clear the bit for this sequence from all messages.
66      */
67     if (new_seq || zero) {
68         for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
69             clear_sequence (mp, i, msgnum);
70     }
71
72     /*
73      * Now flip on the bit for this sequence
74      * for all selected messages.
75      */
76     for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
77         if (is_selected (mp, msgnum))
78             add_sequence (mp, i, msgnum);
79
80     /*
81      * Set the public/private bit for this sequence.
82      */
83     if (public == 1)
84         make_seq_public (mp, i);
85     else if (public == 0)
86         make_seq_private (mp, i);
87     else if (new_seq) {
88         /*
89          * If public == -1, then only set the
90          * public/private bit for new sequences.
91          */
92         if (is_readonly (mp))
93             make_seq_private (mp, i);
94         else
95             make_seq_public (mp, i);
96     }
97
98     mp->msgflags |= SEQMOD;
99     return 1;
100 }
101
102
103 /*
104  * Add a message to a (possibly new) sequence.
105  *
106  * If public ==  1, make sequence public.
107  * If public ==  0, make sequence private.
108  * If public == -1, leave the public/private bit alone for existing
109  *                  sequences.  For new sequences, set this bit based
110  *                  on its readonly status.
111  *
112  * If error, return 0, else return 1.
113  */
114
115 int
116 seq_addmsg (struct msgs *mp, char *cp, int msgnum, int public, int zero)
117 {
118     int i, j, new_seq = 1;
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     for (i = 0; mp->msgattrs[i]; i++) {
133         if (!strcmp (mp->msgattrs[i], cp)) {
134             new_seq = 0;
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 (i >= NUMATTRS) {
144             advise (NULL, "only %d sequences allowed (no room for %s)!", NUMATTRS, cp);
145             return 0;
146         }
147         if (!(mp->msgattrs[i] = strdup (cp))) {
148             advise (NULL, "strdup failed");
149             return 0;
150         }
151         mp->msgattrs[i + 1] = NULL;
152     }
153
154     /*
155      * If sequence is new, or zero flag is set, then first
156      * clear the bit for this sequence from all messages.
157      */
158     if (new_seq || zero) {
159         for (j = mp->lowmsg; j <= mp->hghmsg; j++)
160             clear_sequence (mp, i, j);
161     }
162
163     /*
164      * Now flip on the bit for this sequence
165      * for this particular message.
166      */
167     add_sequence (mp, i, msgnum);
168
169     /*
170      * Set the public/private bit for this sequence.
171      */
172     if (public == 1)
173         make_seq_public (mp, i);
174     else if (public == 0)
175         make_seq_private (mp, i);
176     else if (new_seq) {
177         /*
178          * If public == -1, then only set the
179          * public/private bit for new sequences.
180          */
181         if (is_readonly (mp))
182             make_seq_private (mp, i);
183         else
184             make_seq_public (mp, i);
185     }
186
187     mp->msgflags |= SEQMOD;
188     return 1;
189 }