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
21 ;; Use *-jabber-console-* for sending custom XMPP code. Be careful!
25 (require 'jabber-keymap
)
26 (require 'jabber-util
)
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."
35 (defcustom jabber-console-truncate-lines
3000
36 "Maximum number of lines in console buffer.
37 Not truncate if set to 0"
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.")
56 (defvar jabber-buffer-connection nil
57 "The connection used by this buffer.")
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
)
67 (defun jabber-console-create-buffer (jc)
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
)
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."
85 comment-start str
"@" (jabber-encode-time (current-time)) ":"
87 (when (stringp jabber-debug-log-xml
)
88 (jabber-append-string-to-file string jabber-debug-log-xml
))
91 (defun jabber-console-pp (data)
92 "Pretty Printer for XML-sexp and raw data"
93 (let ((direction (car data
))
96 (jabber-console-comment direction
)
101 (when (stringp jabber-debug-log-xml
)
102 (jabber-append-string-to-file raw jabber-debug-log-xml
)))
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"
134 (jabber-tree-map (lambda (x) (if (numberp x
) (format "%s" x
) x
)) 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
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