From: ueno Date: Sat, 25 Sep 2004 03:58:17 +0000 (+0000) Subject: * riece-server.el: Implement flood protection. X-Git-Tag: riece-1_0_4~17 X-Git-Url: http://git.chise.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=19c03d25125271b325d6f133f1722f0bc738d2a5;p=elisp%2Friece.git * riece-server.el: Implement flood protection. (riece-flush-send-queue): New function. (riece-process-send-string): Use it. * riece-options.el (riece-max-send-size): New user option. * riece-globals.el (riece-send-queue): New variable. --- diff --git a/lisp/riece-globals.el b/lisp/riece-globals.el index ecf6b92..6c18318 100644 --- a/lisp/riece-globals.el +++ b/lisp/riece-globals.el @@ -108,6 +108,9 @@ Local to the server buffers.") (defvar riece-read-point nil "Point at the last input was seen. Local to the server buffers.") +(defvar riece-send-queue nil + "Send queue for avoiding client flood. +Local to the server buffers.") (defvar riece-obarray nil "Namespace of the IRC world. Local to the server buffers.") diff --git a/lisp/riece-options.el b/lisp/riece-options.el index 7fc2e48..f73e2b7 100644 --- a/lisp/riece-options.el +++ b/lisp/riece-options.el @@ -178,6 +178,11 @@ way is to put Riece variables on .emacs or file loaded from there." :type 'symbol :group 'riece-server) +(defcustom riece-max-send-size 1500 + "Maximum size of messages to be sent at a time." + :type 'integer + :group 'riece-server) + (defcustom riece-default-password (getenv "IRCPASSWORD") "Your password." :type '(radio (string :tag "Password") diff --git a/lisp/riece-server.el b/lisp/riece-server.el index 63851f7..6bdecfa 100644 --- a/lisp/riece-server.el +++ b/lisp/riece-server.el @@ -105,9 +105,35 @@ the `riece-server-keyword-map' variable." (put 'riece-with-server-buffer 'lisp-indent-function 1) (put 'riece-with-server-buffer 'edebug-form-spec '(form body)) +(defun riece-flush-send-queue (process) + (with-current-buffer (process-buffer process) + (let ((length 0) + (total 0) + string) + (while (and riece-send-queue + (< total riece-max-send-size)) + (setq string (riece-encode-coding-string (car riece-send-queue)) + length (length string)) + (if (> length riece-max-send-size) + (message "Long message (%d > %d)" length riece-max-send-size) + (process-send-string process string) + (setq total (+ total length))) + (setq riece-send-queue (cdr riece-send-queue))) + (if riece-send-queue + (progn + (if riece-debug + (message "%d bytes sent, %d bytes left" + total (apply #'+ (mapcar #'length riece-send-queue)))) + ;; schedule next send after a second + (riece-run-at-time 1 nil + #'riece-flush-send-queue process)) + (if riece-debug + (message "%d bytes sent" total)))))) + (defun riece-process-send-string (process string) (with-current-buffer (process-buffer process) - (process-send-string process (riece-encode-coding-string string)))) + (setq riece-send-queue (nconc riece-send-queue (list string)))) + (riece-flush-send-queue process)) (defun riece-current-server-name () (or riece-overriding-server-name @@ -178,6 +204,7 @@ the `riece-server-keyword-map' variable." (make-local-variable 'riece-channel-filter) (make-local-variable 'riece-server-name) (make-local-variable 'riece-read-point) + (make-local-variable 'riece-send-queue) (setq riece-read-point (point-min)) (make-local-variable 'riece-obarray) (setq riece-obarray (make-vector riece-obarray-size 0))