]>
diplodocus.org Git - nmh/blob - uip/refile.c
3 * refile.c -- move or link message(s) from a source folder
4 * -- into one or more destination folders
6 * This code is Copyright (c) 2002, by the authors of nmh. See the
7 * COPYRIGHT file in the root directory of the nmh distribution for
8 * complete copyright information.
16 #define REFILE_SWITCHES \
17 X("draft", 0, DRAFTSW) \
18 X("link", 0, LINKSW) \
19 X("nolink", 0, NLINKSW) \
20 X("preserve", 0, PRESSW) \
21 X("nopreserve", 0, NPRESSW) \
22 X("unlink", 0, UNLINKSW) \
23 X("nounlink", 0, NUNLINKSW) \
24 X("src +folder", 0, SRCSW) \
25 X("file file", 0, FILESW) \
26 X("rmmproc program", 0, RPROCSW) \
27 X("normmproc", 0, NRPRCSW) \
28 X("version", 0, VERSIONSW) \
29 X("help", 0, HELPSW) \
31 #define X(sw, minchars, id) id,
32 DEFINE_SWITCH_ENUM(REFILE
);
35 #define X(sw, minchars, id) { sw, minchars, id },
36 DEFINE_SWITCH_ARRAY(REFILE
, switches
);
39 static char maildir
[BUFSIZ
];
49 static void opnfolds (struct st_fold
*, int);
50 static void clsfolds (struct st_fold
*, int);
51 static void remove_files (int, char **);
52 static int m_file (char *, struct st_fold
*, int, int, int);
56 main (int argc
, char **argv
)
58 int linkf
= 0, preserve
= 0, filep
= 0;
59 int foldp
= 0, isdf
= 0, unlink_msgs
= 0;
61 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
62 char **argp
, **arguments
;
63 char *filevec
[NFOLDERS
+ 2];
64 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
65 struct st_fold folders
[NFOLDERS
+ 1];
66 struct msgs_array msgs
= { 0, 0, NULL
};
70 setlocale(LC_ALL
, "");
72 invo_name
= r1bindex (argv
[0], '/');
74 /* read user profile/context */
77 arguments
= getarguments (invo_name
, argc
, argv
, 1);
83 while ((cp
= *argp
++)) {
85 switch (smatch (++cp
, switches
)) {
87 ambigsw (cp
, switches
);
90 adios (NULL
, "-%s unknown\n", cp
);
93 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches] +folder ...",
95 print_help (buf
, switches
, 1);
98 print_version(invo_name
);
124 adios (NULL
, "only one source folder at a time!");
125 if (!(cp
= *argp
++) || *cp
== '-')
126 adios (NULL
, "missing argument to %s", argp
[-2]);
127 folder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
128 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
131 if (filep
> NFOLDERS
)
132 adios (NULL
, "only %d files allowed!", NFOLDERS
);
134 files
[filep
++] = getcpy (m_draft (NULL
, NULL
, 1, &isdf
));
137 if (filep
> NFOLDERS
)
138 adios (NULL
, "only %d files allowed!", NFOLDERS
);
139 if (!(cp
= *argp
++) || *cp
== '-')
140 adios (NULL
, "missing argument to %s", argp
[-2]);
141 files
[filep
++] = path (cp
, TFILE
);
145 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
146 adios (NULL
, "missing argument to %s", argp
[-2]);
153 if (*cp
== '+' || *cp
== '@') {
154 if (foldp
> NFOLDERS
)
155 adios (NULL
, "only %d folders allowed!", NFOLDERS
);
156 folders
[foldp
++].f_name
=
159 app_msgarg(&msgs
, cp
);
162 if (!context_find ("path"))
163 free (path ("./", TFOLDER
));
165 adios (NULL
, "no folder specified");
168 if (!msgs
.size
&& !foldp
&& !filep
&& (cp
= getenv ("mhdraft")) && *cp
)
173 * We are refiling a file to the folders
176 if (folder
|| msgs
.size
)
177 adios (NULL
, "use -file or some messages, not both");
178 opnfolds (folders
, foldp
);
179 for (i
= 0; i
< filep
; i
++)
180 if (m_file (files
[i
], folders
, foldp
, preserve
, 0))
182 /* If -nolink, then "remove" files */
184 remove_files (filep
, filevec
);
189 app_msgarg(&msgs
, "cur");
191 folder
= getfolder (1);
192 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
194 if (chdir (maildir
) == NOTOK
)
195 adios (maildir
, "unable to change directory to");
197 /* read source folder and create message structure */
198 if (!(mp
= folder_read (folder
)))
199 adios (NULL
, "unable to read folder %s", folder
);
201 /* check for empty folder */
203 adios (NULL
, "no messages in %s", folder
);
205 /* parse the message range/sequence/name and set SELECTED */
206 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
207 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
209 seq_setprev (mp
); /* set the previous-sequence */
211 /* create folder structures for each destination folder */
212 opnfolds (folders
, foldp
);
214 /* Link all the selected messages into destination folders.
216 * This causes the add hook to be run for messages that are
217 * linked into another folder. The refile hook is run for
218 * messages that are moved to another folder.
220 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
221 if (is_selected (mp
, msgnum
)) {
222 cp
= getcpy (m_name (msgnum
));
223 if (m_file (cp
, folders
, foldp
, preserve
, !linkf
))
230 * This is a hack. If we are using an external rmmproc,
231 * then save the current folder to the context file,
232 * so the external rmmproc will remove files from the correct
233 * directory. This should be moved to folder_delmsgs().
236 context_replace (pfolder
, folder
);
241 /* If -nolink, then "remove" messages from source folder.
243 * Note that folder_delmsgs does not call the delete hook
244 * because the message has already been handled above.
247 folder_delmsgs (mp
, unlink_msgs
, 1);
250 clsfolds (folders
, foldp
);
252 if (mp
->hghsel
!= mp
->curmsg
253 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
254 seq_setcur (mp
, mp
->hghsel
);
255 seq_save (mp
); /* synchronize message sequences */
257 context_replace (pfolder
, folder
); /* update current folder */
258 context_save (); /* save the context file */
259 folder_free (mp
); /* free folder structure */
266 * Read all the destination folders and
267 * create folder structures for all of them.
271 opnfolds (struct st_fold
*folders
, int nfolders
)
273 char nmaildir
[BUFSIZ
];
274 register struct st_fold
*fp
, *ep
;
275 register struct msgs
*mp
;
277 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
278 chdir (m_maildir (""));
279 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
281 create_folder (nmaildir
, 0, done
);
283 if (chdir (nmaildir
) == NOTOK
)
284 adios (nmaildir
, "unable to change directory to");
285 if (!(mp
= folder_read (fp
->f_name
)))
286 adios (NULL
, "unable to read folder %s", fp
->f_name
);
297 * Set the Previous-Sequence and then sychronize the
298 * sequence file, for each destination folder.
302 clsfolds (struct st_fold
*folders
, int nfolders
)
304 register struct st_fold
*fp
, *ep
;
305 register struct msgs
*mp
;
307 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
316 * If you have a "rmmproc" defined, we called that
317 * to remove all the specified files. If "rmmproc"
318 * is not defined, then just unlink the files.
322 remove_files (int filep
, char **files
)
327 /* If rmmproc is defined, we use that */
329 vec
= files
++; /* vec[0] = filevec[0] */
330 files
[filep
] = NULL
; /* NULL terminate list */
333 vec
[0] = r1bindex (rmmproc
, '/');
334 execvp (rmmproc
, vec
);
335 adios (rmmproc
, "unable to exec");
338 /* Else just unlink the files */
339 files
++; /* advance past filevec[0] */
340 for (i
= 0; i
< filep
; i
++) {
341 if (unlink (files
[i
]) == NOTOK
)
342 admonish (files
[i
], "unable to unlink");
348 * Link (or copy) the message into each of
349 * the destination folders.
353 m_file (char *msgfile
, struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
356 struct st_fold
*fp
, *ep
;
358 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
359 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
, 1, 0, preserve
, nfolders
== 1 && refile
, maildir
)) == -1)