Last active
September 4, 2024 19:12
-
-
Save ammarfaizi2/c0a5142d2415af092bc14fad69bcd875 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
// SPDX-License-Identifier: GPL-2.0-only | |
const LOCK_FILE = "/tmp/public_inbox.lock"; | |
function clean_header_val(string $str): string | |
{ | |
$ex = explode("\n", $str); | |
foreach ($ex as &$v) | |
$v = trim($v); | |
return implode(" ", $ex); | |
} | |
function fx(string $input): int | |
{ | |
$hdr = explode("\n\n", $input, 2); | |
$hdr = trim($hdr[0]); | |
if (!preg_match("/(?:^|\\n)from:\s+?(.+?)(?:\\n\S+\:|$)/si", $hdr, $m)) { | |
printf("x: Cannot find the 'from' header in the email.\n"); | |
return 1; | |
} | |
$from = clean_header_val($m[1]); | |
if (preg_match("/\\@turing\\.[\\w\\d\\.]+$/", $from)) { | |
/* | |
* Drop any email from turing.bla domain | |
* where 'bla' can be anything. Note that | |
* it was not enough to block turing.com | |
* because their spam system has started | |
* expanding to other TLDs (not only .com). | |
*/ | |
// Bounce the email. | |
printf("x: The MDA detected /\\@turing\\.[\\w\\d\\.]+$/ pattern and it's globally recognized as spam. The email cannot be delivered.\n"); | |
return 1; | |
} | |
$uniq = date("Y_m_d_H_i_s_").rand().microtime(true); | |
$tmpName = "/tmp/public_inbox_{$uniq}.txt"; | |
file_put_contents($tmpName, $input); | |
$tmpName = escapeshellarg($tmpName); | |
shell_exec("/usr/local/bin/public-inbox-mda < {$tmpName}"); | |
unlink($tmpName); | |
return 0; | |
} | |
function main(): int | |
{ | |
$handle = fopen(LOCK_FILE, "a"); | |
if (!$handle) { | |
printf("Cannot open the lock file: %s\n", LOCK_FILE); | |
return 1; | |
} | |
flock($handle, LOCK_EX); | |
$ret = fx(file_get_contents("php://stdin")); | |
flock($handle, LOCK_UN); | |
fclose($handle); | |
return $ret; | |
} | |
exit(main()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment