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;
19 my $Lock_pathname="/tmp/LaceMail.lock";
20 #my $PeerAddr="dejhome.dyn.jankratochvil.net.:852";
21 my $PeerAddr="127.0.0.1:2852";
22 my $Socket_timeout=7600; # 15sec is NOT enough!
23 my $DB_table="LaceMail_folder";
24 my $DBI_database="short";
26 my $DBI_pwd=$ENV{"HOME"}."/priv/mysql.".$DBI_user.".pwd";
27 open DBI_PWD,$DBI_pwd or die "open \"$DBI_pwd\": $!";
29 close DBI_PWD or warn "close DBI_pwd: $!";
32 my $DBI=DBI->connect_cached("DBI:mysql:database=$DBI_database;host=","$DBI_user",$DBI_pwd,{
33 "PrintError"=>0, # handled by "RaiseError" below
35 "ShowErrorStatement"=>1,
37 }) or confess "Failed DBI->connect(): $!";
44 eval { $DBI->do("drop table $name"); };
45 $DBI->do("create table $name (".join(",",@$cols).")");
50 create_table($DB_table,[
51 "id int not null auto_increment primary key",
52 "time timestamp not null", # assume ." default now()"
53 "message longtext not null",
54 "retries int null default 0", # null=>done, 0=not yet tried to submit
57 $DBI->do("alter table $DB_table add index (retries,id)");
69 close STDIN or cluck "close STDIN: $!";
72 # assume "retries"=>0,
74 my $prep=$DBI->prepare_cached("insert into $DB_table (".join(",",keys(%row)).")"
75 ." values (".join(",",map("?",keys(%row))).")");
76 $prep->execute(values(%row));
77 print $prep->{"mysql_insertid"}."\n";
83 confess if !defined $pid;
84 exit 0 if $pid; # parent
93 open LOCK,">>$Lock_pathname" or die "open-append \"$Lock_pathname\": $!";
94 if (!flock LOCK,LOCK_EX|LOCK_NB) {
95 # NEVER unlink here, we are not the lock owning process!
99 my $sth=$DBI->prepare("select id,message from $DB_table where retries is not null"
100 # process only non-problematic mails during rerun
101 .($submitonce_run==1 ? "" : " and retries=0")
102 ." order by retries asc,id asc");
107 while (my $row=$sth->fetchrow_hashref()) {
108 $DBI->do("update $DB_table set retries=retries+1 where id=".$row->{"id"});
110 $sock=IO::Socket::INET->new(
111 "PeerAddr"=>$PeerAddr,
113 ) or confess "IO::Socket::INET->new(\"$PeerAddr\"): $!";
114 $sock->connected() or confess "socket not connected";
116 $sock->printflush(length($row->{"message"})."\n".$row->{"message"});
117 alarm $Socket_timeout and $sock->timeout($Socket_timeout) if $Socket_timeout;
119 my $gotlen=$sock->sysread($got,1);
120 confess $row->{"id"}.": sysread(1)=".(!defined $gotlen ? "undef" : $gotlen).": $!"
121 if !defined($gotlen) || $gotlen!=1;
124 # Prevent mailing errors from cron invoking us etc.
125 #print STDERR "FAIL:".$row->{"id"}."\n";
129 $DBI->do("update $DB_table set retries=null where id=".$row->{"id"});
131 print $progresschar.$row->{"id"}.($got eq "1" ? "" : "=FAIL");
135 $sock->shutdown(0); # stopped reading
136 $sock->printflush("BYE\n");
137 $sock->shutdown(2); # stopped using
140 print "\n" if $progresschar;
141 unlink $Lock_pathname;
143 return $progresschar;
148 1 while submitonce();
153 my $sth=$DBI->prepare("select message from $DB_table where retries is not null order by id");
155 while (my $row=$sth->fetchrow_hashref()) {
156 print $row->{"message"},"\n";
162 my($keyword,$interval)=@_;
164 # FIXME: SQL "now()" is raced against the block above
165 my $sth=$DBI->prepare("select id,time,retries from $DB_table where time>now()");
167 while (my $row=$sth->fetchrow_hashref()) {
168 warn "Message time in future: ".join(",",map(
169 "$_=".(!defined $row->{$_} ? "NULL" : $row->{$_})
173 return if $interval eq "";
175 my $print=s/^print://;
176 s/(\d+)y/($1*12)."m"/ge;
177 s/(\d+)m/($1*30)."d"/ge;
178 s/(\d+)d/($1*24)."h"/ge;
179 s/(\d+)h/($1*60)."M"/ge;
180 s/(\d+)M/($1*60)."s"/ge;
182 $sec+=$1 while s/(\d+)s//g;
183 die "Interval parse error; left \"$_\", parsed: $interval" if $_ ne "";
184 $sth=$DBI->prepare(($print ? "select id" : "delete")
185 ." from $DB_table where retries is null and time<from_unixtime(unix_timestamp()-$sec)");
188 print $sth->rows()."\n";
191 while (my $row=$sth->fetchrow_hashref()) {
192 print $row->{"id"},"\n";
198 $Getopt::Long::ignorecase=0;
206 "V|version",sub { print "lacemail-submit: $VERSION\n"; exit 0; },