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