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