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