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