1 ;;; erc-join.el --- autojoin channels on connect and reconnects
3 ;; Copyright (C) 2002-2004, 2006-2012 Free Software Foundation, Inc.
5 ;; Author: Alex Schroeder <alex@gnu.org>
7 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcAutoJoin
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This allows us to customize an `erc-autojoin-channels-alist'. As
27 ;; we /JOIN and /PART channels, this alist is updated to reflect our
28 ;; current setup, so that when we reconnect, we rejoin the same
29 ;; channels. The alist can be customized, so that the customized
30 ;; value will be used when we reconnect in our next Emacs session.
35 (eval-when-compile (require 'cl
))
37 (defgroup erc-autojoin nil
41 ;;;###autoload (autoload 'erc-autojoin-mode "erc-join" nil t)
42 (define-erc-module autojoin nil
43 "Makes ERC autojoin on connects and reconnects."
44 ((add-hook 'erc-after-connect
'erc-autojoin-channels
)
45 (add-hook 'erc-nickserv-identified-hook
'erc-autojoin-after-ident
)
46 (add-hook 'erc-server-JOIN-functions
'erc-autojoin-add
)
47 (add-hook 'erc-server-PART-functions
'erc-autojoin-remove
))
48 ((remove-hook 'erc-after-connect
'erc-autojoin-channels
)
49 (remove-hook 'erc-nickserv-identified-hook
'erc-autojoin-after-ident
)
50 (remove-hook 'erc-server-JOIN-functions
'erc-autojoin-add
)
51 (remove-hook 'erc-server-PART-functions
'erc-autojoin-remove
)))
53 (defcustom erc-autojoin-channels-alist nil
54 "Alist of channels to autojoin on IRC networks.
55 Every element in the alist has the form (SERVER . CHANNELS).
56 SERVER is a regexp matching the server, and channels is the
57 list of channels to join.
59 Customize this variable to set the value for your first connect.
60 Once you are connected and join and part channels, this alist
61 keeps track of what channels you are on, and will join them
62 again when you get disconnected. When you restart Emacs, however,
63 those changes are lost, and the customization you saved the last
66 :type
'(repeat (cons :tag
"Server"
68 (repeat :tag
"Channels"
69 (string :tag
"Name")))))
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'."
79 :type
'(choice (const :tag
"On Connection" 'connect
)
80 (const :tag
"When Identified" 'ident
)))
82 (defcustom erc-autojoin-delay
30
83 "Number of seconds to wait before attempting to autojoin channels.
84 This only takes effect if `erc-autojoin-timing' is `ident'.
85 If NickServ identification occurs before this delay expires, ERC
86 autojoins immediately at that time."
91 (defcustom erc-autojoin-domain-only t
92 "Truncate host name to the domain name when joining a server.
93 If non-nil, and a channel on the server a.b.c is joined, then
94 only b.c is used as the server for `erc-autojoin-channels-alist'.
95 This is important for networks that redirect you to other
96 servers, presumably in the same domain."
100 (defvar erc--autojoin-timer nil
)
101 (make-variable-buffer-local 'erc--autojoin-timer
)
103 (defun erc-autojoin-channels-delayed (server nick buffer
)
104 "Attempt to autojoin channels.
105 This is called from a timer set up by `erc-autojoin-channels'."
106 (if erc--autojoin-timer
107 (setq erc--autojoin-timer
108 (erc-cancel-timer erc--autojoin-timer
)))
109 (with-current-buffer buffer
110 ;; Don't kick of another delayed autojoin or try to wait for
111 ;; another ident response:
112 (let ((erc-autojoin-delay -
1)
113 (erc-autojoin-timing 'connect
))
114 (erc-log "Delayed autojoin started (no ident success detected yet)")
115 (erc-autojoin-channels server nick
))))
117 (defun erc-autojoin-after-ident (network nick
)
118 "Autojoin channels in `erc-autojoin-channels-alist'.
119 This function is run from `erc-nickserv-identified-hook'."
120 (if erc--autojoin-timer
121 (setq erc--autojoin-timer
122 (erc-cancel-timer erc--autojoin-timer
)))
123 (when (eq erc-autojoin-timing
'ident
)
124 (let ((server (or erc-server-announced-name erc-session-server
))
125 (joined (mapcar (lambda (buf)
126 (with-current-buffer buf
(erc-default-target)))
127 (erc-channel-list erc-server-process
))))
128 ;; We may already be in these channels, e.g. because the
129 ;; autojoin timer went off.
130 (dolist (l erc-autojoin-channels-alist
)
131 (when (string-match (car l
) server
)
132 (dolist (chan (cdr l
))
133 (unless (erc-member-ignore-case chan joined
)
134 (erc-server-send (concat "join " chan
))))))))
137 (defun erc-autojoin-channels (server nick
)
138 "Autojoin channels in `erc-autojoin-channels-alist'."
139 (if (eq erc-autojoin-timing
'ident
)
140 ;; Prepare the delayed autojoin timer, in case ident doesn't
141 ;; happen within the allotted time limit:
142 (when (> erc-autojoin-delay
0)
143 (setq erc--autojoin-timer
144 (run-with-timer erc-autojoin-delay nil
145 'erc-autojoin-channels-delayed
146 server nick
(current-buffer))))
147 ;; `erc-autojoin-timing' is `connect':
148 (dolist (l erc-autojoin-channels-alist
)
149 (when (string-match (car l
) server
)
150 (dolist (chan (cdr l
))
151 (erc-server-send (concat "join " chan
))))))
152 ;; Return nil to avoid stomping on any other hook funcs.
155 (defun erc-autojoin-add (proc parsed
)
156 "Add the channel being joined to `erc-autojoin-channels-alist'."
157 (let* ((chnl (erc-response.contents parsed
))
158 (nick (car (erc-parse-user (erc-response.sender parsed
))))
159 (server (with-current-buffer (process-buffer proc
)
160 (or erc-server-announced-name erc-session-server
))))
161 (when (erc-current-nick-p nick
)
162 (when (and erc-autojoin-domain-only
163 (string-match "[^.\n]+\\.\\([^.\n]+\\.[^.\n]+\\)$" server
))
164 (setq server
(match-string 1 server
)))
165 (let ((elem (assoc server erc-autojoin-channels-alist
)))
167 (unless (member chnl
(cdr elem
))
168 (setcdr elem
(cons chnl
(cdr elem
))))
169 (setq erc-autojoin-channels-alist
170 (cons (list server chnl
)
171 erc-autojoin-channels-alist
))))))
172 ;; We must return nil to tell ERC to continue running the other
176 ;; (erc-parse-user "kensanata!~user@dclient217-162-233-228.hispeed.ch")
178 (defun erc-autojoin-remove (proc parsed
)
179 "Remove the channel being left from `erc-autojoin-channels-alist'."
180 (let* ((chnl (car (erc-response.command-args parsed
)))
181 (nick (car (erc-parse-user (erc-response.sender parsed
))))
182 (server (with-current-buffer (process-buffer proc
)
183 (or erc-server-announced-name erc-session-server
))))
184 (when (erc-current-nick-p nick
)
185 (when (and erc-autojoin-domain-only
186 (string-match "[^.\n]+\\.\\([^.\n]+\\.[^.\n]+\\)$" server
))
187 (setq server
(match-string 1 server
)))
188 (let ((elem (assoc server erc-autojoin-channels-alist
)))
190 (setcdr elem
(delete chnl
(cdr elem
)))
192 (setq erc-autojoin-channels-alist
193 (delete elem erc-autojoin-channels-alist
)))))))
194 ;; We must return nil to tell ERC to continue running the other
200 ;;; erc-join.el ends here
203 ;; indent-tabs-mode: t