comparison lisp/erc/erc-join.el @ 109784:2abe46caa971

Allow delayed autojoin in ERC (Bug#5521). * erc/erc-join.el (erc-autojoin-timing, erc-autojoin-delay): New vars. (erc-autojoin-channels-delayed, erc-autojoin-after-ident): New functions. (erc-autojoin-channels): Allow autojoining after ident (Bug#5521).
author Chong Yidong <cyd@stupidchicken.com>
date Sat, 14 Aug 2010 18:58:10 -0400
parents 1d1d5d9bd884
children 417b1e4d63cd
comparison
equal deleted inserted replaced
109783:43f98127e0f3 109784:2abe46caa971
40 40
41 ;;;###autoload (autoload 'erc-autojoin-mode "erc-join" nil t) 41 ;;;###autoload (autoload 'erc-autojoin-mode "erc-join" nil t)
42 (define-erc-module autojoin nil 42 (define-erc-module autojoin nil
43 "Makes ERC autojoin on connects and reconnects." 43 "Makes ERC autojoin on connects and reconnects."
44 ((add-hook 'erc-after-connect 'erc-autojoin-channels) 44 ((add-hook 'erc-after-connect 'erc-autojoin-channels)
45 (add-hook 'erc-nickserv-identified-hook 'erc-autojoin-after-ident)
45 (add-hook 'erc-server-JOIN-functions 'erc-autojoin-add) 46 (add-hook 'erc-server-JOIN-functions 'erc-autojoin-add)
46 (add-hook 'erc-server-PART-functions 'erc-autojoin-remove)) 47 (add-hook 'erc-server-PART-functions 'erc-autojoin-remove))
47 ((remove-hook 'erc-after-connect 'erc-autojoin-channels) 48 ((remove-hook 'erc-after-connect 'erc-autojoin-channels)
49 (remove-hook 'erc-nickserv-identified-hook 'erc-autojoin-after-ident)
48 (remove-hook 'erc-server-JOIN-functions 'erc-autojoin-add) 50 (remove-hook 'erc-server-JOIN-functions 'erc-autojoin-add)
49 (remove-hook 'erc-server-PART-functions 'erc-autojoin-remove))) 51 (remove-hook 'erc-server-PART-functions 'erc-autojoin-remove)))
50 52
51 (defcustom erc-autojoin-channels-alist nil 53 (defcustom erc-autojoin-channels-alist nil
52 "Alist of channels to autojoin on IRC networks. 54 "Alist of channels to autojoin on IRC networks.
64 :type '(repeat (cons :tag "Server" 66 :type '(repeat (cons :tag "Server"
65 (regexp :tag "Name") 67 (regexp :tag "Name")
66 (repeat :tag "Channels" 68 (repeat :tag "Channels"
67 (string :tag "Name"))))) 69 (string :tag "Name")))))
68 70
71 (defcustom erc-autojoin-timing 'connect
72 "When ERC should attempt to autojoin a channel.
73 If the value is `connect', autojoin immediately on connecting.
74 If the value is `ident', autojoin after successful NickServ
75 identification, or after `erc-autojoin-delay' seconds.
76 Any other value means the same as `connect'."
77 :group 'erc-autojoin
78 :type '(choice (const :tag "On Connection" 'connect)
79 (const :tag "When Identified" 'ident)))
80
81 (defcustom erc-autojoin-delay 30
82 "Number of seconds to wait before attempting to autojoin channels.
83 This only takes effect if `erc-autojoin-timing' is `ident'.
84 If NickServ identification occurs before this delay expires, ERC
85 autojoins immediately at that time."
86 :group 'erc-autojoin
87 :type 'integer)
88
69 (defcustom erc-autojoin-domain-only t 89 (defcustom erc-autojoin-domain-only t
70 "Truncate host name to the domain name when joining a server. 90 "Truncate host name to the domain name when joining a server.
71 If non-nil, and a channel on the server a.b.c is joined, then 91 If non-nil, and a channel on the server a.b.c is joined, then
72 only b.c is used as the server for `erc-autojoin-channels-alist'. 92 only b.c is used as the server for `erc-autojoin-channels-alist'.
73 This is important for networks that redirect you to other 93 This is important for networks that redirect you to other
74 servers, presumably in the same domain." 94 servers, presumably in the same domain."
75 :group 'erc-autojoin 95 :group 'erc-autojoin
76 :type 'boolean) 96 :type 'boolean)
77 97
98 (defvar erc--autojoin-timer nil)
99 (make-variable-buffer-local 'erc--autojoin-timer)
100
101 (defun erc-autojoin-channels-delayed (server nick buffer)
102 "Attempt to autojoin channels.
103 This is called from a timer set up by `erc-autojoin-channels'."
104 (if erc--autojoin-timer
105 (setq erc--autojoin-timer
106 (erc-cancel-timer erc--autojoin-timer)))
107 (with-current-buffer buffer
108 ;; Don't kick of another delayed autojoin or try to wait for
109 ;; another ident response:
110 (let ((erc-autojoin-delay -1)
111 (erc-autojoin-timing 'connect))
112 (erc-log "Delayed autojoin started (no ident success detected yet)")
113 (erc-autojoin-channels server nick))))
114
115 (defun erc-autojoin-after-ident (network nick)
116 "Autojoin channels in `erc-autojoin-channels-alist'.
117 This function is run from `erc-nickserv-identified-hook'."
118 (if erc--autojoin-timer
119 (setq erc--autojoin-timer
120 (erc-cancel-timer erc--autojoin-timer)))
121 (when (eq erc-autojoin-timing 'ident)
122 (let ((server (or erc-server-announced-name erc-session-server))
123 (joined (mapcar (lambda (buf)
124 (with-current-buffer buf (erc-default-target)))
125 (erc-channel-list erc-server-process))))
126 ;; We may already be in these channels, e.g. because the
127 ;; autojoin timer went off.
128 (dolist (l erc-autojoin-channels-alist)
129 (when (string-match (car l) server)
130 (dolist (chan (cdr l))
131 (unless (erc-member-ignore-case chan joined)
132 (erc-server-send (concat "join " chan))))))))
133 nil)
134
78 (defun erc-autojoin-channels (server nick) 135 (defun erc-autojoin-channels (server nick)
79 "Autojoin channels in `erc-autojoin-channels-alist'." 136 "Autojoin channels in `erc-autojoin-channels-alist'."
80 (dolist (l erc-autojoin-channels-alist) 137 (if (eq erc-autojoin-timing 'ident)
81 (when (string-match (car l) server) 138 ;; Prepare the delayed autojoin timer, in case ident doesn't
82 (dolist (chan (cdr l)) 139 ;; happen within the allotted time limit:
83 (erc-server-send (concat "join " chan)))))) 140 (when (> erc-autojoin-delay 0)
141 (setq erc--autojoin-timer
142 (run-with-timer erc-autojoin-delay nil
143 'erc-autojoin-channels-delayed
144 server nick (current-buffer))))
145 ;; `erc-autojoin-timing' is `connect':
146 (dolist (l erc-autojoin-channels-alist)
147 (when (string-match (car l) server)
148 (dolist (chan (cdr l))
149 (erc-server-send (concat "join " chan))))))
150 ;; Return nil to avoid stomping on any other hook funcs.
151 nil)
84 152
85 (defun erc-autojoin-add (proc parsed) 153 (defun erc-autojoin-add (proc parsed)
86 "Add the channel being joined to `erc-autojoin-channels-alist'." 154 "Add the channel being joined to `erc-autojoin-channels-alist'."
87 (let* ((chnl (erc-response.contents parsed)) 155 (let* ((chnl (erc-response.contents parsed))
88 (nick (car (erc-parse-user (erc-response.sender parsed)))) 156 (nick (car (erc-parse-user (erc-response.sender parsed))))