Changeset 4200 for trunk/lisp/erc/erc-backend.el
- Timestamp:
- 04/07/07 15:49:28 (2 years ago)
- Files:
-
- trunk/lisp/erc/erc-backend.el (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lisp/erc/erc-backend.el
r4190 r4200 175 175 ;;; Server and connection state 176 176 177 (defvar erc-server-ping-timer-alist nil 178 "Mapping of server buffers to their specific ping timer.") 179 177 180 (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 182 an IRC connection. 183 184 If you wish to determine whether an IRC connection is currently 185 active, use the `erc-server-process-alive' function instead.") 180 186 (make-variable-buffer-local 'erc-server-connected) 181 187 … … 188 194 (make-variable-buffer-local 'erc-server-quitting) 189 195 196 (defvar erc-server-reconnecting nil 197 "Non-nil if the user requests an explicit reconnect, and the 198 current 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 190 205 (defvar erc-server-banned nil 191 206 "Non-nil if the user is denied access because of a server ban.") 192 207 (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) 193 212 194 213 (defvar erc-server-lines-sent nil … … 210 229 This is useful for flood protection.") 211 230 (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. 234 This is useful for detecting hung connections.") 235 (make-variable-buffer-local 'erc-server-last-received-time) 212 236 213 237 (defvar erc-server-lag nil … … 388 412 ;; Ping handling 389 413 390 (defcustom erc-server-send-ping-interval 90414 (defcustom erc-server-send-ping-interval 30 391 415 "*Interval of sending pings to the server, in seconds. 392 416 If this is set to nil, pinging the server is disabled." 393 417 :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. 423 The time is in seconds. 424 425 This must be greater than or equal to the value for 426 `erc-server-send-ping-interval'. 427 428 If this is set to nil, never try to reconnect." 429 :group 'erc-server 430 :type '(choice (const :tag "Disabled" nil) 431 (integer :tag "Seconds"))) 395 432 396 433 (defvar erc-server-ping-handler nil … … 425 462 (buffer-string))) 426 463 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. 466 Additionally, 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. 487 The 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))))) 441 498 442 499 (defun erc-server-process-alive () … … 448 505 ;;;; Connecting to a server 449 506 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. 509 We will store server variables in the buffer given by BUFFER." 453 510 (let ((msg (erc-format-message 'connect ?S server ?p port))) 454 511 (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))) 475 538 (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)))) 477 542 ;; wait with script loading until we receive a confirmation (first 478 543 ;; MOTD line) … … 480 545 ;; it's a bit unclear otherwise that it's attempting to establish a 481 546 ;; connection 482 (erc-display-message nil nil (current-buffer) 483 "Opening connection..\n") 547 (erc-display-message nil nil buffer "Opening connection..\n") 484 548 (erc-login))) 485 549 … … 502 566 "The process filter for the ERC server." 503 567 (with-current-buffer (process-buffer process) 568 (setq erc-server-last-received-time (erc-current-time)) 504 569 ;; If you think this is written in a weird way - please refer to the 505 570 ;; docstring of `erc-server-processing-p' … … 530 595 "Return non-nil if ERC should attempt to reconnect automatically. 531 596 EVENT 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))))) 542 611 543 612 (defun erc-process-sentinel-1 (event) … … 563 632 (condition-case err 564 633 (progn 634 (setq erc-server-reconnecting nil) 565 635 (erc-server-reconnect) 566 636 (setq erc-server-reconnect-count 0)) … … 612 682 This is determined via `erc-encoding-coding-alist' or 613 683 `erc-server-coding-system'." 684 (unless target (setq target (erc-default-target))) 614 685 (or (when target 615 686 (let ((case-fold-search t)) … … 657 728 (erc-log (concat "erc-server-send: " string "(" (buffer-name) ")")) 658 729 (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))) 662 731 (when (consp encoding) 663 732 (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 667 735 (let ((str (concat string "\r\n"))) 668 736 (if forcep … … 904 972 'erc-default-server-functions))) 905 973 (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))))) 910 976 911 977 (add-hook 'erc-default-server-functions 'erc-handle-unknown-server-response) … … 1063 1129 (define-erc-response-handler (ERROR) 1064 1130 "Handle an ERROR command from the server." nil 1131 (setq erc-server-error-occurred t) 1065 1132 (erc-display-message 1066 1133 parsed 'error nil 'ERROR … … 1447 1514 (erc-display-server-message proc parsed)) 1448 1515 1516 (define-erc-response-handler (290) 1517 "Handle dancer-ircd CAPAB messages." nil nil) 1518 1449 1519 (define-erc-response-handler (301) 1450 1520 "AWAY notice." nil
