⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qtool.pl

📁 < linux网络编程工具>>配套源码
💻 PL
📖 第 1 页 / 共 2 页
字号:
#!/usr/bin/env perl
##
## Copyright (c) 1998-2000 Sendmail, Inc. and its suppliers.
##       All rights reserved.
##
## $Id: qtool.pl,v 8.15.16.2 2000/09/17 17:04:22 gshapiro Exp $
##
use strict;
use File::Basename;
use File::Copy;
use File::Spec;
use Fcntl qw(:flock :DEFAULT);
use Getopt::Std;

##
## QTOOL
##	This program is for moving files between sendmail queues. It is
## pretty similar to just moving the files manually, but it locks the files
## the same way sendmail does to prevent problems. 
##
## 	The syntax is the reverse of mv (ie. the target argument comes
## first). This lets you pick the files you want to move using find and
## xargs.
##
## 	Since you cannot delete queues while sendmail is running, QTOOL
## assumes that when you specify a directory as a source, you mean that you
## want all of the queue files within that directory moved, not the 
## directory itself.
##
##	There is a mechanism for adding conditionals for moving the files.
## Just create an Object with a check_move(source, dest) method and add it 
## to the $conditions object. See the handling of the '-s' option for an
## example.
##

##
## OPTION NOTES
##
## The -e option:
##	The -e option takes any valid perl expression and evaluates it
##	using the eval() function. Inside the expression the variable 
##	'$msg' is bound to the ControlFile object for the current source
##	queue message. This lets you check for any value in the message
##	headers or the control file. Here's an example:
##
##      ./qtool.pl -e '$msg->{num_delivery_attempts} >= 2' /q1 /q2
##
##	This would move any queue files whose number of delivery attempts
##	is greater than or equal to 2 from the queue 'q2' to the queue 'q1'.
##
##	See the function ControlFile::parse for a list of available
##	variables.
##

my %opts;
my %sources;
my $dst_name;
my $destination;
my $source_name;
my $source;
my $result;
my $action;
my $new_condition;
my $conditions = new Compound();

Getopt::Std::getopts('bde:s:', \%opts);

sub move_action
{
	my $source = shift;
	my $destination = shift;

	$result = $destination->add($source);
	if ($result)
	{
		print("$result.\n");
	}
}

sub delete_action
{
	my $source = shift;

	return $source->delete();
}

sub bounce_action
{
	my $source = shift;

	return $source->bounce();
}

$action = \&move_action;
if (defined $opts{d})
{
	$action = \&delete_action;
}
elsif (defined $opts{b})
{
	$action = \&bounce_action;
}

if (defined $opts{s})
{
	$new_condition = new OlderThan($opts{s});
	$conditions->add($new_condition);
}

if (defined $opts{e})
{
	$new_condition = new Eval($opts{e});
	$conditions->add($new_condition);
}

if ($action == \&move_action)
{
	$dst_name = shift(@ARGV);
	if (!-d $dst_name)
	{
		print("The destination '$dst_name' must be an existing " .
		      "directory.\n");
		usage();
		exit;
	}
	$destination = new Queue($dst_name);
}

while (@ARGV)
{
	$source_name = shift(@ARGV);
	$result = add_source(\%sources, $source_name);
	if ($result)
	{
		print("$result.\n");
	}
}

if (keys(%sources) == 0)
{
	print("You must at least specify at least one source.\n");
	usage();
	exit;
}

while (($source_name, $source) = each(%sources))
{
	$result = $conditions->check_move($source, $destination);
	if ($result)
	{
		$result = &{$action}($source, $destination);
		if ($result)
		{
			print("$result\n");
		}
	}
}

sub usage
{
	print("Usage: $0 [options] directory source ...\n");
	print("       $0 [-d|-b] source ...\n");
	print("options:\n");
	print("    -b                   Bounce the messages specified by source.\n");
	print("    -d                   Delete the messages specified by source.\n");
	print("    -e [perl expression] Move only messages for which perl expression returns true.\n");
	print("    -s [seconds]         Move only messages older than seconds.\n");
}

##
## ADD_SOURCE -- Adds a source to the source hash.
##
##	Determines whether source is a file, directory, or id. Then it 
##	creates a QueuedMessage or Queue for that source and adds it to the
##	list.
##
##	Parameters:
##		sources -- A hash that contains all of the sources.
##		source_name -- The name of the source to add
##
##	Returns:
##		error_string -- Undef if ok. Error string otherwise.
##
##	Notes:
##		If a new source comes in with the same ID as a previous 
##		source, the previous source gets overwritten in the sources
##		hash. This lets the user specify things like * and it still
##		works nicely.
##

