6 $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
12 use lib $ENV{"PERLMAIL_BASEDIR"} || File::Basename::dirname($0);
18 use Carp qw(cluck confess);
19 require IO::Socket::INET;
25 open DBI_PWD,$DBI_pwd or die "open \"$DBI_pwd\": $!";
27 close DBI_PWD or warn "close DBI_pwd: $!";
30 my $DBI=DBI->connect_cached("DBI:mysql:database=$DBI_database;host=","$DBI_user",$DBI_pwd,{
31 "PrintError"=>0, # handled by "RaiseError" below
33 "ShowErrorStatement"=>1,
35 }) or confess "Failed DBI->connect(): $!";
42 eval { $DBI->do("drop table $name"); };
43 $DBI->do("create table $name (".join(",",@$cols).")");
48 create_table($DB_table,[
49 "id int not null auto_increment primary key",
50 "time timestamp not null", # assume ." default now()"
51 "message longtext not null",
52 "retries int null default 0", # null=>done, 0=not yet tried to submit
55 $DBI->do("alter table $DB_table add index (retries,id)");
67 close STDIN or cluck "close STDIN: $!";
70 # assume "retries"=>0,
72 my $prep=$DBI->prepare_cached("insert into $DB_table (".join(",",keys(%row)).")"
73 ." values (".join(",",map("?",keys(%row))).")");
74 $prep->execute(values(%row));
75 print $prep->{"mysql_insertid"}."\n";
81 confess if !defined $pid;
82 exit 0 if $pid; # parent
91 open LOCK,">>$Lock_pathname" or die "open-append \"$Lock_pathname\": $!";
92 if (!flock LOCK,LOCK_EX|LOCK_NB) {
93 # NEVER unlink here, we are not the lock owning process!
97 my $sth=$DBI->prepare("select id,message from $DB_table where retries is not null"
98 # process only non-problematic mails during rerun
99 .($submitonce_run==1 ? "" : " and retries=0")
100 ." order by retries asc,id asc");
105 while (my $row=$sth->fetchrow_hashref()) {
106 $DBI->do("update $DB_table set retries=retries+1 where id=".$row->{"id"});
108 $sock=IO::Socket::INET->new(
109 "PeerAddr"=>$PeerAddr,
111 ) or confess "IO::Socket::INET->new(\"$PeerAddr\"): $!";
112 $sock->connected() or confess "socket not connected";
114 $sock->printflush(length($row->{"message"})."\n".$row->{"message"});
115 alarm $Socket_timeout and $sock->timeout($Socket_timeout) if $Socket_timeout;
117 my $gotlen=$sock->sysread($got,1);
118 confess $row->{"id"}.": sysread(1)=".(!defined $gotlen ? "undef" : $gotlen).": $!"
119 if !defined($gotlen) || $gotlen!=1;
122 # Prevent mailing errors from cron invoking us etc.
123 #print STDERR "FAIL:".$row->{"id"}."\n";
127 $DBI->do("update $DB_table set retries=null where id=".$row->{"id"});
129 print $progresschar.$row->{"id"}.($got eq "1" ? "" : "=FAIL");
133 $sock->shutdown(0); # stopped reading
134 $sock->printflush("BYE\n");
135 $sock->shutdown(2); # stopped using
138 print "\n" if $progresschar;
139 unlink $Lock_pathname;
141 return $progresschar;
146 1 while submitonce();
151 my $sth=$DBI->prepare("select message from $DB_table where retries is not null order by id");
153 while (my $row=$sth->fetchrow_hashref()) {
154 print $row->{"message"},"\n";
160 my($keyword,$interval)=@_;
162 # FIXME: SQL "now()" is raced against the block above
163 my $sth=$DBI->prepare("select id,time,retries from $DB_table where time>now()");
165 while (my $row=$sth->fetchrow_hashref()) {
166 warn "Message time in future: ".join(",",map(
167 "$_=".(!defined $row->{$_} ? "NULL" : $row->{$_})
171 return if $interval eq "";
173 my $print=s/^print://;
174 s/(\d+)y/($1*12)."m"/ge;
175 s/(\d+)m/($1*30)."d"/ge;
176 s/(\d+)d/($1*24)."h"/ge;
177 s/(\d+)h/($1*60)."M"/ge;
178 s/(\d+)M/($1*60)."s"/ge;
180 $sec+=$1 while s/(\d+)s//g;
181 die "Interval parse error; left \"$_\", parsed: $interval" if $_ ne "";
182 $sth=$DBI->prepare(($print ? "select id" : "delete")
183 ." from $DB_table where retries is null and time<from_unixtime(unix_timestamp()-$sec)");
186 print $sth->rows()."\n";
189 while (my $row=$sth->fetchrow_hashref()) {
190 print $row->{"id"},"\n";
196 $Getopt::Long::ignorecase=0;
204 "V|version",sub { print "perlmail-submit: $VERSION\n"; exit 0; },