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