s/pfolder/curfolder/g
[mmh] / uip / rmm.c
1 /*
2 ** rmm.c -- remove a message(s)
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/utils.h>
11
12 static struct swit switches[] = {
13 #define UNLINKSW  0
14         { "unlink", 0 },
15 #define NUNLINKSW  1
16         { "nounlink", 0 },
17 #define VERSIONSW  2
18         { "version", 0 },
19 #define HELPSW  3
20         { "help", 0 },
21         { NULL, 0 }
22 };
23
24
25 int
26 main(int argc, char **argv)
27 {
28         int msgnum, unlink_msgs = 0;
29         char *cp, *maildir, *folder = NULL;
30         char buf[BUFSIZ], **argp;
31         char **arguments;
32         struct msgs_array msgs = { 0, 0, NULL };
33         struct msgs *mp;
34
35 #ifdef LOCALE
36         setlocale(LC_ALL, "");
37 #endif
38         invo_name = mhbasename(argv[0]);
39
40         /* read user profile/context */
41         context_read();
42
43         arguments = getarguments(invo_name, argc, argv, 1);
44         argp = arguments;
45
46         /* parse arguments */
47         while ((cp = *argp++)) {
48                 if (*cp == '-') {
49                         switch (smatch(++cp, switches)) {
50                         case AMBIGSW:
51                                 ambigsw(cp, switches);
52                                 done(1);
53                         case UNKWNSW:
54                                 adios(NULL, "-%s unknown\n", cp);
55
56                         case HELPSW:
57                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
58                                 print_help(buf, switches, 1);
59                                 done(1);
60                         case VERSIONSW:
61                                 print_version(invo_name);
62                                 done(1);
63
64                         case UNLINKSW:
65                                 unlink_msgs++;
66                                 continue;
67                         case NUNLINKSW:
68                                 unlink_msgs = 0;
69                                 continue;
70                         }
71                 }
72                 if (*cp == '+' || *cp == '@') {
73                         if (folder)
74                                 adios(NULL, "only one folder at a time!");
75                         else
76                                 folder = getcpy(expandfol(cp));
77                 } else
78                         app_msgarg(&msgs, cp);
79         }
80
81         if (!msgs.size)
82                 app_msgarg(&msgs, "cur");
83         if (!folder)
84                 folder = getcurfol();
85         maildir = toabsdir(folder);
86
87         if (chdir(maildir) == NOTOK)
88                 adios(maildir, "unable to change directory to");
89
90         /* read folder and create message structure */
91         if (!(mp = folder_read(folder)))
92                 adios(NULL, "unable to read folder %s", folder);
93
94         /* check for empty folder */
95         if (mp->nummsg == 0)
96                 adios(NULL, "no messages in %s", folder);
97
98         /* parse all the message ranges/sequences and set SELECTED */
99         for (msgnum = 0; msgnum < msgs.size; msgnum++)
100                 if (!m_convert(mp, msgs.msgs[msgnum]))
101                         done(1);
102         seq_setprev(mp);  /* set the previous-sequence */
103
104         /*
105         ** This is hackish.  If we are using a external rmmproc,
106         ** then we need to update the current folder in the
107         ** context so the external rmmproc will remove files
108         ** from the correct directory.  This should be moved to
109         ** folder_delmsgs().
110         */
111         if (rmmproc) {
112                 context_replace(curfolder, folder);
113                 context_save();
114                 fflush(stdout);
115         }
116
117         /* "remove" the SELECTED messages */
118         folder_delmsgs(mp, unlink_msgs, 0);
119
120         seq_save(mp);  /* synchronize message sequences  */
121         context_replace(curfolder, folder);  /* update current folder   */
122         context_save();  /* save the context file   */
123         folder_free(mp);  /* free folder structure   */
124         done(0);
125         return 1;
126 }