Fixed.
[riece] / lisp / riece-doctor.el
1 ;;; riece-doctor.el --- pretend to be a psychotherapist
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 ;; NOTE: This is an add-on module for Riece.
27
28 ;;; Code:
29
30 (require 'riece-globals)
31 (require 'riece-identity)
32 (require 'riece-message)
33 (require 'riece-server)
34
35 (defgroup riece-doctor nil
36   "Interface to doctor.el."
37   :prefix "riece-"
38   :group 'riece)
39
40 (defcustom riece-doctor-hello-regexp "^,doctor$"
41   "Pattern of string patients start consultation."
42   :type 'string
43   :group 'riece-doctor)
44
45 (defcustom riece-doctor-bye-regexp "^,doctor bye$"
46   "Pattern of string patients end consultation."
47   :type 'string
48   :group 'riece-doctor)
49
50 (defvar riece-doctor-patients nil)
51
52 (defconst riece-doctor-description
53   "Pretend to be a psychotherapist.")
54
55 (put 'riece-doctor 'riece-addon-default-disabled t)
56
57 (autoload 'doctor-mode "doctor")
58 (autoload 'doctor-read-print "doctor")
59
60 (defun riece-doctor-buffer-name (user)
61   (concat " *riece-doctor*"
62           (riece-format-identity
63            (riece-make-identity user riece-server-name))))
64
65 (defun riece-doctor-reply (target string)
66   (riece-display-message
67    (riece-make-message (riece-make-identity riece-real-nickname
68                                             riece-server-name)
69                        (riece-make-identity target riece-server-name)
70                        string 'notice t))
71   (riece-send-string (format "NOTICE %s :%s\r\n" target string)))
72
73 (defun riece-doctor-after-privmsg-hook (prefix string)
74   (if (get 'riece-doctor 'riece-addon-enabled)
75       (let* ((user (riece-prefix-nickname prefix))
76              (parameters (riece-split-parameters string))
77              (targets (split-string (car parameters) ","))
78              (message (nth 1 parameters)))
79         (if (string-match riece-doctor-hello-regexp message)
80             (if (riece-identity-member user riece-doctor-patients t)
81                 (riece-doctor-reply
82                  (car targets)
83                  (format "%s: You are already talking with me." user))
84               (save-excursion
85                 (set-buffer (get-buffer-create
86                              (riece-doctor-buffer-name user)))
87                 (erase-buffer)
88                 (doctor-mode))
89               (setq riece-doctor-patients (cons user riece-doctor-patients))
90               (riece-doctor-reply
91                (car targets)
92                (format
93                 "%s: I am the psychotherapist.  \
94 Please, describe your problems."
95                 user)))
96           (if (string-match riece-doctor-bye-regexp message)
97               (let ((pointer (riece-identity-member user
98                                                     riece-doctor-patients t)))
99                 (when pointer
100                   (kill-buffer (riece-doctor-buffer-name user))
101                   (setq riece-doctor-patients (delq (car pointer)
102                                                     riece-doctor-patients))
103                   (riece-doctor-reply
104                    (car targets)
105                    (format "%s: Good bye." user))))
106             (if (riece-identity-member user riece-doctor-patients t)
107                 (let (string)
108                   (save-excursion
109                     (set-buffer (get-buffer (riece-doctor-buffer-name user)))
110                     (goto-char (point-max))
111                     (insert message "\n")
112                     (let ((point (point)))
113                       (doctor-read-print)
114                       (setq string (buffer-substring (1+ point)
115                                                      (- (point) 2))))
116                     (with-temp-buffer
117                       (insert string)
118                       (subst-char-in-region (point-min) (point-max) ?\n ? )
119                       (setq string (buffer-string))))
120                   (riece-doctor-reply
121                    (car targets)
122                    (format "%s: %s" user string)))))))))
123
124 (defun riece-doctor-insinuate ()
125   (add-hook 'riece-after-privmsg-hook 'riece-doctor-after-privmsg-hook))
126
127 (defun riece-doctor-uninstall ()
128   (remove-hook 'riece-after-privmsg-hook 'riece-doctor-after-privmsg-hook))
129
130 (provide 'riece-doctor)
131
132 ;;; riece-doctor.el ends here