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