Initial Commit
[packages] / xemacs-packages / erc / erc-ezbounce.el
1 ;;; erc-ezbounce.el ---  Handle EZBounce bouncer commands
2
3 ;; Copyright (C) 2002, 2004 Free Software Foundation, Inc.
4
5 ;; Author: Andreas Fuchs <asf@void.at>
6 ;; Keywords: comm
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'erc)
30 (eval-when-compile (require 'cl))
31
32 (defconst erc-ezb-version "$Revision: 1.12.2.2 $"
33   "ERC EZBouncer revision.")
34
35 (defgroup erc-ezbounce nil
36   "Interface to the EZBounce IRC bouncer (a virtual IRC server)"
37   :group 'erc)
38
39 (defcustom erc-ezb-regexp "^ezbounce!srv$"
40   "Regexp used by the EZBouncer to identify itself to the user."
41   :group 'erc-ezbounce
42   :type 'string)
43
44 (defcustom erc-ezb-login-alist '()
45   "Alist of logins suitable for the server we're connecting to.
46
47 The alist's format is as follows:
48
49  (((server . port) . (username . password))
50   ((server . port) . (username . password))
51   ...)"
52   :group 'erc-ezbounce
53   :type '(repeat
54           (cons (cons :tag "Server"
55                      string
56                      string)
57                 (cons :tag "Login"
58                       string
59                       string))))
60
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.")
67
68
69 (defvar erc-ezb-session-list '()
70   "List of detached EZBounce sessions.")
71 (make-variable-buffer-local 'erc-ezb-session-list)
72
73 (defvar erc-ezb-inside-session-listing nil
74   "Indicate whether current notices are expected to be EZB session listings.")
75
76 ;;;###autoload
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)
81
82 ;;;###autoload
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))))
88     (when login
89       (let ((username (car login))
90             (password (cdr login)))
91         (when (null username)
92           (setq username (read-from-minibuffer (format "EZBounce user name for %s:%s: " server port))))
93         (when (null password)
94           (setq password (read-passwd (format "EZBounce password for %s:%s: " server port))))
95         (cons username password)))))
96
97 ;;;###autoload
98 (defun erc-ezb-lookup-action (message)
99   (let ((function-alist erc-ezb-action-alist)
100         found)
101     (while (and (not found)
102                 function-alist)
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))))
108     found))
109
110 ;;;###autoload
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)
117                function)
118       (funcall function message)))
119   nil)
120
121 ;;;###autoload
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))))
125     (unless (null login)
126       (let ((username (car login))
127             (pass (cdr login)))
128         (erc-server-send (concat "LOGIN " username " " pass))))))
129
130 ;;;###autoload
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))
135
136 ;;;###autoload
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))
140   
141 ;;;###autoload
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)))))
150
151 ;;;###autoload
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))
158            (port
159             (erc-string-to-port
160              (read-from-minibuffer "IRC port: "
161                                    (erc-port-to-string "6667")))))
162       (erc-server-send (format "CONN %s %s" server port)))))
163     
164
165 ;;;###autoload
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 "")
171         nil
172       (erc-server-send (format "REATTACH %s" session)))))
173
174
175 ;;;###autoload
176 (defun erc-ezb-initialize ()
177   "Add EZBouncer convenience functions to ERC."
178   (add-hook 'erc-server-NOTICE-functions 'erc-ezb-notice-autodetect))
179         
180 (provide 'erc-ezbounce)
181
182 ;; arch-tag: e972aa7b-a9f4-4d16-a489-074ec7a1002e
183 ;;; erc-ezbounce.el ends here