erc-join.el 7.8 KB

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