NetBSD/usr.bin/mail/mime_detach.c
christos f309875081 From Anon Ymous:
1) Statification of modules.

2) Implement the 'detach' and 'Detach' commands for extracting mime
   parts from messages.

3) Teach mail to output "In-Reply-To" and "References" header fields
   when replying so others can thread us.

4) Implement threading, sorting, and tagging, supported by the
   following commands: 'flatten', 'reverse', 'sort', 'thread',
   'unthread', 'down', 'tset', 'up', 'expose', 'hide', 'tag',
   'untag', 'invtags', 'tagbelow', 'hidetags', 'showtags'.
   See the manpage for details (when available - soon).

5) Implement a 'deldups' command to delete duplicate messages based on
   their "Message-Id" field, e.g., in replies to a mailing list that
   are also CCed to a subscriber.  (This can also be accomplished with
   the threading and tagging commands.)

6) Implement 'ifdef' and 'ifndef' commands, and make the conditionals
   nestable (i.e., implement a conditional stack).  The if/else/endif
   commands existed before, but they were primitive and undocumented.
   The 'if' command currently recognizes the "receiving", "sending",
   and "headersonly" mode keywords.

7) Teach the message selecting routine to understand regular
   expressions if "regex-search" is defined.  Otherwise only case
   insensitive substring matches are done (as in the past).

8) Teach the message selection routine to understand boolean
   expressions.  Improved "colon-modifier" support.  See the manpage
   for details (when available - soon).

9) Extend paging to all commands (where relevant).

10) Add shell like piping and redirection of (standard) output (if
   "enable-piping" is defined).  Extend completion to these contexts.

11) The manpage should follow soon!!!!
2006-11-28 18:45:32 +00:00

280 lines
6.9 KiB
C

