(riece-identity-button-popup-menu): Changed
[riece] / lisp / riece-eval.el
1 ;;; riece-eval.el --- eval add-on
2 ;; Copyright (C) 2005 OHASHI Akira
3
4 ;; Author: OHASHI Akira <bg66@koka-in.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 evaluates an input string as lisp object and sends a result
27 ;; as notice. Note the risky of this add-on.
28
29 ;; To use, add the following line to your ~/.riece/init.el:
30 ;; (add-to-list 'riece-addons 'riece-eval)
31
32 ;;; Code:
33
34 (require 'pp)
35 (require 'riece-message)
36
37 (defgroup riece-eval nil
38   "Evaluate an input string as lisp object."
39   :prefix "riece-"
40   :group 'riece)
41
42 (defcustom riece-eval-regexp "^, \\(.+\\)"
43   "*Pattern of string evaluating."
44   :type 'string
45   :group 'riece-eval)
46
47 (defcustom riece-eval-ignore-error nil
48   "*If non-nil, an error is ignored."
49   :type 'boolean
50   :group 'riece-eval)
51
52 (defvar riece-eval-enabled nil)
53
54 (defconst riece-eval-description
55   "Evaluate an input string as lisp object.")
56
57 (defun riece-eval-display-message-function (message)
58   (when (and riece-eval-enabled
59              (riece-message-own-p message)
60              (string-match riece-eval-regexp (riece-message-text message)))
61     (let* ((form (match-string 1 (riece-message-text message)))
62            (string (riece-eval-form form)))
63       (unless (equal string "")
64         (riece-send-string
65          (format "NOTICE %s :%s\r\n"
66                  (riece-identity-prefix (riece-message-target message))
67                  string))
68         (riece-display-message
69          (riece-make-message (riece-current-nickname)
70                              (riece-message-target message)
71                              string 'notice))))))
72
73 (defun riece-eval-form (form)
74   (condition-case err
75       (let ((object (eval (read form))))
76         (cond
77          ((stringp object) object)
78          ((and (listp object)
79                (not (eq object nil)))
80           (let ((string (pp-to-string object)))
81             (substring string 0 (1- (length string)))))
82          ((numberp object)
83           (number-to-string object))
84          ((eq object nil) "")
85          (t (pp-to-string object))))
86     (error
87      (unless riece-eval-ignore-error
88        (format "Error evaluating %s: %s" form err)))))
89
90 (defun riece-eval-insinuate ()
91   (add-hook 'riece-after-display-message-functions
92             'riece-eval-display-message-function))
93
94 (defun riece-eval-enable ()
95   (setq riece-eval-enabled t))
96
97 (defun riece-eval-disable ()
98   (setq riece-eval-enabled nil))
99
100 (provide 'riece-eval)
101
102 ;;; riece-eval.el ends here