]> diplodocus.org Git - nmh/blob - uip/rmm.c
Escape literal leading full stop in man/new.man.
[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 if (nmh_init(argv[0], 1)) { return 1; }
41
42 arguments = getarguments (invo_name, argc, argv, 1);
43 argp = arguments;
44
45 /* parse arguments */
46 while ((cp = *argp++)) {
47 if (*cp == '-') {
48 switch (smatch (++cp, switches)) {
49 case AMBIGSW:
50 ambigsw (cp, switches);
51 done (1);
52 case UNKWNSW:
53 adios (NULL, "-%s unknown\n", cp);
54
55 case HELPSW:
56 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
57 invo_name);
58 print_help (buf, switches, 1);
59 done (0);
60 case VERSIONSW:
61 print_version(invo_name);
62 done (0);
63
64 case UNLINKSW:
65 unlink_msgs++;
66 continue;
67 case NUNLINKSW:
68 unlink_msgs = 0;
69 continue;
70
71 case RPROCSW:
72 if (!(rmmproc = *argp++) || *rmmproc == '-')
73 adios (NULL, "missing argument to %s", argp[-2]);
74 continue;
75 case NRPRCSW:
76 rmmproc = NULL;
77 continue;
78 }
79 }
80 if (*cp == '+' || *cp == '@') {
81 if (folder)
82 adios (NULL, "only one folder at a time!");
83 else
84 folder = pluspath (cp);
85 } else
86 app_msgarg(&msgs, cp);
87 }
88
89 if (!context_find ("path"))
90 free (path ("./", TFOLDER));
91 if (!msgs.size)
92 app_msgarg(&msgs, "cur");
93 if (!folder)
94 folder = getfolder (1);
95 maildir = m_maildir (folder);
96
97 if (chdir (maildir) == NOTOK)
98 adios (maildir, "unable to change directory to");
99
100 /* read folder and create message structure */
101 if (!(mp = folder_read (folder, 1)))
102 adios (NULL, "unable to read folder %s", folder);
103
104 /* check for empty folder */
105 if (mp->nummsg == 0)
106 adios (NULL, "no messages in %s", folder);
107
108 /* parse all the message ranges/sequences and set SELECTED */
109 for (msgnum = 0; msgnum < msgs.size; msgnum++)
110 if (!m_convert (mp, msgs.msgs[msgnum]))
111 done (1);
112 seq_setprev (mp); /* set the previous-sequence */
113
114 /*
115 * As part of the new world locking order, folder_delmsgs() now updates
116 * the sequence and context for us. But since folder_delmsgs() doesn't
117 * have access to the folder name, change the context now.
118 */
119
120 context_replace (pfolder, folder);
121
122 /* "remove" the SELECTED messages */
123 folder_delmsgs (mp, unlink_msgs, 0);
124
125 folder_free (mp); /* free folder structure */
126 done (0);
127 return 1;
128 }