#!/usr/local/bin/perl
# delete_mail.cgi
# Delete multiple messages

require './qmail-lib.pl';
require './boxes-lib.pl';
&ReadParse();
@delete = sort { $a <=> $b } split(/\0/, $in{'d'});

if ($in{'forward'}) {
	# Forwarding selected mails .. redirect
	&redirect("reply_mail.cgi?user=$in{'user'}&".
		  join("&", map { "mailforward=$_" } @delete));
	}
elsif ($in{'new'}) {
	# Need to redirect to compose form
	&redirect("reply_mail.cgi?user=$in{'user'}&new=1");
	}
else {
	# Just deleting emails
	@delete || &error($text{'delete_enone'});
	$gconfig{'logfiles'} = 0;
	$umf = &user_mail_dir($in{'user'});
	&lock_file($umf);
	@mail = $config{'mail_system'} == 1 ?
		&list_maildir($umf, $delete[0], $delete[@delete-1]) :
		&list_mails($umf, $delete[0], $delete[@delete-1]);
	foreach $d (@delete) {
		push(@delmail, $mail[$d]);
		}
	if ($config{'mail_system'} == 1) {
		&delete_maildir($umf, @delmail);
		}
	else {
		&delete_mail($umf, @delmail);
		}
	&unlock_file($umf);
	&webmin_log("delmail", undef, undef, \%in);
	&redirect("list_mail.cgi?user=$in{'user'}");
	}

