2 ;; jabber-core.el - core functions -*- lexical-binding: t; -*-
4 ;; Copyright (C) 2003, 2004, 2007, 2008 - Magnus Henoch - mange@freemail.hu
5 ;; Copyright (C) 2002, 2003, 2004 - tom berger - object@intelectronica.net
7 ;; SSL-Connection Parts:
8 ;; Copyright (C) 2005 - Georg Lehner - jorge@magma.com.ni
10 ;; This file is a part of jabber.el.
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2 of the License, or
15 ;; (at your option) any later version.
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program; if not, write to the Free Software
24 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 ;; Standards (probably) involved -
29 ;; 1. [RFC 6120] Extensible Messaging and Presence Protocol (XMPP): Core
30 ;; https://datatracker.ietf.org/doc/rfc6120/
32 ;; 2. [RFC 7950] Use of Transport Layer Security (TLS) in the Extensible Messaging and Presence Protocol (XMPP)
33 ;; https://datatracker.ietf.org/doc/rfc7590/
35 ;; 3. [RFC 6121] Extensible Messaging and Presence Protocol (XMPP): Instant Messaging and Presence
36 ;; https://datatracker.ietf.org/doc/rfc6121/
38 ;; 4. [RFC 7622] Extensible Messaging and Presence Protocol (XMPP): Address Format
39 ;; https://datatracker.ietf.org/doc/rfc7622/
44 (require 'jabber-util)
45 (require 'jabber-logon)
46 (require 'jabber-conn)
50 (require 'jabber-sasl)
51 (require 'jabber-console)
53 (defvar jabber-connections nil
54 "List of jabber-connection FSMs.")
56 (defvar *jabber-roster* nil
59 (defvar jabber-jid-obarray (make-vector 127 0)
60 "Obarray for keeping JIDs.")
62 (defvar *jabber-disconnecting* nil
63 "Non-nil if are we in the process of voluntary disconnection.")
65 (defvar jabber-message-chain nil
66 "Incoming messages are sent to these functions, in order.")
68 (defvar jabber-iq-chain nil
69 "Incoming infoqueries are sent to these functions, in order.")
71 (defvar jabber-presence-chain nil
72 "Incoming presence notifications are sent to these functions, in order.")
74 (defvar jabber-namespace-prefixes nil
75 "XML namespace prefixes used for the current connection.")
76 (make-variable-buffer-local 'jabber-namespace-prefixes)
78 (defgroup jabber-core nil "customize core functionality."
81 (defcustom jabber-post-connect-hooks '(jabber-send-current-presence
83 jabber-whitespace-ping-start
84 jabber-vcard-avatars-find-current
85 jabber-enable-carbons)
86 "*Hooks run after successful connection and authentication.
87 The functions should accept one argument, the connection object."
89 :options '(jabber-send-current-presence
91 jabber-whitespace-ping-start
92 jabber-keepalive-start
93 jabber-vcard-avatars-find-current
94 jabber-autoaway-start)
97 (defcustom jabber-pre-disconnect-hook nil
98 "*Hooks run just before voluntary disconnection.
99 This might be due to failed authentication."
103 (defcustom jabber-lost-connection-hooks nil
104 "*Hooks run after involuntary disconnection.
105 The functions are called with one argument: the connection object."
109 (defcustom jabber-post-disconnect-hook nil
110 "*Hooks run after disconnection."
114 (defcustom jabber-auto-reconnect nil
115 "Reconnect automatically after losing connection?
116 This will be of limited use unless you have the password library
117 installed, and have configured it to cache your password
118 indefinitely. See `password-cache' and `password-cache-expiry'."
122 (defcustom jabber-reconnect-delay 5
123 "Seconds to wait before reconnecting."
127 (defcustom jabber-roster-buffer "*-jabber-roster-*"
128 "The name of the roster buffer."
132 (defcustom jabber-use-sasl t
133 "If non-nil, use SASL if possible.
134 SASL will still not be used if the library for it is missing or
135 if the server doesn't support it.
137 Disabling this shouldn't be necessary, but it may solve certain
142 (defsubst jabber-have-sasl-p ()
143 "Return non-nil if SASL functions are available."
146 (defvar jabber-account-history ()
147 "Keeps track of previously used jabber accounts.")
149 (defvar jabber-connection-type-history ()
150 "Keeps track of previously used connection types.")
152 ;; jabber-connect and jabber-connect-all should load jabber.el, not
153 ;; just jabber-core.el, when autoloaded.
155 ;;;###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)
156 (defun jabber-connect-all (&optional arg)
157 "Connect to all configured Jabber accounts.
158 See `jabber-account-list'.
159 If no accounts are configured (or with prefix argument), call `jabber-connect'
161 With many prefix arguments, one less is passed to `jabber-connect'."
164 (cl-remove-if (lambda (account)
165 (cdr (assq :disabled (cdr account))))
166 jabber-account-list)))
167 (if (or (null accounts) arg)
168 (let ((current-prefix-arg
170 ;; A number of C-u's; remove one, so to speak.
173 (list (/ (car arg) 4))
175 ;; Otherwise, we just don't care.
178 (call-interactively 'jabber-connect))
179 ;; Only connect those accounts that are not yet connected.
180 (let ((already-connected (mapcar #'jabber-connection-original-jid jabber-connections))
182 (dolist (account accounts)
183 (unless (member (jabber-jid-user (car account)) already-connected)
184 (let* ((jid (car account))
185 (alist (cdr account))
186 (password (cdr (assq :password alist)))
187 (network-server (cdr (assq :network-server alist)))
188 (port (cdr (assq :port alist)))
189 (connection-type (cdr (assq :connection-type alist))))
191 (jabber-jid-username jid)
192 (jabber-jid-server jid)
193 (jabber-jid-resource jid)
194 nil password network-server
195 port connection-type)
196 (setq connected-one t))))
197 (unless connected-one
198 (message "All configured Jabber accounts are already connected"))))))
200 ;;;###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)
201 (defun jabber-connect (username server resource &optional
202 registerp password network-server
203 port connection-type)
204 "Connect to the Jabber server and start a Jabber XML stream.
205 With prefix argument, register a new account.
206 With double prefix argument, specify more connection details."
208 (let* ((jid (completing-read "Enter your JID: " jabber-account-list nil nil nil 'jabber-account-history))
209 (entry (assoc jid jabber-account-list))
211 password network-server port connection-type registerp)
212 (when (zerop (length jid))
213 (error "No JID specified"))
214 (unless (jabber-jid-username jid)
215 (error "Missing username part in JID"))
217 ;; If the user entered the JID of one of the preconfigured
218 ;; accounts, use that data.
219 (setq password (cdr (assq :password alist)))
220 (setq network-server (cdr (assq :network-server alist)))
221 (setq port (cdr (assq :port alist)))
222 (setq connection-type (cdr (assq :connection-type alist))))
223 (when (equal current-prefix-arg '(16))
224 ;; Double prefix arg: ask about everything.
225 ;; (except password, which is asked about later anyway)
228 (read-string (format "Network server: (default `%s') " network-server)
229 nil nil network-server))
230 (when (zerop (length network-server))
231 (setq network-server nil))
235 (read-string (format "Port: (default `%s') " port)
236 nil nil (if port (number-to-string port) "nil")))))
237 (setq connection-type
240 (let ((default (symbol-name (or connection-type jabber-default-connection-type))))
242 (format "Connection type: (default `%s') " default)
243 (mapcar (lambda (type)
244 (cons (symbol-name (car type)) nil))
245 jabber-connect-methods)
246 nil t nil 'jabber-connection-type-history default)))))
247 (setq registerp (or jabber-silent-mode (yes-or-no-p "Register new account? "))))
248 (when (equal current-prefix-arg '(4))
251 (list (jabber-jid-username jid)
252 (jabber-jid-server jid)
253 (jabber-jid-resource jid)
254 registerp password network-server port connection-type)))
258 (if (member (list username
262 (let ((data (fsm-get-state-data c)))
263 (list (plist-get data :username)
264 (plist-get data :server))))
266 (message "Already connected to %s@%s"
268 ;;(jabber-clear-roster)
270 (push (start-jabber-connection username server resource
272 network-server port connection-type)
273 jabber-connections)))
275 (define-state-machine jabber-connection
276 :start ((username server resource registerp password network-server port connection-type)
277 "Start a Jabber connection."
278 (let* ((connection-type
279 (or connection-type jabber-default-connection-type))
281 (jabber-get-send-function connection-type)))
284 (list :send-function send-function
285 ;; Save the JID we originally connected with.
286 :original-jid (concat username "@" server)
292 :connection-type connection-type
293 :encrypted (eq connection-type 'ssl)
294 :network-server network-server
297 (define-enter-state jabber-connection nil
299 ;; `nil' is the error state.
301 ;; Close the network connection.
302 (let ((connection (plist-get state-data :connection)))
303 (when (processp connection)
304 (let ((process-buffer (process-buffer connection)))
305 (delete-process connection)
306 (when (and (bufferp process-buffer)
307 (not jabber-debug-keep-process-buffers))
308 (kill-buffer process-buffer)))))
309 (setq state-data (plist-put state-data :connection nil))
311 (jabber-muc-connection-closed (jabber-connection-bare-jid fsm))
312 ;; Remove lost connections from the roster buffer.
313 (jabber-display-roster)
314 (let ((expected (plist-get state-data :disconnection-expected))
315 (reason (plist-get state-data :disconnection-reason))
316 (ever-session-established (plist-get state-data :ever-session-established)))
318 (run-hook-with-args 'jabber-lost-connection-hooks fsm)
319 (message "%s@%s%s: connection lost: `%s'"
320 (plist-get state-data :username)
321 (plist-get state-data :server)
322 (if (plist-get state-data :resource)
323 (concat "/" (plist-get state-data :resource))
327 (if (and jabber-auto-reconnect (not expected) ever-session-established)
328 ;; Reconnect after a short delay?
329 (list state-data jabber-reconnect-delay)
330 ;; Else the connection is really dead. Remove it from the list
332 (setq jabber-connections
333 (delq fsm jabber-connections))
334 (when jabber-mode-line-mode
335 (jabber-mode-line-presence-update))
336 (jabber-display-roster)
337 ;; And let the FSM sleep...
338 (list state-data nil))))
340 (define-state jabber-connection nil
341 (fsm state-data event callback)
342 ;; In the `nil' state, the connection is dead. We wait for a
343 ;; :timeout message, meaning to reconnect, or :do-disconnect,
344 ;; meaning to cancel reconnection.
347 (list :connecting state-data))
349 (setq jabber-connections
350 (delq fsm jabber-connections))
351 (list nil state-data nil))))
353 (define-enter-state jabber-connection :connecting
355 (let* ((connection-type (plist-get state-data :connection-type))
356 (connect-function (jabber-get-connect-function connection-type))
357 (server (plist-get state-data :server))
358 (network-server (plist-get state-data :network-server))
359 (port (plist-get state-data :port)))
360 (funcall connect-function fsm server network-server port))
361 (list state-data nil))
363 (define-state jabber-connection :connecting
364 (fsm state-data event callback)
365 (cl-case (or (car-safe event) event)
367 (let ((connection (cadr event))
368 (registerp (plist-get state-data :registerp)))
370 (setq state-data (plist-put state-data :connection connection))
372 (when (processp connection)
373 ;; TLS connections leave data in the process buffer, which
374 ;; the XML parser will choke on.
375 (with-current-buffer (process-buffer connection)
378 (set-process-filter connection (fsm-make-filter fsm))
379 (set-process-sentinel connection (fsm-make-sentinel fsm)))
381 (list :connected state-data)))
384 (message "Jabber connection failed")
385 (plist-put state-data :disconnection-reason
386 (mapconcat #'identity (cadr event) "; "))
387 (list nil state-data))
390 ;; We don't have the connection object, so defer the disconnection.
393 (defsubst jabber-fsm-handle-sentinel (state-data event)
394 "Handle sentinel event for jabber fsm."
395 ;; We do the same thing for every state, so avoid code duplication.
396 (let* ((string (car (cddr event)))
397 ;; The event string sometimes (always?) has a trailing
398 ;; newline, that we don't care for.
400 (if (eq ?\n (aref string (1- (length string))))
401 (substring string 0 -1)
404 ;; If we already know the reason (e.g. a stream error), don't
406 (if (plist-get state-data :disconnection-reason)
408 (plist-put state-data :disconnection-reason trimmed-string))))
409 (list nil new-state-data)))
411 (define-enter-state jabber-connection :connected
414 (jabber-send-stream-header fsm)
416 ;; Next thing happening is the server sending its own <stream:stream> start tag.
418 (list state-data nil))
420 (define-state jabber-connection :connected
421 (fsm state-data event callback)
422 (cl-case (or (car-safe event) event)
424 (let ((process (cadr event))
425 (string (car (cddr event))))
426 (jabber-pre-filter process string fsm)
427 (list :connected state-data)))
430 (jabber-fsm-handle-sentinel state-data event))
433 (let ((session-id (cadr event))
434 (stream-version (car (cddr event))))
436 (plist-put state-data :session-id session-id))
437 ;; the stream feature is only sent if the initiating entity has
438 ;; sent 1.0 in the stream header. if sasl is not supported then
439 ;; we don't send 1.0 in the header and therefore we shouldn't wait
440 ;; even if 1.0 is present in the receiving stream.
442 ;; Wait for stream features?
444 (>= (string-to-number stream-version) 1.0)
446 (jabber-have-sasl-p))
447 ;; Stay in same state...
448 (list :connected state-data))
450 ((plist-get state-data :registerp)
451 ;; XXX: require encryption for registration?
452 (list :register-account state-data))
453 ;; Legacy authentication?
455 (list :legacy-auth state-data)))))
458 (let ((stanza (cadr event)))
460 ;; At this stage, we only expect a stream:features stanza.
461 ((not (eq (jabber-xml-node-name stanza) 'features))
462 (list nil (plist-put state-data
463 :disconnection-reason
464 (format "Unexpected stanza %s" stanza))))
465 ((and (jabber-xml-get-children stanza 'starttls)
466 (eq (plist-get state-data :connection-type) 'starttls))
467 (list :starttls state-data))
468 ;; XXX: require encryption for registration?
469 ((plist-get state-data :registerp)
470 ;; We could check for the <register/> element in stream
471 ;; features, but as a client we would only lose by doing
473 (list :register-account state-data))
475 (list :sasl-auth (plist-put state-data :stream-features stanza))))))
478 (jabber-send-string fsm "</stream:stream>")
479 (list nil (plist-put state-data
480 :disconnection-expected t)))))
482 (define-enter-state jabber-connection :starttls
484 (jabber-starttls-initiate fsm)
485 (list state-data nil))
487 (define-state jabber-connection :starttls
488 (fsm state-data event callback)
489 (cl-case (or (car-safe event) event)
491 (let ((process (cadr event))
492 (string (car (cddr event))))
493 (jabber-pre-filter process string fsm)
494 (list :starttls state-data)))
497 (jabber-fsm-handle-sentinel state-data event))
502 (jabber-starttls-process-input fsm (cadr event))
503 ;; Connection is encrypted. Send a stream tag again.
504 (list :connected (plist-put state-data :encrypted t)))
506 (let* ((msg (concat "STARTTLS negotiation failed: "
507 (error-message-string e)))
508 (new-state-data (plist-put state-data :disconnection-reason msg)))
509 (list nil new-state-data)))))
512 (jabber-send-string fsm "</stream:stream>")
513 (list nil (plist-put state-data
514 :disconnection-expected t)))))
516 (define-enter-state jabber-connection :register-account
518 (jabber-get-register fsm nil)
519 (list state-data nil))
521 (define-state jabber-connection :register-account
522 (fsm state-data event callback)
523 ;; The connection will be closed in jabber-register
524 (cl-case (or (car-safe event) event)
526 (let ((process (cadr event))
527 (string (car (cddr event))))
528 (jabber-pre-filter process string fsm)
529 (list :register-account state-data)))
532 (jabber-fsm-handle-sentinel state-data event))
536 (jabber-process-stream-error (cadr event) state-data)
538 (jabber-process-input fsm (cadr event))
539 (list :register-account state-data))))
542 (jabber-send-string fsm "</stream:stream>")
543 (list nil (plist-put state-data
544 :disconnection-expected t)))))
546 (define-enter-state jabber-connection :legacy-auth
548 (jabber-get-auth fsm (plist-get state-data :server)
549 (plist-get state-data :session-id))
550 (list state-data nil))
552 (define-state jabber-connection :legacy-auth
553 (fsm state-data event callback)
554 (cl-case (or (car-safe event) event)
556 (let ((process (cadr event))
557 (string (car (cddr event))))
558 (jabber-pre-filter process string fsm)
559 (list :legacy-auth state-data)))
562 (jabber-fsm-handle-sentinel state-data event))
566 (jabber-process-stream-error (cadr event) state-data)
568 (jabber-process-input fsm (cadr event))
569 (list :legacy-auth state-data))))
571 (:authentication-success
572 (jabber-cache-password (jabber-connection-bare-jid fsm) (cdr event))
573 (list :session-established state-data))
575 (:authentication-failure
576 (jabber-uncache-password (jabber-connection-bare-jid fsm))
577 ;; jabber-logon has already displayed a message
578 (list nil (plist-put state-data
579 :disconnection-expected t)))
582 (jabber-send-string fsm "</stream:stream>")
583 (list nil (plist-put state-data
584 :disconnection-expected t)))))
586 (define-enter-state jabber-connection :sasl-auth
588 (let ((new-state-data
589 (plist-put state-data
591 (jabber-sasl-start-auth
593 (plist-get state-data
594 :stream-features)))))
595 (list new-state-data nil)))
597 (define-state jabber-connection :sasl-auth
598 (fsm state-data event callback)
599 (cl-case (or (car-safe event) event)
601 (let ((process (cadr event))
602 (string (car (cddr event))))
603 (jabber-pre-filter process string fsm)
604 (list :sasl-auth state-data)))
607 (jabber-fsm-handle-sentinel state-data event))
611 (jabber-sasl-process-input
613 (plist-get state-data :sasl-data))))
614 (list :sasl-auth (plist-put state-data :sasl-data new-sasl-data))))
616 (:use-legacy-auth-instead
617 (list :legacy-auth (plist-put state-data :sasl-data nil)))
619 (:authentication-success
620 (jabber-cache-password (jabber-connection-bare-jid fsm) (cdr event))
621 (list :bind (plist-put state-data :sasl-data nil)))
623 (:authentication-failure
624 (jabber-uncache-password (jabber-connection-bare-jid fsm))
625 ;; jabber-sasl has already displayed a message
626 (list nil (plist-put state-data
627 :disconnection-expected t)))
630 (jabber-send-string fsm "</stream:stream>")
631 (list nil (plist-put state-data
632 :disconnection-expected t)))))
634 (define-enter-state jabber-connection :bind
636 (jabber-send-stream-header fsm)
637 (list state-data nil))
639 (define-state jabber-connection :bind
640 (fsm state-data event callback)
641 (cl-case (or (car-safe event) event)
643 (let ((process (cadr event))
644 (string (car (cddr event))))
645 (jabber-pre-filter process string fsm)
646 (list :bind state-data)))
649 (jabber-fsm-handle-sentinel state-data event))
652 ;; we wait for stream features...
653 (list :bind state-data))
656 (let ((stanza (cadr event)))
658 ((eq (jabber-xml-node-name stanza) 'features)
659 ;; Record stream features, discarding earlier data:
660 (setq state-data (plist-put state-data :stream-features stanza))
661 (if (jabber-xml-get-children stanza 'bind)
663 (lambda (jc xml-data success)
665 (if success :bind-success :bind-failure)
667 ;; So let's bind a resource. We can either pick a resource ourselves,
668 ;; or have the server pick one for us.
669 (resource (plist-get state-data :resource)))
670 (jabber-send-iq fsm nil "set"
671 `(bind ((xmlns . "urn:ietf:params:xml:ns:xmpp-bind"))
673 `((resource () ,resource))))
676 (list :bind state-data))
677 (message "Server doesn't permit resource binding")
678 (list nil state-data)))
681 (jabber-process-stream-error (cadr event) state-data)
683 (jabber-process-input fsm (cadr event))
684 (list :bind state-data)))))))
687 (let ((jid (jabber-xml-path (cadr event) '(bind jid ""))))
688 ;; Maybe this isn't the JID we asked for.
689 (plist-put state-data :username (jabber-jid-username jid))
690 (plist-put state-data :server (jabber-jid-server jid))
691 (plist-put state-data :resource (jabber-jid-resource jid)))
693 ;; If the server follows the older RFCs 3920 and 3921, it may
694 ;; offer session initiation here. If it follows RFCs 6120 and
695 ;; 6121, it might not offer it, and we should just skip it.
696 (if (jabber-xml-get-children (plist-get state-data :stream-features) 'session)
697 (let ((handle-session
698 (lambda (jc xml-data success)
700 (if success :session-success :session-failure)
702 (jabber-send-iq fsm nil "set"
703 '(session ((xmlns . "urn:ietf:params:xml:ns:xmpp-session")))
706 (list :bind state-data))
707 ;; Session establishment not offered - assume not necessary.
708 (list :session-established state-data)))
712 (list :session-established state-data))
715 (message "Resource binding failed: %s"
717 (jabber-iq-error (cadr event))))
718 (list nil state-data))
721 (message "Session establishing failed: %s"
723 (jabber-iq-error (cadr event))))
724 (list nil state-data))
727 (jabber-send-string fsm "</stream:stream>")
728 (list nil (plist-put state-data
729 :disconnection-expected t)))))
731 (defvar jabber-pending-presence-timeout 0.5
732 "Wait this long before doing presence packet batch processing.")
734 (define-enter-state jabber-connection :session-established
736 (jabber-send-iq fsm nil
738 '(query ((xmlns . "jabber:iq:roster")))
739 #'jabber-process-roster 'initial
740 #'jabber-initial-roster-failure nil)
741 (list (plist-put state-data :ever-session-established t) nil))
743 (define-state jabber-connection :session-established
744 (fsm state-data event callback)
745 (cl-case (or (car-safe event) event)
747 (let ((process (cadr event))
748 (string (car (cddr event))))
749 (jabber-pre-filter process string fsm)
750 (list :session-established state-data :keep)))
753 (jabber-fsm-handle-sentinel state-data event))
757 (jabber-process-stream-error (cadr event) state-data)
759 (jabber-process-input fsm (cadr event))
760 (list :session-established state-data :keep))))
763 ;; Batch up roster updates
764 (let* ((jid-symbol-to-update (cdr event))
765 (pending-updates (plist-get state-data :roster-pending-updates)))
766 ;; If there are pending updates, there is a timer running
767 ;; already; just add the new symbol and wait.
770 (unless (memq jid-symbol-to-update pending-updates)
771 (nconc pending-updates (list jid-symbol-to-update)))
772 (list :session-established state-data :keep))
773 ;; Otherwise, we need to create the list and start the timer.
775 (plist-put state-data
776 :roster-pending-updates
777 (list jid-symbol-to-update)))
778 (list :session-established state-data jabber-pending-presence-timeout))))
782 (let ((pending-updates (plist-get state-data :roster-pending-updates)))
783 (setq state-data (plist-put state-data :roster-pending-updates nil))
784 (jabber-roster-update fsm nil pending-updates nil)
785 (list :session-established state-data)))
788 ;; This is the only state in which we respond to such messages.
789 ;; This is to make sure we don't send anything inappropriate
790 ;; during authentication etc.
791 (jabber-send-sexp fsm (cdr event))
792 (list :session-established state-data :keep))
795 (jabber-send-string fsm "</stream:stream>")
796 (list nil (plist-put state-data
797 :disconnection-expected t)))))
799 (defun jabber-disconnect (&optional arg)
800 "Disconnect from all Jabber servers. If ARG supplied, disconnect one account."
803 (jabber-disconnect-one (jabber-read-account))
804 (unless *jabber-disconnecting* ; avoid reentry
805 (let ((*jabber-disconnecting* t))
806 (if (null jabber-connections)
807 (message "Already disconnected")
808 (run-hooks 'jabber-pre-disconnect-hook)
809 (dolist (c jabber-connections)
810 (jabber-disconnect-one c t))
811 (setq jabber-connections nil)
813 (jabber-disconnected)
814 (when (called-interactively-p 'interactive)
815 (message "Disconnected from Jabber server(s)")))))))
817 (defun jabber-disconnect-one (jc &optional dont-redisplay)
818 "Disconnect from one Jabber server.
819 If DONT-REDISPLAY is non-nil, don't update roster buffer.
820 JC is the Jabber connection."
821 (interactive (list (jabber-read-account)))
822 (fsm-send-sync jc :do-disconnect)
823 (when (called-interactively-p 'interactive)
824 (message "Disconnected from %s"
825 (jabber-connection-jid jc)))
826 (unless dont-redisplay
827 (jabber-display-roster)))
829 (defun jabber-disconnected ()
830 "Re-initialise jabber package variables.
831 Call this function after disconnection."
832 (when (get-buffer jabber-roster-buffer)
833 (with-current-buffer (get-buffer jabber-roster-buffer)
834 (let ((inhibit-read-only t))
837 (jabber-clear-roster)
838 (run-hooks 'jabber-post-disconnect-hook))
840 (defun jabber-log-xml (fsm direction data)
841 "Print DATA to XML console (and, optionally, in file).
842 If `jabber-debug-log-xml' is nil, do nothing.
843 FSM is the connection that is sending/receiving.
844 DIRECTION is a string, either \"sending\" or \"receive\".
846 (when jabber-debug-log-xml
847 (jabber-process-console fsm direction data)))
849 (defun jabber-pre-filter (process string fsm)
850 (with-current-buffer (process-buffer process)
852 (goto-char (point-max))
855 (unless (boundp 'jabber-filtering)
856 (let (jabber-filtering)
857 (jabber-filter process fsm)))))
859 (defun jabber-filter (process fsm)
860 "The filter function for the Jabber process."
861 (with-current-buffer (process-buffer process)
862 ;; Start from the beginning
863 (goto-char (point-min))
868 (unless (zerop (skip-chars-forward " \t\r\n"))
869 (delete-region (point-min) (point)))
870 ;; Skip processing directive
871 (when (looking-at "<\\?xml[^?]*\\?>")
872 (delete-region (match-beginning 0) (match-end 0)))
875 (when (looking-at "</stream:stream>")
876 (cl-return (fsm-send fsm :stream-end)))
879 (when (looking-at "<stream:stream[^>]*\\(>\\)")
880 ;; Let's pretend that the stream header is a closed tag,
881 ;; and parse it as such.
882 (replace-match "/>" t t nil 1)
883 (let* ((ending-at (point))
884 (stream-header (car (xml-parse-region (point-min) ending-at)))
885 (session-id (jabber-xml-get-attribute stream-header 'id))
886 (stream-version (jabber-xml-get-attribute stream-header 'version)))
888 ;; Need to keep any namespace attributes on the stream
889 ;; header, as they can affect any stanza in the
891 (setq jabber-namespace-prefixes
892 (jabber-xml-merge-namespace-declarations
893 (jabber-xml-node-attributes stream-header)
895 (jabber-log-xml fsm "receive" stream-header)
896 (fsm-send fsm (list :stream-start session-id stream-version))
897 (delete-region (point-min) ending-at)))
901 ;; XXX: do these checks make sense? If so, reinstate them.
902 ;;(if (active-minibuffer-window)
903 ;; (run-with-idle-timer 0.01 nil #'jabber-filter process string)
905 ;; This check is needed for xml.el of Emacs 21, as it chokes on
906 ;; empty attribute values.
908 (while (search-forward-regexp " \\w+=''" nil t)
911 (setq xml-data (jabber-xml-parse-next-stanza))
915 ;; If there's a problem with writing the XML log,
916 ;; make sure the stanza is delivered, at least.
918 (jabber-log-xml fsm "receive" (car xml-data))
921 (message "Couldn't write XML log: %s" (error-message-string e))
923 (delete-region (point-min) (point))
925 (fsm-send fsm (list :stanza
926 (jabber-xml-resolve-namespace-prefixes
927 (car xml-data) nil jabber-namespace-prefixes)))
928 ;; XXX: move this logic elsewhere
929 ;; We explicitly don't catch errors in jabber-process-input,
930 ;; to facilitate debugging.
931 ;; (jabber-process-input (car xml-data))
934 (defun jabber-process-input (jc xml-data)
935 "Process an incoming parsed tag.
937 JC is the Jabber connection.
938 XML-DATA is the parsed tree data from the stream (stanzas)
939 obtained from `xml-parse-region'."
940 (let* ((tag (jabber-xml-node-name xml-data))
941 (functions (eval (cdr (assq tag '((iq . jabber-iq-chain)
942 (presence . jabber-presence-chain)
943 (message . jabber-message-chain)))))))
944 (dolist (f functions)
946 (funcall f jc xml-data)
948 (fsm-debug-output "Error %S while processing %S with function %s" e xml-data f))))))
950 (defun jabber-process-stream-error (xml-data state-data)
951 "Process an incoming stream error.
952 Return nil if XML-DATA is not a stream:error stanza.
953 Return an fsm result list if it is."
954 (when (and (eq (jabber-xml-node-name xml-data) 'error)
955 (equal (jabber-xml-get-xmlns xml-data) "http://etherx.jabber.org/streams"))
956 (let ((condition (jabber-stream-error-condition xml-data))
957 (text (jabber-parse-stream-error xml-data)))
958 (setq state-data (plist-put state-data :disconnection-reason
959 (format "Stream error: %s" text)))
960 ;; Special case: when the error is `conflict', we have been
961 ;; forcibly disconnected by the same user. Don't reconnect
963 (when (eq condition 'conflict)
964 (setq state-data (plist-put state-data :disconnection-expected t)))
965 (list nil state-data))))
967 ;; XXX: This function should probably die. The roster is stored
968 ;; inside the connection plists, and the obarray shouldn't be so big
969 ;; that we need to clean it.
970 (defun jabber-clear-roster ()
971 "Clean up the roster."
972 ;; This is made complicated by the fact that the JIDs are symbols with properties.
973 (mapatoms #'(lambda (x)
974 (unintern x jabber-jid-obarray))
976 (setq *jabber-roster* nil))
978 (defun jabber-send-sexp (jc sexp)
979 "Send the xml corresponding to SEXP to connection JC."
981 (jabber-log-xml jc "sending" sexp)
984 (message "Couldn't write XML log: %s" (error-message-string e))
986 (jabber-send-string jc (jabber-sexp2xml sexp)))
988 (defun jabber-send-sexp-if-connected (jc sexp)
989 "Send the stanza SEXP only if JC has established a session."
990 (fsm-send-sync jc (cons :send-if-connected sexp)))
992 (defun jabber-send-stream-header (jc)
993 "Send stream header to connection JC."
995 (concat "<?xml version='1.0'?><stream:stream to='"
996 (plist-get (fsm-get-state-data jc) :server)
997 "' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'"
998 ;; Not supporting SASL is not XMPP compliant,
999 ;; so don't pretend we are.
1000 (if (and (jabber-have-sasl-p) jabber-use-sasl)
1005 (jabber-log-xml jc "sending" stream-header)
1006 (jabber-send-string jc stream-header)))
1008 (defun jabber-send-string (jc string)
1009 "Send STRING through the connection JC."
1010 (let* ((state-data (fsm-get-state-data jc))
1011 (connection (plist-get state-data :connection))
1012 (send-function (plist-get state-data :send-function)))
1014 (error "%s has no connection" (jabber-connection-jid jc)))
1015 (funcall send-function connection string)))
1017 (provide 'jabber-core)
1019 ;;; arch-tag: 9d273ce6-c45a-447b-abf3-21d3ce73a51a
1020 ;;; jabber-core.el ends here