]>
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
};
82 setlocale(LC_ALL
, "");
84 invo_name
= r1bindex (argv
[0], '/');
86 /* read user profile/context */
89 arguments
= getarguments (invo_name
, argc
, argv
, 1);
95 while ((cp
= *argp
++)) {
97 switch (smatch (++cp
, switches
)) {
99 ambigsw (cp
, switches
);
102 adios (NULL
, "-%s unknown", cp
);
105 snprintf(buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
107 print_help (buf
, switches
, 1);
110 print_version(invo_name
);
115 adios (NULL
, "only one date field at a time");
116 if (!(datesw
= *argp
++) || *datesw
== '-')
117 adios (NULL
, "missing argument to %s", argp
[-2]);
122 adios (NULL
, "only one text field at a time");
123 if (!(subjsort
= *argp
++) || *subjsort
== '-')
124 adios (NULL
, "missing argument to %s", argp
[-2]);
128 subjsort
= "subject";
131 subjsort
= (char *)0;
135 if (!(cp
= *argp
++) || *cp
== '-')
136 adios (NULL
, "missing argument to %s", argp
[-2]);
138 cp
++; /* skip any leading zeros */
139 if (!*cp
) { /* hit end of string */
140 submajor
++; /* sort subject-major */
143 if (!isdigit((unsigned char) *cp
) || !(datelimit
= atoi(cp
)))
144 adios (NULL
, "impossible limit %s", cp
);
145 datelimit
*= 60*60*24;
148 submajor
= 0; /* use date-major, but */
149 datelimit
= 0; /* use no limit */
174 if (*cp
== '+' || *cp
== '@') {
176 adios (NULL
, "only one folder at a time!");
178 folder
= pluspath (cp
);
180 app_msgarg(&msgs
, cp
);
183 if (!context_find ("path"))
184 free (path ("./", TFOLDER
));
187 app_msgarg(&msgs
, "all");
189 adios (NULL
, "must specify messages to sort with -noall");
195 folder
= getfolder (1);
196 maildir
= m_maildir (folder
);
198 if (chdir (maildir
) == NOTOK
)
199 adios (maildir
, "unable to change directory to");
201 /* read folder and create message structure */
202 if (!(mp
= folder_read (folder
)))
203 adios (NULL
, "unable to read folder %s", folder
);
205 /* check for empty folder */
207 adios (NULL
, "no messages in %s", folder
);
209 /* parse all the message ranges/sequences and set SELECTED */
210 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
211 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
213 seq_setprev (mp
); /* set the previous sequence */
215 if ((nmsgs
= read_hdrs (mp
, datesw
)) <= 0)
216 adios (NULL
, "no messages to sort");
218 if (checksw
&& check_failed
) {
219 adios (NULL
, "errors found, no messages sorted");
223 * sort a list of pointers to our "messages to be sorted".
225 dlist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*dlist
));
226 for (i
= 0; i
< nmsgs
; i
++)
227 dlist
[i
] = &smsgs
[i
];
230 if (verbose
) { /* announce what we're doing */
233 printf ("sorting by %s\n", subjsort
);
235 printf ("sorting by %s-major %s-minor\n", subjsort
, datesw
);
237 printf ("sorting by datefield %s\n", datesw
);
240 /* first sort by date, or by subject-major, date-minor */
241 qsort ((char *) dlist
, nmsgs
, sizeof(*dlist
),
242 (qsort_comp
) (submajor
&& subjsort
? txtsort
: dsort
));
245 * if we're sorting on subject, we need another list
246 * in subject order, then a merge pass to collate the
249 if (!submajor
&& subjsort
) { /* already date sorted */
250 struct smsg
**slist
, **flist
;
251 register struct smsg
***il
, **fp
, **dp
;
253 slist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*slist
));
254 memcpy((char *)slist
, (char *)dlist
, (nmsgs
+1)*sizeof(*slist
));
255 qsort((char *)slist
, nmsgs
, sizeof(*slist
), (qsort_comp
) subsort
);
258 * make an inversion list so we can quickly find
259 * the collection of messages with the same subj
260 * given a message number.
262 il
= (struct smsg
***) calloc (mp
->hghsel
+1, sizeof(*il
));
264 adios (NULL
, "couldn't allocate msg list");
265 for (i
= 0; i
< nmsgs
; i
++)
266 il
[slist
[i
]->s_msg
] = &slist
[i
];
268 * make up the final list, chronological but with
269 * all the same subjects grouped together.
271 flist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*flist
));
273 for (dp
= dlist
; *dp
;) {
274 register struct smsg
**s
= il
[(*dp
++)->s_msg
];
276 /* see if we already did this guy */
282 * take the next message(s) if there is one,
283 * its subject isn't null and its subject
284 * is the same as this one and it's not too
287 while (*s
&& (*s
)->s_subj
[0] &&
288 strcmp((*s
)->s_subj
, s
[-1]->s_subj
) == 0 &&
290 (*s
)->s_clock
- s
[-1]->s_clock
<= datelimit
)) {
302 * At this point, dlist is a sorted array of pointers to smsg structures,
303 * each of which contains a message number.
306 rename_msgs (mp
, dlist
);
308 context_replace (pfolder
, folder
); /* update current folder */
309 seq_save (mp
); /* synchronize message sequences */
310 context_save (); /* save the context file */
311 folder_free (mp
); /* free folder/message structure */
317 read_hdrs (struct msgs
*mp
, char *datesw
)
321 register struct smsg
*s
;
323 twscopy (&tb
, dlocaltimenow ());
325 smsgs
= (struct smsg
*)
326 calloc ((size_t) (mp
->hghsel
- mp
->lowsel
+ 2),
329 adios (NULL
, "unable to allocate sort storage");
332 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
333 if (is_selected(mp
, msgnum
)) {
334 if (get_fields (datesw
, msgnum
, s
)) {
346 * Parse the message and get the data or subject field,
351 get_fields (char *datesw
, int msg
, struct smsg
*smsg
)
355 char *msgnam
, buf
[BUFSIZ
], nam
[NAMESZ
];
356 register struct tws
*tw
;
357 register char *datecomp
= NULL
, *subjcomp
= NULL
;
359 m_getfld_state_t gstate
= 0;
361 if ((in
= fopen (msgnam
= m_name (msg
), "r")) == NULL
) {
362 admonish (msgnam
, "unable to read message");
365 for (compnum
= 1;;) {
366 int bufsz
= sizeof buf
;
367 switch (state
= m_getfld (&gstate
, nam
, buf
, &bufsz
, in
)) {
371 if (!mh_strcasecmp (nam
, datesw
)) {
372 datecomp
= add (buf
, datecomp
);
373 while (state
== FLDPLUS
) {
375 state
= m_getfld (&gstate
, nam
, buf
, &bufsz
, in
);
376 datecomp
= add (buf
, datecomp
);
378 if (!subjsort
|| subjcomp
)
380 } else if (subjsort
&& !mh_strcasecmp (nam
, subjsort
)) {
381 subjcomp
= add (buf
, subjcomp
);
382 while (state
== FLDPLUS
) {
384 state
= m_getfld (&gstate
, nam
, buf
, &bufsz
, in
);
385 subjcomp
= add (buf
, subjcomp
);
390 /* just flush this guy */
391 while (state
== FLDPLUS
) {
393 state
= m_getfld (&gstate
, nam
, buf
, &bufsz
, in
);
404 if (state
== LENERR
|| state
== FMTERR
) {
405 admonish (NULL
, "format error in message %d (header #%d)",
417 adios (NULL
, "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
) {
431 "can't parse %s field in message %d, "
432 "will use file modification time",
434 fstat (fileno (in
), &st
);
435 smsg
->s_clock
= st
.st_mtime
;
438 smsg
->s_clock
= dmktime (tw
);
444 * try to make the subject "canonical": delete
445 * leading "re:", everything but letters & smash
446 * letters to lower case.
448 register char *cp
, *cp2
, c
;
452 if (strcmp (subjsort
, "subject") == 0) {
454 if (! isspace((unsigned char) c
)) {
464 while ((c
= *cp
++)) {
465 if (isascii((unsigned char) c
) && isalnum((unsigned char) c
))
466 *cp2
++ = isupper((unsigned char) c
) ?
467 tolower((unsigned char) c
) : c
;
475 smsg
->s_subj
= subjcomp
;
488 dsort (struct smsg
**a
, struct smsg
**b
)
490 if ((*a
)->s_clock
< (*b
)->s_clock
)
492 else if ((*a
)->s_clock
> (*b
)->s_clock
)
494 else if ((*a
)->s_msg
< (*b
)->s_msg
)
504 subsort (struct smsg
**a
, struct smsg
**b
)
508 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
511 return (dsort (a
, b
));
515 txtsort (struct smsg
**a
, struct smsg
**b
)
519 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
521 else if ((*a
)->s_msg
< (*b
)->s_msg
)
528 rename_chain (struct msgs
*mp
, struct smsg
**mlist
, int msg
, int endmsg
)
531 char *newname
, oldname
[BUFSIZ
];
532 char newbuf
[PATH_MAX
+ 1];
535 nxt
= mlist
[msg
] - smsgs
; /* mlist[msg] is a ptr into smsgs */
536 mlist
[msg
] = (struct smsg
*)0;
537 old
= smsgs
[nxt
].s_msg
;
538 new = smsgs
[msg
].s_msg
;
539 strncpy (oldname
, m_name (old
), sizeof(oldname
));
540 newname
= m_name (new);
542 printf ("message %d becomes message %d\n", old
, new);
544 (void)snprintf(oldname
, sizeof (oldname
), "%s/%d", mp
->foldpath
, old
);
545 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, new);
546 ext_hook("ref-hook", oldname
, newbuf
);
548 if (rename (oldname
, newname
) == NOTOK
)
549 adios (newname
, "unable to rename %s to", oldname
);
551 copy_msg_flags (mp
, new, old
);
552 if (mp
->curmsg
== old
)
553 seq_setcur (mp
, new);
560 /* if (nxt != endmsg); */
561 /* rename_chain (mp, mlist, nxt, endmsg); */
565 rename_msgs (struct msgs
*mp
, struct smsg
**mlist
)
569 char f1
[BUFSIZ
], tmpfil
[BUFSIZ
];
570 char newbuf
[PATH_MAX
+ 1];
573 strncpy (tmpfil
, m_name (mp
->hghmsg
+ 1), sizeof(tmpfil
));
575 for (i
= 0; i
< nmsgs
; i
++) {
576 if (! (sp
= mlist
[i
]))
577 continue; /* did this one */
581 continue; /* this one doesn't move */
584 * the guy that was msg j is about to become msg i.
585 * rename 'j' to make a hole, then recursively rename
586 * guys to fill up the hole.
588 old
= smsgs
[j
].s_msg
;
589 new = smsgs
[i
].s_msg
;
590 strncpy (f1
, m_name (old
), sizeof(f1
));
593 printf ("renaming message chain from %d to %d\n", old
, new);
596 * Run the external hook to refile the old message as the
597 * temporary message number that is off of the end of the
598 * messages in the folder.
601 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, old
);
602 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, mp
->hghmsg
+ 1);
603 ext_hook("ref-hook", f1
, newbuf
);
605 if (rename (f1
, tmpfil
) == NOTOK
)
606 adios (tmpfil
, "unable to rename %s to ", f1
);
608 get_msg_flags (mp
, &tmpset
, old
);
610 rename_chain (mp
, mlist
, j
, i
);
613 * Run the external hook to refile the temorary message number
617 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, new);
618 ext_hook("ref-hook", newbuf
, f1
);
620 if (rename (tmpfil
, m_name(new)) == NOTOK
)
621 adios (m_name(new), "unable to rename %s to", tmpfil
);
623 set_msg_flags (mp
, &tmpset
, new);
624 mp
->msgflags
|= SEQMOD
;