erc-page.el 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ;; erc-page.el - CTCP PAGE support for ERC
  2. ;; Copyright (C) 2002, 2004, 2006-2012 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is free software: you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; GNU Emacs is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Commentary:
  15. ;; Requiring this file will make ERC react to CTCP PAGE messages
  16. ;; received, and it will provide a new /PAGE command to send such
  17. ;; messages yourself. To enable it, customize the variable
  18. ;; `erc-page-mode'.
  19. ;;; Code:
  20. (require 'erc)
  21. ;;;###autoload (autoload 'erc-page-mode "erc-page")
  22. (define-erc-module page ctcp-page
  23. "Process CTCP PAGE requests from IRC."
  24. nil nil)
  25. (erc-define-catalog-entry 'english 'CTCP-PAGE "Page from %n (%u@%h): %m")
  26. (defgroup erc-page nil
  27. "React to CTCP PAGE messages."
  28. :group 'erc)
  29. (defcustom erc-page-function nil
  30. "A function to process a \"page\" request.
  31. If nil, this prints the page message in the minibuffer and calls
  32. `beep'. If non-nil, it must be a function that takes two arguments:
  33. SENDER and MSG, both strings.
  34. Example for your ~/.emacs file:
  35. \(setq erc-page-function
  36. (lambda (sender msg)
  37. (play-sound-file \"/home/alex/elisp/erc/sounds/ni.wav\")
  38. (message \"IRC Page from %s: %s\" sender msg)))"
  39. :group 'erc-page
  40. :type '(choice (const nil)
  41. (function)))
  42. (defcustom erc-ctcp-query-PAGE-hook '(erc-ctcp-query-PAGE)
  43. "List of functions to be called when a CTCP PAGE is received.
  44. This is called from `erc-process-ctcp-query'. The functions are called
  45. with six arguments: PROC NICK LOGIN HOST TO MSG. Note that you can
  46. also set `erc-page-function' to a function, which only gets two arguments,
  47. SENDER and MSG, so that might be easier to use."
  48. :group 'erc-page
  49. :type '(repeat function))
  50. (defun erc-ctcp-query-PAGE (proc nick login host to msg)
  51. "Deal with an CTCP PAGE query, if `erc-page-mode' is non-nil.
  52. This will call `erc-page-function', if defined, or it will just print
  53. a message and `beep'. In addition to that, the page message is also
  54. inserted into the server buffer."
  55. (when (and erc-page-mode
  56. (string-match "PAGE\\(\\s-+.*\\)?$" msg))
  57. (let* ((m (match-string 1 msg))
  58. (page-msg (if m (erc-controls-interpret (substring m 1))
  59. "[no message]"))
  60. text)
  61. (if m (setq m (substring m 1)))
  62. (setq text (erc-format-message 'CTCP-PAGE
  63. ?n nick ?u login
  64. ?h host ?m page-msg))
  65. (if erc-page-function
  66. (funcall erc-page-function nick page-msg)
  67. ;; if no function is defined
  68. (message "%s" text)
  69. (beep))
  70. ;; insert text into buffer
  71. (erc-display-message
  72. nil 'notice nil text)))
  73. nil)
  74. (defun erc-cmd-PAGE (line &optional force)
  75. "Send a CTCP page to the user given as the first word in LINE.
  76. The rest of LINE is the message to send. Note that you will only
  77. receive pages if `erc-page-mode' is on."
  78. (when (string-match "^\\s-*\\(\\S-+\\) ?\\(.*\\)" line)
  79. (let ((nick (match-string 1 line))
  80. (msg (match-string 2 line)))
  81. (erc-cmd-CTCP nick "PAGE" msg))))
  82. (put 'erc-cmd-PAGE 'do-not-parse-args t)
  83. (provide 'erc-page)
  84. ;;; erc-page.el ends here
  85. ;;
  86. ;; Local Variables:
  87. ;; indent-tabs-mode: t
  88. ;; tab-width: 8
  89. ;; End: