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