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