]>
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.
18 * We allocate spaces for messages (msgs array)
19 * this number of elements at a time.
24 static struct swit switches
[] = {
44 { "rmmproc program", 0 },
54 static char maildir
[BUFSIZ
];
64 static void opnfolds (struct st_fold
*, int);
65 static void clsfolds (struct st_fold
*, int);
66 static void remove_files (int, char **);
67 static int m_file (char *, struct st_fold
*, int, int, int);
71 main (int argc
, char **argv
)
73 int linkf
= 0, preserve
= 0, filep
= 0;
74 int foldp
= 0, isdf
= 0, unlink_msgs
= 0;
75 int i
, msgnum
, nummsgs
, maxmsgs
;
76 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
77 char **argp
, **arguments
, **msgs
;
78 char *filevec
[NFOLDERS
+ 2];
79 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
80 struct st_fold folders
[NFOLDERS
+ 1];
84 setlocale(LC_ALL
, "");
86 invo_name
= r1bindex (argv
[0], '/');
88 /* read user profile/context */
91 arguments
= getarguments (invo_name
, argc
, argv
, 1);
95 * Allocate the initial space to record message
96 * names, ranges, and sequences.
100 if (!(msgs
= (char **) malloc ((size_t) (maxmsgs
* sizeof(*msgs
)))))
101 adios (NULL
, "unable to allocate storage");
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 if (!(msgs
= (char **) realloc (msgs
,
189 (size_t) (maxmsgs
* sizeof(*msgs
)))))
190 adios (NULL
, "unable to reallocate msgs storage");
192 msgs
[nummsgs
++] = cp
;
196 if (!context_find ("path"))
197 free (path ("./", TFOLDER
));
199 adios (NULL
, "no folder specified");
202 if (!nummsgs
&& !foldp
&& !filep
&& (cp
= getenv ("mhdraft")) && *cp
)
207 * We are refiling a file to the folders
210 if (folder
|| nummsgs
)
211 adios (NULL
, "use -file or some messages, not both");
212 opnfolds (folders
, foldp
);
213 for (i
= 0; i
< filep
; i
++)
214 if (m_file (files
[i
], folders
, foldp
, preserve
, 0))
216 /* If -nolink, then "remove" files */
218 remove_files (filep
, filevec
);
223 msgs
[nummsgs
++] = "cur";
225 folder
= getfolder (1);
226 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
228 if (chdir (maildir
) == NOTOK
)
229 adios (maildir
, "unable to change directory to");
231 /* read source folder and create message structure */
232 if (!(mp
= folder_read (folder
)))
233 adios (NULL
, "unable to read folder %s", folder
);
235 /* check for empty folder */
237 adios (NULL
, "no messages in %s", folder
);
239 /* parse the message range/sequence/name and set SELECTED */
240 for (msgnum
= 0; msgnum
< nummsgs
; msgnum
++)
241 if (!m_convert (mp
, msgs
[msgnum
]))
243 seq_setprev (mp
); /* set the previous-sequence */
245 /* create folder structures for each destination folder */
246 opnfolds (folders
, foldp
);
248 /* Link all the selected messages into destination folders.
250 * This causes the add hook to be run for messages that are
251 * linked into another folder. The refile hook is run for
252 * messages that are moved to another folder.
254 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
255 if (is_selected (mp
, msgnum
)) {
256 cp
= getcpy (m_name (msgnum
));
257 if (m_file (cp
, folders
, foldp
, preserve
, !linkf
))
264 * This is a hack. If we are using an external rmmproc,
265 * then save the current folder to the context file,
266 * so the external rmmproc will remove files from the correct
267 * directory. This should be moved to folder_delmsgs().
270 context_replace (pfolder
, folder
);
275 /* If -nolink, then "remove" messages from source folder.
277 * Note that folder_delmsgs does not call the delete hook
278 * because the message has already been handled above.
281 folder_delmsgs (mp
, unlink_msgs
, 1);
284 clsfolds (folders
, foldp
);
286 if (mp
->hghsel
!= mp
->curmsg
287 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
288 seq_setcur (mp
, mp
->hghsel
);
289 seq_save (mp
); /* synchronize message sequences */
291 context_replace (pfolder
, folder
); /* update current folder */
292 context_save (); /* save the context file */
293 folder_free (mp
); /* free folder structure */
299 * Read all the destination folders and
300 * create folder structures for all of them.
304 opnfolds (struct st_fold
*folders
, int nfolders
)
307 char nmaildir
[BUFSIZ
];
308 register struct st_fold
*fp
, *ep
;
309 register struct msgs
*mp
;
312 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
313 chdir (m_maildir (""));
314 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
316 if (stat (nmaildir
, &st
) == NOTOK
) {
318 adios (nmaildir
, "error on folder");
319 cp
= concat ("Create folder \"", nmaildir
, "\"? ", NULL
);
323 if (!makedir (nmaildir
))
324 adios (NULL
, "unable to create folder %s", nmaildir
);
327 if (chdir (nmaildir
) == NOTOK
)
328 adios (nmaildir
, "unable to change directory to");
329 if (!(mp
= folder_read (fp
->f_name
)))
330 adios (NULL
, "unable to read folder %s", fp
->f_name
);
341 * Set the Previous-Sequence and then sychronize the
342 * sequence file, for each destination folder.
346 clsfolds (struct st_fold
*folders
, int nfolders
)
348 register struct st_fold
*fp
, *ep
;
349 register struct msgs
*mp
;
351 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
360 * If you have a "rmmproc" defined, we called that
361 * to remove all the specified files. If "rmmproc"
362 * is not defined, then just unlink the files.
366 remove_files (int filep
, char **files
)
371 /* If rmmproc is defined, we use that */
373 vec
= files
++; /* vec[0] = filevec[0] */
374 files
[filep
] = NULL
; /* NULL terminate list */
377 vec
[0] = r1bindex (rmmproc
, '/');
378 execvp (rmmproc
, vec
);
379 adios (rmmproc
, "unable to exec");
382 /* Else just unlink the files */
383 files
++; /* advance past filevec[0] */
384 for (i
= 0; i
< filep
; i
++) {
385 if (unlink (files
[i
]) == NOTOK
)
386 admonish (files
[i
], "unable to unlink");
392 * Link (or copy) the message into each of
393 * the destination folders.
397 m_file (char *msgfile
, struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
400 struct st_fold
*fp
, *ep
;
402 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
403 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
, 1, 0, preserve
, nfolders
== 1 && refile
, maildir
)) == -1)