glooxd  0.3-svn
featureresourcebind.cpp
1 /*
2  Copyright (c) 2009 by Jakob Schroeter <js@camaya.net>
3  This file is part of the glooxd library. http://camaya.net/glooxd
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 #include "featureresourcebind.h"
14 #include "sm.h"
15 #include "streambase.h"
16 
17 #include <gloox/adhoc.h>
18 #include <gloox/dataform.h>
19 #include <gloox/gloox.h>
20 #include <gloox/iq.h>
21 
22 #include <cstdio>
23 
24 namespace glooxd
25 {
26 
28  : m_sm( sm ), m_state( BindCanceled )
29  {
30  }
31 
32  const std::string& FeatureResourceBind::filterString() const
33  {
34  static const std::string filter = "/iq/bind[@xmlns='" + gloox::XMLNS_STREAM_BIND + "']";
35  return filter;
36  }
37 
38  gloox::Tag* FeatureResourceBind::tag( int state, const std::string& /*domain*/ )
39  {
40  gloox::Tag* t = 0;
41 
42  if( ( state & StateAuthentication ) == StateAuthentication )
43  {
44  t = new gloox::Tag( "bind" );
45  t->setXmlns( gloox::XMLNS_STREAM_BIND );
46  m_state = BindOffered;
47  }
48 
49  return t;
50  }
51 
52  void FeatureResourceBind::handleTag( gloox::Tag* _tag )
53  {
54  if( !_tag )
55  return;
56 
57  if( m_state == BindOffered && _tag->name() == "iq" )
58  {
59  const std::string resource = _tag->findCData( "/iq/bind[@xmlns='" + gloox::XMLNS_STREAM_BIND + "']/resource" );
60 
61  gloox::JID jid( m_parent->jid().bare() );
62  jid.setResource( resource );
63  if( m_sm.createSession( jid, m_parent->id() ) )
64  {
65  m_parent->setResource( jid.resource() );
66  m_state = BindDone;
67  m_parent->finishStreamFeature( StateDone );
68 
69  gloox::Tag* re = new gloox::Tag( "iq" );
70  re->addAttribute( gloox::TYPE, "result" );
71  re->addAttribute( "id", _tag->findAttribute( "id" ) );
72  gloox::Tag* b = new gloox::Tag( re, "bind" );
73  b->setXmlns( gloox::XMLNS_STREAM_BIND );
74  new gloox::Tag( b, "jid", jid.full() );
75  m_parent->send( re );
76  return;
77  }
78  else
79  printf( "creating a session failed\n" );
80  }
81  else
82  printf( "state doesn't match\n" );
83 
84  gloox::IQ re( gloox::IQ::Error, _tag->findAttribute( "from" ),
85  _tag->findAttribute( "id" ));
86  // FIXME add gloox::Error
87  m_parent->send( re );
88  }
89 
90 }