]>
diplodocus.org Git - nmh/blob - uip/sortm.c
3 * sortm.c -- sort messages in a folder by date/time
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.
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) \
30 #define X(sw, minchars, id) id,
31 DEFINE_SWITCH_ENUM(SORTM
);
34 #define X(sw, minchars, id) { sw, minchars, id },
35 DEFINE_SWITCH_ARRAY(SORTM
, switches
);
44 static struct smsg
*smsgs
;
47 char *subjsort
= (char *) 0; /* sort on subject if != 0 */
49 int submajor
= 0; /* if true, sort on subject-major */
54 /* This keeps compiler happy on calls to qsort */
55 typedef int (*qsort_comp
) (const void *, const void *);
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
**);
70 main (int argc
, char **argv
)
74 char *maildir
, *datesw
= NULL
;
75 char *folder
= NULL
, buf
[BUFSIZ
], **argp
;
77 struct msgs_array msgs
= { 0, 0, NULL
};
83 setlocale(LC_ALL
, "");
85 invo_name
= r1bindex (argv
[0], '/');
87 /* read user profile/context */
90 arguments
= getarguments (invo_name
, argc
, argv
, 1);
96 while ((cp
= *argp
++)) {
98 switch (smatch (++cp
, switches
)) {
100 ambigsw (cp
, switches
);
103 adios (NULL
, "-%s unknown", cp
);
106 snprintf(buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
108 print_help (buf
, switches
, 1);
111 print_version(invo_name
);
116 adios (NULL
, "only one date field at a time");
117 if (!(datesw
= *argp
++) || *datesw
== '-')
118 adios (NULL
, "missing argument to %s", argp
[-2]);
123 adios (NULL
, "only one text field at a time");
124 if (!(subjsort
= *argp
++) || *subjsort
== '-')
125 adios (NULL
, "missing argument to %s", argp
[-2]);
129 subjsort
= "subject";
132 subjsort
= (char *)0;
136 if (!(cp
= *argp
++) || *cp
== '-')
137 adios (NULL
, "missing argument to %s", argp
[-2]);
139 cp
++; /* skip any leading zeros */
140 if (!*cp
) { /* hit end of string */
141 submajor
++; /* sort subject-major */
144 if (!isdigit(*cp
) || !(datelimit
= atoi(cp
)))
145 adios (NULL
, "impossible limit %s", cp
);
146 datelimit
*= 60*60*24;
149 submajor
= 0; /* use date-major, but */
150 datelimit
= 0; /* use no limit */
175 if (*cp
== '+' || *cp
== '@') {
177 adios (NULL
, "only one folder at a time!");
179 folder
= pluspath (cp
);
181 app_msgarg(&msgs
, cp
);
184 if (!context_find ("path"))
185 free (path ("./", TFOLDER
));
188 app_msgarg(&msgs
, "all");
190 adios (NULL
, "must specify messages to sort with -noall");
196 folder
= getfolder (1);
197 maildir
= m_maildir (folder
);
199 if (chdir (maildir
) == NOTOK
)
200 adios (maildir
, "unable to change directory to");
202 /* read folder and create message structure */
203 if (!(mp
= folder_read (folder
)))
204 adios (NULL
, "unable to read folder %s", folder
);
206 /* check for empty folder */
208 adios (NULL
, "no messages in %s", folder
);
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
]))
214 seq_setprev (mp
); /* set the previous sequence */
216 if ((nmsgs
= read_hdrs (mp
, datesw
)) <= 0)
217 adios (NULL
, "no messages to sort");
219 if (checksw
&& check_failed
) {
220 adios (NULL
, "errors found, no messages sorted");
224 * sort a list of pointers to our "messages to be sorted".
226 dlist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*dlist
));
227 for (i
= 0; i
< nmsgs
; i
++)
228 dlist
[i
] = &smsgs
[i
];
231 if (verbose
) { /* announce what we're doing */
234 printf ("sorting by %s\n", subjsort
);
236 printf ("sorting by %s-major %s-minor\n", subjsort
, datesw
);
238 printf ("sorting by datefield %s\n", datesw
);
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
));
246 * if we're sorting on subject, we need another list
247 * in subject order, then a merge pass to collate the
250 if (!submajor
&& subjsort
) { /* already date sorted */
251 struct smsg
**slist
, **flist
;
252 register struct smsg
***il
, **fp
, **dp
;
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
);
259 * make an inversion list so we can quickly find
260 * the collection of messages with the same subj
261 * given a message number.
263 il
= (struct smsg
***) calloc (mp
->hghsel
+1, sizeof(*il
));
265 adios (NULL
, "couldn't allocate msg list");
266 for (i
= 0; i
< nmsgs
; i
++)
267 il
[slist
[i
]->s_msg
] = &slist
[i
];
269 * make up the final list, chronological but with
270 * all the same subjects grouped together.
272 flist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*flist
));
274 for (dp
= dlist
; *dp
;) {
275 register struct smsg
**s
= il
[(*dp
++)->s_msg
];
277 /* see if we already did this guy */
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
288 while (*s
&& (*s
)->s_subj
[0] &&
289 strcmp((*s
)->s_subj
, s
[-1]->s_subj
) == 0 &&
291 (*s
)->s_clock
- s
[-1]->s_clock
<= datelimit
)) {
303 * At this point, dlist is a sorted array of pointers to smsg structures,
304 * each of which contains a message number.
307 rename_msgs (mp
, dlist
);
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 */
318 read_hdrs (struct msgs
*mp
, char *datesw
)
322 register struct smsg
*s
;
324 twscopy (&tb
, dlocaltimenow ());
326 smsgs
= (struct smsg
*)
327 calloc ((size_t) (mp
->hghsel
- mp
->lowsel
+ 2),
330 adios (NULL
, "unable to allocate sort storage");
333 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
334 if (is_selected(mp
, msgnum
)) {
335 if (get_fields (datesw
, msgnum
, s
)) {
347 * Parse the message and get the data or subject field,
352 get_fields (char *datesw
, int msg
, struct smsg
*smsg
)
356 char *msgnam
, buf
[BUFSIZ
], nam
[NAMESZ
];
357 register struct tws
*tw
;
358 register char *datecomp
= NULL
, *subjcomp
= NULL
;
360 m_getfld_state_t gstate
= 0;
362 if ((in
= fopen (msgnam
= m_name (msg
), "r")) == NULL
) {
363 admonish (msgnam
, "unable to read message");
366 for (compnum
= 1;;) {
367 int bufsz
= sizeof buf
;
368 switch (state
= m_getfld (&gstate
, nam
, buf
, &bufsz
, in
)) {
372 if (!mh_strcasecmp (nam
, datesw
)) {
373 datecomp
= add (buf
, datecomp
);
374 while (state
== FLDPLUS
) {
376 state
= m_getfld (&gstate
, nam
, buf
, &bufsz
, in
);
377 datecomp
= add (buf
, datecomp
);
379 if (!subjsort
|| subjcomp
)
381 } else if (subjsort
&& !mh_strcasecmp (nam
, subjsort
)) {
382 subjcomp
= add (buf
, subjcomp
);
383 while (state
== FLDPLUS
) {
385 state
= m_getfld (&gstate
, nam
, buf
, &bufsz
, in
);
386 subjcomp
= add (buf
, subjcomp
);
391 /* just flush this guy */
392 while (state
== FLDPLUS
) {
394 state
= m_getfld (&gstate
, nam
, buf
, &bufsz
, in
);
405 if (state
== LENERR
|| state
== FMTERR
) {
406 admonish (NULL
, "format error in message %d (header #%d)",
418 adios (NULL
, "internal error -- you lose");
422 m_getfld_state_destroy (&gstate
);
425 * If no date component, then use the modification
426 * time of the file as its date
428 if (!datecomp
|| (tw
= dparsetime (datecomp
)) == NULL
) {
432 "can't parse %s field in message %d, "
433 "will use file modification time",
435 fstat (fileno (in
), &st
);
436 smsg
->s_clock
= st
.st_mtime
;
439 smsg
->s_clock
= dmktime (tw
);
445 * try to make the subject "canonical": delete
446 * leading "re:", everything but letters & smash
447 * letters to lower case.
449 register char *cp
, *cp2
;
450 register unsigned char c
;
454 if (strcmp (subjsort
, "subject") == 0) {
466 while ((c
= *cp
++)) {
468 *cp2
++ = isupper(c
) ? tolower(c
) : c
;
476 smsg
->s_subj
= subjcomp
;
489 dsort (struct smsg
**a
, struct smsg
**b
)
491 if ((*a
)->s_clock
< (*b
)->s_clock
)
493 else if ((*a
)->s_clock
> (*b
)->s_clock
)
495 else if ((*a
)->s_msg
< (*b
)->s_msg
)
505 subsort (struct smsg
**a
, struct smsg
**b
)
509 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
512 return (dsort (a
, b
));
516 txtsort (struct smsg
**a
, struct smsg
**b
)
520 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
522 else if ((*a
)->s_msg
< (*b
)->s_msg
)
529 rename_chain (struct msgs
*mp
, struct smsg
**mlist
, int msg
, int endmsg
)
532 char *newname
, oldname
[BUFSIZ
];
533 char newbuf
[PATH_MAX
+ 1];
536 nxt
= mlist
[msg
] - smsgs
; /* mlist[msg] is a ptr into smsgs */
537 mlist
[msg
] = (struct smsg
*)0;
538 old
= smsgs
[nxt
].s_msg
;
539 new = smsgs
[msg
].s_msg
;
540 strncpy (oldname
, m_name (old
), sizeof(oldname
));
541 newname
= m_name (new);
543 printf ("message %d becomes message %d\n", old
, new);
545 (void)snprintf(oldname
, sizeof (oldname
), "%s/%d", mp
->foldpath
, old
);
546 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, new);
547 ext_hook("ref-hook", oldname
, newbuf
);
549 if (rename (oldname
, newname
) == NOTOK
)
550 adios (newname
, "unable to rename %s to", oldname
);
552 copy_msg_flags (mp
, new, old
);
553 if (mp
->curmsg
== old
)
554 seq_setcur (mp
, new);
561 /* if (nxt != endmsg); */
562 /* rename_chain (mp, mlist, nxt, endmsg); */
566 rename_msgs (struct msgs
*mp
, struct smsg
**mlist
)
570 char f1
[BUFSIZ
], tmpfil
[BUFSIZ
];
571 char newbuf
[PATH_MAX
+ 1];
574 strncpy (tmpfil
, m_name (mp
->hghmsg
+ 1), sizeof(tmpfil
));
576 for (i
= 0; i
< nmsgs
; i
++) {
577 if (! (sp
= mlist
[i
]))
578 continue; /* did this one */
582 continue; /* this one doesn't move */
585 * the guy that was msg j is about to become msg i.
586 * rename 'j' to make a hole, then recursively rename
587 * guys to fill up the hole.
589 old
= smsgs
[j
].s_msg
;
590 new = smsgs
[i
].s_msg
;
591 strncpy (f1
, m_name (old
), sizeof(f1
));
594 printf ("renaming message chain from %d to %d\n", old
, new);
597 * Run the external hook to refile the old message as the
598 * temporary message number that is off of the end of the
599 * messages in the folder.
602 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, old
);
603 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, mp
->hghmsg
+ 1);
604 ext_hook("ref-hook", f1
, newbuf
);
606 if (rename (f1
, tmpfil
) == NOTOK
)
607 adios (tmpfil
, "unable to rename %s to ", f1
);
609 get_msg_flags (mp
, &tmpset
, old
);
611 rename_chain (mp
, mlist
, j
, i
);
614 * Run the external hook to refile the temorary message number
618 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, new);
619 ext_hook("ref-hook", newbuf
, f1
);
621 if (rename (tmpfil
, m_name(new)) == NOTOK
)
622 adios (m_name(new), "unable to rename %s to", tmpfil
);
624 set_msg_flags (mp
, &tmpset
, new);
625 mp
->msgflags
|= SEQMOD
;