]> diplodocus.org Git - nmh/blob - uip/sortm.c
58b30d3646c10010f79f3b7e27669f7a54362296
[nmh] / uip / sortm.c
1 /*
2 * sortm.c -- sort messages in a folder by date/time
3 *
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/tws.h>
11 #include <h/utils.h>
12
13 #define SORTM_SWITCHES \
14 X("datefield field", 0, DATESW) \
15 X("textfield field", 0, TEXTSW) \
16 X("notextfield", 0, NSUBJSW) \
17 X("subject", -3, SUBJSW) /* backward-compatibility */ \
18 X("limit days", 0, LIMSW) \
19 X("nolimit", 0, NLIMSW) \
20 X("verbose", 0, VERBSW) \
21 X("noverbose", 0, NVERBSW) \
22 X("all", 0, ALLMSGS) \
23 X("noall", 0, NALLMSGS) \
24 X("check", 0, CHECKSW) \
25 X("nocheck", 0, NCHECKSW) \
26 X("version", 0, VERSIONSW) \
27 X("help", 0, HELPSW) \
28
29 #define X(sw, minchars, id) id,
30 DEFINE_SWITCH_ENUM(SORTM);
31 #undef X
32
33 #define X(sw, minchars, id) { sw, minchars, id },
34 DEFINE_SWITCH_ARRAY(SORTM, switches);
35 #undef X
36
37 struct smsg {
38 int s_msg;
39 time_t s_clock;
40 char *s_subj;
41 };
42
43 static struct smsg *smsgs;
44 int nmsgs;
45
46 char *subjsort = (char *) 0; /* sort on subject if != 0 */
47 time_t datelimit = 0;
48 int submajor = 0; /* if true, sort on subject-major */
49 int verbose;
50 int allmsgs = 1;
51 int check_failed = 0;
52
53 /* This keeps compiler happy on calls to qsort */
54 typedef int (*qsort_comp) (const void *, const void *);
55
56 /*
57 * static prototypes
58 */
59 static int read_hdrs (struct msgs *, char *);
60 static int get_fields (char *, int, struct smsg *);
61 static int dsort (struct smsg **, struct smsg **);
62 static int subsort (struct smsg **, struct smsg **);
63 static int txtsort (struct smsg **, struct smsg **);
64 static void rename_chain (struct msgs *, struct smsg **, int, int);
65 static void rename_msgs (struct msgs *, struct smsg **);
66
67
68 int
69 main (int argc, char **argv)
70 {
71 int i, msgnum;
72 char *cp, *maildir, *datesw = NULL;
73 char *folder = NULL, buf[BUFSIZ], **argp;
74 char **arguments;
75 struct msgs_array msgs = { 0, 0, NULL };
76 struct msgs *mp;
77 struct smsg **dlist;
78 int checksw = 0;
79
80 if (nmh_init(argv[0], 1)) { return 1; }
81
82 arguments = getarguments (invo_name, argc, argv, 1);
83 argp = arguments;
84
85 /*
86 * Parse arguments
87 */
88 while ((cp = *argp++)) {
89 if (*cp == '-') {
90 switch (smatch (++cp, switches)) {
91 case AMBIGSW:
92 ambigsw (cp, switches);
93 done (1);
94 case UNKWNSW:
95 adios (NULL, "-%s unknown", cp);
96
97 case HELPSW:
98 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
99 invo_name);
100 print_help (buf, switches, 1);
101 done (0);
102 case VERSIONSW:
103 print_version(invo_name);
104 done (0);
105
106 case DATESW:
107 if (datesw)
108 adios (NULL, "only one date field at a time");
109 if (!(datesw = *argp++) || *datesw == '-')
110 adios (NULL, "missing argument to %s", argp[-2]);
111 continue;
112
113 case TEXTSW:
114 if (subjsort)
115 adios (NULL, "only one text field at a time");
116 if (!(subjsort = *argp++) || *subjsort == '-')
117 adios (NULL, "missing argument to %s", argp[-2]);
118 continue;
119
120 case SUBJSW:
121 subjsort = "subject";
122 continue;
123 case NSUBJSW:
124 subjsort = NULL;
125 continue;
126
127 case LIMSW:
128 if (!(cp = *argp++) || *cp == '-')
129 adios (NULL, "missing argument to %s", argp[-2]);
130 while (*cp == '0')
131 cp++; /* skip any leading zeros */
132 if (!*cp) { /* hit end of string */
133 submajor++; /* sort subject-major */
134 continue;
135 }
136 if (!isdigit((unsigned char) *cp) || !(datelimit = atoi(cp)))
137 adios (NULL, "impossible limit %s", cp);
138 datelimit *= 60*60*24;
139 continue;
140 case NLIMSW:
141 submajor = 0; /* use date-major, but */
142 datelimit = 0; /* use no limit */
143 continue;
144
145 case VERBSW:
146 verbose++;
147 continue;
148 case NVERBSW:
149 verbose = 0;
150 continue;
151
152 case ALLMSGS:
153 allmsgs = 1;
154 continue;
155 case NALLMSGS:
156 allmsgs = 0;
157 continue;
158
159 case CHECKSW:
160 checksw = 1;
161 continue;
162 case NCHECKSW:
163 checksw = 0;
164 continue;
165 }
166 }
167 if (*cp == '+' || *cp == '@') {
168 if (folder)
169 adios (NULL, "only one folder at a time!");
170 else
171 folder = pluspath (cp);
172 } else
173 app_msgarg(&msgs, cp);
174 }
175
176 if (!context_find ("path"))
177 free (path ("./", TFOLDER));
178 if (!msgs.size) {
179 if (allmsgs) {
180 app_msgarg(&msgs, "all");
181 } else {
182 adios (NULL, "must specify messages to sort with -noall");
183 }
184 }
185 if (!datesw)
186 datesw = "date";
187 if (!folder)
188 folder = getfolder (1);
189 maildir = m_maildir (folder);
190
191 if (chdir (maildir) == NOTOK)
192 adios (maildir, "unable to change directory to");
193
194 /* read folder and create message structure */
195 if (!(mp = folder_read (folder, 1)))
196 adios (NULL, "unable to read folder %s", folder);
197
198 /* check for empty folder */
199 if (mp->nummsg == 0)
200 adios (NULL, "no messages in %s", folder);
201
202 /* parse all the message ranges/sequences and set SELECTED */
203 for (msgnum = 0; msgnum < msgs.size; msgnum++)
204 if (!m_convert (mp, msgs.msgs[msgnum]))
205 done (1);
206 seq_setprev (mp); /* set the previous sequence */
207
208 if ((nmsgs = read_hdrs (mp, datesw)) <= 0)
209 adios (NULL, "no messages to sort");
210
211 if (checksw && check_failed) {
212 adios (NULL, "errors found, no messages sorted");
213 }
214
215 /*
216 * sort a list of pointers to our "messages to be sorted".
217 */
218 dlist = (struct smsg **) mh_xmalloc ((nmsgs+1) * sizeof(*dlist));
219 for (i = 0; i < nmsgs; i++)
220 dlist[i] = &smsgs[i];
221 dlist[nmsgs] = 0;
222
223 if (verbose) { /* announce what we're doing */
224 if (subjsort)
225 if (submajor)
226 printf ("sorting by %s\n", subjsort);
227 else
228 printf ("sorting by %s-major %s-minor\n", subjsort, datesw);
229 else
230 printf ("sorting by datefield %s\n", datesw);
231 }
232
233 /* first sort by date, or by subject-major, date-minor */
234 qsort ((char *) dlist, nmsgs, sizeof(*dlist),
235 (qsort_comp) (submajor && subjsort ? txtsort : dsort));
236
237 /*
238 * if we're sorting on subject, we need another list
239 * in subject order, then a merge pass to collate the
240 * two sorts.
241 */
242 if (!submajor && subjsort) { /* already date sorted */
243 struct smsg **slist, **flist;
244 struct smsg ***il, **fp, **dp;
245
246 slist = (struct smsg **) mh_xmalloc ((nmsgs+1) * sizeof(*slist));
247 memcpy((char *)slist, (char *)dlist, (nmsgs+1)*sizeof(*slist));
248 qsort((char *)slist, nmsgs, sizeof(*slist), (qsort_comp) subsort);
249
250 /*
251 * make an inversion list so we can quickly find
252 * the collection of messages with the same subj
253 * given a message number.
254 */
255 il = mh_xcalloc(mp->hghsel + 1, sizeof *il);
256 for (i = 0; i < nmsgs; i++)
257 il[slist[i]->s_msg] = &slist[i];
258 /*
259 * make up the final list, chronological but with
260 * all the same subjects grouped together.
261 */
262 flist = (struct smsg **) mh_xmalloc ((nmsgs+1) * sizeof(*flist));
263 fp = flist;
264 for (dp = dlist; *dp;) {
265 struct smsg **s = il[(*dp++)->s_msg];
266
267 /* see if we already did this guy */
268 if (! s)
269 continue;
270
271 *fp++ = *s++;
272 /*
273 * take the next message(s) if there is one,
274 * its subject isn't null and its subject
275 * is the same as this one and it's not too
276 * far away in time.
277 */
278 while (*s && (*s)->s_subj[0] &&
279 strcmp((*s)->s_subj, s[-1]->s_subj) == 0 &&
280 (datelimit == 0 ||
281 (*s)->s_clock - s[-1]->s_clock <= datelimit)) {
282 il[(*s)->s_msg] = 0;
283 *fp++ = *s++;
284 }
285 }
286 *fp = 0;
287 free (il);
288 free (slist);
289 free (dlist);
290 dlist = flist;
291 }
292
293 /*
294 * At this point, dlist is a sorted array of pointers to smsg structures,
295 * each of which contains a message number.
296 */
297
298 rename_msgs (mp, dlist);
299
300 context_replace (pfolder, folder); /* update current folder */
301 seq_save (mp); /* synchronize message sequences */
302 context_save (); /* save the context file */
303 folder_free (mp); /* free folder/message structure */
304 done (0);
305 return 1;
306 }
307
308 static int
309 read_hdrs (struct msgs *mp, char *datesw)
310 {
311 int msgnum;
312 struct tws tb;
313 struct smsg *s;
314
315 twscopy (&tb, dlocaltimenow ());
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[BUFSIZ], nam[NAMESZ];
343 struct tws *tw;
344 char *datecomp = NULL, *subjcomp = NULL;
345 FILE *in;
346 m_getfld_state_t gstate = 0;
347
348 if ((in = fopen (msgnam = m_name (msg), "r")) == NULL) {
349 admonish (msgnam, "unable to read message");
350 return (0);
351 }
352 for (compnum = 1;;) {
353 int bufsz = sizeof buf;
354 switch (state = m_getfld (&gstate, nam, buf, &bufsz, in)) {
355 case FLD:
356 case FLDPLUS:
357 compnum++;
358 if (!strcasecmp (nam, datesw)) {
359 datecomp = add (buf, datecomp);
360 while (state == FLDPLUS) {
361 bufsz = sizeof buf;
362 state = m_getfld (&gstate, nam, buf, &bufsz, in);
363 datecomp = add (buf, datecomp);
364 }
365 if (!subjsort || subjcomp)
366 break;
367 } else if (subjsort && !strcasecmp (nam, subjsort)) {
368 subjcomp = add (buf, subjcomp);
369 while (state == FLDPLUS) {
370 bufsz = sizeof buf;
371 state = m_getfld (&gstate, nam, buf, &bufsz, in);
372 subjcomp = add (buf, subjcomp);
373 }
374 if (datecomp)
375 break;
376 } else {
377 /* just flush this guy */
378 while (state == FLDPLUS) {
379 bufsz = sizeof buf;
380 state = m_getfld (&gstate, nam, buf, &bufsz, in);
381 }
382 }
383 continue;
384
385 case BODY:
386 case FILEEOF:
387 break;
388
389 case LENERR:
390 case FMTERR:
391 if (state == LENERR || state == FMTERR) {
392 inform("format error in message %d (header #%d), continuing...",
393 msg, compnum);
394 check_failed = 1;
395 }
396 mh_xfree(datecomp);
397 mh_xfree(subjcomp);
398 fclose (in);
399 return (0);
400
401 default:
402 adios (NULL, "internal error -- you lose");
403 }
404 break;
405 }
406 m_getfld_state_destroy (&gstate);
407
408 /*
409 * If no date component, then use the modification
410 * time of the file as its date
411 */
412 if (!datecomp || (tw = dparsetime (datecomp)) == NULL) {
413 struct stat st;
414
415 inform("can't parse %s field in message %d, "
416 "will use file modification time", datesw, msg);
417 fstat (fileno (in), &st);
418 smsg->s_clock = st.st_mtime;
419 check_failed = 1;
420 } else {
421 smsg->s_clock = dmktime (tw);
422 }
423
424 if (subjsort) {
425 if (subjcomp) {
426 /*
427 * try to make the subject "canonical": delete
428 * leading "re:", everything but letters & smash
429 * letters to lower case.
430 */
431 char *cp, *cp2, c;
432
433 cp = subjcomp;
434 cp2 = subjcomp;
435 if (strcmp (subjsort, "subject") == 0) {
436 while ((c = *cp)) {
437 if (! isspace((unsigned char) c)) {
438 if(uprf(cp, "re:"))
439 cp += 2;
440 else
441 break;
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 mh_xfree(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 (0);
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 temorary 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 }