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