Added mh_xcalloc().
[mmh] / uip / sortm.c
1 /*
2 ** sortm.c -- sort messages in a folder by date/time
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/tws.h>
11 #include <h/utils.h>
12 #include <unistd.h>
13 #include <ctype.h>
14 #include <sys/stat.h>
15 #include <locale.h>
16 #include <sysexits.h>
17
18 #ifdef HAVE_SYS_PARAM_H
19 # include <sys/param.h>
20 #endif
21
22 static struct swit switches[] = {
23 #define DATESW  0
24          { "datefield field", 0 },
25 #define TEXTSW  1
26          { "textfield field", 0 },
27 #define NSUBJSW  2
28          { "notextfield", 2 },
29 #define LIMSW  3
30          { "limit days", 0 },
31 #define NLIMSW  4
32          { "nolimit", 2 },
33 #define VERBSW  5
34          { "verbose", 0 },
35 #define NVERBSW  6
36          { "noverbose", 2 },
37 #define VERSIONSW  7
38          { "Version", 0 },
39 #define HELPSW  8
40          { "help", 0 },
41          { NULL, 0 }
42 };
43
44 struct smsg {
45         int s_msg;
46         time_t s_clock;
47         char *s_subj;
48 };
49
50 static struct smsg *smsgs;
51 int nmsgs;
52
53 char *subjsort = NULL;  /* sort on subject if != 0 */
54 time_t datelimit = 0;
55 int submajor = 0;  /* if true, sort on subject-major */
56 int verbose;
57
58 /* This keeps compiler happy on calls to qsort */
59 typedef int (*qsort_comp) (const void *, const void *);
60
61 /*
62 ** static prototypes
63 */
64 static int read_hdrs(struct msgs *, char *);
65 static int get_fields(char *, int, struct smsg *);
66 static int dsort(struct smsg **, struct smsg **);
67 static int subsort(struct smsg **, struct smsg **);
68 static int txtsort(struct smsg **, struct smsg **);
69 static void rename_chain(struct msgs *, struct smsg **, int, int);
70 static void rename_msgs(struct msgs *, struct smsg **);
71
72
73 int
74 main(int argc, char **argv)
75 {
76         int i, msgnum;
77         unsigned char *cp;
78         char *maildir, *datesw = NULL;
79         char *folder = NULL, buf[BUFSIZ], **argp;
80         char **arguments;
81         struct msgs_array msgs = { 0, 0, NULL };
82         struct msgs *mp;
83         struct smsg **dlist;
84
85         setlocale(LC_ALL, "");
86         invo_name = mhbasename(argv[0]);
87
88         /* read user profile/context */
89         context_read();
90
91         arguments = getarguments(invo_name, argc, argv, 1);
92         argp = arguments;
93
94         /*
95         ** Parse arguments
96         */
97         while ((cp = *argp++)) {
98                 if (*cp == '-') {
99                         switch (smatch(++cp, switches)) {
100                         case AMBIGSW:
101                                 ambigsw(cp, switches);
102                                 exit(EX_USAGE);
103                         case UNKWNSW:
104                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
105
106                         case HELPSW:
107                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
108                                 print_help(buf, switches, 1);
109                                 exit(argc == 2 ? EX_OK : EX_USAGE);
110                         case VERSIONSW:
111                                 print_version(invo_name);
112                                 exit(argc == 2 ? EX_OK : EX_USAGE);
113
114                         case DATESW:
115                                 if (datesw)
116                                         adios(EX_USAGE, NULL, "only one date field at a time");
117                                 if (!(datesw = *argp++) || *datesw == '-')
118                                         adios(EX_USAGE, NULL, "missing argument to %s",
119                                                         argp[-2]);
120                                 continue;
121
122                         case TEXTSW:
123                                 if (subjsort)
124                                         adios(EX_USAGE, NULL, "only one text field at a time");
125                                 if (!(subjsort = *argp++) || *subjsort == '-')
126                                         adios(EX_USAGE, NULL, "missing argument to %s",
127                                                         argp[-2]);
128                                 continue;
129
130                         case NSUBJSW:
131                                 subjsort = NULL;
132                                 continue;
133
134                         case LIMSW:
135                                 if (!(cp = *argp++) || *cp == '-')
136                                                 adios(EX_USAGE, 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(*cp) || !(datelimit = atoi(cp)))
144                                         adios(EX_USAGE, 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                 }
160                 if (*cp == '+' || *cp == '@') {
161                         if (folder)
162                                 adios(EX_USAGE, NULL, "only one folder at a time!");
163                         else
164                                 folder = getcpy(expandfol(cp));
165                 } else
166                         app_msgarg(&msgs, cp);
167         }
168
169         if (!msgs.size)
170                 app_msgarg(&msgs, seq_all);
171         if (!datesw)
172                 datesw = "date";
173         if (!folder)
174                 folder = getcurfol();
175         maildir = toabsdir(folder);
176
177         if (chdir(maildir) == NOTOK)
178                 adios(EX_OSERR, maildir, "unable to change directory to");
179
180         /* read folder and create message structure */
181         if (!(mp = folder_read(folder)))
182                 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
183
184         /* check for empty folder */
185         if (mp->nummsg == 0)
186                 adios(EX_DATAERR, 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                         exit(EX_USAGE);
192         seq_setprev(mp);  /* set the previous sequence */
193
194         if ((nmsgs = read_hdrs(mp, datesw)) <= 0)
195                 adios(EX_DATAERR, 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_xcalloc((size_t) (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                 struct smsg ***il, **fp, **dp;
226
227                 slist = (struct smsg **)
228                                 mh_xmalloc((nmsgs+1) * sizeof(*slist));
229                 memcpy((char *)slist, (char *)dlist, (nmsgs+1)*sizeof(*slist));
230                 qsort((char *)slist, nmsgs, sizeof(*slist),
231                                 (qsort_comp) subsort);
232
233                 /*
234                 ** make an inversion list so we can quickly find
235                 ** the collection of messages with the same subj
236                 ** given a message number.
237                 */
238                 il = (struct smsg ***) mh_xcalloc(mp->hghsel+1, sizeof(*il));
239                 if (! il)
240                         adios(EX_OSERR, NULL, "couldn't allocate msg list");
241                 for (i = 0; i < nmsgs; i++)
242                         il[slist[i]->s_msg] = &slist[i];
243                 /*
244                 ** make up the final list, chronological but with
245                 ** all the same subjects grouped together.
246                 */
247                 flist = (struct smsg **)
248                                 mh_xmalloc((nmsgs+1) * sizeof(*flist));
249                 fp = flist;
250                 for (dp = dlist; *dp;) {
251                         struct smsg **s = il[(*dp++)->s_msg];
252
253                         /* see if we already did this guy */
254                         if (! s)
255                                 continue;
256
257                         *fp++ = *s++;
258                         /*
259                         ** take the next message(s) if there is one,
260                         ** its subject isn't null and its subject
261                         ** is the same as this one and it's not too
262                         ** far away in time.
263                         */
264                         while (*s && (*s)->s_subj[0] && strcmp((*s)->s_subj, s[-1]->s_subj) == 0 && (datelimit == 0 || (*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
277         ** structures, each of which contains a message number.
278         */
279
280         rename_msgs(mp, dlist);
281
282         context_replace(curfolder, 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         return 0;
287 }
288
289 static int
290 read_hdrs(struct msgs *mp, char *datesw)
291 {
292         int msgnum;
293         struct tws tb;
294         struct smsg *s;
295
296         twscopy(&tb, dlocaltimenow());
297
298         smsgs = (struct smsg *) mh_xcalloc((size_t) (mp->hghsel - mp->lowsel + 2),
299                         sizeof(*smsgs));
300         if (smsgs == NULL)
301                 adios(EX_OSERR, NULL, "unable to allocate sort storage");
302
303         s = smsgs;
304         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
305                 if (is_selected(mp, msgnum)) {
306                         if (get_fields(datesw, msgnum, s)) {
307                                 s->s_msg = msgnum;
308                                 s++;
309                         }
310                 }
311         }
312         s->s_msg = 0;
313         return(s - smsgs);
314 }
315
316
317 /*
318 ** Parse the message and get the data or subject field,
319 ** if needed.
320 */
321
322 static int
323 get_fields(char *datesw, int msg, struct smsg *smsg)
324 {
325         int state;
326         int compnum;
327         char *msgnam, buf[BUFSIZ], nam[NAMESZ];
328         struct tws *tw;
329         char *datecomp = NULL, *subjcomp = NULL;
330         FILE *in;
331
332         if ((in = fopen(msgnam = m_name(msg), "r")) == NULL) {
333                 admonish(msgnam, "unable to read message");
334                 return (0);
335         }
336         for (compnum = 1, state = FLD;;) {
337                 switch (state = m_getfld(state, nam, buf, sizeof(buf), in)) {
338                 case FLD:
339                 case FLDEOF:
340                 case FLDPLUS:
341                         compnum++;
342                         if (!mh_strcasecmp(nam, datesw)) {
343                                 datecomp = add(buf, datecomp);
344                                 while (state == FLDPLUS) {
345                                         state = m_getfld(state, nam, buf,
346                                                         sizeof(buf), in);
347                                         datecomp = add(buf, datecomp);
348                                 }
349                                 if (!subjsort || subjcomp)
350                                         break;
351                         } else if (subjsort && !mh_strcasecmp(nam, subjsort)) {
352                                 subjcomp = add(buf, subjcomp);
353                                 while (state == FLDPLUS) {
354                                         state = m_getfld(state, nam, buf,
355                                                         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,
364                                                         sizeof(buf), in);
365                         }
366                         continue;
367
368                 case BODY:
369                 case BODYEOF:
370                 case FILEEOF:
371                         break;
372
373                 case LENERR:
374                 case FMTERR:
375                         if (state == LENERR || state == FMTERR)
376                                 admonish(NULL, "format error in message %d (header #%d)", 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(EX_SOFTWARE, 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",
398                                 datesw, msg);
399                 fstat(fileno(in), &st);
400                 smsg->s_clock = st.st_mtime;
401         } else {
402                 smsg->s_clock = dmktime(tw);
403         }
404
405         if (subjsort) {
406                 if (subjcomp) {
407                         /*
408                         ** try to make the subject "canonical": delete
409                         ** leading "re:", everything but letters & smash
410                         ** letters to lower case.
411                         */
412                         char  *cp, *cp2;
413                         unsigned char c;
414
415                         cp = subjcomp;
416                         cp2 = subjcomp;
417                         if (strcmp(subjsort, "subject") == 0) {
418                                 while ((c = *cp)) {
419                                         if (! isspace(c)) {
420                                                 if(uprf(cp, "re:"))
421                                                         cp += 2;
422                                                 else
423                                                         break;
424                                         }
425                                         cp++;
426                                 }
427                         }
428
429                         while ((c = *cp++)) {
430                                 if (isalnum(c))
431                                         *cp2++ = isupper(c) ? tolower(c) : c;
432                         }
433
434                         *cp2 = '\0';
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         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         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                 snprintf(oldname, sizeof (oldname), "%s/%d",
508                                 mp->foldpath, old);
509                 snprintf(newbuf, sizeof (newbuf), "%s/%d", mp->foldpath, new);
510                 ext_hook("ref-hook", oldname, newbuf);
511
512                 if (rename(oldname, newname) == NOTOK)
513                         adios(EX_IOERR, newname, "unable to rename %s to", oldname);
514
515                 copy_msg_flags(mp, new, old);
516                 if (mp->curmsg == old)
517                         seq_setcur(mp, new);
518
519                 if (nxt == endmsg)
520                         break;
521
522                 msg = nxt;
523         }
524 /* if (nxt != endmsg); */
525 /* rename_chain(mp, mlist, nxt, endmsg); */
526 }
527
528 static void
529 rename_msgs(struct msgs *mp, struct smsg **mlist)
530 {
531         int i, j, old, new;
532         seqset_t tmpset;
533         char f1[BUFSIZ], tmpfil[BUFSIZ];
534         char newbuf[MAXPATHLEN + 1];
535         struct smsg *sp;
536
537         strncpy(tmpfil, m_name(mp->hghmsg + 1), sizeof(tmpfil));
538
539         for (i = 0; i < nmsgs; i++) {
540                 if (! (sp = mlist[i]))
541                         continue;   /* did this one */
542
543                 j = sp - smsgs;
544                 if (j == i)
545                         continue;   /* this one doesn't move */
546
547                 /*
548                 ** the guy that was msg j is about to become msg i.
549                 ** rename 'j' to make a hole, then recursively rename
550                 ** guys to fill up the hole.
551                 */
552                 old = smsgs[j].s_msg;
553                 new = smsgs[i].s_msg;
554                 strncpy(f1, m_name(old), sizeof(f1));
555
556                 if (verbose)
557                         printf("renaming message chain from %d to %d\n",
558                                         old, new);
559
560                 /*
561                 ** Run the external hook to refile the old message as the
562                 ** temporary message number that is off of the end of the
563                 ** messages in the folder.
564                 */
565
566                 snprintf(f1, sizeof (f1), "%s/%d", mp->foldpath, old);
567                 snprintf(newbuf, sizeof (newbuf), "%s/%d",
568                                 mp->foldpath, mp->hghmsg + 1);
569                 ext_hook("ref-hook", f1, newbuf);
570
571                 if (rename(f1, tmpfil) == NOTOK)
572                         adios(EX_IOERR, tmpfil, "unable to rename %s to ", f1);
573
574                 get_msg_flags(mp, &tmpset, old);
575
576                 rename_chain(mp, mlist, j, i);
577
578                 /*
579                 ** Run the external hook to refile the temorary message number
580                 ** to the real place.
581                 */
582
583                 snprintf(f1, sizeof (f1), "%s/%d", mp->foldpath, new);
584                 ext_hook("ref-hook", newbuf, f1);
585
586                 if (rename(tmpfil, m_name(new)) == NOTOK)
587                         adios(EX_IOERR, m_name(new), "unable to rename %s to", tmpfil);
588
589                 set_msg_flags(mp, &tmpset, new);
590                 mp->msgflags |= SEQMOD;
591         }
592 }