erc-capab.el 8.4 KB

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