]>
diplodocus.org Git - nmh/blob - uip/sortm.c
3 * sortm.c -- sort messages in a folder by date/time
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
16 static struct swit switches
[] = {
18 { "datefield field", 0 },
20 { "textfield field", 0 },
24 { "subject", -3 }, /* backward-compatibility */
46 static struct smsg
*smsgs
;
49 char *subjsort
= (char *) 0; /* sort on subject if != 0 */
50 unsigned long datelimit
= 0;
51 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
, "");
83 invo_name
= r1bindex (argv
[0], '/');
85 /* read user profile/context */
88 arguments
= getarguments (invo_name
, argc
, argv
, 1);
94 while ((cp
= *argp
++)) {
96 switch (smatch (++cp
, switches
)) {
98 ambigsw (cp
, switches
);
101 adios (NULL
, "-%s unknown", cp
);
104 snprintf(buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
106 print_help (buf
, switches
, 1);
109 print_version(invo_name
);
114 adios (NULL
, "only one date field at a time");
115 if (!(datesw
= *argp
++) || *datesw
== '-')
116 adios (NULL
, "missing argument to %s", argp
[-2]);
121 adios (NULL
, "only one text field at a time");
122 if (!(subjsort
= *argp
++) || *subjsort
== '-')
123 adios (NULL
, "missing argument to %s", argp
[-2]);
127 subjsort
= "subject";
130 subjsort
= (char *)0;
134 if (!(cp
= *argp
++) || *cp
== '-')
135 adios (NULL
, "missing argument to %s", argp
[-2]);
137 cp
++; /* skip any leading zeros */
138 if (!*cp
) { /* hit end of string */
139 submajor
++; /* sort subject-major */
142 if (!isdigit(*cp
) || !(datelimit
= atoi(cp
)))
143 adios (NULL
, "impossible limit %s", cp
);
144 datelimit
*= 60*60*24;
147 submajor
= 0; /* use date-major, but */
148 datelimit
= 0; /* use no limit */
159 if (*cp
== '+' || *cp
== '@') {
161 adios (NULL
, "only one folder at a time!");
163 folder
= pluspath (cp
);
165 app_msgarg(&msgs
, cp
);
168 if (!context_find ("path"))
169 free (path ("./", TFOLDER
));
171 app_msgarg(&msgs
, "all");
175 folder
= getfolder (1);
176 maildir
= m_maildir (folder
);
178 if (chdir (maildir
) == NOTOK
)
179 adios (maildir
, "unable to change directory to");
181 /* read folder and create message structure */
182 if (!(mp
= folder_read (folder
)))
183 adios (NULL
, "unable to read folder %s", folder
);
185 /* check for empty folder */
187 adios (NULL
, "no messages in %s", folder
);
189 /* parse all the message ranges/sequences and set SELECTED */
190 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
191 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
193 seq_setprev (mp
); /* set the previous sequence */
195 if ((nmsgs
= read_hdrs (mp
, datesw
)) <= 0)
196 adios (NULL
, "no messages to sort");
199 * sort a list of pointers to our "messages to be sorted".
201 dlist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*dlist
));
202 for (i
= 0; i
< nmsgs
; i
++)
203 dlist
[i
] = &smsgs
[i
];
206 if (verbose
) { /* announce what we're doing */
208 printf ("sorting by %s-major %s-minor\n",
209 submajor
? subjsort
: datesw
,
210 submajor
? datesw
: subjsort
);
212 printf ("sorting by datefield %s\n", datesw
);
215 /* first sort by date, or by subject-major, date-minor */
216 qsort ((char *) dlist
, nmsgs
, sizeof(*dlist
),
217 (qsort_comp
) (submajor
&& subjsort
? txtsort
: dsort
));
220 * if we're sorting on subject, we need another list
221 * in subject order, then a merge pass to collate the
224 if (!submajor
&& subjsort
) { /* already date sorted */
225 struct smsg
**slist
, **flist
;
226 register struct smsg
***il
, **fp
, **dp
;
228 slist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*slist
));
229 memcpy((char *)slist
, (char *)dlist
, (nmsgs
+1)*sizeof(*slist
));
230 qsort((char *)slist
, nmsgs
, sizeof(*slist
), (qsort_comp
) subsort
);
233 * make an inversion list so we can quickly find
234 * the collection of messages with the same subj
235 * given a message number.
237 il
= (struct smsg
***) calloc (mp
->hghsel
+1, sizeof(*il
));
239 adios (NULL
, "couldn't allocate msg list");
240 for (i
= 0; i
< nmsgs
; i
++)
241 il
[slist
[i
]->s_msg
] = &slist
[i
];
243 * make up the final list, chronological but with
244 * all the same subjects grouped together.
246 flist
= (struct smsg
**) mh_xmalloc ((nmsgs
+1) * sizeof(*flist
));
248 for (dp
= dlist
; *dp
;) {
249 register struct smsg
**s
= il
[(*dp
++)->s_msg
];
251 /* see if we already did this guy */
257 * take the next message(s) if there is one,
258 * its subject isn't null and its subject
259 * is the same as this one and it's not too
262 while (*s
&& (*s
)->s_subj
[0] &&
263 strcmp((*s
)->s_subj
, s
[-1]->s_subj
) == 0 &&
265 (*s
)->s_clock
- s
[-1]->s_clock
<= datelimit
)) {
277 * At this point, dlist is a sorted array of pointers to smsg structures,
278 * each of which contains a message number.
281 rename_msgs (mp
, dlist
);
283 context_replace (pfolder
, folder
); /* update current folder */
284 seq_save (mp
); /* synchronize message sequences */
285 context_save (); /* save the context file */
286 folder_free (mp
); /* free folder/message structure */
291 read_hdrs (struct msgs
*mp
, char *datesw
)
295 register struct smsg
*s
;
297 twscopy (&tb
, dlocaltimenow ());
299 smsgs
= (struct smsg
*)
300 calloc ((size_t) (mp
->hghsel
- mp
->lowsel
+ 2),
303 adios (NULL
, "unable to allocate sort storage");
306 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
307 if (is_selected(mp
, msgnum
)) {
308 if (get_fields (datesw
, msgnum
, s
)) {
320 * Parse the message and get the data or subject field,
325 get_fields (char *datesw
, int msg
, struct smsg
*smsg
)
329 char *msgnam
, buf
[BUFSIZ
], nam
[NAMESZ
];
330 register struct tws
*tw
;
331 register char *datecomp
= NULL
, *subjcomp
= NULL
;
334 if ((in
= fopen (msgnam
= m_name (msg
), "r")) == NULL
) {
335 admonish (msgnam
, "unable to read message");
338 for (compnum
= 1, state
= FLD
;;) {
339 switch (state
= m_getfld (state
, nam
, buf
, sizeof(buf
), in
)) {
344 if (!mh_strcasecmp (nam
, datesw
)) {
345 datecomp
= add (buf
, datecomp
);
346 while (state
== FLDPLUS
) {
347 state
= m_getfld (state
, nam
, buf
, sizeof(buf
), in
);
348 datecomp
= add (buf
, datecomp
);
350 if (!subjsort
|| subjcomp
)
352 } else if (subjsort
&& !mh_strcasecmp (nam
, subjsort
)) {
353 subjcomp
= add (buf
, subjcomp
);
354 while (state
== FLDPLUS
) {
355 state
= m_getfld (state
, nam
, buf
, sizeof(buf
), in
);
356 subjcomp
= add (buf
, subjcomp
);
361 /* just flush this guy */
362 while (state
== FLDPLUS
)
363 state
= m_getfld (state
, nam
, buf
, sizeof(buf
), in
);
374 if (state
== LENERR
|| state
== FMTERR
)
375 admonish (NULL
, "format error in message %d (header #%d)",
385 adios (NULL
, "internal error -- you lose");
391 * If no date component, then use the modification
392 * time of the file as its date
394 if (!datecomp
|| (tw
= dparsetime (datecomp
)) == NULL
) {
397 admonish (NULL
, "can't parse %s field in message %d", datesw
, msg
);
398 fstat (fileno (in
), &st
);
399 smsg
->s_clock
= st
.st_mtime
;
401 smsg
->s_clock
= dmktime (tw
);
407 * try to make the subject "canonical": delete
408 * leading "re:", everything but letters & smash
409 * letters to lower case.
411 register char *cp
, *cp2
, c
;
415 if (strcmp (subjsort
, "subject") == 0) {
427 while ((c
= *cp
++)) {
429 *cp2
++ = isupper(c
) ? tolower(c
) : c
;
437 smsg
->s_subj
= subjcomp
;
450 dsort (struct smsg
**a
, struct smsg
**b
)
452 if ((*a
)->s_clock
< (*b
)->s_clock
)
454 else if ((*a
)->s_clock
> (*b
)->s_clock
)
456 else if ((*a
)->s_msg
< (*b
)->s_msg
)
466 subsort (struct smsg
**a
, struct smsg
**b
)
470 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
473 return (dsort (a
, b
));
477 txtsort (struct smsg
**a
, struct smsg
**b
)
481 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
483 else if ((*a
)->s_msg
< (*b
)->s_msg
)
490 rename_chain (struct msgs
*mp
, struct smsg
**mlist
, int msg
, int endmsg
)
493 char *newname
, oldname
[BUFSIZ
];
494 char newbuf
[MAXPATHLEN
+ 1];
497 nxt
= mlist
[msg
] - smsgs
; /* mlist[msg] is a ptr into smsgs */
498 mlist
[msg
] = (struct smsg
*)0;
499 old
= smsgs
[nxt
].s_msg
;
500 new = smsgs
[msg
].s_msg
;
501 strncpy (oldname
, m_name (old
), sizeof(oldname
));
502 newname
= m_name (new);
504 printf ("message %d becomes message %d\n", old
, new);
506 (void)snprintf(oldname
, sizeof (oldname
), "%s/%d", mp
->foldpath
, old
);
507 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, new);
508 ext_hook("ref-hook", oldname
, newbuf
);
510 if (rename (oldname
, newname
) == NOTOK
)
511 adios (newname
, "unable to rename %s to", oldname
);
513 copy_msg_flags (mp
, new, old
);
514 if (mp
->curmsg
== old
)
515 seq_setcur (mp
, new);
522 /* if (nxt != endmsg); */
523 /* rename_chain (mp, mlist, nxt, endmsg); */
527 rename_msgs (struct msgs
*mp
, struct smsg
**mlist
)
531 char f1
[BUFSIZ
], tmpfil
[BUFSIZ
];
532 char newbuf
[MAXPATHLEN
+ 1];
535 strncpy (tmpfil
, m_name (mp
->hghmsg
+ 1), sizeof(tmpfil
));
537 for (i
= 0; i
< nmsgs
; i
++) {
538 if (! (sp
= mlist
[i
]))
539 continue; /* did this one */
543 continue; /* this one doesn't move */
546 * the guy that was msg j is about to become msg i.
547 * rename 'j' to make a hole, then recursively rename
548 * guys to fill up the hole.
550 old
= smsgs
[j
].s_msg
;
551 new = smsgs
[i
].s_msg
;
552 strncpy (f1
, m_name (old
), sizeof(f1
));
555 printf ("renaming message chain from %d to %d\n", old
, new);
558 * Run the external hook to refile the old message as the
559 * temporary message number that is off of the end of the
560 * messages in the folder.
563 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, old
);
564 (void)snprintf(newbuf
, sizeof (newbuf
), "%s/%d", mp
->foldpath
, mp
->hghmsg
+ 1);
565 ext_hook("ref-hook", f1
, newbuf
);
567 if (rename (f1
, tmpfil
) == NOTOK
)
568 adios (tmpfil
, "unable to rename %s to ", f1
);
570 get_msg_flags (mp
, &tmpset
, old
);
572 rename_chain (mp
, mlist
, j
, i
);
575 * Run the external hook to refile the temorary message number
579 (void)snprintf(f1
, sizeof (f1
), "%s/%d", mp
->foldpath
, new);
580 ext_hook("ref-hook", newbuf
, f1
);
582 if (rename (tmpfil
, m_name(new)) == NOTOK
)
583 adios (m_name(new), "unable to rename %s to", tmpfil
);
585 set_msg_flags (mp
, &tmpset
, new);
586 mp
->msgflags
|= SEQMOD
;