1 /* GNU Mailutils -- a suite of utilities for electronic mail
2    Copyright (C) 1999-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 "imap4d.h"
18 #include <unistd.h>
19 
20 /*
21 6.3.3.  CREATE Command
22 
23    Arguments:  mailbox name
24 
25    Responses:  no specific responses for this command
26 
27    Result:     OK - create completed
28                NO - create failure: can't create mailbox with that name
29                BAD - command unknown or arguments invalid
30 */
31 /* FIXME: How do we do this ??????:
32    IF a new mailbox is created with the same name as a mailbox which was
33    deleted, its unique identifiers MUST be greater than any unique identifiers
34    used in the previous incarnation of the mailbox.  */
35 int
imap4d_create(struct imap4d_session * session,struct imap4d_command * command,imap4d_tokbuf_t tok)36 imap4d_create (struct imap4d_session *session,
37                struct imap4d_command *command, imap4d_tokbuf_t tok)
38 {
39   char *name;
40   int isdir = 0;
41   int rc = RESP_OK;
42   const char *msg = "Completed";
43   mu_record_t record;
44   int mode;
45 
46   if (imap4d_tokbuf_argc (tok) != 3)
47     return io_completion_response (command, RESP_BAD, "Invalid arguments");
48 
49   name = imap4d_tokbuf_getarg (tok, IMAP4_ARG_1);
50 
51   if (*name == '\0')
52     return io_completion_response (command, RESP_BAD, "Too few arguments");
53 
54   /* Creating, "Inbox" should always fail.  */
55   if (mu_c_strcasecmp (name, "INBOX") == 0)
56     return io_completion_response (command, RESP_BAD, "Already exist");
57 
58   /* RFC 3501:
59          If the mailbox name is suffixed with the server's hierarchy
60 	 separator character, this is a declaration that the client intends
61 	 to create mailbox names under this name in the hierarchy.
62 
63      The trailing delimiter will be removed by namespace normalizer, so
64      test for it now.
65   */
66   if (name[strlen (name) - 1] == MU_HIERARCHY_DELIMITER)
67     isdir = 1;
68 
69   /* Allocates memory.  */
70   name = namespace_get_name (name, &record, &mode);
71 
72   if (!name)
73     return io_completion_response (command, RESP_NO, "Cannot create mailbox");
74 
75   /* It will fail if the mailbox already exists.  */
76   if (access (name, F_OK) != 0)
77     {
78       if (make_interdir (name, MU_HIERARCHY_DELIMITER, MKDIR_PERMISSIONS))
79 	rc = RESP_NO;
80 
81       if (rc == RESP_OK)
82 	{
83 	  if (isdir)
84 	    {
85 	      if (mkdir (name, MKDIR_PERMISSIONS))
86 		{
87 		  mu_diag_output (MU_DIAG_ERR,
88 				  _("Cannot create directory %s: %s"), name,
89 				  mu_strerror (errno));
90 		  rc = RESP_NO;
91 		}
92 	    }
93 	  else
94 	    {
95 	      mu_mailbox_t mbox;
96 
97 	      rc = mu_mailbox_create_from_record (&mbox, record, name);
98 	      if (rc)
99 		{
100 		  mu_diag_output (MU_DIAG_ERR,
101 				  _("Cannot create mailbox (%s) %s: %s"), name,
102 				  record->scheme,
103 				  mu_strerror (rc));
104 		  rc = RESP_NO;
105 		}
106 	      else if ((rc = mu_mailbox_open (mbox,
107 					      MU_STREAM_RDWR | MU_STREAM_CREAT
108 					      | mode)))
109 		{
110 		  mu_diag_output (MU_DIAG_ERR,
111 				  _("Cannot open mailbox (%s) %s: %s"),
112 				  record->scheme,
113 				  name, mu_strerror (rc));
114 		  rc = RESP_NO;
115 		}
116 	      else
117 		{
118 		  mu_mailbox_close (mbox);
119 		  mu_mailbox_destroy (&mbox);
120 		  rc = RESP_OK;
121 		}
122 	    }
123 	}
124       if (rc != RESP_OK)
125 	msg = "Cannot create mailbox";
126     }
127   else
128     {
129       rc = RESP_NO;
130       msg = "already exists";
131     }
132   free (name);
133   return io_completion_response (command, rc, "%s", msg);
134 }
135