2 # https://datatracker.ietf.org/doc/html/rfc1870
12 die "arg required" if @ARGV!=!$ENV{"GATEWAY_INTERFACE"};
17 if ($ENV{"GATEWAY_INTERFACE"}) {
21 Content-type: text/html
23 <html><head><title>Maximum Mail Size Query</title></head><body>
25 my %q=URI::Query->new($ENV{"QUERY_STRING"})->hash();
26 my $domain=$q{"domain"};
28 if ($domain=~/([^-_a-zA-Z0-9.])/) {
29 print "<p><b>Error: Invalid character '".encode_entities($1)."' in specified domain name: ".encode_entities($domain)."</b></p>\n";
37 my $dns=Net::DNS::Resolver->new();
38 #my $dns=Net::DNS::Resolver->new("nameservers"=>["255.255.255.255"]);
39 my $reply=$dns->search($domain,"MX");
41 do { $type="AAAA"; $reply=$dns->search($domain,"AAAA") } if !$reply&&$dns->errorstring() eq "NOERROR";
42 do { $type="A" ; $reply=$dns->search($domain,"A" ) } if !$reply&&$dns->errorstring() eq "NOERROR";
44 print $dns->errorstring().$nl if !$reply;
47 print "Found $type$nl";
49 foreach my $rr ($reply->answer()) {
51 $addr=$rr->exchange() if $rr->type eq "MX";
52 $addr=$rr->address() if $rr->type=~/^(?:A|AAAA)$/;
53 do { print "Invalid RR: ".$rr->string()."$nl"; next; } if !defined $addr;
55 my $smtp=Net::SMTP->new($addr,"Timeout"=>10);
56 do { print "Cannot connect to SMTP server $addr: $@$nl"; next; } if !$smtp;
57 my $size=($smtp->message()=~/^SIZE\s*(\d+)\s*$/mi)[0];
58 push @retval,[$addr,$size];
64 open F,"/proc/loadavg" or die;
65 my $load=defined <F> or die;
69 print "<p>Sorry but the current machine load ($load) does not permit running this service now. Try again later.</p>\n";
72 if ($arg&&load_ok()) {
77 print "<p>Error: Unable to determine anything about domain ".encode_entities($arg)."</p>\n";
82 $different=1 if ($size//-1)!=($r->[1]//-1);
86 # https://stackoverflow.com/a/1533248/2995591
87 my $code_size = ceil(($input_size * 4) / 3);
88 my $padding_size = ($input_size % 3) ? (3 - ($input_size % 3)) : 0;
89 my $crlfs_size = 2 + ceil(2 * ($code_size + $padding_size) / 72);
90 my $total_size = $code_size + $padding_size + $crlfs_size;
112 return "$b bytes" if !$kb;
113 my $mb=int($kb/1000);
114 return "$kb KB" if !$mb;
115 my $gb=int($mb/1000);
116 return "$mb MB" if !$gb;
121 return "unknown" if !defined $size;
122 return "unlimited" if $size==0;
124 return human($rev)." ($rev bytes; raw text mail size $size)";
127 print "<p>Domain ".encode_entities($arg)." has maximum attachment size ".size($size)."</p>\n";
129 print "<p>Domain ".encode_entities($arg)." maximum attachment size depends on which server is contacted:</p>";
130 print "<table border=\"1\">\n";
132 print "<tr><td>".encode_entities($r->[0])."</td><td>".size($r->[1])."</td></tr>\n";
138 if ($ENV{"GATEWAY_INTERFACE"}) {
141 <form action="mailsize.cgi" method="get">
142 <input type="text" name="domain" size="32" value="@{[ encode_entities($arg||"") ]}" autofocus="autofocus" />
143 <input type="submit">
147 <a href="https://git.jankratochvil.net/?p=mailsize.git;a=tree">GIT</a>