]>
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.
11 #include "../sbr/m_maildir.h"
13 #define SORTM_SWITCHES \
14 X("datefield field", 0, DATESW) \
15 X("textfield field", 0, TEXTSW) \
16 X("notextfield", 0, NSUBJSW) \
17 X("subject", -3, SUBJSW) /* backward-compatibility */ \
18 X("limit days", 0, LIMSW) \
19 X("nolimit", 0, NLIMSW) \
20 X("verbose", 0, VERBSW) \
21 X("noverbose", 0, NVERBSW) \
22 X("all", 0, ALLMSGS) \
23 X("noall", 0, NALLMSGS) \
24 X("check", 0, CHECKSW) \
25 X("nocheck", 0, NCHECKSW) \
26 X("version", 0, VERSIONSW) \
27 X("help", 0, HELPSW) \
29 #define X(sw, minchars, id) id,
30 DEFINE_SWITCH_ENUM(SORTM
);
33 #define X(sw, minchars, id) { sw, minchars, id },
34 DEFINE_SWITCH_ARRAY(SORTM
, switches
);
43 static struct smsg
*smsgs
;
46 char *subjsort
= (char *) 0; /* sort on subject if != 0 */
48 int submajor
= 0; /* if true, sort on subject-major */
53 /* This keeps compiler happy on calls to qsort */
54 typedef int (*qsort_comp
) (const void *, const void *);
59 static int read_hdrs (struct msgs
*, char *);
60 static int get_fields (char *, int, struct smsg
*);
61 static int dsort (struct smsg
**, struct smsg
**);
62 static int subsort (struct smsg
**, struct smsg
**);
63 static int txtsort (struct smsg
**, struct smsg
**);
64 static void rename_chain (struct msgs
*, struct smsg
**, int, int);
65 static void rename_msgs (struct msgs
*, struct smsg
**);
69 main (int argc
, char **argv
)
72 char *cp
, *maildir
, *datesw
= NULL
;
73 char *folder
= NULL
, buf
[BUFSIZ
], **argp
;
75 struct msgs_array msgs
= { 0, 0, NULL
};
80 if (nmh_init(argv
[0], 1)) { return 1; }
82 arguments
= getarguments (invo_name
, argc
, argv
, 1);
88 while ((cp
= *argp
++)) {
90 switch (smatch (++cp
, switches
)) {
92 ambigsw (cp
, switches
);
95 adios (NULL
, "-%s unknown", cp
);
98 snprintf(buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
100 print_help (buf
, switches
, 1);
103 print_version(invo_name
);
108 adios (NULL
, "only one date field at a time");
109 if (!(datesw
= *argp
++) || *datesw
== '-')
110 adios (NULL
, "missing argument to %s", argp
[-2]);
115 adios (NULL
, "only one text field at a time");
116 if (!(subjsort
= *argp
++) || *subjsort
== '-')
117 adios (NULL
, "missing argument to %s", argp
[-2]);
121 subjsort
= "subject";
128 if (!(cp
= *argp
++) || *cp
== '-')
129 adios (NULL
, "missing argument to %s", argp
[-2]);
131 cp
++; /* skip any leading zeros */
132 if (!*cp
) { /* hit end of string */
133 submajor
++; /* sort subject-major */
136 if (!isdigit((unsigned char) *cp
) || !(datelimit
= atoi(cp
)))
137 adios (NULL
, "impossible limit %s", cp
);
138 datelimit
*= 60*60*24;
141 submajor
= 0; /* use date-major, but */
142 datelimit
= 0; /* use no limit */
167 if (*cp
== '+' || *cp
== '@') {
169 adios (NULL
, "only one folder at a time!");
171 folder
= pluspath (cp
);
173 app_msgarg(&msgs
, cp
);
176 if (!context_find ("path"))
177 free (path ("./", TFOLDER
));
180 app_msgarg(&msgs
, "all");
182 adios (NULL
, "must specify messages to sort with -noall");
188 folder
= getfolder (1);
189 maildir
= m_maildir (folder
);
191 if (chdir (maildir
) == NOTOK
)
192 adios (maildir
, "unable to change directory to");
194 /* read folder and create message structure */
195 if (!(mp
= folder_read (folder
, 1)))
196 adios (NULL
, "unable to read folder %s", folder
);
198 /* check for empty folder */
200 adios (NULL
, "no messages in %s", folder
);
202 /* parse all the message ranges/sequences and set SELECTED */
203 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
204 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
206 seq_setprev (mp
); /* set the previous sequence */
208 if ((nmsgs
= read_hdrs (mp
, datesw
)) <= 0)
209 adios (NULL
, "no messages to sort");
211 if (checksw
&& check_failed
) {
212 adios (NULL
, "errors found, no messages sorted");
216 * sort a list of pointers to our "messages to be sorted".
218 dlist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*dlist
));
219 for (i
= 0; i
< nmsgs
; i
++)
220 dlist
[i
] = &smsgs
[i
];
223 if (verbose
) { /* announce what we're doing */
226 printf ("sorting by %s\n", subjsort
);
228 printf ("sorting by %s-major %s-minor\n", subjsort
, datesw
);
230 printf ("sorting by datefield %s\n", datesw
);
233 /* first sort by date, or by subject-major, date-minor */
234 qsort ((char *) dlist
, nmsgs
, sizeof(*dlist
),
235 (qsort_comp
) (submajor
&& subjsort
? txtsort
: dsort
));
238 * if we're sorting on subject, we need another list
239 * in subject order, then a merge pass to collate the
242 if (!submajor
&& subjsort
) { /* already date sorted */
243 struct smsg
**slist
, **flist
;
244 struct smsg
***il
, **fp
, **dp
;
246 slist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*slist
));
247 memcpy((char *)slist
, (char *)dlist
, (nmsgs
+1)*sizeof(*slist
));
248 qsort((char *)slist
, nmsgs
, sizeof(*slist
), (qsort_comp
) subsort
);
251 * make an inversion list so we can quickly find
252 * the collection of messages with the same subj
253 * given a message number.
255 il
= mh_xcalloc(mp
->hghsel
+ 1, sizeof *il
);
256 for (i
= 0; i
< nmsgs
; i
++)
257 il
[slist
[i
]->s_msg
] = &slist
[i
];
259 * make up the final list, chronological but with
260 * all the same subjects grouped together.
262 flist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*flist
));
264 for (dp
= dlist
; *dp
;) {
265 struct smsg
**s
= il
[(*dp
++)->s_msg
];
267 /* see if we already did this guy */
273 * take the next message(s) if there is one,
274 * its subject isn't null and its subject
275 * is the same as this one and it's not too
278 while (*s
&& (*s
)->s_subj
[0] &&
279 strcmp((*s
)->s_subj
, s
[-1]->s_subj
) == 0 &&
281 (*s
)->s_clock
- s
[-1]->s_clock
<= datelimit
)) {
294 * At this point, dlist is a sorted array of pointers to smsg structures,
295 * each of which contains a message number.
298 rename_msgs (mp
, dlist
);
300 context_replace (pfolder
, folder
); /* update current folder */
301 seq_save (mp
); /* synchronize message sequences */
302 context_save (); /* save the context file */
303 folder_free (mp
); /* free folder/message structure */
309 read_hdrs (struct msgs
*mp
, char *datesw
)
314 smsgs
= mh_xcalloc(mp
->hghsel
- mp
->lowsel
+ 2, sizeof *smsgs
);
316 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
317 if (is_selected(mp
, msgnum
)) {
318 if (get_fields (datesw
, msgnum
, s
)) {
330 * Parse the message and get the data or subject field,
335 get_fields (char *datesw
, int msg
, struct smsg
*smsg
)
339 char *msgnam
, buf
[NMH_BUFSIZ
], nam
[NAMESZ
];
341 char *datecomp
= NULL
, *subjcomp
= NULL
;
343 m_getfld_state_t gstate
= 0;
345 if ((in
= fopen (msgnam
= m_name (msg
), "r")) == NULL
) {
346 admonish (msgnam
, "unable to read message");
349 for (compnum
= 1;;) {
350 int bufsz
= sizeof buf
;
351 switch (state
= m_getfld (&gstate
, nam
, buf
, &bufsz
, in
)) {
355 if (!strcasecmp (nam
, datesw
)) {
356 datecomp
= add (buf
, datecomp
);
357 while (state
== FLDPLUS
) {
359 state
= m_getfld (&gstate
, nam
, buf
, &bufsz
, in
);
360 datecomp
= add (buf
, datecomp
);
362 if (!subjsort
|| subjcomp
)
364 } else if (subjsort
&& !strcasecmp (nam
, subjsort
)) {
365 subjcomp
= add (buf
, subjcomp
);
366 while (state
== FLDPLUS
) {
368 state
= m_getfld (&gstate
, nam
, buf
, &bufsz
, in
);
369 subjcomp
= add (buf
, subjcomp
);
374 /* just flush this guy */
375 while (state
== FLDPLUS
) {
377 state
= m_getfld (&gstate
, nam
, buf
, &bufsz
, in
);
388 if (state
== LENERR
|| state
== FMTERR
) {
389 inform("format error in message %d (header #%d), continuing...",
399 adios (NULL
, "internal error -- you lose");
403 m_getfld_state_destroy (&gstate
);
406 * If no date component, then use the modification
407 * time of the file as its date
409 if (!datecomp
|| (tw
= dparsetime (datecomp
)) == NULL
) {
412 inform("can't parse %s field in message %d, "
413 "will use file modification time", datesw
, msg
);
414 fstat (fileno (in
), &st
);
415 smsg
->s_clock
= st
.st_mtime
;
418 smsg
->s_clock
= dmktime (tw
);
424 * try to make the subject "canonical": delete
425 * leading "re:", everything but letters & smash
426 * letters to lower case.
432 if (strcmp (subjsort
, "subject") == 0) {
434 if (! isspace((unsigned char) c
)) {
443 while ((c
= *cp
++)) {
444 if (isascii((unsigned char) c
) && isalnum((unsigned char) c
))
445 *cp2
++ = tolower((unsigned char)c
);
453 smsg
->s_subj
= subjcomp
;
465 dsort (struct smsg
**a
, struct smsg
**b
)
467 if ((*a
)->s_clock
< (*b
)->s_clock
)
469 if ((*a
)->s_clock
> (*b
)->s_clock
)
471 if ((*a
)->s_msg
< (*b
)->s_msg
)
480 subsort (struct smsg
**a
, struct smsg
**b
)
484 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
487 return (dsort (a
, b
));
491 txtsort (struct smsg
**a
, struct smsg
**b
)
495 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
497 if ((*a
)->s_msg
< (*b
)->s_msg
)
503 rename_chain (struct msgs
*mp
, struct smsg
**mlist
, int msg
, int endmsg
)
506 char *newname
, oldname
[BUFSIZ
];
507 char newbuf
[PATH_MAX
+ 1];
510 nxt
= mlist
[msg
] - smsgs
; /* mlist[msg] is a ptr into smsgs */
512 old
= smsgs
[nxt
].s_msg
;
513 new = smsgs
[msg
].s_msg
;
514 strncpy (oldname
, m_name (old
), sizeof(oldname
));
515 newname
= m_name (new);
517 printf ("message %d becomes message %d\n", old
, new);
519 (void)snprintf(oldname
, sizeof (oldname
), "%s/%d", mp
->foldpath
, old
);
520 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, new);
521 ext_hook("ref-hook", oldname
, newbuf
);
523 if (rename (oldname
, newname
) == NOTOK
)
524 adios (newname
, "unable to rename %s to", oldname
);
526 copy_msg_flags (mp
, new, old
);
527 if (mp
->curmsg
== old
)
528 seq_setcur (mp
, new);
535 /* if (nxt != endmsg); */
536 /* rename_chain (mp, mlist, nxt, endmsg); */
540 rename_msgs (struct msgs
*mp
, struct smsg
**mlist
)
543 bvector_t tmpset
= bvector_create ();
544 char f1
[BUFSIZ
], tmpfil
[BUFSIZ
];
545 char newbuf
[PATH_MAX
+ 1];
548 strncpy (tmpfil
, m_name (mp
->hghmsg
+ 1), sizeof(tmpfil
));
550 for (i
= 0; i
< nmsgs
; i
++) {
551 if (! (sp
= mlist
[i
]))
552 continue; /* did this one */
556 continue; /* this one doesn't move */
559 * the guy that was msg j is about to become msg i.
560 * rename 'j' to make a hole, then recursively rename
561 * guys to fill up the hole.
563 old
= smsgs
[j
].s_msg
;
564 new = smsgs
[i
].s_msg
;
565 strncpy (f1
, m_name (old
), sizeof(f1
));
568 printf ("renaming message chain from %d to %d\n", old
, new);
571 * Run the external hook to refile the old message as the
572 * temporary message number that is off of the end of the
573 * messages in the folder.
576 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, old
);
577 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, mp
->hghmsg
+ 1);
578 ext_hook("ref-hook", f1
, newbuf
);
580 if (rename (f1
, tmpfil
) == NOTOK
)
581 adios (tmpfil
, "unable to rename %s to ", f1
);
583 get_msg_flags (mp
, tmpset
, old
);
585 rename_chain (mp
, mlist
, j
, i
);
588 * Run the external hook to refile the temporary message number
592 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, new);
593 ext_hook("ref-hook", newbuf
, f1
);
595 if (rename (tmpfil
, m_name(new)) == NOTOK
)
596 adios (m_name(new), "unable to rename %s to", tmpfil
);
598 set_msg_flags (mp
, tmpset
, new);
599 mp
->msgflags
|= SEQMOD
;
602 bvector_free (tmpset
);