sub add_source
{
	my $sources = shift;
	my $source_name = shift;
	my $source_base_name;
	my $source_dir_name;
	my $data_dir_name;
	my $source_id;
	my $source_prefix;
	my $queued_message;
	my $queue;
	my $result;

	($source_base_name, $source_dir_name) = File::Basename::fileparse($source_name);
	$data_dir_name = $source_dir_name;

	$source_prefix = substr($source_base_name, 0, 2);
	if (!-d $source_name && $source_prefix ne 'qf' && 
	    $source_prefix ne 'df')
	{
		$source_base_name = "qf$source_base_name";
		$source_name = File::Spec->catfile("$source_dir_name", 
						   "$source_base_name");
	}
	$source_id = substr($source_base_name, 2);

	if (!-e $source_name)
	{
		$source_name = File::Spec->catfile("$source_dir_name", "qf",
						   "qf$source_id");
		if (!-e $source_name)
		{
			return "'$source_name' does not exist";
		}
		$data_dir_name = File::Spec->catfile("$source_dir_name", "df");
		$source_dir_name = File::Spec->catfile("$source_dir_name", 
						       "qf");
	}

	if (-f $source_name)
	{
		$queued_message = new QueuedMessage($source_dir_name, 
						    $source_id,
						    $data_dir_name);
		$sources->{$source_id} = $queued_message;
		return undef;
	}

	if (!-d $source_name)
	{
		return "'$source_name' is not a plain file or a directory";
	}

	$queue = new Queue($source_name);
	$result = $queue->read();
	if ($result)
	{
		return $result;
	}

	while (($source_id, $queued_message) = each(%{$queue->{files}}))
	{
		$sources->{$source_id} = $queued_message;
	}

	return undef;
}

##
## LOCK_FILE -- Opens and then locks a file.
##
## 	Opens a file for read/write and uses flock to obtain a lock on the
##	file. The flock is Perl's flock which defaults to flock on systems
##	that support it. On systems without flock it falls back to fcntl
##	locking.
##
##	Parameters:
##		file_name -- The name of the file to open and lock.
##
##	Returns:
##		(file_handle, error_string) -- If everything works then
##			file_handle is a reference to a file handle and
##			error_string is undef. If there is a problem then 
##			file_handle is undef and error_string is a string
##			explaining the problem.
##

sub lock_file
{
	my $file_name = shift;
	my $result;

	$result = sysopen(FILE_TO_LOCK, $file_name, Fcntl::O_RDWR);
	if (!$result)
	{
		return (undef, "Unable to open '$file_name': $!");
	}

	$result = flock(FILE_TO_LOCK, Fcntl::LOCK_EX | Fcntl::LOCK_NB);
	if (!$result)
	{
		return (undef, "Could not obtain lock on '$file_name': $!");
	}

	return (\*FILE_TO_LOCK, undef);
}

##
## UNLOCK_FILE -- Unlocks a file.
##
## 	Unlocks a file using Perl's flock.
##
##	Parameters:
##		file -- A file handle.
##
##	Returns:
##		error_string -- If undef then no problem. Otherwise it is a 
##			string that explains problem.
##

sub unlock_file
{
	my $file = shift;
	my $result;

	$result = flock($file, Fcntl::LOCK_UN);
	if (!$result)
	{
		return "Unlock failed on '$result': $!";
	}

	return undef;
}

##
## MOVE_FILE -- Moves a file.
##
##	Moves a file.
##
##	Parameters:
##		src_name -- The name of the file to be move.
##		dst_nome -- The name of the place to move it to.
##
##	Returns:
##		error_string -- If undef then no problem. Otherwise it is a 
##			string that explains problem.
##

sub move_file
{
	my $src_name = shift;
	my $dst_name = shift;
	my $result;

	$result = File::Copy::move($src_name, $dst_name);
	if (!$result)
	{
		return "File move from '$src_name' to '$dst_name' failed: $!";
	}

	return undef;
}


##
## CONTROL_FILE - Represents a sendmail queue control file.
##
##	This object represents represents a sendmail queue control file.
##	It can parse and lock its file.
##


package ControlFile;

sub new
{
	my $this = shift;
	my $class = ref($this) || $this;
	my $self = {};
	bless $self, $class;
	$self->initialize(@_);
	return $self;
}

