]>
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.
15 #define REFILE_SWITCHES \
16 X("draft", 0, DRAFTSW) \
17 X("link", 0, LINKSW) \
18 X("nolink", 0, NLINKSW) \
19 X("preserve", 0, PRESSW) \
20 X("nopreserve", 0, NPRESSW) \
21 X("retainsequences", 0, RETAINSEQSSW) \
22 X("noretainsequences", 0, NRETAINSEQSSW) \
23 X("unlink", 0, UNLINKSW) \
24 X("nounlink", 0, NUNLINKSW) \
25 X("src +folder", 0, SRCSW) \
26 X("file file", 0, FILESW) \
27 X("rmmproc program", 0, RPROCSW) \
28 X("normmproc", 0, NRPRCSW) \
29 X("version", 0, VERSIONSW) \
30 X("help", 0, HELPSW) \
32 #define X(sw, minchars, id) id,
33 DEFINE_SWITCH_ENUM(REFILE
);
36 #define X(sw, minchars, id) { sw, minchars, id },
37 DEFINE_SWITCH_ARRAY(REFILE
, switches
);
40 static char maildir
[BUFSIZ
];
50 static void opnfolds (struct st_fold
*, int);
51 static void clsfolds (struct st_fold
*, int);
52 static void remove_files (int, char **);
53 static int m_file (struct msgs
*, char *, int, struct st_fold
*, int, int, int);
54 static void copy_seqs (struct msgs
*, int, struct msgs
*, int);
58 main (int argc
, char **argv
)
60 int linkf
= 0, preserve
= 0, retainseqs
= 0, filep
= 0;
61 int foldp
= 0, isdf
= 0, unlink_msgs
= 0;
63 char *cp
, *folder
= NULL
, buf
[BUFSIZ
];
64 char **argp
, **arguments
;
65 char *filevec
[NFOLDERS
+ 2];
66 char **files
= &filevec
[1]; /* leave room for remove_files:vec[0] */
67 struct st_fold folders
[NFOLDERS
+ 1];
68 struct msgs_array msgs
= { 0, 0, NULL
};
71 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
);
131 adios (NULL
, "only one source folder at a time!");
132 if (!(cp
= *argp
++) || *cp
== '-')
133 adios (NULL
, "missing argument to %s", argp
[-2]);
134 folder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
135 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
138 if (filep
> NFOLDERS
)
139 adios (NULL
, "only %d files allowed!", NFOLDERS
);
141 files
[filep
++] = getcpy (m_draft (NULL
, NULL
, 1, &isdf
));
144 if (filep
> NFOLDERS
)
145 adios (NULL
, "only %d files allowed!", NFOLDERS
);
146 if (!(cp
= *argp
++) || *cp
== '-')
147 adios (NULL
, "missing argument to %s", argp
[-2]);
148 files
[filep
++] = path (cp
, TFILE
);
152 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
153 adios (NULL
, "missing argument to %s", argp
[-2]);
160 if (*cp
== '+' || *cp
== '@') {
161 if (foldp
> NFOLDERS
)
162 adios (NULL
, "only %d folders allowed!", NFOLDERS
);
163 folders
[foldp
++].f_name
=
166 app_msgarg(&msgs
, cp
);
169 if (!context_find ("path"))
170 free (path ("./", TFOLDER
));
172 adios (NULL
, "no folder specified");
175 * We are refiling a file to the folders
178 if (folder
|| msgs
.size
)
179 adios (NULL
, "use -file or some messages, not both");
180 opnfolds (folders
, foldp
);
181 for (i
= 0; i
< filep
; i
++)
182 if (m_file (0, files
[i
], 0, folders
, foldp
, preserve
, 0))
184 /* If -nolink, then "remove" files */
186 remove_files (filep
, filevec
);
191 app_msgarg(&msgs
, "cur");
193 folder
= getfolder (1);
194 strncpy (maildir
, m_maildir (folder
), sizeof(maildir
));
196 if (chdir (maildir
) == NOTOK
)
197 adios (maildir
, "unable to change directory to");
199 /* read source folder and create message structure */
200 if (!(mp
= folder_read (folder
, 1)))
201 adios (NULL
, "unable to read folder %s", folder
);
203 /* check for empty folder */
205 adios (NULL
, "no messages in %s", folder
);
207 /* parse the message range/sequence/name and set SELECTED */
208 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
209 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
211 seq_setprev (mp
); /* set the previous-sequence */
213 /* create folder structures for each destination folder */
214 opnfolds (folders
, foldp
);
216 /* Link all the selected messages into destination folders.
218 * This causes the add hook to be run for messages that are
219 * linked into another folder. The refile hook is run for
220 * messages that are moved to another folder.
222 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
223 if (is_selected (mp
, msgnum
)) {
224 cp
= getcpy (m_name (msgnum
));
225 if (m_file (mp
, cp
, retainseqs
? msgnum
: 0, folders
, foldp
,
233 * Update this now, since folder_delmsgs() will save the context
234 * but doesn't have access to the folder name.
237 context_replace (pfolder
, folder
); /* update current folder */
240 * Adjust "cur" if necessary
243 if (mp
->hghsel
!= mp
->curmsg
244 && (mp
->numsel
!= mp
->nummsg
|| linkf
))
245 seq_setcur (mp
, mp
->hghsel
);
247 /* If -nolink, then "remove" messages from source folder.
249 * Note that folder_delmsgs does not call the delete hook
250 * because the message has already been handled above.
253 folder_delmsgs (mp
, unlink_msgs
, 1);
255 seq_save (mp
); /* synchronize message sequences */
256 context_save (); /* save the context file */
259 clsfolds (folders
, foldp
);
261 folder_free (mp
); /* free folder structure */
268 * Read all the destination folders and
269 * create folder structures for all of them.
273 opnfolds (struct st_fold
*folders
, int nfolders
)
275 char nmaildir
[BUFSIZ
];
276 register struct st_fold
*fp
, *ep
;
277 register struct msgs
*mp
;
279 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
280 chdir (m_maildir (""));
281 strncpy (nmaildir
, m_maildir (fp
->f_name
), sizeof(nmaildir
));
283 create_folder (nmaildir
, 0, done
);
285 if (chdir (nmaildir
) == NOTOK
)
286 adios (nmaildir
, "unable to change directory to");
287 if (!(mp
= folder_read (fp
->f_name
, 1)))
288 adios (NULL
, "unable to read folder %s", fp
->f_name
);
299 * Set the Previous-Sequence and then sychronize the
300 * sequence file, for each destination folder.
304 clsfolds (struct st_fold
*folders
, int nfolders
)
306 register struct st_fold
*fp
, *ep
;
307 register struct msgs
*mp
;
309 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
318 * If you have a "rmmproc" defined, we called that
319 * to remove all the specified files. If "rmmproc"
320 * is not defined, then just unlink the files.
324 remove_files (int filep
, char **files
)
327 char **vec
, *program
;
329 /* If rmmproc is defined, we use that */
331 vec
= argsplit(rmmproc
, &program
, &vecp
);
332 files
++; /* Yes, we need to do this */
333 for (i
= 0; i
< filep
; i
++)
334 vec
[vecp
++] = files
[i
];
335 vec
[vecp
] = NULL
; /* NULL terminate list */
338 execvp (program
, vec
);
339 adios (rmmproc
, "unable to exec");
342 /* Else just unlink the files */
343 files
++; /* advance past filevec[0] */
344 for (i
= 0; i
< filep
; i
++) {
345 if (unlink (files
[i
]) == NOTOK
)
346 admonish (files
[i
], "unable to unlink");
352 * Link (or copy) the message into each of the destination folders.
353 * If oldmsgnum is not 0, call copy_seqs().
357 m_file (struct msgs
*mp
, char *msgfile
, int oldmsgnum
,
358 struct st_fold
*folders
, int nfolders
, int preserve
, int refile
)
361 struct st_fold
*fp
, *ep
;
363 for (fp
= folders
, ep
= folders
+ nfolders
; fp
< ep
; fp
++) {
364 if ((msgnum
= folder_addmsg (&fp
->f_mp
, msgfile
, 1, 0, preserve
, nfolders
== 1 && refile
, maildir
)) == -1)
366 if (oldmsgnum
) copy_seqs (mp
, oldmsgnum
, fp
->f_mp
, msgnum
);
373 * Copy sequence information for a refiled message to its
374 * new folder. Skip the cur sequence.
377 copy_seqs (struct msgs
*oldmp
, int oldmsgnum
, struct msgs
*newmp
, int newmsgnum
)
382 for (seq
= svector_strs (oldmp
->msgattrs
), seqnum
= 0;
383 *seq
&& seqnum
< svector_size (oldmp
->msgattrs
);
385 if (strcmp (current
, *seq
)) {
386 assert ((int) seqnum
== seq_getnum (oldmp
, *seq
));
387 if (in_sequence (oldmp
, seqnum
, oldmsgnum
)) {
388 seq_addmsg (newmp
, *seq
, newmsgnum
,
389 is_seq_private (oldmp
, seqnum
) ? 0 : 1, 0);