Blame view

imap4d/create.c 3.11 KB
Jakob Kaivo authored
1
/* GNU mailutils - a suite of utilities for electronic mail
2
   Copyright (C) 1999, 2001 Free Software Foundation, Inc.
Jakob Kaivo authored
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

   This program 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 2, or (at your option)
   any later version.

   This program 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 this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include "imap4d.h"
19
#include <unistd.h>
Jakob Kaivo authored
20 21

/*
22
 * Must create a new mailbox
Jakob Kaivo authored
23 24
 */

25 26 27 28
/* FIXME: How do we do this ??????:
   IF a new mailbox is created with the same name as a mailbox which was
   deleted, its unique identifiers MUST be greater than any unique identifiers
   used in the previous incarnation of the mailbox.  */
Jakob Kaivo authored
29
int
30
imap4d_create (struct imap4d_command *command, char *arg)
Jakob Kaivo authored
31
{
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  char *name;
  char *sp = NULL;
  const char *delim = "/";
  int rc = RESP_OK;
  const char *msg = "Completed";

  name = util_getword (arg, &sp);
  if (!name)
    return util_finish (command, RESP_BAD, "Too few arguments");

  util_unquote (&name);

  if (*name == '\0')
    return util_finish (command, RESP_BAD, "Too few arguments");

  /* Creating, "Inbox" should always fail.  */
  if (strcasecmp (name, "INBOX") == 0)
    return util_finish (command, RESP_BAD, "Already exist");

  /* Allocates memory.  */
Sergey Poznyakoff authored
52 53 54 55
  name = namespace_getfullpath (name, delim);

  if (!name)
    return util_finish (command, RESP_NO, "Can not create mailbox");
56

57
  /* It will fail if the mailbox already exists.  */
58 59 60 61
  if (access (name, F_OK) != 0)
    {
      char *dir;
      char *d = name + strlen (delim); /* Pass the root delimeter.  */
62 63 64 65

      /*If the server's hierarchy separtor character appears elsewhere in
	name, the server SHOULD create any superior hierarchcal names
	that are needed for the CREATE command to complete successfully.  */
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
      if (chdir (delim) == 0) /* start on the root.  */
	for (; (dir = strchr (d, delim[0])); d = dir)
	  {
	    *dir++ = '\0';
	    if (chdir (d) != 0)
	      {
		if (mkdir (d, 0700) == 0)
		  {
		    if (chdir (d) == 0)
		      {
			continue;
		      }
		    else
		      {
			rc = RESP_NO;
			msg = "Can not create mailbox";
			break;
		      }
		  }
	      }
	  }
87 88 89 90 91
      /* If the mailbox name is suffixed with the server's hierarchy
	 separator character, this is a declaration that the client intends
	 to create mailbox names under this name in the hierarchy.

	 In other words is d == '\0' it is not an error.   */
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
      if (rc == RESP_OK && d && *d != '\0')
	{
	  int fd = creat (d, 0600);
	  if (fd != -1)
	    close (fd);
	  else
	    {
	      rc = RESP_NO;
	      msg = "Can not create mailbox";
	    }
	}
    }
  else
    {
      rc = RESP_NO;
      msg = "already exists";
    }
  chdir (homedir);
  free (name);
  return util_finish (command, rc, msg);
Jakob Kaivo authored
112
}