Commit 31459010 31459010ef8c1bf790b769260788982c714624d7 by Sergey Poznyakoff

Imap client: quote command arguments as necessary.

* include/mailutils/imapio.h (mu_imapio_send_command)
(mu_imapio_send_command_e,mu_imapio_send_command_v)
(mu_imapio_last_error,mu_imapio_clearerr): New protos.
* include/mailutils/sys/imap.h (imap_command)<uid>: Remove.
<extra>: New member.
* libmailutils/imapio/Makefile.am (libimapio_la_SOURCES): Add new files.
* libmailutils/imapio/qstring.c (mu_imapio_send_qstring_unfold): Improve
functionality.
* libmailutils/imapio/sendcmd.c: New file.
* libmailutils/imapio/sendcmde.c: New file.
* libmailutils/imapio/sendcmdv.c: New file.
* libmailutils/imapio/errstate.c: New file.

* libproto/imap/gencom.c (mu_imap_gencom): Use mu_imapio_send_command_v
* libproto/imap/appstrsiz.c: Quote command arguments as necessary.
* libproto/imap/capability.c: Likewise.
* libproto/imap/check.c: Likewise.
* libproto/imap/close.c: Likewise.
* libproto/imap/copy.c: Likewise.
* libproto/imap/delete.c: Likewise.
* libproto/imap/expunge.c: Likewise.
* libproto/imap/fetch.c: Likewise.
* libproto/imap/list.c: Likewise.
* libproto/imap/login.c: Likewise.
* libproto/imap/mbcreate.c: Likewise.
* libproto/imap/noop.c: Likewise.
* libproto/imap/rename.c: Likewise.
* libproto/imap/select.c: Likewise.
* libproto/imap/store.c: Likewise.
* libproto/imap/unselect.c: Likewise.
1 parent 7e78c6af
......@@ -45,6 +45,13 @@ int mu_imapio_send_qstring (struct _mu_imapio *io, const char *buffer);
int mu_imapio_send_qstring_unfold (struct _mu_imapio *io, const char *buffer,
int unfold);
int mu_imapio_send_command_v (struct _mu_imapio *io, const char *tag,
int argc, char const **argv, const char *extra);
int mu_imapio_send_command (struct _mu_imapio *io, const char *tag,
char const *cmd, ...);
int mu_imapio_send_command_e (struct _mu_imapio *io, const char *tag,
char const *cmd, ...);
int mu_imapio_send_flags (struct _mu_imapio *io, int flags);
int mu_imapio_send_time (struct _mu_imapio *io, struct tm *tm,
struct mu_timezone *tz);
......@@ -59,6 +66,9 @@ int mu_imapio_getbuf (mu_imapio_t io, char **pptr, size_t *psize);
int mu_imapio_reply_string (struct _mu_imapio *io, size_t start, char **pbuf);
int mu_imapio_last_error (struct _mu_imapio *io);
void mu_imapio_clearerr (struct _mu_imapio *io);
int mu_imap_flag_to_attribute (const char *item, int *attr);
int mu_imap_format_flags (mu_stream_t str, int flags);
......
......@@ -144,9 +144,9 @@ struct imap_command
int session_state;
char *capa;
int rx_state;
int uid;
int argc;
char const **argv;
char const *extra;
void (*tagged_handler) (mu_imap_t);
mu_imap_response_action_t untagged_handler;
void *untagged_handler_data;
......
......@@ -19,6 +19,7 @@ noinst_LTLIBRARIES = libimapio.la
libimapio_la_SOURCES = \
create.c\
errstate.c\
flags.c\
getline.c\
litstream.c\
......@@ -27,6 +28,9 @@ libimapio_la_SOURCES = \
qstring.c\
replstr.c\
send.c\
sendcmd.c\
sendcmdv.c\
sendcmde.c\
time.c\
trace.c\
words.c\
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011 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/>. */
#include <config.h>
#include <mailutils/stream.h>
#include <mailutils/sys/imapio.h>
int
mu_imapio_last_error (struct _mu_imapio *io)
{
return mu_stream_last_error (io->_imap_stream);
}
void
mu_imapio_clearerr (struct _mu_imapio *io)
{
mu_stream_clearerr (io->_imap_stream);
}
......@@ -19,50 +19,80 @@
#include <mailutils/types.h>
#include <mailutils/imapio.h>
#include <mailutils/stream.h>
#include <mailutils/cstr.h>
#include <mailutils/cctype.h>
#include <mailutils/sys/imapio.h>
/* Send NIL if empty string, change the quoted string to a literal if the
string contains: double quotes, CR, LF, or \. */
/* If string is NULL, send NIL.
If it contains \r\n, send it as a literal, replacing
contiguous sequences of \r\n by a single space, if UNFOLD is set.
If string contains " or \, quote it,
Otherwise send it as is. */
int
mu_imapio_send_qstring_unfold (struct _mu_imapio *io, const char *buffer,
int unfold)
{
if (buffer == NULL || *buffer == '\0')
int len;
if (buffer == NULL)
return mu_imapio_printf (io, "NIL");
if (strchr (buffer, '"') ||
strchr (buffer, '\r') ||
strchr (buffer, '\n') ||
strchr (buffer, '\\'))
if (buffer[len = strcspn (buffer, "\r\n")])
{
if (unfold)
{
int rc;
size_t len = strlen (buffer);
size_t size = strlen (buffer);
rc = mu_stream_printf (io->_imap_stream,
"{%lu}\n", (unsigned long) strlen (buffer));
"{%lu}\n", (unsigned long) size);
if (rc)
return rc;
for (;;)
while (1)
{
size_t s = strcspn (buffer, "\r\n");
rc = mu_stream_write (io->_imap_stream, buffer, s, NULL);
if (rc)
return rc;
len -= s;
if (len == 0)
mu_stream_write (io->_imap_stream, buffer, len, NULL);
buffer += len;
if (*buffer)
{
mu_stream_write (io->_imap_stream, " ", 1, NULL);
buffer = mu_str_skip_class (buffer, MU_CTYPE_ENDLN);
len = strcspn (buffer, "\r\n");
}
else
break;
buffer += s;
rc = mu_stream_write (io->_imap_stream, " ", 1, NULL);
if (rc)
return rc;
}
}
else
return mu_imapio_send_literal_string (io, buffer);
mu_imapio_send_literal_string (io, buffer);
}
return mu_imapio_printf (io, "\"%s\"", buffer);
else if (buffer[len = strcspn (buffer, io->_imap_ws.ws_escape)])
{
int rc;
rc = mu_stream_write (io->_imap_stream, "\"", 1, NULL);
while (1)
{
mu_stream_write (io->_imap_stream, buffer, len, NULL);
buffer += len;
if (*buffer)
{
mu_stream_write (io->_imap_stream, "\\", 1, NULL);
mu_stream_write (io->_imap_stream, buffer, 1, NULL);
buffer++;
len = strcspn (buffer, io->_imap_ws.ws_escape);
}
else
break;
}
mu_stream_write (io->_imap_stream, "\"", 1, NULL);
}
else if (buffer[0] == 0 || buffer[strcspn (buffer, io->_imap_ws.ws_delim)])
mu_stream_printf (io->_imap_stream, "\"%s\"", buffer);
else
mu_stream_write (io->_imap_stream, buffer, len, NULL);
return mu_stream_last_error (io->_imap_stream);
}
int
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011 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/>. */
#include <config.h>
#include <stdarg.h>
#include <string.h>
#include <mailutils/imapio.h>
#include <mailutils/stream.h>
#include <mailutils/sys/imapio.h>
/* Send a IMAP command to the server, quoting its arguments as necessary.
TAG is the command tag, CMD is the command verb. Command arguments are
given in variadic list terminated with NULL. */
int
mu_imapio_send_command (struct _mu_imapio *io, const char *tag,
char const *cmd, ...)
{
va_list ap;
va_start (ap, cmd);
mu_imapio_printf (io, "%s %s", tag, cmd);
while ((cmd = va_arg (ap, char *)))
{
mu_imapio_send (io, " ", 1);
mu_imapio_send_qstring (io, cmd);
}
va_end (ap);
mu_imapio_send (io, "\r\n", 2);
return mu_stream_last_error (io->_imap_stream);
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011 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/>. */
#include <config.h>
#include <stdarg.h>
#include <string.h>
#include <mailutils/imapio.h>
#include <mailutils/stream.h>
#include <mailutils/sys/imapio.h>
/* Send a IMAP command to the server, quoting its arguments as necessary.
TAG is the command tag, CMD is the command verb. Command arguments are
given in variadic list terminated with NULL. The last argument is sent
over the wire as is, without quoting. */
int
mu_imapio_send_command_e (struct _mu_imapio *io, const char *tag,
char const *cmd, ...)
{
va_list ap;
va_start (ap, cmd);
mu_imapio_printf (io, "%s %s", tag, cmd);
cmd = va_arg (ap, char *);
while (cmd)
{
char const *next = va_arg (ap, char *);
mu_imapio_send (io, " ", 1);
if (next)
mu_imapio_send_qstring (io, cmd);
else
mu_imapio_send (io, cmd, strlen (cmd));
cmd = next;
}
va_end (ap);
mu_imapio_send (io, "\r\n", 2);
return mu_stream_last_error (io->_imap_stream);
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011 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/>. */
#include <config.h>
#include <string.h>
#include <mailutils/imapio.h>
#include <mailutils/stream.h>
#include <mailutils/sys/imapio.h>
/* Send a IMAP command to the server, quoting its arguments as necessary.
TAG is the command tag, ARGV contains ARGC elements and supplies the
command (in ARGV[0]) and its arguments. EXTRA (if not NULL) supplies
additional arguments that will be sent as is. */
int
mu_imapio_send_command_v (struct _mu_imapio *io, const char *tag,
int argc, char const **argv, const char *extra)
{
int i;
mu_imapio_printf (io, "%s %s", tag, argv[0]);
for (i = 1; i < argc; i++)
{
mu_imapio_send (io, " ", 1);
mu_imapio_send_qstring (io, argv[i]);
}
if (extra)
{
mu_imapio_send (io, " ", 1);
mu_imapio_send (io, extra, strlen (extra));
}
mu_imapio_send (io, "\r\n", 2);
return mu_stream_last_error (io->_imap_stream);
}
......@@ -80,8 +80,9 @@ mu_imap_append_stream_size (mu_imap_t imap, const char *mailbox, int flags,
status = _mu_imap_tag_next (imap);
MU_IMAP_CHECK_EAGAIN (imap, status);
status = mu_imapio_printf (imap->io, "%s APPEND %s",
imap->tag_str, mailbox);
status = mu_imapio_printf (imap->io, "%s APPEND ", imap->tag_str);
MU_IMAP_CHECK_ERROR (imap, status);
status = mu_imapio_send_qstring (imap->io, mailbox);
MU_IMAP_CHECK_ERROR (imap, status);
if (flags)
{
......
......@@ -109,8 +109,8 @@ mu_imap_capability (mu_imap_t imap, int reread, mu_iterator_t *piter)
case MU_IMAP_CLIENT_READY:
status = _mu_imap_tag_next (imap);
MU_IMAP_CHECK_EAGAIN (imap, status);
status = mu_imapio_printf (imap->io, "%s CAPABILITY\r\n",
imap->tag_str);
status = mu_imapio_send_command (imap->io, imap->tag_str,
"CAPABILITY", NULL);
MU_IMAP_CHECK_EAGAIN (imap, status);
MU_IMAP_FCLR (imap, MU_IMAP_RESP);
imap->client_state = MU_IMAP_CLIENT_CAPABILITY_RX;
......
......@@ -30,7 +30,6 @@ mu_imap_check (mu_imap_t imap)
MU_IMAP_SESSION_SELECTED,
NULL,
MU_IMAP_CLIENT_CHECK_RX,
0,
1,
&command,
NULL
......
......@@ -37,9 +37,9 @@ mu_imap_close (mu_imap_t imap)
MU_IMAP_SESSION_SELECTED,
NULL,
MU_IMAP_CLIENT_CLOSE_RX,
0,
1,
&command,
NULL,
_mu_close_handler
};
return mu_imap_gencom (imap, &com);
......
......@@ -26,19 +26,23 @@
int
mu_imap_copy (mu_imap_t imap, int uid, const char *msgset, const char *mailbox)
{
char const *argv[3];
char const *argv[4];
int i;
static struct imap_command com;
argv[0] = "COPY";
argv[1] = msgset;
argv[2] = mailbox;
i = 0;
if (uid)
argv[i++] = "UID";
argv[i++] = "COPY";
argv[i++] = msgset;
argv[i++] = mailbox;
com.session_state = MU_IMAP_SESSION_SELECTED;
com.capa = NULL;
com.rx_state = MU_IMAP_CLIENT_COPY_RX;
com.uid = 0;
com.argc = 3;
com.argc = i;
com.argv = argv;
com.extra = NULL;
com.tagged_handler = NULL;
com.untagged_handler = NULL;
......
......@@ -37,9 +37,9 @@ mu_imap_delete (mu_imap_t imap, const char *mailbox)
com.session_state = MU_IMAP_SESSION_AUTH;
com.capa = NULL;
com.rx_state = MU_IMAP_CLIENT_DELETE_RX;
com.uid = 0;
com.argc = 2;
com.argv = argv;
com.extra = NULL;
com.tagged_handler = NULL;
com.untagged_handler = NULL;
......
......@@ -30,7 +30,6 @@ mu_imap_expunge (mu_imap_t imap)
MU_IMAP_SESSION_SELECTED,
NULL,
MU_IMAP_CLIENT_EXPUNGE_RX,
0,
1,
&command,
NULL
......
......@@ -32,18 +32,21 @@ int
mu_imap_fetch (mu_imap_t imap, int uid, const char *msgset, const char *items)
{
char const *argv[3];
int i;
static struct imap_command com;
argv[0] = "FETCH";
argv[1] = msgset;
argv[2] = items;
i = 0;
if (uid)
argv[i++] = "UID";
argv[i++] = "FETCH";
argv[i++] = msgset;
com.session_state = MU_IMAP_SESSION_SELECTED;
com.capa = NULL;
com.rx_state = MU_IMAP_CLIENT_FETCH_RX;
com.uid = uid;
com.argc = 3;
com.argc = i;
com.argv = argv;
com.extra = items;
com.tagged_handler = NULL;
com.untagged_handler = NULL;
......
......@@ -28,7 +28,6 @@ int
mu_imap_gencom (mu_imap_t imap, struct imap_command *cmd)
{
int status;
int i;
if (imap == NULL || !cmd || cmd->argc < 1)
return EINVAL;
......@@ -57,16 +56,8 @@ mu_imap_gencom (mu_imap_t imap, struct imap_command *cmd)
{
status = _mu_imap_tag_next (imap);
MU_IMAP_CHECK_EAGAIN (imap, status);
status = mu_imapio_printf (imap->io, "%s", imap->tag_str);
if (status == 0 && cmd->uid)
status = mu_imapio_printf (imap->io, " UID");
MU_IMAP_CHECK_ERROR (imap, status);
for (i = 0; i < cmd->argc; i++)
{
status = mu_imapio_printf (imap->io, " %s", cmd->argv[i]);
MU_IMAP_CHECK_ERROR (imap, status);
}
status = mu_imapio_send (imap->io, "\r\n", 2);
status = mu_imapio_send_command_v (imap->io, imap->tag_str,
cmd->argc, cmd->argv, cmd->extra);
MU_IMAP_CHECK_ERROR (imap, status);
MU_IMAP_FCLR (imap, MU_IMAP_RESP);
imap->client_state = cmd->rx_state;
......
......@@ -139,9 +139,9 @@ mu_imap_list (mu_imap_t imap, const char *refname, const char *mboxname,
com.session_state = MU_IMAP_SESSION_AUTH;
com.capa = NULL;
com.rx_state = MU_IMAP_CLIENT_LIST_RX;
com.uid = 0;
com.argc = 3;
com.argv = argv;
com.extra = NULL;
com.tagged_handler = NULL;
com.untagged_handler = list_untagged_handler;
com.untagged_handler_data = &clos;
......
......@@ -43,8 +43,8 @@ mu_imap_login (mu_imap_t imap, const char *user, const char *pass)
_mu_imap_xscript_level (imap, MU_XSCRIPT_SECURE);
status = _mu_imap_tag_next (imap);
MU_IMAP_CHECK_EAGAIN (imap, status);
status = mu_imapio_printf (imap->io, "%s LOGIN \"%s\" \"%s\"\r\n",
imap->tag_str, user, pass);
status = mu_imapio_send_command (imap->io, imap->tag_str,
"LOGIN", user, pass, NULL);
_mu_imap_xscript_level (imap, MU_XSCRIPT_NORMAL);
/* FIXME: how to obscure the passwd in the stream buffer? */
MU_IMAP_CHECK_EAGAIN (imap, status);
......
......@@ -37,9 +37,9 @@ mu_imap_mailbox_create (mu_imap_t imap, const char *mailbox)
com.session_state = MU_IMAP_SESSION_AUTH;
com.capa = NULL;
com.rx_state = MU_IMAP_CLIENT_DELETE_RX;
com.uid = 0;
com.argc = 2;
com.argv = argv;
com.extra = 0;
com.tagged_handler = NULL;
com.untagged_handler = NULL;
......
......@@ -30,7 +30,6 @@ mu_imap_noop (mu_imap_t imap)
MU_IMAP_SESSION_INIT,
NULL,
MU_IMAP_CLIENT_NOOP_RX,
0,
1,
&command,
NULL
......
......@@ -36,9 +36,9 @@ mu_imap_rename (mu_imap_t imap, const char *mailbox, const char *new_mailbox)
com.session_state = MU_IMAP_SESSION_AUTH;
com.capa = NULL;
com.rx_state = MU_IMAP_CLIENT_DELETE_RX;
com.uid = 0;
com.argc = 3;
com.argv = argv;
com.extra = NULL;
com.tagged_handler = NULL;
com.untagged_handler = NULL;
......
......@@ -101,10 +101,9 @@ mu_imap_select (mu_imap_t imap, const char *mbox, int writable,
case MU_IMAP_CLIENT_READY:
status = _mu_imap_tag_next (imap);
MU_IMAP_CHECK_EAGAIN (imap, status);
status = mu_imapio_printf (imap->io, "%s %s %s\r\n",
imap->tag_str,
status = mu_imapio_send_command (imap->io, imap->tag_str,
writable ? "SELECT" : "EXAMINE",
mbox);
mbox, NULL);
MU_IMAP_CHECK_ERROR (imap, status);
MU_IMAP_FCLR (imap, MU_IMAP_RESP);
imap->client_state = MU_IMAP_CLIENT_SELECT_RX;
......
......@@ -29,18 +29,21 @@ int
mu_imap_store (mu_imap_t imap, int uid, const char *msgset, const char *items)
{
char const *argv[3];
int i;
static struct imap_command com;
argv[0] = "STORE";
argv[1] = msgset;
argv[2] = items;
i = 0;
if (uid)
argv[i++] = "UID";
argv[i++] = "STORE";
argv[i++] = msgset;
com.session_state = MU_IMAP_SESSION_SELECTED;
com.capa = NULL;
com.rx_state = MU_IMAP_CLIENT_STORE_RX;
com.uid = uid;
com.argc = 3;
com.argc = i;
com.argv = argv;
com.extra = items;
com.tagged_handler = NULL;
com.untagged_handler = NULL;
......
......@@ -30,9 +30,9 @@ mu_imap_unselect (mu_imap_t imap)
MU_IMAP_SESSION_SELECTED,
NULL,
MU_IMAP_CLIENT_UNSELECT_RX,
0,
1,
&command,
NULL,
_mu_close_handler
};
return mu_imap_gencom (imap, &com);
......