]>
diplodocus.org Git - nmh/blob - uip/sortm.c
b44c3fd6cbf18745f71bc8d1c31cf3a45654fc28
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.
12 #define SORTM_SWITCHES \
13 X("datefield field", 0, DATESW) \
14 X("textfield field", 0, TEXTSW) \
15 X("notextfield", 0, NSUBJSW) \
16 X("subject", -3, SUBJSW) /* backward-compatibility */ \
17 X("limit days", 0, LIMSW) \
18 X("nolimit", 0, NLIMSW) \
19 X("verbose", 0, VERBSW) \
20 X("noverbose", 0, NVERBSW) \
21 X("all", 0, ALLMSGS) \
22 X("noall", 0, NALLMSGS) \
23 X("check", 0, CHECKSW) \
24 X("nocheck", 0, NCHECKSW) \
25 X("version", 0, VERSIONSW) \
26 X("help", 0, HELPSW) \
28 #define X(sw, minchars, id) id,
29 DEFINE_SWITCH_ENUM(SORTM
);
32 #define X(sw, minchars, id) { sw, minchars, id },
33 DEFINE_SWITCH_ARRAY(SORTM
, switches
);
42 static struct smsg
*smsgs
;
45 char *subjsort
= (char *) 0; /* sort on subject if != 0 */
47 int submajor
= 0; /* if true, sort on subject-major */
52 /* This keeps compiler happy on calls to qsort */
53 typedef int (*qsort_comp
) (const void *, const void *);
58 static int read_hdrs (struct msgs
*, char *);
59 static int get_fields (char *, int, struct smsg
*);
60 static int dsort (struct smsg
**, struct smsg
**);
61 static int subsort (struct smsg
**, struct smsg
**);
62 static int txtsort (struct smsg
**, struct smsg
**);
63 static void rename_chain (struct msgs
*, struct smsg
**, int, int);
64 static void rename_msgs (struct msgs
*, struct smsg
**);
68 main (int argc
, char **argv
)
71 char *cp
, *maildir
, *datesw
= NULL
;
72 char *folder
= NULL
, buf
[BUFSIZ
], **argp
;
74 struct msgs_array msgs
= { 0, 0, NULL
};
79 if (nmh_init(argv
[0], 1)) { return 1; }
81 arguments
= getarguments (invo_name
, argc
, argv
, 1);
87 while ((cp
= *argp
++)) {
89 switch (smatch (++cp
, switches
)) {
91 ambigsw (cp
, switches
);
94 adios (NULL
, "-%s unknown", cp
);
97 snprintf(buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
99 print_help (buf
, switches
, 1);
102 print_version(invo_name
);
107 adios (NULL
, "only one date field at a time");
108 if (!(datesw
= *argp
++) || *datesw
== '-')
109 adios (NULL
, "missing argument to %s", argp
[-2]);
114 adios (NULL
, "only one text field at a time");
115 if (!(subjsort
= *argp
++) || *subjsort
== '-')
116 adios (NULL
, "missing argument to %s", argp
[-2]);
120 subjsort
= "subject";
127 if (!(cp
= *argp
++) || *cp
== '-')
128 adios (NULL
, "missing argument to %s", argp
[-2]);
130 cp
++; /* skip any leading zeros */
131 if (!*cp
) { /* hit end of string */
132 submajor
++; /* sort subject-major */
135 if (!isdigit((unsigned char) *cp
) || !(datelimit
= atoi(cp
)))
136 adios (NULL
, "impossible limit %s", cp
);
137 datelimit
*= 60*60*24;
140 submajor
= 0; /* use date-major, but */
141 datelimit
= 0; /* use no limit */
166 if (*cp
== '+' || *cp
== '@') {
168 adios (NULL
, "only one folder at a time!");
170 folder
= pluspath (cp
);
172 app_msgarg(&msgs
, cp
);
175 if (!context_find ("path"))
176 free (path ("./", TFOLDER
));
179 app_msgarg(&msgs
, "all");
181 adios (NULL
, "must specify messages to sort with -noall");
187 folder
= getfolder (1);
188 maildir
= m_maildir (folder
);
190 if (chdir (maildir
) == NOTOK
)
191 adios (maildir
, "unable to change directory to");
193 /* read folder and create message structure */
194 if (!(mp
= folder_read (folder
, 1)))
195 adios (NULL
, "unable to read folder %s", folder
);
197 /* check for empty folder */
199 adios (NULL
, "no messages in %s", folder
);
201 /* parse all the message ranges/sequences and set SELECTED */
202 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
203 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
205 seq_setprev (mp
); /* set the previous sequence */
207 if ((nmsgs
= read_hdrs (mp
, datesw
)) <= 0)
208 adios (NULL
, "no messages to sort");
210 if (checksw
&& check_failed
) {
211 adios (NULL
, "errors found, no messages sorted");
215 * sort a list of pointers to our "messages to be sorted".
217 dlist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*dlist
));
218 for (i
= 0; i
< nmsgs
; i
++)
219 dlist
[i
] = &smsgs
[i
];
222 if (verbose
) { /* announce what we're doing */
225 printf ("sorting by %s\n", subjsort
);
227 printf ("sorting by %s-major %s-minor\n", subjsort
, datesw
);
229 printf ("sorting by datefield %s\n", datesw
);
232 /* first sort by date, or by subject-major, date-minor */
233 qsort ((char *) dlist
, nmsgs
, sizeof(*dlist
),
234 (qsort_comp
) (submajor
&& subjsort
? txtsort
: dsort
));
237 * if we're sorting on subject, we need another list
238 * in subject order, then a merge pass to collate the
241 if (!submajor
&& subjsort
) { /* already date sorted */
242 struct smsg
**slist
, **flist
;
243 struct smsg
***il
, **fp
, **dp
;
245 slist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*slist
));
246 memcpy((char *)slist
, (char *)dlist
, (nmsgs
+1)*sizeof(*slist
));
247 qsort((char *)slist
, nmsgs
, sizeof(*slist
), (qsort_comp
) subsort
);
250 * make an inversion list so we can quickly find
251 * the collection of messages with the same subj
252 * given a message number.
254 il
= mh_xcalloc(mp
->hghsel
+ 1, sizeof *il
);
255 for (i
= 0; i
< nmsgs
; i
++)
256 il
[slist
[i
]->s_msg
] = &slist
[i
];
258 * make up the final list, chronological but with
259 * all the same subjects grouped together.
261 flist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*flist
));
263 for (dp
= dlist
; *dp
;) {
264 struct smsg
**s
= il
[(*dp
++)->s_msg
];
266 /* see if we already did this guy */
272 * take the next message(s) if there is one,
273 * its subject isn't null and its subject
274 * is the same as this one and it's not too
277 while (*s
&& (*s
)->s_subj
[0] &&
278 strcmp((*s
)->s_subj
, s
[-1]->s_subj
) == 0 &&
280 (*s
)->s_clock
- s
[-1]->s_clock
<= datelimit
)) {
293 * At this point, dlist is a sorted array of pointers to smsg structures,
294 * each of which contains a message number.
297 rename_msgs (mp
, dlist
);
299 context_replace (pfolder
, folder
); /* update current folder */
300 seq_save (mp
); /* synchronize message sequences */
301 context_save (); /* save the context file */
302 folder_free (mp
); /* free folder/message structure */
308 read_hdrs (struct msgs
*mp
, char *datesw
)
314 twscopy (&tb
, dlocaltimenow ());
316 smsgs
= mh_xcalloc(mp
->hghsel
- mp
->lowsel
+ 2, sizeof *smsgs
);
318 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
319 if (is_selected(mp
, msgnum
)) {
320 if (get_fields (datesw
, msgnum
, s
)) {
332 * Parse the message and get the data or subject field,
337 get_fields (char *datesw
, int msg
, struct smsg
*smsg
)
341 char *msgnam
, buf
[BUFSIZ
], nam
[NAMESZ
];
343 char *datecomp
= NULL
, *subjcomp
= NULL
;
345 m_getfld_state_t gstate
= 0;
347 if ((in
= fopen (msgnam
= m_name (msg
), "r")) == NULL
) {
348 admonish (msgnam
, "unable to read message");
351 for (compnum
= 1;;) {
352 int bufsz
= sizeof buf
;
353 switch (state
= m_getfld (&gstate
, nam
, buf
, &bufsz
, in
)) {
357 if (!strcasecmp (nam
, datesw
)) {
358 datecomp
= add (buf
, datecomp
);
359 while (state
== FLDPLUS
) {
361 state
= m_getfld (&gstate
, nam
, buf
, &bufsz
, in
);
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_getfld (&gstate
, nam
, buf
, &bufsz
, in
);
371 subjcomp
= add (buf
, subjcomp
);
376 /* just flush this guy */
377 while (state
== FLDPLUS
) {
379 state
= m_getfld (&gstate
, nam
, buf
, &bufsz
, in
);
390 if (state
== LENERR
|| state
== FMTERR
) {
391 inform("format error in message %d (header #%d), continuing...",
401 adios (NULL
, "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
)) {
446 while ((c
= *cp
++)) {
447 if (isascii((unsigned char) c
) && isalnum((unsigned char) c
))
448 *cp2
++ = tolower((unsigned char)c
);
456 smsg
->s_subj
= subjcomp
;
468 dsort (struct smsg
**a
, struct smsg
**b
)
470 if ((*a
)->s_clock
< (*b
)->s_clock
)
472 if ((*a
)->s_clock
> (*b
)->s_clock
)
474 if ((*a
)->s_msg
< (*b
)->s_msg
)
483 subsort (struct smsg
**a
, struct smsg
**b
)
487 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
490 return (dsort (a
, b
));
494 txtsort (struct smsg
**a
, struct smsg
**b
)
498 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
500 if ((*a
)->s_msg
< (*b
)->s_msg
)
506 rename_chain (struct msgs
*mp
, struct smsg
**mlist
, int msg
, int endmsg
)
509 char *newname
, oldname
[BUFSIZ
];
510 char newbuf
[PATH_MAX
+ 1];
513 nxt
= mlist
[msg
] - smsgs
; /* mlist[msg] is a ptr into smsgs */
515 old
= smsgs
[nxt
].s_msg
;
516 new = smsgs
[msg
].s_msg
;
517 strncpy (oldname
, m_name (old
), sizeof(oldname
));
518 newname
= m_name (new);
520 printf ("message %d becomes message %d\n", old
, new);
522 (void)snprintf(oldname
, sizeof (oldname
), "%s/%d", mp
->foldpath
, old
);
523 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, new);
524 ext_hook("ref-hook", oldname
, newbuf
);
526 if (rename (oldname
, newname
) == NOTOK
)
527 adios (newname
, "unable to rename %s to", oldname
);
529 copy_msg_flags (mp
, new, old
);
530 if (mp
->curmsg
== old
)
531 seq_setcur (mp
, new);
538 /* if (nxt != endmsg); */
539 /* rename_chain (mp, mlist, nxt, endmsg); */
543 rename_msgs (struct msgs
*mp
, struct smsg
**mlist
)
546 bvector_t tmpset
= bvector_create (0);
547 char f1
[BUFSIZ
], tmpfil
[BUFSIZ
];
548 char newbuf
[PATH_MAX
+ 1];
551 strncpy (tmpfil
, m_name (mp
->hghmsg
+ 1), sizeof(tmpfil
));
553 for (i
= 0; i
< nmsgs
; i
++) {
554 if (! (sp
= mlist
[i
]))
555 continue; /* did this one */
559 continue; /* this one doesn't move */
562 * the guy that was msg j is about to become msg i.
563 * rename 'j' to make a hole, then recursively rename
564 * guys to fill up the hole.
566 old
= smsgs
[j
].s_msg
;
567 new = smsgs
[i
].s_msg
;
568 strncpy (f1
, m_name (old
), sizeof(f1
));
571 printf ("renaming message chain from %d to %d\n", old
, new);
574 * Run the external hook to refile the old message as the
575 * temporary message number that is off of the end of the
576 * messages in the folder.
579 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, old
);
580 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, mp
->hghmsg
+ 1);
581 ext_hook("ref-hook", f1
, newbuf
);
583 if (rename (f1
, tmpfil
) == NOTOK
)
584 adios (tmpfil
, "unable to rename %s to ", f1
);
586 get_msg_flags (mp
, tmpset
, old
);
588 rename_chain (mp
, mlist
, j
, i
);
591 * Run the external hook to refile the temorary message number
595 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, new);
596 ext_hook("ref-hook", newbuf
, f1
);
598 if (rename (tmpfil
, m_name(new)) == NOTOK
)
599 adios (m_name(new), "unable to rename %s to", tmpfil
);
601 set_msg_flags (mp
, tmpset
, new);
602 mp
->msgflags
|= SEQMOD
;
605 bvector_free (tmpset
);