#!/usr/pkg/bin/perl
# mailbox managing
#
require './functions-lib.pl';

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

my $err="", $root="", $reload=0;
my $mbox=$q->param('mbox');
$mbox="user" if $q->param('user');
$cyrus = &cyrus_connect();

if ($mbox)
	{
	check_mailbox($mbox);
#	$entry=$list[0]; ($mbox, $attributes, $sep) = ($entry->[0],$entry->[1],$entry->[2]);
	($name, $supmbox, $partition) = mbox_info($mbox,1);
    $cyrus->setacl($mbox, $config{admin_user}=>"all");
	}

# create new mailbox or subfolder
if ($q->param('create'))
	{ 
    ($mbox, $domain) = split /@/, $mbox;
	$mbox = $mbox.$SEP.name_encode($q->param('folder'));
	$mbox .= '@'.$domain if $domain;
	$cyrus->create($mbox,$q->param('defaultpartition')); 
	$err=$cyrus->error."<BR>\n";
	if ($q->param('quota') && !$err) 
		{ $cyrus->setquota($mbox, 'STORAGE', $q->param('quota')); }
	$err=$cyrus->error;
	msglog("create mailbox ".name_decode($mbox)) unless $err;
	$reload=1;
	}
	
# delete mailbox 
if ($q->param('delete'))
	{ 
	$cyrus->delete($mbox); 
	$err=$cyrus->error;
	if (!$err)
		{
		msglog("delete mailbox ".name_decode($mbox));
        ($mbox, $domain) = split /@/, $mbox;
		$mbox=$supmbox;
	    $mbox .= '@'.$domain if $domain;		
		}
	$reload=1;
	}

# rebuild mailbox 
if ($q->param('rebuild'))
	{
    %acls=$cyrus->listacl($mbox); 
    $cyrus->setacl($mbox, $config{admin_user}=>"all") unless $acls{$config{admin_user}} eq "lrswipcda";    
	$rebuild=$config{lib_path}."/reconstruct";
	if (-f $rebuild)
		{
        system "su -c '$rebuild \"$mbox\"' cyrus"; 
        print "<br>";
        msglog("reconstruct mailbox ".name_decode($mbox));
        }
   	else 
        {
        print "<BR><BR>$rebuild ", $text{not_found}, "<BR>", $text{go_to_config};
        exit;
        }
	}
	
# update quota and/or partition
if ($q->param('update'))
	{ 
    @list=$cyrus->list($mbox.$SEP."*"); 
    unshift @list, $cyrus->list("$mbox"); 
	($quota, $root, $used)=getquota($mbox);
	if ($q->param('quota') != $quota*1024)
		{
		if ($q->param('quota')>0)
#	modify the quota in the mailbox and all subfolders
			{
            foreach (@list)
                {			    
			    $cyrus->setquota($_->[0], 'STORAGE', $q->param('quota'));
			    $err=$cyrus->error;
			    msglog("set quota ".$q->param('quota')." on ".name_decode($_->[0])) unless $err;
			    }
			}
		else
#	remove the quota in mailbox and all subfolders
			{
			foreach (@list)    
			    {
			    my $lmbox=$_->[0];
			    if (index($lmbox,"user")==0) { $pref=substr($lmbox,5,1); }
			    else { $pref=substr($lmbox,0,1); }
			    $quotapath=$lmbox; $quotapath =~ s/\//\./gi;
			    $quotapath=$config{imap_path}."/quota/$pref/".$quotapath;
			    $quotacmd=$config{lib_path}."/quota";
#			die $quotapath; 
			    if (-f $quotapath)
				    {
				    if (-f $quotacmd)
					    {
					    unlink $quotapath;
					    system "su -c '$quotacmd \"$lmbox\"' cyrus";
					    print "<br>";
					    msglog("remove quota from ".name_decode($lmbox));
					    }
        			    else
					    {
					    print "<BR><BR>$quotacmd ", $text{not_found}, "<BR>", $text{go_to_config};
					    exit;
					    }
				    }
                }
			}
		}
	get_partitions() unless %imapPartitions;
	if ($imapPartitions{$q->param('defaultpartition')} ne $partition)
		{
#	move the mailbox to another partition
		$cyrus->rename($mbox,$mbox,$q->param('defaultpartition'));
		$err=$cyrus->error;
		msglog("move ".name_decode($mbox)." to ".$q->param('defaultpartition')." partition") unless $err;
		}
	}

iframe_footer($mbox, $err, $reload);