From 21859ebcafcda497a086a9db4701f0406db599ef Mon Sep 17 00:00:00 2001 From: Eric Hanchrow Date: Fri, 30 Nov 2012 12:18:22 +0800 Subject: [PATCH] New ERC option to avoid sending accidentally-pasted text to the server. * erc.el (erc-last-input-time): New variable. (erc-accidental-paste-threshold-seconds): New option to avoid sending accidentally-pasted text to the server. (erc-send-current-line): Use it. Also, * erc.el (erc-lurker-cleanup, erc-lurker-p): Use float-time. Fixes: debbugs:11592 --- etc/NEWS | 15 +++++-- lisp/erc/ChangeLog | 11 ++++++ lisp/erc/erc.el | 97 ++++++++++++++++++++++++++++------------------ 3 files changed, 82 insertions(+), 41 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index fb86920303a..05a57e9afe7 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -29,10 +29,6 @@ so we will look at it and add it to the manual. * Changes in Specialized Modes and Packages in Emacs 24.4 -** Icomplete is a bit more like IDO. -*** key bindings to navigate through and select the completions. -*** The icomplete-separator is customizable, and its default has changed. -*** Removed icomplete-show-key-bindings. ** Calc *** Calc by default now uses the Gregorian calendar for all dates, and @@ -47,6 +43,17 @@ Nil, the default value, means to always use the Gregorian calendar. The value (YEAR MONTH DAY) means to start using the Gregorian calendar on the given date. +** ERC + +*** New option `erc-accidental-paste-threshold-seconds'. +If set to a number, this can be used to avoid accidentally paste large +amounts of data into the ERC input. + +** Icomplete is a bit more like IDO. +*** key bindings to navigate through and select the completions. +*** The icomplete-separator is customizable, and its default has changed. +*** Removed icomplete-show-key-bindings. + ** MH-E has been updated to MH-E version 8.4. See MH-E-NEWS for details. diff --git a/lisp/erc/ChangeLog b/lisp/erc/ChangeLog index 3f9824545cf..eeb31f99037 100644 --- a/lisp/erc/ChangeLog +++ b/lisp/erc/ChangeLog @@ -1,3 +1,14 @@ +2012-11-30 Eric Hanchrow + + * erc.el (erc-last-input-time): New variable. + (erc-accidental-paste-threshold-seconds): New option to avoid + sending accidentally-pasted text to the server (Bug#11592). + (erc-send-current-line): Use it. + +2012-11-30 Chong Yidong + + * erc.el (erc-lurker-cleanup, erc-lurker-p): Use float-time. + 2012-11-23 Stefan Monnier * erc-backend.el: Fix last change that missed calls to `second' diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el index cec9718e751..e03a0c5dc43 100644 --- a/lisp/erc/erc.el +++ b/lisp/erc/erc.el @@ -2534,9 +2534,9 @@ consumption for long-lived IRC or Emacs sessions." (maphash (lambda (nick last-PRIVMSG-time) (when - (> (time-to-seconds (time-subtract - (current-time) - last-PRIVMSG-time)) + (> (float-time (time-subtract + (current-time) + last-PRIVMSG-time)) erc-lurker-threshold-time) (remhash nick hash))) hash) @@ -2602,7 +2602,7 @@ server within `erc-lurker-threshold-time'. See also (gethash (erc-lurker-maybe-trim nick) (gethash server erc-lurker-state (make-hash-table))))) (or (null last-PRIVMSG-time) - (> (time-to-seconds + (> (float-time (time-subtract (current-time) last-PRIVMSG-time)) erc-lurker-threshold-time)))) @@ -5215,42 +5215,65 @@ Specifically, return the position of `erc-insert-marker'." "Return the value of `point' at the end of the input line." (point-max)) +(defvar erc-last-input-time 0 + "Time of last call to `erc-send-current-line'. +If that function has never been called, the value is 0.") + +(defcustom erc-accidental-paste-threshold-seconds nil + "Minimum time, in seconds, before sending new lines via IRC. +If the value is a number, `erc-send-current-line' signals an +error if its previous invocation was less than this much time +ago. This is useful so that if you accidentally enter large +amounts of text into the ERC buffer, that text is not sent to the +IRC server. + +If the value is nil, `erc-send-current-line' always considers any +submitted line to be intentional." + :group 'erc + :type '(choice number (other :tag "disabled" nil))) + (defun erc-send-current-line () "Parse current line and send it to IRC." (interactive) - (save-restriction - (widen) - (if (< (point) (erc-beg-of-input-line)) - (erc-error "Point is not in the input area") - (let ((inhibit-read-only t) - (str (erc-user-input)) - (old-buf (current-buffer))) - (if (and (not (erc-server-buffer-live-p)) - (not (erc-command-no-process-p str))) - (erc-error "ERC: No process running") - (erc-set-active-buffer (current-buffer)) - - ;; Kill the input and the prompt - (delete-region (erc-beg-of-input-line) - (erc-end-of-input-line)) - - (unwind-protect - (erc-send-input str) - ;; Fix the buffer if the command didn't kill it - (when (buffer-live-p old-buf) - (with-current-buffer old-buf - (save-restriction - (widen) - (goto-char (point-max)) - (when (processp erc-server-process) - (set-marker (process-mark erc-server-process) (point))) - (set-marker erc-insert-marker (point)) - (let ((buffer-modified (buffer-modified-p))) - (erc-display-prompt) - (set-buffer-modified-p buffer-modified)))))) - - ;; Only when last hook has been run... - (run-hook-with-args 'erc-send-completed-hook str)))))) + (let ((now (float-time))) + (if (or (not erc-accidental-paste-threshold-seconds) + (< erc-accidental-paste-threshold-seconds + (- now erc-last-input-time))) + (save-restriction + (widen) + (if (< (point) (erc-beg-of-input-line)) + (erc-error "Point is not in the input area") + (let ((inhibit-read-only t) + (str (erc-user-input)) + (old-buf (current-buffer))) + (if (and (not (erc-server-buffer-live-p)) + (not (erc-command-no-process-p str))) + (erc-error "ERC: No process running") + (erc-set-active-buffer (current-buffer)) + ;; Kill the input and the prompt + (delete-region (erc-beg-of-input-line) + (erc-end-of-input-line)) + (unwind-protect + (erc-send-input str) + ;; Fix the buffer if the command didn't kill it + (when (buffer-live-p old-buf) + (with-current-buffer old-buf + (save-restriction + (widen) + (goto-char (point-max)) + (when (processp erc-server-process) + (set-marker (process-mark erc-server-process) (point))) + (set-marker erc-insert-marker (point)) + (let ((buffer-modified (buffer-modified-p))) + (erc-display-prompt) + (set-buffer-modified-p buffer-modified)))))) + + ;; Only when last hook has been run... + (run-hook-with-args 'erc-send-completed-hook str)))) + (setq erc-last-input-time now)) + (switch-to-buffer "*ERC Accidental Paste Overflow*") + (lwarn 'erc :warning + "You seem to have accidentally pasted some text!")))) (defun erc-user-input () "Return the input of the user in the current buffer." -- 2.39.5