]>
diplodocus.org Git - nmh/blob - uip/sortm.c
7380aa6545712ad6cdc7d0c8da1043ca7bf1d1ec
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/print_version.h"
10 #include "sbr/print_help.h"
11 #include "sbr/error.h"
15 #include "sbr/m_maildir.h"
17 #define SORTM_SWITCHES \
18 X("datefield field", 0, DATESW) \
19 X("textfield field", 0, TEXTSW) \
20 X("notextfield", 0, NSUBJSW) \
21 X("subject", -3, SUBJSW) /* backward-compatibility */ \
22 X("limit days", 0, LIMSW) \
23 X("nolimit", 0, NLIMSW) \
24 X("verbose", 0, VERBSW) \
25 X("noverbose", 0, NVERBSW) \
26 X("all", 0, ALLMSGS) \
27 X("noall", 0, NALLMSGS) \
28 X("check", 0, CHECKSW) \
29 X("nocheck", 0, NCHECKSW) \
30 X("version", 0, VERSIONSW) \
31 X("help", 0, HELPSW) \
33 #define X(sw, minchars, id) id,
34 DEFINE_SWITCH_ENUM(SORTM
);
37 #define X(sw, minchars, id) { sw, minchars, id },
38 DEFINE_SWITCH_ARRAY(SORTM
, switches
);
47 static struct smsg
*smsgs
;
50 char *subjsort
; /* sort on subject if != 0 */
52 bool submajor
; /* if true, sort on subject-major */
57 /* This keeps compiler happy on calls to qsort */
58 typedef int (*qsort_comp
) (const void *, const void *);
63 static int read_hdrs (struct msgs
*, char *);
64 static int get_fields (char *, int, struct smsg
*);
65 static int dsort (struct smsg
**, struct smsg
**);
66 static int subsort (struct smsg
**, struct smsg
**);
67 static int txtsort (struct smsg
**, struct smsg
**);
68 static void rename_chain (struct msgs
*, struct smsg
**, int, int);
69 static void rename_msgs (struct msgs
*, struct smsg
**);
73 main (int argc
, char **argv
)
76 char *cp
, *maildir
, *datesw
= NULL
;
77 char *folder
= NULL
, buf
[BUFSIZ
], **argp
;
79 struct msgs_array msgs
= { 0, 0, NULL
};
84 if (nmh_init(argv
[0], true, true)) { return 1; }
86 arguments
= getarguments (invo_name
, argc
, argv
, 1);
92 while ((cp
= *argp
++)) {
94 switch (smatch (++cp
, switches
)) {
96 ambigsw (cp
, switches
);
99 die("-%s unknown", cp
);
102 snprintf(buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
104 print_help (buf
, switches
, 1);
107 print_version(invo_name
);
112 die("only one date field at a time");
113 if (!(datesw
= *argp
++) || *datesw
== '-')
114 die("missing argument to %s", argp
[-2]);
119 die("only one text field at a time");
120 if (!(subjsort
= *argp
++) || *subjsort
== '-')
121 die("missing argument to %s", argp
[-2]);
125 subjsort
= "subject";
132 if (!(cp
= *argp
++) || *cp
== '-')
133 die("missing argument to %s", argp
[-2]);
135 cp
++; /* skip any leading zeros */
136 if (!*cp
) { /* hit end of string */
137 submajor
= true; /* sort subject-major */
140 if (!isdigit((unsigned char) *cp
) || !(datelimit
= atoi(cp
)))
141 die("impossible limit %s", cp
);
142 datelimit
*= 60*60*24;
145 submajor
= false; /* use date-major, but */
146 datelimit
= 0; /* use no limit */
171 if (*cp
== '+' || *cp
== '@') {
173 die("only one folder at a time!");
174 folder
= pluspath (cp
);
176 app_msgarg(&msgs
, cp
);
179 if (!context_find ("path"))
180 free (path ("./", TFOLDER
));
183 app_msgarg(&msgs
, "all");
185 die("must specify messages to sort with -noall");
191 folder
= getfolder (1);
192 maildir
= m_maildir (folder
);
194 if (chdir (maildir
) == NOTOK
)
195 adios (maildir
, "unable to change directory to");
197 /* read folder and create message structure */
198 if (!(mp
= folder_read (folder
, 1)))
199 die("unable to read folder %s", folder
);
201 /* check for empty folder */
203 die("no messages in %s", folder
);
205 /* parse all the message ranges/sequences and set SELECTED */
206 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
207 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
209 seq_setprev (mp
); /* set the previous sequence */
211 if ((nmsgs
= read_hdrs (mp
, datesw
)) <= 0)
212 die("no messages to sort");
214 if (checksw
&& check_failed
) {
215 die("errors found, no messages sorted");
219 * sort a list of pointers to our "messages to be sorted".
221 dlist
= mh_xmalloc ((nmsgs
+1) * sizeof(*dlist
));
222 for (i
= 0; i
< nmsgs
; i
++)
223 dlist
[i
] = &smsgs
[i
];
226 if (verbose
) { /* announce what we're doing */
229 printf ("sorting by %s\n", subjsort
);
231 printf ("sorting by %s-major %s-minor\n", subjsort
, datesw
);
233 printf ("sorting by datefield %s\n", datesw
);
236 /* first sort by date, or by subject-major, date-minor */
237 qsort (dlist
, nmsgs
, sizeof(*dlist
),
238 (qsort_comp
) (submajor
&& subjsort
? txtsort
: dsort
));
241 * if we're sorting on subject, we need another list
242 * in subject order, then a merge pass to collate the
245 if (!submajor
&& subjsort
) { /* already date sorted */
246 struct smsg
**slist
, **flist
;
247 struct smsg
***il
, **fp
, **dp
;
249 slist
= mh_xmalloc ((nmsgs
+1) * sizeof(*slist
));
250 memcpy(slist
, dlist
, (nmsgs
+1)*sizeof(*slist
));
251 qsort(slist
, nmsgs
, sizeof(*slist
), (qsort_comp
) subsort
);
254 * make an inversion list so we can quickly find
255 * the collection of messages with the same subj
256 * given a message number.
258 il
= mh_xcalloc(mp
->hghsel
+ 1, sizeof *il
);
259 for (i
= 0; i
< nmsgs
; i
++)
260 il
[slist
[i
]->s_msg
] = &slist
[i
];
262 * make up the final list, chronological but with
263 * all the same subjects grouped together.
265 flist
= mh_xmalloc ((nmsgs
+1) * sizeof(*flist
));
267 for (dp
= dlist
; *dp
;) {
268 struct smsg
**s
= il
[(*dp
++)->s_msg
];
270 /* see if we already did this guy */
276 * take the next message(s) if there is one,
277 * its subject isn't null and its subject
278 * is the same as this one and it's not too
281 while (*s
&& (*s
)->s_subj
[0] &&
282 strcmp((*s
)->s_subj
, s
[-1]->s_subj
) == 0 &&
284 (*s
)->s_clock
- s
[-1]->s_clock
<= datelimit
)) {
297 * At this point, dlist is a sorted array of pointers to smsg structures,
298 * each of which contains a message number.
301 rename_msgs (mp
, dlist
);
303 context_replace (pfolder
, folder
); /* update current folder */
304 seq_save (mp
); /* synchronize message sequences */
305 context_save (); /* save the context file */
306 folder_free (mp
); /* free folder/message structure */
312 read_hdrs (struct msgs
*mp
, char *datesw
)
317 smsgs
= mh_xcalloc(mp
->hghsel
- mp
->lowsel
+ 2, sizeof *smsgs
);
319 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
320 if (is_selected(mp
, msgnum
)) {
321 if (get_fields (datesw
, msgnum
, s
)) {
333 * Parse the message and get the data or subject field,
338 get_fields (char *datesw
, int msg
, struct smsg
*smsg
)
342 char *msgnam
, buf
[NMH_BUFSIZ
], nam
[NAMESZ
];
344 char *datecomp
= NULL
, *subjcomp
= NULL
;
346 m_getfld_state_t gstate
;
348 if ((in
= fopen (msgnam
= m_name (msg
), "r")) == NULL
) {
349 admonish (msgnam
, "unable to read message");
352 gstate
= m_getfld_state_init(in
);
353 for (compnum
= 1;;) {
354 int bufsz
= sizeof buf
;
355 switch (state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
)) {
359 if (!strcasecmp (nam
, datesw
)) {
360 datecomp
= add (buf
, datecomp
);
361 while (state
== FLDPLUS
) {
363 state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
);
364 datecomp
= add (buf
, datecomp
);
366 if (!subjsort
|| subjcomp
)
368 } else if (subjsort
&& !strcasecmp (nam
, subjsort
)) {
369 subjcomp
= add (buf
, subjcomp
);
370 while (state
== FLDPLUS
) {
372 state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
);
373 subjcomp
= add (buf
, subjcomp
);
378 /* just flush this guy */
379 while (state
== FLDPLUS
) {
381 state
= m_getfld2(&gstate
, nam
, buf
, &bufsz
);
392 if (state
== LENERR
|| state
== FMTERR
) {
393 inform("format error in message %d (header #%d), continuing...",
403 die("internal error -- you lose");
407 m_getfld_state_destroy (&gstate
);
410 * If no date component, then use the modification
411 * time of the file as its date
413 if (!datecomp
|| (tw
= dparsetime (datecomp
)) == NULL
) {
416 inform("can't parse %s field in message %d, "
417 "will use file modification time", datesw
, msg
);
418 fstat (fileno (in
), &st
);
419 smsg
->s_clock
= st
.st_mtime
;
422 smsg
->s_clock
= dmktime (tw
);
428 * try to make the subject "canonical": delete
429 * leading "re:", everything but letters & smash
430 * letters to lower case.
436 if (strcmp (subjsort
, "subject") == 0) {
438 if (! isspace((unsigned char) c
)) {
447 while ((c
= *cp
++)) {
448 if (isascii((unsigned char) c
) && isalnum((unsigned char) c
))
449 *cp2
++ = tolower((unsigned char)c
);
457 smsg
->s_subj
= subjcomp
;
469 dsort (struct smsg
**a
, struct smsg
**b
)
471 if ((*a
)->s_clock
< (*b
)->s_clock
)
473 if ((*a
)->s_clock
> (*b
)->s_clock
)
475 if ((*a
)->s_msg
< (*b
)->s_msg
)
484 subsort (struct smsg
**a
, struct smsg
**b
)
488 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
495 txtsort (struct smsg
**a
, struct smsg
**b
)
499 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
501 if ((*a
)->s_msg
< (*b
)->s_msg
)
507 rename_chain (struct msgs
*mp
, struct smsg
**mlist
, int msg
, int endmsg
)
510 char *newname
, oldname
[BUFSIZ
];
511 char newbuf
[PATH_MAX
+ 1];
514 nxt
= mlist
[msg
] - smsgs
; /* mlist[msg] is a ptr into smsgs */
516 old
= smsgs
[nxt
].s_msg
;
517 new = smsgs
[msg
].s_msg
;
518 strncpy (oldname
, m_name (old
), sizeof(oldname
));
519 newname
= m_name (new);
521 printf ("message %d becomes message %d\n", old
, new);
523 (void)snprintf(oldname
, sizeof (oldname
), "%s/%d", mp
->foldpath
, old
);
524 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, new);
525 ext_hook("ref-hook", oldname
, newbuf
);
527 if (rename (oldname
, newname
) == NOTOK
)
528 adios (newname
, "unable to rename %s to", oldname
);
530 copy_msg_flags (mp
, new, old
);
531 if (mp
->curmsg
== old
)
532 seq_setcur (mp
, new);
539 /* if (nxt != endmsg); */
540 /* rename_chain (mp, mlist, nxt, endmsg); */
544 rename_msgs (struct msgs
*mp
, struct smsg
**mlist
)
547 bvector_t tmpset
= bvector_create ();
548 char f1
[BUFSIZ
], tmpfil
[BUFSIZ
];
549 char newbuf
[PATH_MAX
+ 1];
552 strncpy (tmpfil
, m_name (mp
->hghmsg
+ 1), sizeof(tmpfil
));
554 for (i
= 0; i
< nmsgs
; i
++) {
555 if (! (sp
= mlist
[i
]))
556 continue; /* did this one */
560 continue; /* this one doesn't move */
563 * the guy that was msg j is about to become msg i.
564 * rename 'j' to make a hole, then recursively rename
565 * guys to fill up the hole.
567 old
= smsgs
[j
].s_msg
;
568 new = smsgs
[i
].s_msg
;
569 strncpy (f1
, m_name (old
), sizeof(f1
));
572 printf ("renaming message chain from %d to %d\n", old
, new);
575 * Run the external hook to refile the old message as the
576 * temporary message number that is off of the end of the
577 * messages in the folder.
580 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, old
);
581 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, mp
->hghmsg
+ 1);
582 ext_hook("ref-hook", f1
, newbuf
);
584 if (rename (f1
, tmpfil
) == NOTOK
)
585 adios (tmpfil
, "unable to rename %s to ", f1
);
587 get_msg_flags (mp
, tmpset
, old
);
589 rename_chain (mp
, mlist
, j
, i
);
592 * Run the external hook to refile the temporary message number
596 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, new);
597 ext_hook("ref-hook", newbuf
, f1
);
599 if (rename (tmpfil
, m_name(new)) == NOTOK
)
600 adios (m_name(new), "unable to rename %s to", tmpfil
);
602 set_msg_flags (mp
, tmpset
, new);
603 mp
->msgflags
|= SEQMOD
;
606 bvector_free (tmpset
);