* lisp/erc/erc-log.el (erc-generate-log-file-name-function): Clarify tags
[bpt/emacs.git] / lisp / erc / erc-capab.el
CommitLineData
6904f7fe
MB
1;;; erc-capab.el --- support for dancer-ircd and hyperion's CAPAB
2
acaf905b 3;; Copyright (C) 2006-2012 Free Software Foundation, Inc.
6904f7fe 4
df5d5f59
GM
5;; Maintainer: FSF
6
7; This file is part of GNU Emacs.
8
4ee57b2a 9;; GNU Emacs is free software: you can redistribute it and/or modify
6904f7fe 10;; it under the terms of the GNU General Public License as published by
4ee57b2a
GM
11;; the Free Software Foundation, either version 3 of the License, or
12;; (at your option) any later version.
6904f7fe
MB
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
4ee57b2a 20;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
6904f7fe
MB
21
22;;; Commentary:
23
24;; This file defines the ERC module `erc-capab-identify', which allows
25;; flagging of unidentified users on servers running dancer-ircd or
26;; hyperion. freenode.net supports this capability, for example.
27
28;; With CAPAB IDENTIFY-MSG and IDENTIFY-CTCP enabled, messages from
29;; users who have identified themselves to NickServ will have a plus
30;; sign and messages from unidentified users will have a minus sign
31;; added as a prefix. Note that it is not necessary for your nickname
32;; to be identified in order to receive these marked messages.
33
34;; The plus or minus sign is removed from the message, and a prefix,
35;; `erc-capab-identify-prefix', is inserted in the front of the user's
36;; nickname if the nickname is not identified.
37
38;; Please note that once this has been enabled on a server, there is no
39;; way to tell the server to stop sending marked messages. If you
40;; disable this module, it will continue removing message flags, but the
41;; unidentified nickname prefix will not be added to messages.
42
43;; Visit <http://freenode.net/faq.shtml#spoofing> and
44;; <http://freenode.net/faq.shtml#registering> to find further
45;; explanations of this capability.
46
47;; From freenode.net's web site (not there anymore) on how to mark
48;; unidentified users:
49;; "We recommend that you add an asterisk before the nick, and
6772c8e1 50;; optionally either highlight or colorize the line in some
6904f7fe
MB
51;; appropriate fashion, if the user is not identified."
52
53;;; Usage:
54
865fe16f 55;; Put the following in your init file.
6904f7fe
MB
56
57;; (require 'erc-capab)
58;; (erc-capab-identify-mode 1)
59
60;; `erc-capab-identify-prefix' will now be added to the beginning of
ff59d266
MB
61;; unidentified users' nicknames. The default is an asterisk, "*".
62;; You can customize the prefix and the face used to display it,
63;; `erc-capab-identify-unidentified'. If the value of
64;; `erc-capab-identify-prefix' is nil or you disable this module (see
6904f7fe
MB
65;; `erc-capab-identify-disable'), no prefix will be inserted, but the
66;; flag sent by the server will still be stripped.
67
68;;; Code:
69
70(require 'erc)
71(eval-when-compile (require 'cl))
72
73;;; Customization:
74
75(defgroup erc-capab nil
76 "Support for dancer-ircd's CAPAB settings."
77 :group 'erc)
78
79(defcustom erc-capab-identify-prefix "*"
ff59d266
MB
80 "The prefix used for unidentified users.
81
82If you change this from the default \"*\", be sure to use a
83character not found in IRC nicknames to avoid confusion."
6904f7fe
MB
84 :group 'erc-capab
85 :type '(choice string (const nil)))
86
ff59d266
MB
87(defface erc-capab-identify-unidentified '((t)) ; same as `erc-default-face'
88 "Face to use for `erc-capab-identify-prefix'."
89 :group 'erc-capab
90 :group 'erc-faces)
6904f7fe 91
ff59d266 92;;; Define module:
6904f7fe
MB
93
94;;;###autoload (autoload 'erc-capab-identify-mode "erc-capab" nil t)
95(define-erc-module capab-identify nil
96 "Handle dancer-ircd's CAPAB IDENTIFY-MSG and IDENTIFY-CTCP."
97 ;; append so that `erc-server-parameters' is already set by `erc-server-005'
98 ((add-hook 'erc-server-005-functions 'erc-capab-identify-setup t)
99 (add-hook 'erc-server-290-functions 'erc-capab-identify-activate)
100 (add-hook 'erc-server-PRIVMSG-functions
101 'erc-capab-identify-remove/set-identified-flag)
102 (add-hook 'erc-server-NOTICE-functions
103 'erc-capab-identify-remove/set-identified-flag)
104 (add-hook 'erc-insert-modify-hook 'erc-capab-identify-add-prefix t)
105 (mapc (lambda (buffer)
106 (when buffer
107 (with-current-buffer buffer (erc-capab-identify-setup))))
108 (erc-buffer-list 'erc-open-server-buffer-p)))
109 ((remove-hook 'erc-server-005-functions 'erc-capab-identify-setup)
110 (remove-hook 'erc-server-290-functions 'erc-capab-identify-activate)
111 ;; we don't remove the `erc-capab-identify-remove/set-identified-flag' hooks
112 ;; because there doesn't seem to be a way to tell the server to turn it off
113 (remove-hook 'erc-insert-modify-hook 'erc-capab-identify-add-prefix)))
114
115;;; Variables:
116
117(defvar erc-capab-identify-activated nil
118 "CAPAB IDENTIFY-MSG has been activated.")
119(make-variable-buffer-local 'erc-capab-identify-activated)
120
121(defvar erc-capab-identify-sent nil
122 "CAPAB IDENTIFY-MSG and IDENTIFY-CTCP messages have been sent.")
123(make-variable-buffer-local 'erc-capab-identify-sent)
124
125;;; Functions:
126
127(defun erc-capab-identify-setup (&optional proc parsed)
128 "Set up CAPAB IDENTIFY on the current server.
129
130Optional argument PROC is the current server's process.
131Optional argument PARSED is the current message, a response struct.
132
133These arguments are sent to this function when called as a hook in
134`erc-server-005-functions'."
135 (unless erc-capab-identify-sent
ff59d266 136 (erc-capab-identify-send-messages)))
6904f7fe 137
ff59d266 138(defun erc-capab-identify-send-messages ()
6904f7fe
MB
139 "Send CAPAB IDENTIFY messages if the server supports it."
140 (when (and (stringp erc-server-version)
141 (string-match "^\\(dancer-ircd\\|hyperion\\)" erc-server-version)
142 ;; could possibly check for '("IRCD" . "dancer") in
143 ;; `erc-server-parameters' instead of looking for a specific name
144 ;; in `erc-server-version'
145 (assoc "CAPAB" erc-server-parameters))
146 (erc-log "Sending CAPAB IDENTIFY-MSG and IDENTIFY-CTCP")
147 (erc-server-send "CAPAB IDENTIFY-MSG")
148 (erc-server-send "CAPAB IDENTIFY-CTCP")
149 (setq erc-capab-identify-sent t)))
150
151
152(defun erc-capab-identify-activate (proc parsed)
153 "Set `erc-capab-identify-activated' and display an activation message.
154
155PROC is the current server's process.
156PARSED is an `erc-parsed' response struct."
157 (when (or (string= "IDENTIFY-MSG" (erc-response.contents parsed))
158 (string= "IDENTIFY-CTCP" (erc-response.contents parsed)))
159 (setq erc-capab-identify-activated t)
160 (erc-display-message
161 parsed 'notice 'active (format "%s activated"
162 (erc-response.contents parsed)))))
163
164(defun erc-capab-identify-remove/set-identified-flag (proc parsed)
165 "Remove PARSED message's id flag and add the `erc-identified' text property.
166
167PROC is the current server's process.
168PARSED is an `erc-parsed' response struct."
169 (let ((msg (erc-response.contents parsed)))
170 (when (and erc-capab-identify-activated
171 (string-match "^\\([-\\+]\\)\\(.+\\)$" msg))
172 (setf (erc-response.contents parsed)
173 (if erc-capab-identify-mode
174 (erc-propertize (match-string 2 msg)
175 'erc-identified
176 (if (string= (match-string 1 msg) "+")
177 1
178 0))
179 (match-string 2 msg)))
180 nil)))
181
182(defun erc-capab-identify-add-prefix ()
183 "Add `erc-capab-identify-prefix' to nickname if user is unidentified."
184 (when (and erc-capab-identify-prefix
185 (erc-with-server-buffer erc-capab-identify-activated))
186 (goto-char (or (erc-find-parsed-property) (point-min)))
ff59d266 187 (let ((nickname (erc-capab-identify-get-unidentified-nickname
6904f7fe
MB
188 (erc-get-parsed-vector (point)))))
189 (when (and nickname
190 (goto-char (point-min))
191 ;; assuming the first use of `nickname' is the sender's nick
192 (re-search-forward (regexp-quote nickname) nil t))
193 (goto-char (match-beginning 0))
194 (insert (erc-propertize erc-capab-identify-prefix
ff59d266 195 'face 'erc-capab-identify-unidentified))))))
6904f7fe 196
ff59d266 197(defun erc-capab-identify-get-unidentified-nickname (parsed)
6904f7fe
MB
198 "Return the nickname of the user if unidentified.
199PARSED is an `erc-parsed' response struct."
200 (when (and (erc-response-p parsed)
201 (equal 0 (get-text-property 0 'erc-identified
202 (erc-response.contents parsed))))
203 (let ((nickuserhost (erc-get-parsed-vector-nick parsed)))
204 (when nickuserhost
205 (nth 0 (erc-parse-user nickuserhost))))))
206
207(provide 'erc-capab)
208
6904f7fe 209;;; erc-capab.el ends here