Commit 27ac3a54 27ac3a545e89bd4aa357d6a624363b4e9a80b859 by Sergey Poznyakoff

Implement SMTP shell in mu.

* include/mailutils/smtp.h (mu_smtp_get_reply_iterator)
(mu_smtp_cmd,mu_smtp_test_param): New protos.
* libproto/mailer/Makefile.am (libmu_mailer_la_SOURCES): Add
smtp_cmd.c
* libproto/mailer/smtp_cmd.c: New file.
* libproto/mailer/smtp_io.c (mu_smtp_get_reply_iterator): New function.
* libproto/mailer/smtp_param.c (mu_smtp_set_param): Accept NULL
parameter value.
(mu_smtp_test_param): New function.
* mu/Makefile.am [MU_COND_SUPPORT_SMTP]: Add smtp.c to MODULES.
(mu_SOURCES): Add getans.c, getyn.c and util.c
* mu/getans.c: New file.
* mu/getyn.c: New file.
* mu/smtp.c: New file.
* mu/util.c: New file.
* mu/mu.h (port_from_sa): New proto.
* mu/pop.c (port_from_sa): Move to util.c
1 parent e04498e5
......@@ -47,7 +47,9 @@ int mu_smtp_write (mu_smtp_t smtp, const char *fmt, ...) MU_PRINTFLIKE(2,3);
int mu_smtp_replcode (mu_smtp_t smtp, char *buf);
int mu_smtp_sget_reply (mu_smtp_t smtp, const char **pbuf);
int mu_smtp_get_reply_iterator (mu_smtp_t smtp, mu_iterator_t *pitr);
int mu_smtp_cmd (mu_smtp_t smtp, int argc, char **argv);
#define MU_SMTP_TRACE_CLR 0
#define MU_SMTP_TRACE_SET 1
......@@ -59,6 +61,7 @@ int mu_smtp_disconnect (mu_smtp_t smtp);
int mu_smtp_ehlo (mu_smtp_t smtp);
int mu_smtp_set_param (mu_smtp_t smtp, int code, const char *val);
int mu_smtp_get_param (mu_smtp_t smtp, int code, const char **param);
int mu_smtp_test_param (mu_smtp_t smtp, int pcode);
int mu_smtp_set_url (mu_smtp_t smtp, mu_url_t url);
int mu_smtp_get_url (mu_smtp_t smtp, mu_url_t *purl);
int mu_smtp_set_secret (mu_smtp_t smtp, mu_secret_t secret);
......
......@@ -34,6 +34,7 @@ libmu_mailer_la_SOURCES = \
smtp_capa.c\
smtp_capa_itr.c\
smtp_carrier.c\
smtp_cmd.c\
smtp_create.c\
smtp_data.c\
smtp_disconnect.c\
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010-2012 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This library 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <mailutils/errno.h>
#include <mailutils/cctype.h>
#include <mailutils/list.h>
#include <mailutils/util.h>
#include <mailutils/smtp.h>
#include <mailutils/stream.h>
#include <mailutils/sys/smtp.h>
/* Send an arbitrary command */
int
mu_smtp_cmd (mu_smtp_t smtp, int argc, char **argv)
{
int status, i;
if (!smtp)
return EINVAL;
if (MU_SMTP_FISSET (smtp, _MU_SMTP_ERR))
return MU_ERR_FAILURE;
status = mu_smtp_write (smtp, "%s", argv[0]);
MU_SMTP_CHECK_ERROR (smtp, status);
for (i = 1; i < argc; i++)
{
status = mu_smtp_write (smtp, " %s", argv[i]);
MU_SMTP_CHECK_ERROR (smtp, status);
}
status = mu_smtp_write (smtp, "\r\n");
MU_SMTP_CHECK_ERROR (smtp, status);
status = mu_smtp_response (smtp);
MU_SMTP_CHECK_ERROR (smtp, status);
if (smtp->replcode[0] > '3')
return MU_ERR_REPLY;
return 0;
}
......@@ -144,4 +144,10 @@ mu_smtp_sget_reply (mu_smtp_t smtp, const char **pbuf)
return 0;
}
int
mu_smtp_get_reply_iterator (mu_smtp_t smtp, mu_iterator_t *pitr)
{
if (!smtp || !pitr)
return EINVAL;
return mu_list_get_iterator (smtp->mlrepl, pitr);
}
......
......@@ -49,6 +49,8 @@ mu_smtp_set_param (mu_smtp_t smtp, int pcode, const char *newparam)
mu_secret_password_unref (smtp->secret);
mu_secret_destroy (&smtp->secret);
}
if (!newparam)
return 0;
MU_SMTP_FCLR (smtp, _MU_SMTP_CLNPASS);
return mu_secret_create (&smtp->secret, newparam, strlen (newparam));
}
......@@ -56,18 +58,28 @@ mu_smtp_set_param (mu_smtp_t smtp, int pcode, const char *newparam)
{
mu_url_t url;
int rc;
rc = mu_url_create (&url, newparam);
if (rc)
return rc;
mu_url_destroy (&smtp->url);
smtp->url = url;
if (!newparam)
mu_url_destroy (&smtp->url);
else
{
rc = mu_url_create (&url, newparam);
if (rc)
return rc;
mu_url_destroy (&smtp->url);
smtp->url = url;
}
return 0;
}
param = strdup (newparam);
if (!param)
return ENOMEM;
if (newparam)
{
param = strdup (newparam);
if (!param)
return ENOMEM;
}
else
param = NULL;
free (smtp->param[pcode]);
smtp->param[pcode] = param;
return 0;
......@@ -99,3 +111,24 @@ mu_smtp_get_param (mu_smtp_t smtp, int pcode, const char **pparam)
return 0;
}
int
mu_smtp_test_param (mu_smtp_t smtp, int pcode)
{
if (!smtp)
return EINVAL;
if (pcode < 0 || pcode >= MU_SMTP_MAX_PARAM)
return EINVAL;
if (pcode == MU_SMTP_PARAM_PASSWORD)
{
if (smtp->secret)
return 0;
return MU_ERR_NOENT;
}
else if (pcode == MU_SMTP_PARAM_URL)
{
if (smtp->url)
return 0;
return MU_ERR_NOENT;
}
return smtp->param[pcode] ? 0 : MU_ERR_NOENT;
}
......
......@@ -269,6 +269,9 @@ void mh_global_save_state (void);
int mh_interactive_mode_p (void);
int mh_getyn (const char *fmt, ...) MU_PRINTFLIKE(1,2);
int mh_getyn_interactive (const char *fmt, ...) MU_PRINTFLIKE(1,2);
int mu_vgetans (const char *variants, const char *fmt, va_list ap);
int mu_getans (const char *variants, const char *fmt, ...)
MU_PRINTFLIKE(2,3);
int mh_check_folder (const char *pathname, int confirm);
int mh_makedir (const char *p);
......
......@@ -39,6 +39,12 @@ else
IDLE_MODULES+=dbm.c
endif
if MU_COND_SUPPORT_SMTP
SMTP_C=smtp.c
else
IDLE_MODULES+=smtp.c
endif
MODULES = \
acl.c\
cflags.c\
......@@ -51,16 +57,20 @@ MODULES = \
ldflags.c\
logger.c\
$(POP_C)\
send.c\
query.c\
send.c\
$(SMTP_C)\
wicket.c
mu_SOURCES = \
dispatch.c\
getans.c\
getarg.c\
getyn.c\
mu.h\
mu.c\
shell.c\
util.c\
verbose.c\
$(MODULES)
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010, 2012 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/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <mailutils/mailutils.h>
#include "mu.h"
int
mu_vgetans (const char *variants, const char *fmt, va_list ap)
{
char repl[64];
while (1)
{
size_t n;
char *p;
mu_stream_vprintf (mu_strout, fmt, ap);
mu_stream_write (mu_strout, "? ", 2, NULL);
mu_stream_flush (mu_strout);
if (mu_stream_read (mu_strin, repl, sizeof repl, &n) || n == 0)
return 0;
mu_rtrim_class (repl, MU_CTYPE_ENDLN);
p = strchr (variants, *repl);
if (p)
return *p;
mu_stream_printf (mu_strout, _("Please answer one of [%s]: "), variants);
}
return 0; /* to pacify gcc */
}
int
mu_getans (const char *variants, const char *fmt, ...)
{
va_list ap;
int rc;
va_start (ap, fmt);
rc = mu_vgetans (variants, fmt, ap);
va_end (ap);
return rc;
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010, 2012 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/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <mailutils/mailutils.h>
#include "mu.h"
int
mu_vgetyn (const char *fmt, va_list ap)
{
char repl[64];
while (1)
{
int rc;
size_t n;
mu_stream_vprintf (mu_strout, fmt, ap);
mu_stream_write (mu_strout, "? ", 2, NULL);
mu_stream_flush (mu_strout);
if (mu_stream_read (mu_strin, repl, sizeof repl, &n) || n == 0)
return 0;
mu_rtrim_class (repl, MU_CTYPE_ENDLN);
rc = mu_true_answer_p (repl);
if (rc >= 0)
return rc;
/* TRANSLATORS: See msgids "nN" and "yY". */
mu_stream_printf (mu_strout, "%s", _("Please answer yes or no: "));
}
return 0; /* to pacify gcc */
}
int
mu_getyn (const char *fmt, ...)
{
va_list ap;
int rc;
va_start (ap, fmt);
rc = mu_vgetyn (fmt, ap);
va_end (ap);
return rc;
}
......@@ -42,6 +42,8 @@ int mu_help (void);
mutool_action_t dispatch_find_action (const char *name);
char *dispatch_docstring (const char *text);
int port_from_sa (struct mu_sockaddr *sa);
#define VERBOSE_MASK(n) (1<<((n)+1))
#define SET_VERBOSE_MASK(n) (shell_verbose_flags |= VERBOSE_MASK (n))
......
......@@ -473,22 +473,6 @@ com_disconnect (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
}
static int
port_from_sa (struct mu_sockaddr *sa)
{
switch (sa->addr->sa_family)
{
case AF_INET:
return ntohs (((struct sockaddr_in *)sa->addr)->sin_port);
#ifdef MAILUTILS_IPV6
case AF_INET6:
return ntohs (((struct sockaddr_in6 *)sa->addr)->sin6_port);
#endif
}
return 0;
}
static int
com_connect (int argc, char **argv)
{
int status;
......
This diff is collapsed. Click to expand it.
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010-2012 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 <arpa/inet.h>
#include <mailutils/mailutils.h>
#include "mu.h"
int
port_from_sa (struct mu_sockaddr *sa)
{
switch (sa->addr->sa_family)
{
case AF_INET:
return ntohs (((struct sockaddr_in *)sa->addr)->sin_port);
#ifdef MAILUTILS_IPV6
case AF_INET6:
return ntohs (((struct sockaddr_in6 *)sa->addr)->sin6_port);
#endif
}
return 0;
}