]>
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
);
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
**) malloc ((nmsgs
+1) * sizeof(*slist
));
253 adios (NULL
, "couldn't allocate sort memory");
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
**) malloc ((nmsgs
+1) * sizeof(*flist
));
273 adios (NULL
, "couldn't allocate msg list");
275 for (dp
= dlist
; *dp
;) {
276 register struct smsg
**s
= il
[(*dp
++)->s_msg
];
278 /* see if we already did this guy */
284 * take the next message(s) if there is one,
285 * its subject isn't null and its subject
286 * is the same as this one and it's not too
289 while (*s
&& (*s
)->s_subj
[0] &&
290 strcmp((*s
)->s_subj
, s
[-1]->s_subj
) == 0 &&
292 (*s
)->s_clock
- s
[-1]->s_clock
<= datelimit
)) {
302 rename_msgs (mp
, dlist
);
304 context_replace (pfolder
, folder
); /* update current folder */
305 seq_save (mp
); /* synchronize message sequences */
306 context_save (); /* save the context file */
307 folder_free (mp
); /* free folder/message structure */
312 read_hdrs (struct msgs
*mp
, char *datesw
)
316 register struct smsg
*s
;
318 twscopy (&tb
, dlocaltimenow ());
320 smsgs
= (struct smsg
*)
321 calloc ((size_t) (mp
->hghsel
- mp
->lowsel
+ 2),
324 adios (NULL
, "unable to allocate sort storage");
327 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
328 if (is_selected(mp
, msgnum
)) {
329 if (get_fields (datesw
, msgnum
, s
)) {
341 * Parse the message and get the data or subject field,
346 get_fields (char *datesw
, int msg
, struct smsg
*smsg
)
350 char *msgnam
, buf
[BUFSIZ
], nam
[NAMESZ
];
351 register struct tws
*tw
;
352 register char *datecomp
= NULL
, *subjcomp
= NULL
;
355 if ((in
= fopen (msgnam
= m_name (msg
), "r")) == NULL
) {
356 admonish (msgnam
, "unable to read message");
359 for (compnum
= 1, state
= FLD
;;) {
360 switch (state
= m_getfld (state
, nam
, buf
, sizeof(buf
), in
)) {
365 if (!strcasecmp (nam
, datesw
)) {
366 datecomp
= add (buf
, datecomp
);
367 while (state
== FLDPLUS
) {
368 state
= m_getfld (state
, nam
, buf
, sizeof(buf
), in
);
369 datecomp
= add (buf
, datecomp
);
371 if (!subjsort
|| subjcomp
)
373 } else if (subjsort
&& !strcasecmp (nam
, subjsort
)) {
374 subjcomp
= add (buf
, subjcomp
);
375 while (state
== FLDPLUS
) {
376 state
= m_getfld (state
, nam
, buf
, sizeof(buf
), in
);
377 subjcomp
= add (buf
, subjcomp
);
382 /* just flush this guy */
383 while (state
== FLDPLUS
)
384 state
= m_getfld (state
, nam
, buf
, sizeof(buf
), in
);
395 if (state
== LENERR
|| state
== FMTERR
)
396 admonish (NULL
, "format error in message %d (header #%d)",
406 adios (NULL
, "internal error -- you lose");
412 * If no date component, then use the modification
413 * time of the file as its date
415 if (!datecomp
|| (tw
= dparsetime (datecomp
)) == NULL
) {
418 admonish (NULL
, "can't parse %s field in message %d", datesw
, msg
);
419 fstat (fileno (in
), &st
);
420 smsg
->s_clock
= st
.st_mtime
;
422 smsg
->s_clock
= dmktime (tw
);
428 * try to make the subject "canonical": delete
429 * leading "re:", everything but letters & smash
430 * letters to lower case.
432 register char *cp
, *cp2
, c
;
436 if (strcmp (subjsort
, "subject") == 0)
443 *cp2
++ = isupper(c
) ? tolower(c
) : c
;
449 while ((c
= *cp
++)) {
451 *cp2
++ = isupper(c
) ? tolower(c
) : c
;
459 smsg
->s_subj
= subjcomp
;
472 dsort (struct smsg
**a
, struct smsg
**b
)
474 if ((*a
)->s_clock
< (*b
)->s_clock
)
476 else if ((*a
)->s_clock
> (*b
)->s_clock
)
478 else if ((*a
)->s_msg
< (*b
)->s_msg
)
488 subsort (struct smsg
**a
, struct smsg
**b
)
492 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
495 return (dsort (a
, b
));
499 txtsort (struct smsg
**a
, struct smsg
**b
)
503 if ((i
= strcmp ((*a
)->s_subj
, (*b
)->s_subj
)))
505 else if ((*a
)->s_msg
< (*b
)->s_msg
)
512 rename_chain (struct msgs
*mp
, struct smsg
**mlist
, int msg
, int endmsg
)
515 char *newname
, oldname
[BUFSIZ
];
518 nxt
= mlist
[msg
] - smsgs
; /* mlist[msg] is a ptr into smsgs */
519 mlist
[msg
] = (struct smsg
*)0;
520 old
= smsgs
[nxt
].s_msg
;
521 new = smsgs
[msg
].s_msg
;
522 strncpy (oldname
, m_name (old
), sizeof(oldname
));
523 newname
= m_name (new);
525 printf ("message %d becomes message %d\n", old
, new);
527 if (rename (oldname
, newname
) == NOTOK
)
528 adios (newname
, "unable to rename %s to", oldname
);
530 copy_msg_flags (mp
, new, old
);
531 if (mp
->curmsg
== old
)
532 seq_setcur (mp
, new);
539 /* if (nxt != endmsg); */
540 /* rename_chain (mp, mlist, nxt, endmsg); */
544 rename_msgs (struct msgs
*mp
, struct smsg
**mlist
)
548 char f1
[BUFSIZ
], tmpfil
[BUFSIZ
];
551 strncpy (tmpfil
, m_name (mp
->hghmsg
+ 1), sizeof(tmpfil
));
553 for (i
= 0; i
< nmsgs
; i
++) {
554 if (! (sp
= mlist
[i
]))
555 continue; /* did this one */
559 continue; /* this one doesn't move */
562 * the guy that was msg j is about to become msg i.
563 * rename 'j' to make a hole, then recursively rename
564 * guys to fill up the hole.
566 old
= smsgs
[j
].s_msg
;
567 new = smsgs
[i
].s_msg
;
568 strncpy (f1
, m_name (old
), sizeof(f1
));
571 printf ("renaming message chain from %d to %d\n", old
, new);
573 if (rename (f1
, tmpfil
) == NOTOK
)
574 adios (tmpfil
, "unable to rename %s to ", f1
);
575 get_msg_flags (mp
, &tmpset
, old
);
577 rename_chain (mp
, mlist
, j
, i
);
578 if (rename (tmpfil
, m_name(new)) == NOTOK
)
579 adios (m_name(new), "unable to rename %s to", tmpfil
);
581 set_msg_flags (mp
, &tmpset
, new);
582 mp
->msgflags
|= SEQMOD
;