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

require './sendmail-lib.pl';
require './boxes-lib.pl';
&ReadParse();
&can_read_mail($in{'user'}) || &error($text{'delete_ecannot'});
@delete = sort { $a <=> $b } split(/\0/, $in{'d'});

if ($in{'mark'}) {
	# Marking emails with some status
	@delete || &error($text{'delete_emnone'});
	@mail = &list_mails($in{'user'}, $delete[0], $delete[@delete-1]);
	dbmopen(%read, "$module_config_directory/$in{'user'}.read", 0600);
	foreach $d (@delete) {
		local $hid = $mail[$d]->{'header'}->{'message-id'};
		if ($in{'mode'}) {
			$read{$hid} = $in{'mode'};
			}
		else {
			delete($read{$hid});
			}
		}
	dbmclose(%read);
	$s = int((@mail - $delete[0] - 1) / $config{'perpage'}) *
	     $config{'perpage'};
	&redirect("list_mail.cgi?user=$in{'user'}&start=$s");
	}
elsif ($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;
	&lock_file(&user_mail_file($in{'user'}));
	@mail = &list_mails($in{'user'}, $delete[0], $delete[@delete-1]);
	foreach $d (@delete) {
		push(@delmail, $mail[$d]);
		}
	&delete_mail($in{'user'}, @delmail);
	&unlock_file(&user_mail_file($in{'user'}));
	&webmin_log("delmail", undef, undef, \%in);
	&redirect("list_mail.cgi?user=$in{'user'}");
	}

