1 ;;; erc-ezbounce.el --- Handle EZBounce bouncer commands
3 ;; Copyright (C) 2002, 2004 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 2, or (at your option)
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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
30 (eval-when-compile (require 'cl))
32 (defconst erc-ezb-version "$Revision: 1.12.2.2 $"
33 "ERC EZBouncer revision.")
35 (defgroup erc-ezbounce nil
36 "Interface to the EZBounce IRC bouncer (a virtual IRC server)"
39 (defcustom erc-ezb-regexp "^ezbounce!srv$"
40 "Regexp used by the EZBouncer to identify itself to the user."
44 (defcustom erc-ezb-login-alist '()
45 "Alist of logins suitable for the server we're connecting to.
47 The alist's format is as follows:
49 (((server . port) . (username . password))
50 ((server . port) . (username . password))
54 (cons (cons :tag "Server"
61 (defvar erc-ezb-action-alist '(("^\\[awaiting login/pass command\\]$" . erc-ezb-identify)
62 ("^\\[use /quote CONN <server> to connect\\]$" . erc-ezb-select)
63 ("^ID +IRC NICK +TO +TIME$" . erc-ezb-init-session-list)
64 ("^$" . erc-ezb-end-of-session-list)
65 (".*" . erc-ezb-add-session))
66 "Alist of actions to take on NOTICEs from EZBounce.")
69 (defvar erc-ezb-session-list '()
70 "List of detached EZBounce sessions.")
71 (make-variable-buffer-local 'erc-ezb-session-list)
73 (defvar erc-ezb-inside-session-listing nil
74 "Indicate whether current notices are expected to be EZB session listings.")
77 (defun erc-cmd-ezb (line &optional force)
78 "Send EZB commands to the EZBouncer verbatim."
79 (erc-server-send (concat "EZB " line)))
80 (put 'erc-cmd-EZB 'do-not-parse-args t)
83 (defun erc-ezb-get-login (server port)
84 "Return an appropriate EZBounce login for SERVER and PORT.
85 Look up entries in `erc-ezb-login-alist'. If the username or password
86 in the alist is `nil', prompt for the appropriate values."
87 (let ((login (cdr (assoc (cons server port) erc-ezb-login-alist))))
89 (let ((username (car login))
90 (password (cdr login)))
92 (setq username (read-from-minibuffer (format "EZBounce user name for %s:%s: " server port))))
94 (setq password (read-passwd (format "EZBounce password for %s:%s: " server port))))
95 (cons username password)))))
98 (defun erc-ezb-lookup-action (message)
99 (let ((function-alist erc-ezb-action-alist)
101 (while (and (not found)
103 (let ((regexp (caar function-alist))
104 (function (cdar function-alist)))
105 (when (string-match regexp message)
106 (setq found function))
107 (setq function-alist (cdr function-alist))))
111 (defun erc-ezb-notice-autodetect (proc parsed)
112 "React on an EZBounce NOTICE request."
113 (let* ((sender (erc-response.sender parsed))
114 (message (erc-response.contents parsed))
115 (function (erc-ezb-lookup-action message)))
116 (when (and (string-match erc-ezb-regexp sender)
118 (funcall function message)))
122 (defun erc-ezb-identify (message)
123 "Identify to the EZBouncer server."
124 (let ((login (erc-ezb-get-login erc-session-server (erc-port-to-string erc-session-port))))
126 (let ((username (car login))
128 (erc-server-send (concat "LOGIN " username " " pass))))))
131 (defun erc-ezb-init-session-list (message)
132 "Reset the EZBounce session list to NIL."
133 (setq erc-ezb-session-list nil)
134 (setq erc-ezb-inside-session-listing t))
137 (defun erc-ezb-end-of-session-list (message)
138 "Indicate the end of the EZBounce session listing."
139 (setq erc-ezb-inside-session-listing nil))
142 (defun erc-ezb-add-session (message)
143 "Add an EZBounce session to the session list."
144 (when (and erc-ezb-inside-session-listing
145 (string-match "^\\([^ \n]+\\) +\\([^ \n]+\\) +\\([^ \n]+\\) +\\([^ \n]+\\)$" message))
146 (let ((id (match-string 1 message))
147 (nick (match-string 2 message))
148 (to (match-string 3 message)))
149 (add-to-list 'erc-ezb-session-list (list id nick to)))))
152 (defun erc-ezb-select (message)
153 "Select an IRC server to use by EZBounce, in ERC style."
154 (unless (and erc-ezb-session-list
155 (erc-ezb-select-session))
156 (let* ((server (read-from-minibuffer
157 "IRC server: " "" nil nil 'erc-server-history-list))
160 (read-from-minibuffer "IRC port: "
161 (erc-port-to-string "6667")))))
162 (erc-server-send (format "CONN %s %s" server port)))))
166 (defun erc-ezb-select-session ()
167 "Select a detached EZBounce session."
168 (let ((session (completing-read "Existing Session (RET to enter a new one): "
169 erc-ezb-session-list)))
170 (if (string= session "")
172 (erc-server-send (format "REATTACH %s" session)))))
176 (defun erc-ezb-initialize ()
177 "Add EZBouncer convenience functions to ERC."
178 (add-hook 'erc-server-NOTICE-functions 'erc-ezb-notice-autodetect))
180 (provide 'erc-ezbounce)
182 ;; arch-tag: e972aa7b-a9f4-4d16-a489-074ec7a1002e
183 ;;; erc-ezbounce.el ends here