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.
13 static struct swit switches[] = {
15 { "datefield field", 0 },
17 { "textfield field", 0 },
41 static struct smsg *smsgs;
44 char *subjsort = NULL; /* sort on subject if != 0 */
46 int submajor = 0; /* if true, sort on subject-major */
49 /* This keeps compiler happy on calls to qsort */
50 typedef int (*qsort_comp) (const void *, const void *);
55 static int read_hdrs(struct msgs *, char *);
56 static int get_fields(char *, int, struct smsg *);
57 static int dsort(struct smsg **, struct smsg **);
58 static int subsort(struct smsg **, struct smsg **);
59 static int txtsort(struct smsg **, struct smsg **);
60 static void rename_chain(struct msgs *, struct smsg **, int, int);
61 static void rename_msgs(struct msgs *, struct smsg **);
65 main(int argc, char **argv)
69 char *maildir, *datesw = NULL;
70 char *folder = NULL, buf[BUFSIZ], **argp;
72 struct msgs_array msgs = { 0, 0, NULL };
77 setlocale(LC_ALL, "");
79 invo_name = mhbasename(argv[0]);
81 /* read user profile/context */
84 arguments = getarguments(invo_name, argc, argv, 1);
90 while ((cp = *argp++)) {
92 switch (smatch(++cp, switches)) {
94 ambigsw(cp, switches);
97 adios(NULL, "-%s unknown", cp);
100 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
101 print_help(buf, switches, 1);
104 print_version(invo_name);
109 adios(NULL, "only one date field at a time");
110 if (!(datesw = *argp++) || *datesw == '-')
111 adios(NULL, "missing argument to %s",
117 adios(NULL, "only one text field at a time");
118 if (!(subjsort = *argp++) || *subjsort == '-')
119 adios(NULL, "missing argument to %s",
128 if (!(cp = *argp++) || *cp == '-')
129 adios(NULL, "missing argument to %s", argp[-2]);
131 cp++; /* skip any leading zeros */
132 if (!*cp) { /* hit end of string */
133 submajor++; /* sort subject-major */
136 if (!isdigit(*cp) || !(datelimit = atoi(cp)))
137 adios(NULL, "impossible limit %s", cp);
138 datelimit *= 60*60*24;
141 submajor = 0; /* use date-major, but */
142 datelimit = 0; /* use no limit */
153 if (*cp == '+' || *cp == '@') {
155 adios(NULL, "only one folder at a time!");
157 folder = getcpy(expandfol(cp));
159 app_msgarg(&msgs, cp);
163 app_msgarg(&msgs, seq_all);
167 folder = getcurfol();
168 maildir = toabsdir(folder);
170 if (chdir(maildir) == NOTOK)
171 adios(maildir, "unable to change directory to");
173 /* read folder and create message structure */
174 if (!(mp = folder_read(folder)))
175 adios(NULL, "unable to read folder %s", folder);
177 /* check for empty folder */
179 adios(NULL, "no messages in %s", folder);
181 /* parse all the message ranges/sequences and set SELECTED */
182 for (msgnum = 0; msgnum < msgs.size; msgnum++)
183 if (!m_convert(mp, msgs.msgs[msgnum]))
185 seq_setprev(mp); /* set the previous sequence */
187 if ((nmsgs = read_hdrs(mp, datesw)) <= 0)
188 adios(NULL, "no messages to sort");
191 ** sort a list of pointers to our "messages to be sorted".
193 dlist = (struct smsg **) mh_xmalloc((nmsgs+1) * sizeof(*dlist));
194 for (i = 0; i < nmsgs; i++)
195 dlist[i] = &smsgs[i];
198 if (verbose) { /* announce what we're doing */
200 printf("sorting by %s-major %s-minor\n",
201 submajor ? subjsort : datesw,
202 submajor ? datesw : subjsort);
204 printf("sorting by datefield %s\n", datesw);
207 /* first sort by date, or by subject-major, date-minor */
208 qsort((char *) dlist, nmsgs, sizeof(*dlist),
209 (qsort_comp) (submajor && subjsort ? txtsort : dsort));
212 ** if we're sorting on subject, we need another list
213 ** in subject order, then a merge pass to collate the
216 if (!submajor && subjsort) { /* already date sorted */
217 struct smsg **slist, **flist;
218 register struct smsg ***il, **fp, **dp;
220 slist = (struct smsg **)
221 mh_xmalloc((nmsgs+1) * sizeof(*slist));
222 memcpy((char *)slist, (char *)dlist, (nmsgs+1)*sizeof(*slist));
223 qsort((char *)slist, nmsgs, sizeof(*slist),
224 (qsort_comp) subsort);
227 ** make an inversion list so we can quickly find
228 ** the collection of messages with the same subj
229 ** given a message number.
231 il = (struct smsg ***) calloc(mp->hghsel+1, sizeof(*il));
233 adios(NULL, "couldn't allocate msg list");
234 for (i = 0; i < nmsgs; i++)
235 il[slist[i]->s_msg] = &slist[i];
237 ** make up the final list, chronological but with
238 ** all the same subjects grouped together.
240 flist = (struct smsg **)
241 mh_xmalloc((nmsgs+1) * sizeof(*flist));
243 for (dp = dlist; *dp;) {
244 register struct smsg **s = il[(*dp++)->s_msg];
246 /* see if we already did this guy */
252 ** take the next message(s) if there is one,
253 ** its subject isn't null and its subject
254 ** is the same as this one and it's not too
257 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)) {
269 ** At this point, dlist is a sorted array of pointers to smsg
270 ** structures, each of which contains a message number.
273 rename_msgs(mp, dlist);
275 context_replace(curfolder, folder); /* update current folder */
276 seq_save(mp); /* synchronize message sequences */
277 context_save(); /* save the context file */
278 folder_free(mp); /* free folder/message structure */
284 read_hdrs(struct msgs *mp, char *datesw)
288 register struct smsg *s;
290 twscopy(&tb, dlocaltimenow());
292 smsgs = (struct smsg *) calloc((size_t) (mp->hghsel - mp->lowsel + 2),
295 adios(NULL, "unable to allocate sort storage");
298 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
299 if (is_selected(mp, msgnum)) {
300 if (get_fields(datesw, msgnum, s)) {
312 ** Parse the message and get the data or subject field,
317 get_fields(char *datesw, int msg, struct smsg *smsg)
321 char *msgnam, buf[BUFSIZ], nam[NAMESZ];
322 register struct tws *tw;
323 register char *datecomp = NULL, *subjcomp = NULL;
326 if ((in = fopen(msgnam = m_name(msg), "r")) == NULL) {
327 admonish(msgnam, "unable to read message");
330 for (compnum = 1, state = FLD;;) {
331 switch (state = m_getfld(state, nam, buf, sizeof(buf), in)) {
336 if (!mh_strcasecmp(nam, datesw)) {
337 datecomp = add(buf, datecomp);
338 while (state == FLDPLUS) {
339 state = m_getfld(state, nam, buf,
341 datecomp = add(buf, datecomp);
343 if (!subjsort || subjcomp)
345 } else if (subjsort && !mh_strcasecmp(nam, subjsort)) {
346 subjcomp = add(buf, subjcomp);
347 while (state == FLDPLUS) {
348 state = m_getfld(state, nam, buf,
350 subjcomp = add(buf, subjcomp);
355 /* just flush this guy */
356 while (state == FLDPLUS)
357 state = m_getfld(state, nam, buf,
369 if (state == LENERR || state == FMTERR)
370 admonish(NULL, "format error in message %d (header #%d)", msg, compnum);
379 adios(NULL, "internal error -- you lose");
385 ** If no date component, then use the modification
386 ** time of the file as its date
388 if (!datecomp || (tw = dparsetime(datecomp)) == NULL) {
391 admonish(NULL, "can't parse %s field in message %d",
393 fstat(fileno(in), &st);
394 smsg->s_clock = st.st_mtime;
396 smsg->s_clock = dmktime(tw);
402 ** try to make the subject "canonical": delete
403 ** leading "re:", everything but letters & smash
404 ** letters to lower case.
406 register char *cp, *cp2;
407 register unsigned char c;
411 if (strcmp(subjsort, "subject") == 0) {
423 while ((c = *cp++)) {
425 *cp2++ = isupper(c) ? tolower(c) : c;
432 smsg->s_subj = subjcomp;
445 dsort(struct smsg **a, struct smsg **b)
447 if ((*a)->s_clock < (*b)->s_clock)
449 else if ((*a)->s_clock > (*b)->s_clock)
451 else if ((*a)->s_msg < (*b)->s_msg)
461 subsort(struct smsg **a, struct smsg **b)
465 if ((i = strcmp((*a)->s_subj, (*b)->s_subj)))
468 return (dsort(a, b));
472 txtsort(struct smsg **a, struct smsg **b)
476 if ((i = strcmp((*a)->s_subj, (*b)->s_subj)))
478 else if ((*a)->s_msg < (*b)->s_msg)
485 rename_chain(struct msgs *mp, struct smsg **mlist, int msg, int endmsg)
488 char *newname, oldname[BUFSIZ];
489 char newbuf[MAXPATHLEN + 1];
492 nxt = mlist[msg] - smsgs; /* mlist[msg] is a ptr into smsgs */
493 mlist[msg] = (struct smsg *)0;
494 old = smsgs[nxt].s_msg;
495 new = smsgs[msg].s_msg;
496 strncpy(oldname, m_name(old), sizeof(oldname));
497 newname = m_name(new);
499 printf("message %d becomes message %d\n", old, new);
501 snprintf(oldname, sizeof (oldname), "%s/%d",
503 snprintf(newbuf, sizeof (newbuf), "%s/%d", mp->foldpath, new);
504 ext_hook("ref-hook", oldname, newbuf);
506 if (rename(oldname, newname) == NOTOK)
507 adios(newname, "unable to rename %s to", oldname);
509 copy_msg_flags(mp, new, old);
510 if (mp->curmsg == old)
518 /* if (nxt != endmsg); */
519 /* rename_chain(mp, mlist, nxt, endmsg); */
523 rename_msgs(struct msgs *mp, struct smsg **mlist)
527 char f1[BUFSIZ], tmpfil[BUFSIZ];
528 char newbuf[MAXPATHLEN + 1];
531 strncpy(tmpfil, m_name(mp->hghmsg + 1), sizeof(tmpfil));
533 for (i = 0; i < nmsgs; i++) {
534 if (! (sp = mlist[i]))
535 continue; /* did this one */
539 continue; /* this one doesn't move */
542 ** the guy that was msg j is about to become msg i.
543 ** rename 'j' to make a hole, then recursively rename
544 ** guys to fill up the hole.
546 old = smsgs[j].s_msg;
547 new = smsgs[i].s_msg;
548 strncpy(f1, m_name(old), sizeof(f1));
551 printf("renaming message chain from %d to %d\n",
555 ** Run the external hook to refile the old message as the
556 ** temporary message number that is off of the end of the
557 ** messages in the folder.
560 snprintf(f1, sizeof (f1), "%s/%d", mp->foldpath, old);
561 snprintf(newbuf, sizeof (newbuf), "%s/%d",
562 mp->foldpath, mp->hghmsg + 1);
563 ext_hook("ref-hook", f1, newbuf);
565 if (rename(f1, tmpfil) == NOTOK)
566 adios(tmpfil, "unable to rename %s to ", f1);
568 get_msg_flags(mp, &tmpset, old);
570 rename_chain(mp, mlist, j, i);
573 ** Run the external hook to refile the temorary message number
574 ** to the real place.
577 snprintf(f1, sizeof (f1), "%s/%d", mp->foldpath, new);
578 ext_hook("ref-hook", newbuf, f1);
580 if (rename(tmpfil, m_name(new)) == NOTOK)
581 adios(m_name(new), "unable to rename %s to", tmpfil);
583 set_msg_flags(mp, &tmpset, new);
584 mp->msgflags |= SEQMOD;