This Patch adds two functions to qmHandle: 1. DelMsgTo() 2. DelMsgFrom() With DelMsgTo() you can delete Message where "To:" have/contains the given Address. With DelMsgFrom() you can delete Message where "From:" have/contains the given Address. e.g: Delete all Messages qmail would deliver to "info@example.com": ./qmHandle -T"info\@example.com" Delete all Messages qmail would come from "info@example.com": ./qmHandle -F"info\@example.com" To apply this patch do the follow: patch -d /path/to/qmHandle-1.2.0/ < /path/to/qmHandle-1.2.0-add-functions-From-To.patch Written by Michael Diekmann 14.09.2005 --- ./1.2.0-orig/qmHandle Wed Dec 10 15:01:17 2003 +++ ./1.2.0-work/qmHandle Wed Sep 14 12:16:04 2005 @@ -11,7 +11,7 @@ use warnings; use diagnostics; -my $version = '1.2.0'; +my $version = '1.2.1'; #################### USER CONFIGURATION BEGIN #################### @@ -74,6 +74,8 @@ $arg =~ /^-m(.+)/ and do { push @actions, "ViewMsg($1)"; last SWITCH; }; $arg =~ /^-d(.+)/ and do { push @actions, "DelMsg($1)"; $dactions++; last SWITCH; }; $arg =~ /^-S(.+)/ and do { push @actions, "DelMsgSubj(\"$1\")"; $dactions++; last SWITCH; }; + $arg =~ /^-T(.+)/ and do { push @actions, "DelMsgTo(\"$1\")"; $dactions++; last SWITCH; }; + $arg =~ /^-F(.+)/ and do { push @actions, "DelMsgFrom(\"$1\")"; $dactions++; last SWITCH; }; $arg eq '-D' and do { push @actions, "DelAll()"; $dactions++; last SWITCH; }; $arg eq '-V' and do { push @actions, "Version()"; last SWITCH; }; Usage(); @@ -197,6 +199,40 @@ return $msgsub; } +# Returns the To: of a message +sub getTo { + my $msg = shift; + my $msgto; + open (MSG, "${queue}mess/$msg") or die("cannot open message $msg"); + while () { + if ( $_ =~ /^To: /) { + $msgto = $'; + chop ($msgto); + } elsif ( $_ eq "\n") { + last; + } + } + close (MSG); + return $msgto; +} + +# Returns the From: of a message +sub getFrom { + my $msg = shift; + my $msgfrom; + open (MSG, "${queue}mess/$msg") or die("cannot open message $msg"); + while () { + if ( $_ =~ /^From: /) { + $msgfrom = $'; + chop ($msgfrom); + } elsif ( $_ eq "\n") { + last; + } + } + close (MSG); + return $msgfrom +} + # ##### MAIN FUNCTIONS ##### @@ -409,6 +445,78 @@ } +sub DelMsgTo { + my $to = shift; + my $msgto; + my $delnum = 0; + + print "Looking for messages with To: $to\n"; + + # Search messages + my ($ok) = 0; + foreach my $msg (@msglist) { + $msgto = getTo($msg); + + if ($msgto and $msgto =~ /$to/) { + $ok = 1; + print "Deleting message: $msg\n"; + unlink "${queue}mess/$msg"; + unlink "${queue}info/$msg"; + if ($type{$msg} eq 'R') { + unlink "${queue}remote/$msg"; + } else { + unlink "${queue}local/$msg"; + } + $delnum++; + } + + } + + # If no messages are found, print a notice + if ($ok == 0) { + print "No messages matching To: \"$to\" found in the queue!\n"; + } else { + print "$delnum messages deleted\n"; + } + +} + +sub DelMsgFrom { + my $from = shift; + my $msgfrom; + my $delnum = 0; + + print "Looking for messages with From: $from\n"; + + # Search messages + my ($ok) = 0; + foreach my $msg (@msglist) { + $msgfrom = getFrom($msg); + + if ($msgfrom and $msgfrom =~ /$from/) { + $ok = 1; + print "Deleting message: $msg\n"; + unlink "${queue}mess/$msg"; + unlink "${queue}info/$msg"; + if ($type{$msg} eq 'R') { + unlink "${queue}remote/$msg"; + } else { + unlink "${queue}local/$msg"; + } + $delnum++; + } + + } + + # If no messages are found, print a notice + if ($ok == 0) { + print "No messages matching From: \"$from\" found in the queue!\n"; + } else { + print "$delnum messages deleted\n"; + } + +} + # Delete all messages in the queue (thanks Kasper Holtze) sub DelAll { @@ -475,6 +583,9 @@ print " -mN : display message number N\n"; print " -dN : delete message number N\n"; print " -Stext : delete all messages that have/contain text as Subject\n"; + print " -Taddress : delete all messages that have/contain address in To:\n"; + print " -Faddress : delete all messages that have/contain address in From:\n"; + print " -D : delete all messages in the queue (local and remote)\n"; print " -V : print program version\n"; print "\n"; @@ -491,4 +602,3 @@ sub Version { print "qmHandle v$version\n"; } -