1 ;;; erc-join.el --- autojoin channels on connect and reconnects
3 ;; Copyright (C) 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011, 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-server-JOIN-functions
'erc-autojoin-add
)
46 (add-hook 'erc-server-PART-functions
'erc-autojoin-remove
))
47 ((remove-hook 'erc-after-connect
'erc-autojoin-channels
)
48 (remove-hook 'erc-server-JOIN-functions
'erc-autojoin-add
)
49 (remove-hook 'erc-server-PART-functions
'erc-autojoin-remove
)))
51 (defcustom erc-autojoin-channels-alist nil
52 "Alist of channels to autojoin on IRC networks.
53 Every element in the alist has the form (SERVER . CHANNELS).
54 SERVER is a regexp matching the server, and channels is the
55 list of channels to join.
57 Customize this variable to set the value for your first connect.
58 Once you are connected and join and part channels, this alist
59 keeps track of what channels you are on, and will join them
60 again when you get disconnected. When you restart Emacs, however,
61 those changes are lost, and the customization you saved the last
64 :type
'(repeat (cons :tag
"Server"
66 (repeat :tag
"Channels"
67 (string :tag
"Name")))))
69 (defcustom erc-autojoin-domain-only t
70 "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
72 only b.c is used as the server for `erc-autojoin-channels-alist'.
73 This is important for networks that redirect you to other
74 servers, presumably in the same domain."
78 (defun erc-autojoin-channels (server nick
)
79 "Autojoin channels in `erc-autojoin-channels-alist'."
80 (dolist (l erc-autojoin-channels-alist
)
81 (when (string-match (car l
) server
)
82 (dolist (chan (cdr l
))
83 (erc-server-send (concat "join " chan
))))))
85 (defun erc-autojoin-add (proc parsed
)
86 "Add the channel being joined to `erc-autojoin-channels-alist'."
87 (let* ((chnl (erc-response.contents parsed
))
88 (nick (car (erc-parse-user (erc-response.sender parsed
))))
89 (server (with-current-buffer (process-buffer proc
)
90 (or erc-server-announced-name erc-session-server
))))
91 (when (erc-current-nick-p nick
)
92 (when (and erc-autojoin-domain-only
93 (string-match "[^.\n]+\\.\\([^.\n]+\\.[^.\n]+\\)$" server
))
94 (setq server
(match-string 1 server
)))
95 (let ((elem (assoc server erc-autojoin-channels-alist
)))
97 (unless (member chnl
(cdr elem
))
98 (setcdr elem
(cons chnl
(cdr elem
))))
99 (setq erc-autojoin-channels-alist
100 (cons (list server chnl
)
101 erc-autojoin-channels-alist
))))))
102 ;; We must return nil to tell ERC to continue running the other
106 ;; (erc-parse-user "kensanata!~user@dclient217-162-233-228.hispeed.ch")
108 (defun erc-autojoin-remove (proc parsed
)
109 "Remove the channel being left from `erc-autojoin-channels-alist'."
110 (let* ((chnl (car (erc-response.command-args parsed
)))
111 (nick (car (erc-parse-user (erc-response.sender parsed
))))
112 (server (with-current-buffer (process-buffer proc
)
113 (or erc-server-announced-name erc-session-server
))))
114 (when (erc-current-nick-p nick
)
115 (when (and erc-autojoin-domain-only
116 (string-match "[^.\n]+\\.\\([^.\n]+\\.[^.\n]+\\)$" server
))
117 (setq server
(match-string 1 server
)))
118 (let ((elem (assoc server erc-autojoin-channels-alist
)))
120 (setcdr elem
(delete chnl
(cdr elem
)))
122 (setq erc-autojoin-channels-alist
123 (delete elem erc-autojoin-channels-alist
)))))))
124 ;; We must return nil to tell ERC to continue running the other
130 ;;; erc-join.el ends here
133 ;; indent-tabs-mode: t
137 ;; arch-tag: d62d8b15-8e31-49d6-8a73-12f11e717414