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