Initial Commit
[packages] / xemacs-packages / erc / erc-autojoin.el
1 ;;; erc-autojoin.el --- autojoin channels on connect and reconnects
2
3 ;; Copyright (C) 2002, 2003, 2004 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 2, 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 (defconst erc-autojoin-version "$Revision: 1.15.2.2 $"
40   "ERC autojoin revision.")
41
42 (defgroup erc-autojoin nil
43   "Enable autojoining."
44   :group 'erc)
45
46 ;;;###autoload (autoload 'erc-autojoin-mode "erc-autojoin" nil t)
47 (define-erc-module autojoin nil
48   "Makes ERC autojoin on connects and reconnects."
49   ((add-hook 'erc-after-connect 'erc-autojoin-channels)
50    (add-hook 'erc-server-JOIN-functions 'erc-autojoin-add)
51    (add-hook 'erc-server-PART-functions 'erc-autojoin-remove))
52   ((remove-hook 'erc-after-connect 'erc-autojoin-channels)
53    (remove-hook 'erc-server-JOIN-functions 'erc-autojoin-add)
54    (remove-hook 'erc-server-PART-functions 'erc-autojoin-remove)))
55
56 (defcustom erc-autojoin-channels-alist nil
57   "Alist of channels to autojoin on IRC networks.
58 Every element in the alist has the form (SERVER . CHANNELS).
59 SERVER is a regexp matching the server, and channels is the
60 list of channels to join.
61
62 Customize this variable to set the value for your first connect.
63 Once you are connected and join and part channels, this alist
64 keeps track of what channels you are on, and will join them
65 again when you get disconnected.  When you restart Emacs, however,
66 those changes are lost, and the customization you saved the last
67 time is used again."
68   :group 'erc-autojoin
69   :type '(repeat (cons :tag "Server"
70                        (regexp :tag "Name")
71                        (repeat :tag "Channels"
72                                (string :tag "Name")))))
73
74 (defcustom erc-autojoin-domain-only t
75   "Truncate host name to the domain name when joining a server.
76 If non-nil, and a channel on the server a.b.c is joined, then
77 only b.c is used as the server for `erc-autojoin-channels-alist'.
78 This is important for networks that redirect you to other
79 servers, presumably in the same domain."
80   :group 'erc-autojoin
81   :type 'boolean)
82
83 (defun erc-autojoin-channels (server nick)
84   "Autojoin channels in `erc-autojoin-channels-alist'."
85   (dolist (l erc-autojoin-channels-alist)
86     (when (string-match (car l) server)
87       (dolist (chan (cdr l))
88         (erc-server-send (concat "join " chan))))))
89
90 (defun erc-autojoin-add (proc parsed)
91   "Add the channel being joined to `erc-autojoin-channels-alist'."
92   (let* ((chnl (erc-response.contents parsed))
93          (nick (car (erc-parse-user (erc-response.sender parsed))))
94          (server (with-current-buffer (process-buffer proc)
95                    (or erc-server-announced-name erc-session-server))))
96     (when (erc-current-nick-p nick)
97       (when (and erc-autojoin-domain-only
98                  (string-match "[^.\n]+\\.\\([^.\n]+\\.[^.\n]+\\)$" server))
99         (setq server (match-string 1 server)))
100       (let ((elem (assoc server erc-autojoin-channels-alist)))
101         (if elem
102             (unless (member chnl (cdr elem))
103               (setcdr elem (cons chnl (cdr elem))))
104           (setq erc-autojoin-channels-alist
105                 (cons (list server chnl)
106                       erc-autojoin-channels-alist))))))
107   ;; We must return nil to tell ERC to continue running the other
108   ;; functions.
109   nil)
110
111 ;; (erc-parse-user "kensanata!~user@dclient217-162-233-228.hispeed.ch")
112
113 (defun erc-autojoin-remove (proc parsed)
114   "Remove the channel being left from `erc-autojoin-channels-alist'."
115   (let* ((chnl (car (erc-response.command-args parsed)))
116          (nick (car (erc-parse-user (erc-response.sender parsed))))
117          (server (with-current-buffer (process-buffer proc)
118                    (or erc-server-announced-name erc-session-server))))
119     (when (erc-current-nick-p nick)
120       (when (and erc-autojoin-domain-only
121                  (string-match "[^.\n]+\\.\\([^.\n]+\\.[^.\n]+\\)$" server))
122         (setq server (match-string 1 server)))
123       (let ((elem (assoc server erc-autojoin-channels-alist)))
124         (when elem
125           (setcdr elem (delete chnl (cdr elem)))
126           (unless (cdr elem)
127             (setq erc-autojoin-channels-alist
128                   (delete elem erc-autojoin-channels-alist)))))))
129   ;; We must return nil to tell ERC to continue running the other
130   ;; functions.
131   nil)
132
133 (provide 'erc-autojoin)
134
135 ;;; erc-autojoin.el ends here
136 ;;
137 ;; Local Variables:
138 ;; indent-tabs-mode: t
139 ;; tab-width: 8
140 ;; End:
141
142 ;; arch-tag: d62d8b15-8e31-49d6-8a73-12f11e717414