qstring.c
2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011-2012, 2014 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/types.h>
#include <mailutils/imapio.h>
#include <mailutils/stream.h>
#include <mailutils/cstr.h>
#include <mailutils/cctype.h>
#include <mailutils/sys/imapio.h>
/* 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)
{
int len;
if (buffer == NULL)
return mu_imapio_printf (io, "NIL");
if (buffer[len = strcspn (buffer, "\r\n")])
{
if (unfold)
{
int rc;
size_t size = strlen (buffer);
rc = mu_stream_printf (io->_imap_stream,
"{%lu}\n", (unsigned long) size);
if (rc)
return rc;
while (1)
{
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;
}
}
else
mu_imapio_send_literal_string (io, 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
mu_imapio_send_qstring (struct _mu_imapio *io, const char *buffer)
{
return mu_imapio_send_qstring_unfold (io, buffer, 0);
}