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