]>
diplodocus.org Git - nmh/blob - uip/sortm.c
3 * sortm.c -- sort messages in a folder by date/time
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
14 #define SORTM_SWITCHES \
15 X("datefield field", 0, DATESW) \
16 X("textfield field", 0, TEXTSW) \
17 X("notextfield", 0, NSUBJSW) \
18 X("subject", -3, SUBJSW) /* backward-compatibility */ \
19 X("limit days", 0, LIMSW) \
20 X("nolimit", 0, NLIMSW) \
21 X("verbose", 0, VERBSW) \
22 X("noverbose", 0, NVERBSW) \
23 X("all", 0, ALLMSGS) \
24 X("noall", 0, NALLMSGS) \
25 X("check", 0, CHECKSW) \
26 X("nocheck", 0, NCHECKSW) \
27 X("version", 0, VERSIONSW) \
28 X("help", 0, HELPSW) \
30 #define X(sw, minchars, id) id,
31 DEFINE_SWITCH_ENUM(SORTM
);
34 #define X(sw, minchars, id) { sw, minchars, id },
35 DEFINE_SWITCH_ARRAY(SORTM
, switches
);
44 static struct smsg
*smsgs
;
47 char *subjsort
= (char *) 0; /* sort on subject if != 0 */
49 int submajor
= 0; /* if true, sort on subject-major */
54 /* This keeps compiler happy on calls to qsort */
55 typedef int (*qsort_comp
) (const void *, const void *);
60 static int read_hdrs (struct msgs
*, char *);
61 static int get_fields (char *, int, struct smsg
*);
62 static int dsort (struct smsg
**, struct smsg
**);
63 static int subsort (struct smsg
**, struct smsg
**);
64 static int txtsort (struct smsg
**, struct smsg
**);
65 static void rename_chain (struct msgs
*, struct smsg
**, int, int);
66 static void rename_msgs (struct msgs
*, struct smsg
**);
70 main (int argc
, char **argv
)
73 char *cp
, *maildir
, *datesw
= NULL
;
74 char *folder
= NULL
, buf
[BUFSIZ
], **argp
;
76 struct msgs_array msgs
= { 0, 0, NULL
};
81 setlocale(LC_ALL
, "");
82 invo_name
= r1bindex (argv
[0], '/');
84 /* read user profile/context */
87 arguments
= getarguments (invo_name
, argc
, argv
, 1);
93 while ((cp
= *argp
++)) {
95 switch (smatch (++cp
, switches
)) {
97 ambigsw (cp
, switches
);
100 adios (NULL
, "-%s unknown", cp
);
103 snprintf(buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
105 print_help (buf
, switches
, 1);
108 print_version(invo_name
);
113 adios (NULL
, "only one date field at a time");
114 if (!(datesw
= *argp
++) || *datesw
== '-')
115 adios (NULL
, "missing argument to %s", argp
[-2]);
120 adios (NULL
, "only one text field at a time");
121 if (!(subjsort
= *argp
++) || *subjsort
== '-')
122 adios (NULL
, "missing argument to %s", argp
[-2]);
126 subjsort
= "subject";
129 subjsort
= (char *)0;
133 if (!(cp
= *argp
++) || *cp
== '-')
134 adios (NULL
, "missing argument to %s", argp
[-2]);
136 cp
++; /* skip any leading zeros */
137 if (!*cp
) { /* hit end of string */
138 submajor
++; /* sort subject-major */
141 if (!isdigit((unsigned char) *cp
) || !(datelimit
= atoi(cp
)))
142 adios (NULL
, "impossible limit %s", cp
);
143 datelimit
*= 60*60*24;
146 submajor
= 0; /* use date-major, but */
147 datelimit
= 0; /* use no limit */
172 if (*cp
== '+' || *cp
== '@') {
174 adios (NULL
, "only one folder at a time!");
176 folder
= pluspath (cp
);
178 app_msgarg(&msgs
, cp
);
181 if (!context_find ("path"))
182 free (path ("./", TFOLDER
));
185 app_msgarg(&msgs
, "all");
187 adios (NULL
, "must specify messages to sort with -noall");
193 folder
= getfolder (1);
194 maildir
= m_maildir (folder
);
196 if (chdir (maildir
) == NOTOK
)
197 adios (maildir
, "unable to change directory to");
199 /* read folder and create message structure */
200 if (!(mp
= folder_read (folder
, 1)))
201 adios (NULL
, "unable to read folder %s", folder
);
203 /* check for empty folder */
205 adios (NULL
, "no messages in %s", folder
);
207 /* parse all the message ranges/sequences and set SELECTED */
208 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
209 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
211 seq_setprev (mp
); /* set the previous sequence */
213 if ((nmsgs
= read_hdrs (mp
, datesw
)) <= 0)
214 adios (NULL
, "no messages to sort");
216 if (checksw
&& check_failed
) {
217 adios (NULL
, "errors found, no messages sorted");
221 * sort a list of pointers to our "messages to be sorted".
223 dlist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*dlist
));
224 for (i
= 0; i
< nmsgs
; i
++)
225 dlist
[i
] = &smsgs
[i
];
228 if (verbose
) { /* announce what we're doing */
231 printf ("sorting by %s\n", subjsort
);
233 printf ("sorting by %s-major %s-minor\n", subjsort
, datesw
);
235 printf ("sorting by datefield %s\n", datesw
);
238 /* first sort by date, or by subject-major, date-minor */
239 qsort ((char *) dlist
, nmsgs
, sizeof(*dlist
),
240 (qsort_comp
) (submajor
&& subjsort
? txtsort
: dsort
));
243 * if we're sorting on subject, we need another list
244 * in subject order, then a merge pass to collate the
247 if (!submajor
&& subjsort
) { /* already date sorted */
248 struct smsg
**slist
, **flist
;
249 register struct smsg
***il
, **fp
, **dp
;
251 slist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*slist
));
252 memcpy((char *)slist
, (char *)dlist
, (nmsgs
+1)*sizeof(*slist
));
253 qsort((char *)slist
, nmsgs
, sizeof(*slist
), (qsort_comp
) subsort
);
256 * make an inversion list so we can quickly find
257 * the collection of messages with the same subj
258 * given a message number.
260 il
= (struct smsg
***) calloc (mp
->hghsel
+1, sizeof(*il
));
262 adios (NULL
, "couldn't allocate msg list");
263 for (i
= 0; i
< nmsgs
; i
++)
264 il
[slist
[i
]->s_msg
] = &slist
[i
];
266 * make up the final list, chronological but with
267 * all the same subjects grouped together.
269 flist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*flist
));
271 for (dp
= dlist
; *dp
;) {
272 register struct smsg
**s
= il
[(*dp
++)->s_msg
];
274 /* see if we already did this guy */
280 * take the next message(s) if there is one,
281 * its subject isn't null and its subject
282 * is the same as this one and it's not too
285 while (*s
&& (*s
)->s_subj
[0] &&
286 strcmp((*s
)->s_subj
, s
[-1]->s_subj
) == 0 &&
288 (*s
)->s_clock
- s
[-1]->s_clock
<= datelimit
)) {
300 * At this point, dlist is a sorted array of pointers to smsg structures,
301 * each of which contains a message number.
304 rename_msgs (mp
, dlist
);
306 context_replace (pfolder
, folder
); /* update current folder */
307 seq_save (mp
); /* synchronize message sequences */
308 context_save (); /* save the context file */
309 folder_free (mp
); /* free folder/message structure */
315 read_hdrs (struct msgs
*mp
, char *datesw
)
319 register struct smsg
*s
;
321 twscopy (&tb
, dlocaltimenow ());
323 smsgs
= (struct smsg
*)
324 calloc ((size_t) (mp
->hghsel
- mp
->lowsel
+ 2),
327 adios (NULL
, "unable to allocate sort storage");
330 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
331 if (is_selected(mp
, msgnum
)) {
332 if (get_fields (datesw
, msgnum
, s
)) {
344 * Parse the message and get the data or subject field,
349 get_fields (char *datesw
, int msg
, struct smsg
*smsg
)
353 char *msgnam
, buf
[BUFSIZ
], nam
[NAMESZ
];
354 register struct tws
*tw
;
355 register char *datecomp
= NULL
, *subjcomp
= NULL
;
357 m_getfld_state_t gstate
= 0;
359 if ((in
= fopen (msgnam
= m_name (msg
), "r")) == NULL
) {
360 admonish (msgnam
, "unable to read message");
363 for (compnum
= 1;;) {
364 int bufsz
= sizeof buf
;
365 switch (state
= m_getfld (&gstate
, nam
, buf
, &bufsz
, in
)) {
369 if (!strcasecmp (nam
, datesw
)) {
370 datecomp
= add (buf
, datecomp
);
371 while (state
== FLDPLUS
) {
373 state
= m_getfld (&gstate
, nam
, buf
, &bufsz
, in
);
374 datecomp
= add (buf
, datecomp
);
376 if (!subjsort
|| subjcomp
)
378 } else if (subjsort
&& !strcasecmp (nam
, subjsort
)) {
379 subjcomp
= add (buf
, subjcomp
);
380 while (state
== FLDPLUS
) {
382 state
= m_getfld (&gstate
, nam
, buf
, &bufsz
, in
);
383 subjcomp
= add (buf
, subjcomp
);
388 /* just flush this guy */
389 while (state
== FLDPLUS
) {
391 state
= m_getfld (&gstate
, nam
, buf
, &bufsz
, in
);
402 if (state
== LENERR
|| state
== FMTERR
) {
403 admonish (NULL
, "format error in message %d (header #%d)",
415 adios (NULL
, "internal error -- you lose");
419 m_getfld_state_destroy (&gstate
);
422 * If no date component, then use the modification
423 * time of the file as its date
425 if (!datecomp
|| (tw
= dparsetime (datecomp
)) == NULL
) {
429 "can't parse %s field in message %d, "
430 "will use file modification time",
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.
446 register char *cp
, *cp2
, c
;
450 if (strcmp (subjsort
, "subject") == 0) {
452 if (! isspace((unsigned char) c
)) {
462 while ((c
= *cp
++)) {
463 if (isascii((unsigned char) c
) && isalnum((unsigned char) c
))
464 *cp2
++ = isupper((unsigned char) c
) ?
465 tolower((unsigned char) c
) : c
;
473 smsg
->s_subj
= subjcomp
;
486 dsort (struct smsg
**a
, struct smsg
**b
)
488 if ((*a
)->s_clock
< (*b
)->s_clock
)
490 else if ((*a
)->s_clock
> (*b
)->s_clock
)
492 else if ((*a
)->s_msg
< (*b
)->s_msg
)
502 subsort (struct smsg
**a
, struct smsg
**b
)
506 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
509 return (dsort (a
, b
));
513 txtsort (struct smsg
**a
, struct smsg
**b
)
517 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
519 else 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 */
534 mlist
[msg
] = (struct smsg
*)0;
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 (0);
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 temorary 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
);