Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[bpt/emacs.git] / lisp / erc / erc-join.el
CommitLineData
c6b99621 1;;; erc-join.el --- autojoin channels on connect and reconnects
597993cf 2
49f70d46 3;; Copyright (C) 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
597993cf
MB
4
5;; Author: Alex Schroeder <alex@gnu.org>
6;; Keywords: irc
7;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcAutoJoin
8
9;; This file is part of GNU Emacs.
10
4ee57b2a 11;; GNU Emacs is free software: you can redistribute it and/or modify
597993cf 12;; it under the terms of the GNU General Public License as published by
4ee57b2a
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
597993cf
MB
15
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.
20
21;; You should have received a copy of the GNU General Public License
4ee57b2a 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
597993cf
MB
23
24;;; Commentary:
25
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.
31
32;;; Code:
33
34(require 'erc)
8508e990 35(eval-when-compile (require 'cl))
597993cf
MB
36
37(defgroup erc-autojoin nil
38 "Enable autojoining."
39 :group 'erc)
40
c6b99621 41;;;###autoload (autoload 'erc-autojoin-mode "erc-join" nil t)
597993cf
MB
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)))
50
51(defcustom erc-autojoin-channels-alist nil
52 "Alist of channels to autojoin on IRC networks.
53Every element in the alist has the form (SERVER . CHANNELS).
54SERVER is a regexp matching the server, and channels is the
55list of channels to join.
56
57Customize this variable to set the value for your first connect.
58Once you are connected and join and part channels, this alist
59keeps track of what channels you are on, and will join them
60again when you get disconnected. When you restart Emacs, however,
61those changes are lost, and the customization you saved the last
62time is used again."
63 :group 'erc-autojoin
64 :type '(repeat (cons :tag "Server"
65 (regexp :tag "Name")
66 (repeat :tag "Channels"
67 (string :tag "Name")))))
68
69(defcustom erc-autojoin-domain-only t
70 "Truncate host name to the domain name when joining a server.
71If non-nil, and a channel on the server a.b.c is joined, then
72only b.c is used as the server for `erc-autojoin-channels-alist'.
73This is important for networks that redirect you to other
74servers, presumably in the same domain."
75 :group 'erc-autojoin
76 :type 'boolean)
77
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))))))
84
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
21bc768b 93 (string-match "[^.\n]+\\.\\([^.\n]+\\.[^.\n]+\\)$" server))
597993cf
MB
94 (setq server (match-string 1 server)))
95 (let ((elem (assoc server erc-autojoin-channels-alist)))
96 (if elem
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
103 ;; functions.
104 nil)
105
106;; (erc-parse-user "kensanata!~user@dclient217-162-233-228.hispeed.ch")
107
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
21bc768b 116 (string-match "[^.\n]+\\.\\([^.\n]+\\.[^.\n]+\\)$" server))
597993cf
MB
117 (setq server (match-string 1 server)))
118 (let ((elem (assoc server erc-autojoin-channels-alist)))
119 (when elem
120 (setcdr elem (delete chnl (cdr elem)))
121 (unless (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
125 ;; functions.
126 nil)
127
c6b99621 128(provide 'erc-join)
597993cf 129
c6b99621 130;;; erc-join.el ends here
597993cf
MB
131;;
132;; Local Variables:
133;; indent-tabs-mode: t
134;; tab-width: 8
135;; End:
136
137;; arch-tag: d62d8b15-8e31-49d6-8a73-12f11e717414