- int linkf = 0, preserve = 0, filep = 0;
- int foldp = 0, isdf = 0, unlink_msgs = 0;
- int i, msgnum;
- char *cp, *folder = NULL, buf[BUFSIZ];
- char **argp, **arguments;
- char *filevec[NFOLDERS + 2];
- char **files = &filevec[1]; /* leave room for remove_files:vec[0] */
- struct st_fold folders[NFOLDERS + 1];
- struct msgs_array msgs = { 0, 0, NULL };
- struct msgs *mp;
-
-#ifdef LOCALE
- setlocale(LC_ALL, "");
-#endif
- invo_name = r1bindex (argv[0], '/');
-
- /* read user profile/context */
- context_read();
-
- arguments = getarguments (invo_name, argc, argv, 1);
- argp = arguments;
-
- /*
- * Parse arguments
- */
- while ((cp = *argp++)) {
- if (*cp == '-') {
- switch (smatch (++cp, switches)) {
- case AMBIGSW:
- ambigsw (cp, switches);
- done (1);
- case UNKWNSW:
- adios (NULL, "-%s unknown\n", cp);
-
- case HELPSW:
- snprintf (buf, sizeof(buf), "%s [msgs] [switches] +folder ...",
- invo_name);
- print_help (buf, switches, 1);
- done (1);
- case VERSIONSW:
- print_version(invo_name);
- done (1);
-
- case LINKSW:
- linkf++;
- continue;
- case NLINKSW:
- linkf = 0;
- continue;
-
- case PRESSW:
- preserve++;
- continue;
- case NPRESSW:
- preserve = 0;
- continue;
-
- case UNLINKSW:
- unlink_msgs++;
- continue;
- case NUNLINKSW:
- unlink_msgs = 0;
- continue;
-
- case SRCSW:
- if (folder)
- adios (NULL, "only one source folder at a time!");
- if (!(cp = *argp++) || *cp == '-')
- adios (NULL, "missing argument to %s", argp[-2]);
- folder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
- *cp != '@' ? TFOLDER : TSUBCWF);
- continue;
- case DRAFTSW:
- if (filep > NFOLDERS)
- adios (NULL, "only %d files allowed!", NFOLDERS);
- isdf = 0;
- files[filep++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
- continue;
- case FILESW:
- if (filep > NFOLDERS)
- adios (NULL, "only %d files allowed!", NFOLDERS);
- if (!(cp = *argp++) || *cp == '-')
- adios (NULL, "missing argument to %s", argp[-2]);
- files[filep++] = path (cp, TFILE);
- continue;
-
- case RPROCSW:
- if (!(rmmproc = *argp++) || *rmmproc == '-')
- adios (NULL, "missing argument to %s", argp[-2]);
- continue;
- case NRPRCSW:
- rmmproc = NULL;
- continue;
- }
+ int linkf = 0, filep = 0;
+ int foldp = 0;
+ int i, msgnum;
+ char *cp, *folder = NULL, buf[BUFSIZ];
+ char **argp, **arguments;
+ char *filevec[NFOLDERS + 1];
+ char **files = filevec;
+ struct st_fold folders[NFOLDERS + 1];
+ struct msgs_array msgs = { 0, 0, NULL };
+ struct msgs *mp;
+
+ setlocale(LC_ALL, "");
+ invo_name = mhbasename(argv[0]);
+
+ context_read();
+
+ arguments = getarguments(invo_name, argc, argv, 1);
+ argp = arguments;
+
+ /*
+ ** Parse arguments
+ */
+ while ((cp = *argp++)) {
+ if (*cp == '-') {
+ switch (smatch(++cp, switches)) {
+ case AMBIGSW:
+ ambigsw(cp, switches);
+ exit(EX_USAGE);
+ case UNKWNSW:
+ adios(EX_USAGE, NULL, "-%s unknown\n", cp);
+
+ case HELPSW:
+ snprintf(buf, sizeof(buf), "%s [msgs] [switches] +folder ...", invo_name);
+ print_help(buf, switches, 1);
+ exit(argc == 2 ? EX_OK : EX_USAGE);
+ case VERSIONSW:
+ print_version(invo_name);
+ exit(argc == 2 ? EX_OK : EX_USAGE);
+
+ case LINKSW:
+ linkf++;
+ continue;
+ case NLINKSW:
+ linkf = 0;
+ continue;
+
+ case SRCSW:
+ if (folder)
+ adios(EX_USAGE, NULL, "only one source folder at a time!");
+ if (!(cp = *argp++) || *cp == '-')
+ adios(EX_USAGE, NULL, "missing argument to %s",
+ argp[-2]);
+ folder = getcpy(expandfol(cp));
+ continue;
+ case FILESW:
+ if (filep > NFOLDERS)
+ adios(EX_USAGE, NULL, "only %d files allowed!",
+ NFOLDERS);
+ if (!(cp = *argp++) || *cp == '-')
+ adios(EX_USAGE, NULL, "missing argument to %s",
+ argp[-2]);
+ files[filep++] = getcpy(expanddir(cp));
+ continue;
+ }
+ }
+ if (*cp == '+' || *cp == '@') {
+ if (foldp > NFOLDERS)
+ adios(EX_USAGE, NULL, "only %d folders allowed!",
+ NFOLDERS);
+ folders[foldp++].f_name = getcpy(expandfol(cp));
+ } else
+ app_msgarg(&msgs, cp);
+ }
+
+ if (foldp == 0)
+ adios(EX_USAGE, NULL, "no folder specified");
+
+ /*
+ ** We are refiling a file to the folders
+ */
+ if (filep > 0) {
+ if (folder || msgs.size)
+ adios(EX_USAGE, NULL, "use -file or msgs, not both");
+ opnfolds(folders, foldp);
+ for (i = 0; i < filep; i++)
+ if (m_file(files[i], folders, foldp, 0))
+ exit(EX_IOERR);
+ /* If -nolink, then unlink files */
+ if (!linkf) {
+ int i;
+ char **files = filevec;
+
+ /* just unlink the files */
+ for (i = 0; i < filep; i++) {
+ if (unlink(files[i]) == NOTOK)
+ admonish(files[i], "unable to unlink");
+ }
+ }
+ exit(EX_OK);
+ }
+
+ if (!msgs.size)
+ app_msgarg(&msgs, seq_cur);
+ if (!folder)
+ folder = getcurfol();
+ strncpy(maildir, toabsdir(folder), sizeof(maildir));
+
+ if (chdir(maildir) == NOTOK)
+ adios(EX_OSERR, maildir, "unable to change directory to");
+
+ /* read source folder and create message structure */
+ if (!(mp = folder_read(folder)))
+ adios(EX_IOERR, NULL, "unable to read folder %s", folder);
+
+ /* check for empty folder */
+ if (mp->nummsg == 0)
+ adios(EX_DATAERR, NULL, "no messages in %s", folder);
+
+ /* parse the message range/sequence/name and set SELECTED */
+ for (msgnum = 0; msgnum < msgs.size; msgnum++)
+ if (!m_convert(mp, msgs.msgs[msgnum]))
+ exit(EX_SOFTWARE);
+ seq_setprev(mp); /* set the previous-sequence */
+
+ /* create folder structures for each destination folder */
+ opnfolds(folders, foldp);
+
+ /* Link all the selected messages into destination folders.
+ **
+ ** This causes the add hook to be run for messages that are
+ ** linked into another folder. The refile hook is run for
+ ** messages that are moved to another folder.
+ */
+ for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
+ if (is_selected(mp, msgnum)) {
+ cp = getcpy(m_name(msgnum));
+ if (m_file(cp, folders, foldp, !linkf))
+ exit(EX_IOERR);
+ free(cp);
+ }
+ }
+
+ /*
+ ** If -nolink, then remove (= unlink) messages from source folder.
+ **
+ ** Note that folder_delmsgs does not call the delete hook
+ ** because the message has already been handled above.
+ */
+ if (!linkf) {
+ folder_delmsgs(mp, 0);