aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDario Gjorgjevski2020-09-13 16:49:00 +0200
committerLars Ingebrigtsen2020-09-13 16:49:00 +0200
commit2bd41321a6d9f6b478dd73aa9a2ea32709e57b32 (patch)
tree94e1b56650a565b2fe8d47bc01fa4618db45db2f
parentdef34a20766ea5179afd5e5ed090a2b86fcccb5e (diff)
downloademacs-2bd41321a6d9f6b478dd73aa9a2ea32709e57b32.tar.gz
emacs-2bd41321a6d9f6b478dd73aa9a2ea32709e57b32.zip
Make ERC desktop notifications lenient to invalid XML characters
* lisp/xml.el (xml-invalid-characters-re): New constant. * lisp/erc/erc-desktop-notifications.el (erc-notifications-notify): Strip IRC control codes and invalid XML characters before notifying (bug#43328).
-rw-r--r--lisp/erc/erc-desktop-notifications.el11
-rw-r--r--lisp/xml.el19
2 files changed, 18 insertions, 12 deletions
diff --git a/lisp/erc/erc-desktop-notifications.el b/lisp/erc/erc-desktop-notifications.el
index 1e65f8f4275..3a9a4a4bac6 100644
--- a/lisp/erc/erc-desktop-notifications.el
+++ b/lisp/erc/erc-desktop-notifications.el
@@ -31,6 +31,7 @@
31(require 'erc) 31(require 'erc)
32(require 'xml) 32(require 'xml)
33(require 'notifications) 33(require 'notifications)
34(require 'erc-goodies)
34(require 'erc-match) 35(require 'erc-match)
35(require 'dbus) 36(require 'dbus)
36 37
@@ -62,12 +63,12 @@ This will replace the last notification sent with this function."
62 ;; setting the current buffer to the existing query buffer) 63 ;; setting the current buffer to the existing query buffer)
63 (dbus-ignore-errors 64 (dbus-ignore-errors
64 (setq erc-notifications-last-notification 65 (setq erc-notifications-last-notification
65 (let ((channel (if privp (erc-get-buffer nick) (current-buffer)))) 66 (let* ((channel (if privp (erc-get-buffer nick) (current-buffer)))
67 (title (format "%s in %s" (xml-escape-string nick t) channel))
68 (body (xml-escape-string (erc-controls-strip msg) t)))
66 (notifications-notify :bus erc-notifications-bus 69 (notifications-notify :bus erc-notifications-bus
67 :title (format "%s in %s" 70 :title title
68 (xml-escape-string nick) 71 :body body
69 channel)
70 :body (xml-escape-string msg)
71 :replaces-id erc-notifications-last-notification 72 :replaces-id erc-notifications-last-notification
72 :app-icon erc-notifications-icon 73 :app-icon erc-notifications-icon
73 :actions '("default" "Switch to buffer") 74 :actions '("default" "Switch to buffer")
diff --git a/lisp/xml.el b/lisp/xml.el
index 10ef8e2087a..236d9cbe6c0 100644
--- a/lisp/xml.el
+++ b/lisp/xml.el
@@ -1015,7 +1015,10 @@ The first line is indented with the optional INDENT-STRING."
1015 1015
1016(defalias 'xml-print 'xml-debug-print) 1016(defalias 'xml-print 'xml-debug-print)
1017 1017
1018(defun xml-escape-string (string) 1018(defconst xml-invalid-characters-re
1019 "[^\u0009\u000A\u000D\u0020-\uD7FF\uE000-\uFFFD\U00010000-\U0010FFFF]")
1020
1021(defun xml-escape-string (string &optional noerror)
1019 "Convert STRING into a string containing valid XML character data. 1022 "Convert STRING into a string containing valid XML character data.
1020Replace occurrences of &<>\\='\" in STRING with their default XML 1023Replace occurrences of &<>\\='\" in STRING with their default XML
1021entity references (e.g., replace each & with &amp;). 1024entity references (e.g., replace each & with &amp;).
@@ -1026,15 +1029,17 @@ restriction on \" or \\=', but we just substitute for these too
1026\(as is permitted by the spec). 1029\(as is permitted by the spec).
1027 1030
1028If STRING contains characters that are invalid in XML (as defined 1031If STRING contains characters that are invalid in XML (as defined
1029by https://www.w3.org/TR/xml/#charsets), signal an error of type 1032by https://www.w3.org/TR/xml/#charsets), operate depending on the
1030`xml-invalid-character'." 1033value of NOERROR: if it is non-nil, remove them; else, signal an
1034error of type `xml-invalid-character'."
1031 (with-temp-buffer 1035 (with-temp-buffer
1032 (insert string) 1036 (insert string)
1033 (goto-char (point-min)) 1037 (goto-char (point-min))
1034 (when (re-search-forward 1038 (while (re-search-forward xml-invalid-characters-re nil t)
1035 "[^\u0009\u000A\u000D\u0020-\uD7FF\uE000-\uFFFD\U00010000-\U0010FFFF]" 1039 (if noerror
1036 nil t) 1040 (replace-match "")
1037 (signal 'xml-invalid-character (list (char-before) (match-beginning 0)))) 1041 (signal 'xml-invalid-character
1042 (list (char-before) (match-beginning 0)))))
1038 (dolist (substitution '(("&" . "&amp;") 1043 (dolist (substitution '(("&" . "&amp;")
1039 ("<" . "&lt;") 1044 ("<" . "&lt;")
1040 (">" . "&gt;") 1045 (">" . "&gt;")