]>
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
8 * This code is Copyright (c) 2002, by the authors of nmh. See the
9 * COPYRIGHT file in the root directory of the nmh distribution for
10 * complete copyright information.
19 * We allocate spaces for messages (msgs array)
20 * this number of elements at a time.
25 static struct swit switches
[] = {
45 { "rmmproc program", 0 },
55 static char maildir
[BUFSIZ
];
65 static void opnfolds (struct st_fold
*, int);
66 static void clsfolds (struct st_fold
*, int);
67 static void remove_files (int, char **);
68 static int m_file (char *, struct st_fold
*, int, int, int);
72 main (int argc
, char **argv
)
74 int linkf
= 0, preserve
= 0, filep
= 0;
75 int foldp
= 0, isdf
= 0, unlink_msgs
= 0;
76 int i
, msgnum
, nummsgs
, maxmsgs
;
77 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
78 char **argp
, **arguments
, **msgs
;
79 char *filevec
[NFOLDERS
+ 2];
80 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
81 struct st_fold folders
[NFOLDERS
+ 1];
85 setlocale(LC_ALL
, "");
87 invo_name
= r1bindex (argv
[0], '/');
89 /* read user profile/context */
92 arguments
= getarguments (invo_name
, argc
, argv
, 1);
96 * Allocate the initial space to record message
97 * names, ranges, and sequences.
101 msgs
= (char **) mh_xmalloc ((size_t) (maxmsgs
* sizeof(*msgs
)));
106 while ((cp
= *argp
++)) {
108 switch (smatch (++cp
, switches
)) {
110 ambigsw (cp
, switches
);
113 adios (NULL
, "-%s unknown\n", cp
);
116 snprintf (buf
, sizeof(buf
), "%s [msgs] [switches] +folder ...",
118 print_help (buf
, switches
, 1);
121 print_version(invo_name
);
147 adios (NULL
, "only one source folder at a time!");
148 if (!(cp
= *argp
++) || *cp
== '-')
149 adios (NULL
, "missing argument to %s", argp
[-2]);
150 folder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
151 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
154 if (filep
> NFOLDERS
)
155 adios (NULL
, "only %d files allowed!", NFOLDERS
);
157 files
[filep
++] = getcpy (m_draft (NULL
, NULL
, 1, &isdf
));
160 if (filep
> NFOLDERS
)
161 adios (NULL
, "only %d files allowed!", NFOLDERS
);
162 if (!(cp
= *argp
++) || *cp
== '-')
163 adios (NULL
, "missing argument to %s", argp
[-2]);
164 files
[filep
++] = path (cp
, TFILE
);
168 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
169 adios (NULL
, "missing argument to %s", argp
[-2]);
176 if (*cp
== '+' || *cp
== '@') {
177 if (foldp
> NFOLDERS
)
178 adios (NULL
, "only %d folders allowed!", NFOLDERS
);
179 folders
[foldp
++].f_name
=
180 path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
183 * Check if we need to allocate more space
184 * for message names, ranges, and sequences.
186 if (nummsgs
>= maxmsgs
) {
188 msgs
= (char **) mh_xrealloc (msgs
,
189 (size_t) (maxmsgs
* sizeof(*msgs
)));
191 msgs
[nummsgs
++] = cp
;
195 if (!context_find ("path"))
196 free (path ("./", TFOLDER
));
198 adios (NULL
, "no folder specified");
201 if (!nummsgs
&& !foldp
&& !filep
&& (cp
= getenv ("mhdraft")) && *cp
)
206 * We are refiling a file to the folders
209 if (folder
|| nummsgs
)
210 adios (NULL
, "use -file or some messages, not both");
211 opnfolds (folders
, foldp
);
212 for (i
= 0; i
< filep
; i
++)
213 if (m_file (files
[i
], folders
, foldp
, preserve
, 0))
215 /* If -nolink, then "remove" files */
217 remove_files (filep
, filevec
);
222 msgs
[nummsgs
++] = "cur";
224 folder
= getfolder (1);
225 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
227 if (chdir (maildir
) == NOTOK
)
228 adios (maildir
, "unable to change directory to");
230 /* read source folder and create message structure */
231 if (!(mp
= folder_read (folder
)))
232 adios (NULL
, "unable to read folder %s", folder
);
234 /* check for empty folder */
236 adios (NULL
, "no messages in %s", folder
);
238 /* parse the message range/sequence/name and set SELECTED */
239 for (msgnum
= 0; msgnum
< nummsgs
; msgnum
++)
240 if (!m_convert (mp
, msgs
[msgnum
]))
242 seq_setprev (mp
); /* set the previous-sequence */
244 /* create folder structures for each destination folder */
245 opnfolds (folders
, foldp
);
247 /* Link all the selected messages into destination folders.
249 * This causes the add hook to be run for messages that are
250 * linked into another folder. The refile hook is run for
251 * messages that are moved to another folder.
253 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
254 if (is_selected (mp
, msgnum
)) {
255 cp
= getcpy (m_name (msgnum
));
256 if (m_file (cp
, folders
, foldp
, preserve
, !linkf
))
263 * This is a hack. If we are using an external rmmproc,
264 * then save the current folder to the context file,
265 * so the external rmmproc will remove files from the correct
266 * directory. This should be moved to folder_delmsgs().
269 context_replace (pfolder
, folder
);
274 /* If -nolink, then "remove" messages from source folder.
276 * Note that folder_delmsgs does not call the delete hook
277 * because the message has already been handled above.
280 folder_delmsgs (mp
, unlink_msgs
, 1);
283 clsfolds (folders
, foldp
);
285 if (mp
->hghsel
!= mp
->curmsg
286 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
287 seq_setcur (mp
, mp
->hghsel
);
288 seq_save (mp
); /* synchronize message sequences */
290 context_replace (pfolder
, folder
); /* update current folder */
291 context_save (); /* save the context file */
292 folder_free (mp
); /* free folder structure */
298 * Read all the destination folders and
299 * create folder structures for all of them.
303 opnfolds (struct st_fold
*folders
, int nfolders
)
306 char nmaildir
[BUFSIZ
];
307 register struct st_fold
*fp
, *ep
;
308 register struct msgs
*mp
;
311 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
312 chdir (m_maildir (""));
313 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
315 create_folder (nmaildir
, 0, done
);
317 if (chdir (nmaildir
) == NOTOK
)
318 adios (nmaildir
, "unable to change directory to");
319 if (!(mp
= folder_read (fp
->f_name
)))
320 adios (NULL
, "unable to read folder %s", fp
->f_name
);
331 * Set the Previous-Sequence and then sychronize the
332 * sequence file, for each destination folder.
336 clsfolds (struct st_fold
*folders
, int nfolders
)
338 register struct st_fold
*fp
, *ep
;
339 register struct msgs
*mp
;
341 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
350 * If you have a "rmmproc" defined, we called that
351 * to remove all the specified files. If "rmmproc"
352 * is not defined, then just unlink the files.
356 remove_files (int filep
, char **files
)
361 /* If rmmproc is defined, we use that */
363 vec
= files
++; /* vec[0] = filevec[0] */
364 files
[filep
] = NULL
; /* NULL terminate list */
367 vec
[0] = r1bindex (rmmproc
, '/');
368 execvp (rmmproc
, vec
);
369 adios (rmmproc
, "unable to exec");
372 /* Else just unlink the files */
373 files
++; /* advance past filevec[0] */
374 for (i
= 0; i
< filep
; i
++) {
375 if (unlink (files
[i
]) == NOTOK
)
376 admonish (files
[i
], "unable to unlink");
382 * Link (or copy) the message into each of
383 * the destination folders.
387 m_file (char *msgfile
, struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
390 struct st_fold
*fp
, *ep
;
392 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
393 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
, 1, 0, preserve
, nfolders
== 1 && refile
, maildir
)) == -1)