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