* patch #3966: Create a mh_xmalloc function to prevent mistakes when
[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 /*
17  * We allocate space for messages (msgs array)
18  * this number of elements at a time.
19  */
20 #define MAXMSGS  256
21
22
23 static struct swit switches[] = {
24 #define DATESW                 0
25      { "datefield field", 0 },
26 #define TEXTSW                 1
27      { "textfield field", 0 },
28 #define NSUBJSW                2
29      { "notextfield", 0 },
30 #define SUBJSW                 3
31      { "subject", -3 },            /* backward-compatibility */
32 #define LIMSW                  4
33      { "limit days", 0 },
34 #define NLIMSW                 5
35      { "nolimit", 0 },
36 #define VERBSW                 6
37      { "verbose", 0 },
38 #define NVERBSW                7
39      { "noverbose", 0 },
40 #define VERSIONSW              8
41      { "version", 0 },
42 #define HELPSW                 9
43      { "help", 0 },
44      { NULL, 0 }
45 };
46
47 struct smsg {
48     int s_msg;
49     time_t s_clock;
50     char *s_subj;
51 };
52
53 static struct smsg *smsgs;
54 int nmsgs;
55
56 char *subjsort = (char *) 0;    /* sort on subject if != 0 */
57 unsigned long datelimit = 0;
58 int submajor = 0;               /* if true, sort on subject-major */
59 int verbose;
60
61 /* This keeps compiler happy on calls to qsort */
62 typedef int (*qsort_comp) (const void *, const void *);
63
64 /*
65  * static prototypes
66  */
67 static int read_hdrs (struct msgs *, char *);
68 static int get_fields (char *, int, struct smsg *);
69 static int dsort (struct smsg **, struct smsg **);
70 static int subsort (struct smsg **, struct smsg **);
71 static int txtsort (struct smsg **, struct smsg **);
72 static void rename_chain (struct msgs *, struct smsg **, int, int);
73 static void rename_msgs (struct msgs *, struct smsg **);
74
75
76 int
77 main (int argc, char **argv)
78 {
79     int nummsgs, maxmsgs, i, msgnum;
80     char *cp, *maildir, *datesw = NULL;
81     char *folder = NULL, buf[BUFSIZ], **argp;
82     char **arguments, **msgs;
83     struct msgs *mp;
84     struct smsg **dlist;
85
86 #ifdef LOCALE
87     setlocale(LC_ALL, "");
88 #endif
89     invo_name = r1bindex (argv[0], '/');
90
91     /* read user profile/context */
92     context_read();
93
94     arguments = getarguments (invo_name, argc, argv, 1);
95     argp = arguments;
96
97     /*
98      * Allocate the initial space to record message
99      * names and ranges.
100      */
101     nummsgs = 0;
102     maxmsgs = MAXMSGS;
103     msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs)));
104
105     /*
106      * Parse arguments
107      */
108     while ((cp = *argp++)) {
109         if (*cp == '-') {
110             switch (smatch (++cp, switches)) {
111             case AMBIGSW:
112                 ambigsw (cp, switches);
113                 done (1);
114             case UNKWNSW:
115                 adios (NULL, "-%s unknown", cp);
116
117             case HELPSW:
118                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
119                         invo_name);
120                 print_help (buf, switches, 1);
121                 done (1);
122             case VERSIONSW:
123                 print_version(invo_name);
124                 done (1);
125
126             case DATESW:
127                 if (datesw)
128                     adios (NULL, "only one date field at a time");
129                 if (!(datesw = *argp++) || *datesw == '-')
130                     adios (NULL, "missing argument to %s", argp[-2]);
131                 continue;
132
133             case TEXTSW:
134                 if (subjsort)
135                     adios (NULL, "only one text field at a time");
136                 if (!(subjsort = *argp++) || *subjsort == '-')
137                     adios (NULL, "missing argument to %s", argp[-2]);
138                 continue;
139
140             case SUBJSW:
141                 subjsort = "subject";
142                 continue;
143             case NSUBJSW:
144                 subjsort = (char *)0;
145                 continue;
146
147             case LIMSW:
148                 if (!(cp = *argp++) || *cp == '-')
149                         adios (NULL, "missing argument to %s", argp[-2]);
150                 while (*cp == '0')
151                     cp++;               /* skip any leading zeros */
152                 if (!*cp) {             /* hit end of string */
153                     submajor++;         /* sort subject-major */
154                     continue;
155                 }
156                 if (!isdigit(*cp) || !(datelimit = atoi(cp)))
157                     adios (NULL, "impossible limit %s", cp);
158                 datelimit *= 60*60*24;
159                 continue;
160             case NLIMSW:
161                 submajor = 0;   /* use date-major, but */
162                 datelimit = 0;  /* use no limit */
163                 continue;
164
165             case VERBSW:
166                 verbose++;
167                 continue;
168             case NVERBSW:
169                 verbose = 0;
170                 continue;
171             }
172         }
173         if (*cp == '+' || *cp == '@') {
174             if (folder)
175                 adios (NULL, "only one folder at a time!");
176             else
177                 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
178         } else {
179             /*
180              * Check if we need to allocate more space
181              * for message names/ranges.
182              */
183             if (nummsgs >= maxmsgs) {
184                 maxmsgs += MAXMSGS;
185                 if (!(msgs = (char **) realloc (msgs,
186                         (size_t) (maxmsgs * sizeof(*msgs)))))
187                     adios (NULL, "unable to reallocate msgs storage");
188             }
189             msgs[nummsgs++] = cp;
190         }
191     }
192
193     if (!context_find ("path"))
194         free (path ("./", TFOLDER));
195     if (!nummsgs)
196         msgs[nummsgs++] = "all";
197     if (!datesw)
198         datesw = "date";
199     if (!folder)
200         folder = getfolder (1);
201     maildir = m_maildir (folder);
202
203     if (chdir (maildir) == NOTOK)
204         adios (maildir, "unable to change directory to");
205
206     /* read folder and create message structure */
207     if (!(mp = folder_read (folder)))
208         adios (NULL, "unable to read folder %s", folder);
209
210     /* check for empty folder */
211     if (mp->nummsg == 0)
212         adios (NULL, "no messages in %s", folder);
213
214     /* parse all the message ranges/sequences and set SELECTED */
215     for (msgnum = 0; msgnum < nummsgs; msgnum++)
216         if (!m_convert (mp, msgs[msgnum]))
217             done (1);
218     seq_setprev (mp);   /* set the previous sequence */
219
220     if ((nmsgs = read_hdrs (mp, datesw)) <= 0)
221         adios (NULL, "no messages to sort");
222
223     /*
224      * sort a list of pointers to our "messages to be sorted".
225      */
226     dlist = (struct smsg **) mh_xmalloc ((nmsgs+1) * sizeof(*dlist));
227     for (i = 0; i < nmsgs; i++)
228         dlist[i] = &smsgs[i];
229     dlist[nmsgs] = 0;
230
231     if (verbose) {      /* announce what we're doing */
232         if (subjsort)
233             printf ("sorting by %s-major %s-minor\n",
234                 submajor ? subjsort : datesw,
235                 submajor ? datesw : subjsort);
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     return done (0);
313 }
314
315 static int
316 read_hdrs (struct msgs *mp, char *datesw)
317 {
318     int msgnum;
319     struct tws tb;
320     register struct smsg *s;
321
322     twscopy (&tb, dlocaltimenow ());
323
324     smsgs = (struct smsg *)
325         calloc ((size_t) (mp->hghsel - mp->lowsel + 2),
326             sizeof(*smsgs));
327     if (smsgs == NULL)
328         adios (NULL, "unable to allocate sort storage");
329
330     s = smsgs;
331     for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
332         if (is_selected(mp, msgnum)) {
333             if (get_fields (datesw, msgnum, s)) {
334                 s->s_msg = msgnum;
335                 s++;
336             }
337         }
338     }
339     s->s_msg = 0;
340     return(s - smsgs);
341 }
342
343
344 /*
345  * Parse the message and get the data or subject field,
346  * if needed.
347  */
348
349 static int
350 get_fields (char *datesw, int msg, struct smsg *smsg)
351 {
352     register int state;
353     int compnum;
354     char *msgnam, buf[BUFSIZ], nam[NAMESZ];
355     register struct tws *tw;
356     register char *datecomp = NULL, *subjcomp = NULL;
357     register FILE *in;
358
359     if ((in = fopen (msgnam = m_name (msg), "r")) == NULL) {
360         admonish (msgnam, "unable to read message");
361         return (0);
362     }
363     for (compnum = 1, state = FLD;;) {
364         switch (state = m_getfld (state, nam, buf, sizeof(buf), in)) {
365         case FLD:
366         case FLDEOF:
367         case FLDPLUS:
368             compnum++;
369             if (!strcasecmp (nam, datesw)) {
370                 datecomp = add (buf, datecomp);
371                 while (state == FLDPLUS) {
372                     state = m_getfld (state, nam, buf, sizeof(buf), in);
373                     datecomp = add (buf, datecomp);
374                 }
375                 if (!subjsort || subjcomp)
376                     break;
377             } else if (subjsort && !strcasecmp (nam, subjsort)) {
378                 subjcomp = add (buf, subjcomp);
379                 while (state == FLDPLUS) {
380                     state = m_getfld (state, nam, buf, sizeof(buf), in);
381                     subjcomp = add (buf, subjcomp);
382                 }
383                 if (datecomp)
384                     break;
385             } else {
386                 /* just flush this guy */
387                 while (state == FLDPLUS)
388                     state = m_getfld (state, nam, buf, sizeof(buf), in);
389             }
390             continue;
391
392         case BODY:
393         case BODYEOF:
394         case FILEEOF:
395             break;
396
397         case LENERR:
398         case FMTERR:
399             if (state == LENERR || state == FMTERR)
400                 admonish (NULL, "format error in message %d (header #%d)",
401                       msg, compnum);
402             if (datecomp)
403                 free (datecomp);
404             if (subjcomp)
405                 free (subjcomp);
406             fclose (in);
407             return (0);
408
409         default:
410             adios (NULL, "internal error -- you lose");
411         }
412         break;
413     }
414
415     /*
416      * If no date component, then use the modification
417      * time of the file as its date
418      */
419     if (!datecomp || (tw = dparsetime (datecomp)) == NULL) {
420         struct stat st;
421
422         admonish (NULL, "can't parse %s field in message %d", datesw, msg);
423         fstat (fileno (in), &st);
424         smsg->s_clock = st.st_mtime;
425     } else {
426         smsg->s_clock = dmktime (tw);
427     }
428
429     if (subjsort) {
430         if (subjcomp) {
431             /*
432              * try to make the subject "canonical": delete
433              * leading "re:", everything but letters & smash
434              * letters to lower case.
435              */
436             register char  *cp, *cp2, c;
437
438             cp = subjcomp;
439             cp2 = subjcomp;
440             if (strcmp (subjsort, "subject") == 0) {
441                 while ((c = *cp)) {
442                     if (! isspace(c)) {
443                         if(uprf(cp, "re:"))
444                             cp += 2;
445                         else
446                             break;
447                     }
448                     cp++;
449                 }
450             }
451
452             while ((c = *cp++)) {
453                 if (isalnum(c))
454                     *cp2++ = isupper(c) ? tolower(c) : c;
455             }
456
457             *cp2 = '\0';
458         }
459         else
460             subjcomp = "";
461
462         smsg->s_subj = subjcomp;
463     }
464     fclose (in);
465     if (datecomp)
466         free (datecomp);
467
468     return (1);
469 }
470
471 /*
472  * sort on dates.
473  */
474 static int
475 dsort (struct smsg **a, struct smsg **b)
476 {
477     if ((*a)->s_clock < (*b)->s_clock)
478         return (-1);
479     else if ((*a)->s_clock > (*b)->s_clock)
480         return (1);
481     else if ((*a)->s_msg < (*b)->s_msg)
482         return (-1);
483     else
484         return (1);
485 }
486
487 /*
488  * sort on subjects.
489  */
490 static int
491 subsort (struct smsg **a, struct smsg **b)
492 {
493     register int i;
494
495     if ((i = strcmp ((*a)->s_subj, (*b)->s_subj)))
496         return (i);
497
498     return (dsort (a, b));
499 }
500
501 static int
502 txtsort (struct smsg **a, struct smsg **b)
503 {
504     register int i;
505
506     if ((i = strcmp ((*a)->s_subj, (*b)->s_subj)))
507         return (i);
508     else if ((*a)->s_msg < (*b)->s_msg)
509         return (-1);
510     else
511         return (1);
512 }
513
514 static void
515 rename_chain (struct msgs *mp, struct smsg **mlist, int msg, int endmsg)
516 {
517     int nxt, old, new;
518     char *newname, oldname[BUFSIZ];
519     char newbuf[MAXPATHLEN + 1];
520
521     for (;;) {
522         nxt = mlist[msg] - smsgs;       /* mlist[msg] is a ptr into smsgs */
523         mlist[msg] = (struct smsg *)0;
524         old = smsgs[nxt].s_msg;
525         new = smsgs[msg].s_msg;
526         strncpy (oldname, m_name (old), sizeof(oldname));
527         newname = m_name (new);
528         if (verbose)
529             printf ("message %d becomes message %d\n", old, new);
530
531         (void)snprintf(oldname, sizeof (oldname), "%s/%d", mp->foldpath, old);
532         (void)snprintf(newbuf, sizeof (newbuf), "%s/%d", mp->foldpath, new);
533         ext_hook("ref-hook", oldname, newbuf);
534
535         if (rename (oldname, newname) == NOTOK)
536             adios (newname, "unable to rename %s to", oldname);
537
538         copy_msg_flags (mp, new, old);
539         if (mp->curmsg == old)
540             seq_setcur (mp, new);
541
542         if (nxt == endmsg)
543             break;
544
545         msg = nxt;
546     }
547 /*      if (nxt != endmsg); */
548 /*      rename_chain (mp, mlist, nxt, endmsg); */
549 }
550
551 static void
552 rename_msgs (struct msgs *mp, struct smsg **mlist)
553 {
554     int i, j, old, new;
555     seqset_t tmpset;
556     char f1[BUFSIZ], tmpfil[BUFSIZ];
557     char newbuf[MAXPATHLEN + 1];
558     struct smsg *sp;
559
560     strncpy (tmpfil, m_name (mp->hghmsg + 1), sizeof(tmpfil));
561
562     for (i = 0; i < nmsgs; i++) {
563         if (! (sp = mlist[i]))
564             continue;   /* did this one */
565
566         j = sp - smsgs;
567         if (j == i)
568             continue;   /* this one doesn't move */
569
570         /*
571          * the guy that was msg j is about to become msg i.
572          * rename 'j' to make a hole, then recursively rename
573          * guys to fill up the hole.
574          */
575         old = smsgs[j].s_msg;
576         new = smsgs[i].s_msg;
577         strncpy (f1, m_name (old), sizeof(f1));
578
579         if (verbose)
580             printf ("renaming message chain from %d to %d\n", old, new);
581
582         /*
583          *      Run the external hook to refile the old message as the
584          *      temporary message number that is off of the end of the
585          *      messages in the folder.
586          */
587
588         (void)snprintf(f1, sizeof (f1), "%s/%d", mp->foldpath, old);
589         (void)snprintf(newbuf, sizeof (newbuf), "%s/%d", mp->foldpath, mp->hghmsg + 1);
590         ext_hook("ref-hook", f1, newbuf);
591
592         if (rename (f1, tmpfil) == NOTOK)
593             adios (tmpfil, "unable to rename %s to ", f1);
594
595         get_msg_flags (mp, &tmpset, old);
596
597         rename_chain (mp, mlist, j, i);
598
599         /*
600          *      Run the external hook to refile the temorary message number
601          *      to the real place.
602          */
603
604         (void)snprintf(f1, sizeof (f1), "%s/%d", mp->foldpath, new);
605         ext_hook("ref-hook", newbuf, f1);
606
607         if (rename (tmpfil, m_name(new)) == NOTOK)
608             adios (m_name(new), "unable to rename %s to", tmpfil);
609
610         set_msg_flags (mp, &tmpset, new);
611         mp->msgflags |= SEQMOD;
612     }
613 }