From 2d2446985c9357bcf458b10efecb19100dc0b47c Mon Sep 17 00:00:00 2001
From: TEC <git@tecosaur.net>
Date: Sun, 7 Aug 2022 16:21:21 +0800
Subject: org: Add "mark domain as safe" convenience action

* lisp/org.el (org--confirm-resource-safe): Pick out domains from URLs,
and provide an option of marking that domain as safe.

(cherry picked from Org-mode commit 1ae801e9c86d5b150fd085230722e4dac550df30)
---
 lisp/org/org.el | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)

diff --git a/lisp/org/org.el b/lisp/org/org.el
index 96366959605..1bc7e98cba5 100644
--- a/lisp/org/org.el
+++ b/lisp/org/org.el
@@ -4788,6 +4788,13 @@ org--confirm-resource-safe
   "Ask the user if URI should be considered safe, returning non-nil if so."
   (unless noninteractive
     (let ((current-file (and buffer-file-name (file-truename buffer-file-name)))
+          (domain (and (string-match
+                        (rx (seq "http" (? "s") "://")
+                            (optional (+ (not (any "@/\n"))) "@")
+                            (optional "www.")
+                            (one-or-more (not (any ":/?\n"))))
+                        uri)
+                       (match-string 0 uri)))
           (buf (get-buffer-create "*Org Remote Resource*")))
       ;; Set up the contents of the *Org Remote Resource* buffer.
       (with-current-buffer buf
@@ -4798,6 +4805,11 @@ org--confirm-resource-safe
                 "Do you want to download this?  You can type\n "
                 (propertize "!" 'face 'success)
                 " to download this resource, and permanantly mark it as safe.\n "
+                (if domain
+                    (concat
+                     (propertize "d" 'face 'success)
+                     " to download this resource, and mark this domain as safe.\n ")
+                  "")
                 (propertize "f" 'face 'success)
                 (if current-file
                     (concat
@@ -4815,8 +4827,8 @@ org--confirm-resource-safe
       ;; Display the buffer and read a choice.
       (save-window-excursion
         (pop-to-buffer buf)
-        (let* ((exit-chars (append '(?y ?n ?! ?\s) (and current-file '(?f))))
-               (prompt (format "Please type y, n%s, or !%s: "
+        (let* ((exit-chars (append '(?y ?n ?! ?d ?\s) (and current-file '(?f))))
+               (prompt (format "Please type y, n%s, d, or !%s: "
                                (if current-file ", f" "")
                                (if (< (line-number-at-pos (point-max))
                                       (window-body-height))
@@ -4824,15 +4836,17 @@ org--confirm-resource-safe
                                  ", or C-v/M-v to scroll")))
                char)
           (setq char (read-char-choice prompt exit-chars))
-          (when (memq char '(?! ?f))
+          (when (memq char '(?! ?f ?d))
             (customize-push-and-save
              'org-safe-remote-resources
-             (list (concat "\\`"
-                           (regexp-quote
-                            (if (and (= char ?f) current-file)
-                                (concat "file://" current-file) uri))
-                           "\\'"))))
-          (prog1 (memq char '(?! ?\s ?y ?f))
+             (list (if (eq char ?d)
+                       (concat "\\`" (regexp-quote domain) "\\(?:/\\|\\'\\)")
+                     (concat "\\`"
+                             (regexp-quote
+                              (if (and (= char ?f) current-file)
+                                  (concat "file://" current-file) uri))
+                             "\\'")))))
+          (prog1 (memq char '(?y ?n ?! ?d ?\s ?f))
             (quit-window t)))))))
 
 (defun org-extract-log-state-settings (x)
