]>
diplodocus.org Git - nmh/blob - uip/sortm.c
79543207b36b7bd6b04a4b573e68bbf31945cdba
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.
13 #include "sbr/m_maildir.h"
15 #define SORTM_SWITCHES \
16 X("datefield field", 0, DATESW) \
17 X("textfield field", 0, TEXTSW) \
18 X("notextfield", 0, NSUBJSW) \
19 X("subject", -3, SUBJSW) /* backward-compatibility */ \
20 X("limit days", 0, LIMSW) \
21 X("nolimit", 0, NLIMSW) \
22 X("verbose", 0, VERBSW) \
23 X("noverbose", 0, NVERBSW) \
24 X("all", 0, ALLMSGS) \
25 X("noall", 0, NALLMSGS) \
26 X("check", 0, CHECKSW) \
27 X("nocheck", 0, NCHECKSW) \
28 X("version", 0, VERSIONSW) \
29 X("help", 0, HELPSW) \
31 #define X(sw, minchars, id) id,
32 DEFINE_SWITCH_ENUM(SORTM
);
35 #define X(sw, minchars, id) { sw, minchars, id },
36 DEFINE_SWITCH_ARRAY(SORTM
, switches
);
45 static struct smsg
*smsgs
;
48 char *subjsort
; /* sort on subject if != 0 */
50 bool submajor
; /* if true, sort on subject-major */
55 /* This keeps compiler happy on calls to qsort */
56 typedef int (*qsort_comp
) (const void *, const void *);
61 static int read_hdrs (struct msgs
*, char *);
62 static int get_fields (char *, int, struct smsg
*);
63 static int dsort (struct smsg
**, struct smsg
**);
64 static int subsort (struct smsg
**, struct smsg
**);
65 static int txtsort (struct smsg
**, struct smsg
**);
66 static void rename_chain (struct msgs
*, struct smsg
**, int, int);
67 static void rename_msgs (struct msgs
*, struct smsg
**);
71 main (int argc
, char **argv
)
74 char *cp
, *maildir
, *datesw
= NULL
;
75 char *folder
= NULL
, buf
[BUFSIZ
], **argp
;
77 struct msgs_array msgs
= { 0, 0, NULL
};
82 if (nmh_init(argv
[0], true, true)) { return 1; }
84 arguments
= getarguments (invo_name
, argc
, argv
, 1);
90 while ((cp
= *argp
++)) {
92 switch (smatch (++cp
, switches
)) {
94 ambigsw (cp
, switches
);
97 die("-%s unknown", cp
);
100 snprintf(buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
102 print_help (buf
, switches
, 1);
105 print_version(invo_name
);
110 die("only one date field at a time");
111 if (!(datesw
= *argp
++) || *datesw
== '-')
112 die("missing argument to %s", argp
[-2]);
117 die("only one text field at a time");
118 if (!(subjsort
= *argp
++) || *subjsort
== '-')
119 die("missing argument to %s", argp
[-2]);
123 subjsort
= "subject";
130 if (!(cp
= *argp
++) || *cp
== '-')
131 die("missing argument to %s", argp
[-2]);
133 cp
++; /* skip any leading zeros */
134 if (!*cp
) { /* hit end of string */
135 submajor
= true; /* sort subject-major */
138 if (!isdigit((unsigned char) *cp
) || !(datelimit
= atoi(cp
)))
139 die("impossible limit %s", cp
);
140 datelimit
*= 60*60*24;
143 submajor
= false; /* use date-major, but */
144 datelimit
= 0; /* use no limit */
169 if (*cp
== '+' || *cp
== '@') {
171 die("only one folder at a time!");
172 folder
= pluspath (cp
);
174 app_msgarg(&msgs
, cp
);
177 if (!context_find ("path"))
178 free (path ("./", TFOLDER
));
181 app_msgarg(&msgs
, "all");
183 die("must specify messages to sort with -noall");
189 folder
= getfolder (1);
190 maildir
= m_maildir (folder
);
192 if (chdir (maildir
) == NOTOK
)
193 adios (maildir
, "unable to change directory to");
195 /* read folder and create message structure */
196 if (!(mp
= folder_read (folder
, 1)))
197 die("unable to read folder %s", folder
);
199 /* check for empty folder */
201 die("no messages in %s", folder
);
203 /* parse all the message ranges/sequences and set SELECTED */
204 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
205 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
207 seq_setprev (mp
); /* set the previous sequence */
209 if ((nmsgs
= read_hdrs (mp
, datesw
)) <= 0)
210 die("no messages to sort");
212 if (checksw
&& check_failed
) {
213 die("errors found, no messages sorted");
217 * sort a list of pointers to our "messages to be sorted".
219 dlist
= mh_xmalloc ((nmsgs
+1) * sizeof(*dlist
));
220 for (i
= 0; i
< nmsgs
; i
++)
221 dlist
[i
] = &smsgs
[i
];
224 if (verbose
) { /* announce what we're doing */
227 printf ("sorting by %s\n", subjsort
);
229 printf ("sorting by %s-major %s-minor\n", subjsort
, datesw
);
231 printf ("sorting by datefield %s\n", datesw
);
234 /* first sort by date, or by subject-major, date-minor */
235 qsort (dlist
, nmsgs
, sizeof(*dlist
),
236 (qsort_comp
) (submajor
&& subjsort
? txtsort
: dsort
));
239 * if we're sorting on subject, we need another list
240 * in subject order, then a merge pass to collate the
243 if (!submajor
&& subjsort
) { /* already date sorted */
244 struct smsg
**slist
, **flist
;
245 struct smsg
***il
, **fp
, **dp
;
247 slist
= mh_xmalloc ((nmsgs
+1) * sizeof(*slist
));
248 memcpy(slist
, dlist
, (nmsgs
+1)*sizeof(*slist
));
249 qsort(slist
, nmsgs
, sizeof(*slist
), (qsort_comp
) subsort
);
252 * make an inversion list so we can quickly find
253 * the collection of messages with the same subj
254 * given a message number.
256 il
= mh_xcalloc(mp
->hghsel
+ 1, sizeof *il
);
257 for (i
= 0; i
< nmsgs
; i
++)
258 il
[slist
[i
]->s_msg
] = &slist
[i
];
260 * make up the final list, chronological but with
261 * all the same subjects grouped together.
263 flist
= mh_xmalloc ((nmsgs
+1) * sizeof(*flist
));
265 for (dp
= dlist
; *dp
;) {
266 struct smsg
**s
= il
[(*dp
++)->s_msg
];
268 /* see if we already did this guy */
274 * take the next message(s) if there is one,
275 * its subject isn't null and its subject
276 * is the same as this one and it's not too
279 while (*s
&& (*s
)->s_subj
[0] &&
280 strcmp((*s
)->s_subj
, s
[-1]->s_subj
) == 0 &&
282 (*s
)->s_clock
- s
[-1]->s_clock
<= datelimit
)) {
295 * At this point, dlist is a sorted array of pointers to smsg structures,
296 * each of which contains a message number.
299 rename_msgs (mp
, dlist
);
301 context_replace (pfolder
, folder
); /* update current folder */
302 seq_save (mp
); /* synchronize message sequences */
303 context_save (); /* save the context file */
304 folder_free (mp
); /* free folder/message structure */
310 read_hdrs (struct msgs
*mp
, char *datesw
)
315 smsgs
= mh_xcalloc(mp
->hghsel
- mp
->lowsel
+ 2, sizeof *smsgs
);
317 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
318 if (is_selected(mp
, msgnum
)) {
319 if (get_fields (datesw
, msgnum
, s
)) {
331 * Parse the message and get the data or subject field,
336 get_fields (char *datesw
, int msg
, struct smsg
*smsg
)
340 char *msgnam
, buf
[NMH_BUFSIZ
], nam
[NAMESZ
];
342 char *datecomp
= NULL
, *subjcomp
= NULL
;
344 m_getfld_state_t gstate
;
346 if ((in
= fopen (msgnam
= m_name (msg
), "r")) == NULL
) {
347 admonish (msgnam
, "unable to read message");
350 gstate
= m_getfld_state_init(in
);
351 for (compnum
= 1;;) {
352 int bufsz
= sizeof buf
;
353 switch (state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
)) {
357 if (!strcasecmp (nam
, datesw
)) {
358 datecomp
= add (buf
, datecomp
);
359 while (state
== FLDPLUS
) {
361 state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
);
362 datecomp
= add (buf
, datecomp
);
364 if (!subjsort
|| subjcomp
)
366 } else if (subjsort
&& !strcasecmp (nam
, subjsort
)) {
367 subjcomp
= add (buf
, subjcomp
);
368 while (state
== FLDPLUS
) {
370 state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
);
371 subjcomp
= add (buf
, subjcomp
);
376 /* just flush this guy */
377 while (state
== FLDPLUS
) {
379 state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
);
390 if (state
== LENERR
|| state
== FMTERR
) {
391 inform("format error in message %d (header #%d), continuing...",
401 die("internal error -- you lose");
405 m_getfld_state_destroy (&gstate
);
408 * If no date component, then use the modification
409 * time of the file as its date
411 if (!datecomp
|| (tw
= dparsetime (datecomp
)) == NULL
) {
414 inform("can't parse %s field in message %d, "
415 "will use file modification time", datesw
, msg
);
416 fstat (fileno (in
), &st
);
417 smsg
->s_clock
= st
.st_mtime
;
420 smsg
->s_clock
= dmktime (tw
);
426 * try to make the subject "canonical": delete
427 * leading "re:", everything but letters & smash
428 * letters to lower case.
434 if (strcmp (subjsort
, "subject") == 0) {
436 if (! isspace((unsigned char) c
)) {
445 while ((c
= *cp
++)) {
446 if (isascii((unsigned char) c
) && isalnum((unsigned char) c
))
447 *cp2
++ = tolower((unsigned char)c
);
455 smsg
->s_subj
= subjcomp
;
467 dsort (struct smsg
**a
, struct smsg
**b
)
469 if ((*a
)->s_clock
< (*b
)->s_clock
)
471 if ((*a
)->s_clock
> (*b
)->s_clock
)
473 if ((*a
)->s_msg
< (*b
)->s_msg
)
482 subsort (struct smsg
**a
, struct smsg
**b
)
486 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
493 txtsort (struct smsg
**a
, struct smsg
**b
)
497 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
499 if ((*a
)->s_msg
< (*b
)->s_msg
)
505 rename_chain (struct msgs
*mp
, struct smsg
**mlist
, int msg
, int endmsg
)
508 char *newname
, oldname
[BUFSIZ
];
509 char newbuf
[PATH_MAX
+ 1];
512 nxt
= mlist
[msg
] - smsgs
; /* mlist[msg] is a ptr into smsgs */
514 old
= smsgs
[nxt
].s_msg
;
515 new = smsgs
[msg
].s_msg
;
516 strncpy (oldname
, m_name (old
), sizeof(oldname
));
517 newname
= m_name (new);
519 printf ("message %d becomes message %d\n", old
, new);
521 (void)snprintf(oldname
, sizeof (oldname
), "%s/%d", mp
->foldpath
, old
);
522 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, new);
523 ext_hook("ref-hook", oldname
, newbuf
);
525 if (rename (oldname
, newname
) == NOTOK
)
526 adios (newname
, "unable to rename %s to", oldname
);
528 copy_msg_flags (mp
, new, old
);
529 if (mp
->curmsg
== old
)
530 seq_setcur (mp
, new);
537 /* if (nxt != endmsg); */
538 /* rename_chain (mp, mlist, nxt, endmsg); */
542 rename_msgs (struct msgs
*mp
, struct smsg
**mlist
)
545 bvector_t tmpset
= bvector_create ();
546 char f1
[BUFSIZ
], tmpfil
[BUFSIZ
];
547 char newbuf
[PATH_MAX
+ 1];
550 strncpy (tmpfil
, m_name (mp
->hghmsg
+ 1), sizeof(tmpfil
));
552 for (i
= 0; i
< nmsgs
; i
++) {
553 if (! (sp
= mlist
[i
]))
554 continue; /* did this one */
558 continue; /* this one doesn't move */
561 * the guy that was msg j is about to become msg i.
562 * rename 'j' to make a hole, then recursively rename
563 * guys to fill up the hole.
565 old
= smsgs
[j
].s_msg
;
566 new = smsgs
[i
].s_msg
;
567 strncpy (f1
, m_name (old
), sizeof(f1
));
570 printf ("renaming message chain from %d to %d\n", old
, new);
573 * Run the external hook to refile the old message as the
574 * temporary message number that is off of the end of the
575 * messages in the folder.
578 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, old
);
579 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, mp
->hghmsg
+ 1);
580 ext_hook("ref-hook", f1
, newbuf
);
582 if (rename (f1
, tmpfil
) == NOTOK
)
583 adios (tmpfil
, "unable to rename %s to ", f1
);
585 get_msg_flags (mp
, tmpset
, old
);
587 rename_chain (mp
, mlist
, j
, i
);
590 * Run the external hook to refile the temporary message number
594 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, new);
595 ext_hook("ref-hook", newbuf
, f1
);
597 if (rename (tmpfil
, m_name(new)) == NOTOK
)
598 adios (m_name(new), "unable to rename %s to", tmpfil
);
600 set_msg_flags (mp
, tmpset
, new);
601 mp
->msgflags
|= SEQMOD
;
604 bvector_free (tmpset
);