]>
diplodocus.org Git - nmh/blob - uip/sortm.c
5505b82ff33c1c1e667585bb4567e234a90a5215
1 /* sortm.c -- sort messages in a folder by date/time
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 #include "sbr/ext_hook.h"
10 #include "sbr/folder_read.h"
11 #include "sbr/folder_free.h"
12 #include "sbr/context_save.h"
13 #include "sbr/context_replace.h"
14 #include "sbr/context_find.h"
15 #include "sbr/ambigsw.h"
17 #include "sbr/print_version.h"
18 #include "sbr/print_help.h"
19 #include "sbr/error.h"
23 #include "sbr/m_maildir.h"
25 #define SORTM_SWITCHES \
26 X("datefield field", 0, DATESW) \
27 X("textfield field", 0, TEXTSW) \
28 X("notextfield", 0, NSUBJSW) \
29 X("subject", -3, SUBJSW) /* backward-compatibility */ \
30 X("limit days", 0, LIMSW) \
31 X("nolimit", 0, NLIMSW) \
32 X("verbose", 0, VERBSW) \
33 X("noverbose", 0, NVERBSW) \
34 X("all", 0, ALLMSGS) \
35 X("noall", 0, NALLMSGS) \
36 X("check", 0, CHECKSW) \
37 X("nocheck", 0, NCHECKSW) \
38 X("version", 0, VERSIONSW) \
39 X("help", 0, HELPSW) \
41 #define X(sw, minchars, id) id,
42 DEFINE_SWITCH_ENUM(SORTM
);
45 #define X(sw, minchars, id) { sw, minchars, id },
46 DEFINE_SWITCH_ARRAY(SORTM
, switches
);
55 static struct smsg
*smsgs
;
58 char *subjsort
; /* sort on subject if != 0 */
60 bool submajor
; /* if true, sort on subject-major */
65 /* This keeps compiler happy on calls to qsort */
66 typedef int (*qsort_comp
) (const void *, const void *);
71 static int read_hdrs (struct msgs
*, char *);
72 static int get_fields (char *, int, struct smsg
*);
73 static int dsort (struct smsg
**, struct smsg
**);
74 static int subsort (struct smsg
**, struct smsg
**);
75 static int txtsort (struct smsg
**, struct smsg
**);
76 static void rename_chain (struct msgs
*, struct smsg
**, int, int);
77 static void rename_msgs (struct msgs
*, struct smsg
**);
81 main (int argc
, char **argv
)
84 char *cp
, *maildir
, *datesw
= NULL
;
85 char *folder
= NULL
, buf
[BUFSIZ
], **argp
;
87 struct msgs_array msgs
= { 0, 0, NULL
};
92 if (nmh_init(argv
[0], true, true)) { return 1; }
94 arguments
= getarguments (invo_name
, argc
, argv
, 1);
100 while ((cp
= *argp
++)) {
102 switch (smatch (++cp
, switches
)) {
104 ambigsw (cp
, switches
);
107 die("-%s unknown", cp
);
110 snprintf(buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
112 print_help (buf
, switches
, 1);
115 print_version(invo_name
);
120 die("only one date field at a time");
121 if (!(datesw
= *argp
++) || *datesw
== '-')
122 die("missing argument to %s", argp
[-2]);
127 die("only one text field at a time");
128 if (!(subjsort
= *argp
++) || *subjsort
== '-')
129 die("missing argument to %s", argp
[-2]);
133 subjsort
= "subject";
140 if (!(cp
= *argp
++) || *cp
== '-')
141 die("missing argument to %s", argp
[-2]);
143 cp
++; /* skip any leading zeros */
144 if (!*cp
) { /* hit end of string */
145 submajor
= true; /* sort subject-major */
148 if (!isdigit((unsigned char) *cp
) || !(datelimit
= atoi(cp
)))
149 die("impossible limit %s", cp
);
150 datelimit
*= 60*60*24;
153 submajor
= false; /* use date-major, but */
154 datelimit
= 0; /* use no limit */
179 if (*cp
== '+' || *cp
== '@') {
181 die("only one folder at a time!");
182 folder
= pluspath (cp
);
184 app_msgarg(&msgs
, cp
);
187 if (!context_find ("path"))
188 free (path ("./", TFOLDER
));
191 app_msgarg(&msgs
, "all");
193 die("must specify messages to sort with -noall");
199 folder
= getfolder (1);
200 maildir
= m_maildir (folder
);
202 if (chdir (maildir
) == NOTOK
)
203 adios (maildir
, "unable to change directory to");
205 /* read folder and create message structure */
206 if (!(mp
= folder_read (folder
, 1)))
207 die("unable to read folder %s", folder
);
209 /* check for empty folder */
211 die("no messages in %s", folder
);
213 /* parse all the message ranges/sequences and set SELECTED */
214 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
215 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
217 seq_setprev (mp
); /* set the previous sequence */
219 if ((nmsgs
= read_hdrs (mp
, datesw
)) <= 0)
220 die("no messages to sort");
222 if (checksw
&& check_failed
) {
223 die("errors found, no messages sorted");
227 * sort a list of pointers to our "messages to be sorted".
229 dlist
= mh_xmalloc ((nmsgs
+1) * sizeof(*dlist
));
230 for (i
= 0; i
< nmsgs
; i
++)
231 dlist
[i
] = &smsgs
[i
];
234 if (verbose
) { /* announce what we're doing */
237 printf ("sorting by %s\n", subjsort
);
239 printf ("sorting by %s-major %s-minor\n", subjsort
, datesw
);
241 printf ("sorting by datefield %s\n", datesw
);
244 /* first sort by date, or by subject-major, date-minor */
245 qsort (dlist
, nmsgs
, sizeof(*dlist
),
246 (qsort_comp
) (submajor
&& subjsort
? txtsort
: dsort
));
249 * if we're sorting on subject, we need another list
250 * in subject order, then a merge pass to collate the
253 if (!submajor
&& subjsort
) { /* already date sorted */
254 struct smsg
**slist
, **flist
;
255 struct smsg
***il
, **fp
, **dp
;
257 slist
= mh_xmalloc ((nmsgs
+1) * sizeof(*slist
));
258 memcpy(slist
, dlist
, (nmsgs
+1)*sizeof(*slist
));
259 qsort(slist
, nmsgs
, sizeof(*slist
), (qsort_comp
) subsort
);
262 * make an inversion list so we can quickly find
263 * the collection of messages with the same subj
264 * given a message number.
266 il
= mh_xcalloc(mp
->hghsel
+ 1, sizeof *il
);
267 for (i
= 0; i
< nmsgs
; i
++)
268 il
[slist
[i
]->s_msg
] = &slist
[i
];
270 * make up the final list, chronological but with
271 * all the same subjects grouped together.
273 flist
= mh_xmalloc ((nmsgs
+1) * sizeof(*flist
));
275 for (dp
= dlist
; *dp
;) {
276 struct smsg
**s
= il
[(*dp
++)->s_msg
];
278 /* see if we already did this guy */
284 * take the next message(s) if there is one,
285 * its subject isn't null and its subject
286 * is the same as this one and it's not too
289 while (*s
&& (*s
)->s_subj
[0] &&
290 strcmp((*s
)->s_subj
, s
[-1]->s_subj
) == 0 &&
292 (*s
)->s_clock
- s
[-1]->s_clock
<= datelimit
)) {
305 * At this point, dlist is a sorted array of pointers to smsg structures,
306 * each of which contains a message number.
309 rename_msgs (mp
, dlist
);
311 context_replace (pfolder
, folder
); /* update current folder */
312 seq_save (mp
); /* synchronize message sequences */
313 context_save (); /* save the context file */
314 folder_free (mp
); /* free folder/message structure */
320 read_hdrs (struct msgs
*mp
, char *datesw
)
325 smsgs
= mh_xcalloc(mp
->hghsel
- mp
->lowsel
+ 2, sizeof *smsgs
);
327 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
328 if (is_selected(mp
, msgnum
)) {
329 if (get_fields (datesw
, msgnum
, s
)) {
341 * Parse the message and get the data or subject field,
346 get_fields (char *datesw
, int msg
, struct smsg
*smsg
)
350 char *msgnam
, buf
[NMH_BUFSIZ
], nam
[NAMESZ
];
352 char *datecomp
= NULL
, *subjcomp
= NULL
;
354 m_getfld_state_t gstate
;
356 if ((in
= fopen (msgnam
= m_name (msg
), "r")) == NULL
) {
357 admonish (msgnam
, "unable to read message");
360 gstate
= m_getfld_state_init(in
);
361 for (compnum
= 1;;) {
362 int bufsz
= sizeof buf
;
363 switch (state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
)) {
367 if (!strcasecmp (nam
, datesw
)) {
368 datecomp
= add (buf
, datecomp
);
369 while (state
== FLDPLUS
) {
371 state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
);
372 datecomp
= add (buf
, datecomp
);
374 if (!subjsort
|| subjcomp
)
376 } else if (subjsort
&& !strcasecmp (nam
, subjsort
)) {
377 subjcomp
= add (buf
, subjcomp
);
378 while (state
== FLDPLUS
) {
380 state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
);
381 subjcomp
= add (buf
, subjcomp
);
386 /* just flush this guy */
387 while (state
== FLDPLUS
) {
389 state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
);
400 if (state
== LENERR
|| state
== FMTERR
) {
401 inform("format error in message %d (header #%d), continuing...",
411 die("internal error -- you lose");
415 m_getfld_state_destroy (&gstate
);
418 * If no date component, then use the modification
419 * time of the file as its date
421 if (!datecomp
|| (tw
= dparsetime (datecomp
)) == NULL
) {
424 inform("can't parse %s field in message %d, "
425 "will use file modification time", datesw
, msg
);
426 fstat (fileno (in
), &st
);
427 smsg
->s_clock
= st
.st_mtime
;
430 smsg
->s_clock
= dmktime (tw
);
436 * try to make the subject "canonical": delete
437 * leading "re:", everything but letters & smash
438 * letters to lower case.
444 if (strcmp (subjsort
, "subject") == 0) {
446 if (! isspace((unsigned char) c
)) {
455 while ((c
= *cp
++)) {
456 if (isascii((unsigned char) c
) && isalnum((unsigned char) c
))
457 *cp2
++ = tolower((unsigned char)c
);
465 smsg
->s_subj
= subjcomp
;
477 dsort (struct smsg
**a
, struct smsg
**b
)
479 if ((*a
)->s_clock
< (*b
)->s_clock
)
481 if ((*a
)->s_clock
> (*b
)->s_clock
)
483 if ((*a
)->s_msg
< (*b
)->s_msg
)
492 subsort (struct smsg
**a
, struct smsg
**b
)
496 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
503 txtsort (struct smsg
**a
, struct smsg
**b
)
507 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
509 if ((*a
)->s_msg
< (*b
)->s_msg
)
515 rename_chain (struct msgs
*mp
, struct smsg
**mlist
, int msg
, int endmsg
)
518 char *newname
, oldname
[BUFSIZ
];
519 char newbuf
[PATH_MAX
+ 1];
522 nxt
= mlist
[msg
] - smsgs
; /* mlist[msg] is a ptr into smsgs */
524 old
= smsgs
[nxt
].s_msg
;
525 new = smsgs
[msg
].s_msg
;
526 strncpy (oldname
, m_name (old
), sizeof(oldname
));
527 newname
= m_name (new);
529 printf ("message %d becomes message %d\n", old
, new);
531 (void)snprintf(oldname
, sizeof (oldname
), "%s/%d", mp
->foldpath
, old
);
532 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, new);
533 ext_hook("ref-hook", oldname
, newbuf
);
535 if (rename (oldname
, newname
) == NOTOK
)
536 adios (newname
, "unable to rename %s to", oldname
);
538 copy_msg_flags (mp
, new, old
);
539 if (mp
->curmsg
== old
)
540 seq_setcur (mp
, new);
547 /* if (nxt != endmsg); */
548 /* rename_chain (mp, mlist, nxt, endmsg); */
552 rename_msgs (struct msgs
*mp
, struct smsg
**mlist
)
555 bvector_t tmpset
= bvector_create ();
556 char f1
[BUFSIZ
], tmpfil
[BUFSIZ
];
557 char newbuf
[PATH_MAX
+ 1];
560 strncpy (tmpfil
, m_name (mp
->hghmsg
+ 1), sizeof(tmpfil
));
562 for (i
= 0; i
< nmsgs
; i
++) {
563 if (! (sp
= mlist
[i
]))
564 continue; /* did this one */
568 continue; /* this one doesn't move */
571 * the guy that was msg j is about to become msg i.
572 * rename 'j' to make a hole, then recursively rename
573 * guys to fill up the hole.
575 old
= smsgs
[j
].s_msg
;
576 new = smsgs
[i
].s_msg
;
577 strncpy (f1
, m_name (old
), sizeof(f1
));
580 printf ("renaming message chain from %d to %d\n", old
, new);
583 * Run the external hook to refile the old message as the
584 * temporary message number that is off of the end of the
585 * messages in the folder.
588 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, old
);
589 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, mp
->hghmsg
+ 1);
590 ext_hook("ref-hook", f1
, newbuf
);
592 if (rename (f1
, tmpfil
) == NOTOK
)
593 adios (tmpfil
, "unable to rename %s to ", f1
);
595 get_msg_flags (mp
, tmpset
, old
);
597 rename_chain (mp
, mlist
, j
, i
);
600 * Run the external hook to refile the temporary message number
604 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, new);
605 ext_hook("ref-hook", newbuf
, f1
);
607 if (rename (tmpfil
, m_name(new)) == NOTOK
)
608 adios (m_name(new), "unable to rename %s to", tmpfil
);
610 set_msg_flags (mp
, tmpset
, new);
611 mp
->msgflags
|= SEQMOD
;
614 bvector_free (tmpset
);