b9b85bff27eb36e64aae2a09fbce918d5fa24dc5
[bpt/emacs.git] / lisp / erc / erc-join.el
1 ;;; erc-join.el --- autojoin channels on connect and reconnects
2
3 ;; Copyright (C) 2002, 2003, 2004, 2006, 2007, 2008 Free Software Foundation, Inc.
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
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, or (at your option)
14 ;; any later version.
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
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; This allows us to customize an `erc-autojoin-channels-alist'. As
29 ;; we /JOIN and /PART channels, this alist is updated to reflect our
30 ;; current setup, so that when we reconnect, we rejoin the same
31 ;; channels. The alist can be customized, so that the customized
32 ;; value will be used when we reconnect in our next Emacs session.
33
34 ;;; Code:
35
36 (require 'erc)
37 (eval-when-compile (require 'cl))
38
39 (defgroup erc-autojoin nil
40 "Enable autojoining."
41 :group 'erc)
42
43 ;;;###autoload (autoload 'erc-autojoin-mode "erc-join" nil t)
44 (define-erc-module autojoin nil
45 "Makes ERC autojoin on connects and reconnects."
46 ((add-hook 'erc-after-connect 'erc-autojoin-channels)
47 (add-hook 'erc-server-JOIN-functions 'erc-autojoin-add)
48 (add-hook 'erc-server-PART-functions 'erc-autojoin-remove))
49 ((remove-hook 'erc-after-connect 'erc-autojoin-channels)
50 (remove-hook 'erc-server-JOIN-functions 'erc-autojoin-add)
51 (remove-hook 'erc-server-PART-functions 'erc-autojoin-remove)))
52
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.
58
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
64 time is used again."
65 :group 'erc-autojoin
66 :type '(repeat (cons :tag "Server"
67 (regexp :tag "Name")
68 (repeat :tag "Channels"
69 (string :tag "Name")))))
70
71 (defcustom erc-autojoin-domain-only t
72 "Truncate host name to the domain name when joining a server.
73 If non-nil, and a channel on the server a.b.c is joined, then
74 only b.c is used as the server for `erc-autojoin-channels-alist'.
75 This is important for networks that redirect you to other
76 servers, presumably in the same domain."
77 :group 'erc-autojoin
78 :type 'boolean)
79
80 (defun erc-autojoin-channels (server nick)
81 "Autojoin channels in `erc-autojoin-channels-alist'."
82 (dolist (l erc-autojoin-channels-alist)
83 (when (string-match (car l) server)
84 (dolist (chan (cdr l))
85 (erc-server-send (concat "join " chan))))))
86
87 (defun erc-autojoin-add (proc parsed)
88 "Add the channel being joined to `erc-autojoin-channels-alist'."
89 (let* ((chnl (erc-response.contents parsed))
90 (nick (car (erc-parse-user (erc-response.sender parsed))))
91 (server (with-current-buffer (process-buffer proc)
92 (or erc-server-announced-name erc-session-server))))
93 (when (erc-current-nick-p nick)
94 (when (and erc-autojoin-domain-only
95 (string-match "[^.\n]+\\.\\([^.\n]+\\.[^.\n]+\\)$" server))
96 (setq server (match-string 1 server)))
97 (let ((elem (assoc server erc-autojoin-channels-alist)))
98 (if elem
99 (unless (member chnl (cdr elem))
100 (setcdr elem (cons chnl (cdr elem))))
101 (setq erc-autojoin-channels-alist
102 (cons (list server chnl)
103 erc-autojoin-channels-alist))))))
104 ;; We must return nil to tell ERC to continue running the other
105 ;; functions.
106 nil)
107
108 ;; (erc-parse-user "kensanata!~user@dclient217-162-233-228.hispeed.ch")
109
110 (defun erc-autojoin-remove (proc parsed)
111 "Remove the channel being left from `erc-autojoin-channels-alist'."
112 (let* ((chnl (car (erc-response.command-args parsed)))
113 (nick (car (erc-parse-user (erc-response.sender parsed))))
114 (server (with-current-buffer (process-buffer proc)
115 (or erc-server-announced-name erc-session-server))))
116 (when (erc-current-nick-p nick)
117 (when (and erc-autojoin-domain-only
118 (string-match "[^.\n]+\\.\\([^.\n]+\\.[^.\n]+\\)$" server))
119 (setq server (match-string 1 server)))
120 (let ((elem (assoc server erc-autojoin-channels-alist)))
121 (when elem
122 (setcdr elem (delete chnl (cdr elem)))
123 (unless (cdr elem)
124 (setq erc-autojoin-channels-alist
125 (delete elem erc-autojoin-channels-alist)))))))
126 ;; We must return nil to tell ERC to continue running the other
127 ;; functions.
128 nil)
129
130 (provide 'erc-join)
131
132 ;;; erc-join.el ends here
133 ;;
134 ;; Local Variables:
135 ;; indent-tabs-mode: t
136 ;; tab-width: 8
137 ;; End:
138
139 ;; arch-tag: d62d8b15-8e31-49d6-8a73-12f11e717414