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