erc-ezbounce.el 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. ;;; erc-ezbounce.el --- Handle EZBounce bouncer commands
  2. ;; Copyright (C) 2002, 2004, 2006-2012 Free Software Foundation, Inc.
  3. ;; Author: Andreas Fuchs <asf@void.at>
  4. ;; Keywords: comm
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  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. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (require 'erc)
  19. (eval-when-compile (require 'cl))
  20. (defgroup erc-ezbounce nil
  21. "Interface to the EZBounce IRC bouncer (a virtual IRC server)"
  22. :group 'erc)
  23. (defcustom erc-ezb-regexp "^ezbounce!srv$"
  24. "Regexp used by the EZBouncer to identify itself to the user."
  25. :group 'erc-ezbounce
  26. :type 'string)
  27. (defcustom erc-ezb-login-alist '()
  28. "Alist of logins suitable for the server we're connecting to.
  29. The alist's format is as follows:
  30. (((server . port) . (username . password))
  31. ((server . port) . (username . password))
  32. ...)"
  33. :group 'erc-ezbounce
  34. :type '(repeat
  35. (cons (cons :tag "Server"
  36. string
  37. string)
  38. (cons :tag "Login"
  39. string
  40. string))))
  41. (defvar erc-ezb-action-alist '(("^\\[awaiting login/pass command\\]$" . erc-ezb-identify)
  42. ("^\\[use /quote CONN <server> to connect\\]$" . erc-ezb-select)
  43. ("^ID +IRC NICK +TO +TIME$" . erc-ezb-init-session-list)
  44. ("^$" . erc-ezb-end-of-session-list)
  45. (".*" . erc-ezb-add-session))
  46. "Alist of actions to take on NOTICEs from EZBounce.")
  47. (defvar erc-ezb-session-list '()
  48. "List of detached EZBounce sessions.")
  49. (make-variable-buffer-local 'erc-ezb-session-list)
  50. (defvar erc-ezb-inside-session-listing nil
  51. "Indicate whether current notices are expected to be EZB session listings.")
  52. ;;;###autoload
  53. (defun erc-cmd-ezb (line &optional force)
  54. "Send EZB commands to the EZBouncer verbatim."
  55. (erc-server-send (concat "EZB " line)))
  56. (put 'erc-cmd-EZB 'do-not-parse-args t)
  57. ;;;###autoload
  58. (defun erc-ezb-get-login (server port)
  59. "Return an appropriate EZBounce login for SERVER and PORT.
  60. Look up entries in `erc-ezb-login-alist'. If the username or password
  61. in the alist is `nil', prompt for the appropriate values."
  62. (let ((login (cdr (assoc (cons server port) erc-ezb-login-alist))))
  63. (when login
  64. (let ((username (car login))
  65. (password (cdr login)))
  66. (when (null username)
  67. (setq username (read-from-minibuffer (format "EZBounce user name for %s:%s: " server port))))
  68. (when (null password)
  69. (setq password (read-passwd (format "EZBounce password for %s:%s: " server port))))
  70. (cons username password)))))
  71. ;;;###autoload
  72. (defun erc-ezb-lookup-action (message)
  73. (let ((function-alist erc-ezb-action-alist)
  74. found)
  75. (while (and (not found)
  76. function-alist)
  77. (let ((regexp (caar function-alist))
  78. (function (cdar function-alist)))
  79. (when (string-match regexp message)
  80. (setq found function))
  81. (setq function-alist (cdr function-alist))))
  82. found))
  83. ;;;###autoload
  84. (defun erc-ezb-notice-autodetect (proc parsed)
  85. "React on an EZBounce NOTICE request."
  86. (let* ((sender (erc-response.sender parsed))
  87. (message (erc-response.contents parsed))
  88. (function (erc-ezb-lookup-action message)))
  89. (when (and (string-match erc-ezb-regexp sender)
  90. function)
  91. (funcall function message)))
  92. nil)
  93. ;;;###autoload
  94. (defun erc-ezb-identify (message)
  95. "Identify to the EZBouncer server."
  96. (let ((login (erc-ezb-get-login erc-session-server (erc-port-to-string erc-session-port))))
  97. (unless (null login)
  98. (let ((username (car login))
  99. (pass (cdr login)))
  100. (erc-server-send (concat "LOGIN " username " " pass))))))
  101. ;;;###autoload
  102. (defun erc-ezb-init-session-list (message)
  103. "Reset the EZBounce session list to nil."
  104. (setq erc-ezb-session-list nil)
  105. (setq erc-ezb-inside-session-listing t))
  106. ;;;###autoload
  107. (defun erc-ezb-end-of-session-list (message)
  108. "Indicate the end of the EZBounce session listing."
  109. (setq erc-ezb-inside-session-listing nil))
  110. ;;;###autoload
  111. (defun erc-ezb-add-session (message)
  112. "Add an EZBounce session to the session list."
  113. (when (and erc-ezb-inside-session-listing
  114. (string-match "^\\([^ \n]+\\) +\\([^ \n]+\\) +\\([^ \n]+\\) +\\([^ \n]+\\)$" message))
  115. (let ((id (match-string 1 message))
  116. (nick (match-string 2 message))
  117. (to (match-string 3 message)))
  118. (add-to-list 'erc-ezb-session-list (list id nick to)))))
  119. ;;;###autoload
  120. (defun erc-ezb-select (message)
  121. "Select an IRC server to use by EZBounce, in ERC style."
  122. (unless (and erc-ezb-session-list
  123. (erc-ezb-select-session))
  124. (let* ((server (read-from-minibuffer
  125. "IRC server: " "" nil nil 'erc-server-history-list))
  126. (port
  127. (erc-string-to-port
  128. (read-from-minibuffer "IRC port: "
  129. (erc-port-to-string "6667")))))
  130. (erc-server-send (format "CONN %s %s" server port)))))
  131. ;;;###autoload
  132. (defun erc-ezb-select-session ()
  133. "Select a detached EZBounce session."
  134. (let ((session (completing-read "Existing Session (RET to enter a new one): "
  135. erc-ezb-session-list)))
  136. (if (string= session "")
  137. nil
  138. (erc-server-send (format "REATTACH %s" session)))))
  139. ;;;###autoload
  140. (defun erc-ezb-initialize ()
  141. "Add EZBouncer convenience functions to ERC."
  142. (add-hook 'erc-server-NOTICE-functions 'erc-ezb-notice-autodetect))
  143. (provide 'erc-ezbounce)
  144. ;;; erc-ezbounce.el ends here