6 $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
12 use Carp qw(cluck confess);
13 require IO::Socket::INET;
20 exit $ExitCode if defined $ExitCode;
23 my $Lock_pathname="/tmp/LaceMail.lock";
24 my $PeerAddr="dejhome.dyn.jankratochvil.net.:852";
25 my $Socket_timeout=600; # 15sec is NOT enough!
26 my $DB_table="LaceMail_folder";
27 my $DBI_database="short";
29 my $DBI_pwd=$ENV{"HOME"}."/priv/mysql.".$DBI_user.".pwd";
30 open DBI_PWD,$DBI_pwd or die "open \"$DBI_pwd\": $!";
32 close DBI_PWD or warn "close DBI_pwd: $!";
35 my $DBI=DBI->connect_cached("DBI:mysql:database=$DBI_database;host=","$DBI_user",$DBI_pwd,{
36 "PrintError"=>0, # handled by "RaiseError" below
38 "ShowErrorStatement"=>1,
40 }) or confess "Failed DBI->connect(): $!";
47 eval { $DBI->do("drop table $name"); };
48 $DBI->do("create table $name (".join(",",@$cols).")");
53 create_table($DB_table,[
54 "id int not null auto_increment primary key",
55 "time timestamp not null", # assume ." default now()"
56 "message longtext not null",
57 "retries int null default 0", # null=>done, 0=not yet tried to submit
60 $DBI->do("alter table $DB_table add index (retries,id)");
74 # assume "retries"=>0,
76 my $prep=$DBI->prepare_cached("insert into $DB_table (".join(",",keys(%row)).")"
77 ." values (".join(",",map("?",keys(%row))).")");
78 $prep->execute(values(%row));
79 print $prep->{"mysql_insertid"}."\n";
80 $ExitCode=0; # we will succeed even if --submit fails
88 open LOCK,">>$Lock_pathname" or die "open-append \"$Lock_pathname\": $!";
89 if (!flock LOCK,LOCK_EX|LOCK_NB) {
90 # NEVER unlink here, we are not the lock owning process!
94 my $sth=$DBI->prepare("select id,message from $DB_table where retries is not null"
95 # process only non-problematic mails during rerun
96 .($submitonce_run==1 ? "" : " and retries=0")
97 ." order by retries asc,id asc");
102 while (my $row=$sth->fetchrow_hashref()) {
103 $DBI->do("update $DB_table set retries=retries+1 where id=".$row->{"id"});
105 $sock=IO::Socket::INET->new(
106 "PeerAddr"=>$PeerAddr,
108 ) or confess "IO::Socket::INET->new(\"$PeerAddr\"): $!";
109 $sock->connected() or confess "socket not connected";
111 $sock->printflush(length($row->{"message"})."\n".$row->{"message"});
112 alarm $Socket_timeout and $sock->timeout($Socket_timeout) if $Socket_timeout;
114 my $gotlen=$sock->sysread($got,1);
115 confess $row->{"id"}.": sysread(1)=".(!defined $gotlen ? "undef" : $gotlen).": $!"
116 if !defined($gotlen) || $gotlen!=1;
119 # Prevent mailing errors from cron invoking us etc.
120 #print STDERR "FAIL:".$row->{"id"}."\n";
124 $DBI->do("update $DB_table set retries=null where id=".$row->{"id"});
126 print $progresschar.$row->{"id"}.($got eq "1" ? "" : "=FAIL");
130 $sock->shutdown(0); # stopped reading
131 $sock->printflush("BYE\n");
132 $sock->shutdown(2); # stopped using
135 print "\n" if $progresschar;
136 unlink $Lock_pathname;
138 return $progresschar;
143 1 while submitonce();
148 my $sth=$DBI->prepare("select message from $DB_table where retries is not null order by id");
150 while (my $row=$sth->fetchrow_hashref()) {
151 print $row->{"message"},"\n";
157 my($keyword,$interval)=@_;
159 # FIXME: SQL "now()" is raced against the block above
160 my $sth=$DBI->prepare("select id,time,retries from $DB_table where time>now()");
162 while (my $row=$sth->fetchrow_hashref()) {
163 warn "Message time in future: ".join(",",map(
164 "$_=".(!defined $row->{$_} ? "NULL" : $row->{$_})
168 return if $interval eq "";
170 my $print=s/^print://;
171 s/(\d+)y/($1*12)."m"/ge;
172 s/(\d+)m/($1*30)."d"/ge;
173 s/(\d+)d/($1*24)."h"/ge;
174 s/(\d+)h/($1*60)."M"/ge;
175 s/(\d+)M/($1*60)."s"/ge;
177 $sec+=$1 while s/(\d+)s//g;
178 die "Interval parse error; left \"$_\", parsed: $interval" if $_ ne "";
179 $sth=$DBI->prepare(($print ? "select id" : "delete")
180 ." from $DB_table where retries is null and time<from_unixtime(unix_timestamp()-$sec)");
183 print $sth->rows()."\n";
186 while (my $row=$sth->fetchrow_hashref()) {
187 print $row->{"id"},"\n";
193 $Getopt::Long::ignorecase=0;
200 "V|version",sub { print "lacemail-submit: $VERSION\n"; exit 0; },