Version 0.8.90
[emacs-jabber.git] / jabber-console.el
blob5b8925487e1ff6313efe277c251cfdf3cd9c159f
1 ;; jabber-console.el - XML Console mode
3 ;; Copyright (C) 2009, 2010 - Demyan Rogozhin <demyan.rogozhin@gmail.com>
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2 of the License, or
8 ;; (at your option) any later version.
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program; if not, write to the Free Software
17 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ;;; Commentary:
21 ;; Use *-jabber-console-* for sending custom XMPP code. Be careful!
23 ;;; Code:
25 (require 'jabber-keymap)
26 (require 'jabber-util)
27 (require 'ewoc)
28 (require 'sgml-mode) ;we base on this mode to hightlight XML
30 (defcustom jabber-console-name-format "*-jabber-console-%s-*"
31 "Format for console buffer name. %s mean connection jid."
32 :type 'string
33 :group 'jabber-debug)
35 (defcustom jabber-console-truncate-lines 3000
36 "Maximum number of lines in console buffer.
37 Not truncate if set to 0"
38 :type 'integer
39 :group 'jabber-debug)
41 (defvar jabber-point-insert nil
42 "Position where the message being composed starts")
44 (defvar jabber-send-function nil
45 "Function for sending a message from a chat buffer.")
47 (defvar jabber-console-mode-hook nil
48 "Hook called at the end of `jabber-console-mode'.
49 Note that functions in this hook have no way of knowing
50 what kind of chat buffer is being created.")
52 (defvar jabber-console-ewoc nil
53 "The ewoc showing the XML elements of this stream buffer.")
55 ;;;###autoload
56 (defvar jabber-buffer-connection nil
57 "The connection used by this buffer.")
58 ;;;###autoload
59 (make-variable-buffer-local 'jabber-buffer-connection)
61 (defvar jabber-console-mode-map
62 (let ((map (make-sparse-keymap)))
63 (set-keymap-parent map jabber-common-keymap)
64 (define-key map "\r" 'jabber-chat-buffer-send)
65 map))
67 (defun jabber-console-create-buffer (jc)
68 (with-current-buffer
69 (get-buffer-create (format jabber-console-name-format (jabber-connection-bare-jid jc)))
70 (unless (eq major-mode 'jabber-console-mode)
71 (jabber-console-mode))
72 ;; Make sure the connection variable is up to date.
73 (setq jabber-buffer-connection jc)
74 (current-buffer)))
76 (defun jabber-console-send (jc data)
77 ;; Put manual string into buffers ewoc
78 (jabber-process-console jc "raw" data)
79 ;; ...than sent it to server
80 (jabber-send-string jc data))
82 (defun jabber-console-comment (str)
83 "Insert comment into console buffer."
84 (let ((string (concat
85 comment-start str "@" (jabber-encode-time (current-time)) ":"
86 comment-end "\n")))
87 (when (stringp jabber-debug-log-xml)
88 (jabber-append-string-to-file string jabber-debug-log-xml))
89 (insert string)))
91 (defun jabber-console-pp (data)
92 "Pretty Printer for XML-sexp and raw data"
93 (let ((direction (car data))
94 (xml-list (cdr data))
95 (raw (cadr data)))
96 (jabber-console-comment direction)
97 (if (stringp raw)
98 ;; raw code input
99 (progn
100 (insert raw)
101 (when (stringp jabber-debug-log-xml)
102 (jabber-append-string-to-file raw jabber-debug-log-xml)))
103 ;; receive/sending
104 (progn
105 (xml-print xml-list)
106 (when (stringp jabber-debug-log-xml)
107 (jabber-append-string-to-file
108 "\n" jabber-debug-log-xml 'xml-print xml-list))))))
110 (define-derived-mode jabber-console-mode sgml-mode "Jabber Console"
111 "Major mode for debug XMPP protocol"
112 ;; Make sure to set this variable somewhere
113 (make-local-variable 'jabber-send-function)
114 (make-local-variable 'jabber-point-insert)
115 (make-local-variable 'jabber-console-ewoc)
117 (setq jabber-send-function 'jabber-console-send)
119 (unless jabber-console-ewoc
120 (setq jabber-console-ewoc
121 (ewoc-create #'jabber-console-pp nil "<!-- + -->"))
122 (goto-char (point-max))
123 (put-text-property (point-min) (point) 'read-only t)
124 (let ((inhibit-read-only t))
125 (put-text-property (point-min) (point) 'front-sticky t)
126 (put-text-property (point-min) (point) 'rear-nonsticky t))
127 (setq jabber-point-insert (point-marker))))
129 (put 'jabber-console-mode 'mode-class 'special)
131 (defun jabber-console-sanitize (xml-data)
132 "Sanitize XML-DATA for jabber-process-console"
133 (if (listp xml-data)
134 (jabber-tree-map (lambda (x) (if (numberp x) (format "%s" x) x)) xml-data)
135 xml-data))
137 (defun jabber-process-console (jc direction xml-data)
138 "Log XML-DATA i/o as XML in \"*-jabber-console-JID-*\" buffer"
139 (let ((buffer (get-buffer-create (jabber-console-create-buffer jc))))
140 (with-current-buffer buffer
141 (progn
142 (ewoc-enter-last jabber-console-ewoc (list direction (jabber-console-sanitize xml-data)))
143 (when (< 1 jabber-console-truncate-lines)
144 (let ((jabber-log-lines-to-keep jabber-console-truncate-lines))
145 (jabber-truncate-top buffer jabber-console-ewoc)))))))
147 (provide 'jabber-console)
148 ;;; jabber-console.el ends here