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