1 ;;; erc-capab.el --- support for dancer-ircd and hyperion's CAPAB
3 ;; Copyright (C) 2006-2013 Free Software Foundation, Inc.
7 ; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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.
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.
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.
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.
43 ;; Visit <http://freenode.net/faq.shtml#spoofing> and
44 ;; <http://freenode.net/faq.shtml#registering> to find further
45 ;; explanations of this capability.
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
50 ;; optionally either highlight or colorize the line in some
51 ;; appropriate fashion, if the user is not identified."
55 ;; Put the following in your init file.
57 ;; (require 'erc-capab)
58 ;; (erc-capab-identify-mode 1)
60 ;; `erc-capab-identify-prefix' will now be added to the beginning of
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
65 ;; `erc-capab-identify-disable'), no prefix will be inserted, but the
66 ;; flag sent by the server will still be stripped.
71 (eval-when-compile (require 'cl
))
75 (defgroup erc-capab nil
76 "Support for dancer-ircd's CAPAB settings."
79 (defcustom erc-capab-identify-prefix
"*"
80 "The prefix used for unidentified users.
82 If you change this from the default \"*\", be sure to use a
83 character not found in IRC nicknames to avoid confusion."
85 :type
'(choice string
(const nil
)))
87 (defface erc-capab-identify-unidentified
'((t)) ; same as `erc-default-face'
88 "Face to use for `erc-capab-identify-prefix'."
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)
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
)))
117 (defvar erc-capab-identify-activated nil
118 "CAPAB IDENTIFY-MSG has been activated.")
119 (make-variable-buffer-local 'erc-capab-identify-activated
)
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
)
127 (defun erc-capab-identify-setup (&optional proc parsed
)
128 "Set up CAPAB IDENTIFY on the current server.
130 Optional argument PROC is the current server's process.
131 Optional argument PARSED is the current message, a response struct.
133 These arguments are sent to this function when called as a hook in
134 `erc-server-005-functions'."
135 (unless erc-capab-identify-sent
136 (erc-capab-identify-send-messages)))
138 (defun erc-capab-identify-send-messages ()
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
)))
152 (defun erc-capab-identify-activate (proc parsed
)
153 "Set `erc-capab-identify-activated' and display an activation message.
155 PROC is the current server's process.
156 PARSED 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
)
161 parsed
'notice
'active
(format "%s activated"
162 (erc-response.contents parsed
)))))
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.
167 PROC is the current server's process.
168 PARSED 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
)
176 (if (string= (match-string 1 msg
) "+")
179 (match-string 2 msg
)))
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)))
187 (let ((nickname (erc-capab-identify-get-unidentified-nickname
188 (erc-get-parsed-vector (point)))))
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
195 'face
'erc-capab-identify-unidentified
))))))
197 (defun erc-capab-identify-get-unidentified-nickname (parsed)
198 "Return the nickname of the user if unidentified.
199 PARSED 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
)))
205 (nth 0 (erc-parse-user nickuserhost
))))))
209 ;;; erc-capab.el ends here