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
Showing
13 changed files
with
295 additions
and
28 deletions
... | @@ -47,7 +47,9 @@ int mu_smtp_write (mu_smtp_t smtp, const char *fmt, ...) MU_PRINTFLIKE(2,3); | ... | @@ -47,7 +47,9 @@ int mu_smtp_write (mu_smtp_t smtp, const char *fmt, ...) MU_PRINTFLIKE(2,3); |
47 | 47 | ||
48 | int mu_smtp_replcode (mu_smtp_t smtp, char *buf); | 48 | int mu_smtp_replcode (mu_smtp_t smtp, char *buf); |
49 | int mu_smtp_sget_reply (mu_smtp_t smtp, const char **pbuf); | 49 | int mu_smtp_sget_reply (mu_smtp_t smtp, const char **pbuf); |
50 | int mu_smtp_get_reply_iterator (mu_smtp_t smtp, mu_iterator_t *pitr); | ||
50 | 51 | ||
52 | int mu_smtp_cmd (mu_smtp_t smtp, int argc, char **argv); | ||
51 | 53 | ||
52 | #define MU_SMTP_TRACE_CLR 0 | 54 | #define MU_SMTP_TRACE_CLR 0 |
53 | #define MU_SMTP_TRACE_SET 1 | 55 | #define MU_SMTP_TRACE_SET 1 |
... | @@ -59,6 +61,7 @@ int mu_smtp_disconnect (mu_smtp_t smtp); | ... | @@ -59,6 +61,7 @@ int mu_smtp_disconnect (mu_smtp_t smtp); |
59 | int mu_smtp_ehlo (mu_smtp_t smtp); | 61 | int mu_smtp_ehlo (mu_smtp_t smtp); |
60 | int mu_smtp_set_param (mu_smtp_t smtp, int code, const char *val); | 62 | int mu_smtp_set_param (mu_smtp_t smtp, int code, const char *val); |
61 | int mu_smtp_get_param (mu_smtp_t smtp, int code, const char **param); | 63 | int mu_smtp_get_param (mu_smtp_t smtp, int code, const char **param); |
64 | int mu_smtp_test_param (mu_smtp_t smtp, int pcode); | ||
62 | int mu_smtp_set_url (mu_smtp_t smtp, mu_url_t url); | 65 | int mu_smtp_set_url (mu_smtp_t smtp, mu_url_t url); |
63 | int mu_smtp_get_url (mu_smtp_t smtp, mu_url_t *purl); | 66 | int mu_smtp_get_url (mu_smtp_t smtp, mu_url_t *purl); |
64 | int mu_smtp_set_secret (mu_smtp_t smtp, mu_secret_t secret); | 67 | int mu_smtp_set_secret (mu_smtp_t smtp, mu_secret_t secret); | ... | ... |
... | @@ -34,6 +34,7 @@ libmu_mailer_la_SOURCES = \ | ... | @@ -34,6 +34,7 @@ libmu_mailer_la_SOURCES = \ |
34 | smtp_capa.c\ | 34 | smtp_capa.c\ |
35 | smtp_capa_itr.c\ | 35 | smtp_capa_itr.c\ |
36 | smtp_carrier.c\ | 36 | smtp_carrier.c\ |
37 | smtp_cmd.c\ | ||
37 | smtp_create.c\ | 38 | smtp_create.c\ |
38 | smtp_data.c\ | 39 | smtp_data.c\ |
39 | smtp_disconnect.c\ | 40 | smtp_disconnect.c\ | ... | ... |
libproto/mailer/smtp_cmd.c
0 → 100644
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2010-2012 Free Software Foundation, Inc. | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU Lesser General Public License as published by | ||
6 | the Free Software Foundation; either version 3, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU Lesser General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Lesser General Public License | ||
15 | along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */ | ||
16 | |||
17 | #ifdef HAVE_CONFIG_H | ||
18 | # include <config.h> | ||
19 | #endif | ||
20 | |||
21 | #include <errno.h> | ||
22 | #include <stdarg.h> | ||
23 | #include <stdlib.h> | ||
24 | #include <string.h> | ||
25 | #include <mailutils/errno.h> | ||
26 | #include <mailutils/cctype.h> | ||
27 | #include <mailutils/list.h> | ||
28 | #include <mailutils/util.h> | ||
29 | #include <mailutils/smtp.h> | ||
30 | #include <mailutils/stream.h> | ||
31 | #include <mailutils/sys/smtp.h> | ||
32 | |||
33 | /* Send an arbitrary command */ | ||
34 | int | ||
35 | mu_smtp_cmd (mu_smtp_t smtp, int argc, char **argv) | ||
36 | { | ||
37 | int status, i; | ||
38 | |||
39 | if (!smtp) | ||
40 | return EINVAL; | ||
41 | if (MU_SMTP_FISSET (smtp, _MU_SMTP_ERR)) | ||
42 | return MU_ERR_FAILURE; | ||
43 | status = mu_smtp_write (smtp, "%s", argv[0]); | ||
44 | MU_SMTP_CHECK_ERROR (smtp, status); | ||
45 | for (i = 1; i < argc; i++) | ||
46 | { | ||
47 | status = mu_smtp_write (smtp, " %s", argv[i]); | ||
48 | MU_SMTP_CHECK_ERROR (smtp, status); | ||
49 | } | ||
50 | |||
51 | status = mu_smtp_write (smtp, "\r\n"); | ||
52 | MU_SMTP_CHECK_ERROR (smtp, status); | ||
53 | status = mu_smtp_response (smtp); | ||
54 | MU_SMTP_CHECK_ERROR (smtp, status); | ||
55 | |||
56 | if (smtp->replcode[0] > '3') | ||
57 | return MU_ERR_REPLY; | ||
58 | |||
59 | return 0; | ||
60 | } | ||
61 | |||
62 |
... | @@ -144,4 +144,10 @@ mu_smtp_sget_reply (mu_smtp_t smtp, const char **pbuf) | ... | @@ -144,4 +144,10 @@ mu_smtp_sget_reply (mu_smtp_t smtp, const char **pbuf) |
144 | return 0; | 144 | return 0; |
145 | } | 145 | } |
146 | 146 | ||
147 | 147 | int | |
148 | mu_smtp_get_reply_iterator (mu_smtp_t smtp, mu_iterator_t *pitr) | ||
149 | { | ||
150 | if (!smtp || !pitr) | ||
151 | return EINVAL; | ||
152 | return mu_list_get_iterator (smtp->mlrepl, pitr); | ||
153 | } | ... | ... |
... | @@ -49,6 +49,8 @@ mu_smtp_set_param (mu_smtp_t smtp, int pcode, const char *newparam) | ... | @@ -49,6 +49,8 @@ mu_smtp_set_param (mu_smtp_t smtp, int pcode, const char *newparam) |
49 | mu_secret_password_unref (smtp->secret); | 49 | mu_secret_password_unref (smtp->secret); |
50 | mu_secret_destroy (&smtp->secret); | 50 | mu_secret_destroy (&smtp->secret); |
51 | } | 51 | } |
52 | if (!newparam) | ||
53 | return 0; | ||
52 | MU_SMTP_FCLR (smtp, _MU_SMTP_CLNPASS); | 54 | MU_SMTP_FCLR (smtp, _MU_SMTP_CLNPASS); |
53 | return mu_secret_create (&smtp->secret, newparam, strlen (newparam)); | 55 | return mu_secret_create (&smtp->secret, newparam, strlen (newparam)); |
54 | } | 56 | } |
... | @@ -56,18 +58,28 @@ mu_smtp_set_param (mu_smtp_t smtp, int pcode, const char *newparam) | ... | @@ -56,18 +58,28 @@ mu_smtp_set_param (mu_smtp_t smtp, int pcode, const char *newparam) |
56 | { | 58 | { |
57 | mu_url_t url; | 59 | mu_url_t url; |
58 | int rc; | 60 | int rc; |
59 | 61 | ||
60 | rc = mu_url_create (&url, newparam); | 62 | if (!newparam) |
61 | if (rc) | 63 | mu_url_destroy (&smtp->url); |
62 | return rc; | 64 | else |
63 | mu_url_destroy (&smtp->url); | 65 | { |
64 | smtp->url = url; | 66 | rc = mu_url_create (&url, newparam); |
67 | if (rc) | ||
68 | return rc; | ||
69 | mu_url_destroy (&smtp->url); | ||
70 | smtp->url = url; | ||
71 | } | ||
65 | return 0; | 72 | return 0; |
66 | } | 73 | } |
67 | 74 | ||
68 | param = strdup (newparam); | 75 | if (newparam) |
69 | if (!param) | 76 | { |
70 | return ENOMEM; | 77 | param = strdup (newparam); |
78 | if (!param) | ||
79 | return ENOMEM; | ||
80 | } | ||
81 | else | ||
82 | param = NULL; | ||
71 | free (smtp->param[pcode]); | 83 | free (smtp->param[pcode]); |
72 | smtp->param[pcode] = param; | 84 | smtp->param[pcode] = param; |
73 | return 0; | 85 | return 0; |
... | @@ -99,3 +111,24 @@ mu_smtp_get_param (mu_smtp_t smtp, int pcode, const char **pparam) | ... | @@ -99,3 +111,24 @@ mu_smtp_get_param (mu_smtp_t smtp, int pcode, const char **pparam) |
99 | return 0; | 111 | return 0; |
100 | } | 112 | } |
101 | 113 | ||
114 | int | ||
115 | mu_smtp_test_param (mu_smtp_t smtp, int pcode) | ||
116 | { | ||
117 | if (!smtp) | ||
118 | return EINVAL; | ||
119 | if (pcode < 0 || pcode >= MU_SMTP_MAX_PARAM) | ||
120 | return EINVAL; | ||
121 | if (pcode == MU_SMTP_PARAM_PASSWORD) | ||
122 | { | ||
123 | if (smtp->secret) | ||
124 | return 0; | ||
125 | return MU_ERR_NOENT; | ||
126 | } | ||
127 | else if (pcode == MU_SMTP_PARAM_URL) | ||
128 | { | ||
129 | if (smtp->url) | ||
130 | return 0; | ||
131 | return MU_ERR_NOENT; | ||
132 | } | ||
133 | return smtp->param[pcode] ? 0 : MU_ERR_NOENT; | ||
134 | } | ... | ... |
... | @@ -269,6 +269,9 @@ void mh_global_save_state (void); | ... | @@ -269,6 +269,9 @@ void mh_global_save_state (void); |
269 | int mh_interactive_mode_p (void); | 269 | int mh_interactive_mode_p (void); |
270 | int mh_getyn (const char *fmt, ...) MU_PRINTFLIKE(1,2); | 270 | int mh_getyn (const char *fmt, ...) MU_PRINTFLIKE(1,2); |
271 | int mh_getyn_interactive (const char *fmt, ...) MU_PRINTFLIKE(1,2); | 271 | int mh_getyn_interactive (const char *fmt, ...) MU_PRINTFLIKE(1,2); |
272 | int mu_vgetans (const char *variants, const char *fmt, va_list ap); | ||
273 | int mu_getans (const char *variants, const char *fmt, ...) | ||
274 | MU_PRINTFLIKE(2,3); | ||
272 | int mh_check_folder (const char *pathname, int confirm); | 275 | int mh_check_folder (const char *pathname, int confirm); |
273 | int mh_makedir (const char *p); | 276 | int mh_makedir (const char *p); |
274 | 277 | ... | ... |
... | @@ -39,6 +39,12 @@ else | ... | @@ -39,6 +39,12 @@ else |
39 | IDLE_MODULES+=dbm.c | 39 | IDLE_MODULES+=dbm.c |
40 | endif | 40 | endif |
41 | 41 | ||
42 | if MU_COND_SUPPORT_SMTP | ||
43 | SMTP_C=smtp.c | ||
44 | else | ||
45 | IDLE_MODULES+=smtp.c | ||
46 | endif | ||
47 | |||
42 | MODULES = \ | 48 | MODULES = \ |
43 | acl.c\ | 49 | acl.c\ |
44 | cflags.c\ | 50 | cflags.c\ |
... | @@ -51,16 +57,20 @@ MODULES = \ | ... | @@ -51,16 +57,20 @@ MODULES = \ |
51 | ldflags.c\ | 57 | ldflags.c\ |
52 | logger.c\ | 58 | logger.c\ |
53 | $(POP_C)\ | 59 | $(POP_C)\ |
54 | send.c\ | ||
55 | query.c\ | 60 | query.c\ |
61 | send.c\ | ||
62 | $(SMTP_C)\ | ||
56 | wicket.c | 63 | wicket.c |
57 | 64 | ||
58 | mu_SOURCES = \ | 65 | mu_SOURCES = \ |
59 | dispatch.c\ | 66 | dispatch.c\ |
67 | getans.c\ | ||
60 | getarg.c\ | 68 | getarg.c\ |
69 | getyn.c\ | ||
61 | mu.h\ | 70 | mu.h\ |
62 | mu.c\ | 71 | mu.c\ |
63 | shell.c\ | 72 | shell.c\ |
73 | util.c\ | ||
64 | verbose.c\ | 74 | verbose.c\ |
65 | $(MODULES) | 75 | $(MODULES) |
66 | 76 | ... | ... |
mu/getans.c
0 → 100644
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2010, 2012 Free Software Foundation, Inc. | ||
3 | |||
4 | GNU Mailutils is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 3, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | GNU Mailutils is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */ | ||
16 | |||
17 | #ifdef HAVE_CONFIG_H | ||
18 | # include <config.h> | ||
19 | #endif | ||
20 | #include <mailutils/mailutils.h> | ||
21 | #include "mu.h" | ||
22 | |||
23 | int | ||
24 | mu_vgetans (const char *variants, const char *fmt, va_list ap) | ||
25 | { | ||
26 | char repl[64]; | ||
27 | |||
28 | while (1) | ||
29 | { | ||
30 | size_t n; | ||
31 | char *p; | ||
32 | |||
33 | mu_stream_vprintf (mu_strout, fmt, ap); | ||
34 | mu_stream_write (mu_strout, "? ", 2, NULL); | ||
35 | mu_stream_flush (mu_strout); | ||
36 | if (mu_stream_read (mu_strin, repl, sizeof repl, &n) || n == 0) | ||
37 | return 0; | ||
38 | mu_rtrim_class (repl, MU_CTYPE_ENDLN); | ||
39 | |||
40 | p = strchr (variants, *repl); | ||
41 | if (p) | ||
42 | return *p; | ||
43 | |||
44 | mu_stream_printf (mu_strout, _("Please answer one of [%s]: "), variants); | ||
45 | } | ||
46 | return 0; /* to pacify gcc */ | ||
47 | } | ||
48 | |||
49 | int | ||
50 | mu_getans (const char *variants, const char *fmt, ...) | ||
51 | { | ||
52 | va_list ap; | ||
53 | int rc; | ||
54 | |||
55 | va_start (ap, fmt); | ||
56 | rc = mu_vgetans (variants, fmt, ap); | ||
57 | va_end (ap); | ||
58 | return rc; | ||
59 | } | ||
60 |
mu/getyn.c
0 → 100644
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2010, 2012 Free Software Foundation, Inc. | ||
3 | |||
4 | GNU Mailutils is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 3, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | GNU Mailutils is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */ | ||
16 | |||
17 | #ifdef HAVE_CONFIG_H | ||
18 | # include <config.h> | ||
19 | #endif | ||
20 | #include <mailutils/mailutils.h> | ||
21 | #include "mu.h" | ||
22 | |||
23 | int | ||
24 | mu_vgetyn (const char *fmt, va_list ap) | ||
25 | { | ||
26 | char repl[64]; | ||
27 | |||
28 | while (1) | ||
29 | { | ||
30 | int rc; | ||
31 | size_t n; | ||
32 | |||
33 | mu_stream_vprintf (mu_strout, fmt, ap); | ||
34 | mu_stream_write (mu_strout, "? ", 2, NULL); | ||
35 | mu_stream_flush (mu_strout); | ||
36 | if (mu_stream_read (mu_strin, repl, sizeof repl, &n) || n == 0) | ||
37 | return 0; | ||
38 | mu_rtrim_class (repl, MU_CTYPE_ENDLN); | ||
39 | |||
40 | rc = mu_true_answer_p (repl); | ||
41 | |||
42 | if (rc >= 0) | ||
43 | return rc; | ||
44 | |||
45 | /* TRANSLATORS: See msgids "nN" and "yY". */ | ||
46 | mu_stream_printf (mu_strout, "%s", _("Please answer yes or no: ")); | ||
47 | } | ||
48 | return 0; /* to pacify gcc */ | ||
49 | } | ||
50 | |||
51 | int | ||
52 | mu_getyn (const char *fmt, ...) | ||
53 | { | ||
54 | va_list ap; | ||
55 | int rc; | ||
56 | |||
57 | va_start (ap, fmt); | ||
58 | rc = mu_vgetyn (fmt, ap); | ||
59 | va_end (ap); | ||
60 | return rc; | ||
61 | } | ||
62 |
... | @@ -42,6 +42,8 @@ int mu_help (void); | ... | @@ -42,6 +42,8 @@ int mu_help (void); |
42 | mutool_action_t dispatch_find_action (const char *name); | 42 | mutool_action_t dispatch_find_action (const char *name); |
43 | char *dispatch_docstring (const char *text); | 43 | char *dispatch_docstring (const char *text); |
44 | 44 | ||
45 | int port_from_sa (struct mu_sockaddr *sa); | ||
46 | |||
45 | 47 | ||
46 | #define VERBOSE_MASK(n) (1<<((n)+1)) | 48 | #define VERBOSE_MASK(n) (1<<((n)+1)) |
47 | #define SET_VERBOSE_MASK(n) (shell_verbose_flags |= VERBOSE_MASK (n)) | 49 | #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) | ... | @@ -473,22 +473,6 @@ com_disconnect (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED) |
473 | } | 473 | } |
474 | 474 | ||
475 | static int | 475 | static int |
476 | port_from_sa (struct mu_sockaddr *sa) | ||
477 | { | ||
478 | switch (sa->addr->sa_family) | ||
479 | { | ||
480 | case AF_INET: | ||
481 | return ntohs (((struct sockaddr_in *)sa->addr)->sin_port); | ||
482 | |||
483 | #ifdef MAILUTILS_IPV6 | ||
484 | case AF_INET6: | ||
485 | return ntohs (((struct sockaddr_in6 *)sa->addr)->sin6_port); | ||
486 | #endif | ||
487 | } | ||
488 | return 0; | ||
489 | } | ||
490 | |||
491 | static int | ||
492 | com_connect (int argc, char **argv) | 476 | com_connect (int argc, char **argv) |
493 | { | 477 | { |
494 | int status; | 478 | int status; | ... | ... |
mu/smtp.c
0 → 100644
This diff is collapsed.
Click to expand it.
mu/util.c
0 → 100644
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2010-2012 Free Software Foundation, Inc. | ||
3 | |||
4 | GNU Mailutils is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 3, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | GNU Mailutils is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */ | ||
16 | |||
17 | #if defined(HAVE_CONFIG_H) | ||
18 | # include <config.h> | ||
19 | #endif | ||
20 | |||
21 | #include <stdlib.h> | ||
22 | #include <arpa/inet.h> | ||
23 | #include <mailutils/mailutils.h> | ||
24 | #include "mu.h" | ||
25 | |||
26 | int | ||
27 | port_from_sa (struct mu_sockaddr *sa) | ||
28 | { | ||
29 | switch (sa->addr->sa_family) | ||
30 | { | ||
31 | case AF_INET: | ||
32 | return ntohs (((struct sockaddr_in *)sa->addr)->sin_port); | ||
33 | |||
34 | #ifdef MAILUTILS_IPV6 | ||
35 | case AF_INET6: | ||
36 | return ntohs (((struct sockaddr_in6 *)sa->addr)->sin6_port); | ||
37 | #endif | ||
38 | } | ||
39 | return 0; | ||
40 | } | ||
41 |
-
Please register or sign in to post a comment