022366ed83b3f1d7c8dcbe52d658e604cef610bd
[elisp/riece.git] / lisp / riece-doctor.el
1 ;;; riece-doctor.el --- "become a psychotherapist" add-on
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: IRC, riece
6
7 ;; This file is part of Riece.
8
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; This add-on allows you to become a psychotherapist.
27
28 ;; To use, add the following line to your ~/.riece/init.el:
29 ;; (add-to-list 'riece-addons 'riece-doctor t)
30
31 ;;; Code:
32
33 (require 'doctor)
34
35 (defvar riece-doctor-patients nil)
36
37 (defun riece-doctor-buffer-name (user)
38   (concat " *riece-doctor*" (riece-identity-canonicalize-prefix user)))
39
40 (defun riece-doctor-reply (target string)
41   (riece-send-string (format "NOTICE %s :%s\r\n" target string))
42   (riece-own-channel-message string
43                              (riece-make-identity target riece-server-name)
44                              'notice))
45
46 (defun riece-doctor-after-privmsg-hook (prefix string)
47   (let* ((user (riece-prefix-nickname prefix))
48          (parameters (riece-split-parameters string))
49          (targets (split-string (car parameters) ","))
50          (message (nth 1 parameters)))
51     (if (string-match "^, doctor" message)
52         (if (riece-identity-member-no-server user riece-doctor-patients)
53             (riece-doctor-reply
54              (car targets)
55              "You are already talking with me.")
56           (save-excursion
57             (set-buffer (get-buffer-create (riece-doctor-buffer-name user)))
58             (erase-buffer)
59             (doctor-mode))
60           (setq riece-doctor-patients (cons user riece-doctor-patients))
61           (riece-doctor-reply
62            (car targets)           
63            "I am the psychotherapist.  Please, describe your problems."))
64       (if (string-match "^, bye doctor" message)
65           (let ((pointer (riece-identity-member-no-server
66                           user riece-doctor-patients)))
67             (when pointer
68               (kill-buffer (riece-doctor-buffer-name user))
69               (setq riece-doctor-patients (delq (car pointer)
70                                                 riece-doctor-patients))
71               (riece-doctor-reply (car targets) "Good bye.")))
72         (when (riece-identity-member-no-server user riece-doctor-patients)
73           (riece-doctor-reply
74            (car targets)
75            (save-excursion
76              (set-buffer (get-buffer (riece-doctor-buffer-name user)))
77              (goto-char (point-max))
78              (insert message "\n")
79              (let ((point (point))
80                    string)
81                (doctor-read-print)
82                (setq string (buffer-substring (1+ point)(- (point) 2)))
83                (with-temp-buffer
84                  (insert string)
85                  (subst-char-in-region (point-min) (point-max) ?\n ? )
86                  (buffer-string))))))))))
87
88 (defun riece-doctor-insinuate ()
89   (make-variable-buffer-local 'riece-doctor-patients)
90   (add-hook 'riece-after-privmsg-hook 'riece-doctor-after-privmsg-hook))
91
92 (provide 'riece-doctor)
93
94 ;;; riece-doctor.el ends here