all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Eric Hanchrow <eric.hanchrow@gmail.com>
To: 11592@debbugs.gnu.org
Subject: bug#11592: [PATCH] prevent accidental pastes in ERC
Date: Wed, 30 May 2012 17:32:52 -0700	[thread overview]
Message-ID: <CAHZoxq-APNE_3rYbHf0shcO3=J1R+rJGM=Sv857RuxBtMLqt7g@mail.gmail.com> (raw)
In-Reply-To: <CAHZoxq-3j2ksOMRsL9UEitu3oEjk0ro1CKoOfUhcUGeZtsQv9w@mail.gmail.com>

diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el
index 2d8c256..5677445 100644
--- a/lisp/erc/erc.el
+++ b/lisp/erc/erc.el
@@ -5020,42 +5020,63 @@ 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' (as returned by `float-time'),
+or 0 if that function has never been called.
+
+Used to detect accidental pastes (i.e., large amounts of text
+accidentally entered into the ERC buffer.)")
+
+(defcustom erc-accidental-paste-threshold-seconds nil
+  "Time in seconds that must pass between invocations of
+  `erc-send-current-line' in order for that function to consider
+  the new line intentional.  If nil, erc-send-current-line always
+  considers the new 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."
--
1.7.9.5

  parent reply	other threads:[~2012-05-31  0:32 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-28 21:57 [PATCH] prevent accidental pastes in ERC Eric Hanchrow
2012-05-30  8:10 ` Julien Danjou
2012-05-31  0:32 ` Eric Hanchrow [this message]
2012-05-31 19:24   ` bug#11592: " Glenn Morris
2012-05-31 20:08     ` Deniz Dogan
2012-06-01  0:07       ` Eric Hanchrow
2012-06-02  7:39         ` Chong Yidong
     [not found]           ` <CAHZoxq8yfAYwp_LH0nMnsw-QAg9pX9AVEnLCXos7N0yTgAVeDQ@mail.gmail.com>
2012-06-10 22:04             ` bug#11592: Fwd: " Eric Hanchrow

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAHZoxq-APNE_3rYbHf0shcO3=J1R+rJGM=Sv857RuxBtMLqt7g@mail.gmail.com' \
    --to=eric.hanchrow@gmail.com \
    --cc=11592@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.