Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / erc / erc-ezbounce.el
blobe848d2550b13a74957e118549b2ade0d97d422a0
1 ;;; erc-ezbounce.el --- Handle EZBounce bouncer commands
3 ;; Copyright (C) 2002, 2004, 2006-2014 Free Software Foundation, Inc.
5 ;; Author: Andreas Fuchs <asf@void.at>
6 ;; Maintainer: FSF
7 ;; Keywords: comm
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/>.
24 ;;; Commentary:
26 ;;; Code:
28 (require 'erc)
30 (defgroup erc-ezbounce nil
31 "Interface to the EZBounce IRC bouncer (a virtual IRC server)"
32 :group 'erc)
34 (defcustom erc-ezb-regexp "^ezbounce!srv$"
35 "Regexp used by the EZBouncer to identify itself to the user."
36 :group 'erc-ezbounce
37 :type 'string)
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))
46 ...)"
47 :group 'erc-ezbounce
48 :type '(repeat
49 (cons (cons :tag "Server"
50 string
51 string)
52 (cons :tag "Login"
53 string
54 string))))
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.")
71 ;;;###autoload
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)
77 ;;;###autoload
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))))
83 (when login
84 (let ((username (car login))
85 (password (cdr login)))
86 (when (null username)
87 (setq username (read-from-minibuffer (format "EZBounce user name for %s:%s: " server port))))
88 (when (null password)
89 (setq password (read-passwd (format "EZBounce password for %s:%s: " server port))))
90 (cons username password)))))
92 ;;;###autoload
93 (defun erc-ezb-lookup-action (message)
94 (let ((function-alist erc-ezb-action-alist)
95 found)
96 (while (and (not found)
97 function-alist)
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))))
103 found))
105 ;;;###autoload
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)
112 function)
113 (funcall function message)))
114 nil)
116 ;;;###autoload
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))))
120 (unless (null login)
121 (let ((username (car login))
122 (pass (cdr login)))
123 (erc-server-send (concat "LOGIN " username " " pass))))))
125 ;;;###autoload
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))
131 ;;;###autoload
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))
136 ;;;###autoload
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)))))
146 ;;;###autoload
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))
153 (port
154 (erc-string-to-port
155 (read-from-minibuffer "IRC port: "
156 (erc-port-to-string "6667")))))
157 (erc-server-send (format "CONN %s %s" server port)))))
160 ;;;###autoload
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)))))
170 ;;;###autoload
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 ;;; erc-ezbounce.el ends here