]>
diplodocus.org Git - nmh/blob - uip/sortm.c
cbda50a6b12d2fcd771f56e8d2d4c6011365c898
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/seq_setcur.h"
10 #include "sbr/seq_save.h"
11 #include "sbr/smatch.h"
13 #include "sbr/m_convert.h"
14 #include "sbr/getfolder.h"
15 #include "sbr/ext_hook.h"
16 #include "sbr/folder_read.h"
17 #include "sbr/folder_free.h"
18 #include "sbr/context_save.h"
19 #include "sbr/context_replace.h"
20 #include "sbr/context_find.h"
21 #include "sbr/ambigsw.h"
23 #include "sbr/print_version.h"
24 #include "sbr/print_help.h"
25 #include "sbr/error.h"
29 #include "sbr/m_maildir.h"
31 #define SORTM_SWITCHES \
32 X("datefield field", 0, DATESW) \
33 X("textfield field", 0, TEXTSW) \
34 X("notextfield", 0, NSUBJSW) \
35 X("subject", -3, SUBJSW) /* backward-compatibility */ \
36 X("limit days", 0, LIMSW) \
37 X("nolimit", 0, NLIMSW) \
38 X("verbose", 0, VERBSW) \
39 X("noverbose", 0, NVERBSW) \
40 X("all", 0, ALLMSGS) \
41 X("noall", 0, NALLMSGS) \
42 X("check", 0, CHECKSW) \
43 X("nocheck", 0, NCHECKSW) \
44 X("version", 0, VERSIONSW) \
45 X("help", 0, HELPSW) \
47 #define X(sw, minchars, id) id,
48 DEFINE_SWITCH_ENUM(SORTM
);
51 #define X(sw, minchars, id) { sw, minchars, id },
52 DEFINE_SWITCH_ARRAY(SORTM
, switches
);
61 static struct smsg
*smsgs
;
64 char *subjsort
; /* sort on subject if != 0 */
66 bool submajor
; /* if true, sort on subject-major */
71 /* This keeps compiler happy on calls to qsort */
72 typedef int (*qsort_comp
) (const void *, const void *);
77 static int read_hdrs (struct msgs
*, char *);
78 static int get_fields (char *, int, struct smsg
*);
79 static int dsort (struct smsg
**, struct smsg
**);
80 static int subsort (struct smsg
**, struct smsg
**);
81 static int txtsort (struct smsg
**, struct smsg
**);
82 static void rename_chain (struct msgs
*, struct smsg
**, int, int);
83 static void rename_msgs (struct msgs
*, struct smsg
**);
87 main (int argc
, char **argv
)
90 char *cp
, *maildir
, *datesw
= NULL
;
91 char *folder
= NULL
, buf
[BUFSIZ
], **argp
;
93 struct msgs_array msgs
= { 0, 0, NULL
};
98 if (nmh_init(argv
[0], true, true)) { return 1; }
100 arguments
= getarguments (invo_name
, argc
, argv
, 1);
106 while ((cp
= *argp
++)) {
108 switch (smatch (++cp
, switches
)) {
110 ambigsw (cp
, switches
);
113 die("-%s unknown", cp
);
116 snprintf(buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
118 print_help (buf
, switches
, 1);
121 print_version(invo_name
);
126 die("only one date field at a time");
127 if (!(datesw
= *argp
++) || *datesw
== '-')
128 die("missing argument to %s", argp
[-2]);
133 die("only one text field at a time");
134 if (!(subjsort
= *argp
++) || *subjsort
== '-')
135 die("missing argument to %s", argp
[-2]);
139 subjsort
= "subject";
146 if (!(cp
= *argp
++) || *cp
== '-')
147 die("missing argument to %s", argp
[-2]);
149 cp
++; /* skip any leading zeros */
150 if (!*cp
) { /* hit end of string */
151 submajor
= true; /* sort subject-major */
154 if (!isdigit((unsigned char) *cp
) || !(datelimit
= atoi(cp
)))
155 die("impossible limit %s", cp
);
156 datelimit
*= 60*60*24;
159 submajor
= false; /* use date-major, but */
160 datelimit
= 0; /* use no limit */
185 if (*cp
== '+' || *cp
== '@') {
187 die("only one folder at a time!");
188 folder
= pluspath (cp
);
190 app_msgarg(&msgs
, cp
);
193 if (!context_find ("path"))
194 free (path ("./", TFOLDER
));
197 app_msgarg(&msgs
, "all");
199 die("must specify messages to sort with -noall");
205 folder
= getfolder (1);
206 maildir
= m_maildir (folder
);
208 if (chdir (maildir
) == NOTOK
)
209 adios (maildir
, "unable to change directory to");
211 /* read folder and create message structure */
212 if (!(mp
= folder_read (folder
, 1)))
213 die("unable to read folder %s", folder
);
215 /* check for empty folder */
217 die("no messages in %s", folder
);
219 /* parse all the message ranges/sequences and set SELECTED */
220 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
221 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
223 seq_setprev (mp
); /* set the previous sequence */
225 if ((nmsgs
= read_hdrs (mp
, datesw
)) <= 0)
226 die("no messages to sort");
228 if (checksw
&& check_failed
) {
229 die("errors found, no messages sorted");
233 * sort a list of pointers to our "messages to be sorted".
235 dlist
= mh_xmalloc ((nmsgs
+1) * sizeof(*dlist
));
236 for (i
= 0; i
< nmsgs
; i
++)
237 dlist
[i
] = &smsgs
[i
];
240 if (verbose
) { /* announce what we're doing */
243 printf ("sorting by %s\n", subjsort
);
245 printf ("sorting by %s-major %s-minor\n", subjsort
, datesw
);
247 printf ("sorting by datefield %s\n", datesw
);
250 /* first sort by date, or by subject-major, date-minor */
251 qsort (dlist
, nmsgs
, sizeof(*dlist
),
252 (qsort_comp
) (submajor
&& subjsort
? txtsort
: dsort
));
255 * if we're sorting on subject, we need another list
256 * in subject order, then a merge pass to collate the
259 if (!submajor
&& subjsort
) { /* already date sorted */
260 struct smsg
**slist
, **flist
;
261 struct smsg
***il
, **fp
, **dp
;
263 slist
= mh_xmalloc ((nmsgs
+1) * sizeof(*slist
));
264 memcpy(slist
, dlist
, (nmsgs
+1)*sizeof(*slist
));
265 qsort(slist
, nmsgs
, sizeof(*slist
), (qsort_comp
) subsort
);
268 * make an inversion list so we can quickly find
269 * the collection of messages with the same subj
270 * given a message number.
272 il
= mh_xcalloc(mp
->hghsel
+ 1, sizeof *il
);
273 for (i
= 0; i
< nmsgs
; i
++)
274 il
[slist
[i
]->s_msg
] = &slist
[i
];
276 * make up the final list, chronological but with
277 * all the same subjects grouped together.
279 flist
= mh_xmalloc ((nmsgs
+1) * sizeof(*flist
));
281 for (dp
= dlist
; *dp
;) {
282 struct smsg
**s
= il
[(*dp
++)->s_msg
];
284 /* see if we already did this guy */
290 * take the next message(s) if there is one,
291 * its subject isn't null and its subject
292 * is the same as this one and it's not too
295 while (*s
&& (*s
)->s_subj
[0] &&
296 strcmp((*s
)->s_subj
, s
[-1]->s_subj
) == 0 &&
298 (*s
)->s_clock
- s
[-1]->s_clock
<= datelimit
)) {
311 * At this point, dlist is a sorted array of pointers to smsg structures,
312 * each of which contains a message number.
315 rename_msgs (mp
, dlist
);
317 context_replace (pfolder
, folder
); /* update current folder */
318 seq_save (mp
); /* synchronize message sequences */
319 context_save (); /* save the context file */
320 folder_free (mp
); /* free folder/message structure */
326 read_hdrs (struct msgs
*mp
, char *datesw
)
331 smsgs
= mh_xcalloc(mp
->hghsel
- mp
->lowsel
+ 2, sizeof *smsgs
);
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
[NMH_BUFSIZ
], nam
[NAMESZ
];
358 char *datecomp
= NULL
, *subjcomp
= NULL
;
360 m_getfld_state_t gstate
;
362 if ((in
= fopen (msgnam
= m_name (msg
), "r")) == NULL
) {
363 admonish (msgnam
, "unable to read message");
366 gstate
= m_getfld_state_init(in
);
367 for (compnum
= 1;;) {
368 int bufsz
= sizeof buf
;
369 switch (state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
)) {
373 if (!strcasecmp (nam
, datesw
)) {
374 datecomp
= add (buf
, datecomp
);
375 while (state
== FLDPLUS
) {
377 state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
);
378 datecomp
= add (buf
, datecomp
);
380 if (!subjsort
|| subjcomp
)
382 } else if (subjsort
&& !strcasecmp (nam
, subjsort
)) {
383 subjcomp
= add (buf
, subjcomp
);
384 while (state
== FLDPLUS
) {
386 state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
);
387 subjcomp
= add (buf
, subjcomp
);
392 /* just flush this guy */
393 while (state
== FLDPLUS
) {
395 state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
);
406 if (state
== LENERR
|| state
== FMTERR
) {
407 inform("format error in message %d (header #%d), continuing...",
417 die("internal error -- you lose");
421 m_getfld_state_destroy (&gstate
);
424 * If no date component, then use the modification
425 * time of the file as its date
427 if (!datecomp
|| (tw
= dparsetime (datecomp
)) == NULL
) {
430 inform("can't parse %s field in message %d, "
431 "will use file modification time", datesw
, msg
);
432 fstat (fileno (in
), &st
);
433 smsg
->s_clock
= st
.st_mtime
;
436 smsg
->s_clock
= dmktime (tw
);
442 * try to make the subject "canonical": delete
443 * leading "re:", everything but letters & smash
444 * letters to lower case.
450 if (strcmp (subjsort
, "subject") == 0) {
452 if (! isspace((unsigned char) c
)) {
461 while ((c
= *cp
++)) {
462 if (isascii((unsigned char) c
) && isalnum((unsigned char) c
))
463 *cp2
++ = tolower((unsigned char)c
);
471 smsg
->s_subj
= subjcomp
;
483 dsort (struct smsg
**a
, struct smsg
**b
)
485 if ((*a
)->s_clock
< (*b
)->s_clock
)
487 if ((*a
)->s_clock
> (*b
)->s_clock
)
489 if ((*a
)->s_msg
< (*b
)->s_msg
)
498 subsort (struct smsg
**a
, struct smsg
**b
)
502 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
509 txtsort (struct smsg
**a
, struct smsg
**b
)
513 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
515 if ((*a
)->s_msg
< (*b
)->s_msg
)
521 rename_chain (struct msgs
*mp
, struct smsg
**mlist
, int msg
, int endmsg
)
524 char *newname
, oldname
[BUFSIZ
];
525 char newbuf
[PATH_MAX
+ 1];
528 nxt
= mlist
[msg
] - smsgs
; /* mlist[msg] is a ptr into smsgs */
530 old
= smsgs
[nxt
].s_msg
;
531 new = smsgs
[msg
].s_msg
;
532 strncpy (oldname
, m_name (old
), sizeof(oldname
));
533 newname
= m_name (new);
535 printf ("message %d becomes message %d\n", old
, new);
537 (void)snprintf(oldname
, sizeof (oldname
), "%s/%d", mp
->foldpath
, old
);
538 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, new);
539 ext_hook("ref-hook", oldname
, newbuf
);
541 if (rename (oldname
, newname
) == NOTOK
)
542 adios (newname
, "unable to rename %s to", oldname
);
544 copy_msg_flags (mp
, new, old
);
545 if (mp
->curmsg
== old
)
546 seq_setcur (mp
, new);
553 /* if (nxt != endmsg); */
554 /* rename_chain (mp, mlist, nxt, endmsg); */
558 rename_msgs (struct msgs
*mp
, struct smsg
**mlist
)
561 bvector_t tmpset
= bvector_create ();
562 char f1
[BUFSIZ
], tmpfil
[BUFSIZ
];
563 char newbuf
[PATH_MAX
+ 1];
566 strncpy (tmpfil
, m_name (mp
->hghmsg
+ 1), sizeof(tmpfil
));
568 for (i
= 0; i
< nmsgs
; i
++) {
569 if (! (sp
= mlist
[i
]))
570 continue; /* did this one */
574 continue; /* this one doesn't move */
577 * the guy that was msg j is about to become msg i.
578 * rename 'j' to make a hole, then recursively rename
579 * guys to fill up the hole.
581 old
= smsgs
[j
].s_msg
;
582 new = smsgs
[i
].s_msg
;
583 strncpy (f1
, m_name (old
), sizeof(f1
));
586 printf ("renaming message chain from %d to %d\n", old
, new);
589 * Run the external hook to refile the old message as the
590 * temporary message number that is off of the end of the
591 * messages in the folder.
594 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, old
);
595 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, mp
->hghmsg
+ 1);
596 ext_hook("ref-hook", f1
, newbuf
);
598 if (rename (f1
, tmpfil
) == NOTOK
)
599 adios (tmpfil
, "unable to rename %s to ", f1
);
601 get_msg_flags (mp
, tmpset
, old
);
603 rename_chain (mp
, mlist
, j
, i
);
606 * Run the external hook to refile the temporary message number
610 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, new);
611 ext_hook("ref-hook", newbuf
, f1
);
613 if (rename (tmpfil
, m_name(new)) == NOTOK
)
614 adios (m_name(new), "unable to rename %s to", tmpfil
);
616 set_msg_flags (mp
, tmpset
, new);
617 mp
->msgflags
|= SEQMOD
;
620 bvector_free (tmpset
);