]>
diplodocus.org Git - nmh/blob - uip/sortm.c
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.
10 #include "sbr/m_name.h"
11 #include "sbr/m_getfld.h"
12 #include "sbr/getarguments.h"
13 #include "sbr/seq_setprev.h"
14 #include "sbr/seq_setcur.h"
15 #include "sbr/seq_save.h"
16 #include "sbr/smatch.h"
18 #include "sbr/m_convert.h"
19 #include "sbr/getfolder.h"
20 #include "sbr/ext_hook.h"
21 #include "sbr/folder_read.h"
22 #include "sbr/folder_free.h"
23 #include "sbr/context_save.h"
24 #include "sbr/context_replace.h"
25 #include "sbr/context_find.h"
26 #include "sbr/ambigsw.h"
28 #include "sbr/print_version.h"
29 #include "sbr/print_help.h"
30 #include "sbr/error.h"
34 #include "sbr/m_maildir.h"
36 #define SORTM_SWITCHES \
37 X("datefield field", 0, DATESW) \
38 X("textfield field", 0, TEXTSW) \
39 X("notextfield", 0, NSUBJSW) \
40 X("subject", -3, SUBJSW) /* backward-compatibility */ \
41 X("limit days", 0, LIMSW) \
42 X("nolimit", 0, NLIMSW) \
43 X("verbose", 0, VERBSW) \
44 X("noverbose", 0, NVERBSW) \
45 X("all", 0, ALLMSGS) \
46 X("noall", 0, NALLMSGS) \
47 X("check", 0, CHECKSW) \
48 X("nocheck", 0, NCHECKSW) \
49 X("version", 0, VERSIONSW) \
50 X("help", 0, HELPSW) \
52 #define X(sw, minchars, id) id,
53 DEFINE_SWITCH_ENUM(SORTM
);
56 #define X(sw, minchars, id) { sw, minchars, id },
57 DEFINE_SWITCH_ARRAY(SORTM
, switches
);
66 static struct smsg
*smsgs
;
69 char *subjsort
; /* sort on subject if != 0 */
71 bool submajor
; /* if true, sort on subject-major */
76 /* This keeps compiler happy on calls to qsort */
77 typedef int (*qsort_comp
) (const void *, const void *);
82 static int read_hdrs (struct msgs
*, char *);
83 static int get_fields (char *, int, struct smsg
*);
84 static int dsort (struct smsg
**, struct smsg
**);
85 static int subsort (struct smsg
**, struct smsg
**);
86 static int txtsort (struct smsg
**, struct smsg
**);
87 static void rename_chain (struct msgs
*, struct smsg
**, int, int);
88 static void rename_msgs (struct msgs
*, struct smsg
**);
92 main (int argc
, char **argv
)
95 char *cp
, *maildir
, *datesw
= NULL
;
96 char *folder
= NULL
, buf
[BUFSIZ
], **argp
;
98 struct msgs_array msgs
= { 0, 0, NULL
};
101 bool checksw
= false;
103 if (nmh_init(argv
[0], true, true)) { return 1; }
105 arguments
= getarguments (invo_name
, argc
, argv
, 1);
111 while ((cp
= *argp
++)) {
113 switch (smatch (++cp
, switches
)) {
115 ambigsw (cp
, switches
);
118 die("-%s unknown", cp
);
121 snprintf(buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
123 print_help (buf
, switches
, 1);
126 print_version(invo_name
);
131 die("only one date field at a time");
132 if (!(datesw
= *argp
++) || *datesw
== '-')
133 die("missing argument to %s", argp
[-2]);
138 die("only one text field at a time");
139 if (!(subjsort
= *argp
++) || *subjsort
== '-')
140 die("missing argument to %s", argp
[-2]);
144 subjsort
= "subject";
151 if (!(cp
= *argp
++) || *cp
== '-')
152 die("missing argument to %s", argp
[-2]);
154 cp
++; /* skip any leading zeros */
155 if (!*cp
) { /* hit end of string */
156 submajor
= true; /* sort subject-major */
159 if (!isdigit((unsigned char) *cp
) || !(datelimit
= atoi(cp
)))
160 die("impossible limit %s", cp
);
161 datelimit
*= 60*60*24;
164 submajor
= false; /* use date-major, but */
165 datelimit
= 0; /* use no limit */
190 if (*cp
== '+' || *cp
== '@') {
192 die("only one folder at a time!");
193 folder
= pluspath (cp
);
195 app_msgarg(&msgs
, cp
);
198 if (!context_find ("path"))
199 free (path ("./", TFOLDER
));
202 app_msgarg(&msgs
, "all");
204 die("must specify messages to sort with -noall");
210 folder
= getfolder (1);
211 maildir
= m_maildir (folder
);
213 if (chdir (maildir
) == NOTOK
)
214 adios (maildir
, "unable to change directory to");
216 /* read folder and create message structure */
217 if (!(mp
= folder_read (folder
, 1)))
218 die("unable to read folder %s", folder
);
220 /* check for empty folder */
222 die("no messages in %s", folder
);
224 /* parse all the message ranges/sequences and set SELECTED */
225 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
226 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
228 seq_setprev (mp
); /* set the previous sequence */
230 if ((nmsgs
= read_hdrs (mp
, datesw
)) <= 0)
231 die("no messages to sort");
233 if (checksw
&& check_failed
) {
234 die("errors found, no messages sorted");
238 * sort a list of pointers to our "messages to be sorted".
240 dlist
= mh_xmalloc ((nmsgs
+1) * sizeof(*dlist
));
241 for (i
= 0; i
< nmsgs
; i
++)
242 dlist
[i
] = &smsgs
[i
];
245 if (verbose
) { /* announce what we're doing */
248 printf ("sorting by %s\n", subjsort
);
250 printf ("sorting by %s-major %s-minor\n", subjsort
, datesw
);
252 printf ("sorting by datefield %s\n", datesw
);
255 /* first sort by date, or by subject-major, date-minor */
256 qsort (dlist
, nmsgs
, sizeof(*dlist
),
257 (qsort_comp
) (submajor
&& subjsort
? txtsort
: dsort
));
260 * if we're sorting on subject, we need another list
261 * in subject order, then a merge pass to collate the
264 if (!submajor
&& subjsort
) { /* already date sorted */
265 struct smsg
**slist
, **flist
;
266 struct smsg
***il
, **fp
, **dp
;
268 slist
= mh_xmalloc ((nmsgs
+1) * sizeof(*slist
));
269 memcpy(slist
, dlist
, (nmsgs
+1)*sizeof(*slist
));
270 qsort(slist
, nmsgs
, sizeof(*slist
), (qsort_comp
) subsort
);
273 * make an inversion list so we can quickly find
274 * the collection of messages with the same subj
275 * given a message number.
277 il
= mh_xcalloc(mp
->hghsel
+ 1, sizeof *il
);
278 for (i
= 0; i
< nmsgs
; i
++)
279 il
[slist
[i
]->s_msg
] = &slist
[i
];
281 * make up the final list, chronological but with
282 * all the same subjects grouped together.
284 flist
= mh_xmalloc ((nmsgs
+1) * sizeof(*flist
));
286 for (dp
= dlist
; *dp
;) {
287 struct smsg
**s
= il
[(*dp
++)->s_msg
];
289 /* see if we already did this guy */
295 * take the next message(s) if there is one,
296 * its subject isn't null and its subject
297 * is the same as this one and it's not too
300 while (*s
&& (*s
)->s_subj
[0] &&
301 strcmp((*s
)->s_subj
, s
[-1]->s_subj
) == 0 &&
303 (*s
)->s_clock
- s
[-1]->s_clock
<= datelimit
)) {
316 * At this point, dlist is a sorted array of pointers to smsg structures,
317 * each of which contains a message number.
320 rename_msgs (mp
, dlist
);
322 context_replace (pfolder
, folder
); /* update current folder */
323 seq_save (mp
); /* synchronize message sequences */
324 context_save (); /* save the context file */
325 folder_free (mp
); /* free folder/message structure */
331 read_hdrs (struct msgs
*mp
, char *datesw
)
336 smsgs
= mh_xcalloc(mp
->hghsel
- mp
->lowsel
+ 2, sizeof *smsgs
);
338 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
339 if (is_selected(mp
, msgnum
)) {
340 if (get_fields (datesw
, msgnum
, s
)) {
352 * Parse the message and get the data or subject field,
357 get_fields (char *datesw
, int msg
, struct smsg
*smsg
)
361 char *msgnam
, buf
[NMH_BUFSIZ
], nam
[NAMESZ
];
363 char *datecomp
= NULL
, *subjcomp
= NULL
;
365 m_getfld_state_t gstate
;
367 if ((in
= fopen (msgnam
= m_name (msg
), "r")) == NULL
) {
368 admonish (msgnam
, "unable to read message");
371 gstate
= m_getfld_state_init(in
);
372 for (compnum
= 1;;) {
373 int bufsz
= sizeof buf
;
374 switch (state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
)) {
378 if (!strcasecmp (nam
, datesw
)) {
379 datecomp
= add (buf
, datecomp
);
380 while (state
== FLDPLUS
) {
382 state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
);
383 datecomp
= add (buf
, datecomp
);
385 if (!subjsort
|| subjcomp
)
387 } else if (subjsort
&& !strcasecmp (nam
, subjsort
)) {
388 subjcomp
= add (buf
, subjcomp
);
389 while (state
== FLDPLUS
) {
391 state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
);
392 subjcomp
= add (buf
, subjcomp
);
397 /* just flush this guy */
398 while (state
== FLDPLUS
) {
400 state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
);
411 if (state
== LENERR
|| state
== FMTERR
) {
412 inform("format error in message %d (header #%d), continuing...",
422 die("internal error -- you lose");
426 m_getfld_state_destroy (&gstate
);
429 * If no date component, then use the modification
430 * time of the file as its date
432 if (!datecomp
|| (tw
= dparsetime (datecomp
)) == NULL
) {
435 inform("can't parse %s field in message %d, "
436 "will use file modification time", datesw
, msg
);
437 fstat (fileno (in
), &st
);
438 smsg
->s_clock
= st
.st_mtime
;
441 smsg
->s_clock
= dmktime (tw
);
447 * try to make the subject "canonical": delete
448 * leading "re:", everything but letters & smash
449 * letters to lower case.
455 if (strcmp (subjsort
, "subject") == 0) {
457 if (! isspace((unsigned char) c
)) {
466 while ((c
= *cp
++)) {
467 if (isascii((unsigned char) c
) && isalnum((unsigned char) c
))
468 *cp2
++ = tolower((unsigned char)c
);
476 smsg
->s_subj
= subjcomp
;
488 dsort (struct smsg
**a
, struct smsg
**b
)
490 if ((*a
)->s_clock
< (*b
)->s_clock
)
492 if ((*a
)->s_clock
> (*b
)->s_clock
)
494 if ((*a
)->s_msg
< (*b
)->s_msg
)
503 subsort (struct smsg
**a
, struct smsg
**b
)
507 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
514 txtsort (struct smsg
**a
, struct smsg
**b
)
518 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
520 if ((*a
)->s_msg
< (*b
)->s_msg
)
526 rename_chain (struct msgs
*mp
, struct smsg
**mlist
, int msg
, int endmsg
)
529 char *newname
, oldname
[BUFSIZ
];
530 char newbuf
[PATH_MAX
+ 1];
533 nxt
= mlist
[msg
] - smsgs
; /* mlist[msg] is a ptr into smsgs */
535 old
= smsgs
[nxt
].s_msg
;
536 new = smsgs
[msg
].s_msg
;
537 strncpy (oldname
, m_name (old
), sizeof(oldname
));
538 newname
= m_name (new);
540 printf ("message %d becomes message %d\n", old
, new);
542 (void)snprintf(oldname
, sizeof (oldname
), "%s/%d", mp
->foldpath
, old
);
543 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, new);
544 ext_hook("ref-hook", oldname
, newbuf
);
546 if (rename (oldname
, newname
) == NOTOK
)
547 adios (newname
, "unable to rename %s to", oldname
);
549 copy_msg_flags (mp
, new, old
);
550 if (mp
->curmsg
== old
)
551 seq_setcur (mp
, new);
558 /* if (nxt != endmsg); */
559 /* rename_chain (mp, mlist, nxt, endmsg); */
563 rename_msgs (struct msgs
*mp
, struct smsg
**mlist
)
566 bvector_t tmpset
= bvector_create ();
567 char f1
[BUFSIZ
], tmpfil
[BUFSIZ
];
568 char newbuf
[PATH_MAX
+ 1];
571 strncpy (tmpfil
, m_name (mp
->hghmsg
+ 1), sizeof(tmpfil
));
573 for (i
= 0; i
< nmsgs
; i
++) {
574 if (! (sp
= mlist
[i
]))
575 continue; /* did this one */
579 continue; /* this one doesn't move */
582 * the guy that was msg j is about to become msg i.
583 * rename 'j' to make a hole, then recursively rename
584 * guys to fill up the hole.
586 old
= smsgs
[j
].s_msg
;
587 new = smsgs
[i
].s_msg
;
588 strncpy (f1
, m_name (old
), sizeof(f1
));
591 printf ("renaming message chain from %d to %d\n", old
, new);
594 * Run the external hook to refile the old message as the
595 * temporary message number that is off of the end of the
596 * messages in the folder.
599 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, old
);
600 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, mp
->hghmsg
+ 1);
601 ext_hook("ref-hook", f1
, newbuf
);
603 if (rename (f1
, tmpfil
) == NOTOK
)
604 adios (tmpfil
, "unable to rename %s to ", f1
);
606 get_msg_flags (mp
, tmpset
, old
);
608 rename_chain (mp
, mlist
, j
, i
);
611 * Run the external hook to refile the temporary message number
615 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, new);
616 ext_hook("ref-hook", newbuf
, f1
);
618 if (rename (tmpfil
, m_name(new)) == NOTOK
)
619 adios (m_name(new), "unable to rename %s to", tmpfil
);
621 set_msg_flags (mp
, tmpset
, new);
622 mp
->msgflags
|= SEQMOD
;
625 bvector_free (tmpset
);