* uip/sendsbr.c: replaced st_mtim with st_mtime, that's what
[mmh] / uip / rmm.c
1
2 /*
3  * rmm.c -- remove a message(s)
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/utils.h>
14
15 /*
16  * We allocate space for message names and ranges
17  * (msgs array) this number of elements at a time.
18  */
19 #define MAXMSGS  256
20
21 static struct swit switches[] = {
22 #define UNLINKSW      0
23     { "unlink", 0 },
24 #define NUNLINKSW    1
25     { "nounlink", 0 },
26 #define VERSIONSW     2
27     { "version", 0 },
28 #define HELPSW        3
29     { "help", 0 },
30     { NULL, 0 }
31 };
32
33
34 int
35 main (int argc, char **argv)
36 {
37     int nummsgs, maxmsgs, msgnum, unlink_msgs = 0;
38     char *cp, *maildir, *folder = NULL;
39     char buf[BUFSIZ], **argp;
40     char **arguments, **msgs;
41     struct msgs *mp;
42
43 #ifdef LOCALE
44     setlocale(LC_ALL, "");
45 #endif
46     invo_name = r1bindex (argv[0], '/');
47
48     /* read user profile/context */
49     context_read();
50
51     arguments = getarguments (invo_name, argc, argv, 1);
52     argp = arguments;
53
54     /*
55      * Allocate the initial space to record message
56      * names and ranges.
57      */
58     nummsgs = 0;
59     maxmsgs = MAXMSGS;
60     msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs)));
61
62     /* parse arguments */
63     while ((cp = *argp++)) {
64         if (*cp == '-') {
65             switch (smatch (++cp, switches)) {
66             case AMBIGSW: 
67                 ambigsw (cp, switches);
68                 done (1);
69             case UNKWNSW: 
70                 adios (NULL, "-%s unknown\n", cp);
71
72             case HELPSW: 
73                 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
74                           invo_name);
75                 print_help (buf, switches, 1);
76                 done (1);
77             case VERSIONSW:
78                 print_version(invo_name);
79                 done (1);
80
81             case UNLINKSW:
82                 unlink_msgs++;
83                 continue;
84             case NUNLINKSW:
85                 unlink_msgs = 0;
86                 continue;
87             }
88         }
89         if (*cp == '+' || *cp == '@') {
90             if (folder)
91                 adios (NULL, "only one folder at a time!");
92             else
93                 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
94         } else {
95             /*
96              * Check if we need to allocate more space
97              * for message names/ranges.
98              */
99             if (nummsgs >= maxmsgs){
100                 maxmsgs += MAXMSGS;
101                 msgs = (char **) mh_xrealloc (msgs,
102                     (size_t) (maxmsgs * sizeof(*msgs)));
103             }
104             msgs[nummsgs++] = cp;
105         }
106     }
107
108     if (!context_find ("path"))
109         free (path ("./", TFOLDER));
110     if (!nummsgs)
111         msgs[nummsgs++] = "cur";
112     if (!folder)
113         folder = getfolder (1);
114     maildir = m_maildir (folder);
115
116     if (chdir (maildir) == NOTOK)
117         adios (maildir, "unable to change directory to");
118
119     /* read folder and create message structure */
120     if (!(mp = folder_read (folder)))
121         adios (NULL, "unable to read folder %s", folder);
122
123     /* check for empty folder */
124     if (mp->nummsg == 0)
125         adios (NULL, "no messages in %s", folder);
126
127     /* parse all the message ranges/sequences and set SELECTED */
128     for (msgnum = 0; msgnum < nummsgs; msgnum++)
129         if (!m_convert (mp, msgs[msgnum]))
130             done (1);
131     seq_setprev (mp);           /* set the previous-sequence      */
132
133     /*
134      * This is hackish.  If we are using a external rmmproc,
135      * then we need to update the current folder in the
136      * context so the external rmmproc will remove files
137      * from the correct directory.  This should be moved to
138      * folder_delmsgs().
139      */
140     if (rmmproc) {
141         context_replace (pfolder, folder);
142         context_save ();
143         fflush (stdout);
144     }
145
146     /* "remove" the SELECTED messages */
147     folder_delmsgs (mp, unlink_msgs, 0);
148
149     seq_save (mp);              /* synchronize message sequences  */
150     context_replace (pfolder, folder);  /* update current folder   */
151     context_save ();                    /* save the context file   */
152     folder_free (mp);                   /* free folder structure   */
153     return done (0);
154 }