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]))
195 if ((nmsgs = read_hdrs(mp, datesw)) <= 0)
196 adios(EX_DATAERR, NULL, "no messages to sort");
199 ** sort a list of pointers to our "messages to be sorted".
201 dlist = mh_xcalloc(nmsgs+1, sizeof(*dlist));
202 for (i = 0; i < nmsgs; i++)
203 dlist[i] = &smsgs[i];
206 if (verbose) { /* announce what we're doing */
208 printf("sorting by %s-major %s-minor\n",
209 submajor ? subjsort : datesw,
210 submajor ? datesw : subjsort);
212 printf("sorting by datefield %s\n", datesw);
215 /* first sort by date, or by subject-major, date-minor */
216 qsort((char *) dlist, nmsgs, sizeof(*dlist),
217 (qsort_comp) (submajor && subjsort ? txtsort : dsort));
220 ** if we're sorting on subject, we need another list
221 ** in subject order, then a merge pass to collate the
224 if (!submajor && subjsort) { /* already date sorted */
225 struct smsg **slist, **flist;
226 struct smsg ***il, **fp, **dp;
228 slist = mh_xcalloc(nmsgs+1, sizeof(*slist));
229 memcpy((char *)slist, (char *)dlist, (nmsgs+1)*sizeof(*slist));
230 qsort((char *)slist, nmsgs, sizeof(*slist),
231 (qsort_comp) subsort);
234 ** make an inversion list so we can quickly find
235 ** the collection of messages with the same subj
236 ** given a message number.
238 il = mh_xcalloc(mp->hghsel+1, sizeof(*il));
240 adios(EX_OSERR, NULL, "couldn't allocate msg list");
241 for (i = 0; i < nmsgs; i++)
242 il[slist[i]->s_msg] = &slist[i];
244 ** make up the final list, chronological but with
245 ** all the same subjects grouped together.
247 flist = mh_xcalloc(nmsgs+1, sizeof(*flist));
249 for (dp = dlist; *dp;) {
250 struct smsg **s = il[(*dp++)->s_msg];
252 /* see if we already did this guy */
258 ** take the next message(s) if there is one,
259 ** its subject isn't null and its subject
260 ** is the same as this one and it's not too
263 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)) {
275 ** At this point, dlist is a sorted array of pointers to smsg
276 ** structures, each of which contains a message number.
279 rename_msgs(mp, dlist);
281 context_replace(curfolder, folder); /* update current folder */
282 seq_save(mp); /* synchronize message sequences */
283 context_save(); /* save the context file */
284 folder_free(mp); /* free folder/message structure */
289 read_hdrs(struct msgs *mp, char *datesw)
295 twscopy(&tb, dlocaltimenow());
297 smsgs = mh_xcalloc(mp->hghsel - mp->lowsel + 2, sizeof(*smsgs));
300 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
301 if (is_selected(mp, msgnum)) {
302 if (get_fields(datesw, msgnum, s)) {
314 ** Parse the message and get the data or subject field,
319 get_fields(char *datesw, int msg, struct smsg *smsg)
322 struct field f = {{0}};
326 char *datecomp = NULL, *subjcomp = NULL;
329 if ((in = fopen(msgnam = m_name(msg), "r")) == NULL) {
330 admonish(msgnam, "unable to read message");
333 for (compnum = 1, state = FLD2;; compnum++) {
334 switch (state = m_getfld2(state, &f, in)) {
339 if (mh_strcasecmp(f.name, datesw)==0) {
340 datecomp = mh_xstrdup(f.value);
341 if (!subjsort || subjcomp) {
344 } else if (subjsort && mh_strcasecmp(f.name,
346 subjcomp = mh_xstrdup(f.value);
358 admonish(NULL, "format error in message %d (header #%d)", msg, compnum);
363 admonish(NULL, "format error in message %d (header #%d)", msg, compnum);
374 adios(EX_SOFTWARE, NULL, "internal error -- you lose");
380 ** If no date component, then use the modification
381 ** time of the file as its date
383 if (!datecomp || (tw = dparsetime(datecomp)) == NULL) {
386 admonish(NULL, "can't parse %s field in message %d",
388 fstat(fileno(in), &st);
389 smsg->s_clock = st.st_mtime;
391 smsg->s_clock = dmktime(tw);
397 ** try to make the subject "canonical": delete
398 ** leading "re:", everything but letters & smash
399 ** letters to lower case.
406 if (strcmp(subjsort, "subject") == 0) {
418 while ((c = *cp++)) {
420 *cp2++ = isupper(c) ? tolower(c) : c;
427 smsg->s_subj = subjcomp;
440 dsort(struct smsg **a, struct smsg **b)
442 if ((*a)->s_clock < (*b)->s_clock)
444 else if ((*a)->s_clock > (*b)->s_clock)
446 else if ((*a)->s_msg < (*b)->s_msg)
456 subsort(struct smsg **a, struct smsg **b)
460 if ((i = strcmp((*a)->s_subj, (*b)->s_subj)))
463 return (dsort(a, b));
467 txtsort(struct smsg **a, struct smsg **b)
471 if ((i = strcmp((*a)->s_subj, (*b)->s_subj)))
473 else if ((*a)->s_msg < (*b)->s_msg)
480 rename_chain(struct msgs *mp, struct smsg **mlist, int msg, int endmsg)
483 char *newname, oldname[BUFSIZ];
484 char newbuf[MAXPATHLEN + 1];
487 nxt = mlist[msg] - smsgs; /* mlist[msg] is a ptr into smsgs */
488 mlist[msg] = (struct smsg *)0;
489 old = smsgs[nxt].s_msg;
490 new = smsgs[msg].s_msg;
491 strncpy(oldname, m_name(old), sizeof(oldname));
492 newname = m_name(new);
494 printf("message %d becomes message %d\n", old, new);
496 snprintf(oldname, sizeof (oldname), "%s/%d",
498 snprintf(newbuf, sizeof (newbuf), "%s/%d", mp->foldpath, new);
499 ext_hook("ref-hook", oldname, newbuf);
501 if (rename(oldname, newname) == NOTOK)
502 adios(EX_IOERR, newname, "unable to rename %s to", oldname);
504 copy_msg_flags(mp, new, old);
505 if (mp->curmsg == old)
513 /* if (nxt != endmsg); */
514 /* rename_chain(mp, mlist, nxt, endmsg); */
518 rename_msgs(struct msgs *mp, struct smsg **mlist)
522 char f1[BUFSIZ], tmpfil[BUFSIZ];
523 char newbuf[MAXPATHLEN + 1];
526 strncpy(tmpfil, m_name(mp->hghmsg + 1), sizeof(tmpfil));
528 for (i = 0; i < nmsgs; i++) {
529 if (! (sp = mlist[i]))
530 continue; /* did this one */
534 continue; /* this one doesn't move */
537 ** the guy that was msg j is about to become msg i.
538 ** rename 'j' to make a hole, then recursively rename
539 ** guys to fill up the hole.
541 old = smsgs[j].s_msg;
542 new = smsgs[i].s_msg;
543 strncpy(f1, m_name(old), sizeof(f1));
546 printf("renaming message chain from %d to %d\n",
550 ** Run the external hook to refile the old message as the
551 ** temporary message number that is off of the end of the
552 ** messages in the folder.
555 snprintf(f1, sizeof (f1), "%s/%d", mp->foldpath, old);
556 snprintf(newbuf, sizeof (newbuf), "%s/%d",
557 mp->foldpath, mp->hghmsg + 1);
558 ext_hook("ref-hook", f1, newbuf);
560 if (rename(f1, tmpfil) == NOTOK)
561 adios(EX_IOERR, tmpfil, "unable to rename %s to ", f1);
563 get_msg_flags(mp, &tmpset, old);
565 rename_chain(mp, mlist, j, i);
568 ** Run the external hook to refile the temorary message number
569 ** to the real place.
572 snprintf(f1, sizeof (f1), "%s/%d", mp->foldpath, new);
573 ext_hook("ref-hook", newbuf, f1);
575 if (rename(tmpfil, m_name(new)) == NOTOK)
576 adios(EX_IOERR, m_name(new), "unable to rename %s to", tmpfil);
578 set_msg_flags(mp, &tmpset, new);
579 mp->msgflags |= SEQMOD;