Commit aa082652 aa0826526df4dc51cafc2297b7b8306831c38f98 by Sergey Poznyakoff

New utility: mu

The `mu' program is a multi-purpose tool for doing various mail-
and mailutils-related operations.  It includes a pop3 shell,
a coder/decoder for all filter formats supported by Mailutils,
a utility to extract arbitrary values from the MU configuration
files, a configuration information utility and many more, in the
short run.  It supercedes examples/pop3client and (partially)
mailutils-config, both of which will be removed in the future.

* Makefile.am (SUBDIRS): Add mu.
* configure.ac: Likewise.
* po/POTFILES.in: Add new files.
* mu/.gitignore: New file.
* mu/Makefile.am: New file.
* mu/filter.c: New file.
* mu/flt2047.c: New file.
* mu/info.c: New file.
* mu/mu.c: New file.
* mu/mu.h: New file.
* mu/pop.c: New file.
* mu/query.c: New file.
* mu/shell.c: New file.
1 parent af4665d4
......@@ -109,6 +109,7 @@ SUBDIRS = . \
doc\
config\
examples\
mu\
$(FRM_DIR)\
$(POP3D_DIR)\
$(IMAP4D_DIR)\
......
......@@ -1389,6 +1389,7 @@ AC_CONFIG_FILES([
mu-aux/Makefile
mu-aux/mailutils.spec
sieve/Makefile
mu/Makefile
])
AC_OUTPUT
......
## This file is part of GNU Mailutils.
## Copyright (C) 1999, 2000, 2001, 2002, 2005, 2007, 2010 Free
## Software Foundation, Inc.
##
## GNU Mailutils is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2, or (at
## your option) any later version.
##
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
bin_PROGRAMS = mu
mu_SOURCES = \
info.c\
mu.h\
mu.c\
filter.c\
flt2047.c\
pop.c\
query.c\
shell.c
mu_LDADD = \
${MU_APP_LIBRARIES}\
${MU_LIB_MBOX}\
${MU_LIB_IMAP}\
${MU_LIB_POP}\
${MU_LIB_NNTP}\
${MU_LIB_MH}\
${MU_LIB_MAILDIR}\
${MU_LIB_MAILER}\
${MU_LIB_AUTH}\
@MU_AUTHLIBS@\
${MU_LIB_MAILUTILS}\
@READLINE_LIBS@ @MU_COMMON_LIBRARIES@
INCLUDES = @MU_APP_COMMON_INCLUDES@ @MU_AUTHINCS@
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <mailutils/mailutils.h>
#include "argp.h"
#include "mu.h"
static char filter_doc[] = N_("mu filter");
static char filter_args_doc[] = N_("[OPTIONS] NAME");
static struct argp_option filter_options[] = {
{ "encode", 'e', NULL, 0, N_("encode the input (default)") },
{ "decode", 'd', NULL, 0, N_("decode the input") },
{ "line-length", 'l', NULL, 0, N_("limit output line length") },
{ "newline", 'n', NULL, 0, N_("print additional newline") },
{ NULL }
};
static int filter_mode = MU_FILTER_ENCODE;
static int newline_option = 0;
static size_t line_length;
static int line_length_option = 0;
static error_t
filter_parse_opt (int key, char *arg, struct argp_state *state)
{
char *p;
switch (key)
{
case 'e':
filter_mode = MU_FILTER_ENCODE;
break;
case 'd':
filter_mode = MU_FILTER_DECODE;
break;
case 'n':
newline_option = 1;
break;
case 'l':
line_length = strtoul (arg, &p, 10);
if (*p)
argp_error (state, N_("not a number"));
line_length_option = 1;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp filter_argp = {
filter_options,
filter_parse_opt,
filter_args_doc,
filter_doc,
NULL,
NULL,
NULL
};
/* FIXME: This is definitely a kludge. The API should provide a function
for that. */
static void
reset_line_length (const char *name, size_t length)
{
mu_list_t list;
int status;
mu_filter_record_t frec;
mu_filter_get_list (&list);
status = mu_list_locate (list, (void*)name, (void**)&frec);
if (status == 0)
frec->max_line_length = length;
/* don't bail out, leave that to mu_filter_create */
}
int
mutool_filter (int argc, char **argv)
{
int rc, index;
mu_stream_t in, out, flt;
const char *fltname;
if (argp_parse (&filter_argp, argc, argv, ARGP_IN_ORDER, &index, NULL))
return 1;
argc -= index;
argv += index;
if (argc != 1)
{
mu_error (_("what filter do you want?"));
return 1;
}
fltname = argv[0];
rc = mu_stdio_stream_create (&in, MU_STDIN_FD, 0);
if (rc)
{
mu_error (_("cannot open input stream: %s"), mu_strerror (rc));
return 1;
}
rc = mu_stdio_stream_create (&out, MU_STDOUT_FD, 0);
if (rc)
{
mu_error (_("cannot open output stream: %s"), mu_strerror (rc));
return 1;
}
if (line_length_option)
reset_line_length (fltname, line_length);
rc = mu_filter_create (&flt, in, fltname, filter_mode, MU_STREAM_READ);
if (rc)
{
mu_error (_("cannot open filter stream: %s"), mu_strerror (rc));
return 1;
}
rc = mu_stream_copy (out, flt, 0, NULL);
if (rc)
{
mu_error ("%s", mu_strerror (rc));
return 1;
}
if (newline_option)
mu_stream_write (out, "\n", 1, NULL);
mu_stream_flush (out);
return 0;
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <mailutils/mailutils.h>
#include "argp.h"
#include "mu.h"
static char flt2047_doc[] = N_("mu 2047 - decode/encode message headers");
static char flt2047_args_doc[] = N_("[OPTIONS] [text]");
static struct argp_option flt2047_options[] = {
{ "encode", 'e', NULL, 0, N_("encode the input (default)") },
{ "decode", 'd', NULL, 0, N_("decode the input") },
{ "newline", 'n', NULL, 0, N_("print additional newline") },
{ "charset", 'c', NULL, 0, N_("set charset (default: iso-8859-1)") },
{ "encoding", 'E', NULL, 0, N_("set encoding (default: quoted-printable)") },
{ NULL }
};
static int decode_mode = 0;
static int newline_option = 0;
static const char *charset = "iso-8859-1";
static const char *encoding = "quoted-printable";
static error_t
flt2047_parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case 'c':
charset = arg;
break;
case 'e':
decode_mode = 0;
break;
case 'E':
encoding = arg;
break;
case 'd':
decode_mode = 1;
break;
case 'n':
newline_option = 0;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp flt2047_argp = {
flt2047_options,
flt2047_parse_opt,
flt2047_args_doc,
flt2047_doc,
NULL,
NULL,
NULL
};
int
mutool_flt2047 (int argc, char **argv)
{
int rc, index;
mu_stream_t in, out;
char *p;
if (argp_parse (&flt2047_argp, argc, argv, ARGP_IN_ORDER, &index, NULL))
return 1;
argc -= index;
argv += index;
rc = mu_stdio_stream_create (&in, MU_STDIN_FD, 0);
if (rc)
{
mu_error (_("cannot open input stream: %s"), mu_strerror (rc));
return 1;
}
rc = mu_stdio_stream_create (&out, MU_STDOUT_FD, 0);
if (rc)
{
mu_error (_("cannot open output stream: %s"), mu_strerror (rc));
return 1;
}
if (argc)
{
char *p;
while (argc--)
{
const char *text = *argv++;
if (decode_mode)
rc = mu_rfc2047_decode (charset, text, &p);
else
rc = mu_rfc2047_encode (charset, encoding, text, &p);
if (rc)
{
mu_error ("%s", mu_strerror (rc));
return 1;
}
mu_stream_printf (out, "%s\n", p);
}
}
else
{
size_t size = 0, n;
char *buf = NULL;
while ((rc = mu_stream_getline (in, &buf, &size, &n)) == 0 && n > 0)
{
mu_rtrim_class (buf, MU_CTYPE_SPACE);
if (decode_mode)
rc = mu_rfc2047_decode (charset, buf, &p);
else
rc = mu_rfc2047_encode (charset, encoding, buf, &p);
if (rc)
{
mu_error ("%s", mu_strerror (rc));
return 1;
}
mu_stream_printf (out, "%s\n", p);
}
}
mu_stream_flush (out);
return 0;
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <mailutils/mailutils.h>
#include "argp.h"
#include "mu.h"
static char info_doc[] = N_("mu info - print a list of configuration\
options used to build mailutils; optional arguments are interpreted\
as a list of configuration options to check for.");
static char info_args_doc[] = N_("[OPTIONS] [capa...]");
static struct argp_option info_options[] = {
{ "verbose", 'v', NULL, 0,
N_("increase output verbosity"), 0},
{ NULL }
};
static int verbose;
static error_t
info_parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case 'v':
verbose++;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp info_argp = {
info_options,
info_parse_opt,
info_args_doc,
info_doc,
NULL,
NULL,
NULL
};
int
mutool_info (int argc, char **argv)
{
int index;
if (argp_parse (&info_argp, argc, argv, ARGP_IN_ORDER, &index, NULL))
return 1;
argc -= index;
argv += index;
if (argc == 0)
mu_fprint_options (stdout, verbose);
else
{
int i, found = 0;
for (i = 0; i < argc; i++)
{
const struct mu_conf_option *opt = mu_check_option (argv[i]);
if (opt)
{
found++;
mu_fprint_conf_option (stdout, opt, verbose);
}
}
return found == argc ? 0 : 1;
}
return 0;
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <mailutils/mailutils.h>
#include <mailutils/tls.h>
#include "mailutils/libargp.h"
#include "mu.h"
static char args_doc[] = N_("[OPTIONS] COMMAND [CMDOPTS]");
static char doc[] = N_("mu -- GNU Mailutils test tool.\n\
Commands are:\n\
mu info - show Mailutils configuration\n\
mu query - query configuration values\n\
mu pop - POP3 client program\n\
mu filter - filter program\n\
mu 2047 - decode/encode message headers as per RFC 2047\n\
\n\
Try `mu COMMAND --help' to get help on a particular COMMAND\n\
\n\
Options are:\n");
/* FIXME: add
mu imap
mu send [opts]
*/
static struct argp_option options[] = {
{ NULL }
};
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = {
options,
parse_opt,
args_doc,
doc,
NULL,
NULL,
NULL
};
static const char *mu_tool_capa[] = {
"common",
"debug",
"license",
"locking",
"mailbox",
"auth",
NULL
};
struct mu_cfg_param mu_tool_param[] = {
{ NULL }
};
struct mutool_action_tab
{
const char *name;
mutool_action_t action;
};
struct mutool_action_tab mutool_action_tab[] = {
{ "info", mutool_info },
{ "pop", mutool_pop },
{ "filter", mutool_filter },
{ "2047", mutool_flt2047 },
{ "query", mutool_query },
{ NULL }
};
static mutool_action_t
find_action (const char *name)
{
struct mutool_action_tab *p;
for (p = mutool_action_tab; p->name; p++)
if (strcmp (p->name, name) == 0)
return p->action;
return NULL;
}
int
main (int argc, char **argv)
{
int index;
mutool_action_t action;
/* Native Language Support */
MU_APP_INIT_NLS ();
MU_AUTH_REGISTER_ALL_MODULES ();
/* Register the desired mailbox formats. */
mu_register_all_mbox_formats ();
#ifdef WITH_TLS
mu_gocs_register ("tls", mu_tls_module_init);
#endif
mu_argp_init (NULL, NULL);
if (mu_app_init (&argp, mu_tool_capa, mu_tool_param,
argc, argv, ARGP_IN_ORDER, &index, NULL))
exit (1);
argc -= index;
argv += index;
if (argc < 1)
{
mu_error (_("what do you want me to do?"));
exit (1);
}
action = find_action (argv[0]);
if (!action)
{
mu_error (_("don't know what %s is"), argv[0]);
exit (1);
}
exit (action (argc, argv));
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
typedef int (*mutool_action_t) (int argc, char **argv);
struct mutool_command
{
const char *name; /* User printable name of the function. */
int argmin; /* Min. acceptable number of arguments (-1 means
pass all arguments as a single string */
int argmax; /* Max. allowed number of arguments (-1 means not
limited */
mutool_action_t func; /* Function to call to do the job. */
const char *doc; /* Documentation for this function. */
};
int mutool_pop (int argc, char **argv);
int mutool_filter (int argc, char **argv);
int mutool_flt2047 (int argc, char **argv);
int mutool_info (int argc, char **argv);
int mutool_query (int argc, char **argv);
extern char *mutool_shell_prompt;
extern mu_vartab_t mutool_prompt_vartab;
int mutool_shell (const char *name, struct mutool_command *cmd);
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <netdb.h>
#include <mailutils/mailutils.h>
#include "mu.h"
#include "argp.h"
#include "xalloc.h"
static char pop_doc[] = N_("mu pop - POP3 client shell");
static char pop_args_doc[] = "";
static struct argp_option pop_options[] = {
{ NULL }
};
static error_t
pop_parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp pop_argp = {
pop_options,
pop_parse_opt,
pop_args_doc,
pop_doc,
NULL,
NULL,
NULL
};
int
get_bool (const char *str, int *pb)
{
if (mu_c_strcasecmp (str, "yes") == 0
|| mu_c_strcasecmp (str, "on") == 0
|| mu_c_strcasecmp (str, "true") == 0)
*pb = 1;
else if (mu_c_strcasecmp (str, "no") == 0
|| mu_c_strcasecmp (str, "off") == 0
|| mu_c_strcasecmp (str, "false") == 0)
*pb = 0;
else
return 1;
return 0;
}
int
get_port (const char *port_str, int *pn)
{
short port_num;
long num;
char *p;
num = port_num = strtol (port_str, &p, 0);
if (*p == 0)
{
if (num != port_num)
{
mu_error ("bad port number: %s", port_str);
return 1;
}
}
else
{
struct servent *sp = getservbyname (port_str, "tcp");
if (!sp)
{
mu_error ("unknown port name");
return 1;
}
port_num = ntohs (sp->s_port);
}
*pn = port_num;
return 0;
}
/* Global handle for pop3. */
mu_pop3_t pop3;
/* Flag if verbosity is needed. */
int verbose;
#define VERBOSE_MASK(n) (1<<((n)+1))
#define SET_VERBOSE_MASK(n) (verbose |= VERBOSE_MASK (n))
#define CLR_VERBOSE_MASK(n) (verbose &= VERBOSE_MASK (n))
#define QRY_VERBOSE_MASK(n) (verbose & VERBOSE_MASK (n))
#define HAS_VERBOSE_MASK(n) (verbose & ~1)
#define SET_VERBOSE() (verbose |= 1)
#define CLR_VERBOSE() (verbose &= ~1)
#define QRY_VERBOSE() (verbose & 1)
enum pop_session_status
{
pop_session_disconnected,
pop_session_connected,
pop_session_logged_in
};
enum pop_session_status pop_session_status;
int connect_argc;
char **connect_argv;
/* Host we are connected to. */
#define host connect_argv[0]
int port = 110;
char *username;
const char *
pop_session_str (enum pop_session_status stat)
{
switch (stat)
{
case pop_session_disconnected:
return "disconnected";
case pop_session_connected:
return "connected";
case pop_session_logged_in:
return "logged in";
}
return "unknown";
}
static void
pop_prompt_vartab ()
{
mu_vartab_t vtab;
if (mu_vartab_create (&vtab))
return;
mu_vartab_define (vtab, "user",
(pop_session_status == pop_session_logged_in) ?
username : "not logged in", 1);
mu_vartab_define (vtab, "host",
(pop_session_status != pop_session_disconnected) ?
host : "not connected", 1);
mu_vartab_define (vtab, "program-name", mu_program_name, 1);
mu_vartab_define (vtab, "canonical-program-name", "mu", 1);
mu_vartab_define (vtab, "package", PACKAGE, 1);
mu_vartab_define (vtab, "version", PACKAGE_VERSION, 1);
mu_vartab_define (vtab, "status", pop_session_str (pop_session_status), 1);
mu_vartab_destroy (&mutool_prompt_vartab);
mutool_prompt_vartab = vtab;
}
static int
string_to_xlev (const char *name, int *pv)
{
if (strcmp (name, "secure") == 0)
*pv = MU_XSCRIPT_SECURE;
else if (strcmp (name, "payload") == 0)
*pv = MU_XSCRIPT_PAYLOAD;
else
return 1;
return 0;
}
static int
change_verbose_mask (int set, int argc, char **argv)
{
int i;
for (i = 0; i < argc; i++)
{
int lev;
if (string_to_xlev (argv[i], &lev))
{
mu_error ("unknown level: %s", argv[i]);
return 1;
}
if (set)
SET_VERBOSE_MASK (lev);
else
CLR_VERBOSE_MASK (lev);
}
return 0;
}
void
set_verbose (mu_pop3_t p)
{
if (p)
{
if (QRY_VERBOSE ())
{
mu_pop3_trace (p, MU_POP3_TRACE_SET);
}
else
mu_pop3_trace (p, MU_POP3_TRACE_CLR);
}
}
void
set_verbose_mask (mu_pop3_t p)
{
if (p)
{
mu_pop3_trace_mask (p, QRY_VERBOSE_MASK (MU_XSCRIPT_SECURE)
? MU_POP3_TRACE_SET : MU_POP3_TRACE_CLR,
MU_XSCRIPT_SECURE);
mu_pop3_trace_mask (p, QRY_VERBOSE_MASK (MU_XSCRIPT_PAYLOAD)
? MU_POP3_TRACE_SET : MU_POP3_TRACE_CLR,
MU_XSCRIPT_PAYLOAD);
}
}
static int
com_verbose (int argc, char **argv)
{
if (argc == 1)
{
if (QRY_VERBOSE ())
{
printf ("verbose is on");
if (HAS_VERBOSE_MASK ())
{
char *delim = " (";
if (QRY_VERBOSE_MASK (MU_XSCRIPT_SECURE))
{
printf("%ssecure", delim);
delim = ", ";
}
if (QRY_VERBOSE_MASK (MU_XSCRIPT_PAYLOAD))
printf("%spayload", delim);
printf (")");
}
printf ("\n");
}
else
printf ("verbose is off\n");
}
else
{
int bv;
if (get_bool (argv[1], &bv) == 0)
{
verbose |= bv;
if (argc > 2)
change_verbose_mask (verbose, argc - 2, argv + 2);
set_verbose (pop3);
}
else if (strcmp (argv[1], "mask") == 0)
change_verbose_mask (1, argc - 2, argv + 2);
else if (strcmp (argv[1], "unmask") == 0)
change_verbose_mask (0, argc - 2, argv + 2);
else
mu_error ("unknown subcommand");
set_verbose_mask (pop3);
}
return 0;
}
static int
com_user (int argc, char **argv)
{
int status;
status = mu_pop3_user (pop3, argv[1]);
if (status == 0)
{
username = strdup (argv[1]);
pop_prompt_vartab ();
}
return status;
}
static int
com_apop (int argc, char **argv)
{
int status;
status = mu_pop3_apop (pop3, argv[1], argv[2]);
if (status == 0)
{
username = strdup (argv[1]);
pop_session_status = pop_session_logged_in;
}
return status;
}
static int
com_capa (int argc, char **argv)
{
mu_iterator_t iterator = NULL;
int status = 0;
int reread = 0;
int i = 1;
for (i = 1; i < argc; i++)
{
if (strcmp (argv[i], "-reread") == 0)
reread = 1;
else
break;
}
if (i < argc)
{
if (reread)
{
status = mu_pop3_capa (pop3, 1, NULL);
if (status)
return status;
}
for (; i < argc; i++)
{
const char *elt;
int rc = mu_pop3_capa_test (pop3, argv[i], &elt);
switch (rc)
{
case 0:
if (*elt)
printf ("%s: %s\n", argv[i], elt);
else
printf ("%s is set\n", argv[i]);
break;
case MU_ERR_NOENT:
printf ("%s is not set\n", argv[i]);
break;
default:
return rc;
}
}
}
else
{
status = mu_pop3_capa (pop3, reread, &iterator);
if (status == 0)
{
for (mu_iterator_first (iterator);
!mu_iterator_is_done (iterator); mu_iterator_next (iterator))
{
char *capa = NULL;
mu_iterator_current (iterator, (void **) &capa);
printf ("CAPA: %s\n", capa ? capa : "");
}
mu_iterator_destroy (&iterator);
}
}
return status;
}
static int
com_uidl (int argc, char **argv)
{
int status = 0;
if (argc == 1)
{
mu_iterator_t uidl_iterator = NULL;
status = mu_pop3_uidl_all (pop3, &uidl_iterator);
if (status == 0)
{
for (mu_iterator_first (uidl_iterator);
!mu_iterator_is_done (uidl_iterator);
mu_iterator_next (uidl_iterator))
{
char *uidl = NULL;
mu_iterator_current (uidl_iterator, (void **) &uidl);
printf ("UIDL: %s\n", uidl ? uidl : "");
}
mu_iterator_destroy (&uidl_iterator);
}
}
else
{
char *uidl = NULL;
unsigned int msgno = strtoul (argv[1], NULL, 10);
status = mu_pop3_uidl (pop3, msgno, &uidl);
if (status == 0)
printf ("Msg: %d UIDL: %s\n", msgno, uidl ? uidl : "");
free (uidl);
}
return status;
}
static int
com_list (int argc, char **argv)
{
int status = 0;
if (argc == 1)
{
mu_iterator_t list_iterator;
status = mu_pop3_list_all (pop3, &list_iterator);
if (status == 0)
{
for (mu_iterator_first (list_iterator);
!mu_iterator_is_done (list_iterator);
mu_iterator_next (list_iterator))
{
char *list = NULL;
mu_iterator_current (list_iterator, (void **) &list);
printf ("LIST: %s\n", (list) ? list : "");
}
mu_iterator_destroy (&list_iterator);
}
}
else
{
size_t size = 0;
unsigned int msgno = strtoul (argv[1], NULL, 10);
status = mu_pop3_list (pop3, msgno, &size);
if (status == 0)
printf ("Msg: %u Size: %lu\n", msgno, (unsigned long) size);
}
return status;
}
static int
com_noop (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
return mu_pop3_noop (pop3);
}
static void
echo_off (struct termios *stored_settings)
{
struct termios new_settings;
tcgetattr (0, stored_settings);
new_settings = *stored_settings;
new_settings.c_lflag &= (~ECHO);
tcsetattr (0, TCSANOW, &new_settings);
}
static void
echo_on (struct termios *stored_settings)
{
tcsetattr (0, TCSANOW, stored_settings);
}
static int
com_pass (int argc, char **argv)
{
int status;
char pass[256];
char *pwd;
if (argc == 1)
{
struct termios stored_settings;
printf ("passwd:");
fflush (stdout);
echo_off (&stored_settings);
fgets (pass, sizeof pass, stdin);
echo_on (&stored_settings);
putchar ('\n');
fflush (stdout);
pass[strlen (pass) - 1] = '\0'; /* nuke the trailing line. */
pwd = pass;
}
else
pwd = argv[1];
status = mu_pop3_pass (pop3, pwd);
if (status == 0)
{
pop_session_status = pop_session_logged_in;
pop_prompt_vartab ();
}
return status;
}
static int
com_stat (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
size_t count = 0;
mu_off_t size = 0;
int status = 0;
status = mu_pop3_stat (pop3, &count, &size);
printf ("Mesgs: %lu Size %lu\n",
(unsigned long) count, (unsigned long) size);
return status;
}
static int
com_stls (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
return mu_pop3_stls (pop3);
}
static int
com_dele (int argc, char **argv)
{
unsigned msgno;
msgno = strtoul (argv[1], NULL, 10);
return mu_pop3_dele (pop3, msgno);
}
static int
com_rset (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
return mu_pop3_rset (pop3);
}
static int
com_top (int argc, char **argv)
{
mu_stream_t stream;
unsigned int msgno;
unsigned int lines;
int status;
msgno = strtoul (argv[1], NULL, 10);
if (argc == 3)
lines = strtoul (argv[2], NULL, 10);
else
lines = 5;
status = mu_pop3_top (pop3, msgno, lines, &stream);
if (status == 0)
{
size_t n = 0;
char buf[128];
while ((mu_stream_readline (stream, buf, sizeof buf, &n) == 0) && n)
printf ("%s", buf);
mu_stream_destroy (&stream);
}
return status;
}
static int
com_retr (int argc, char **argv)
{
mu_stream_t stream;
unsigned int msgno;
int status;
msgno = strtoul (argv[1], NULL, 10);
status = mu_pop3_retr (pop3, msgno, &stream);
if (status == 0)
{
size_t n = 0;
char buf[128];
while ((mu_stream_readline (stream, buf, sizeof buf, &n) == 0) && n)
printf ("%s", buf);
mu_stream_destroy (&stream);
}
return status;
}
static int
com_disconnect (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
if (pop3)
{
mu_pop3_disconnect (pop3);
mu_pop3_destroy (&pop3);
pop3 = NULL;
mu_argcv_free (connect_argc, connect_argv);
connect_argc = 0;
connect_argv = NULL;
pop_session_status = pop_session_disconnected;
pop_prompt_vartab ();
}
return 0;
}
static int
com_connect (int argc, char **argv)
{
int status;
int n = 0;
int tls = 0;
int i = 1;
for (i = 1; i < argc; i++)
{
if (strcmp (argv[i], "-tls") == 0)
{
if (WITH_TLS)
tls = 1;
else
{
mu_error ("TLS not supported");
return 0;
}
}
else
break;
}
argc -= i;
argv += i;
if (argc >= 2)
{
if (get_port (argv[1], &n))
return 0;
}
else if (tls)
n = MU_POP3_DEFAULT_SSL_PORT;
else
n = MU_POP3_DEFAULT_PORT;
if (pop_session_status != pop_session_disconnected)
com_disconnect (0, NULL);
status = mu_pop3_create (&pop3);
if (status == 0)
{
mu_stream_t tcp;
if (QRY_VERBOSE ())
{
set_verbose (pop3);
set_verbose_mask (pop3);
}
status = mu_tcp_stream_create (&tcp, argv[0], n, MU_STREAM_READ);
if (status == 0)
{
#ifdef WITH_TLS
if (tls)
{
mu_stream_t tlsstream;
status = mu_tls_client_stream_create (&tlsstream, tcp, tcp, 0);
mu_stream_unref (tcp);
if (status)
{
mu_error ("cannot create TLS stream: %s",
mu_strerror (status));
return 0;
}
tcp = tlsstream;
}
#endif
mu_pop3_set_carrier (pop3, tcp);
status = mu_pop3_connect (pop3);
}
else
{
mu_pop3_destroy (&pop3);
pop3 = NULL;
}
}
if (status)
mu_error ("Failed to create pop3: %s", mu_strerror (status));
else
{
connect_argc = argc;
connect_argv = xcalloc (argc, sizeof (*connect_argv));
for (i = 0; i < argc; i++)
connect_argv[i] = xstrdup (argv[i]);
connect_argv[i] = NULL;
port = n;
pop_session_status = pop_session_connected;
pop_prompt_vartab ();
}
return status;
}
static int
com_quit (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
int status = 0;
if (pop3)
{
if (mu_pop3_quit (pop3) == 0)
{
status = com_disconnect (0, NULL);
}
else
{
printf ("Try 'exit' to leave %s\n", mu_program_name);
}
}
else
printf ("Try 'exit' to leave %s\n", mu_program_name);
return status;
}
struct mutool_command pop_comtab[] = {
{ "apop", 3, 3, com_apop,
"Authenticate with APOP: APOP user secret" },
{ "capa", 1, -1, com_capa,
"List capabilities: capa [-reread] [names...]" },
{ "disconnect", 1, 1,
com_disconnect, "Close connection: disconnect" },
{ "dele", 2, 2, com_dele,
"Mark message: DELE msgno" },
{ "list", 1, 2, com_list,
"List messages: LIST [msgno]" },
{ "noop", 1, 1, com_noop,
"Send no operation: NOOP" },
{ "pass", 1, 2, com_pass,
"Send passwd: PASS [passwd]" },
{ "connect", 1, 4, com_connect,
"Open connection: connect [-tls] hostname port" },
{ "quit", 1, 1, com_quit,
"Go to Update state : QUIT" },
{ "retr", 2, 2, com_retr,
"Dowload message: RETR msgno" },
{ "rset", 1, 1, com_rset,
"Unmark all messages: RSET" },
{ "stat", 1, 1, com_stat,
"Get the size and count of mailbox : STAT" },
{ "stls", 1, 1, com_stls,
"Start TLS negotiation" },
{ "top", 2, 3, com_top,
"Get the header of message: TOP msgno [lines]" },
{ "uidl", 1, 2, com_uidl,
"Get the unique id of message: UIDL [msgno]" },
{ "user", 2, 2, com_user,
"send login: USER user" },
{ "verbose", 1, 4, com_verbose,
"Enable Protocol tracing: verbose [on|off|mask|unmask] [x1 [x2]]" },
{ NULL }
};
int
mutool_pop (int argc, char **argv)
{
int index;
if (argp_parse (&pop_argp, argc, argv, ARGP_IN_ORDER, &index, NULL))
return 1;
argc -= index;
argv += index;
if (argc)
{
mu_error (_("too many arguments"));
return 1;
}
/* Command line prompt */
mutool_shell_prompt = xstrdup ("pop> ");
pop_prompt_vartab ();
mutool_shell ("pop", pop_comtab);
return 0;
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <mailutils/mailutils.h>
#include <mailutils/libcfg.h>
#include "argp.h"
#include "mu.h"
static char query_doc[] = N_("mu query - query configuration values.");
static char query_args_doc[] = N_("[OPTIONS]");
char *file_name;
int verbose_option;
static struct argp_option query_options[] = {
{ "file", 'f', N_("FILE"), 0,
N_("query configuration values from FILE (default mailutils.rc)"),
0 },
{ "verbose", 'v', NULL, 0,
N_("increase output verbosity"), 0},
{ NULL }
};
static error_t
query_parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case 'f':
file_name = arg;
break;
case 'v':
verbose_option++;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp query_argp = {
query_options,
query_parse_opt,
query_args_doc,
query_doc,
NULL,
NULL,
NULL
};
int
mutool_query (int argc, char **argv)
{
int rc, index;
mu_cfg_tree_t *tree = NULL;
int fmtflags = 0;
mu_stream_t stream;
if (argp_parse (&query_argp, argc, argv, ARGP_IN_ORDER, &index, NULL))
return 1;
argc -= index;
argv += index;
if (argc == 0)
{
mu_error (_("query what?"));
return 1;
}
if (file_name)
{
mu_load_site_rcfile = 0;
mu_load_user_rcfile = 0;
mu_load_rcfile = file_name;
}
if (mu_libcfg_parse_config (&tree))
return 1;
if (!tree)
return 0;
rc = mu_stdio_stream_create (&stream, MU_STDOUT_FD, 0);
if (rc)
{
mu_error ("mu_stdio_stream_create: %s", mu_strerror (rc));
return 1;
}
if (verbose_option)
fmtflags = MU_CFG_FMT_LOCUS;
for ( ; argc > 0; argc--, argv++)
{
char *path = *argv;
mu_cfg_node_t *node;
if (mu_cfg_find_node (tree, path, &node) == 0)
mu_cfg_format_node (stream, node, fmtflags);
}
return 0;
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <mailutils/mailutils.h>
#include <mailutils/tls.h>
#include "mailutils/libargp.h"
#include "mu.h"
#include "xalloc.h"
#ifdef WITH_READLINE
# include <readline/readline.h>
# include <readline/history.h>
#endif
char *mutool_shell_prompt;
mu_vartab_t mutool_prompt_vartab;
static char *
expand_prompt ()
{
char *str;
if (!mutool_prompt_vartab
|| mu_vartab_expand (mutool_prompt_vartab, mutool_shell_prompt, &str))
str = strdup (mutool_shell_prompt);
return str;
}
static int shell_exit (int, char **);
static int shell_help (int, char **);
static int shell_prompt (int, char **);
#ifdef WITH_READLINE
static int shell_history (int, char **);
#endif
struct mutool_command default_comtab[] = {
{ "prompt", -1, -1, shell_prompt, "set command prompt" },
{ "exit", 1, 1, shell_exit, "exit program" },
{ "help", 1, 2, shell_help, "display this text" },
{ "?", 1, 1, shell_help, "synonym for `help'" },
#ifdef WITH_READLINE
{ "history", 1, 1, shell_history, "show command history" },
#endif
{ NULL }
};
/* Print a single comtab entry */
static void
print_comtab (struct mutool_command *tab)
{
printf ("%s\t\t%s\n", tab->name, tab->doc);
}
/* Print a single comtab entry.
FIXME: Way too primitive. Rewrite. */
int
print_help (struct mutool_command *tab, size_t n)
{
if (n)
{
for (; n > 0 && tab->name; tab++, n--)
print_comtab (tab);
}
else
{
for (; tab->name; tab++)
print_comtab (tab);
}
return 0;
}
/* Print all commands from TAB matching NAME.
FIXME: Way too primitive. Rewrite. */
void
list_commands (struct mutool_command *tab, const char *name)
{
size_t namelen = strlen (name);
int printed = 0;
for (; tab->name; tab++)
{
/* Print in six columns. */
if (printed == 6)
{
printed = 0;
printf ("\n");
}
if (mu_c_strncasecmp (tab->name, name, namelen) == 0)
{
printf ("%s\t", tab->name);
printed++;
}
}
if (printed && printed < 6)
printf ("\n");
}
static struct mutool_command *
simple_find_command (struct mutool_command *cp, const char *name)
{
for (; cp->name; cp++)
if (strcmp (cp->name, name) == 0)
return cp;
return NULL;
}
static struct mutool_command *shell_comtab;
static size_t user_command_count;
static struct mutool_command *
find_command (const char *name)
{
return simple_find_command (shell_comtab, name);
}
/* Print out help for ARG, or for all of the commands if ARG is
not present. */
int
shell_help (int argc, char **argv)
{
char *name = argv[1];
if (name)
{
struct mutool_command *com = find_command (argv[1]);
if (com)
print_comtab (com);
else
{
printf ("No commands match `%s'. Possibilties are:\n", name);
list_commands (shell_comtab, name);
}
}
else
{
print_help (shell_comtab, user_command_count);
printf ("\n");
print_help (shell_comtab + user_command_count, 0);
}
return 0;
}
static int
shell_prompt (int argc, char **argv)
{
int quote;
size_t size;
free (mutool_shell_prompt);
size = mu_argcv_quoted_length (argv[1], &quote);
mutool_shell_prompt = xmalloc (size + 1);
mu_argcv_unquote_copy (mutool_shell_prompt, argv[1], size);
return 0;
}
#ifdef WITH_READLINE
#define HISTFILE_PREFIX "~/.mu_"
#define HISTFILE_SUFFIX "_history"
static char *
get_history_file_name ()
{
static char *filename = NULL;
if (!filename)
{
char *hname;
hname = xmalloc(sizeof HISTFILE_PREFIX + strlen (rl_readline_name) +
sizeof HISTFILE_SUFFIX - 1);
strcpy (hname, "~/.mu_");
strcat (hname, rl_readline_name);
strcat (hname, HISTFILE_SUFFIX);
filename = mu_tilde_expansion (hname, "/", NULL);
free(hname);
}
return filename;
}
/* Interface to Readline Completion */
static char *shell_command_generator (const char *, int);
static char **shell_completion (char *, int, int);
/* Tell the GNU Readline library how to complete. We want to try to complete
on command names if this is the first word in the line, or on filenames
if not. */
void
mutool_initialize_readline (const char *name)
{
/* Allow conditional parsing of the ~/.inputrc file. */
rl_readline_name = (char *) name;
rl_attempted_completion_function = (CPPFunction *) shell_completion;
read_history (get_history_file_name ());
}
static void
finish_readline ()
{
write_history (get_history_file_name());
}
/* Attempt to complete on the contents of TEXT. START and END bound the
region of rl_line_buffer that contains the word to complete. TEXT is
the word to complete. We can use the entire contents of rl_line_buffer
in case we want to do some simple parsing. Return the array of matches,
or NULL if there aren't any. */
static char **
shell_completion (char *text, int start, int end MU_ARG_UNUSED)
{
char **matches;
matches = (char **) NULL;
/* If this word is at the start of the line, then it is a command
to complete. Otherwise it is the name of a file in the current
directory. */
if (start == 0)
matches = rl_completion_matches (text, shell_command_generator);
return (matches);
}
/* Generator function for command completion. STATE lets us know whether
to start from scratch; without any state (i.e. STATE == 0), then we
start at the top of the list. */
static char *
shell_command_generator (const char *text, int state)
{
const char *name;
static int len;
static struct mutool_command *cmd;
/* If this is a new word to complete, initialize now. This includes
saving the length of TEXT for efficiency, and initializing the index
variable to 0. */
if (!state)
{
cmd = shell_comtab;
len = strlen (text);
}
if (!cmd->name)
return NULL;
/* Return the next name which partially matches from the command list. */
while ((name = cmd->name))
{
cmd++;
if (strncmp (name, text, len) == 0)
return xstrdup (name);
}
/* If no names matched, then return NULL. */
return NULL;
}
static char *pre_input_line;
static int
pre_input (void)
{
rl_insert_text (pre_input_line);
free (pre_input_line);
rl_pre_input_hook = NULL;
rl_redisplay ();
return 0;
}
static int
retrieve_history (char *str)
{
char *out;
int rc;
rc = history_expand (str, &out);
switch (rc)
{
case -1:
mu_error ("%s", out);
free (out);
return 1;
case 0:
break;
case 1:
pre_input_line = out;
rl_pre_input_hook = pre_input;
return 1;
case 2:
printf ("%s\n", out);
free (out);
return 1;
}
return 0;
}
static int
shell_history (int argc, char **argv)
{
int i;
HIST_ENTRY **hlist;
hlist = history_list ();
for (i = 0; i < history_length; i++)
printf ("%4d) %s\n", i + 1, hlist[i]->line);
return 0;
}
#else
# define finish_readline()
# define mutool_initialize_readline (const char *name)
char *
readline (char *prompt)
{
static size_t size = 0;
static char *buf = NULL;
if (prompt)
{
printf ("%s", prompt);
fflush (stdout);
}
if (getline (&buf, &size, stdin) <= 0)
return NULL;
return buf;
}
void
add_history (const char *s MU_ARG_UNUSED)
{
}
#endif
/* Parse and execute a command line. */
int
execute_line (char *line)
{
int argc;
char **argv;
int status = 0;
if (mu_argcv_get (line, NULL, "#", &argc, &argv))
{
mu_error("cannot parse input line");
return 0;
}
if (argc >= 0)
{
struct mutool_command *cmd = find_command (argv[0]);
if (!cmd)
mu_error ("%s: no such command.", argv[0]);
else if (cmd->argmin > 0 && argc < cmd->argmin)
mu_error ("%s: too few arguments", argv[0]);
else if (cmd->argmax > 0 && argc > cmd->argmax)
mu_error ("%s: too many arguments", argv[0]);
else
{
if (cmd->argmin <= 0 && argc != 2)
{
char *word = mu_str_skip_class (line, MU_CTYPE_SPACE);
char *arg = mu_str_skip_class_comp (word, MU_CTYPE_SPACE);
if (*arg)
{
*arg++ = 0;
arg = mu_str_skip_class (arg, MU_CTYPE_SPACE);
}
mu_argcv_free (argc, argv);
argc = 2;
argv = xcalloc (argc + 1, sizeof (argv[0]));
argv[0] = xstrdup (word);
argv[1] = xstrdup (arg);
argv[2] = NULL;
}
status = cmd->func (argc, argv);
}
}
mu_argcv_free (argc, argv);
return status;
}
static char *
input_line_interactive ()
{
char *p = expand_prompt ();
char *line = readline (p);
free (p);
return line;
}
static char *
input_line_script ()
{
size_t size = 0;
char *buf = NULL;
if (getline (&buf, &size, stdin) <= 0)
return NULL;
return buf;
}
static int done;
static int
shell_exit (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
done = 1;
return 0;
}
int
mutool_shell (const char *name, struct mutool_command *cmd)
{
size_t n;
int interactive = isatty (0);
char *(*input_line) () = interactive ?
input_line_interactive : input_line_script;
for (n = 0; cmd[n].name; n++)
;
user_command_count = n;
shell_comtab = xcalloc (n + MU_ARRAY_SIZE (default_comtab),
sizeof (shell_comtab[0]));
memcpy (shell_comtab, cmd, n * sizeof (shell_comtab[0]));
memcpy (shell_comtab + n, default_comtab, sizeof (default_comtab));
mutool_initialize_readline (name);
while (!done)
{
char *s, *line = input_line ();
if (!line)
break;
/* Remove leading and trailing whitespace from the line.
Then, if there is anything left, add it to the history list
and execute it. */
s = mu_str_stripws (line);
if (*s)
{
int status;
#ifdef WITH_READLINE
if (interactive)
{
if (retrieve_history (s))
continue;
add_history (s);
}
#endif
status = execute_line (s);
if (status != 0)
mu_error ("Error: %s", mu_strerror (status));
}
free (line);
}
if (interactive)
finish_readline ();
return 0;
}
......@@ -197,5 +197,13 @@ sieve/sieve.c
sql/mysql.c
mu/filter.c
mu/flt2047.c
mu/info.c
mu/mu.c
mu/pop.c
mu/query.c
mu/shell.c
# EOF
......