The web is alive.
authorJan Kratochvil <jan@jankratochvil.net>
Thu, 10 Aug 2023 08:58:04 +0000 (10:58 +0200)
committerJan Kratochvil <jan@jankratochvil.net>
Thu, 10 Aug 2023 08:58:04 +0000 (10:58 +0200)
.htaccess [new file with mode: 0644]
mailsize
mailsize.cgi [new symlink]

diff --git a/.htaccess b/.htaccess
new file mode 100644 (file)
index 0000000..117a1cc
--- /dev/null
+++ b/.htaccess
@@ -0,0 +1,5 @@
+AddHandler cgi-script .cgi
+<Files "index.html">
+       RewriteEngine On
+       RewriteRule ^.*$ http://localhost/mailsize.cgi [P,L]
+</Files>
index d8064da..2600f79 100755 (executable)
--- a/mailsize
+++ b/mailsize
@@ -1,4 +1,5 @@
 #! /usr/bin/perl
+# https://datatracker.ietf.org/doc/html/rfc1870
 use strict;
 use warnings;
 require Net::DNS;
@@ -6,6 +7,7 @@ require Net::SMTP;
 use HTML::Entities;
 require URI::Query;
 use POSIX;
+$|=1;
 
 die "arg required" if @ARGV!=!$ENV{"GATEWAY_INTERFACE"};
 my $arg=$ARGV[0];
@@ -57,7 +59,17 @@ sub query($) {
   }
   return @retval;
 }
-if ($arg) {
+sub load_ok() {
+  local *F;
+  open F,"/proc/loadavg" or die;
+  my $load=defined <F> or die;
+  close F or die;
+  $load=~s/ .*//;
+  return 1 if $load<16;
+  print "<p>Sorry but the current machine load ($load) does not permit running this service now. Try again later.</p>\n";
+  return 0;
+}
+if ($arg&&load_ok()) {
   print "<pre>\n";
   my @r=query $arg;
   print "</pre>\n";
@@ -65,8 +77,9 @@ if ($arg) {
     print "<p>Error: Unable to determine anything about domain ".encode_entities($arg)."</p>\n";
   } else {
     my $size=$r[0][1];
+    my $different;
     for my $r (@r) {
-      $size=undef if !$size||!$r->[1]||$size!=$r->[1];
+      $different=1 if ($size//-1)!=($r->[1]//-1);
     }
     sub sizecalc($) {
       my($input_size)=@_;
@@ -79,6 +92,7 @@ if ($arg) {
     }
     sub rev($) {
       my($size)=@_;
+      return 0 if $size==0;
       my $l=0;
       my $r=$size;
       while ($l+1<$r) {
@@ -104,11 +118,12 @@ if ($arg) {
     }
     sub size($) {
       my($size)=@_;
-      return "unknown" if !$size;
+      return "unknown" if !defined $size;
+      return "unlimited" if $size==0;
       my $rev=rev($size);
       return human($rev)." ($rev bytes; raw text mail size $size)";
     }
-    if ($size) {
+    if (!$different) {
       print "<p>Domain ".encode_entities($arg)." has maximum attachment size ".size($size)."</p>\n";
     } else {
       print "<p>Domain ".encode_entities($arg)." maximum attachment size depends on which server is contacted:</p>";
@@ -123,7 +138,7 @@ if ($arg) {
 if ($ENV{"GATEWAY_INTERFACE"}) {
   print <<"EOH";
 <p>
-  <form action="/cgi-bin/smtpsize" method="get">
+  <form action="mailsize.cgi" method="get">
     <input type="text" name="domain" size="32" value="@{[ encode_entities($arg||"") ]}" autofocus="autofocus" />
     <input type="submit">
   </form>
diff --git a/mailsize.cgi b/mailsize.cgi
new file mode 120000 (symlink)
index 0000000..5eebf26
--- /dev/null
@@ -0,0 +1 @@
+mailsize
\ No newline at end of file