]> diplodocus.org Git - nmh/blob - uip/sortm.c
Removed export of most of the variables in test/common.sh.in. The
[nmh] / uip / sortm.c
1
2 /*
3 * sortm.c -- sort messages in a folder by date/time
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/tws.h>
12 #include <h/utils.h>
13
14 #define SORTM_SWITCHES \
15 X("datefield field", 0, DATESW) \
16 X("textfield field", 0, TEXTSW) \
17 X("notextfield", 0, NSUBJSW) \
18 X("subject", -3, SUBJSW) /* backward-compatibility */ \
19 X("limit days", 0, LIMSW) \
20 X("nolimit", 0, NLIMSW) \
21 X("verbose", 0, VERBSW) \
22 X("noverbose", 0, NVERBSW) \
23 X("all", 0, ALLMSGS) \
24 X("noall", 0, NALLMSGS) \
25 X("check", 0, CHECKSW) \
26 X("nocheck", 0, NCHECKSW) \
27 X("version", 0, VERSIONSW) \
28 X("help", 0, HELPSW) \
29
30 #define X(sw, minchars, id) id,
31 DEFINE_SWITCH_ENUM(SORTM);
32 #undef X
33
34 #define X(sw, minchars, id) { sw, minchars, id },
35 DEFINE_SWITCH_ARRAY(SORTM, switches);
36 #undef X
37
38 struct smsg {
39 int s_msg;
40 time_t s_clock;
41 char *s_subj;
42 };
43
44 static struct smsg *smsgs;
45 int nmsgs;
46
47 char *subjsort = (char *) 0; /* sort on subject if != 0 */
48 time_t datelimit = 0;
49 int submajor = 0; /* if true, sort on subject-major */
50 int verbose;
51 int allmsgs = 1;
52 int check_failed = 0;
53
54 /* This keeps compiler happy on calls to qsort */
55 typedef int (*qsort_comp) (const void *, const void *);
56
57 /*
58 * static prototypes
59 */
60 static int read_hdrs (struct msgs *, char *);
61 static int get_fields (char *, int, struct smsg *);
62 static int dsort (struct smsg **, struct smsg **);
63 static int subsort (struct smsg **, struct smsg **);
64 static int txtsort (struct smsg **, struct smsg **);
65 static void rename_chain (struct msgs *, struct smsg **, int, int);
66 static void rename_msgs (struct msgs *, struct smsg **);
67
68
69 int
70 main (int argc, char **argv)
71 {
72 int i, msgnum;
73 char *cp, *maildir, *datesw = NULL;
74 char *folder = NULL, buf[BUFSIZ], **argp;
75 char **arguments;
76 struct msgs_array msgs = { 0, 0, NULL };
77 struct msgs *mp;
78 struct smsg **dlist;
79 int checksw = 0;
80
81 if (nmh_init(argv[0], 1)) { return 1; }
82
83 arguments = getarguments (invo_name, argc, argv, 1);
84 argp = arguments;
85
86 /*
87 * Parse arguments
88 */
89 while ((cp = *argp++)) {
90 if (*cp == '-') {
91 switch (smatch (++cp, switches)) {
92 case AMBIGSW:
93 ambigsw (cp, switches);
94 done (1);
95 case UNKWNSW:
96 adios (NULL, "-%s unknown", cp);
97
98 case HELPSW:
99 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
100 invo_name);
101 print_help (buf, switches, 1);
102 done (0);
103 case VERSIONSW:
104 print_version(invo_name);
105 done (0);
106
107 case DATESW:
108 if (datesw)
109 adios (NULL, "only one date field at a time");
110 if (!(datesw = *argp++) || *datesw == '-')
111 adios (NULL, "missing argument to %s", argp[-2]);
112 continue;
113
114 case TEXTSW:
115 if (subjsort)
116 adios (NULL, "only one text field at a time");
117 if (!(subjsort = *argp++) || *subjsort == '-')
118 adios (NULL, "missing argument to %s", argp[-2]);
119 continue;
120
121 case SUBJSW:
122 subjsort = "subject";
123 continue;
124 case NSUBJSW:
125 subjsort = (char *)0;
126 continue;
127
128 case LIMSW:
129 if (!(cp = *argp++) || *cp == '-')
130 adios (NULL, "missing argument to %s", argp[-2]);
131 while (*cp == '0')
132 cp++; /* skip any leading zeros */
133 if (!*cp) { /* hit end of string */
134 submajor++; /* sort subject-major */
135 continue;
136 }
137 if (!isdigit((unsigned char) *cp) || !(datelimit = atoi(cp)))
138 adios (NULL, "impossible limit %s", cp);
139 datelimit *= 60*60*24;
140 continue;
141 case NLIMSW:
142 submajor = 0; /* use date-major, but */
143 datelimit = 0; /* use no limit */
144 continue;
145
146 case VERBSW:
147 verbose++;
148 continue;
149 case NVERBSW:
150 verbose = 0;
151 continue;
152
153 case ALLMSGS:
154 allmsgs = 1;
155 continue;
156 case NALLMSGS:
157 allmsgs = 0;
158 continue;
159
160 case CHECKSW:
161 checksw = 1;
162 continue;
163 case NCHECKSW:
164 checksw = 0;
165 continue;
166 }
167 }
168 if (*cp == '+' || *cp == '@') {
169 if (folder)
170 adios (NULL, "only one folder at a time!");
171 else
172 folder = pluspath (cp);
173 } else
174 app_msgarg(&msgs, cp);
175 }
176
177 if (!context_find ("path"))
178 free (path ("./", TFOLDER));
179 if (!msgs.size) {
180 if (allmsgs) {
181 app_msgarg(&msgs, "all");
182 } else {
183 adios (NULL, "must specify messages to sort with -noall");
184 }
185 }
186 if (!datesw)
187 datesw = "date";
188 if (!folder)
189 folder = getfolder (1);
190 maildir = m_maildir (folder);
191
192 if (chdir (maildir) == NOTOK)
193 adios (maildir, "unable to change directory to");
194
195 /* read folder and create message structure */
196 if (!(mp = folder_read (folder, 1)))
197 adios (NULL, "unable to read folder %s", folder);
198
199 /* check for empty folder */
200 if (mp->nummsg == 0)
201 adios (NULL, "no messages in %s", folder);
202
203 /* parse all the message ranges/sequences and set SELECTED */
204 for (msgnum = 0; msgnum < msgs.size; msgnum++)
205 if (!m_convert (mp, msgs.msgs[msgnum]))
206 done (1);
207 seq_setprev (mp); /* set the previous sequence */
208
209 if ((nmsgs = read_hdrs (mp, datesw)) <= 0)
210 adios (NULL, "no messages to sort");
211
212 if (checksw && check_failed) {
213 adios (NULL, "errors found, no messages sorted");
214 }
215
216 /*
217 * sort a list of pointers to our "messages to be sorted".
218 */
219 dlist = (struct smsg **) mh_xmalloc ((nmsgs+1) * sizeof(*dlist));
220 for (i = 0; i < nmsgs; i++)
221 dlist[i] = &smsgs[i];
222 dlist[nmsgs] = 0;
223
224 if (verbose) { /* announce what we're doing */
225 if (subjsort)
226 if (submajor)
227 printf ("sorting by %s\n", subjsort);
228 else
229 printf ("sorting by %s-major %s-minor\n", subjsort, datesw);
230 else
231 printf ("sorting by datefield %s\n", datesw);
232 }
233
234 /* first sort by date, or by subject-major, date-minor */
235 qsort ((char *) dlist, nmsgs, sizeof(*dlist),
236 (qsort_comp) (submajor && subjsort ? txtsort : dsort));
237
238 /*
239 * if we're sorting on subject, we need another list
240 * in subject order, then a merge pass to collate the
241 * two sorts.
242 */
243 if (!submajor && subjsort) { /* already date sorted */
244 struct smsg **slist, **flist;
245 register struct smsg ***il, **fp, **dp;
246
247 slist = (struct smsg **) mh_xmalloc ((nmsgs+1) * sizeof(*slist));
248 memcpy((char *)slist, (char *)dlist, (nmsgs+1)*sizeof(*slist));
249 qsort((char *)slist, nmsgs, sizeof(*slist), (qsort_comp) subsort);
250
251 /*
252 * make an inversion list so we can quickly find
253 * the collection of messages with the same subj
254 * given a message number.
255 */
256 il = (struct smsg ***) mh_xcalloc (mp->hghsel+1, sizeof(*il));
257 if (! il)
258 adios (NULL, "couldn't allocate msg list");
259 for (i = 0; i < nmsgs; i++)
260 il[slist[i]->s_msg] = &slist[i];
261 /*
262 * make up the final list, chronological but with
263 * all the same subjects grouped together.
264 */
265 flist = (struct smsg **) mh_xmalloc ((nmsgs+1) * sizeof(*flist));
266 fp = flist;
267 for (dp = dlist; *dp;) {
268 register struct smsg **s = il[(*dp++)->s_msg];
269
270 /* see if we already did this guy */
271 if (! s)
272 continue;
273
274 *fp++ = *s++;
275 /*
276 * take the next message(s) if there is one,
277 * its subject isn't null and its subject
278 * is the same as this one and it's not too
279 * far away in time.
280 */
281 while (*s && (*s)->s_subj[0] &&
282 strcmp((*s)->s_subj, s[-1]->s_subj) == 0 &&
283 (datelimit == 0 ||
284 (*s)->s_clock - s[-1]->s_clock <= datelimit)) {
285 il[(*s)->s_msg] = 0;
286 *fp++ = *s++;
287 }
288 }
289 *fp = 0;
290 free (il);
291 free (slist);
292 free (dlist);
293 dlist = flist;
294 }
295
296 /*
297 * At this point, dlist is a sorted array of pointers to smsg structures,
298 * each of which contains a message number.
299 */
300
301 rename_msgs (mp, dlist);
302
303 context_replace (pfolder, folder); /* update current folder */
304 seq_save (mp); /* synchronize message sequences */
305 context_save (); /* save the context file */
306 folder_free (mp); /* free folder/message structure */
307 done (0);
308 return 1;
309 }
310
311 static int
312 read_hdrs (struct msgs *mp, char *datesw)
313 {
314 int msgnum;
315 struct tws tb;
316 register struct smsg *s;
317
318 twscopy (&tb, dlocaltimenow ());
319
320 smsgs = (struct smsg *)
321 mh_xcalloc ((size_t) (mp->hghsel - mp->lowsel + 2),
322 sizeof(*smsgs));
323 if (smsgs == NULL)
324 adios (NULL, "unable to allocate sort storage");
325
326 s = smsgs;
327 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
328 if (is_selected(mp, msgnum)) {
329 if (get_fields (datesw, msgnum, s)) {
330 s->s_msg = msgnum;
331 s++;
332 }
333 }
334 }
335 s->s_msg = 0;
336 return(s - smsgs);
337 }
338
339
340 /*
341 * Parse the message and get the data or subject field,
342 * if needed.
343 */
344
345 static int
346 get_fields (char *datesw, int msg, struct smsg *smsg)
347 {
348 register int state;
349 int compnum;
350 char *msgnam, buf[BUFSIZ], nam[NAMESZ];
351 register struct tws *tw;
352 register char *datecomp = NULL, *subjcomp = NULL;
353 register FILE *in;
354 m_getfld_state_t gstate = 0;
355
356 if ((in = fopen (msgnam = m_name (msg), "r")) == NULL) {
357 admonish (msgnam, "unable to read message");
358 return (0);
359 }
360 for (compnum = 1;;) {
361 int bufsz = sizeof buf;
362 switch (state = m_getfld (&gstate, nam, buf, &bufsz, in)) {
363 case FLD:
364 case FLDPLUS:
365 compnum++;
366 if (!strcasecmp (nam, datesw)) {
367 datecomp = add (buf, datecomp);
368 while (state == FLDPLUS) {
369 bufsz = sizeof buf;
370 state = m_getfld (&gstate, nam, buf, &bufsz, in);
371 datecomp = add (buf, datecomp);
372 }
373 if (!subjsort || subjcomp)
374 break;
375 } else if (subjsort && !strcasecmp (nam, subjsort)) {
376 subjcomp = add (buf, subjcomp);
377 while (state == FLDPLUS) {
378 bufsz = sizeof buf;
379 state = m_getfld (&gstate, nam, buf, &bufsz, in);
380 subjcomp = add (buf, subjcomp);
381 }
382 if (datecomp)
383 break;
384 } else {
385 /* just flush this guy */
386 while (state == FLDPLUS) {
387 bufsz = sizeof buf;
388 state = m_getfld (&gstate, nam, buf, &bufsz, in);
389 }
390 }
391 continue;
392
393 case BODY:
394 case FILEEOF:
395 break;
396
397 case LENERR:
398 case FMTERR:
399 if (state == LENERR || state == FMTERR) {
400 admonish (NULL, "format error in message %d (header #%d)",
401 msg, compnum);
402 check_failed = 1;
403 }
404 if (datecomp)
405 free (datecomp);
406 if (subjcomp)
407 free (subjcomp);
408 fclose (in);
409 return (0);
410
411 default:
412 adios (NULL, "internal error -- you lose");
413 }
414 break;
415 }
416 m_getfld_state_destroy (&gstate);
417
418 /*
419 * If no date component, then use the modification
420 * time of the file as its date
421 */
422 if (!datecomp || (tw = dparsetime (datecomp)) == NULL) {
423 struct stat st;
424
425 advise (NULL,
426 "can't parse %s field in message %d, "
427 "will use file modification time",
428 datesw, msg);
429 fstat (fileno (in), &st);
430 smsg->s_clock = st.st_mtime;
431 check_failed = 1;
432 } else {
433 smsg->s_clock = dmktime (tw);
434 }
435
436 if (subjsort) {
437 if (subjcomp) {
438 /*
439 * try to make the subject "canonical": delete
440 * leading "re:", everything but letters & smash
441 * letters to lower case.
442 */
443 register char *cp, *cp2, c;
444
445 cp = subjcomp;
446 cp2 = subjcomp;
447 if (strcmp (subjsort, "subject") == 0) {
448 while ((c = *cp)) {
449 if (! isspace((unsigned char) c)) {
450 if(uprf(cp, "re:"))
451 cp += 2;
452 else
453 break;
454 }
455 cp++;
456 }
457 }
458
459 while ((c = *cp++)) {
460 if (isascii((unsigned char) c) && isalnum((unsigned char) c))
461 *cp2++ = isupper((unsigned char) c) ?
462 tolower((unsigned char) c) : c;
463 }
464
465 *cp2 = '\0';
466 }
467 else
468 subjcomp = "";
469
470 smsg->s_subj = subjcomp;
471 }
472 fclose (in);
473 if (datecomp)
474 free (datecomp);
475
476 return (1);
477 }
478
479 /*
480 * sort on dates.
481 */
482 static int
483 dsort (struct smsg **a, struct smsg **b)
484 {
485 if ((*a)->s_clock < (*b)->s_clock)
486 return (-1);
487 else if ((*a)->s_clock > (*b)->s_clock)
488 return (1);
489 else if ((*a)->s_msg < (*b)->s_msg)
490 return (-1);
491 else
492 return (1);
493 }
494
495 /*
496 * sort on subjects.
497 */
498 static int
499 subsort (struct smsg **a, struct smsg **b)
500 {
501 register int i;
502
503 if ((i = strcmp ((*a)->s_subj, (*b)->s_subj)))
504 return (i);
505
506 return (dsort (a, b));
507 }
508
509 static int
510 txtsort (struct smsg **a, struct smsg **b)
511 {
512 register int i;
513
514 if ((i = strcmp ((*a)->s_subj, (*b)->s_subj)))
515 return (i);
516 else if ((*a)->s_msg < (*b)->s_msg)
517 return (-1);
518 else
519 return (1);
520 }
521
522 static void
523 rename_chain (struct msgs *mp, struct smsg **mlist, int msg, int endmsg)
524 {
525 int nxt, old, new;
526 char *newname, oldname[BUFSIZ];
527 char newbuf[PATH_MAX + 1];
528
529 for (;;) {
530 nxt = mlist[msg] - smsgs; /* mlist[msg] is a ptr into smsgs */
531 mlist[msg] = (struct smsg *)0;
532 old = smsgs[nxt].s_msg;
533 new = smsgs[msg].s_msg;
534 strncpy (oldname, m_name (old), sizeof(oldname));
535 newname = m_name (new);
536 if (verbose)
537 printf ("message %d becomes message %d\n", old, new);
538
539 (void)snprintf(oldname, sizeof (oldname), "%s/%d", mp->foldpath, old);
540 (void)snprintf(newbuf, sizeof (newbuf), "%s/%d", mp->foldpath, new);
541 ext_hook("ref-hook", oldname, newbuf);
542
543 if (rename (oldname, newname) == NOTOK)
544 adios (newname, "unable to rename %s to", oldname);
545
546 copy_msg_flags (mp, new, old);
547 if (mp->curmsg == old)
548 seq_setcur (mp, new);
549
550 if (nxt == endmsg)
551 break;
552
553 msg = nxt;
554 }
555 /* if (nxt != endmsg); */
556 /* rename_chain (mp, mlist, nxt, endmsg); */
557 }
558
559 static void
560 rename_msgs (struct msgs *mp, struct smsg **mlist)
561 {
562 int i, j, old, new;
563 bvector_t tmpset = bvector_create (0);
564 char f1[BUFSIZ], tmpfil[BUFSIZ];
565 char newbuf[PATH_MAX + 1];
566 struct smsg *sp;
567
568 strncpy (tmpfil, m_name (mp->hghmsg + 1), sizeof(tmpfil));
569
570 for (i = 0; i < nmsgs; i++) {
571 if (! (sp = mlist[i]))
572 continue; /* did this one */
573
574 j = sp - smsgs;
575 if (j == i)
576 continue; /* this one doesn't move */
577
578 /*
579 * the guy that was msg j is about to become msg i.
580 * rename 'j' to make a hole, then recursively rename
581 * guys to fill up the hole.
582 */
583 old = smsgs[j].s_msg;
584 new = smsgs[i].s_msg;
585 strncpy (f1, m_name (old), sizeof(f1));
586
587 if (verbose)
588 printf ("renaming message chain from %d to %d\n", old, new);
589
590 /*
591 * Run the external hook to refile the old message as the
592 * temporary message number that is off of the end of the
593 * messages in the folder.
594 */
595
596 (void)snprintf(f1, sizeof (f1), "%s/%d", mp->foldpath, old);
597 (void)snprintf(newbuf, sizeof (newbuf), "%s/%d", mp->foldpath, mp->hghmsg + 1);
598 ext_hook("ref-hook", f1, newbuf);
599
600 if (rename (f1, tmpfil) == NOTOK)
601 adios (tmpfil, "unable to rename %s to ", f1);
602
603 get_msg_flags (mp, tmpset, old);
604
605 rename_chain (mp, mlist, j, i);
606
607 /*
608 * Run the external hook to refile the temorary message number
609 * to the real place.
610 */
611
612 (void)snprintf(f1, sizeof (f1), "%s/%d", mp->foldpath, new);
613 ext_hook("ref-hook", newbuf, f1);
614
615 if (rename (tmpfil, m_name(new)) == NOTOK)
616 adios (m_name(new), "unable to rename %s to", tmpfil);
617
618 set_msg_flags (mp, tmpset, new);
619 mp->msgflags |= SEQMOD;
620 }
621
622 bvector_free (tmpset);
623 }