1 ;; jabber-core.el - core functions
3 ;; Copyright (C) 2003, 2004, 2007, 2008 - Magnus Henoch - mange@freemail.hu
4 ;; Copyright (C) 2002, 2003, 2004 - tom berger - object@intelectronica.net
6 ;; SSL-Connection Parts:
7 ;; Copyright (C) 2005 - Georg Lehner - jorge@magma.com.ni
9 ;; This file is a part of jabber.el.
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2 of the License, or
14 ;; (at your option) any later version.
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program; if not, write to the Free Software
23 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 (require 'jabber-util
)
28 (require 'jabber-logon
)
29 (require 'jabber-conn
)
32 (require 'jabber-sasl
)
34 (defvar jabber-connections nil
35 "List of jabber-connection FSMs.")
37 (defvar *jabber-roster
* nil
40 (defvar jabber-jid-obarray
(make-vector 127 0)
41 "obarray for keeping JIDs")
43 (defvar *jabber-connected
* nil
44 "boolean - are we connected")
46 (defvar *jabber-authenticated
* nil
47 "boolean - are we authenticated")
49 (defvar *jabber-disconnecting
* nil
50 "boolean - are we in the process of disconnecting by free will")
52 (defvar jabber-message-chain nil
53 "Incoming messages are sent to these functions, in order.")
55 (defvar jabber-iq-chain nil
56 "Incoming infoqueries are sent to these functions, in order.")
58 (defvar jabber-presence-chain nil
59 "Incoming presence notifications are sent to these functions, in order.")
61 (defvar jabber-choked-count
0
62 "Number of successive times that the process buffer has been nonempty.")
64 (defvar jabber-choked-timer nil
)
66 (defgroup jabber-core nil
"customize core functionality"
69 (defcustom jabber-post-connect-hooks
'(jabber-send-current-presence
71 "*Hooks run after successful connection and authentication.
72 The functions should accept one argument, the connection object."
76 (defcustom jabber-pre-disconnect-hook nil
77 "*Hooks run just before voluntary disconnection
78 This might be due to failed authentication. Check `*jabber-authenticated*'."
82 (defcustom jabber-lost-connection-hook nil
83 "*Hooks run after involuntary disconnection"
87 (defcustom jabber-post-disconnect-hook nil
88 "*Hooks run after disconnection"
92 (defcustom jabber-auto-reconnect nil
93 "Reconnect automatically after losing connection?
94 This will be of limited use unless you have the password library
95 installed, and have configured it to cache your password
96 indefinitely. See `password-cache' and `password-cache-expiry'."
100 (defcustom jabber-reconnect-delay
5
101 "Seconds to wait before reconnecting"
105 (defcustom jabber-roster-buffer
"*-jabber-*"
106 "The name of the roster buffer"
110 (defvar jabber-process-buffer
" *-jabber-process-*"
111 "The name of the process buffer")
113 (defcustom jabber-use-sasl t
114 "If non-nil, use SASL if possible.
115 SASL will still not be used if the library for it is missing or
116 if the server doesn't support it.
118 Disabling this shouldn't be necessary, but it may solve certain
123 (defsubst jabber-have-sasl-p
()
124 "Return non-nil if SASL functions are available."
127 ;; jabber-connect and jabber-connect-all should load jabber.el, not
128 ;; just jabber-core.el, when autoloaded.
130 ;;;###autoload (autoload 'jabber-connect-all "jabber" "Connect to all configured Jabber accounts.\nSee `jabber-account-list'.\nIf no accounts are configured (or ARG supplied), call `jabber-connect' interactively." t)
131 (defun jabber-connect-all (&optional arg
)
132 "Connect to all configured Jabber accounts.
133 See `jabber-account-list'.
134 If no accounts are configured (or ARG supplied), call `jabber-connect' interactively."
137 (remove-if (lambda (account)
138 (cdr (assq :disabled
(cdr account
))))
139 jabber-account-list
)))
140 (if (or (null accounts
) arg
)
141 (progn (setq current-prefix-arg nil
) (call-interactively 'jabber-connect
))
142 ;; Only connect those accounts that are not yet connected.
143 (let ((already-connected (mapcar #'jabber-connection-bare-jid jabber-connections
))
145 (dolist (account accounts
)
146 (unless (member (jabber-jid-user (car account
)) already-connected
)
147 (let* ((jid (car account
))
148 (alist (cdr account
))
149 (password (cdr (assq :password alist
)))
150 (network-server (cdr (assq :network-server alist
)))
151 (port (cdr (assq :port alist
)))
152 (connection-type (cdr (assq :connection-type alist
))))
154 (jabber-jid-username jid
)
155 (jabber-jid-server jid
)
156 (jabber-jid-resource jid
)
157 nil password network-server
158 port connection-type
))))))))
160 ;;;###autoload (autoload 'jabber-connect "jabber" "Connect to the Jabber server and start a Jabber XML stream.\nWith prefix argument, register a new account.\nWith double prefix argument, specify more connection details." t)
161 (defun jabber-connect (username server resource
&optional
162 registerp password network-server
163 port connection-type
)
164 "Connect to the Jabber server and start a Jabber XML stream.
165 With prefix argument, register a new account.
166 With double prefix argument, specify more connection details."
168 (let* ((jid (completing-read "Enter your JID: " jabber-account-list
))
169 (entry (assoc jid jabber-account-list
))
171 password network-server port connection-type registerp
)
174 (unless (zerop (length s
)) s
)))
176 ;; If the user entered the JID of one of the preconfigured
177 ;; accounts, use that data.
178 (setq password
(cdr (assq :password alist
)))
179 (setq network-server
(cdr (assq :network-server alist
)))
180 (setq port
(cdr (assq :port alist
)))
181 (setq connection-type
(cdr (assq :connection-type alist
))))
182 (when (equal current-prefix-arg
'(16))
183 ;; Double prefix arg: ask about everything.
184 ;; (except password, which is asked about later anyway)
187 (read-string (format "Network server: (default `%s') " network-server
)
188 nil nil network-server
))
189 (when (zerop (length network-server
))
190 (setq network-server nil
))
194 (read-string (format "Port: (default `%s') " port
)
195 nil nil
(if port
(number-to-string port
) "nil")))))
196 (setq connection-type
199 (let ((default (symbol-name (or connection-type jabber-default-connection-type
))))
201 (format "Connection type: (default `%s') " default
)
202 (mapcar (lambda (type)
203 (cons (symbol-name (car type
)) nil
))
204 jabber-connect-methods
)
205 nil t nil nil default
)))))
206 (setq registerp
(yes-or-no-p "Register new account? ")))
207 (when (equal current-prefix-arg
'(4))
210 (list (jabber-jid-username jid
)
211 (jabber-jid-server jid
)
212 (jabber-jid-resource jid
)
213 registerp password network-server port connection-type
))))
214 (if (member (list username
218 (let ((data (fsm-get-state-data c
)))
219 (list (plist-get data
:username
)
220 (plist-get data
:server
))))
222 (message "Already connected to %s@%s"
224 (setq *jabber-authenticated
* nil
)
225 ;;(jabber-clear-roster)
226 (jabber-reset-choked)
228 (push (start-jabber-connection username server resource
230 network-server port connection-type
)
231 jabber-connections
)))
233 (define-state-machine jabber-connection
234 :start
((username server resource registerp password network-server port connection-type
)
235 "Start a Jabber connection."
236 (let* ((connection-type
237 (or connection-type jabber-default-connection-type
))
239 (jabber-get-send-function connection-type
)))
242 (list :send-function send-function
248 :connection-type connection-type
249 :encrypted
(eq connection-type
'ssl
)
250 :network-server network-server
253 (define-enter-state jabber-connection nil
255 ;; `nil' is the error state.
257 ;; Close the network connection.
258 (let ((connection (plist-get state-data
:connection
)))
259 (when (processp connection
)
260 (let ((process-buffer (process-buffer connection
)))
261 (delete-process connection
)
262 (when (and (bufferp process-buffer
)
263 (not jabber-debug-keep-process-buffers
))
264 (kill-buffer process-buffer
)))))
265 (setq state-data
(plist-put state-data
:connection nil
))
266 ;; Remove lost connections from the roster buffer.
267 (jabber-display-roster)
268 (let ((expected (plist-get state-data
:disconnection-expected
))
269 (reason (plist-get state-data
:disconnection-reason
))
270 (ever-session-established (plist-get state-data
:ever-session-established
)))
272 (run-hooks 'jabber-lost-connection-hook
)
273 (message "%s@%s/%s: connection lost: `%s'"
274 (plist-get state-data
:username
)
275 (plist-get state-data
:server
)
276 (plist-get state-data
:resource
)
279 (if (and jabber-auto-reconnect
(not expected
) ever-session-established
)
280 ;; Reconnect after a short delay?
281 (list state-data jabber-reconnect-delay
)
282 ;; Else the connection is really dead. Remove it from the list
284 (setq jabber-connections
285 (delq fsm jabber-connections
))
286 ;; And let the FSM sleep...
287 (list state-data nil
))))
289 (define-state jabber-connection nil
290 (fsm state-data event callback
)
291 ;; In the `nil' state, the connection is dead. We wait for a
292 ;; :timeout message, meaning to reconnect, or :do-disconnect,
293 ;; meaning to cancel reconnection.
296 (list :connecting state-data
))
298 (setq jabber-connections
299 (delq fsm jabber-connections
))
300 (list nil state-data nil
))))
302 (define-enter-state jabber-connection
:connecting
304 (let* ((connection-type (plist-get state-data
:connection-type
))
305 (connect-function (jabber-get-connect-function connection-type
))
306 (server (plist-get state-data
:server
))
307 (network-server (plist-get state-data
:network-server
))
308 (port (plist-get state-data
:port
)))
309 (funcall connect-function fsm server network-server port
))
310 (list state-data nil
))
312 (define-state jabber-connection
:connecting
313 (fsm state-data event callback
)
314 (case (or (car-safe event
) event
)
316 (let ((connection (cadr event
))
317 (registerp (plist-get state-data
:registerp
)))
319 (setq state-data
(plist-put state-data
:connection connection
))
321 (when (processp connection
)
322 ;; TLS connections leave data in the process buffer, which
323 ;; the XML parser will choke on.
324 (with-current-buffer (process-buffer connection
)
327 (set-process-filter connection
(fsm-make-filter fsm
))
328 (set-process-sentinel connection
(fsm-make-sentinel fsm
)))
330 (list :connected state-data
)))
333 (message "Jabber connection failed")
334 (list nil state-data
))
337 ;; We don't have the connection object, so defer the disconnection.
340 (defsubst jabber-fsm-handle-sentinel
(state-data event
)
341 "Handle sentinel event for jabber fsm."
342 ;; We do the same thing for every state, so avoid code duplication.
343 (let* ((string (car (cddr event
)))
345 ;; If we already know the reason (e.g. a stream error), don't
347 (if (plist-get state-data
:disconnection-reason
)
349 (plist-put state-data
:disconnection-reason string
))))
350 (list nil new-state-data
)))
352 (define-enter-state jabber-connection
:connected
355 (jabber-send-stream-header fsm
)
357 ;; XXX: Update to multiaccount? Remove?
358 ;; (setq jabber-choked-timer
359 ;; (run-with-timer 5 5 #'jabber-check-choked))
361 ;;XXX: why is this here? I'll try commenting it out...
362 ;;(accept-process-output *jabber-connection*)
364 ;; Next thing happening is the server sending its own <stream:stream> start tag.
366 (setq *jabber-connected
* t
)
367 (list state-data nil
))
369 (define-state jabber-connection
:connected
370 (fsm state-data event callback
)
371 (case (or (car-safe event
) event
)
373 (let ((process (cadr event
))
374 (string (car (cddr event
))))
375 (jabber-pre-filter process string fsm
)
376 (list :connected state-data
)))
379 (jabber-fsm-handle-sentinel state-data event
))
382 (let ((session-id (cadr event
))
383 (stream-version (car (cddr event
))))
385 (plist-put state-data
:session-id session-id
))
386 ;; the stream feature is only sent if the initiating entity has
387 ;; sent 1.0 in the stream header. if sasl is not supported then
388 ;; we don't send 1.0 in the header and therefore we shouldn't wait
389 ;; even if 1.0 is present in the receiving stream.
391 ;; Wait for stream features?
393 (>= (string-to-number stream-version
) 1.0)
395 (jabber-have-sasl-p))
396 ;; Stay in same state...
397 (list :connected state-data
))
399 ((plist-get state-data
:registerp
)
400 ;; XXX: require encryption for registration?
401 (list :register-account state-data
))
402 ;; Legacy authentication?
404 (list :legacy-auth state-data
)))))
407 (let ((stanza (cadr event
)))
409 ;; At this stage, we only expect a stream:features stanza.
410 ((not (eq (jabber-xml-node-name stanza
) 'stream
:features
))
411 (list nil
(plist-put state-data
412 :disconnection-reason
413 (format "Unexpected stanza %s" stanza
))))
414 ((and (jabber-xml-get-children stanza
'starttls
)
415 (eq (plist-get state-data
:connection-type
) 'starttls
))
416 (list :starttls state-data
))
417 ;; XXX: require encryption for registration?
418 ((plist-get state-data
:registerp
)
419 ;; We could check for the <register/> element in stream
420 ;; features, but as a client we would only lose by doing
422 (list :register-account state-data
))
424 (list :sasl-auth
(plist-put state-data
:stream-features stanza
))))))
427 (jabber-send-string fsm
"</stream:stream>")
428 (list nil
(plist-put state-data
429 :disconnection-expected t
)))))
431 (define-enter-state jabber-connection
:starttls
433 (jabber-starttls-initiate fsm
)
434 (list state-data nil
))
436 (define-state jabber-connection
:starttls
437 (fsm state-data event callback
)
438 (case (or (car-safe event
) event
)
440 (let ((process (cadr event
))
441 (string (car (cddr event
))))
442 (jabber-pre-filter process string fsm
)
443 (list :starttls state-data
)))
446 (jabber-fsm-handle-sentinel state-data event
))
449 (if (jabber-starttls-process-input fsm
(cadr event
))
450 ;; Connection is encrypted. Send a stream tag again.
451 (list :connected
(plist-put state-data
:encrypted t
))
452 (message "STARTTLS negotiation failed")
453 (list nil state-data
)))
456 (jabber-send-string fsm
"</stream:stream>")
457 (list nil
(plist-put state-data
458 :disconnection-expected t
)))))
460 (define-enter-state jabber-connection
:register-account
462 (jabber-get-register fsm nil
)
463 (list state-data nil
))
465 (define-state jabber-connection
:register-account
466 (fsm state-data event callback
)
467 ;; The connection will be closed in jabber-register
468 (case (or (car-safe event
) event
)
470 (let ((process (cadr event
))
471 (string (car (cddr event
))))
472 (jabber-pre-filter process string fsm
)
473 (list :register-account state-data
)))
476 (jabber-fsm-handle-sentinel state-data event
))
480 (jabber-process-stream-error (cadr event
) state-data
)
482 (jabber-process-input fsm
(cadr event
))
483 (list :register-account state-data
))))
486 (jabber-send-string fsm
"</stream:stream>")
487 (list nil
(plist-put state-data
488 :disconnection-expected t
)))))
490 (define-enter-state jabber-connection
:legacy-auth
492 (jabber-get-auth fsm
(plist-get state-data
:server
)
493 (plist-get state-data
:session-id
))
494 (list state-data nil
))
496 (define-state jabber-connection
:legacy-auth
497 (fsm state-data event callback
)
498 (case (or (car-safe event
) event
)
500 (let ((process (cadr event
))
501 (string (car (cddr event
))))
502 (jabber-pre-filter process string fsm
)
503 (list :legacy-auth state-data
)))
506 (jabber-fsm-handle-sentinel state-data event
))
510 (jabber-process-stream-error (cadr event
) state-data
)
512 (jabber-process-input fsm
(cadr event
))
513 (list :legacy-auth state-data
))))
515 (:authentication-success
516 (jabber-cache-password (jabber-connection-bare-jid fsm
) (cdr event
))
517 (list :session-established state-data
))
519 (:authentication-failure
520 (jabber-uncache-password (jabber-connection-bare-jid fsm
))
521 ;; jabber-logon has already displayed a message
522 (list nil
(plist-put state-data
523 :disconnection-expected t
)))
526 (jabber-send-string fsm
"</stream:stream>")
527 (list nil
(plist-put state-data
528 :disconnection-expected t
)))))
530 (define-enter-state jabber-connection
:sasl-auth
532 (let ((new-state-data
533 (plist-put state-data
535 (jabber-sasl-start-auth
537 (plist-get state-data
538 :stream-features
)))))
539 (list new-state-data nil
)))
541 (define-state jabber-connection
:sasl-auth
542 (fsm state-data event callback
)
543 (case (or (car-safe event
) event
)
545 (let ((process (cadr event
))
546 (string (car (cddr event
))))
547 (jabber-pre-filter process string fsm
)
548 (list :sasl-auth state-data
)))
551 (jabber-fsm-handle-sentinel state-data event
))
555 (jabber-sasl-process-input
557 (plist-get state-data
:sasl-data
))))
558 (list :sasl-auth
(plist-put state-data
:sasl-data new-sasl-data
))))
560 (:use-legacy-auth-instead
561 (list :legacy-auth
(plist-put state-data
:sasl-data nil
)))
563 (:authentication-success
564 (jabber-cache-password (jabber-connection-bare-jid fsm
) (cdr event
))
565 (list :bind
(plist-put state-data
:sasl-data nil
)))
567 (:authentication-failure
568 (jabber-uncache-password (jabber-connection-bare-jid fsm
))
569 ;; jabber-sasl has already displayed a message
570 (list nil
(plist-put state-data
571 :disconnection-expected t
)))
574 (jabber-send-string fsm
"</stream:stream>")
575 (list nil
(plist-put state-data
576 :disconnection-expected t
)))))
578 (define-enter-state jabber-connection
:bind
580 (jabber-send-stream-header fsm
)
581 (list state-data nil
))
583 (define-state jabber-connection
:bind
584 (fsm state-data event callback
)
585 (case (or (car-safe event
) event
)
587 (let ((process (cadr event
))
588 (string (car (cddr event
))))
589 (jabber-pre-filter process string fsm
)
590 (list :bind state-data
)))
593 (jabber-fsm-handle-sentinel state-data event
))
596 ;; we wait for stream features...
597 (list :bind state-data
))
600 (let ((stanza (cadr event
)))
602 ((eq (jabber-xml-node-name stanza
) 'stream
:features
)
603 (if (and (jabber-xml-get-children stanza
'bind
)
604 (jabber-xml-get-children stanza
'session
))
607 (jc xml-data success
)
609 (if success
:bind-success
:bind-failure
)
611 ;; So let's bind a resource. We can either pick a resource ourselves,
612 ;; or have the server pick one for us.
613 (let ((resource (plist-get state-data
:resource
)))
614 (jabber-send-iq fsm nil
"set"
615 `(bind ((xmlns .
"urn:ietf:params:xml:ns:xmpp-bind"))
617 `((resource () ,resource
))))
620 (list :bind state-data
))
621 (message "Server doesn't permit resource binding and session establishing")
622 (list nil state-data
)))
625 (jabber-process-stream-error (cadr event
) state-data
)
627 (jabber-process-input fsm
(cadr event
))
628 (list :bind state-data
)))))))
631 (let ((jid (jabber-xml-path (cadr event
) '(bind jid
""))))
632 ;; Maybe this isn't the JID we asked for.
633 (plist-put state-data
:username
(jabber-jid-username jid
))
634 (plist-put state-data
:server
(jabber-jid-server jid
))
635 (plist-put state-data
:resource
(jabber-jid-resource jid
)))
637 ;; Been there, done that. Time to establish a session.
640 (jc xml-data success
)
642 (if success
:session-success
:session-failure
)
644 (jabber-send-iq fsm nil
"set"
645 '(session ((xmlns .
"urn:ietf:params:xml:ns:xmpp-session")))
647 #'handle-session nil
)
648 (list :bind state-data
)))
652 (list :session-established state-data
))
655 (message "Resource binding failed: %s"
657 (jabber-iq-error (cadr event
))))
658 (list nil state-data
))
661 (message "Session establishing failed: %s"
663 (jabber-iq-error (cadr event
))))
664 (list nil state-data
))
667 (jabber-send-string fsm
"</stream:stream>")
668 (list nil
(plist-put state-data
669 :disconnection-expected t
)))))
671 (define-enter-state jabber-connection
:session-established
673 (jabber-send-iq fsm nil
675 '(query ((xmlns .
"jabber:iq:roster")))
676 #'jabber-process-roster
'initial
677 #'jabber-report-success
"Roster retrieval")
678 (list (plist-put state-data
:ever-session-established t
) nil
))
680 (defvar jabber-pending-presence-timeout
0.5
681 "Wait this long before doing presence packet batch processing.")
683 (define-state jabber-connection
:session-established
684 (fsm state-data event callback
)
685 (case (or (car-safe event
) event
)
687 (let ((process (cadr event
))
688 (string (car (cddr event
))))
689 (jabber-pre-filter process string fsm
)
690 (list :session-established state-data
:keep
)))
693 (jabber-fsm-handle-sentinel state-data event
))
697 (jabber-process-stream-error (cadr event
) state-data
)
699 (jabber-process-input fsm
(cadr event
))
700 (list :session-established state-data
:keep
))))
703 ;; Batch up roster updates
704 (let* ((jid-symbol-to-update (cdr event
))
705 (pending-updates (plist-get state-data
:roster-pending-updates
)))
706 ;; If there are pending updates, there is a timer running
707 ;; already; just add the new symbol and wait.
710 (unless (memq jid-symbol-to-update pending-updates
)
711 (nconc pending-updates
(list jid-symbol-to-update
)))
712 (list :session-established state-data
:keep
))
713 ;; Otherwise, we need to create the list and start the timer.
715 (plist-put state-data
716 :roster-pending-updates
717 (list jid-symbol-to-update
)))
718 (list :session-established state-data jabber-pending-presence-timeout
))))
722 (let ((pending-updates (plist-get state-data
:roster-pending-updates
)))
723 (setq state-data
(plist-put state-data
:roster-pending-updates nil
))
724 (jabber-roster-update fsm nil pending-updates nil
)
725 (list :session-established state-data
)))
728 ;; This is the only state in which we respond to such messages.
729 ;; This is to make sure we don't send anything inappropriate
730 ;; during authentication etc.
731 (jabber-send-sexp fsm
(cdr event
))
732 (list :session-established state-data
:keep
))
735 (jabber-send-string fsm
"</stream:stream>")
736 (list nil
(plist-put state-data
737 :disconnection-expected t
)))))
739 (defun jabber-disconnect (&optional arg
)
740 "Disconnect from all Jabber servers. If ARG supplied, disconnect one account."
743 (jabber-disconnect-one (jabber-read-account))
744 (unless *jabber-disconnecting
* ; avoid reentry
745 (let ((*jabber-disconnecting
* t
))
746 (dolist (c jabber-connections
)
747 (jabber-disconnect-one c t
))
748 (setq jabber-connections nil
)
750 (jabber-disconnected)
751 (when (interactive-p)
752 (message "Disconnected from Jabber server(s)"))))))
754 (defun jabber-disconnect-one (jc &optional dont-redisplay
)
755 "Disconnect from one Jabber server.
756 If DONT-REDISPLAY is non-nil, don't update roster buffer."
757 (interactive (list (jabber-read-account)))
758 ;;(run-hooks 'jabber-pre-disconnect-hook)
759 (fsm-send-sync jc
:do-disconnect
)
760 (when (interactive-p)
761 (message "Disconnected from %s"
762 (jabber-connection-jid jc
)))
763 (unless dont-redisplay
764 (jabber-display-roster)))
766 (defun jabber-disconnected ()
767 "Re-initialise jabber package variables.
768 Call this function after disconnection."
769 (when jabber-choked-timer
770 (jabber-cancel-timer jabber-choked-timer
)
771 (setq jabber-choked-timer nil
))
773 (when (get-buffer jabber-roster-buffer
)
774 (with-current-buffer (get-buffer jabber-roster-buffer
)
775 (let ((inhibit-read-only t
))
778 (setq *jabber-connection
* nil
)
779 (jabber-clear-roster)
780 (setq *jabber-authenticated
* nil
)
781 (setq *jabber-connected
* nil
)
782 (setq *jabber-active-groupchats
* nil
)
783 (run-hooks 'jabber-post-disconnect-hook
))
785 (defun jabber-log-xml (fsm direction data
)
786 "Print DATA to XML log.
787 If `jabber-debug-log-xml' is nil, do nothing.
788 FSM is the connection that is sending/receiving.
789 DIRECTION is a string, either \"sending\" or \"receive\".
791 (when jabber-debug-log-xml
792 (with-current-buffer (get-buffer-create (format "*-jabber-xml-log-%s-*" (jabber-connection-bare-jid fsm
)))
794 (goto-char (point-max))
795 (insert (format "%s %S\n\n" direction data
))))))
797 (defun jabber-pre-filter (process string fsm
)
798 (with-current-buffer (process-buffer process
)
800 (goto-char (point-max))
803 (unless (boundp 'jabber-filtering
)
804 (let (jabber-filtering)
805 (jabber-filter process fsm
)))))
807 (defun jabber-filter (process fsm
)
808 "the filter function for the jabber process"
809 (with-current-buffer (process-buffer process
)
810 ;; Start from the beginning
811 (goto-char (point-min))
816 (unless (zerop (skip-chars-forward " \t\r\n"))
817 (delete-region (point-min) (point)))
818 ;; Skip processing directive
819 (when (looking-at "<\\?xml[^?]*\\?>")
820 (delete-region (match-beginning 0) (match-end 0)))
823 (when (looking-at "</stream:stream>")
824 (return (fsm-send fsm
:stream-end
)))
827 (when (looking-at "<stream:stream[^>]*>")
828 (let ((stream-header (match-string 0))
829 (ending-at (match-end 0))
830 session-id stream-version
)
831 ;; These regexps extract attribute values from the stream
832 ;; header, taking into account that the quotes may be either
833 ;; single or double quotes.
835 (and (or (string-match "id='\\([^']+\\)'" stream-header
)
836 (string-match "id=\"\\([^\"]+\\)\"" stream-header
))
837 (jabber-unescape-xml (match-string 1 stream-header
))))
840 (string-match "version='\\([0-9.]+\\)'" stream-header
)
841 (string-match "version=\"\\([0-9.]+\\)\"" stream-header
))
842 (match-string 1 stream-header
)))
843 (jabber-log-xml fsm
"receive" stream-header
)
845 ;; If the server is XMPP compliant, i.e. there is a version attribute
846 ;; and it's >= 1.0, there will be a stream:features tag shortly,
847 ;; so just wait for that.
849 (fsm-send fsm
(list :stream-start session-id stream-version
))
851 (delete-region (point-min) ending-at
)))
855 ;; XXX: do these checks make sense? If so, reinstate them.
856 ;;(if (active-minibuffer-window)
857 ;; (run-with-idle-timer 0.01 nil #'jabber-filter process string)
859 ;; This check is needed for xml.el of Emacs 21, as it chokes on
860 ;; empty attribute values.
862 (while (search-forward-regexp " \\w+=''" nil t
)
865 (setq xml-data
(and (catch 'unfinished
866 (jabber-xml-skip-tag-forward)
867 (> (point) (point-min)))
868 (xml-parse-region (point-min) (point))))
870 (jabber-reset-choked))
874 ;; If there's a problem with writing the XML log,
875 ;; make sure the stanza is delivered, at least.
877 (jabber-log-xml fsm
"receive" (car xml-data
))
880 (message "Couldn't write XML log: %s" (error-message-string e
))
882 (delete-region (point-min) (point))
884 (fsm-send fsm
(list :stanza
(car xml-data
)))
885 ;; XXX: move this logic elsewhere
886 ;; We explicitly don't catch errors in jabber-process-input,
887 ;; to facilitate debugging.
888 ;; (jabber-process-input (car xml-data))
891 (defun jabber-reset-choked ()
892 (setq jabber-choked-count
0))
894 (defun jabber-check-choked ()
895 ;; "Choked" means that data is sitting in the process buffer
896 ;; without being parsed, despite several attempts.
897 (if (zerop (buffer-size (process-buffer *jabber-connection
*)))
898 (jabber-reset-choked)
899 (incf jabber-choked-count
)
900 (if (and (> jabber-choked-count
3)
901 ;; Now we're definitely choked. Take action.
902 ;; But ask user first.
903 (yes-or-no-p "jabber.el is severely confused. Bail out? "))
904 (run-with-idle-timer 0.1 nil
'jabber-choked-bail-out
)
905 (jabber-reset-choked))))
907 (defun jabber-choked-bail-out ()
908 ;; So here we are. Something in the process buffer prevents us
909 ;; from continuing normally. Let's die honorably by providing
910 ;; bug report material.
911 (with-current-buffer (generate-new-buffer "*jabber-bug*")
912 (insert "jabber.el couldn't cope with the data received from the server.
913 This should never happen, but apparently it did.
915 The information below will be helpful in tracking down and fixing
916 the bug. You may want to edit out any sensitive information.
919 http://sourceforge.net/tracker/?group_id=88346&atid=586350 and
920 submit a bug report, including the information below.
925 (insert "\n\nThe following couldn't be parsed:\n")
926 (insert-buffer-substring (process-buffer *jabber-connection
*))
927 (switch-to-buffer (current-buffer)))
930 (defun jabber-process-input (jc xml-data
)
931 "process an incoming parsed tag"
932 (let* ((tag (jabber-xml-node-name xml-data
))
933 (functions (eval (cdr (assq tag
'((iq . jabber-iq-chain
)
934 (presence . jabber-presence-chain
)
935 (message . jabber-message-chain
)))))))
936 (dolist (f functions
)
938 (funcall f jc xml-data
)
940 (fsm-debug-output "Error %S while processing %S with function %s" e xml-data f
))))))
942 (defun jabber-process-stream-error (xml-data state-data
)
943 "Process an incoming stream error.
944 Return nil if XML-DATA is not a stream:error stanza.
945 Return an fsm result list if it is."
946 (when (eq (jabber-xml-node-name xml-data
) 'stream
:error
)
947 (let ((condition (jabber-stream-error-condition xml-data
))
948 (text (jabber-parse-stream-error xml-data
)))
949 (setq state-data
(plist-put state-data
:disconnection-reason
950 (format "Stream error: %s" text
)))
951 ;; Special case: when the error is `conflict', we have been
952 ;; forcibly disconnected by the same user. Don't reconnect
954 (when (eq condition
'conflict
)
955 (setq state-data
(plist-put state-data
:disconnection-expected t
)))
956 (list nil state-data
))))
958 ;; XXX: This function should probably die. The roster is stored
959 ;; inside the connection plists, and the obarray shouldn't be so big
960 ;; that we need to clean it.
961 (defun jabber-clear-roster ()
962 "Clean up the roster."
963 ;; This is made complicated by the fact that the JIDs are symbols with properties.
964 (mapatoms #'(lambda (x)
965 (unintern x jabber-jid-obarray
))
967 (setq *jabber-roster
* nil
))
969 (defun jabber-send-sexp (jc sexp
)
970 "Send the xml corresponding to SEXP to connection JC."
972 (jabber-log-xml jc
"sending" sexp
)
975 (message "Couldn't write XML log: %s" (error-message-string e
))
977 (jabber-send-string jc
(jabber-sexp2xml sexp
)))
979 (defun jabber-send-sexp-if-connected (jc sexp
)
980 "Send the stanza SEXP only if JC has established a session."
981 (fsm-send-sync jc
(cons :send-if-connected sexp
)))
983 (defun jabber-send-stream-header (jc)
984 "Send stream header to connection JC."
986 (concat "<?xml version='1.0'?><stream:stream to='"
987 (plist-get (fsm-get-state-data jc
) :server
)
988 "' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'"
989 ;; Not supporting SASL is not XMPP compliant,
990 ;; so don't pretend we are.
991 (if (and (jabber-have-sasl-p) jabber-use-sasl
)
996 (jabber-log-xml jc
"sending" stream-header
)
997 (jabber-send-string jc stream-header
)))
999 (defun jabber-send-string (jc string
)
1000 "Send STRING to the connection JC."
1001 (let* ((state-data (fsm-get-state-data jc
))
1002 (connection (plist-get state-data
:connection
))
1003 (send-function (plist-get state-data
:send-function
)))
1005 (error "%s has no connection" (jabber-connection-jid jc
)))
1006 (funcall send-function connection string
)))
1008 (provide 'jabber-core
)
1010 ;;; arch-tag: 9d273ce6-c45a-447b-abf3-21d3ce73a51a