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