]> diplodocus.org Git - nmh/blob - uip/sortm.c
Changed m_getfld() to use char instead of unsigned char.
[nmh] / uip / sortm.c
1
2 /*
3 * sortm.c -- sort messages in a folder by date/time
4 *
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.
8 */
9
10 #include <h/mh.h>
11 #include <h/tws.h>
12 #include <h/utils.h>
13
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) \
29
30 #define X(sw, minchars, id) id,
31 DEFINE_SWITCH_ENUM(SORTM);
32 #undef X
33
34 #define X(sw, minchars, id) { sw, minchars, id },
35 DEFINE_SWITCH_ARRAY(SORTM, switches);
36 #undef X
37
38 struct smsg {
39 int s_msg;
40 time_t s_clock;
41 char *s_subj;
42 };
43
44 static struct smsg *smsgs;
45 int nmsgs;
46
47 char *subjsort = (char *) 0; /* sort on subject if != 0 */
48 time_t datelimit = 0;
49 int submajor = 0; /* if true, sort on subject-major */
50 int verbose;
51 int allmsgs = 1;
52 int check_failed = 0;
53
54 /* This keeps compiler happy on calls to qsort */
55 typedef int (*qsort_comp) (const void *, const void *);
56
57 /*
58 * static prototypes
59 */
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 **);
67
68
69 int
70 main (int argc, char **argv)
71 {
72 int i, msgnum;
73 char *cp, *maildir, *datesw = NULL;
74 char *folder = NULL, buf[BUFSIZ], **argp;
75 char **arguments;
76 struct msgs_array msgs = { 0, 0, NULL };
77 struct msgs *mp;
78 struct smsg **dlist;
79 int checksw = 0;
80
81 #ifdef LOCALE
82 setlocale(LC_ALL, "");
83 #endif
84 invo_name = r1bindex (argv[0], '/');
85
86 /* read user profile/context */
87 context_read();
88
89 arguments = getarguments (invo_name, argc, argv, 1);
90 argp = arguments;
91
92 /*
93 * Parse arguments
94 */
95 while ((cp = *argp++)) {
96 if (*cp == '-') {
97 switch (smatch (++cp, switches)) {
98 case AMBIGSW:
99 ambigsw (cp, switches);
100 done (1);
101 case UNKWNSW:
102 adios (NULL, "-%s unknown", cp);
103
104 case HELPSW:
105 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
106 invo_name);
107 print_help (buf, switches, 1);
108 done (0);
109 case VERSIONSW:
110 print_version(invo_name);
111 done (0);
112
113 case DATESW:
114 if (datesw)
115 adios (NULL, "only one date field at a time");
116 if (!(datesw = *argp++) || *datesw == '-')
117 adios (NULL, "missing argument to %s", argp[-2]);
118 continue;
119
120 case TEXTSW:
121 if (subjsort)
122 adios (NULL, "only one text field at a time");
123 if (!(subjsort = *argp++) || *subjsort == '-')
124 adios (NULL, "missing argument to %s", argp[-2]);
125 continue;
126
127 case SUBJSW:
128 subjsort = "subject";
129 continue;
130 case NSUBJSW:
131 subjsort = (char *)0;
132 continue;
133
134 case LIMSW:
135 if (!(cp = *argp++) || *cp == '-')
136 adios (NULL, "missing argument to %s", argp[-2]);
137 while (*cp == '0')
138 cp++; /* skip any leading zeros */
139 if (!*cp) { /* hit end of string */
140 submajor++; /* sort subject-major */
141 continue;
142 }
143 if (!isdigit((unsigned char) *cp) || !(datelimit = atoi(cp)))
144 adios (NULL, "impossible limit %s", cp);
145 datelimit *= 60*60*24;
146 continue;
147 case NLIMSW:
148 submajor = 0; /* use date-major, but */
149 datelimit = 0; /* use no limit */
150 continue;
151
152 case VERBSW:
153 verbose++;
154 continue;
155 case NVERBSW:
156 verbose = 0;
157 continue;
158
159 case ALLMSGS:
160 allmsgs = 1;
161 continue;
162 case NALLMSGS:
163 allmsgs = 0;
164 continue;
165
166 case CHECKSW:
167 checksw = 1;
168 continue;
169 case NCHECKSW:
170 checksw = 0;
171 continue;
172 }
173 }
174 if (*cp == '+' || *cp == '@') {
175 if (folder)
176 adios (NULL, "only one folder at a time!");
177 else
178 folder = pluspath (cp);
179 } else
180 app_msgarg(&msgs, cp);
181 }
182
183 if (!context_find ("path"))
184 free (path ("./", TFOLDER));
185 if (!msgs.size) {
186 if (allmsgs) {
187 app_msgarg(&msgs, "all");
188 } else {
189 adios (NULL, "must specify messages to sort with -noall");
190 }
191 }
192 if (!datesw)
193 datesw = "date";
194 if (!folder)
195 folder = getfolder (1);
196 maildir = m_maildir (folder);
197
198 if (chdir (maildir) == NOTOK)
199 adios (maildir, "unable to change directory to");
200
201 /* read folder and create message structure */
202 if (!(mp = folder_read (folder)))
203 adios (NULL, "unable to read folder %s", folder);
204
205 /* check for empty folder */
206 if (mp->nummsg == 0)
207 adios (NULL, "no messages in %s", folder);
208
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]))
212 done (1);
213 seq_setprev (mp); /* set the previous sequence */
214
215 if ((nmsgs = read_hdrs (mp, datesw)) <= 0)
216 adios (NULL, "no messages to sort");
217
218 if (checksw && check_failed) {
219 adios (NULL, "errors found, no messages sorted");
220 }
221
222 /*
223 * sort a list of pointers to our "messages to be sorted".
224 */
225 dlist = (struct smsg **) mh_xmalloc ((nmsgs+1) * sizeof(*dlist));
226 for (i = 0; i < nmsgs; i++)
227 dlist[i] = &smsgs[i];
228 dlist[nmsgs] = 0;
229
230 if (verbose) { /* announce what we're doing */
231 if (subjsort)
232 if (submajor)
233 printf ("sorting by %s\n", subjsort);
234 else
235 printf ("sorting by %s-major %s-minor\n", subjsort, datesw);
236 else
237 printf ("sorting by datefield %s\n", datesw);
238 }
239
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));
243
244 /*
245 * if we're sorting on subject, we need another list
246 * in subject order, then a merge pass to collate the
247 * two sorts.
248 */
249 if (!submajor && subjsort) { /* already date sorted */
250 struct smsg **slist, **flist;
251 register struct smsg ***il, **fp, **dp;
252
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);
256
257 /*
258 * make an inversion list so we can quickly find
259 * the collection of messages with the same subj
260 * given a message number.
261 */
262 il = (struct smsg ***) calloc (mp->hghsel+1, sizeof(*il));
263 if (! il)
264 adios (NULL, "couldn't allocate msg list");
265 for (i = 0; i < nmsgs; i++)
266 il[slist[i]->s_msg] = &slist[i];
267 /*
268 * make up the final list, chronological but with
269 * all the same subjects grouped together.
270 */
271 flist = (struct smsg **) mh_xmalloc ((nmsgs+1) * sizeof(*flist));
272 fp = flist;
273 for (dp = dlist; *dp;) {
274 register struct smsg **s = il[(*dp++)->s_msg];
275
276 /* see if we already did this guy */
277 if (! s)
278 continue;
279
280 *fp++ = *s++;
281 /*
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
285 * far away in time.
286 */
287 while (*s && (*s)->s_subj[0] &&
288 strcmp((*s)->s_subj, s[-1]->s_subj) == 0 &&
289 (datelimit == 0 ||
290 (*s)->s_clock - s[-1]->s_clock <= datelimit)) {
291 il[(*s)->s_msg] = 0;
292 *fp++ = *s++;
293 }
294 }
295 *fp = 0;
296 free (slist);
297 free (dlist);
298 dlist = flist;
299 }
300
301 /*
302 * At this point, dlist is a sorted array of pointers to smsg structures,
303 * each of which contains a message number.
304 */
305
306 rename_msgs (mp, dlist);
307
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 */
312 done (0);
313 return 1;
314 }
315
316 static int
317 read_hdrs (struct msgs *mp, char *datesw)
318 {
319 int msgnum;
320 struct tws tb;
321 register struct smsg *s;
322
323 twscopy (&tb, dlocaltimenow ());
324
325 smsgs = (struct smsg *)
326 calloc ((size_t) (mp->hghsel - mp->lowsel + 2),
327 sizeof(*smsgs));
328 if (smsgs == NULL)
329 adios (NULL, "unable to allocate sort storage");
330
331 s = smsgs;
332 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
333 if (is_selected(mp, msgnum)) {
334 if (get_fields (datesw, msgnum, s)) {
335 s->s_msg = msgnum;
336 s++;
337 }
338 }
339 }
340 s->s_msg = 0;
341 return(s - smsgs);
342 }
343
344
345 /*
346 * Parse the message and get the data or subject field,
347 * if needed.
348 */
349
350 static int
351 get_fields (char *datesw, int msg, struct smsg *smsg)
352 {
353 register int state;
354 int compnum;
355 char *msgnam, buf[BUFSIZ], nam[NAMESZ];
356 register struct tws *tw;
357 register char *datecomp = NULL, *subjcomp = NULL;
358 register FILE *in;
359 m_getfld_state_t gstate = 0;
360
361 if ((in = fopen (msgnam = m_name (msg), "r")) == NULL) {
362 admonish (msgnam, "unable to read message");
363 return (0);
364 }
365 for (compnum = 1;;) {
366 int bufsz = sizeof buf;
367 switch (state = m_getfld (&gstate, (unsigned char *) nam,
368 (unsigned char *) buf, &bufsz, in)) {
369 case FLD:
370 case FLDPLUS:
371 compnum++;
372 if (!mh_strcasecmp (nam, datesw)) {
373 datecomp = add (buf, datecomp);
374 while (state == FLDPLUS) {
375 bufsz = sizeof buf;
376 state = m_getfld (&gstate, (unsigned char *) nam,
377 (unsigned char *) buf, &bufsz, in);
378 datecomp = add (buf, datecomp);
379 }
380 if (!subjsort || subjcomp)
381 break;
382 } else if (subjsort && !mh_strcasecmp (nam, subjsort)) {
383 subjcomp = add (buf, subjcomp);
384 while (state == FLDPLUS) {
385 bufsz = sizeof buf;
386 state = m_getfld (&gstate, (unsigned char *) nam,
387 (unsigned char *) buf, &bufsz, in);
388 subjcomp = add (buf, subjcomp);
389 }
390 if (datecomp)
391 break;
392 } else {
393 /* just flush this guy */
394 while (state == FLDPLUS) {
395 bufsz = sizeof buf;
396 state = m_getfld (&gstate, (unsigned char *) nam,
397 (unsigned char *) buf, &bufsz, in);
398 }
399 }
400 continue;
401
402 case BODY:
403 case FILEEOF:
404 break;
405
406 case LENERR:
407 case FMTERR:
408 if (state == LENERR || state == FMTERR) {
409 admonish (NULL, "format error in message %d (header #%d)",
410 msg, compnum);
411 check_failed = 1;
412 }
413 if (datecomp)
414 free (datecomp);
415 if (subjcomp)
416 free (subjcomp);
417 fclose (in);
418 return (0);
419
420 default:
421 adios (NULL, "internal error -- you lose");
422 }
423 break;
424 }
425 m_getfld_state_destroy (&gstate);
426
427 /*
428 * If no date component, then use the modification
429 * time of the file as its date
430 */
431 if (!datecomp || (tw = dparsetime (datecomp)) == NULL) {
432 struct stat st;
433
434 advise (NULL,
435 "can't parse %s field in message %d, "
436 "will use file modification time",
437 datesw, msg);
438 fstat (fileno (in), &st);
439 smsg->s_clock = st.st_mtime;
440 check_failed = 1;
441 } else {
442 smsg->s_clock = dmktime (tw);
443 }
444
445 if (subjsort) {
446 if (subjcomp) {
447 /*
448 * try to make the subject "canonical": delete
449 * leading "re:", everything but letters & smash
450 * letters to lower case.
451 */
452 register char *cp, *cp2, c;
453
454 cp = subjcomp;
455 cp2 = subjcomp;
456 if (strcmp (subjsort, "subject") == 0) {
457 while ((c = *cp)) {
458 if (! isspace((unsigned char) c)) {
459 if(uprf(cp, "re:"))
460 cp += 2;
461 else
462 break;
463 }
464 cp++;
465 }
466 }
467
468 while ((c = *cp++)) {
469 if (isascii((unsigned char) c) && isalnum((unsigned char) c))
470 *cp2++ = isupper((unsigned char) c) ?
471 tolower((unsigned char) c) : c;
472 }
473
474 *cp2 = '\0';
475 }
476 else
477 subjcomp = "";
478
479 smsg->s_subj = subjcomp;
480 }
481 fclose (in);
482 if (datecomp)
483 free (datecomp);
484
485 return (1);
486 }
487
488 /*
489 * sort on dates.
490 */
491 static int
492 dsort (struct smsg **a, struct smsg **b)
493 {
494 if ((*a)->s_clock < (*b)->s_clock)
495 return (-1);
496 else if ((*a)->s_clock > (*b)->s_clock)
497 return (1);
498 else if ((*a)->s_msg < (*b)->s_msg)
499 return (-1);
500 else
501 return (1);
502 }
503
504 /*
505 * sort on subjects.
506 */
507 static int
508 subsort (struct smsg **a, struct smsg **b)
509 {
510 register int i;
511
512 if ((i = strcmp ((*a)->s_subj, (*b)->s_subj)))
513 return (i);
514
515 return (dsort (a, b));
516 }
517
518 static int
519 txtsort (struct smsg **a, struct smsg **b)
520 {
521 register int i;
522
523 if ((i = strcmp ((*a)->s_subj, (*b)->s_subj)))
524 return (i);
525 else if ((*a)->s_msg < (*b)->s_msg)
526 return (-1);
527 else
528 return (1);
529 }
530
531 static void
532 rename_chain (struct msgs *mp, struct smsg **mlist, int msg, int endmsg)
533 {
534 int nxt, old, new;
535 char *newname, oldname[BUFSIZ];
536 char newbuf[PATH_MAX + 1];
537
538 for (;;) {
539 nxt = mlist[msg] - smsgs; /* mlist[msg] is a ptr into smsgs */
540 mlist[msg] = (struct smsg *)0;
541 old = smsgs[nxt].s_msg;
542 new = smsgs[msg].s_msg;
543 strncpy (oldname, m_name (old), sizeof(oldname));
544 newname = m_name (new);
545 if (verbose)
546 printf ("message %d becomes message %d\n", old, new);
547
548 (void)snprintf(oldname, sizeof (oldname), "%s/%d", mp->foldpath, old);
549 (void)snprintf(newbuf, sizeof (newbuf), "%s/%d", mp->foldpath, new);
550 ext_hook("ref-hook", oldname, newbuf);
551
552 if (rename (oldname, newname) == NOTOK)
553 adios (newname, "unable to rename %s to", oldname);
554
555 copy_msg_flags (mp, new, old);
556 if (mp->curmsg == old)
557 seq_setcur (mp, new);
558
559 if (nxt == endmsg)
560 break;
561
562 msg = nxt;
563 }
564 /* if (nxt != endmsg); */
565 /* rename_chain (mp, mlist, nxt, endmsg); */
566 }
567
568 static void
569 rename_msgs (struct msgs *mp, struct smsg **mlist)
570 {
571 int i, j, old, new;
572 seqset_t tmpset;
573 char f1[BUFSIZ], tmpfil[BUFSIZ];
574 char newbuf[PATH_MAX + 1];
575 struct smsg *sp;
576
577 strncpy (tmpfil, m_name (mp->hghmsg + 1), sizeof(tmpfil));
578
579 for (i = 0; i < nmsgs; i++) {
580 if (! (sp = mlist[i]))
581 continue; /* did this one */
582
583 j = sp - smsgs;
584 if (j == i)
585 continue; /* this one doesn't move */
586
587 /*
588 * the guy that was msg j is about to become msg i.
589 * rename 'j' to make a hole, then recursively rename
590 * guys to fill up the hole.
591 */
592 old = smsgs[j].s_msg;
593 new = smsgs[i].s_msg;
594 strncpy (f1, m_name (old), sizeof(f1));
595
596 if (verbose)
597 printf ("renaming message chain from %d to %d\n", old, new);
598
599 /*
600 * Run the external hook to refile the old message as the
601 * temporary message number that is off of the end of the
602 * messages in the folder.
603 */
604
605 (void)snprintf(f1, sizeof (f1), "%s/%d", mp->foldpath, old);
606 (void)snprintf(newbuf, sizeof (newbuf), "%s/%d", mp->foldpath, mp->hghmsg + 1);
607 ext_hook("ref-hook", f1, newbuf);
608
609 if (rename (f1, tmpfil) == NOTOK)
610 adios (tmpfil, "unable to rename %s to ", f1);
611
612 get_msg_flags (mp, &tmpset, old);
613
614 rename_chain (mp, mlist, j, i);
615
616 /*
617 * Run the external hook to refile the temorary message number
618 * to the real place.
619 */
620
621 (void)snprintf(f1, sizeof (f1), "%s/%d", mp->foldpath, new);
622 ext_hook("ref-hook", newbuf, f1);
623
624 if (rename (tmpfil, m_name(new)) == NOTOK)
625 adios (m_name(new), "unable to rename %s to", tmpfil);
626
627 set_msg_flags (mp, &tmpset, new);
628 mp->msgflags |= SEQMOD;
629 }
630 }