sub initialize
{
	my $self = shift;
	my $queue_dir = shift;
	$self->{id} = shift;

	$self->{file_name} = $queue_dir . '/qf' . $self->{id};
	$self->{headers} = {};
}

##
## PARSE - Parses the control file.
##
##	Parses the control file. It just sticks each entry into a hash.
##	If a key has more than one entry, then it points to a list of
##	entries.
##

sub parse
{
	my $self = shift;
	if ($self->{parsed})
	{
		return;
	}
	my %parse_table = 
	(
		'A' => 'auth',
		'B' => 'body_type',
		'C' => 'controlling_user',
		'D' => 'data_file_name',
		'E' => 'error_recipient',
		'F' => 'flags',
		'H' => 'parse_header',
		'I' => 'inode_number',
		'K' => 'next_delivery_time',
		'L' => 'content-length',
		'M' => 'message',
		'N' => 'num_delivery_attempts',
		'P' => 'priority',
		'Q' => 'original_recipient',
		'R' => 'recipient',
		'S' => 'sender',
		'T' => 'creation_time',
		'V' => 'version',
		'X' => 'charset',
		'Z' => 'envid',
		'$' => 'macro'
	);
	my $line;
	my $line_type;
	my $line_value;
	my $member_name;
	my $member;
	my $last_type;

	open(CONTROL_FILE, "$self->{file_name}");
	while ($line = <CONTROL_FILE>)
	{
		$line_type = substr($line, 0, 1);
		if ($line_type eq "\t" && $last_type eq 'H')
		{
			$line_type = 'H';
			$line_value = $line;
		}
		else
		{
			$line_value = substr($line, 1);
		}
		$member_name = $parse_table{$line_type};
		$last_type = $line_type;
		if (!$member_name)
		{
			$member_name = 'unknown';
		}
		if ($self->can($member_name))
		{
			$self->$member_name($line_value);
		}
		$member = $self->{$member_name};
		if (!$member)
		{
			$self->{$member_name} = $line_value;
			next;
		}
		if (ref($member) eq 'ARRAY')
		{
			push(@{$member}, $line_value);
			next;
		}
		$self->{$member_name} = [$member, $line_value];
	}
	close(CONTROL_FILE);

	$self->{parsed} = 1;
}

sub parse_header
{
	my $self = shift;
	my $line = shift;
	my $headers = $self->{headers};
	my $last_header = $self->{last_header};
	my $header_name;
	my $header_value;
	my $first_char;

	$first_char = substr($line, 0, 1);
	if ($first_char eq "?")
	{
		$line = substr($line, 3);
	}
	elsif ($first_char eq "\t")
	{
	 	if (ref($headers->{$last_header}) eq 'ARRAY')
		{
			$headers->{$last_header}[-1] = 
				$headers->{$last_header}[-1] .  $line;
		}
		else
		{
			$headers->{$last_header} = $headers->{$last_header} . 
						   $line;
		}
		return;
	}
	($header_name, $header_value) = split(/:/, $line, 2);
	$self->{last_header} = $header_name;
	if (exists $headers->{$header_name})
	{
		$headers->{$header_name} = [$headers->{$header_name}, 
					    $header_value];
	}
	else
	{
		$headers->{$header_name} = $header_value;
	}
}

sub is_locked
{
	my $self = shift;

	return (defined $self->{lock_handle});
}

sub lock
{
	my $self = shift;
	my $lock_handle;
	my $result;

	if ($self->is_locked())
	{
		# Already locked
		return undef;
	}

	($lock_handle, $result) = ::lock_file($self->{file_name});
	if (!$lock_handle)
	{
		return $result;
	}

	$self->{lock_handle} = $lock_handle;

	return undef;
}

sub unlock
{
	my $self = shift;
	my $result;

	if (!$self->is_locked())
	{
		# Not locked
		return undef;
	}

	$result = ::unlock_file($self->{lock_handle});

	$self->{lock_handle} = undef;

	return $result;
}

sub do_stat
{
	my $self = shift;
	my $result;
	my @result;

	$result = open(QUEUE_FILE, $self->{file_name});
	if (!$result)
	{
		return "Unable to open '$self->{file_name}': $!";
	}
	@result = stat(QUEUE_FILE);
	if (!@result)
	{
		return "Unable to stat '$self->{file_name}': $!";
	}
	$self->{control_size} = $result[7];
	$self->{control_last_mod_time} = $result[9];
}

sub DESTROY
{
	my $self = shift;

	$self->unlock();
}

sub delete
{
	my $self = shift;
	my $result;

	$result = unlink($self->{file_name});
	if (!$result)
	{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -