Created app_msgarg() (append message arg) and a simple resizable array
[mmh] / uip / sortm.c
1
2 /*
3  * sortm.c -- sort messages in a folder by date/time
4  *
5  * $Id$
6  *
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.
10  */
11
12 #include <h/mh.h>
13 #include <h/tws.h>
14 #include <h/utils.h>
15
16 static struct swit switches[] = {
17 #define DATESW                 0
18      { "datefield field", 0 },
19 #define TEXTSW                 1
20      { "textfield field", 0 },
21 #define NSUBJSW                2
22      { "notextfield", 0 },
23 #define SUBJSW                 3
24      { "subject", -3 },            /* backward-compatibility */
25 #define LIMSW                  4
26      { "limit days", 0 },
27 #define NLIMSW                 5
28      { "nolimit", 0 },
29 #define VERBSW                 6
30      { "verbose", 0 },
31 #define NVERBSW                7
32      { "noverbose", 0 },
33 #define VERSIONSW              8
34      { "version", 0 },
35 #define HELPSW                 9
36      { "help", 0 },
37      { NULL, 0 }
38 };
39
40 struct smsg {
41     int s_msg;
42     time_t s_clock;
43     char *s_subj;
44 };
45
46 static struct smsg *smsgs;
47 int nmsgs;
48
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 */
52 int verbose;
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
80 #ifdef LOCALE
81     setlocale(LC_ALL, "");
82 #endif
83     invo_name = r1bindex (argv[0], '/');
84
85     /* read user profile/context */
86     context_read();
87
88     arguments = getarguments (invo_name, argc, argv, 1);
89     argp = arguments;
90
91     /*
92      * Parse arguments
93      */
94     while ((cp = *argp++)) {
95         if (*cp == '-') {
96             switch (smatch (++cp, switches)) {
97             case AMBIGSW:
98                 ambigsw (cp, switches);
99                 done (1);
100             case UNKWNSW:
101                 adios (NULL, "-%s unknown", cp);
102
103             case HELPSW:
104                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
105                         invo_name);
106                 print_help (buf, switches, 1);
107                 done (1);
108             case VERSIONSW:
109                 print_version(invo_name);
110                 done (1);
111
112             case DATESW:
113                 if (datesw)
114                     adios (NULL, "only one date field at a time");
115                 if (!(datesw = *argp++) || *datesw == '-')
116                     adios (NULL, "missing argument to %s", argp[-2]);
117                 continue;
118
119             case TEXTSW:
120                 if (subjsort)
121                     adios (NULL, "only one text field at a time");
122                 if (!(subjsort = *argp++) || *subjsort == '-')
123                     adios (NULL, "missing argument to %s", argp[-2]);
124                 continue;
125
126             case SUBJSW:
127                 subjsort = "subject";
128                 continue;
129             case NSUBJSW:
130                 subjsort = (char *)0;
131                 continue;
132
133             case LIMSW:
134                 if (!(cp = *argp++) || *cp == '-')
135                         adios (NULL, "missing argument to %s", argp[-2]);
136                 while (*cp == '0')
137                     cp++;               /* skip any leading zeros */
138                 if (!*cp) {             /* hit end of string */
139                     submajor++;         /* sort subject-major */
140                     continue;
141                 }
142                 if (!isdigit(*cp) || !(datelimit = atoi(cp)))
143                     adios (NULL, "impossible limit %s", cp);
144                 datelimit *= 60*60*24;
145                 continue;
146             case NLIMSW:
147                 submajor = 0;   /* use date-major, but */
148                 datelimit = 0;  /* use no limit */
149                 continue;
150
151             case VERBSW:
152                 verbose++;
153                 continue;
154             case NVERBSW:
155                 verbose = 0;
156                 continue;
157             }
158         }
159         if (*cp == '+' || *cp == '@') {
160             if (folder)
161                 adios (NULL, "only one folder at a time!");
162             else
163                 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
164         } else
165                 app_msgarg(&msgs, cp);
166     }
167
168     if (!context_find ("path"))
169         free (path ("./", TFOLDER));
170     if (!msgs.size)
171         app_msgarg(&msgs, "all");
172     if (!datesw)
173         datesw = "date";
174     if (!folder)
175         folder = getfolder (1);
176     maildir = m_maildir (folder);
177
178     if (chdir (maildir) == NOTOK)
179         adios (maildir, "unable to change directory to");
180
181     /* read folder and create message structure */
182     if (!(mp = folder_read (folder)))
183         adios (NULL, "unable to read folder %s", folder);
184
185     /* check for empty folder */
186     if (mp->nummsg == 0)
187         adios (NULL, "no messages in %s", folder);
188
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]))
192             done (1);
193     seq_setprev (mp);   /* set the previous sequence */
194
195     if ((nmsgs = read_hdrs (mp, datesw)) <= 0)
196         adios (NULL, "no messages to sort");
197
198     /*
199      * sort a list of pointers to our "messages to be sorted".
200      */
201     dlist = (struct smsg **) mh_xmalloc ((nmsgs+1) * sizeof(*dlist));
202     for (i = 0; i < nmsgs; i++)
203         dlist[i] = &smsgs[i];
204     dlist[nmsgs] = 0;
205
206     if (verbose) {      /* announce what we're doing */
207         if (subjsort)
208             printf ("sorting by %s-major %s-minor\n",
209                 submajor ? subjsort : datesw,
210                 submajor ? datesw : subjsort);
211         else
212             printf ("sorting by datefield %s\n", datesw);
213     }
214
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));
218
219     /*
220      * if we're sorting on subject, we need another list
221      * in subject order, then a merge pass to collate the
222      * two sorts.
223      */
224     if (!submajor && subjsort) {        /* already date sorted */
225         struct smsg **slist, **flist;
226         register struct smsg ***il, **fp, **dp;
227
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);
231
232         /*
233          * make an inversion list so we can quickly find
234          * the collection of messages with the same subj
235          * given a message number.
236          */
237         il = (struct smsg ***) calloc (mp->hghsel+1, sizeof(*il));
238         if (! il)
239             adios (NULL, "couldn't allocate msg list");
240         for (i = 0; i < nmsgs; i++)
241             il[slist[i]->s_msg] = &slist[i];
242         /*
243          * make up the final list, chronological but with
244          * all the same subjects grouped together.
245          */
246         flist = (struct smsg **) mh_xmalloc ((nmsgs+1) * sizeof(*flist));
247         fp = flist;
248         for (dp = dlist; *dp;) {
249             register struct smsg **s = il[(*dp++)->s_msg];
250
251             /* see if we already did this guy */
252             if (! s)
253                 continue;
254
255             *fp++ = *s++;
256             /*
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
260              * far away in time.
261              */
262             while (*s && (*s)->s_subj[0] &&
263                    strcmp((*s)->s_subj, s[-1]->s_subj) == 0 &&
264                    (datelimit == 0 ||
265                    (*s)->s_clock - s[-1]->s_clock <= datelimit)) {
266                 il[(*s)->s_msg] = 0;
267                 *fp++ = *s++;
268             }
269         }
270         *fp = 0;
271         free (slist);
272         free (dlist);
273         dlist = flist;
274     }
275
276     /*
277      * At this point, dlist is a sorted array of pointers to smsg structures,
278      * each of which contains a message number.
279      */
280
281     rename_msgs (mp, dlist);
282
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 */
287     return done (0);
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, c;
412
413             cp = subjcomp;
414             cp2 = subjcomp;
415             if (strcmp (subjsort, "subject") == 0) {
416                 while ((c = *cp)) {
417                     if (! isspace(c)) {
418                         if(uprf(cp, "re:"))
419                             cp += 2;
420                         else
421                             break;
422                     }
423                     cp++;
424                 }
425             }
426
427             while ((c = *cp++)) {
428                 if (isalnum(c))
429                     *cp2++ = isupper(c) ? tolower(c) : c;
430             }
431
432             *cp2 = '\0';
433         }
434         else
435             subjcomp = "";
436
437         smsg->s_subj = subjcomp;
438     }
439     fclose (in);
440     if (datecomp)
441         free (datecomp);
442
443     return (1);
444 }
445
446 /*
447  * sort on dates.
448  */
449 static int
450 dsort (struct smsg **a, struct smsg **b)
451 {
452     if ((*a)->s_clock < (*b)->s_clock)
453         return (-1);
454     else if ((*a)->s_clock > (*b)->s_clock)
455         return (1);
456     else if ((*a)->s_msg < (*b)->s_msg)
457         return (-1);
458     else
459         return (1);
460 }
461
462 /*
463  * sort on subjects.
464  */
465 static int
466 subsort (struct smsg **a, struct smsg **b)
467 {
468     register int i;
469
470     if ((i = strcmp ((*a)->s_subj, (*b)->s_subj)))
471         return (i);
472
473     return (dsort (a, b));
474 }
475
476 static int
477 txtsort (struct smsg **a, struct smsg **b)
478 {
479     register int i;
480
481     if ((i = strcmp ((*a)->s_subj, (*b)->s_subj)))
482         return (i);
483     else if ((*a)->s_msg < (*b)->s_msg)
484         return (-1);
485     else
486         return (1);
487 }
488
489 static void
490 rename_chain (struct msgs *mp, struct smsg **mlist, int msg, int endmsg)
491 {
492     int nxt, old, new;
493     char *newname, oldname[BUFSIZ];
494     char newbuf[MAXPATHLEN + 1];
495
496     for (;;) {
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);
503         if (verbose)
504             printf ("message %d becomes message %d\n", old, new);
505
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);
509
510         if (rename (oldname, newname) == NOTOK)
511             adios (newname, "unable to rename %s to", oldname);
512
513         copy_msg_flags (mp, new, old);
514         if (mp->curmsg == old)
515             seq_setcur (mp, new);
516
517         if (nxt == endmsg)
518             break;
519
520         msg = nxt;
521     }
522 /*      if (nxt != endmsg); */
523 /*      rename_chain (mp, mlist, nxt, endmsg); */
524 }
525
526 static void
527 rename_msgs (struct msgs *mp, struct smsg **mlist)
528 {
529     int i, j, old, new;
530     seqset_t tmpset;
531     char f1[BUFSIZ], tmpfil[BUFSIZ];
532     char newbuf[MAXPATHLEN + 1];
533     struct smsg *sp;
534
535     strncpy (tmpfil, m_name (mp->hghmsg + 1), sizeof(tmpfil));
536
537     for (i = 0; i < nmsgs; i++) {
538         if (! (sp = mlist[i]))
539             continue;   /* did this one */
540
541         j = sp - smsgs;
542         if (j == i)
543             continue;   /* this one doesn't move */
544
545         /*
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.
549          */
550         old = smsgs[j].s_msg;
551         new = smsgs[i].s_msg;
552         strncpy (f1, m_name (old), sizeof(f1));
553
554         if (verbose)
555             printf ("renaming message chain from %d to %d\n", old, new);
556
557         /*
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.
561          */
562
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);
566
567         if (rename (f1, tmpfil) == NOTOK)
568             adios (tmpfil, "unable to rename %s to ", f1);
569
570         get_msg_flags (mp, &tmpset, old);
571
572         rename_chain (mp, mlist, j, i);
573
574         /*
575          *      Run the external hook to refile the temorary message number
576          *      to the real place.
577          */
578
579         (void)snprintf(f1, sizeof (f1), "%s/%d", mp->foldpath, new);
580         ext_hook("ref-hook", newbuf, f1);
581
582         if (rename (tmpfil, m_name(new)) == NOTOK)
583             adios (m_name(new), "unable to rename %s to", tmpfil);
584
585         set_msg_flags (mp, &tmpset, new);
586         mp->msgflags |= SEQMOD;
587     }
588 }