1 ;;; erc-ezbounce.el --- Handle EZBounce bouncer commands
3 ;; Copyright (C) 2002, 2004, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Andreas Fuchs <asf@void.at>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 (eval-when-compile (require 'cl
))
30 (defgroup erc-ezbounce nil
31 "Interface to the EZBounce IRC bouncer (a virtual IRC server)"
34 (defcustom erc-ezb-regexp
"^ezbounce!srv$"
35 "Regexp used by the EZBouncer to identify itself to the user."
39 (defcustom erc-ezb-login-alist
'()
40 "Alist of logins suitable for the server we're connecting to.
42 The alist's format is as follows:
44 (((server . port) . (username . password))
45 ((server . port) . (username . password))
49 (cons (cons :tag
"Server"
56 (defvar erc-ezb-action-alist
'(("^\\[awaiting login/pass command\\]$" . erc-ezb-identify
)
57 ("^\\[use /quote CONN <server> to connect\\]$" . erc-ezb-select
)
58 ("^ID +IRC NICK +TO +TIME$" . erc-ezb-init-session-list
)
59 ("^$" . erc-ezb-end-of-session-list
)
60 (".*" . erc-ezb-add-session
))
61 "Alist of actions to take on NOTICEs from EZBounce.")
64 (defvar erc-ezb-session-list
'()
65 "List of detached EZBounce sessions.")
66 (make-variable-buffer-local 'erc-ezb-session-list
)
68 (defvar erc-ezb-inside-session-listing nil
69 "Indicate whether current notices are expected to be EZB session listings.")
72 (defun erc-cmd-ezb (line &optional force
)
73 "Send EZB commands to the EZBouncer verbatim."
74 (erc-server-send (concat "EZB " line
)))
75 (put 'erc-cmd-EZB
'do-not-parse-args t
)
78 (defun erc-ezb-get-login (server port
)
79 "Return an appropriate EZBounce login for SERVER and PORT.
80 Look up entries in `erc-ezb-login-alist'. If the username or password
81 in the alist is `nil', prompt for the appropriate values."
82 (let ((login (cdr (assoc (cons server port
) erc-ezb-login-alist
))))
84 (let ((username (car login
))
85 (password (cdr login
)))
87 (setq username
(read-from-minibuffer (format "EZBounce user name for %s:%s: " server port
))))
89 (setq password
(read-passwd (format "EZBounce password for %s:%s: " server port
))))
90 (cons username password
)))))
93 (defun erc-ezb-lookup-action (message)
94 (let ((function-alist erc-ezb-action-alist
)
96 (while (and (not found
)
98 (let ((regexp (caar function-alist
))
99 (function (cdar function-alist
)))
100 (when (string-match regexp message
)
101 (setq found function
))
102 (setq function-alist
(cdr function-alist
))))
106 (defun erc-ezb-notice-autodetect (proc parsed
)
107 "React on an EZBounce NOTICE request."
108 (let* ((sender (erc-response.sender parsed
))
109 (message (erc-response.contents parsed
))
110 (function (erc-ezb-lookup-action message
)))
111 (when (and (string-match erc-ezb-regexp sender
)
113 (funcall function message
)))
117 (defun erc-ezb-identify (message)
118 "Identify to the EZBouncer server."
119 (let ((login (erc-ezb-get-login erc-session-server
(erc-port-to-string erc-session-port
))))
121 (let ((username (car login
))
123 (erc-server-send (concat "LOGIN " username
" " pass
))))))
126 (defun erc-ezb-init-session-list (message)
127 "Reset the EZBounce session list to nil."
128 (setq erc-ezb-session-list nil
)
129 (setq erc-ezb-inside-session-listing t
))
132 (defun erc-ezb-end-of-session-list (message)
133 "Indicate the end of the EZBounce session listing."
134 (setq erc-ezb-inside-session-listing nil
))
137 (defun erc-ezb-add-session (message)
138 "Add an EZBounce session to the session list."
139 (when (and erc-ezb-inside-session-listing
140 (string-match "^\\([^ \n]+\\) +\\([^ \n]+\\) +\\([^ \n]+\\) +\\([^ \n]+\\)$" message
))
141 (let ((id (match-string 1 message
))
142 (nick (match-string 2 message
))
143 (to (match-string 3 message
)))
144 (add-to-list 'erc-ezb-session-list
(list id nick to
)))))
147 (defun erc-ezb-select (message)
148 "Select an IRC server to use by EZBounce, in ERC style."
149 (unless (and erc-ezb-session-list
150 (erc-ezb-select-session))
151 (let* ((server (read-from-minibuffer
152 "IRC server: " "" nil nil
'erc-server-history-list
))
155 (read-from-minibuffer "IRC port: "
156 (erc-port-to-string "6667")))))
157 (erc-server-send (format "CONN %s %s" server port
)))))
161 (defun erc-ezb-select-session ()
162 "Select a detached EZBounce session."
163 (let ((session (completing-read "Existing Session (RET to enter a new one): "
164 erc-ezb-session-list
)))
165 (if (string= session
"")
167 (erc-server-send (format "REATTACH %s" session
)))))
171 (defun erc-ezb-initialize ()
172 "Add EZBouncer convenience functions to ERC."
173 (add-hook 'erc-server-NOTICE-functions
'erc-ezb-notice-autodetect
))
175 (provide 'erc-ezbounce
)
177 ;; arch-tag: e972aa7b-a9f4-4d16-a489-074ec7a1002e
178 ;;; erc-ezbounce.el ends here