Revision: mange@freemail.hu--2005/emacs-jabber--cvs-head--0--patch-556
[emacs-jabber.git] / jabber-core.el
blob5376cd520f3bf2e128d1dd95ea6f5a7777a7b450
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
25 (require 'cl)
27 (require 'jabber-util)
28 (require 'jabber-logon)
29 (require 'jabber-conn)
30 (require 'fsm)
32 (require 'jabber-sasl)
34 (defvar jabber-connections nil
35 "List of jabber-connection FSMs.")
37 (defvar *jabber-roster* nil
38 "the roster list")
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"
67 :group 'jabber)
69 (defcustom jabber-post-connect-hooks '(jabber-send-current-presence
70 jabber-muc-autojoin)
71 "*Hooks run after successful connection and authentication.
72 The functions should accept one argument, the connection object."
73 :type 'hook
74 :group 'jabber-core)
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*'."
79 :type 'hook
80 :group 'jabber-core)
82 (defcustom jabber-lost-connection-hook nil
83 "*Hooks run after involuntary disconnection"
84 :type 'hook
85 :group 'jabber-core)
87 (defcustom jabber-post-disconnect-hook nil
88 "*Hooks run after disconnection"
89 :type 'hook
90 :group 'jabber-core)
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'."
97 :type 'boolean
98 :group 'jabber-core)
100 (defcustom jabber-reconnect-delay 5
101 "Seconds to wait before reconnecting"
102 :type 'integer
103 :group 'jabber-core)
105 (defcustom jabber-roster-buffer "*-jabber-*"
106 "The name of the roster buffer"
107 :type 'string
108 :group 'jabber-core)
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
119 problems."
120 :type 'boolean
121 :group 'jabber-core)
123 (defsubst jabber-have-sasl-p ()
124 "Return non-nil if SASL functions are available."
125 (featurep 'sasl))
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."
135 (interactive "P")
136 (let ((accounts
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))
144 (connected-one nil))
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))))
153 (jabber-connect
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."
167 (interactive
168 (let* ((jid (completing-read "Enter your JID: " jabber-account-list))
169 (entry (assoc jid jabber-account-list))
170 (alist (cdr entry))
171 password network-server port connection-type registerp)
172 (flet ((nonempty
174 (unless (zerop (length s)) s)))
175 (when entry
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)
185 (setq password nil)
186 (setq network-server
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))
191 (setq port
192 (car
193 (read-from-string
194 (read-string (format "Port: (default `%s') " port)
195 nil nil (if port (number-to-string port) "nil")))))
196 (setq connection-type
197 (car
198 (read-from-string
199 (let ((default (symbol-name (or connection-type jabber-default-connection-type))))
200 (completing-read
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))
208 (setq registerp t))
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
215 server)
216 (mapcar
217 (lambda (c)
218 (let ((data (fsm-get-state-data c)))
219 (list (plist-get data :username)
220 (plist-get data :server))))
221 jabber-connections))
222 (message "Already connected to %s@%s"
223 username server)
224 (setq *jabber-authenticated* nil)
225 ;;(jabber-clear-roster)
226 (jabber-reset-choked)
228 (push (start-jabber-connection username server resource
229 registerp password
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))
238 (send-function
239 (jabber-get-send-function connection-type)))
241 (list :connecting
242 (list :send-function send-function
243 :username username
244 :server server
245 :resource resource
246 :password password
247 :registerp registerp
248 :connection-type connection-type
249 :encrypted (eq connection-type 'ssl)
250 :network-server network-server
251 :port port)))))
253 (define-enter-state jabber-connection nil
254 (fsm state-data)
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)))
271 (unless expected
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)
277 reason))
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
283 ;; of connections.
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.
294 (case event
295 (:timeout
296 (list :connecting state-data))
297 (:do-disconnect
298 (setq jabber-connections
299 (delq fsm jabber-connections))
300 (list nil state-data nil))))
302 (define-enter-state jabber-connection :connecting
303 (fsm state-data)
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)
315 (:connected
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)
325 (erase-buffer))
327 (set-process-filter connection (fsm-make-filter fsm))
328 (set-process-sentinel connection (fsm-make-sentinel fsm)))
330 (list :connected state-data)))
332 (:connection-failed
333 (message "Jabber connection failed")
334 (list nil state-data))
336 (:do-disconnect
337 ;; We don't have the connection object, so defer the disconnection.
338 :defer)))
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)))
344 (new-state-data
345 ;; If we already know the reason (e.g. a stream error), don't
346 ;; overwrite it.
347 (if (plist-get state-data :disconnection-reason)
348 state-data
349 (plist-put state-data :disconnection-reason string))))
350 (list nil new-state-data)))
352 (define-enter-state jabber-connection :connected
353 (fsm state-data)
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)
372 (:filter
373 (let ((process (cadr event))
374 (string (car (cddr event))))
375 (jabber-pre-filter process string fsm)
376 (list :connected state-data)))
378 (:sentinel
379 (jabber-fsm-handle-sentinel state-data event))
381 (:stream-start
382 (let ((session-id (cadr event))
383 (stream-version (car (cddr event))))
384 (setq state-data
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.
390 (cond
391 ;; Wait for stream features?
392 ((and stream-version
393 (>= (string-to-number stream-version) 1.0)
394 jabber-use-sasl
395 (jabber-have-sasl-p))
396 ;; Stay in same state...
397 (list :connected state-data))
398 ;; Register account?
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)))))
406 (:stanza
407 (let ((stanza (cadr event)))
408 (cond
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
421 ;; that.
422 (list :register-account state-data))
424 (list :sasl-auth (plist-put state-data :stream-features stanza))))))
426 (:do-disconnect
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
432 (fsm state-data)
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)
439 (:filter
440 (let ((process (cadr event))
441 (string (car (cddr event))))
442 (jabber-pre-filter process string fsm)
443 (list :starttls state-data)))
445 (:sentinel
446 (jabber-fsm-handle-sentinel state-data event))
448 (:stanza
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)))
455 (:do-disconnect
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
461 (fsm state-data)
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)
469 (:filter
470 (let ((process (cadr event))
471 (string (car (cddr event))))
472 (jabber-pre-filter process string fsm)
473 (list :register-account state-data)))
475 (:sentinel
476 (jabber-fsm-handle-sentinel state-data event))
478 (:stanza
480 (jabber-process-stream-error (cadr event) state-data)
481 (progn
482 (jabber-process-input fsm (cadr event))
483 (list :register-account state-data))))
485 (:do-disconnect
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
491 (fsm state-data)
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)
499 (:filter
500 (let ((process (cadr event))
501 (string (car (cddr event))))
502 (jabber-pre-filter process string fsm)
503 (list :legacy-auth state-data)))
505 (:sentinel
506 (jabber-fsm-handle-sentinel state-data event))
508 (:stanza
510 (jabber-process-stream-error (cadr event) state-data)
511 (progn
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)))
525 (:do-disconnect
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
531 (fsm state-data)
532 (let ((new-state-data
533 (plist-put state-data
534 :sasl-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)
544 (:filter
545 (let ((process (cadr event))
546 (string (car (cddr event))))
547 (jabber-pre-filter process string fsm)
548 (list :sasl-auth state-data)))
550 (:sentinel
551 (jabber-fsm-handle-sentinel state-data event))
553 (:stanza
554 (let ((new-sasl-data
555 (jabber-sasl-process-input
556 fsm (cadr event)
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)))
573 (:do-disconnect
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
579 (fsm state-data)
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)
586 (:filter
587 (let ((process (cadr event))
588 (string (car (cddr event))))
589 (jabber-pre-filter process string fsm)
590 (list :bind state-data)))
592 (:sentinel
593 (jabber-fsm-handle-sentinel state-data event))
595 (:stream-start
596 ;; we wait for stream features...
597 (list :bind state-data))
599 (:stanza
600 (let ((stanza (cadr event)))
601 (cond
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))
605 (labels
606 ((handle-bind
607 (jc xml-data success)
608 (fsm-send jc (list
609 (if success :bind-success :bind-failure)
610 xml-data))))
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"))
616 ,@(when resource
617 `((resource () ,resource))))
618 #'handle-bind t
619 #'handle-bind nil))
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)
626 (progn
627 (jabber-process-input fsm (cadr event))
628 (list :bind state-data)))))))
630 (:bind-success
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.
638 (labels
639 ((handle-session
640 (jc xml-data success)
641 (fsm-send jc (list
642 (if success :session-success :session-failure)
643 xml-data))))
644 (jabber-send-iq fsm nil "set"
645 '(session ((xmlns . "urn:ietf:params:xml:ns:xmpp-session")))
646 #'handle-session t
647 #'handle-session nil)
648 (list :bind state-data)))
650 (:session-success
651 ;; We have a session
652 (list :session-established state-data))
654 (:bind-failure
655 (message "Resource binding failed: %s"
656 (jabber-parse-error
657 (jabber-iq-error (cadr event))))
658 (list nil state-data))
660 (:session-failure
661 (message "Session establishing failed: %s"
662 (jabber-parse-error
663 (jabber-iq-error (cadr event))))
664 (list nil state-data))
666 (:do-disconnect
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
672 (fsm state-data)
673 (jabber-send-iq fsm nil
674 "get"
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)
686 (:filter
687 (let ((process (cadr event))
688 (string (car (cddr event))))
689 (jabber-pre-filter process string fsm)
690 (list :session-established state-data :keep)))
692 (:sentinel
693 (jabber-fsm-handle-sentinel state-data event))
695 (:stanza
697 (jabber-process-stream-error (cadr event) state-data)
698 (progn
699 (jabber-process-input fsm (cadr event))
700 (list :session-established state-data :keep))))
702 (:roster-update
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.
708 (if pending-updates
709 (progn
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.
714 (setq state-data
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))))
720 (:timeout
721 ;; Update roster
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)))
727 (:send-if-connected
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))
734 (:do-disconnect
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."
741 (interactive "P")
742 (if arg
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))
776 (erase-buffer))))
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\".
790 DATA is any sexp."
791 (when jabber-debug-log-xml
792 (with-current-buffer (get-buffer-create (format "*-jabber-xml-log-%s-*" (jabber-connection-bare-jid fsm)))
793 (save-excursion
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)
799 ;; Append new data
800 (goto-char (point-max))
801 (insert string)
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))
812 (let (xml-data)
813 (loop
815 ;; Skip whitespace
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)))
822 ;; Stream end?
823 (when (looking-at "</stream:stream>")
824 (return (fsm-send fsm :stream-end)))
826 ;; Stream header?
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.
834 (setq session-id
835 (and (or (string-match "id='\\([^']+\\)'" stream-header)
836 (string-match "id=\"\\([^\"]+\\)\"" stream-header))
837 (jabber-unescape-xml (match-string 1 stream-header))))
838 (setq stream-version
839 (and (or
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)))
853 ;; Normal tag
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.
861 (save-excursion
862 (while (search-forward-regexp " \\w+=''" nil t)
863 (replace-match "")))
865 (setq xml-data (and (catch 'unfinished
866 (jabber-xml-skip-tag-forward)
867 (> (point) (point-min)))
868 (xml-parse-region (point-min) (point))))
869 (if xml-data
870 (jabber-reset-choked))
872 while xml-data
874 ;; If there's a problem with writing the XML log,
875 ;; make sure the stanza is delivered, at least.
876 (condition-case e
877 (jabber-log-xml fsm "receive" (car xml-data))
878 (error
879 (ding)
880 (message "Couldn't write XML log: %s" (error-message-string e))
881 (sit-for 2)))
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))
889 ))))
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.
918 Please go to
919 http://sourceforge.net/tracker/?group_id=88346&atid=586350 and
920 submit a bug report, including the information below.
923 (goto-address)
924 (emacs-version t)
925 (insert "\n\nThe following couldn't be parsed:\n")
926 (insert-buffer-substring (process-buffer *jabber-connection*))
927 (switch-to-buffer (current-buffer)))
928 (jabber-disconnect))
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)
937 (condition-case e
938 (funcall f jc xml-data)
939 ((debug error)
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
953 ;; automatically.
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))
966 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."
971 (condition-case e
972 (jabber-log-xml jc "sending" sexp)
973 (error
974 (ding)
975 (message "Couldn't write XML log: %s" (error-message-string e))
976 (sit-for 2)))
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."
985 (let ((stream-header
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)
992 " version='1.0'"
995 ")))
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)))
1004 (unless connection
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