]> diplodocus.org Git - nmh/blob - uip/pick.c
Cope with sasl_decode64() returning SASL_CONTINUE as well as SASL_OK.
[nmh] / uip / pick.c
1
2 /*
3 * pick.c -- search for messages by content
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <h/tws.h>
14 #include <h/picksbr.h>
15 #include <h/utils.h>
16
17 static struct swit switches[] = {
18 #define ANDSW 0
19 { "and", 0 },
20 #define ORSW 1
21 { "or", 0 },
22 #define NOTSW 2
23 { "not", 0 },
24 #define LBRSW 3
25 { "lbrace", 0 },
26 #define RBRSW 4
27 { "rbrace", 0 },
28 #define CCSW 5
29 { "cc pattern", 0 },
30 #define DATESW 6
31 { "date pattern", 0 },
32 #define FROMSW 7
33 { "from pattern", 0 },
34 #define SRCHSW 8
35 { "search pattern", 0 },
36 #define SUBJSW 9
37 { "subject pattern", 0 },
38 #define TOSW 10
39 { "to pattern", 0 },
40 #define OTHRSW 11
41 { "-othercomponent pattern", 0 },
42 #define AFTRSW 12
43 { "after date", 0 },
44 #define BEFRSW 13
45 { "before date", 0 },
46 #define DATFDSW 14
47 { "datefield field", 5 },
48 #define SEQSW 15
49 { "sequence name", 0 },
50 #define PUBLSW 16
51 { "public", 0 },
52 #define NPUBLSW 17
53 { "nopublic", 0 },
54 #define ZEROSW 18
55 { "zero", 0 },
56 #define NZEROSW 19
57 { "nozero", 0 },
58 #define LISTSW 20
59 { "list", 0 },
60 #define NLISTSW 21
61 { "nolist", 0 },
62 #define VERSIONSW 22
63 { "version", 0 },
64 #define HELPSW 23
65 { "help", 0 },
66 { NULL, 0 }
67 };
68
69 static int listsw = -1;
70
71 static void putzero_done (int);
72
73 int
74 main (int argc, char **argv)
75 {
76 int publicsw = -1, zerosw = 1, seqp = 0, vecp = 0;
77 int lo, hi, msgnum;
78 char *maildir, *folder = NULL, buf[100];
79 char *cp, **argp, **arguments;
80 char *seqs[NUMATTRS + 1], *vec[MAXARGS];
81 struct msgs_array msgs = { 0, 0, NULL };
82 struct msgs *mp;
83 register FILE *fp;
84
85 done=putzero_done;
86
87 #ifdef LOCALE
88 setlocale(LC_ALL, "");
89 #endif
90 invo_name = r1bindex (argv[0], '/');
91
92 /* read user profile/context */
93 context_read();
94
95 arguments = getarguments (invo_name, argc, argv, 1);
96 argp = arguments;
97
98 while ((cp = *argp++)) {
99 if (*cp == '-') {
100 if (*++cp == '-') {
101 vec[vecp++] = --cp;
102 goto pattern;
103 }
104 switch (smatch (cp, switches)) {
105 case AMBIGSW:
106 ambigsw (cp, switches);
107 listsw = 0; /* HACK */
108 done (1);
109 case UNKWNSW:
110 adios (NULL, "-%s unknown", cp);
111
112 case HELPSW:
113 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
114 invo_name);
115 print_help (buf, switches, 1);
116 listsw = 0; /* HACK */
117 done (1);
118 case VERSIONSW:
119 print_version(invo_name);
120 listsw = 0; /* HACK */
121 done (1);
122
123 case CCSW:
124 case DATESW:
125 case FROMSW:
126 case SUBJSW:
127 case TOSW:
128 case DATFDSW:
129 case AFTRSW:
130 case BEFRSW:
131 case SRCHSW:
132 vec[vecp++] = --cp;
133 pattern:
134 if (!(cp = *argp++))/* allow -xyz arguments */
135 adios (NULL, "missing argument to %s", argp[-2]);
136 vec[vecp++] = cp;
137 continue;
138 case OTHRSW:
139 adios (NULL, "internal error!");
140
141 case ANDSW:
142 case ORSW:
143 case NOTSW:
144 case LBRSW:
145 case RBRSW:
146 vec[vecp++] = --cp;
147 continue;
148
149 case SEQSW:
150 if (!(cp = *argp++) || *cp == '-')
151 adios (NULL, "missing argument to %s", argp[-2]);
152
153 /* check if too many sequences specified */
154 if (seqp >= NUMATTRS)
155 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
156 seqs[seqp++] = cp;
157 continue;
158 case PUBLSW:
159 publicsw = 1;
160 continue;
161 case NPUBLSW:
162 publicsw = 0;
163 continue;
164 case ZEROSW:
165 zerosw++;
166 continue;
167 case NZEROSW:
168 zerosw = 0;
169 continue;
170
171 case LISTSW:
172 listsw = 1;
173 continue;
174 case NLISTSW:
175 listsw = 0;
176 continue;
177 }
178 }
179 if (*cp == '+' || *cp == '@') {
180 if (folder)
181 adios (NULL, "only one folder at a time!");
182 else
183 folder = pluspath (cp);
184 } else
185 app_msgarg(&msgs, cp);
186 }
187 vec[vecp] = NULL;
188
189 if (!context_find ("path"))
190 free (path ("./", TFOLDER));
191
192 /*
193 * If we didn't specify which messages to search,
194 * then search the whole folder.
195 */
196 if (!msgs.size)
197 app_msgarg(&msgs, "all");
198
199 if (!folder)
200 folder = getfolder (1);
201 maildir = m_maildir (folder);
202
203 if (chdir (maildir) == NOTOK)
204 adios (maildir, "unable to change directory to");
205
206 /* read folder and create message structure */
207 if (!(mp = folder_read (folder)))
208 adios (NULL, "unable to read folder %s", folder);
209
210 /* check for empty folder */
211 if (mp->nummsg == 0)
212 adios (NULL, "no messages in %s", folder);
213
214 /* parse all the message ranges/sequences and set SELECTED */
215 for (msgnum = 0; msgnum < msgs.size; msgnum++)
216 if (!m_convert (mp, msgs.msgs[msgnum]))
217 done (1);
218 seq_setprev (mp); /* set the previous-sequence */
219
220 /*
221 * If we aren't saving the results to a sequence,
222 * we default to list the results.
223 */
224 if (listsw == -1)
225 listsw = !seqp;
226
227 if (publicsw == 1 && is_readonly(mp))
228 adios (NULL, "folder %s is read-only, so -public not allowed", folder);
229
230 if (!pcompile (vec, NULL))
231 done (1);
232
233 lo = mp->lowsel;
234 hi = mp->hghsel;
235
236 /*
237 * Scan through all the SELECTED messages and check for a
238 * match. If the message does not match, then unselect it.
239 */
240 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
241 if (is_selected (mp, msgnum)) {
242 if ((fp = fopen (cp = m_name (msgnum), "r")) == NULL)
243 admonish (cp, "unable to read message");
244 if (fp && pmatches (fp, msgnum, 0L, 0L)) {
245 if (msgnum < lo)
246 lo = msgnum;
247 if (msgnum > hi)
248 hi = msgnum;
249 } else {
250 /* if it doesn't match, then unselect it */
251 unset_selected (mp, msgnum);
252 mp->numsel--;
253 }
254 if (fp)
255 fclose (fp);
256 }
257 }
258
259 mp->lowsel = lo;
260 mp->hghsel = hi;
261
262 if (mp->numsel <= 0)
263 adios (NULL, "no messages match specification");
264
265 seqs[seqp] = NULL;
266
267 /*
268 * Add the matching messages to sequences
269 */
270 for (seqp = 0; seqs[seqp]; seqp++)
271 if (!seq_addsel (mp, seqs[seqp], publicsw, zerosw))
272 done (1);
273
274 /*
275 * Print the name of all the matches
276 */
277 if (listsw) {
278 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
279 if (is_selected (mp, msgnum))
280 printf ("%s\n", m_name (msgnum));
281 } else {
282 printf ("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s");
283 }
284
285 context_replace (pfolder, folder); /* update current folder */
286 seq_save (mp); /* synchronize message sequences */
287 context_save (); /* save the context file */
288 folder_free (mp); /* free folder/message structure */
289 done (0);
290 return 1;
291 }
292
293
294 static void
295 putzero_done (int status)
296 {
297 if (listsw && status && !isatty (fileno (stdout)))
298 printf ("0\n");
299 exit (status);
300 }