]>
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 static struct swit switches
[] = {
16 { "datefield field", 0 },
18 { "textfield field", 0 },
22 { "subject", -3 }, /* backward-compatibility */
52 static struct smsg
*smsgs
;
55 char *subjsort
= (char *) 0; /* sort on subject if != 0 */
57 int submajor
= 0; /* if true, sort on subject-major */
62 /* This keeps compiler happy on calls to qsort */
63 typedef int (*qsort_comp
) (const void *, const void *);
68 static int read_hdrs (struct msgs
*, char *);
69 static int get_fields (char *, int, struct smsg
*);
70 static int dsort (struct smsg
**, struct smsg
**);
71 static int subsort (struct smsg
**, struct smsg
**);
72 static int txtsort (struct smsg
**, struct smsg
**);
73 static void rename_chain (struct msgs
*, struct smsg
**, int, int);
74 static void rename_msgs (struct msgs
*, struct smsg
**);
78 main (int argc
, char **argv
)
82 char *maildir
, *datesw
= NULL
;
83 char *folder
= NULL
, buf
[BUFSIZ
], **argp
;
85 struct msgs_array msgs
= { 0, 0, NULL
};
91 setlocale(LC_ALL
, "");
93 invo_name
= r1bindex (argv
[0], '/');
95 /* read user profile/context */
98 arguments
= getarguments (invo_name
, argc
, argv
, 1);
104 while ((cp
= *argp
++)) {
106 switch (smatch (++cp
, switches
)) {
108 ambigsw (cp
, switches
);
111 adios (NULL
, "-%s unknown", cp
);
114 snprintf(buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
116 print_help (buf
, switches
, 1);
119 print_version(invo_name
);
124 adios (NULL
, "only one date field at a time");
125 if (!(datesw
= *argp
++) || *datesw
== '-')
126 adios (NULL
, "missing argument to %s", argp
[-2]);
131 adios (NULL
, "only one text field at a time");
132 if (!(subjsort
= *argp
++) || *subjsort
== '-')
133 adios (NULL
, "missing argument to %s", argp
[-2]);
137 subjsort
= "subject";
140 subjsort
= (char *)0;
144 if (!(cp
= *argp
++) || *cp
== '-')
145 adios (NULL
, "missing argument to %s", argp
[-2]);
147 cp
++; /* skip any leading zeros */
148 if (!*cp
) { /* hit end of string */
149 submajor
++; /* sort subject-major */
152 if (!isdigit(*cp
) || !(datelimit
= atoi(cp
)))
153 adios (NULL
, "impossible limit %s", cp
);
154 datelimit
*= 60*60*24;
157 submajor
= 0; /* use date-major, but */
158 datelimit
= 0; /* use no limit */
183 if (*cp
== '+' || *cp
== '@') {
185 adios (NULL
, "only one folder at a time!");
187 folder
= pluspath (cp
);
189 app_msgarg(&msgs
, cp
);
192 if (!context_find ("path"))
193 free (path ("./", TFOLDER
));
196 app_msgarg(&msgs
, "all");
198 adios (NULL
, "must specify messages to sort with -noall");
204 folder
= getfolder (1);
205 maildir
= m_maildir (folder
);
207 if (chdir (maildir
) == NOTOK
)
208 adios (maildir
, "unable to change directory to");
210 /* read folder and create message structure */
211 if (!(mp
= folder_read (folder
)))
212 adios (NULL
, "unable to read folder %s", folder
);
214 /* check for empty folder */
216 adios (NULL
, "no messages in %s", folder
);
218 /* parse all the message ranges/sequences and set SELECTED */
219 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
220 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
222 seq_setprev (mp
); /* set the previous sequence */
224 if ((nmsgs
= read_hdrs (mp
, datesw
)) <= 0)
225 adios (NULL
, "no messages to sort");
227 if (checksw
&& check_failed
) {
228 adios (NULL
, "errors found, no messages sorted");
232 * sort a list of pointers to our "messages to be sorted".
234 dlist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*dlist
));
235 for (i
= 0; i
< nmsgs
; i
++)
236 dlist
[i
] = &smsgs
[i
];
239 if (verbose
) { /* announce what we're doing */
242 printf ("sorting by %s\n", subjsort
);
244 printf ("sorting by %s-major %s-minor\n", subjsort
, datesw
);
246 printf ("sorting by datefield %s\n", datesw
);
249 /* first sort by date, or by subject-major, date-minor */
250 qsort ((char *) dlist
, nmsgs
, sizeof(*dlist
),
251 (qsort_comp
) (submajor
&& subjsort
? txtsort
: dsort
));
254 * if we're sorting on subject, we need another list
255 * in subject order, then a merge pass to collate the
258 if (!submajor
&& subjsort
) { /* already date sorted */
259 struct smsg
**slist
, **flist
;
260 register struct smsg
***il
, **fp
, **dp
;
262 slist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*slist
));
263 memcpy((char *)slist
, (char *)dlist
, (nmsgs
+1)*sizeof(*slist
));
264 qsort((char *)slist
, nmsgs
, sizeof(*slist
), (qsort_comp
) subsort
);
267 * make an inversion list so we can quickly find
268 * the collection of messages with the same subj
269 * given a message number.
271 il
= (struct smsg
***) calloc (mp
->hghsel
+1, sizeof(*il
));
273 adios (NULL
, "couldn't allocate msg list");
274 for (i
= 0; i
< nmsgs
; i
++)
275 il
[slist
[i
]->s_msg
] = &slist
[i
];
277 * make up the final list, chronological but with
278 * all the same subjects grouped together.
280 flist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*flist
));
282 for (dp
= dlist
; *dp
;) {
283 register struct smsg
**s
= il
[(*dp
++)->s_msg
];
285 /* see if we already did this guy */
291 * take the next message(s) if there is one,
292 * its subject isn't null and its subject
293 * is the same as this one and it's not too
296 while (*s
&& (*s
)->s_subj
[0] &&
297 strcmp((*s
)->s_subj
, s
[-1]->s_subj
) == 0 &&
299 (*s
)->s_clock
- s
[-1]->s_clock
<= datelimit
)) {
311 * At this point, dlist is a sorted array of pointers to smsg structures,
312 * each of which contains a message number.
315 rename_msgs (mp
, dlist
);
317 context_replace (pfolder
, folder
); /* update current folder */
318 seq_save (mp
); /* synchronize message sequences */
319 context_save (); /* save the context file */
320 folder_free (mp
); /* free folder/message structure */
326 read_hdrs (struct msgs
*mp
, char *datesw
)
330 register struct smsg
*s
;
332 twscopy (&tb
, dlocaltimenow ());
334 smsgs
= (struct smsg
*)
335 calloc ((size_t) (mp
->hghsel
- mp
->lowsel
+ 2),
338 adios (NULL
, "unable to allocate sort storage");
341 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
342 if (is_selected(mp
, msgnum
)) {
343 if (get_fields (datesw
, msgnum
, s
)) {
355 * Parse the message and get the data or subject field,
360 get_fields (char *datesw
, int msg
, struct smsg
*smsg
)
364 char *msgnam
, buf
[BUFSIZ
], nam
[NAMESZ
];
365 register struct tws
*tw
;
366 register char *datecomp
= NULL
, *subjcomp
= NULL
;
368 m_getfld_state_t gstate
;
370 if ((in
= fopen (msgnam
= m_name (msg
), "r")) == NULL
) {
371 admonish (msgnam
, "unable to read message");
374 m_getfld_state_init (&gstate
);
375 for (compnum
= 1;;) {
376 int bufsz
= sizeof buf
;
377 switch (state
= m_getfld (gstate
, nam
, buf
, &bufsz
, in
)) {
381 if (!mh_strcasecmp (nam
, datesw
)) {
382 datecomp
= add (buf
, datecomp
);
383 while (state
== FLDPLUS
) {
385 state
= m_getfld (gstate
, nam
, buf
, &bufsz
, in
);
386 datecomp
= add (buf
, datecomp
);
388 if (!subjsort
|| subjcomp
)
390 } else if (subjsort
&& !mh_strcasecmp (nam
, subjsort
)) {
391 subjcomp
= add (buf
, subjcomp
);
392 while (state
== FLDPLUS
) {
394 state
= m_getfld (gstate
, nam
, buf
, &bufsz
, in
);
395 subjcomp
= add (buf
, subjcomp
);
400 /* just flush this guy */
401 while (state
== FLDPLUS
) {
403 state
= m_getfld (gstate
, nam
, buf
, &bufsz
, in
);
414 if (state
== LENERR
|| state
== FMTERR
) {
415 admonish (NULL
, "format error in message %d (header #%d)",
427 adios (NULL
, "internal error -- you lose");
431 m_getfld_state_destroy (&gstate
);
434 * If no date component, then use the modification
435 * time of the file as its date
437 if (!datecomp
|| (tw
= dparsetime (datecomp
)) == NULL
) {
440 admonish (NULL
, "can't parse %s field in message %d", datesw
, msg
);
441 fstat (fileno (in
), &st
);
442 smsg
->s_clock
= st
.st_mtime
;
445 smsg
->s_clock
= dmktime (tw
);
451 * try to make the subject "canonical": delete
452 * leading "re:", everything but letters & smash
453 * letters to lower case.
455 register char *cp
, *cp2
;
456 register unsigned char c
;
460 if (strcmp (subjsort
, "subject") == 0) {
472 while ((c
= *cp
++)) {
474 *cp2
++ = isupper(c
) ? tolower(c
) : c
;
482 smsg
->s_subj
= subjcomp
;
495 dsort (struct smsg
**a
, struct smsg
**b
)
497 if ((*a
)->s_clock
< (*b
)->s_clock
)
499 else if ((*a
)->s_clock
> (*b
)->s_clock
)
501 else if ((*a
)->s_msg
< (*b
)->s_msg
)
511 subsort (struct smsg
**a
, struct smsg
**b
)
515 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
518 return (dsort (a
, b
));
522 txtsort (struct smsg
**a
, struct smsg
**b
)
526 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
528 else if ((*a
)->s_msg
< (*b
)->s_msg
)
535 rename_chain (struct msgs
*mp
, struct smsg
**mlist
, int msg
, int endmsg
)
538 char *newname
, oldname
[BUFSIZ
];
539 char newbuf
[PATH_MAX
+ 1];
542 nxt
= mlist
[msg
] - smsgs
; /* mlist[msg] is a ptr into smsgs */
543 mlist
[msg
] = (struct smsg
*)0;
544 old
= smsgs
[nxt
].s_msg
;
545 new = smsgs
[msg
].s_msg
;
546 strncpy (oldname
, m_name (old
), sizeof(oldname
));
547 newname
= m_name (new);
549 printf ("message %d becomes message %d\n", old
, new);
551 (void)snprintf(oldname
, sizeof (oldname
), "%s/%d", mp
->foldpath
, old
);
552 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, new);
553 ext_hook("ref-hook", oldname
, newbuf
);
555 if (rename (oldname
, newname
) == NOTOK
)
556 adios (newname
, "unable to rename %s to", oldname
);
558 copy_msg_flags (mp
, new, old
);
559 if (mp
->curmsg
== old
)
560 seq_setcur (mp
, new);
567 /* if (nxt != endmsg); */
568 /* rename_chain (mp, mlist, nxt, endmsg); */
572 rename_msgs (struct msgs
*mp
, struct smsg
**mlist
)
576 char f1
[BUFSIZ
], tmpfil
[BUFSIZ
];
577 char newbuf
[PATH_MAX
+ 1];
580 strncpy (tmpfil
, m_name (mp
->hghmsg
+ 1), sizeof(tmpfil
));
582 for (i
= 0; i
< nmsgs
; i
++) {
583 if (! (sp
= mlist
[i
]))
584 continue; /* did this one */
588 continue; /* this one doesn't move */
591 * the guy that was msg j is about to become msg i.
592 * rename 'j' to make a hole, then recursively rename
593 * guys to fill up the hole.
595 old
= smsgs
[j
].s_msg
;
596 new = smsgs
[i
].s_msg
;
597 strncpy (f1
, m_name (old
), sizeof(f1
));
600 printf ("renaming message chain from %d to %d\n", old
, new);
603 * Run the external hook to refile the old message as the
604 * temporary message number that is off of the end of the
605 * messages in the folder.
608 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, old
);
609 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, mp
->hghmsg
+ 1);
610 ext_hook("ref-hook", f1
, newbuf
);
612 if (rename (f1
, tmpfil
) == NOTOK
)
613 adios (tmpfil
, "unable to rename %s to ", f1
);
615 get_msg_flags (mp
, &tmpset
, old
);
617 rename_chain (mp
, mlist
, j
, i
);
620 * Run the external hook to refile the temorary message number
624 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, new);
625 ext_hook("ref-hook", newbuf
, f1
);
627 if (rename (tmpfil
, m_name(new)) == NOTOK
)
628 adios (m_name(new), "unable to rename %s to", tmpfil
);
630 set_msg_flags (mp
, &tmpset
, new);
631 mp
->msgflags
|= SEQMOD
;