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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2001, 2005, 2007-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/>. */
#include "imap4d.h"
/*
6.4.7. COPY Command
Arguments: message set
mailbox name
Responses: no specific responses for this command
Result: OK - copy completed
NO - copy error: can't copy those messages or to that
name
BAD - command unknown or arguments invalid
copy messages in argv[2] to mailbox in argv[3]
*/
int
imap4d_copy (struct imap4d_session *session,
struct imap4d_command *command, imap4d_tokbuf_t tok)
{
int rc;
char *text;
if (imap4d_tokbuf_argc (tok) != 4)
return io_completion_response (command, RESP_BAD, "Invalid arguments");
rc = imap4d_copy0 (tok, 0, &text);
if (rc == RESP_NONE)
{
/* Reset the state ourself. */
int new_state = (rc == RESP_OK) ? command->success : command->failure;
if (new_state != STATE_NONE)
state = new_state;
return io_sendf ("%s %s\n", command->tag, text);
}
return io_completion_response (command, rc, "%s", text);
}
struct copy_env
{
mu_mailbox_t dst;
mu_off_t total;
int ret;
char **err_text;
};
static int
size_sum (size_t msgno, mu_message_t msg, void *data)
{
struct copy_env *env = data;
int rc;
size_t size;
rc = mu_message_size (msg, &size);
if (rc)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_message_size", NULL, rc);
env->ret = RESP_BAD;
return MU_ERR_FAILURE;
}
env->total += size;
return 0;
}
static int
do_copy (size_t msgno, mu_message_t msg, void *data)
{
struct copy_env *env = data;
int status;
imap4d_enter_critical ();
status = mu_mailbox_append_message (env->dst, msg);
imap4d_leave_critical ();
if (status)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_append_message", NULL,
status);
env->ret = RESP_BAD;
return MU_ERR_FAILURE;
}
return 0;
}
static int
try_copy (mu_mailbox_t dst, mu_msgset_t msgset, char **err_text)
{
int rc;
struct copy_env env;
env.dst = dst;
env.total = 0;
env.ret = RESP_OK;
env.err_text = err_text;
*env.err_text = "Operation failed";
/* Check size */
rc = mu_msgset_foreach_message (msgset, size_sum, &env);
if (rc)
return RESP_NO;
if (env.ret != RESP_OK)
return env.ret;
rc = quota_check (env.total);
if (rc)
{
*env.err_text = "Mailbox quota exceeded";
return RESP_NO;
}
env.total = 0;
rc = mu_msgset_foreach_message (msgset, do_copy, &env);
quota_update (env.total);
if (rc)
return RESP_NO;
return env.ret;
}
static int
safe_copy (mu_mailbox_t dst, mu_msgset_t msgset, char **err_text)
{
size_t nmesg;
int status;
status = mu_mailbox_messages_count (dst, &nmesg);
if (status)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_messages_count",
NULL, status);
*err_text = "Operation failed";
return RESP_NO;
}
status = try_copy (dst, msgset, err_text);
if (status != RESP_OK)
{
size_t maxmesg;
/* If the COPY command is unsuccessful for any reason, server
implementations MUST restore the destination mailbox to its state
before the COPY attempt. */
status = mu_mailbox_messages_count (dst, &maxmesg);
if (status)
{
mu_url_t url = NULL;
mu_mailbox_get_url (dst, &url);
mu_error (_("cannot count messages in mailbox %s: %s"),
mu_url_to_string (url), mu_strerror (status));
imap4d_bye (ERR_MAILBOX_CORRUPTED);
}
for (nmesg++; nmesg <= maxmesg; nmesg++)
{
mu_message_t msg;
if (mu_mailbox_get_message (dst, nmesg, &msg) == 0)
{
mu_attribute_t attr;
mu_message_get_attribute (msg, &attr);
mu_attribute_set_userflag (attr, MU_ATTRIBUTE_DELETED);
}
}
imap4d_enter_critical ();
status = mu_mailbox_flush (dst, 1);
imap4d_leave_critical ();
if (status)
{
mu_url_t url = NULL;
mu_mailbox_get_url (dst, &url);
mu_error (_("cannot flush mailbox %s: %s"),
mu_url_to_string (url), mu_strerror (status));
imap4d_bye (ERR_MAILBOX_CORRUPTED);
}
return RESP_NO;
}
return RESP_OK;
}
int
imap4d_copy0 (imap4d_tokbuf_t tok, int isuid, char **err_text)
{
int status;
char *msgset_str;
mu_msgset_t msgset;
char *name;
char *mailbox_name;
char *end;
mu_mailbox_t cmbox = NULL;
int arg = IMAP4_ARG_1 + !!isuid;
int ns;
*err_text = NULL;
if (imap4d_tokbuf_argc (tok) != arg + 2)
{
*err_text = "Invalid arguments";
return 1;
}
msgset_str = imap4d_tokbuf_getarg (tok, arg);
name = imap4d_tokbuf_getarg (tok, arg + 1);
status = mu_msgset_create (&msgset, mbox, MU_MSGSET_NUM);
if (status)
{
*err_text = "Software error";
return RESP_BAD;
}
status = mu_msgset_parse_imap (msgset,
isuid ? MU_MSGSET_UID : MU_MSGSET_NUM,
msgset_str, &end);
if (status)
{
mu_msgset_free (msgset);
*err_text = "Error parsing message set";
/* FIXME: print error location */
return RESP_BAD;
}
mailbox_name = namespace_getfullpath (name, &ns);
if (!mailbox_name)
{
mu_msgset_free (msgset);
*err_text = "Copy failed";
return RESP_NO;
}
/* If the destination mailbox does not exist, a server should return
an error. */
status = mu_mailbox_create_default (&cmbox, mailbox_name);
if (status == 0)
{
/* It SHOULD NOT automatifcllly create the mailbox. */
status = mu_mailbox_open (cmbox, MU_STREAM_RDWR | mailbox_mode[ns]);
if (status == 0)
{
mu_list_t msglist;
mu_msgset_get_list (msgset, &msglist);
if (!mu_list_is_empty (msglist))
status = safe_copy (cmbox, msgset, err_text);
mu_mailbox_close (cmbox);
}
mu_mailbox_destroy (&cmbox);
}
mu_msgset_free (msgset);
if (status == 0)
{
*err_text = "Completed";
return RESP_OK;
}
/* Unless it is certain that the destination mailbox cannot be created,
the server MUST send the response code "[TRYCREATE]" as the prefix
of the text of the tagged NO response. This gives a hint to the
client that it can attempt a CREATE command and retry the copy if
the CREATE is successful. */
if (!*err_text)
*err_text = "[TRYCREATE] failed";
return RESP_NO;
}
|