Re: command line deletion of files

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



We use a perl scrip that does similar process to delete mass phishing or malware email messages from the mail spool. The script locates the sequence number for the message based on the message ID/filename, and then issues the IMAP delete and expunge commands.

Relevant section of code below (This is called from a subroutine that uses File::Find to locate the files in the file system, but it could be called with a simple list of files.) Advantage over ipurge is that this is not based on date or size but a specific set of files.

Enjoy,
John
------------------------------------------------------------------------------------
use Net::IMAP::Simple;
use Cyrus::IMAP::Admin;

sub imapdeletemessage {
    ( my $path ) = @_;
    ## Extract out foldername and filename (UID) from path
    $path =~ /\/var\/spool\/imap\/\d\/(.*)\/(\d+.)/;
    my $foldername = $1;
    my $messageuid = $2;
return "Error: Unable to identify folder or message for $path" if ( ! defined($foldername) || ! defined ($messageuid) );
    $foldername =~ s/\//\./g;
    $messageuid =~ s/\.$//g;
    ## Give ourselves permissions
    if ( my $err = &setimapacl($foldername, 'rte')) {
       return "$err";
    }

    # Create the IMAP object
    my $imap = Net::IMAP::Simple->new('localhost') ||
       die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n";

    # Log on
    if(!$imap->login($CYRUSADMIN,$CYRUSADMINPW)){
        print STDERR "Login failed: " . $imap->errstr . "\n";
        exit(64);
    }

    ## Select folder and get number of messages
    my $nm = $imap->select($foldername);
    if ( $imap->waserr || ! defined($nm)  ) {
       &setimapacl($foldername, 'none');
       return "ERROR: unable to select $foldername: " . $imap->errstr;
    }

    ### Now get Sequence Number for Message ID
    my @seq = $imap->seq($messageuid);
    if ( ! defined($seq[0])) {
       &setimapacl($foldername, 'none');
return "ERROR: No sequence number for UID $messageuid $foldername, probably deleted\n";
    }
    # print "seq for uid $messageuid is $seq[0]\n";

    ## Delete the message
    if ( ! $imap->delete( $seq[0] )) {
       &setimapacl($foldername, 'none');
return "ERROR: Unable to delete $messageuid from $foldername: " . $imap->errstr;
    }

    ## Expunge the mailbox to be safe
    my $expunged = $imap->expunge_mailbox( $foldername );
    if  ( $imap->waserr ) {
        &setimapacl($foldername, 'none');
        return "ERROR: unabled to expunge $foldername: " . $imap->errstr;
    }
    $imap->quit;
    print "IMAP Deleted: UID:$messageuid from folder:$foldername\n";
print LOG "IMAP Deleted: UID:$messageuid from folder:$foldername\n" if ( $logfile);

    ## Take away our permissions
    if ( my $err = &setimapacl($foldername, 'none')) {
       return "$err";
    }
    return 0;
};


On 09/29/2016 11:27 AM, Shawn Bakhtiar via Info-cyrus wrote:
Good morning,

trying to get rid of some emails that have large attachments (i.e. videos sent over email, or cd images, etc...)

Would it be proper to

rm -rf /var/spool/imap/u/username/mailbox/4321.

then

reconstruct -rf user.username

Or is there a more "proper" way using cyrus?


----
Cyrus Home Page: http://www.cyrusimap.org/
List Archives/Info: http://lists.andrew.cmu.edu/pipermail/info-cyrus/
To Unsubscribe:
https://lists.andrew.cmu.edu/mailman/listinfo/info-cyrus



[Index of Archives]     [Cyrus SASL]     [Squirrel Mail]     [Asterisk PBX]     [Video For Linux]     [Photo]     [Yosemite News]     [gtk]     [KDE]     [Gimp on Windows]     [Steve's Art]

  Powered by Linux