4 # Copyright (C) 2002-2003 Jan Kratochvil <project-PerlMail@jankratochvil.net>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 use vars qw($VERSION);
22 $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
28 use lib $ENV{"PERLMAIL_BASEDIR"} || File::Basename::dirname($0);
34 use Carp qw(cluck confess);
35 require IO::Socket::INET;
41 open DBI_PWD,$DBI_pwd or die "open \"$DBI_pwd\": $!";
43 close DBI_PWD or warn "close DBI_pwd: $!";
46 my $DBI=DBI->connect_cached("DBI:mysql:database=$DBI_database;host=","$DBI_user",$DBI_pwd,{
47 "PrintError"=>0, # handled by "RaiseError" below
49 "ShowErrorStatement"=>1,
51 }) or confess "Failed DBI->connect(): $!";
58 eval { $DBI->do("drop table $name"); };
59 $DBI->do("create table $name (".join(",",@$cols).")");
64 create_table($DB_table,[
65 "id int not null auto_increment primary key",
66 "time timestamp not null", # assume ." default now()"
67 "message longtext not null",
68 "retries int null default 0", # null=>done, 0=not yet tried to submit
71 $DBI->do("alter table $DB_table add index (retries,id)");
83 close STDIN or cluck "close STDIN: $!";
86 # assume "retries"=>0,
88 my $prep=$DBI->prepare_cached("insert into $DB_table (".join(",",keys(%row)).")"
89 ." values (".join(",",map("?",keys(%row))).")");
90 $prep->execute(values(%row));
91 print $prep->{"mysql_insertid"}."\n";
97 confess if !defined $pid;
98 exit 0 if $pid; # parent
102 my $submitonce_run=0;
107 open LOCK,">>$Lock_pathname" or die "open-append \"$Lock_pathname\": $!";
108 if (!flock LOCK,LOCK_EX|LOCK_NB) {
109 # NEVER unlink here, we are not the lock owning process!
113 my $sth=$DBI->prepare("select id,message from $DB_table where retries is not null"
114 # process only non-problematic mails during rerun
115 .($submitonce_run==1 ? "" : " and retries=0")
116 ." order by retries asc,id asc");
121 while (my $row=$sth->fetchrow_hashref()) {
122 $DBI->do("update $DB_table set retries=retries+1 where id=".$row->{"id"});
124 $sock=IO::Socket::INET->new(
125 "PeerAddr"=>$PeerAddr,
127 ) or confess "IO::Socket::INET->new(\"$PeerAddr\"): $!";
128 $sock->connected() or confess "socket not connected";
130 $sock->printflush(length($row->{"message"})."\n".$row->{"message"});
131 alarm $Socket_timeout and $sock->timeout($Socket_timeout) if $Socket_timeout;
133 my $gotlen=$sock->sysread($got,1);
134 confess $row->{"id"}.": sysread(1)=".(!defined $gotlen ? "undef" : $gotlen).": $!"
135 if !defined($gotlen) || $gotlen!=1;
138 # Prevent mailing errors from cron invoking us etc.
139 #print STDERR "FAIL:".$row->{"id"}."\n";
143 $DBI->do("update $DB_table set retries=null where id=".$row->{"id"});
145 print $progresschar.$row->{"id"}.($got eq "1" ? "" : "=FAIL");
149 $sock->shutdown(0); # stopped reading
150 $sock->printflush("BYE\n");
151 $sock->shutdown(2); # stopped using
154 print "\n" if $progresschar;
155 unlink $Lock_pathname;
157 return $progresschar;
162 1 while submitonce();
167 my $sth=$DBI->prepare("select message from $DB_table where retries is not null order by id");
169 while (my $row=$sth->fetchrow_hashref()) {
170 print $row->{"message"},"\n";
176 my($keyword,$interval)=@_;
178 # FIXME: SQL "now()" is raced against the block above
179 my $sth=$DBI->prepare("select id,time,retries from $DB_table where time>now()");
181 while (my $row=$sth->fetchrow_hashref()) {
182 warn "Message time in future: ".join(",",map(
183 "$_=".(!defined $row->{$_} ? "NULL" : $row->{$_})
187 return if $interval eq "";
189 my $print=s/^print://;
190 s/(\d+)y/($1*12)."m"/ge;
191 s/(\d+)m/($1*30)."d"/ge;
192 s/(\d+)d/($1*24)."h"/ge;
193 s/(\d+)h/($1*60)."M"/ge;
194 s/(\d+)M/($1*60)."s"/ge;
196 $sec+=$1 while s/(\d+)s//g;
197 die "Interval parse error; left \"$_\", parsed: $interval" if $_ ne "";
198 $sth=$DBI->prepare(($print ? "select id" : "delete")
199 ." from $DB_table where retries is null and time<from_unixtime(unix_timestamp()-$sec)");
202 print $sth->rows()."\n";
205 while (my $row=$sth->fetchrow_hashref()) {
206 print $row->{"id"},"\n";
212 $Getopt::Long::ignorecase=0;
220 "V|version",sub { print "perlmail-submit: $VERSION\n"; exit 0; },