Show
Ignore:
Timestamp:
04/07/07 15:49:28 (2 years ago)
Author:
miyoshi
Message:

Sync up with Emacs CVS HEAD.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lisp/erc/erc-backend.el

    r4190 r4200  
    175175;;; Server and connection state 
    176176 
     177(defvar erc-server-ping-timer-alist nil 
     178  "Mapping of server buffers to their specific ping timer.") 
     179 
    177180(defvar erc-server-connected nil 
    178   "Non-nil if the `current-buffer' is associated with an open IRC connection. 
    179 This variable is buffer-local.") 
     181  "Non-nil if the current buffer has been used by ERC to establish 
     182an IRC connection. 
     183 
     184If you wish to determine whether an IRC connection is currently 
     185active, use the `erc-server-process-alive' function instead.") 
    180186(make-variable-buffer-local 'erc-server-connected) 
    181187 
     
    188194(make-variable-buffer-local 'erc-server-quitting) 
    189195 
     196(defvar erc-server-reconnecting nil 
     197  "Non-nil if the user requests an explicit reconnect, and the 
     198current IRC process is still alive.") 
     199(make-variable-buffer-local 'erc-server-reconnecting) 
     200 
     201(defvar erc-server-timed-out nil 
     202  "Non-nil if the IRC server failed to respond to a ping.") 
     203(make-variable-buffer-local 'erc-server-timed-out) 
     204 
    190205(defvar erc-server-banned nil 
    191206  "Non-nil if the user is denied access because of a server ban.") 
    192207(make-variable-buffer-local 'erc-server-banned) 
     208 
     209(defvar erc-server-error-occurred nil 
     210  "Non-nil if the user triggers some server error.") 
     211(make-variable-buffer-local 'erc-server-error-occurred) 
    193212 
    194213(defvar erc-server-lines-sent nil 
     
    210229This is useful for flood protection.") 
    211230(make-variable-buffer-local 'erc-server-last-ping-time) 
     231 
     232(defvar erc-server-last-received-time nil 
     233  "Time the last message was received from the server. 
     234This is useful for detecting hung connections.") 
     235(make-variable-buffer-local 'erc-server-last-received-time) 
    212236 
    213237(defvar erc-server-lag nil 
     
    388412;; Ping handling 
    389413 
    390 (defcustom erc-server-send-ping-interval 9
     414(defcustom erc-server-send-ping-interval 3
    391415  "*Interval of sending pings to the server, in seconds. 
    392416If this is set to nil, pinging the server is disabled." 
    393417  :group 'erc-server 
    394   :type '(choice (const nil) (integer :tag "Seconds"))) 
     418  :type '(choice (const :tag "Disabled" nil) 
     419                 (integer :tag "Seconds"))) 
     420 
     421(defcustom erc-server-send-ping-timeout 120 
     422  "*If the time between ping and response is greater than this, reconnect. 
     423The time is in seconds. 
     424 
     425This must be greater than or equal to the value for 
     426`erc-server-send-ping-interval'. 
     427 
     428If this is set to nil, never try to reconnect." 
     429  :group 'erc-server 
     430  :type '(choice (const :tag "Disabled" nil) 
     431                 (integer :tag "Seconds"))) 
    395432 
    396433(defvar erc-server-ping-handler nil 
     
    425462    (buffer-string))) 
    426463 
    427 (defun erc-server-setup-periodical-server-ping (&rest ignore) 
    428   "Set up a timer to periodically ping the current server." 
    429   (and erc-server-ping-handler (erc-cancel-timer erc-server-ping-handler)) 
    430   (when erc-server-send-ping-interval 
    431     (setq erc-server-ping-handler 
    432           (run-with-timer 
    433            4 erc-server-send-ping-interval 
    434            (lambda (buf) 
    435              (when (buffer-live-p buf) 
    436                (with-current-buffer buf 
    437                  (erc-server-send 
    438                   (format "PING %.0f" 
    439                           (erc-current-time)))))) 
    440            (current-buffer))))) 
     464(defun erc-server-send-ping (buf) 
     465  "Send a ping to the IRC server buffer in BUF. 
     466Additionally, detect whether the IRC process has hung." 
     467  (if (buffer-live-p buf) 
     468      (with-current-buffer buf 
     469        (if (and erc-server-send-ping-timeout 
     470                 (> 
     471                  (erc-time-diff (erc-current-time) 
     472                                 erc-server-last-received-time) 
     473                  erc-server-send-ping-timeout)) 
     474            (progn 
     475              ;; if the process is hung, kill it 
     476              (setq erc-server-timed-out t) 
     477              (delete-process erc-server-process)) 
     478          (erc-server-send (format "PING %.0f" (erc-current-time))))) 
     479    ;; remove timer if the server buffer has been killed 
     480    (let ((timer (assq buf erc-server-ping-timer-alist))) 
     481      (when timer 
     482        (erc-cancel-timer (cdr timer)) 
     483        (setcdr timer nil))))) 
     484 
     485(defun erc-server-setup-periodical-ping (buffer) 
     486  "Set up a timer to periodically ping the current server. 
     487The current buffer is given by BUFFER." 
     488  (with-current-buffer buffer 
     489    (and erc-server-ping-handler (erc-cancel-timer erc-server-ping-handler)) 
     490    (when erc-server-send-ping-interval 
     491      (setq erc-server-ping-handler (run-with-timer 
     492                                     4 erc-server-send-ping-interval 
     493                                     #'erc-server-send-ping 
     494                                     buffer)) 
     495      (setq erc-server-ping-timer-alist (cons (cons buffer 
     496                                                    erc-server-ping-handler) 
     497                                              erc-server-ping-timer-alist))))) 
    441498 
    442499(defun erc-server-process-alive () 
     
    448505;;;; Connecting to a server 
    449506 
    450 (defun erc-server-connect (server port
    451   "Perform the connection and login
    452 We will store server variables in the current buffer." 
     507(defun erc-server-connect (server port buffer
     508  "Perform the connection and login using the specified SERVER and PORT
     509We will store server variables in the buffer given by BUFFER." 
    453510  (let ((msg (erc-format-message 'connect ?S server ?p port))) 
    454511    (message "%s" msg) 
    455     (setq erc-server-process 
    456           (funcall erc-server-connect-function 
    457                    (format "erc-%s-%s" server port) 
    458                    (current-buffer) server port)) 
    459     (message "%s...done" msg)) 
    460   ;; Misc server variables 
    461   (setq erc-server-quitting nil) 
    462   (setq erc-server-banned nil) 
    463   (setq erc-server-last-sent-time (erc-current-time)) 
    464   (setq erc-server-last-ping-time (erc-current-time)) 
    465   (setq erc-server-lines-sent 0) 
    466   ;; last peers (sender and receiver) 
    467   (setq erc-server-last-peers '(nil . nil)) 
    468   ;; process handlers 
    469   (set-process-sentinel erc-server-process 'erc-process-sentinel) 
    470   (set-process-filter erc-server-process 'erc-server-filter-function) 
    471   ;; we do our own encoding and decoding 
    472   (when (fboundp 'set-process-coding-system) 
    473     (set-process-coding-system erc-server-process 'raw-text)) 
    474   (set-marker (process-mark erc-server-process) (point)) 
     512    (let ((process (funcall erc-server-connect-function 
     513                            (format "erc-%s-%s" server port) 
     514                            nil server port))) 
     515      (message "%s...done" msg) 
     516      ;; Misc server variables 
     517      (with-current-buffer buffer 
     518        (setq erc-server-process process) 
     519        (setq erc-server-quitting nil) 
     520        (setq erc-server-reconnecting nil) 
     521        (setq erc-server-timed-out nil) 
     522        (setq erc-server-banned nil) 
     523        (setq erc-server-error-occurred nil) 
     524        (let ((time (erc-current-time))) 
     525          (setq erc-server-last-sent-time time) 
     526          (setq erc-server-last-ping-time time) 
     527          (setq erc-server-last-received-time time)) 
     528        (setq erc-server-lines-sent 0) 
     529        ;; last peers (sender and receiver) 
     530        (setq erc-server-last-peers '(nil . nil))) 
     531      ;; we do our own encoding and decoding 
     532      (when (fboundp 'set-process-coding-system) 
     533        (set-process-coding-system process 'raw-text)) 
     534      ;; process handlers 
     535      (set-process-sentinel process 'erc-process-sentinel) 
     536      (set-process-filter process 'erc-server-filter-function) 
     537      (set-process-buffer process buffer))) 
    475538  (erc-log "\n\n\n********************************************\n") 
    476   (message (erc-format-message 'login ?n (erc-current-nick))) 
     539  (message (erc-format-message 
     540            'login ?n 
     541            (with-current-buffer buffer (erc-current-nick)))) 
    477542  ;; wait with script loading until we receive a confirmation (first 
    478543  ;; MOTD line) 
     
    480545      ;; it's a bit unclear otherwise that it's attempting to establish a 
    481546      ;; connection 
    482       (erc-display-message nil nil (current-buffer) 
    483                            "Opening connection..\n") 
     547      (erc-display-message nil nil buffer "Opening connection..\n") 
    484548    (erc-login))) 
    485549 
     
    502566  "The process filter for the ERC server." 
    503567  (with-current-buffer (process-buffer process) 
     568    (setq erc-server-last-received-time (erc-current-time)) 
    504569    ;; If you think this is written in a weird way - please refer to the 
    505570    ;; docstring of `erc-server-processing-p' 
     
    530595  "Return non-nil if ERC should attempt to reconnect automatically. 
    531596EVENT is the message received from the closed connection process." 
    532   (and erc-server-auto-reconnect 
    533        (not erc-server-banned) 
    534        ;; make sure we don't infinitely try to reconnect, unless the 
    535        ;; user wants that 
    536        (or (eq erc-server-reconnect-attempts t) 
    537            (and (integerp erc-server-reconnect-attempts) 
    538                 (< erc-server-reconnect-count erc-server-reconnect-attempts))) 
    539        (not (string-match "^deleted" event)) 
    540        ;; open-network-stream-nowait error for connection refused 
    541        (not (string-match "^failed with code 111" event)))) 
     597  (or erc-server-reconnecting 
     598      (and erc-server-auto-reconnect 
     599           (not erc-server-banned) 
     600           (not erc-server-error-occurred) 
     601           ;; make sure we don't infinitely try to reconnect, unless the 
     602           ;; user wants that 
     603           (or (eq erc-server-reconnect-attempts t) 
     604               (and (integerp erc-server-reconnect-attempts) 
     605                    (< erc-server-reconnect-count 
     606                       erc-server-reconnect-attempts))) 
     607           (or erc-server-timed-out 
     608               (not (string-match "^deleted" event))) 
     609           ;; open-network-stream-nowait error for connection refused 
     610           (not (string-match "^failed with code 111" event))))) 
    542611 
    543612(defun erc-process-sentinel-1 (event) 
     
    563632            (condition-case err 
    564633                (progn 
     634                  (setq erc-server-reconnecting nil) 
    565635                  (erc-server-reconnect) 
    566636                  (setq erc-server-reconnect-count 0)) 
     
    612682This is determined via `erc-encoding-coding-alist' or 
    613683`erc-server-coding-system'." 
     684  (unless target (setq target (erc-default-target))) 
    614685  (or (when target 
    615686        (let ((case-fold-search t)) 
     
    657728  (erc-log (concat "erc-server-send: " string "(" (buffer-name) ")")) 
    658729  (setq erc-server-last-sent-time (erc-current-time)) 
    659   (let ((buf (erc-server-buffer)) 
    660         (encoding (erc-coding-system-for-target 
    661                    (or target (erc-default-target))))) 
     730  (let ((encoding (erc-coding-system-for-target target))) 
    662731    (when (consp encoding) 
    663732      (setq encoding (car encoding))) 
    664     (if (and buf 
    665              (erc-server-process-alive)) 
    666         (with-current-buffer buf 
     733    (if (erc-server-process-alive) 
     734        (erc-with-server-buffer 
    667735          (let ((str (concat string "\r\n"))) 
    668736            (if forcep 
     
    904972                  'erc-default-server-functions))) 
    905973    (run-hook-with-args-until-success hook process message) 
    906     (let ((server-buffer (erc-server-buffer))) 
    907       (when (buffer-live-p server-buffer) 
    908         (with-current-buffer server-buffer 
    909           (run-hook-with-args 'erc-timer-hook (erc-current-time))))))) 
     974    (erc-with-server-buffer 
     975      (run-hook-with-args 'erc-timer-hook (erc-current-time))))) 
    910976 
    911977(add-hook 'erc-default-server-functions 'erc-handle-unknown-server-response) 
     
    10631129(define-erc-response-handler (ERROR) 
    10641130  "Handle an ERROR command from the server." nil 
     1131  (setq erc-server-error-occurred t) 
    10651132  (erc-display-message 
    10661133   parsed 'error nil 'ERROR 
     
    14471514  (erc-display-server-message proc parsed)) 
    14481515 
     1516(define-erc-response-handler (290) 
     1517  "Handle dancer-ircd CAPAB messages." nil nil) 
     1518 
    14491519(define-erc-response-handler (301) 
    14501520  "AWAY notice." nil