summaryrefslogtreecommitdiff
path: root/regress/tinymta/Drop.pm
blob: 9135079dd5ff9560384caa23b81dae1a4e0eb556 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#-*- perl -*-
#
#  Copyright (C) 2006 Ken'ichi Fukamachi
#   All rights reserved. This program is free software; you can
#   redistribute it and/or modify it under the same terms as Perl itself.
#
# $FML: Drop.pm,v 1.2 2006/06/12 22:53:06 fukachan Exp $
#

package TinyMTA::Drop;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD $global_counter);
use Carp;

=head1 NAME

TinyMTA::Drop - mail drop wrapper

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 METHODS

=head2 new()

constructor.

=cut


# Descriptions: constructor.
#    Arguments: OBJ($self) OBJ($config)
# Side Effects: 
# Return Value: none
sub new
{
    my ($self, $config) = @_;
    my ($type) = ref($self) || $self;
    my $me     = { _config => $config };
    return bless $me, $type;
}


# Descriptions: main routine.
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: none
sub run
{
    my ($self) = @_;
    my $config = $self->{ _config };

    # 1. prepare queue directory. 
    my $queue_dir = $config->{ queue_dir };
    unless (-d $queue_dir) {
	mkdir $queue_dir, 0730;
	if (-d $queue_dir) {
	    $self->log("$queue_dir created");
	}
	else {
	    $self->logerror("cannot mkdir $queue_dir");
	}
    }

    # 2. drop the message taken from STDIN into a new queue file.
    my ($qid, $qf, $qtmp) = $self->queue_filename();
    my $wh = new FileHandle "> $qtmp";
    if (defined $wh) {
	my $buf;

	$wh->autoflush(1);
      LINE:
	while (sysread(STDIN, $buf, 8192)) {
	    syswrite($wh, $buf, 8192);
	}
	$wh->close();
    }

    if (-s $qtmp) {
	if (rename($qtmp, $qf)) {
	    $self->log("queue-in: $qid");
	}
	else {
	    $self->logerror("cannot create $qf");
	    croak("cannot create $qf\n");
	}
    }
    else {
	$self->logerror("0 byte tmp file: $qtmp");
    }
}


# Descriptions: retrun a new queue file name.
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: ARRAY(STR, STR, STR)
sub queue_filename
{
    my ($self)    = @_;
    my $config    = $self->{ _config };
    my $queue_dir = $config->{ queue_dir };

    use FileHandle;
    my $qid = sprintf("%d.%d.%d", time, $$, ++$global_counter);
    my $new = File::Spec->catfile($queue_dir, $qid);
    my $tmp = File::Spec->catfile($queue_dir, ",$qid");

    return($qid, $new, $tmp);
}


# Descriptions: log as normal level.
#    Arguments: OBJ($self) STR($msg)
# Side Effects: none
# Return Value: none
sub log
{
    my ($self, $msg) = @_;
    &TinyMTA::Log::log($msg);
}


# Descriptions: log as error level.
#    Arguments: OBJ($self) STR($msg)
# Side Effects: none
# Return Value: none
sub logerror
{
    my ($self, $msg) = @_;
    &TinyMTA::Log::log("error: $msg");
}


######################################################################
#
# dispatcher
#

# Descriptions: main dispatcher.
#    Arguments: OBJ($main_cf) STR($config_cf_file)
# Side Effects: none
# Return Value: none
sub main::dispatch
{
    my ($main_cf, $config_cf_file) = @_;

    my $config = TinyMTA::Config::load_file($config_cf_file, $main_cf);
    my $obj    = new TinyMTA::Drop $config;
    $obj->run();
}


=head1 CODING STYLE

See C<http://www.fml.org/software/FNF/> on fml coding style guide.

=head1 AUTHOR

Ken'ichi Fukamachi

=head1 COPYRIGHT

Copyright (C) 2006 Ken'ichi Fukamachi

All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.

=head1 HISTORY

TinyMTA::Drop appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.

=cut


1;