#!/usr/pkg/bin/perl
# create a new mailbox at the top level
#
require './functions-lib.pl';

use CGI; $q=new CGI; 
iframe_header();

my $err="", $root="", $mbox="";
$cyrus = &cyrus_connect();

#@list=$cyrus->list("user"); $entry=$list[0]; $sep=$entry->[2];

# create the mailbox 
$mbox = name_encode($q->param('mbox'));
$mbox="user".$SEP.$mbox if $q->param('user');

$cyrus->create($mbox,$q->param('defaultpartition')); 
$err=$cyrus->error;

unless ($err)
    {
# set admins ACLs        
    $cyrus->setacl($mbox, $config{admin_user}=>"all");
    $err=$cyrus->error;
# set quota    
    if ($q->param('quota') && !$err) 
		{ $cyrus->setquota($mbox, 'STORAGE', $q->param('quota')); }
    $err=$cyrus->error;
    msglog("create mailbox ".name_decode($mbox)) unless $err;
    if ($config{'sync_mbox'} && !getpwnam($q->param('mbox')))
        {
# if the user doesn't exist, remove his ACLs
        $cyrus->deleteacl($mbox, name_encode($q->param('mbox')));
        $err=$cyrus->error;
        }
    }        

iframe_footer($mbox,$err,1);