return to first page linux journal archive
keywordscontents

Listing 1. Perl Script, qmail_db_lookup

#!/usr/local/bin/perl
	
   if($db_name eq ""){
   	print "Usage: $0 <database>\n";
   	exit(1);
   }
   # Trim everything except the actual recipient.
   split(/-/, $ENV{"LOCAL"});
   $recipient = @_[scalar(@_)-1];
   
   # Scan the database looking for a match. 
   $result_address = "";
   $default_address = "";
   open(DB, $ARGV[0]) ||
	die "cannot open database";
   while(<DB>){
   	chop();
   
   	# Trim out any comments.
   	$index = index($_, "#");
   	$_ = substr($_, 0, $index) if $index >=0;
   	
   	# Get the local name and the forwarding
	# address.
   	($db_local_address, $db_forward_to) =\
		m|^\s*(\S*)\s*:\s*(\S.*)$|;
   	if($db_local_address eq "-"){
   		$default_address = $db_forward_to;
   	} elsif($db_local_address eq $recipient){
   		$found = "yes";
   		$result_address = $db_forward_to;
   		last;
   	}
   }
   close(DB);
   
   # If we couldn't find the recipient, forward to
   # the default user.
   $result_address = $default_address if($result_address eq "");
   
   # If we were able to resolve an address, then
   # call qmail's 'forward'.
   if($result_address ne ""){
   	system(
   "/var/qmail/bin/forward $result_address");
   	exit(0);
   }else{
   	exit(100);
   }