unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#43328: [PATCH] Make ERC desktop notifications lenient to invalid XML
@ 2020-09-11  9:41 Dario Gjorgjevski
  2020-10-15 15:49 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 2+ messages in thread
From: Dario Gjorgjevski @ 2020-09-11  9:41 UTC (permalink / raw)
  To: 43328

[-- Attachment #1: Type: text/plain, Size: 557 bytes --]

Hi,

Currently, ‘erc-notifications-notify’ signals an error if the message
contains invalid XML characters.  These characters can be either either
IRC control codes (e.g., ^B for bold text or ^C6,12 for colored text) or
just plain invalid data.

This patch changes the behavior so that they are stripped instead.  To
test, try sending yourself a message such as

   /msg <nick> ^BHi!

where <nick> is your own nick.  The current version will signal an error
whereas this patch won’t.

The proposed patch is attached to this message.


[-- Attachment #2: Make ERC desktop notifications lenient to invalid XML --]
[-- Type: text/x-diff, Size: 4649 bytes --]

From ad376fafc7b453fe8f34dbdb2a8d60b7757d6edd Mon Sep 17 00:00:00 2001
From: Dario Gjorgjevski <dario.gjorgjevski@gmail.com>
Date: Fri, 11 Sep 2020 11:26:17 +0200
Subject: [PATCH] Make ERC desktop notifications lenient to invalid XML
 characters
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Currently, ‘erc-notifications-notify’ signals an error if the message
contains invalid XML characters.  These characters can be either
either IRC control codes (e.g., ^B for bold text or ^C6,12 for colored
text) or just plain invalid data.

This commit changes the behavior so that they are stripped instead.
To test, try sending yourself a message such as

   /msg <nick> ^BHi!

where <nick> is your own nick.  The current version will signal an
error whereas this commit won’t.

* lisp/xml.el (xml-invalid-characters-re): New constant.

(xml-escape-string): New optional parameter NOERROR, causing invalid
characters to be stripped instead of signaling an error.

* lisp/erc/erc-desktop-notifications.el (erc-notifications-notify):
Strip IRC control codes and invalid XML characters before notifying.
---
 lisp/erc/erc-desktop-notifications.el | 11 ++++++-----
 lisp/xml.el                           | 19 ++++++++++++-------
 2 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/lisp/erc/erc-desktop-notifications.el b/lisp/erc/erc-desktop-notifications.el
index 1e65f8f427..3a9a4a4bac 100644
--- a/lisp/erc/erc-desktop-notifications.el
+++ b/lisp/erc/erc-desktop-notifications.el
@@ -31,6 +31,7 @@
 (require 'erc)
 (require 'xml)
 (require 'notifications)
+(require 'erc-goodies)
 (require 'erc-match)
 (require 'dbus)
 
@@ -62,12 +63,12 @@ This will replace the last notification sent with this function."
   ;; setting the current buffer to the existing query buffer)
   (dbus-ignore-errors
     (setq erc-notifications-last-notification
-          (let ((channel (if privp (erc-get-buffer nick) (current-buffer))))
+          (let* ((channel (if privp (erc-get-buffer nick) (current-buffer)))
+                 (title (format "%s in %s" (xml-escape-string nick t) channel))
+                 (body (xml-escape-string (erc-controls-strip msg) t)))
             (notifications-notify :bus erc-notifications-bus
-                                  :title (format "%s in %s"
-                                                 (xml-escape-string nick)
-                                                 channel)
-                                  :body (xml-escape-string msg)
+                                  :title title
+                                  :body body
                                   :replaces-id erc-notifications-last-notification
                                   :app-icon erc-notifications-icon
                                   :actions '("default" "Switch to buffer")
diff --git a/lisp/xml.el b/lisp/xml.el
index 10ef8e2087..236d9cbe6c 100644
--- a/lisp/xml.el
+++ b/lisp/xml.el
@@ -1015,7 +1015,10 @@ The first line is indented with the optional INDENT-STRING."
 
 (defalias 'xml-print 'xml-debug-print)
 
-(defun xml-escape-string (string)
+(defconst xml-invalid-characters-re
+  "[^\u0009\u000A\u000D\u0020-\uD7FF\uE000-\uFFFD\U00010000-\U0010FFFF]")
+
+(defun xml-escape-string (string &optional noerror)
   "Convert STRING into a string containing valid XML character data.
 Replace occurrences of &<>\\='\" in STRING with their default XML
 entity references (e.g., replace each & with &amp;).
@@ -1026,15 +1029,17 @@ restriction on \" or \\=', but we just substitute for these too
 \(as is permitted by the spec).
 
 If STRING contains characters that are invalid in XML (as defined
-by https://www.w3.org/TR/xml/#charsets), signal an error of type
-`xml-invalid-character'."
+by https://www.w3.org/TR/xml/#charsets), operate depending on the
+value of NOERROR: if it is non-nil, remove them; else, signal an
+error of type `xml-invalid-character'."
   (with-temp-buffer
     (insert string)
     (goto-char (point-min))
-    (when (re-search-forward
-           "[^\u0009\u000A\u000D\u0020-\uD7FF\uE000-\uFFFD\U00010000-\U0010FFFF]"
-           nil t)
-      (signal 'xml-invalid-character (list (char-before) (match-beginning 0))))
+    (while (re-search-forward xml-invalid-characters-re nil t)
+      (if noerror
+          (replace-match "")
+        (signal 'xml-invalid-character
+                (list (char-before) (match-beginning 0)))))
     (dolist (substitution '(("&" . "&amp;")
 			    ("<" . "&lt;")
 			    (">" . "&gt;")
-- 
2.17.1


[-- Attachment #3: Type: text/plain, Size: 173 bytes --]


Best regards,
Dario

-- 
dario.gjorgjevski@gmail.com :: +49 1525 8666837
%   gpg --keyserver 'hkps://hkps.pool.sks-keyservers.net' \
\`>     --recv-keys '744A4F0B4F1C9371'

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* bug#43328: [PATCH] Make ERC desktop notifications lenient to invalid XML
  2020-09-11  9:41 bug#43328: [PATCH] Make ERC desktop notifications lenient to invalid XML Dario Gjorgjevski
@ 2020-10-15 15:49 ` Lars Ingebrigtsen
  0 siblings, 0 replies; 2+ messages in thread
From: Lars Ingebrigtsen @ 2020-10-15 15:49 UTC (permalink / raw)
  To: Dario Gjorgjevski; +Cc: 43328

Dario Gjorgjevski <dario.gjorgjevski@gmail.com> writes:

> The proposed patch is attached to this message.

Looks like this patch was applied to Emacs 28 about a month ago, but
apparently I neglected to tell the patch submitter.  Sorry.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-10-15 15:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-11  9:41 bug#43328: [PATCH] Make ERC desktop notifications lenient to invalid XML Dario Gjorgjevski
2020-10-15 15:49 ` Lars Ingebrigtsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).