Fixed error message.
[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 (defvar riece-doctor-enabled nil)
53
54 (defconst riece-doctor-description
55   "Pretend to be a psychotherapist.")
56
57 (put 'riece-doctor 'riece-addon-default-disabled t)
58
59 (autoload 'doctor-mode "doctor")
60 (autoload 'doctor-read-print "doctor")
61
62 (defun riece-doctor-buffer-name (user)
63   (concat " *riece-doctor*"
64           (riece-format-identity
65            (riece-make-identity user riece-server-name))))
66
67 (defun riece-doctor-reply (target string)
68   (riece-display-message
69    (riece-make-message (riece-make-identity riece-real-nickname
70                                             riece-server-name)
71                        (riece-make-identity target riece-server-name)
72                        string 'notice t))
73   (riece-send-string (format "NOTICE %s :%s\r\n" target string)))
74
75 (defun riece-doctor-after-privmsg-hook (prefix string)
76   (if riece-doctor-enabled
77       (let* ((user (riece-prefix-nickname prefix))
78              (parameters (riece-split-parameters string))
79              (targets (split-string (car parameters) ","))
80              (message (nth 1 parameters)))
81         (if (string-match riece-doctor-hello-regexp message)
82             (if (riece-identity-member user riece-doctor-patients t)
83                 (riece-doctor-reply
84                  (car targets)
85                  (format "%s: You are already talking with me." user))
86               (save-excursion
87                 (set-buffer (get-buffer-create
88                              (riece-doctor-buffer-name user)))
89                 (erase-buffer)
90                 (doctor-mode))
91               (setq riece-doctor-patients (cons user riece-doctor-patients))
92               (riece-doctor-reply
93                (car targets)
94                (format
95                 "%s: I am the psychotherapist.  \
96 Please, describe your problems."
97                 user)))
98           (if (string-match riece-doctor-bye-regexp message)
99               (let ((pointer (riece-identity-member user
100                                                     riece-doctor-patients t)))
101                 (when pointer
102                   (kill-buffer (riece-doctor-buffer-name user))
103                   (setq riece-doctor-patients (delq (car pointer)
104                                                     riece-doctor-patients))
105                   (riece-doctor-reply
106                    (car targets)
107                    (format "%s: Good bye." user))))
108             (if (riece-identity-member user riece-doctor-patients t)
109                 (let (string)
110                   (save-excursion
111                     (set-buffer (get-buffer (riece-doctor-buffer-name user)))
112                     (goto-char (point-max))
113                     (insert message "\n")
114                     (let ((point (point)))
115                       (doctor-read-print)
116                       (setq string (buffer-substring (1+ point)
117                                                      (- (point) 2))))
118                     (with-temp-buffer
119                       (insert string)
120                       (subst-char-in-region (point-min) (point-max) ?\n ? )
121                       (setq string (buffer-string))))
122                   (riece-doctor-reply
123                    (car targets)
124                    (format "%s: %s" user string)))))))))
125
126 (defun riece-doctor-insinuate ()
127   (add-hook 'riece-after-privmsg-hook 'riece-doctor-after-privmsg-hook))
128
129 (defun riece-doctor-uninstall ()
130   (remove-hook 'riece-after-privmsg-hook 'riece-doctor-after-privmsg-hook))
131
132 (defun riece-doctor-enable ()
133   (setq riece-doctor-enabled t))
134
135 (defun riece-doctor-disable ()
136   (setq riece-doctor-enabled nil))
137
138 (provide 'riece-doctor)
139
140 ;;; riece-doctor.el ends here