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