/* $NetBSD: mime_detach.c,v 1.1 2006/11/28 18:45:32 christos Exp $ */
/*-
* Copyright (c) 2006 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Anon Ymous.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef MIME_SUPPORT
#include <sys/cdefs.h>
#ifndef __lint__
__RCSID("$NetBSD: mime_detach.c,v 1.1 2006/11/28 18:45:32 christos Exp $");
#endif /* not __lint__ */
#include <assert.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include "def.h"
#include "extern.h"
#ifdef USE_EDITLINE
#include "complete.h"
#endif
#ifdef MIME_SUPPORT
#include "mime.h"
#include "mime_child.h"
#include "mime_codecs.h"
#include "mime_detach.h"
#endif
static struct {
int overwrite;
int batch;
int ask;
} detach_ctl;
PUBLIC int
mime_detach_control(void)
{
char *cp;
detach_ctl.batch = value(ENAME_MIME_DETACH_BATCH) != NULL;
detach_ctl.ask = detach_ctl.batch ? 0 : 1;
detach_ctl.overwrite = 0;
cp = value(ENAME_MIME_DETACH_OVERWRITE);
if (cp == NULL || strcasecmp(cp, "no") == 0)
detach_ctl.overwrite = 0;
else if (*cp== '\0' || strcasecmp(cp, "yes") == 0)
detach_ctl.overwrite = 1;
else if (strcasecmp(cp, "ask") == 0) {
detach_ctl.overwrite = 0;
detach_ctl.ask = 1;
}
else {
(void)printf("invalid %s setting: %s",
ENAME_MIME_DETACH_OVERWRITE, cp);
return -1;
}
return 0;
}
static char *
detach_get_fname(char *prompt, char *pathname)
{
if (!detach_ctl.batch) {
char *fname;
fname = my_gets(&elm.filec, prompt, pathname);
fname = skip_blank(fname); /* XXX - do this? */
if (*fname == '\0') /* ignore this attachment */
return NULL;
pathname = savestr(fname); /* save this or it gets trashed */
}
else if (detach_ctl.ask)
(void)printf("%s%s\n", prompt, pathname);
return pathname;
}
static enum {
DETACH_OPEN_OK,
DETACH_NEXT,
DETACH_RENAME,
DETACH_FAILED
}
detach_open_core(char *fname, const char *partstr)
{
int flags;
int fd;
flags = (detach_ctl.overwrite ? 0 : O_EXCL) | O_CREAT | O_TRUNC | O_WRONLY;
if ((fd = open(fname, flags, 0600)) != -1 &&
Fdopen(fd, "w") != NULL)
return DETACH_OPEN_OK;
if (detach_ctl.ask && fd == -1 && errno == EEXIST) {
char *p;
start:
(void)sasprintf(&p, "%-7s overwrite: Always/Never/once/next/rename (ANonr)[n]? ",
partstr, fname);
p = my_gets(&elm.string, p, NULL);
p = skip_blank(p);
switch (*p) {
case 'A': detach_ctl.overwrite = 1;
detach_ctl.batch = 1;
detach_ctl.ask = 0;
/* FALLTHROUGH */
case 'o':
if (Fopen(fname, "w") != NULL)
return DETACH_OPEN_OK;
break;
case 'N': detach_ctl.overwrite = 0;
detach_ctl.batch = 1;
detach_ctl.ask = 0;
/* FALLTHROUGH */
case '\0': /* default */
case 'n': /* Next */
return DETACH_NEXT;
default:
goto start;
case 'r': /* Rename */
return DETACH_RENAME;
}
}
warn(fname);
if (fd != -1)
(void)close(fd);
return DETACH_FAILED;
}
static char *
detach_open_target(struct mime_info *mip)
{
char *pathname;
char *prompt;
/*
* Get the suggested target pathname.
*/
if (mip->mi_filename != NULL)
(void)sasprintf(&pathname, "%s/%s", mip->mi_detachdir, mip->mi_filename);
else {
if (mip->mi_detachall == 0)
return NULL;
(void)sasprintf(&pathname, "%s/msg-%s.part-%s.%s",
mip->mi_detachdir, mip->mi_msgstr,
mip->mi_partstr[0] ? mip->mi_partstr : "0",
mip->mi_subtype ? mip->mi_subtype : "unknown");
}
/*
* Make up the prompt
*/
(void)sasprintf(&prompt, "%-7s filename: ", mip->mi_partstr);
/*
* The main loop.
*/
do {
struct stat sb;
char *fname;
if ((fname = detach_get_fname(prompt, pathname)) == NULL)
return NULL;
/*
* Make sure we don't have the name of something other
* than a normal file!
*/
if (stat(fname, &sb) == 0 && !S_ISREG(sb.st_mode)) {
(void)printf("not a regular file: %s", fname);
if (!detach_ctl.ask)
return NULL;
continue;
}
switch (detach_open_core(fname, mip->mi_partstr)) {
case DETACH_OPEN_OK:
return fname;
case DETACH_NEXT:
return NULL;
case DETACH_RENAME:
detach_ctl.batch = 0;
break;
case DETACH_FAILED:
break;
}
} while (!detach_ctl.batch);
return NULL;
}
/*
* The main entry point for detaching.
*/
PUBLIC FILE *
mime_detach_parts(struct mime_info *mip)
{
mime_codec_t dec;
char *pathname;
if (mip->mi_ignore_body || mip->mp->m_blines == 0)
return NULL;
if ((dec = mime_fio_decoder(mip->mi_encoding)) == NULL &&
(dec = mime_fio_decoder(MIME_TRANSFER_7BIT)) == NULL)
assert(/*CONSTCOND*/ 0); /* this should never get hit! */
if ((pathname = detach_open_target(mip)) == NULL)
return NULL;
(void)printf("writing: %s\n", pathname);
/*
* XXX - should we do character set conversion here (done by
* run_decoder()), or just run dec()?
*/
#if 0
mime_run_function(dec, pipe_end(mip), NULL);
#else
run_decoder(mip, dec);
#endif
return pipe_end(mip);
}
/*
* Set the message part number to be used when constructing a filename
* for detaching unnamed parts in detach_open_target(). When
* threading, this is not a single number, hence the string value.
*/
PUBLIC void
mime_detach_msgnum(struct mime_info *mip, const char *msgstr)
{
for (/*EMPTY*/; mip; mip = mip->mi_flink)
mip->mi_msgstr = msgstr;
}
#endif /* MIME_SUPPORT */