1 /* GNU Mailutils -- a suite of utilities for electronic mail
2    Copyright (C) 2011-2021 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 #include <config.h>
18 #include <stdarg.h>
19 #include <string.h>
20 #include <mailutils/imapio.h>
21 #include <mailutils/stream.h>
22 #include <mailutils/sys/imapio.h>
23 
24 /* Send a IMAP command to the server, quoting its arguments as necessary.
25    TAG is the command tag, CMD is the command verb.  Command arguments are
26    given in variadic list terminated with NULL.
27 
28    If MSGSET is supplied, it is sent when the function encounters the
29    "\\" (single backslash) in ARGV.
30 */
31 int
mu_imapio_send_command(struct _mu_imapio * io,const char * tag,mu_msgset_t msgset,char const * cmd,...)32 mu_imapio_send_command (struct _mu_imapio *io, const char *tag,
33 			mu_msgset_t msgset, char const *cmd, ...)
34 {
35   va_list ap;
36 
37   va_start (ap, cmd);
38   mu_imapio_printf (io, "%s %s", tag, cmd);
39   while ((cmd = va_arg (ap, char *)))
40     {
41       mu_imapio_send (io, " ", 1);
42       if (msgset && strcmp (cmd, "\\") == 0)
43 	mu_imapio_send_msgset (io, msgset);
44       else
45 	mu_imapio_send_qstring (io, cmd);
46     }
47   va_end (ap);
48   mu_imapio_send (io, "\r\n", 2);
49   return mu_stream_last_error (io->_imap_stream);
50 }
51