Re: bug#67677: 30.0.50; ERC 5.6: Use templates for formatting chat messages

"J.P." <[email protected]> Mon, 18 Dec 2023 06:50:25 -0800
Newsgroups gmane.emacs.erc.general
Message-ID <[email protected]>
A version of these changes has been installed as

  49bfea4386f * Use templates for formatting chat messages in ERC

Unfortunately, they included a somewhat glaring omission involving CTCP
replies that's hopefully addressed by the attached patch.
0001-5.6-Populate-erc-msg-prop-overrides-for-CTCP-replies.patch (text/x-patch, 5.9 KB)
From ad93b795ae3ed3f0b91acdf5806703e32b3ef074 Mon Sep 17 00:00:00 2001
From: "F. Jason Park" <[email protected]>
Date: Sun, 17 Dec 2023 21:49:13 -0800
Subject: [PATCH] [5.6] Populate erc--msg-prop-overrides for CTCP replies

* lisp/erc/erc-backend.el (erc-server-PRIVMSG): Don't set string
intended for insertion to the undefined return value of
`erc-process-ctcp-reply' and `erc-process-ctcp-query'.  Rework control
flow slightly for clarity.
* lisp/erc/erc.el (erc-process-ctcp-reply): Bind
`erc--msg-prop-overrides' and populate with `erc--ctcp' and `erc--cmd'
"msg props" for the benefit of `erc-display-message' calls made by
CTCP reply handlers.  (Bug#67677)
---
 lisp/erc/erc-backend.el | 53 +++++++++++++++++++----------------------
 lisp/erc/erc.el         | 10 ++++++--
 2 files changed, 32 insertions(+), 31 deletions(-)

diff --git a/lisp/erc/erc-backend.el b/lisp/erc/erc-backend.el
index 0c336540483..6d30409e156 100644
--- a/lisp/erc/erc-backend.el
+++ b/lisp/erc/erc-backend.el
@@ -1996,8 +1996,8 @@ erc--speaker-status-prefix-wanted-p
              (erc--msg-prop-overrides `((erc--tmp) ,@erc--msg-prop-overrides))
              (erc--speaker-status-prefix-wanted-p nil)
              (erc-current-message-catalog erc--message-speaker-catalog)
-             s buffer statusmsg cmem-prefix
-             fnick)
+             ;;
+             buffer statusmsg cmem-prefix fnick)
         (setq buffer (erc-get-buffer (if privp nick tgt) proc))
         ;; Even worth checking for empty target here? (invalid anyway)
         (unless (or buffer noticep (string-empty-p tgt) (eq ?$ (aref tgt 0))
@@ -2042,36 +2042,31 @@ erc--speaker-status-prefix-wanted-p
                                          erc-show-speaker-membership-status
                                          inputp)
                                      (cdr cdata))))))
-        (cond
-         ((erc-is-message-ctcp-p msg)
-          ;; FIXME explain undefined return values being assigned to `s'.
-          (setq s (if-let ((parsed
-                            (erc--ctcp-response-from-parsed
-                             :parsed parsed :buffer buffer :statusmsg statusmsg
-                             :prefix cmem-prefix :dispname fnick))
-                           (msgp))
-                      (erc-process-ctcp-query proc parsed nick login host)
-                    (erc-process-ctcp-reply proc parsed nick login host
-                                            (match-string 1 msg)))))
-         (t
+        (if (erc-is-message-ctcp-p msg)
+            (if noticep
+                (erc-process-ctcp-reply proc parsed nick login host
+                                        (match-string 1 msg))
+              (setq parsed (erc--ctcp-response-from-parsed
+                            :parsed parsed :buffer buffer :statusmsg statusmsg
+                            :prefix cmem-prefix :dispname fnick))
+              (erc-process-ctcp-query proc parsed nick login host))
           (setq erc-server-last-peers (cons nick (cdr erc-server-last-peers)))
           (with-current-buffer (or buffer (current-buffer))
             ;; Re-bind in case either buffer has a local value.
-            (let ((erc-current-message-catalog erc--message-speaker-catalog))
-              (setq s (erc--determine-speaker-message-format-args
-                       nick msg privp msgp inputp statusmsg
-                       cmem-prefix fnick))))))
-        (when s
-          (if (and noticep privp)
-              (progn
-                (push (cons 'erc--msg (car s)) erc--msg-prop-overrides)
-                (setq s (apply #'erc-format-message s))
-                (run-hook-with-args 'erc-echo-notice-always-hook
-                                    s parsed buffer nick)
-                (run-hook-with-args-until-success
-                 'erc-echo-notice-hook s parsed buffer nick))
-            (apply #'erc-display-message parsed nil buffer
-                   (ensure-list s))))))))
+            (let ((erc-current-message-catalog erc--message-speaker-catalog)
+                  (msg-args (erc--determine-speaker-message-format-args
+                             nick msg privp msgp inputp statusmsg
+                             cmem-prefix fnick)))
+              (if (or msgp (not privp))
+                  ;; This is a PRIVMSG or a NOTICE to a channel.
+                  (apply #'erc-display-message parsed nil buffer msg-args)
+                ;; This is a NOTICE directed at the client's current nick.
+                (push (cons 'erc--msg (car msg-args)) erc--msg-prop-overrides)
+                (let ((fmtmsg (apply #'erc-format-message msg-args)))
+                  (run-hook-with-args 'erc-echo-notice-always-hook
+                                      fmtmsg parsed buffer nick)
+                  (run-hook-with-args-until-success
+                   'erc-echo-notice-hook fmtmsg parsed buffer nick))))))))))
 
 (define-erc-response-handler (QUIT)
   "Another user has quit IRC." nil
diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el
index faa2cbefd1b..37e47591d73 100644
--- a/lisp/erc/erc.el
+++ b/lisp/erc/erc.el
@@ -6525,8 +6525,14 @@ erc-ctcp-query-VERSION
 (defun erc-process-ctcp-reply (proc parsed nick login host msg)
   "Process MSG as a CTCP reply."
   (let* ((type (car (split-string msg)))
-         (hook (intern (concat "erc-ctcp-reply-" type "-hook"))))
-    (if (boundp hook)
+         (hook (intern-soft (concat "erc-ctcp-reply-" type "-hook")))
+         ;; Help `erc-display-message' by ensuring subsequent
+         ;; insertions retain the necessary props.
+         (cmd (erc--get-eq-comparable-cmd (erc-response.command parsed)))
+         (erc--msg-prop-overrides `((erc--ctcp . ,(and hook (intern type)))
+                                    (erc--cmd . ,cmd)
+                                    ,@erc--msg-prop-overrides)))
+    (if (and hook (boundp hook))
         (run-hook-with-args-until-success
          hook proc nick login host
          (car (erc-response.command-args parsed)) msg)
-- 
2.42.0