]>
diplodocus.org Git - nmh/blob - uip/sortm.c
3 * sortm.c -- sort messages in a folder by date/time
9 #include <zotnet/tws/tws.h>
12 * We allocate space for messages (msgs array)
13 * this number of elements at a time.
18 static struct swit switches
[] = {
20 { "datefield field", 0 },
22 { "textfield field", 0 },
26 { "subject", -3 }, /* backward-compatibility */
48 static struct smsg
*smsgs
;
51 char *subjsort
= (char *) 0; /* sort on subject if != 0 */
52 unsigned long datelimit
= 0;
53 int submajor
= 0; /* if true, sort on subject-major */
56 /* This keeps compiler happy on calls to qsort */
57 typedef int (*qsort_comp
) (const void *, const void *);
62 static int read_hdrs (struct msgs
*, char *);
63 static int get_fields (char *, int, struct smsg
*);
64 static int dsort (struct smsg
**, struct smsg
**);
65 static int subsort (struct smsg
**, struct smsg
**);
66 static int txtsort (struct smsg
**, struct smsg
**);
67 static void rename_chain (struct msgs
*, struct smsg
**, int, int);
68 static void rename_msgs (struct msgs
*, struct smsg
**);
72 main (int argc
, char **argv
)
74 int nummsgs
, maxmsgs
, i
, msgnum
;
75 char *cp
, *maildir
, *datesw
= NULL
;
76 char *folder
= NULL
, buf
[BUFSIZ
], **argp
;
77 char **arguments
, **msgs
;
82 setlocale(LC_ALL
, "");
84 invo_name
= r1bindex (argv
[0], '/');
86 /* read user profile/context */
89 arguments
= getarguments (invo_name
, argc
, argv
, 1);
93 * Allocate the initial space to record message
98 if (!(msgs
= (char **) malloc ((size_t) (maxmsgs
* sizeof(*msgs
)))))
99 adios (NULL
, "unable to allocate storage");
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 */
169 if (*cp
== '+' || *cp
== '@') {
171 adios (NULL
, "only one folder at a time!");
173 folder
= path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
176 * Check if we need to allocate more space
177 * for message names/ranges.
179 if (nummsgs
>= maxmsgs
) {
181 if (!(msgs
= (char **) realloc (msgs
,
182 (size_t) (maxmsgs
* sizeof(*msgs
)))))
183 adios (NULL
, "unable to reallocate msgs storage");
185 msgs
[nummsgs
++] = cp
;
189 if (!context_find ("path"))
190 free (path ("./", TFOLDER
));
192 msgs
[nummsgs
++] = "all";
196 folder
= getfolder (1);
197 maildir
= m_maildir (folder
);
199 if (chdir (maildir
) == NOTOK
)
200 adios (maildir
, "unable to change directory to");
202 /* read folder and create message structure */
203 if (!(mp
= folder_read (folder
)))
204 adios (NULL
, "unable to read folder %s", folder
);
206 /* check for empty folder */
208 adios (NULL
, "no messages in %s", folder
);
210 /* parse all the message ranges/sequences and set SELECTED */
211 for (msgnum
= 0; msgnum
< nummsgs
; msgnum
++)
212 if (!m_convert (mp
, msgs
[msgnum
]))
214 seq_setprev (mp
); /* set the previous sequence */
216 if ((nmsgs
= read_hdrs (mp
, datesw
)) <= 0)
217 adios (NULL
, "no messages to sort");
220 * sort a list of pointers to our "messages to be sorted".
222 dlist
= (struct smsg
**) malloc ((nmsgs
+1) * sizeof(*dlist
));
224 adios (NULL
, "couldn't allocate sort memory");
225 for (i
= 0; i
< nmsgs
; i
++)
226 dlist
[i
] = &smsgs
[i
];
229 if (verbose
) /* announce what we're doing */
231 printf ("sorting by %s-major %s-minor\n",
232 submajor
? subjsort
: datesw
,
233 submajor
? datesw
: subjsort
);
235 printf ("sorting by datefield %s\n", datesw
);
237 /* first sort by date, or by subject-major, date-minor */
238 qsort ((char *) dlist
, nmsgs
, sizeof(*dlist
),
239 (qsort_comp
) (submajor
&& subjsort
? txtsort
: dsort
));
242 * if we're sorting on subject, we need another list
243 * in subject order, then a merge pass to collate the
246 if (!submajor
&& subjsort
) { /* already date sorted */
247 struct smsg
**slist
, **flist
;
248 register struct smsg
***il
, **fp
, **dp
;
250 slist
= (struct smsg
**) malloc ((nmsgs
+1) * sizeof(*slist
));
252 adios (NULL
, "couldn't allocate sort memory");
253 memcpy((char *)slist
, (char *)dlist
, (nmsgs
+1)*sizeof(*slist
));
254 qsort((char *)slist
, nmsgs
, sizeof(*slist
), (qsort_comp
) subsort
);
257 * make an inversion list so we can quickly find
258 * the collection of messages with the same subj
259 * given a message number.
261 il
= (struct smsg
***) calloc (mp
->hghsel
+1, sizeof(*il
));
263 adios (NULL
, "couldn't allocate msg list");
264 for (i
= 0; i
< nmsgs
; i
++)
265 il
[slist
[i
]->s_msg
] = &slist
[i
];
267 * make up the final list, chronological but with
268 * all the same subjects grouped together.
270 flist
= (struct smsg
**) malloc ((nmsgs
+1) * sizeof(*flist
));
272 adios (NULL
, "couldn't allocate msg list");
274 for (dp
= dlist
; *dp
;) {
275 register struct smsg
**s
= il
[(*dp
++)->s_msg
];
277 /* see if we already did this guy */
283 * take the next message(s) if there is one,
284 * its subject isn't null and its subject
285 * is the same as this one and it's not too
288 while (*s
&& (*s
)->s_subj
[0] &&
289 strcmp((*s
)->s_subj
, s
[-1]->s_subj
) == 0 &&
291 (*s
)->s_clock
- s
[-1]->s_clock
<= datelimit
)) {
301 rename_msgs (mp
, dlist
);
303 context_replace (pfolder
, folder
); /* update current folder */
304 seq_save (mp
); /* synchronize message sequences */
305 context_save (); /* save the context file */
306 folder_free (mp
); /* free folder/message structure */
311 read_hdrs (struct msgs
*mp
, char *datesw
)
315 register struct smsg
*s
;
317 twscopy (&tb
, dlocaltimenow ());
319 smsgs
= (struct smsg
*)
320 calloc ((size_t) (mp
->hghsel
- mp
->lowsel
+ 2),
323 adios (NULL
, "unable to allocate sort storage");
326 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
327 if (is_selected(mp
, msgnum
)) {
328 if (get_fields (datesw
, msgnum
, s
)) {
340 * Parse the message and get the data or subject field,
345 get_fields (char *datesw
, int msg
, struct smsg
*smsg
)
349 char *msgnam
, buf
[BUFSIZ
], nam
[NAMESZ
];
350 register struct tws
*tw
;
351 register char *datecomp
= NULL
, *subjcomp
= NULL
;
354 if ((in
= fopen (msgnam
= m_name (msg
), "r")) == NULL
) {
355 admonish (msgnam
, "unable to read message");
358 for (compnum
= 1, state
= FLD
;;) {
359 switch (state
= m_getfld (state
, nam
, buf
, sizeof(buf
), in
)) {
364 if (!strcasecmp (nam
, datesw
)) {
365 datecomp
= add (buf
, datecomp
);
366 while (state
== FLDPLUS
) {
367 state
= m_getfld (state
, nam
, buf
, sizeof(buf
), in
);
368 datecomp
= add (buf
, datecomp
);
370 if (!subjsort
|| subjcomp
)
372 } else if (subjsort
&& !strcasecmp (nam
, subjsort
)) {
373 subjcomp
= add (buf
, subjcomp
);
374 while (state
== FLDPLUS
) {
375 state
= m_getfld (state
, nam
, buf
, sizeof(buf
), in
);
376 subjcomp
= add (buf
, subjcomp
);
381 /* just flush this guy */
382 while (state
== FLDPLUS
)
383 state
= m_getfld (state
, nam
, buf
, sizeof(buf
), in
);
394 if (state
== LENERR
|| state
== FMTERR
)
395 admonish (NULL
, "format error in message %d (header #%d)",
405 adios (NULL
, "internal error -- you lose");
411 * If no date component, then use the modification
412 * time of the file as its date
414 if (!datecomp
|| (tw
= dparsetime (datecomp
)) == NULL
) {
417 admonish (NULL
, "can't parse %s field in message %d", datesw
, msg
);
418 fstat (fileno (in
), &st
);
419 smsg
->s_clock
= st
.st_mtime
;
421 smsg
->s_clock
= dmktime (tw
);
427 * try to make the subject "canonical": delete
428 * leading "re:", everything but letters & smash
429 * letters to lower case.
431 register char *cp
, *cp2
, c
;
435 if (strcmp (subjsort
, "subject") == 0)
442 *cp2
++ = isupper(c
) ? tolower(c
) : c
;
448 while ((c
= *cp
++)) {
450 *cp2
++ = isupper(c
) ? tolower(c
) : c
;
458 smsg
->s_subj
= subjcomp
;
471 dsort (struct smsg
**a
, struct smsg
**b
)
473 if ((*a
)->s_clock
< (*b
)->s_clock
)
475 else if ((*a
)->s_clock
> (*b
)->s_clock
)
477 else if ((*a
)->s_msg
< (*b
)->s_msg
)
487 subsort (struct smsg
**a
, struct smsg
**b
)
491 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
494 return (dsort (a
, b
));
498 txtsort (struct smsg
**a
, struct smsg
**b
)
502 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
504 else if ((*a
)->s_msg
< (*b
)->s_msg
)
511 rename_chain (struct msgs
*mp
, struct smsg
**mlist
, int msg
, int endmsg
)
514 char *newname
, oldname
[BUFSIZ
];
517 nxt
= mlist
[msg
] - smsgs
; /* mlist[msg] is a ptr into smsgs */
518 mlist
[msg
] = (struct smsg
*)0;
519 old
= smsgs
[nxt
].s_msg
;
520 new = smsgs
[msg
].s_msg
;
521 strncpy (oldname
, m_name (old
), sizeof(oldname
));
522 newname
= m_name (new);
524 printf ("message %d becomes message %d\n", old
, new);
526 if (rename (oldname
, newname
) == NOTOK
)
527 adios (newname
, "unable to rename %s to", oldname
);
529 copy_msg_flags (mp
, new, old
);
530 if (mp
->curmsg
== old
)
531 seq_setcur (mp
, new);
538 /* if (nxt != endmsg); */
539 /* rename_chain (mp, mlist, nxt, endmsg); */
543 rename_msgs (struct msgs
*mp
, struct smsg
**mlist
)
547 char f1
[BUFSIZ
], tmpfil
[BUFSIZ
];
550 strncpy (tmpfil
, m_name (mp
->hghmsg
+ 1), sizeof(tmpfil
));
552 for (i
= 0; i
< nmsgs
; i
++) {
553 if (! (sp
= mlist
[i
]))
554 continue; /* did this one */
558 continue; /* this one doesn't move */
561 * the guy that was msg j is about to become msg i.
562 * rename 'j' to make a hole, then recursively rename
563 * guys to fill up the hole.
565 old
= smsgs
[j
].s_msg
;
566 new = smsgs
[i
].s_msg
;
567 strncpy (f1
, m_name (old
), sizeof(f1
));
570 printf ("renaming message chain from %d to %d\n", old
, new);
572 if (rename (f1
, tmpfil
) == NOTOK
)
573 adios (tmpfil
, "unable to rename %s to ", f1
);
574 get_msg_flags (mp
, &tmpset
, old
);
576 rename_chain (mp
, mlist
, j
, i
);
577 if (rename (tmpfil
, m_name(new)) == NOTOK
)
578 adios (m_name(new), "unable to rename %s to", tmpfil
);
580 set_msg_flags (mp
, &tmpset
, new);
581 mp
->msgflags
|= SEQMOD
;