(riece-doctor-buffer-name): Qualify patient names.
[elisp/riece.git] / 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 (require 'doctor)
34
35 (defvar riece-doctor-patients nil)
36
37 (defun riece-doctor-buffer-name (user)
38   (concat " *riece-doctor*"
39           (riece-decode-identity (riece-make-identity user
40                                                       riece-server-name))))
41
42 (defun riece-doctor-reply (target string)
43   (riece-send-string (format "NOTICE %s :%s\r\n" target string))
44   (riece-own-channel-message string
45                              (riece-make-identity target riece-server-name)
46                              'notice))
47
48 (defun riece-doctor-after-privmsg-hook (prefix string)
49   (let* ((user (riece-prefix-nickname prefix))
50          (parameters (riece-split-parameters string))
51          (targets (split-string (car parameters) ","))
52          (message (nth 1 parameters)))
53     (if (string-match "^, doctor" message)
54         (if (riece-identity-member-no-server user riece-doctor-patients)
55             (riece-doctor-reply
56              (car targets)
57              "You are already talking with me.")
58           (save-excursion
59             (set-buffer (get-buffer-create (riece-doctor-buffer-name user)))
60             (erase-buffer)
61             (doctor-mode))
62           (setq riece-doctor-patients (cons user riece-doctor-patients))
63           (riece-doctor-reply
64            (car targets)           
65            "I am the psychotherapist.  Please, describe your problems."))
66       (if (string-match "^, bye doctor" message)
67           (let ((pointer (riece-identity-member-no-server
68                           user riece-doctor-patients)))
69             (when pointer
70               (kill-buffer (riece-doctor-buffer-name user))
71               (setq riece-doctor-patients (delq (car pointer)
72                                                 riece-doctor-patients))
73               (riece-doctor-reply (car targets) "Good bye.")))
74         (when (riece-identity-member-no-server user riece-doctor-patients)
75           (riece-doctor-reply
76            (car targets)
77            (save-excursion
78              (set-buffer (get-buffer (riece-doctor-buffer-name user)))
79              (goto-char (point-max))
80              (insert message "\n")
81              (let ((point (point))
82                    string)
83                (doctor-read-print)
84                (setq string (buffer-substring (1+ point)(- (point) 2)))
85                (with-temp-buffer
86                  (insert string)
87                  (subst-char-in-region (point-min) (point-max) ?\n ? )
88                  (buffer-string))))))))))
89
90 (defun riece-doctor-insinuate ()
91   (make-variable-buffer-local 'riece-doctor-patients)
92   (add-hook 'riece-after-privmsg-hook 'riece-doctor-after-privmsg-hook))
93
94 (provide 'riece-doctor)
95
96 ;;; riece-doctor.el ends here