From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost.localdomain (unknown [138.197.159.143]) by gnuweeb.org (Postfix) with ESMTPSA id 761C37ED93; Sat, 26 Feb 2022 16:53:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1645894385; bh=CiLvjeBUgimyiWivNiu6U45XtMidBAE+XlxHZRYvEro=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jtKjACF8OsgCLh6+o4XQvRjNzx0hTl/vjjEJU2e3tm8532TJGIEWdrcjc7wVrE2es CMqJkFK0AqAPNkzko3/gd4sVIwb3wn42dwZdvW5wsyDr4ygKgsgWnrES9E/tWwSL9d EXDm3hkWK+DzODWmFNFZZDfpkY4UjVeLZOhcHfQwF6vX1NsrlHxvajaclvu/kWSEiL CiXlbU0PtxKa0SNWsRPBhne/wKWDD3IqI9z49eAWzR9oGthO+KukpRrH60Lo2VuoWT hneqdrr8tuHRWC9p0P/wAk/nyrgjStarvgHA57pZil7WBXl8fWe41dF823nQVhw0zf UwSDREazBJLzg== From: Alviro Iskandar Setiawan To: Ammar Faizi Cc: Nugra , Arthur Lapz , Alviro Iskandar Setiawan , GNU/Weeb Mailing List , Alviro Iskandar Setiawan Subject: [PATCH pit 2/3] bin/public-inbox-tg: Add Telegram CC notification support Date: Sat, 26 Feb 2022 16:52:56 +0000 Message-Id: <20220226165257.2417545-3-alviro.iskandar@gnuweeb.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220226165257.2417545-1-alviro.iskandar@gnuweeb.org> References: <20220226165257.2417545-1-alviro.iskandar@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: This adds "Telegram-Cc:" header when a registered Telegram user is in the "To:" or "Cc:" list. Use simple file lookup as the database. No SQL database is involved. Signed-off-by: Alviro Iskandar Setiawan --- bin/public-inbox-tg.php | 75 +++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 17 deletions(-) diff --git a/bin/public-inbox-tg.php b/bin/public-inbox-tg.php index a09a690..dea2de6 100644 --- a/bin/public-inbox-tg.php +++ b/bin/public-inbox-tg.php @@ -10,21 +10,52 @@ const TARGET_CHAT_ID = -1001483770714; // // Private Cloud // const TARGET_CHAT_ID = -1001226735471; +const DATA_DIR = __DIR__."/../data"; + require __DIR__."/../lib.php"; -function buildList(string $cc, string $name): string +function listTelegramLookup(array $list): array { - if (empty($cc)) - return ""; + $ret = []; - $ret = ""; - $ccs = explode(",", $cc); - foreach ($ccs as $cc) - $ret .= "{$name}: ".trim($cc)."\n"; + if (!is_dir(DATA_DIR)) + mkdir(DATA_DIR); + + if (!is_dir(DATA_DIR."/tg")) + mkdir(DATA_DIR."/tg"); + + foreach ($list as $c) { + if (preg_match("/.+\<(.+?)\>/", $c, $m)) + $c = $m[1]; + + $u = DATA_DIR."/tg/".$c; + if (!file_exists($u)) + continue; + + $ret[] = trim(file_get_contents($u)); + } return $ret; } +function extractList(string $str): array +{ + $str = explode(",", $str); + foreach ($str as &$c) + $c = trim($c); + + return $str; +} + +function buildList(array $list, string $name): string +{ + $ret = ""; + foreach ($list as $c) + $ret .= "{$name}: ".trim($c)."\n"; + + return trim($ret); +} + // Thanks to https://stackoverflow.com/a/2955878/7275114 function slugify($text, string $divider = '-'): string { @@ -104,27 +135,37 @@ function fx(string $input): int $err = ""; if (preg_match("/(?:^|\\n)to:\s+?(.+?)(?:\\n\S+\:|\\n\\n)/si", $hdr, $m)) { - $to = clean_header_val($m[1]); - $toList = trim(buildList($to, "To")); + $toList = extractList(clean_header_val($m[1])); + $toListStr = buildList($toList, "To"); } else { - $toList = ""; + $toListStr = ""; } if (preg_match("/(?:^|\\n)cc:\s+?(.+?)(\\n\S+\:|\\n\\n)/si", $hdr, $m)) { - $cc = clean_header_val($m[1]); - $ccList = trim(buildList($cc, "Cc")); + $ccList = extractList(clean_header_val($m[1])); + $ccListStr = buildList($toList, "Cc"); } else { - $ccList = ""; + $ccListStr = ""; } $content = trim(substr($body, 0, MAX_BODYLEN)); $msg = "#ml\nFrom: {$from}\n"; - if ($toList) - $msg .= "{$toList}\n"; + if ($toListStr) + $msg .= "{$toListStr}\n"; + + if ($ccListStr) + $msg .= "{$ccListStr}\n"; - if ($ccList) - $msg .= "{$ccList}\n"; + $tgCCs = array_merge(...[listTelegramLookup($toList), listTelegramLookup($ccList)]); + $tgCCs = array_unique($tgCCs); + + if (count($tgCCs) > 0) { + $msg .= "Telegram-Cc: "; + foreach ($tgCCs as $tgCC) + $msg .= $tgCC; + $msg .= "\n"; + } $msg .= "Date: {$date}\n"; $msg .= "Subject: {$subject}"; -- 2.32.0