Merge remote-tracking branch 'sourceforge/master'
[emacs-jabber.git] / jabber-logon.el
blob62bc6eac8b4dac2bee2ddfe3d8782f0a6c336ab9
1 ;; jabber-logon.el - logon 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 ;; This file is a part of jabber.el.
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program; if not, write to the Free Software
20 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 (require 'jabber-xml)
23 (require 'jabber-util)
24 ;; sha1-el is known under two names
25 (condition-case e
26 (require 'sha1)
27 (error (require 'sha1-el)))
29 (defun jabber-get-auth (jc to session-id)
30 "Send IQ get request in namespace \"jabber:iq:auth\"."
31 (jabber-send-iq jc to
32 "get"
33 `(query ((xmlns . "jabber:iq:auth"))
34 (username () ,(plist-get (fsm-get-state-data jc) :username)))
35 #'jabber-do-logon session-id
36 #'jabber-report-success "Impossible error - auth field request"))
38 (defun jabber-do-logon (jc xml-data session-id)
39 "send username and password in logon attempt"
40 (let* ((digest-allowed (jabber-xml-get-children (jabber-iq-query xml-data) 'digest))
41 (passwd (when
42 (or digest-allowed
43 (plist-get (fsm-get-state-data jc) :encrypted)
44 (yes-or-no-p "Jabber server only allows cleartext password transmission! Continue? "))
45 (or (plist-get (fsm-get-state-data jc) :password)
46 (jabber-read-password (jabber-connection-bare-jid jc)))))
47 auth)
48 (if (null passwd)
49 (fsm-send jc :authentication-failure)
50 (if digest-allowed
51 (setq auth `(digest () ,(sha1 (concat session-id passwd))))
52 (setq auth `(password () ,passwd)))
54 ;; For legacy authentication we must specify a resource.
55 (unless (plist-get (fsm-get-state-data jc) :resource)
56 ;; Yes, this is ugly. Where is my encapsulation?
57 (plist-put (fsm-get-state-data jc) :resource "emacs-jabber"))
59 (jabber-send-iq jc (plist-get (fsm-get-state-data jc) :server)
60 "set"
61 `(query ((xmlns . "jabber:iq:auth"))
62 (username () ,(plist-get (fsm-get-state-data jc) :username))
63 ,auth
64 (resource () ,(plist-get (fsm-get-state-data jc) :resource)))
65 #'jabber-process-logon passwd
66 #'jabber-process-logon nil))))
68 (defun jabber-process-logon (jc xml-data closure-data)
69 "receive login success or failure, and request roster.
70 CLOSURE-DATA should be the password on success and nil on failure."
71 (if closure-data
72 ;; Logon success
73 (fsm-send jc (cons :authentication-success closure-data))
75 ;; Logon failure
76 (jabber-report-success jc xml-data "Logon")
77 (fsm-send jc :authentication-failure)))
79 (provide 'jabber-logon)
81 ;;; arch-tag: f24ebe5e-3420-44bb-af81-d4de21f378b0