* riece-server.el: Implement flood protection.
authorDaiki Ueno <ueno@unixuser.org>
Sat, 25 Sep 2004 03:58:17 +0000 (03:58 +0000)
committerDaiki Ueno <ueno@unixuser.org>
Sat, 25 Sep 2004 03:58:17 +0000 (03:58 +0000)
(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.

lisp/riece-globals.el
lisp/riece-options.el
lisp/riece-server.el

index ecf6b92..6c18318 100644 (file)
@@ -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-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.")
 (defvar riece-obarray nil
   "Namespace of the IRC world.
 Local to the server buffers.")
index 7fc2e48..f73e2b7 100644 (file)
@@ -178,6 +178,11 @@ way is to put Riece variables on .emacs or file loaded from there."
   :type 'symbol
   :group 'riece-server)
 
   :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")
 (defcustom riece-default-password (getenv "IRCPASSWORD")
   "Your password."
   :type '(radio (string :tag "Password")
index 63851f7..6bdecfa 100644 (file)
@@ -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))
 
 (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)
 (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
 
 (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-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))
     (setq riece-read-point (point-min))
     (make-local-variable 'riece-obarray)
     (setq riece-obarray (make-vector riece-obarray-size 0))