2 ** sortm.c -- sort messages in a folder by date/time
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.
18 #ifdef HAVE_SYS_PARAM_H
19 # include <sys/param.h>
22 static struct swit switches[] = {
24 { "datefield field", 0 },
26 { "textfield field", 0 },
44 char *version=VERSION;
52 static struct smsg *smsgs;
55 char *subjsort = NULL; /* sort on subject if != 0 */
57 int submajor = 0; /* if true, sort on subject-major */
60 /* This keeps compiler happy on calls to qsort */
61 typedef int (*qsort_comp) (const void *, const void *);
66 static int read_hdrs(struct msgs *, char *);
67 static int get_fields(char *, int, struct smsg *);
68 static int dsort(struct smsg **, struct smsg **);
69 static int subsort(struct smsg **, struct smsg **);
70 static int txtsort(struct smsg **, struct smsg **);
71 static void rename_chain(struct msgs *, struct smsg **, int, int);
72 static void rename_msgs(struct msgs *, struct smsg **);
76 main(int argc, char **argv)
80 char *maildir, *datesw = NULL;
81 char *folder = NULL, buf[BUFSIZ], **argp;
83 struct msgs_array msgs = { 0, 0, NULL };
87 setlocale(LC_ALL, "");
88 invo_name = mhbasename(argv[0]);
90 /* read user profile/context */
93 arguments = getarguments(invo_name, argc, argv, 1);
99 while ((cp = *argp++)) {
101 switch (smatch(++cp, switches)) {
103 ambigsw(cp, switches);
106 adios(EX_USAGE, NULL, "-%s unknown", cp);
109 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
110 print_help(buf, switches, 1);
111 exit(argc == 2 ? EX_OK : EX_USAGE);
113 print_version(invo_name);
114 exit(argc == 2 ? EX_OK : EX_USAGE);
118 adios(EX_USAGE, NULL, "only one date field at a time");
119 if (!(datesw = *argp++) || *datesw == '-')
120 adios(EX_USAGE, NULL, "missing argument to %s",
126 adios(EX_USAGE, NULL, "only one text field at a time");
127 if (!(subjsort = *argp++) || *subjsort == '-')
128 adios(EX_USAGE, NULL, "missing argument to %s",
137 if (!(cp = *argp++) || *cp == '-')
138 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
140 cp++; /* skip any leading zeros */
141 if (!*cp) { /* hit end of string */
142 submajor++; /* sort subject-major */
145 if (!isdigit(*cp) || !(datelimit = atoi(cp)))
146 adios(EX_USAGE, NULL, "impossible limit %s", cp);
147 datelimit *= 60*60*24;
150 submajor = 0; /* use date-major, but */
151 datelimit = 0; /* use no limit */
162 if (*cp == '+' || *cp == '@') {
164 adios(EX_USAGE, NULL, "only one folder at a time!");
166 folder = mh_xstrdup(expandfol(cp));
168 app_msgarg(&msgs, cp);
172 app_msgarg(&msgs, seq_all);
176 folder = getcurfol();
177 maildir = toabsdir(folder);
179 if (chdir(maildir) == NOTOK)
180 adios(EX_OSERR, maildir, "unable to change directory to");
182 /* read folder and create message structure */
183 if (!(mp = folder_read(folder)))
184 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
186 /* check for empty folder */
188 adios(EX_DATAERR, NULL, "no messages in %s", folder);
190 /* parse all the message ranges/sequences and set SELECTED */
191 for (msgnum = 0; msgnum < msgs.size; msgnum++)
192 if (!m_convert(mp, msgs.msgs[msgnum]))
194 seq_setprev(mp); /* set the previous sequence */
196 if ((nmsgs = read_hdrs(mp, datesw)) <= 0)
197 adios(EX_DATAERR, NULL, "no messages to sort");
200 ** sort a list of pointers to our "messages to be sorted".
202 dlist = mh_xcalloc(nmsgs+1, sizeof(*dlist));
203 for (i = 0; i < nmsgs; i++)
204 dlist[i] = &smsgs[i];
207 if (verbose) { /* announce what we're doing */
209 printf("sorting by %s-major %s-minor\n",
210 submajor ? subjsort : datesw,
211 submajor ? datesw : subjsort);
213 printf("sorting by datefield %s\n", datesw);
216 /* first sort by date, or by subject-major, date-minor */
217 qsort((char *) dlist, nmsgs, sizeof(*dlist),
218 (qsort_comp) (submajor && subjsort ? txtsort : dsort));
221 ** if we're sorting on subject, we need another list
222 ** in subject order, then a merge pass to collate the
225 if (!submajor && subjsort) { /* already date sorted */
226 struct smsg **slist, **flist;
227 struct smsg ***il, **fp, **dp;
229 slist = mh_xcalloc(nmsgs+1, sizeof(*slist));
230 memcpy((char *)slist, (char *)dlist, (nmsgs+1)*sizeof(*slist));
231 qsort((char *)slist, nmsgs, sizeof(*slist),
232 (qsort_comp) subsort);
235 ** make an inversion list so we can quickly find
236 ** the collection of messages with the same subj
237 ** given a message number.
239 il = mh_xcalloc(mp->hghsel+1, sizeof(*il));
241 adios(EX_OSERR, NULL, "couldn't allocate msg list");
242 for (i = 0; i < nmsgs; i++)
243 il[slist[i]->s_msg] = &slist[i];
245 ** make up the final list, chronological but with
246 ** all the same subjects grouped together.
248 flist = mh_xcalloc(nmsgs+1, sizeof(*flist));
250 for (dp = dlist; *dp;) {
251 struct smsg **s = il[(*dp++)->s_msg];
253 /* see if we already did this guy */
259 ** take the next message(s) if there is one,
260 ** its subject isn't null and its subject
261 ** is the same as this one and it's not too
264 while (*s && (*s)->s_subj[0] && strcmp((*s)->s_subj, s[-1]->s_subj) == 0 && (datelimit == 0 || (*s)->s_clock - s[-1]->s_clock <= datelimit)) {
276 ** At this point, dlist is a sorted array of pointers to smsg
277 ** structures, each of which contains a message number.
280 rename_msgs(mp, dlist);
282 context_replace(curfolder, folder); /* update current folder */
283 seq_save(mp); /* synchronize message sequences */
284 context_save(); /* save the context file */
285 folder_free(mp); /* free folder/message structure */
290 read_hdrs(struct msgs *mp, char *datesw)
296 twscopy(&tb, dlocaltimenow());
298 smsgs = mh_xcalloc(mp->hghsel - mp->lowsel + 2, sizeof(*smsgs));
301 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
302 if (is_selected(mp, msgnum)) {
303 if (get_fields(datesw, msgnum, s)) {
315 ** Parse the message and get the data or subject field,
320 get_fields(char *datesw, int msg, struct smsg *smsg)
323 struct field f = {{0}};
327 char *datecomp = NULL, *subjcomp = NULL;
330 if ((in = fopen(msgnam = m_name(msg), "r")) == NULL) {
331 admonish(msgnam, "unable to read message");
334 for (compnum = 1, state = FLD2;; compnum++) {
335 switch (state = m_getfld2(state, &f, in)) {
340 if (mh_strcasecmp(f.name, datesw)==0) {
341 datecomp = mh_xstrdup(f.value);
342 if (!subjsort || subjcomp) {
345 } else if (subjsort && mh_strcasecmp(f.name,
347 subjcomp = mh_xstrdup(f.value);
359 admonish(NULL, "format error in message %d (header #%d)", msg, compnum);
364 admonish(NULL, "format error in message %d (header #%d)", msg, compnum);
375 adios(EX_SOFTWARE, NULL, "internal error -- you lose");
381 ** If no date component, then use the modification
382 ** time of the file as its date
384 if (!datecomp || (tw = dparsetime(datecomp)) == NULL) {
387 admonish(NULL, "can't parse %s field in message %d",
389 fstat(fileno(in), &st);
390 smsg->s_clock = st.st_mtime;
392 smsg->s_clock = dmktime(tw);
398 ** try to make the subject "canonical": delete
399 ** leading "re:", everything but letters & smash
400 ** letters to lower case.
407 if (strcmp(subjsort, "subject") == 0) {
419 while ((c = *cp++)) {
421 *cp2++ = isupper(c) ? tolower(c) : c;
428 smsg->s_subj = subjcomp;
441 dsort(struct smsg **a, struct smsg **b)
443 if ((*a)->s_clock < (*b)->s_clock)
445 else if ((*a)->s_clock > (*b)->s_clock)
447 else if ((*a)->s_msg < (*b)->s_msg)
457 subsort(struct smsg **a, struct smsg **b)
461 if ((i = strcmp((*a)->s_subj, (*b)->s_subj)))
464 return (dsort(a, b));
468 txtsort(struct smsg **a, struct smsg **b)
472 if ((i = strcmp((*a)->s_subj, (*b)->s_subj)))
474 else if ((*a)->s_msg < (*b)->s_msg)
481 rename_chain(struct msgs *mp, struct smsg **mlist, int msg, int endmsg)
484 char *newname, oldname[BUFSIZ];
485 char newbuf[MAXPATHLEN + 1];
488 nxt = mlist[msg] - smsgs; /* mlist[msg] is a ptr into smsgs */
489 mlist[msg] = (struct smsg *)0;
490 old = smsgs[nxt].s_msg;
491 new = smsgs[msg].s_msg;
492 strncpy(oldname, m_name(old), sizeof(oldname));
493 newname = m_name(new);
495 printf("message %d becomes message %d\n", old, new);
497 snprintf(oldname, sizeof (oldname), "%s/%d",
499 snprintf(newbuf, sizeof (newbuf), "%s/%d", mp->foldpath, new);
500 ext_hook("ref-hook", oldname, newbuf);
502 if (rename(oldname, newname) == NOTOK)
503 adios(EX_IOERR, newname, "unable to rename %s to", oldname);
505 copy_msg_flags(mp, new, old);
506 if (mp->curmsg == old)
514 /* if (nxt != endmsg); */
515 /* rename_chain(mp, mlist, nxt, endmsg); */
519 rename_msgs(struct msgs *mp, struct smsg **mlist)
523 char f1[BUFSIZ], tmpfil[BUFSIZ];
524 char newbuf[MAXPATHLEN + 1];
527 strncpy(tmpfil, m_name(mp->hghmsg + 1), sizeof(tmpfil));
529 for (i = 0; i < nmsgs; i++) {
530 if (! (sp = mlist[i]))
531 continue; /* did this one */
535 continue; /* this one doesn't move */
538 ** the guy that was msg j is about to become msg i.
539 ** rename 'j' to make a hole, then recursively rename
540 ** guys to fill up the hole.
542 old = smsgs[j].s_msg;
543 new = smsgs[i].s_msg;
544 strncpy(f1, m_name(old), sizeof(f1));
547 printf("renaming message chain from %d to %d\n",
551 ** Run the external hook to refile the old message as the
552 ** temporary message number that is off of the end of the
553 ** messages in the folder.
556 snprintf(f1, sizeof (f1), "%s/%d", mp->foldpath, old);
557 snprintf(newbuf, sizeof (newbuf), "%s/%d",
558 mp->foldpath, mp->hghmsg + 1);
559 ext_hook("ref-hook", f1, newbuf);
561 if (rename(f1, tmpfil) == NOTOK)
562 adios(EX_IOERR, tmpfil, "unable to rename %s to ", f1);
564 get_msg_flags(mp, &tmpset, old);
566 rename_chain(mp, mlist, j, i);
569 ** Run the external hook to refile the temorary message number
570 ** to the real place.
573 snprintf(f1, sizeof (f1), "%s/%d", mp->foldpath, new);
574 ext_hook("ref-hook", newbuf, f1);
576 if (rename(tmpfil, m_name(new)) == NOTOK)
577 adios(EX_IOERR, m_name(new), "unable to rename %s to", tmpfil);
579 set_msg_flags(mp, &tmpset, new);
580 mp->msgflags |= SEQMOD;