Merge from emacs-24; up to 2012-05-08T15:19:18Z!monnier@iro.umontreal.ca
[bpt/emacs.git] / lisp / erc / erc-ezbounce.el
1 ;;; erc-ezbounce.el --- Handle EZBounce bouncer commands
2
3 ;; Copyright (C) 2002, 2004, 2006-2012 Free Software Foundation, Inc.
4
5 ;; Author: Andreas Fuchs <asf@void.at>
6 ;; Maintainer: FSF
7 ;; Keywords: comm
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'erc)
29 (eval-when-compile (require 'cl))
30
31 (defgroup erc-ezbounce nil
32 "Interface to the EZBounce IRC bouncer (a virtual IRC server)"
33 :group 'erc)
34
35 (defcustom erc-ezb-regexp "^ezbounce!srv$"
36 "Regexp used by the EZBouncer to identify itself to the user."
37 :group 'erc-ezbounce
38 :type 'string)
39
40 (defcustom erc-ezb-login-alist '()
41 "Alist of logins suitable for the server we're connecting to.
42
43 The alist's format is as follows:
44
45 (((server . port) . (username . password))
46 ((server . port) . (username . password))
47 ...)"
48 :group 'erc-ezbounce
49 :type '(repeat
50 (cons (cons :tag "Server"
51 string
52 string)
53 (cons :tag "Login"
54 string
55 string))))
56
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.")
63
64
65 (defvar erc-ezb-session-list '()
66 "List of detached EZBounce sessions.")
67 (make-variable-buffer-local 'erc-ezb-session-list)
68
69 (defvar erc-ezb-inside-session-listing nil
70 "Indicate whether current notices are expected to be EZB session listings.")
71
72 ;;;###autoload
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)
77
78 ;;;###autoload
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))))
84 (when login
85 (let ((username (car login))
86 (password (cdr login)))
87 (when (null username)
88 (setq username (read-from-minibuffer (format "EZBounce user name for %s:%s: " server port))))
89 (when (null password)
90 (setq password (read-passwd (format "EZBounce password for %s:%s: " server port))))
91 (cons username password)))))
92
93 ;;;###autoload
94 (defun erc-ezb-lookup-action (message)
95 (let ((function-alist erc-ezb-action-alist)
96 found)
97 (while (and (not found)
98 function-alist)
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))))
104 found))
105
106 ;;;###autoload
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)
113 function)
114 (funcall function message)))
115 nil)
116
117 ;;;###autoload
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))))
121 (unless (null login)
122 (let ((username (car login))
123 (pass (cdr login)))
124 (erc-server-send (concat "LOGIN " username " " pass))))))
125
126 ;;;###autoload
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))
131
132 ;;;###autoload
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))
136
137 ;;;###autoload
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)))))
146
147 ;;;###autoload
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))
154 (port
155 (erc-string-to-port
156 (read-from-minibuffer "IRC port: "
157 (erc-port-to-string "6667")))))
158 (erc-server-send (format "CONN %s %s" server port)))))
159
160
161 ;;;###autoload
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 "")
167 nil
168 (erc-server-send (format "REATTACH %s" session)))))
169
170
171 ;;;###autoload
172 (defun erc-ezb-initialize ()
173 "Add EZBouncer convenience functions to ERC."
174 (add-hook 'erc-server-NOTICE-functions 'erc-ezb-notice-autodetect))
175
176 (provide 'erc-ezbounce)
177
178 ;;; erc-ezbounce.el ends here