8f0091a1f3bb9772bf72ac8d9171410abe05d3b8
[riece] / lisp / riece-eval-ruby.el
1 ;;; riece-eval-ruby.el --- evaluate Ruby programs in channels
2 ;; Copyright (C) 1998-2005 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Keywords: IRC, riece, Ruby
7
8 ;; This file is part of Riece.
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; To use, add the following line to your ~/.riece/init.el:
28 ;; (add-to-list 'riece-addons 'riece-eval-ruby)
29
30 ;;; Code:
31
32 (require 'riece-ruby)
33 (require 'riece-message)
34
35 (defgroup riece-eval-ruby nil
36   "Evaluate Ruby programs in channels."
37   :prefix "riece-"
38   :group 'riece)
39
40 (defcustom riece-eval-ruby-prefix-regexp "^,ruby\\s-+"
41   "Pattern of of the prefix for sending Ruby programs."
42   :type 'string
43   :group 'riece-eval-ruby)
44
45 (defvar riece-eval-ruby-enabled nil)
46
47 (defconst riece-eval-ruby-description
48   "Evaluate an input string as Ruby program.")
49
50 (defun riece-eval-ruby-exit-handler (name)
51   (riece-ruby-inspect name)
52   (let* ((data (copy-sequence (or riece-ruby-data "nil")))
53          (length (length data))
54          (index 0))
55     (while (< index length)
56       (if (eq (aref data index) ?\n)
57           (aset data index ? ))
58       (setq index (1+ index)))
59     (riece-send-string
60      (format "NOTICE %s :%s\r\n"
61              (riece-identity-prefix
62               (riece-ruby-property name 'riece-eval-ruby-target))
63              data))
64     (riece-display-message
65      (riece-make-message (riece-current-nickname)
66                          (riece-ruby-property name 'riece-eval-ruby-target)
67                          data
68                          'notice))))
69
70 (defun riece-eval-ruby-display-message-function (message)
71   (if (and riece-eval-ruby-enabled
72            (riece-message-own-p message)
73            (string-match riece-eval-ruby-prefix-regexp
74                          (riece-message-text message)))
75       (let ((name (riece-ruby-execute
76                    (substring (riece-message-text message)
77                               (match-end 0)))))
78         (riece-ruby-set-property name
79                                  'riece-eval-ruby-target
80                                  (riece-message-target message))
81         (riece-ruby-set-exit-handler name
82                                      #'riece-eval-ruby-exit-handler))))
83
84 (defun riece-eval-ruby-insinuate ()
85   (add-hook 'riece-after-display-message-functions
86             'riece-eval-ruby-display-message-function))
87
88 (defun riece-eval-ruby-enable ()
89   (setq riece-eval-ruby-enabled t))
90
91 (defun riece-eval-ruby-disable ()
92   (setq riece-eval-ruby-enabled nil))
93
94 (provide 'riece-eval-ruby)
95
96 ;;; riece-eval-ruby.el ends here