public inbox for [email protected]
 help / color / mirror / Atom feed
From: Alviro Iskandar Setiawan <[email protected]>
To: Ammar Faizi <[email protected]>
Cc: Nugra <[email protected]>, Arthur Lapz <[email protected]>,
	Alviro Iskandar Setiawan <[email protected]>,
	GNU/Weeb Mailing List <[email protected]>,
	Alviro Iskandar Setiawan <[email protected]>
Subject: [PATCH pit 2/3] bin/public-inbox-tg: Add Telegram CC notification support
Date: Sat, 26 Feb 2022 16:52:56 +0000	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

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 <[email protected]>
---
 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


  parent reply	other threads:[~2022-02-26 16:53 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-26 16:52 [PATCH pit 0/3] Add Telegram Notification Support Alviro Iskandar Setiawan
2022-02-26 16:52 ` [PATCH pit 1/3] lib: Remove unnecessary var_dump Alviro Iskandar Setiawan
2022-02-26 16:57   ` Ammar Faizi
2022-02-26 16:52 ` Alviro Iskandar Setiawan [this message]
2022-02-26 16:52 ` [PATCH pit 3/3] data: Add GNU/Weeb friends Alviro Iskandar Setiawan
2022-02-26 17:02 ` [PATCH pit 0/3] Add Telegram Notification Support Ammar Faizi
2022-02-26 17:11 ` Ammar Faizi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220226165257.2417545-3-alviro.iskandar@gnuweeb.org \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox