]> diplodocus.org Git - nmh/blob - uip/rmm.c
8882b8f623033bbf72c81aeb7cb85a9613575a12
[nmh] / uip / rmm.c
1
2 /*
3 * rmm.c -- remove a message(s)
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/utils.h>
12
13 #define RMM_SWITCHES \
14 X("unlink", 0, UNLINKSW) \
15 X("nounlink", 0, NUNLINKSW) \
16 X("rmmproc program", 0, RPROCSW) \
17 X("normmproc", 0, NRPRCSW) \
18 X("version", 0, VERSIONSW) \
19 X("help", 0, HELPSW) \
20
21 #define X(sw, minchars, id) id,
22 DEFINE_SWITCH_ENUM(RMM);
23 #undef X
24
25 #define X(sw, minchars, id) { sw, minchars, id },
26 DEFINE_SWITCH_ARRAY(RMM, switches);
27 #undef X
28
29
30 int
31 main (int argc, char **argv)
32 {
33 int msgnum, unlink_msgs = 0;
34 char *cp, *maildir, *folder = NULL;
35 char buf[BUFSIZ], **argp;
36 char **arguments;
37 struct msgs_array msgs = { 0, 0, NULL };
38 struct msgs *mp;
39
40 setlocale(LC_ALL, "");
41 invo_name = r1bindex (argv[0], '/');
42
43 /* read user profile/context */
44 context_read();
45
46 arguments = getarguments (invo_name, argc, argv, 1);
47 argp = arguments;
48
49 /* parse arguments */
50 while ((cp = *argp++)) {
51 if (*cp == '-') {
52 switch (smatch (++cp, switches)) {
53 case AMBIGSW:
54 ambigsw (cp, switches);
55 done (1);
56 case UNKWNSW:
57 adios (NULL, "-%s unknown\n", cp);
58
59 case HELPSW:
60 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
61 invo_name);
62 print_help (buf, switches, 1);
63 done (0);
64 case VERSIONSW:
65 print_version(invo_name);
66 done (0);
67
68 case UNLINKSW:
69 unlink_msgs++;
70 continue;
71 case NUNLINKSW:
72 unlink_msgs = 0;
73 continue;
74
75 case RPROCSW:
76 if (!(rmmproc = *argp++) || *rmmproc == '-')
77 adios (NULL, "missing argument to %s", argp[-2]);
78 continue;
79 case NRPRCSW:
80 rmmproc = NULL;
81 continue;
82 }
83 }
84 if (*cp == '+' || *cp == '@') {
85 if (folder)
86 adios (NULL, "only one folder at a time!");
87 else
88 folder = pluspath (cp);
89 } else
90 app_msgarg(&msgs, cp);
91 }
92
93 if (!context_find ("path"))
94 free (path ("./", TFOLDER));
95 if (!msgs.size)
96 app_msgarg(&msgs, "cur");
97 if (!folder)
98 folder = getfolder (1);
99 maildir = m_maildir (folder);
100
101 if (chdir (maildir) == NOTOK)
102 adios (maildir, "unable to change directory to");
103
104 /* read folder and create message structure */
105 if (!(mp = folder_read (folder, 1)))
106 adios (NULL, "unable to read folder %s", folder);
107
108 /* check for empty folder */
109 if (mp->nummsg == 0)
110 adios (NULL, "no messages in %s", folder);
111
112 /* parse all the message ranges/sequences and set SELECTED */
113 for (msgnum = 0; msgnum < msgs.size; msgnum++)
114 if (!m_convert (mp, msgs.msgs[msgnum]))
115 done (1);
116 seq_setprev (mp); /* set the previous-sequence */
117
118 /*
119 * As part of the new world locking order, folder_delmsgs() now updates
120 * the sequence and context for us. But since folder_delmsgs() doesn't
121 * have access to the folder name, change the context now.
122 */
123
124 context_replace (pfolder, folder);
125
126 /* "remove" the SELECTED messages */
127 folder_delmsgs (mp, unlink_msgs, 0);
128
129 folder_free (mp); /* free folder structure */
130 done (0);
131 return 1;
132 }