Exemplo n.º 1
0
 void FileTransfer::parseFileList( const TagList& files )
 {
   TagList::const_iterator it = files.begin();
   for( ; it != files.end(); ++it )
   {
     File f;
     Tag *t = (*it)->findChild( "name" );
     f.name = t ? t->cdata() : EmptyString;
     t = (*it)->findChild( "desc" );
     f.desc = t ? t->cdata() : EmptyString;
     t = (*it)->findChild( "date" );
     f.date = t ? t->cdata() : EmptyString;
     t = (*it)->findChild( "size" );
     f.size = t ? atoi( t->cdata().c_str() ) : -1;
     t = (*it)->findChild( "range" );
     if( t )
     {
       f.range = true;
       f.offset = t->hasAttribute( "offset" ) ? atoi( t->findAttribute( "offset" ).c_str() ) : -1;
     }
     t = (*it)->findChild( "hash", XMLNS, XMLNS_HASHES );
     if( t )
     {
       f.hash_algo = t->findAttribute( "algo" );
       f.hash = t->cdata();
     }
     m_files.push_back( f );
   }
 }
Exemplo n.º 2
0
 virtual void handleMessage( const Message& msg, MessageSession * /*session*/ )
 {
     Tag* m = msg.tag();
     Tag *x = m->findChild( "xtls", "xmlns", "test:xtls" );
     if( x )
     {
         printf( "decrypting: %d\n", x->cdata().length() );
         m_tls->decrypt( Base64::decode64( x->cdata() ) );
         xtlsSend();
     }
     delete m;
 }
Exemplo n.º 3
0
  bool UniqueMUCRoom::handleIqID( Stanza *stanza, int context )
  {
    switch( stanza->subtype() )
    {
      case StanzaIqResult:
        if( context == RequestUniqueName )
        {
          Tag *u = stanza->findChild( "unique", XMLNS_MUC_UNIQUE );
          if( u )
          {
            const std::string& name = u->cdata();
            if( !name.empty() )
              setName( name );
          }
        }
        break;
      case StanzaIqError:
        if( context == RequestUniqueName )
        {
          SHA s;
          s.feed( m_parent->jid().full() );
          s.feed( m_parent->getID() );
          setName( s.hex() );
        }
        break;
      default:
        break;
    }

    MUCRoom::join();

    return false;
  }
Exemplo n.º 4
0
  Presence::Presence( Tag* tag )
    : Stanza( tag ), m_subtype( Invalid ), m_stati( 0 ), m_priority( 0 )
  {
    if( !tag || tag->name() != "presence" )
      return;

    const std::string& type = tag->findAttribute( TYPE );
    if( type.empty() )
      m_subtype = Available;
    else
      m_subtype = (PresenceType)util::lookup( type, msgTypeStringValues );

    if( m_subtype == Available )
    {
      Tag* t = tag->findChild( "show" );
      if( t )
        m_subtype = (PresenceType)util::lookup( t->cdata(), msgShowStringValues );
    }

    const TagList& c = tag->children();
    TagList::const_iterator it = c.begin();
    for( ; it != c.end(); ++it )
    {
      if( (*it)->name() == "status" )
        setLang( &m_stati, m_status, (*it) );
      else if( (*it)->name() == "priority" )
        m_priority = atoi( (*it)->cdata().c_str() );
    }
  }
Exemplo n.º 5
0
  void InBandBytestream::filter( Stanza *stanza )
  {
    if( !m_inbandBytestreamDataHandler || !m_open )
      return;

    if( stanza->subtype() == StanzaMessageError )
    {
      m_inbandBytestreamDataHandler->handleInBandError( m_sid, stanza->from(), stanza->error() );
      m_open = false;
    }

    Tag *data = 0;
    if( ( data = stanza->findChild( "data", "xmlns", XMLNS_IBB ) ) == 0 )
      return;

    const std::string& sid = data->findAttribute( "sid" );
    if( sid.empty() || sid != m_sid )
      return;

    const std::string& seq = data->findAttribute( "seq" );
    if( seq.empty() )
    {
      m_open = false;
      return;
    }

    std::stringstream str;
    int sequence = 0;
    str << seq;
    str >> sequence;

    if( m_lastChunkReceived + 1 != sequence )
    {
      m_open = false;
      return;
    }
    m_lastChunkReceived = sequence;

    if( !data->cdata().length() )
    {
      m_open = false;
      return;
    }

    m_inbandBytestreamDataHandler->handleInBandData( Base64::decode64( data->cdata() ), sid );
  }
Exemplo n.º 6
0
  DataFormField::DataFormField( Tag *tag )
    : m_type( FIELD_TYPE_INVALID ), m_required( false )
  {
    if( !tag )
      return;

    if( tag->hasAttribute( "type", "boolean" ) )
      m_type = FIELD_TYPE_BOOLEAN;
    else if( tag->hasAttribute( "type", "fixed" ) )
      m_type = FIELD_TYPE_FIXED;
    else if( tag->hasAttribute( "type", "hidden" ) )
      m_type = FIELD_TYPE_HIDDEN;
    else if( tag->hasAttribute( "type", "jid-multi" ) )
      m_type = FIELD_TYPE_JID_MULTI;
    else if( tag->hasAttribute( "type", "jid-single" ) )
      m_type = FIELD_TYPE_JID_SINGLE;
    else if( tag->hasAttribute( "type", "list-multi" ) )
      m_type = FIELD_TYPE_LIST_MULTI;
    else if( tag->hasAttribute( "type", "list-single" ) )
      m_type = FIELD_TYPE_LIST_SINGLE;
    else if( tag->hasAttribute( "type", "text-multi" ) )
      m_type = FIELD_TYPE_TEXT_MULTI;
    else if( tag->hasAttribute( "type", "text-private" ) )
      m_type = FIELD_TYPE_TEXT_PRIVATE;
    else if( tag->hasAttribute( "type", "text-single" ) )
      m_type = FIELD_TYPE_TEXT_SINGLE;

    if( tag->hasAttribute( "var" ) )
      m_name = tag->findAttribute( "var" );

    if( tag->hasAttribute( "label" ) )
      m_label = tag->findAttribute( "label" );

    Tag::TagList l = tag->children();
    Tag::TagList::const_iterator it = l.begin();
    for( ; it != l.end(); ++it )
    {
      if( (*it)->name() == "desc" )
        m_desc = (*it)->cdata();
      else if( (*it)->name() == "required" )
        m_required = true;
      else if( (*it)->name() == "value" )
      {
        if( m_type == FIELD_TYPE_TEXT_MULTI || m_type == FIELD_TYPE_LIST_MULTI )
          m_values.push_back( (*it)->cdata() );
        else
          m_value = (*it)->cdata();
      }
      else if( (*it)->name() == "option" )
      {
        Tag *v = (*it)->findChild( "value" );
        if( v )
          m_options[(*it)->findAttribute( "label" )] = v->cdata();
      }
    }

  }
Exemplo n.º 7
0
  SoftwareVersion::SoftwareVersion( const Tag* tag )
    : StanzaExtension( ExtVersion )
  {
    if( !tag )
      return;

    Tag* t = tag->findChild( "name" );
    if( t )
      m_name = t->cdata();

    t = tag->findChild( "version" );
    if( t )
      m_version = t->cdata();

    t = tag->findChild( "os" );
    if( t )
      m_os = t->cdata();
  }
Exemplo n.º 8
0
const std::string AdhocTag::getValue(const std::string &var) {
    if (xdata == NULL)
        return "";
    Tag *v = xdata->findChildWithAttrib("var", var);
    if (!v)
        return "";
    Tag *value =v->findChild("value");
    if (!value)
        return "";
    return value->cdata();
}
Exemplo n.º 9
0
  DataFormField::DataFormField( const Tag* tag )
    : m_type( TypeInvalid ), m_required( false )
  {
    if( !tag )
      return;

    const std::string& type = tag->findAttribute( TYPE );
    if( type.empty() )
    {
      if( !tag->name().empty() )
        m_type = TypeNone;
    }
    else
      m_type = (FieldType)util::lookup( type, fieldTypeValues );

    if( tag->hasAttribute( "var" ) )
      m_name = tag->findAttribute( "var" );

    if( tag->hasAttribute( "label" ) )
      m_label = tag->findAttribute( "label" );

    const TagList& l = tag->children();
    TagList::const_iterator it = l.begin();
    for( ; it != l.end(); ++it )
    {
      if( (*it)->name() == "desc" )
        m_desc = (*it)->cdata();
      else if( (*it)->name() == "required" )
        m_required = true;
      else if( (*it)->name() == "value" )
      {
        if( m_type == TypeTextMulti || m_type == TypeListMulti || m_type == TypeJidMulti )
          addValue( (*it)->cdata() );
        else
          setValue( (*it)->cdata() );
      }
      else if( (*it)->name() == "option" )
      {
        Tag* v = (*it)->findChild( "value" );
        if( v )
          m_options.insert( std::make_pair( (*it)->findAttribute( "label" ), v->cdata() ) );
      }
    }

  }
Exemplo n.º 10
0
MoodsExtenstion::MoodsExtenstion(const Tag* tag)
: StanzaExtension( ExtUser+107 )
{
	if(!tag)
		return;
	TagList tag_list = tag->children();
	if ( !tag_list.empty() )
		m_mood_name = jProtocol::fromStd(tag_list.front()->name());
	if(m_mood_name=="text")
		m_mood_name="";
	if(!m_mood_name.isEmpty())
	{
		if(!jPluginSystem::instance().getMoodTr().contains(m_mood_name))
			m_mood_name="undefined";
		Tag* t = tag->findChild( "text" );
		if( t )
			m_mood_text = jProtocol::fromStd(t->cdata());
	}
}
Exemplo n.º 11
0
  void Client::processResourceBind( Stanza *stanza )
  {
    switch( stanza->subtype() )
    {
      case StanzaIqResult:
      {
        Tag *bind = stanza->findChild( "bind" );
        Tag *jid = bind->findChild( "jid" );
        m_jid.setJID( jid->cdata() );
        m_resourceBound = true;

        if( m_streamFeatures & StreamFeatureSession )
          createSession();
        else
          connected();
        break;
      }
      case StanzaIqError:
      {
        Tag *error = stanza->findChild( "error" );
        if( stanza->hasChild( "error", "type", "modify" )
            && error->hasChild( "bad-request", "xmlns", XMLNS_XMPP_STANZAS ) )
        {
          notifyOnResourceBindError( RbErrorBadRequest );
        }
        else if( stanza->hasChild( "error", "type", "cancel" ) )
        {
          if( error->hasChild( "not-allowed", "xmlns", XMLNS_XMPP_STANZAS ) )
            notifyOnResourceBindError( RbErrorNotAllowed );
          else if( error->hasChild( "conflict", "xmlns", XMLNS_XMPP_STANZAS ) )
            notifyOnResourceBindError( RbErrorConflict );
          else
            notifyOnResourceBindError( RbErrorUnknownError );
        }
        else
          notifyOnResourceBindError( RbErrorUnknownError );
        break;
      }
      default:
        break;
    }
  }
Exemplo n.º 12
0
XmppGSEvent::XmppGSEvent( const Tag* tag )
	:StanzaExtension(ExtIotEvent), m_method(defCfgOprt_Unknown), m_pDevice(NULL), m_runstate(1)
{
	if( !tag || tag->name() != "gsiot" || tag->xmlns() != XMLNS_GSIOT_EVENT )
		return;

	//GSIOTClient::XmppPrint( tag, "recv", stanza, false ); //jyc20170227 debug
	//XmppGSEvent::PrintTag( tag , ExtIotEvent);

	Tag *tmgr = tag->findChild("event");

	if( tmgr )
	{
		Tag *tDevice = tmgr->findChild("device");
		if( tDevice )
		{
			m_pDevice = new GSIOTDevice(tDevice);
		}
		else
		{
			LOGMSG( "not found device tag" );
			return;
		}

		this->UntagEditAttr( tmgr );

		if(tmgr->hasAttribute("method"))
		{
			m_srcmethod = tmgr->findAttribute("method");
			std::string method = m_srcmethod;
			g_toLowerCase( method );
			if( method == "add" )
			{
				m_method = defCfgOprt_Add;
			}
			else if( method == "edit" )
			{
				m_method = defCfgOprt_Modify;
			}
			else if( method == "delete" )
			{
				m_method = defCfgOprt_Delete;
			}
		}

		Tag *tdo = tmgr->findChild("do");  //jyc20170223 trans  "edit trigger"
		if( tdo )
		{
			const TagList& l = tdo->children();
			TagList::const_iterator it = l.begin();
			for( ; it != l.end(); ++it )
			{
				ControlEvent *pNew = NULL;

				if((*it)->name() == "smsthing")
				{
					pNew = new AutoSendSMSEvent(*it);
				}
				else if((*it)->name() == "mailthing")
				{
					//...
				}
				else if((*it)->name() == "messagething")
				{
					pNew = new AutoNoticeEvent(*it);
				}
				else if((*it)->name() == "devicething")
				{
					pNew = new AutoControlEvent(*it);
				}
				else if((*it)->name() == "callthing")
				{
					//...
				}
				else if((*it)->name() == "eventthing")
				{
					pNew = new AutoEventthing(*it);
				}

				if( pNew )
				{
					if( m_pDevice )
					{
						pNew->SetDeviceType( m_pDevice->getType() );
						pNew->SetDeviceID( m_pDevice->getId() );
					}

					m_Events.push_back( pNew );
				}
			}
		}

		Tag *tState = tmgr->findChild("state");
		if( tState )
		{
			m_runstate = atoi( tState->cdata().c_str() );
		} 
	}
}
Exemplo n.º 13
0
XmppGSPlayback::XmppGSPlayback( const Tag* tag )
	:StanzaExtension(ExtIotPlayback), m_camera_id(0), m_startdt(0), m_enddt(0), m_state(defPBState_Unknown), m_sound(-1)
{
	if( !tag || tag->name() != "gsiot" || tag->xmlns() != XMLNS_GSIOT_PLAYBACK )
		return;

	Tag *tmgr = tag->findChild("playback");

	if( tmgr )
	{
		Tag *tcamera = tmgr->findChild("device");
		if( tcamera )
		{
			if( tcamera->hasAttribute("id") )
			{
				m_camera_id = atoi( tcamera->findAttribute("id").c_str() );
			}
		}

		if( tmgr->findChild("startdate") )
		{
			m_startdt = atoi( tmgr->findChild("startdate")->cdata().c_str() );
		}

		if( tmgr->findChild("enddate") )
		{
			m_enddt = atoi( tmgr->findChild("enddate")->cdata().c_str() );
		}

		if( tmgr->findChild("url") )
		{
			m_url = tmgr->findChild("url")->cdata();
		}

		//#ifdef _DEBUG
#if 0
		std::string useUrl_lastback;
		if( IsRUNCODEEnable( defCodeIndex_TEST_Debug_TempCode ) )
		{
			if( !g_IsRTMFP_url( m_url ) ) useUrl_lastback = m_url;

			//m_url = "rtmfp://192.168.0.76:1985/p2p";
			m_url = "rtmfp://p2p.gsss.cn:1985/p2p";
			//m_url = "rtmfp://unknownp2p.gsss.cn:1985/p2p";

			GUID guid;
			::CoCreateGuid( &guid );
			m_url += "/";
			m_url += g_BufferToString( (unsigned char*)&guid, sizeof( guid ), false, false );
		}
#endif

		if( IsRUNCODEEnable( defCodeIndex_RTMFP_DelFromUrlStreamID ) && g_IsRTMFP_url( m_url ) )
		{
			std::vector<std::string> useUrl_unit_vec;
			split( m_url, useUrl_unit_vec, "/" );

			const int unitsize = useUrl_unit_vec.size();
			// 偶数个表示最后一个为streamid,去掉
			if( 0 == (unitsize%2) )
			{
				g_replace_all_distinct( m_url, std::string( "/" )+useUrl_unit_vec[unitsize-1], "" );
			}
		}

		Tag *tchildlist_url_backup = tmgr->findChild("url_backup");
		if( tchildlist_url_backup )
		{
			const TagList& l = tchildlist_url_backup->children();
			TagList::const_iterator it = l.begin();
			for( ; it != l.end(); ++it )
			{
				Tag *tChild = (*it);
				if( tChild->name() == "url" )
				{
					m_url_backup.push_back( tChild->cdata() );
				}
			}
		}
		//if( !useUrl_lastback.empty() ) m_url_backup.push_back( useUrl_lastback );

		if( tmgr->findChild("key") )
		{
			m_key = tmgr->findChild("key")->cdata();
		}

		if( tmgr->findChild("state") )
		{
			std::string strState = tmgr->findChild("state")->cdata();
			g_toLowerCase( strState );
			if( strState == "start" )
			{
				m_state = defPBState_Start;
			}
			else if( strState == "stop" )
			{
				m_state = defPBState_Stop;
			}
			else if( strState == "set" )
			{
				m_state = defPBState_Set;
			}
			else if( strState == "get" )
			{
				m_state = defPBState_Get;
			}
			else if( strState == "pause" )
			{
				m_state = defPBState_Pause;
			}
			else if( strState == "resume" )
			{
				m_state = defPBState_Resume;
			}
			else if( strState == "normalplay" )
			{
				m_state = defPBState_NormalPlay;
			}
			else if( strState == "fastplay" )
			{
				m_state = defPBState_FastPlay;
			}
			else if( strState == "fastplaythrow" )
			{
				m_state = defPBState_FastPlayThrow;
			}
			else if( strState == "fastplay1" )
			{
				m_state = defPBState_FastPlay1;
			}
			else if( strState == "fastplay2" )
			{
				m_state = defPBState_FastPlay2;
			}
			else if( strState == "slowplay" )
			{
				m_state = defPBState_SlowPlay;
			}
			else if( strState == "slowplay1" )
			{
				m_state = defPBState_SlowPlay1;
			}
			else if( strState == "slowplay2" )
			{
				m_state = defPBState_SlowPlay2;
			}
			else if( strState == "stopall" )
			{
				m_state = defPBState_StopAll;
			}
		}

		if( tmgr->findChild("sound") )
		{
			m_sound = atoi( tmgr->findChild("sound")->cdata().c_str() );
		}
		else
		{
			m_sound = -1; // 无效值
		}
	}
}
Exemplo n.º 14
0
int main( int /*argc*/, char** /*argv*/ )
{
  int fail = 0;
  std::string name;
  Tag *t = new Tag( "toe" ); t->addAttribute( "foo", "bar" );
  Tag *u = new Tag( t, "uni" ); u->addAttribute( "u3", "3u" );
  Tag *v = new Tag( t, "vie" ); v->addAttribute( "v3", "3v" );
  Tag *v2 = new Tag( t, "vie" ); v->addAttribute( "v32", "3v2" );
  Tag *w = new Tag( u, "who" ); w->addAttribute( "w3", "3w" );
  Tag *x = new Tag( v, "xep" ); x->addAttribute( "x3", "3x" );
  Tag *y = new Tag( u, "yps" ); y->addAttribute( "y3", "3y" );
  Tag *z = new Tag( w, "zoo" ); z->addAttribute( "z3", "3z" );
  Tag *c = 0;
  Tag *d = 0;

  // -------
  name = "simple ctor";
  if( t->name() != "toe" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }

  // -------
  name = "cdata ctor";
  c = new Tag( "cod", "foobar" );
  if( c->name() != "cod" || c->cdata() != "foobar" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  delete c;
  c = 0;

  //-------
  name = "clone test 1";
  c = z->clone();
  if( *z != *c )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  delete c;
  c = 0;

  //-------
  name = "clone test 2";
  c = t->clone();
  if( *t != *c )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  delete c;
  c = 0;

  //-------
  name = "operator== test 1";
  c = new Tag( "name" );
  if( *t == *c )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  delete c;
  c = 0;

  //-------
  name = "operator== test 2";
  c = new Tag( "test" );
  c->addAttribute( "me", "help" );
  c->addChild( new Tag( "yes" ) );
  if( *t == *c )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  delete c;
  c = 0;

  //-------
  name = "operator== test 3";
  c = new Tag( "hello" );
  c->addAttribute( "test", "bacd" );
  c->addChild( new Tag( "hello" ) );
  d = new Tag( "hello" );
  d->addAttribute( "test", "bacd" );
  d->addChild( new Tag( "helloo" ) );
  if( *d == *c )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  delete c;
  delete d;
  c = 0;
  d = 0;

  //-------
  name = "operator!= test 1";
  c = new Tag( "hello" );
  c->addAttribute( "test", "bacd" );
  c->addChild( new Tag( "hello" ) );
  d = new Tag( "hello" );
  d->addAttribute( "test", "bacd" );
  d->addChild( new Tag( "hello" ) );
  if( *d != *c )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  delete c;
  delete d;
  c = 0;
  d = 0;

  //-------
  name = "findChildren test";
  TagList l = t->findChildren( "vie" );
  TagList::const_iterator it = l.begin();
  if( l.size() != 2 || (*it) != v || *(++it) != v2 )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  delete c;
  c = 0;

  //-------
  name = "util::escape";
  if ( util::escape( "&<>'\"" ) != "&amp;&lt;&gt;&apos;&quot;" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }

  //-------
  name = "xml() 1";
  if( t->xml() != "<toe foo='bar'><uni u3='3u'><who w3='3w'><zoo z3='3z'/></who><yps y3='3y'/>"
                    "</uni><vie v3='3v' v32='3v2'><xep x3='3x'/></vie><vie/></toe>" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t->xml().c_str() );
  }

  //-------
  name = "xml() 2";
  t->addAttribute( "test", "bacd" );
  if( t->xml() != "<toe foo='bar' test='bacd'><uni u3='3u'><who w3='3w'><zoo z3='3z'/></who><yps y3='3y'/>"
                    "</uni><vie v3='3v' v32='3v2'><xep x3='3x'/></vie><vie/></toe>" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t->xml().c_str() );
  }

  //-------
  name = "hasChild 1";
  if( !t->hasChild( "uni" ) || !t->hasChild( "vie" ) || !u->hasChild( "who" ) || !w->hasChild( "zoo" )
      || !u->hasChild( "yps" ) )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t->xml().c_str() );
  }

  //-------
  name = "hasAttribute 1";
  if( !t->hasAttribute( "test" ) || !t->hasAttribute( "test", "bacd" )
      || !t->hasAttribute( "foo" ) || !t->hasAttribute( "foo", "bar" ) )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t->xml().c_str() );
  }

  //-------
  name = "findAttribute 1";
  if( t->findAttribute( "test" ) != "bacd" || t->findAttribute( "foo" ) != "bar" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t->xml().c_str() );
  }

  //-------
  name = "findChild 1";
  c = t->findChild( "uni" );
  if( c != u )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t->xml().c_str() );
  }

  //-------
  name = "findChild 2";
  c = t->findChild( "uni", "u3" );
  if( c != u )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t->xml().c_str() );
  }

  //-------
  name = "findChild 3";
  c = t->findChild( "uni", "u3", "3u" );
  if( c != u )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t->xml().c_str() );
  }

  //-------
  name = "findChildWithAttrib 1";
  c = t->findChildWithAttrib( "u3" );
  if( c != u )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t->xml().c_str() );
  }

  //-------
  name = "findChildWithAttrib 2";
  c = t->findChildWithAttrib( "u3", "3u" );
  if( c != u )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t->xml().c_str() );
  }

  //-------
  name = "attribute order";
  c = new Tag( "abc" );
  c->addAttribute( "abc", "def" );
  c->addAttribute( "xyz", "123" );
  d = c->clone();
  if( *c != *d )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), d->xml().c_str() );
  }
  delete c;
  c = 0;
  delete d;
  d = 0;

  //-------
  name = "mixed content 1";
  c = new Tag( "abc" );
  c->addCData( "cdata1" );
  new Tag( c, "fgh" );
  c->addCData( "cdata2" );
  new Tag( c, "xyz" );
  c->addCData( "cdata3" );
  if( c->xml() != "<abc>cdata1<fgh/>cdata2<xyz/>cdata3</abc>" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), c->xml().c_str() );
  }
  delete c;
  c = 0;

  //-------
  name = "operator bool()";
  Tag tag1( "" );
  if( tag1 )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), tag1.xml().c_str() );
  }

  //-------
  name = "bool operator!()";
  Tag tag2( "abc" );
  if( !tag2 )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), d->xml().c_str() );
  }

  //-------
  {
    name = "simple xmlns";
    Tag t( "abc" );
    t.setXmlns( "foo" );
    if( t.xml() != "<abc xmlns='foo'/>" )
    {
      ++fail;
      fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t.xml().c_str() );
    }
  }

  //-------
  {
    name = "deep xmlns";
    Tag t( "abc" );
    Tag* f = new Tag( &t, "def" );
    f = new Tag( f, "ghi" );
    t.setXmlns( "foo" );
    if( t.xml() != "<abc xmlns='foo'><def><ghi/></def></abc>" )
    {
      ++fail;
      fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t.xml().c_str() );
    }
  }

  //-------
  {
    name = "simple nested xmlns 2";
    Tag t( "abc" );
    t.setXmlns( "foo" );
    Tag* d = new Tag( &t, "def" );
    d->setXmlns( "foobar", "xyz" );
    d->setPrefix( "xyz" );
    if( t.xml() != "<abc xmlns='foo'><xyz:def xmlns:xyz='foobar'/></abc>" )
    {
      ++fail;
      fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t.xml().c_str() );
    }
  }

  //-------
  {
    name = "attribute with xmlns";
    Tag t( "abc" );
    t.setXmlns( "foo", "xyz" );
    Tag::Attribute* a = new Tag::Attribute( "foo", "bar", "foo" );
    a->setPrefix( "xyz" );
    t.addAttribute( a );
    if( t.xml() != "<abc xmlns:xyz='foo' xyz:foo='bar'/>" )
    {
      ++fail;
      fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t.xml().c_str() );
    }
  }

  //-------
  {
    name = "escape attribute value";
    Tag t( "foo", "abc", "&amp;" );
    if( t.xml() != "<foo abc='&amp;amp;'/>" )
    {
      ++fail;
      fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t.xml().c_str() );
    }
  }

  //-------
  {
    name = "remove child 1";
    Tag t( "foo" );
    t.addChild( new Tag( "test", "xmlns", "foo" ) );
    t.addChild( new Tag( "abc", "xmlns", "foobar" ) );
    t.addAttribute( "attr1", "value1" );
    t.addAttribute( "attr2", "value2" );
    t.removeChild( "test" );
    if( t.hasChild( "test" ) )
    {
      ++fail;
      fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t.xml().c_str() );
    }

    name = "remove child 2";
    t.removeChild( "abc", "foobar" );
    if( t.hasChild( "abc", "xmlns", "foobar" ) )
    {
      ++fail;
      fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t.xml().c_str() );
    }

    name = "remove attrib 1";
    t.removeAttribute( "attr1" );
    if( t.hasAttribute( "attr1", "value1") )
    {
      ++fail;
      fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t.xml().c_str() );
    }

    name = "remove attrib 2";
    t.removeAttribute( "attr2", "value2" );
    if( t.hasAttribute( "attr2", "value2") )
    {
      ++fail;
      fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t.xml().c_str() );
    }
  }

  //-------
  {
    name = "invalid chars 1";
    Tag t( "foo" );
    bool check = t.addAttribute( "nul", std::string( 1, 0x00 ) );
    if( check || t.hasAttribute( "nul" ) )
    {
      ++fail;
      fprintf( stderr, "test '%s' failed:%s\n", name.c_str(), t.xml().c_str() );
    }
  }

  //-------
  {
    name = "invalid chars 2";
    for( int i = 0; i <= 0xff; ++i )
    {
      Tag::Attribute a( "test", std::string( 1, i ) );

      if( ( i < 0x09 || i == 0x0b || i == 0x0c
          || ( i > 0x0d && i < 0x20 ) || i == 0xc0
          || i == 0xc1 || i >= 0xf5 ) && a )
      {
        ++fail;
        fprintf( stderr, "test '%s' (branch 1) failed (i == %02X)\n", name.c_str(), i );
      }
      else if( ( i == 0x09 || i == 0x0a || i == 0x0d
                 || ( i >= 0x20 && i < 0xc0 )
                 || ( i > 0xc1 && i < 0xf5 ) ) && !a )
      {
        ++fail;
        fprintf( stderr, "test '%s' (branch 2) failed (i == %02X)\n", name.c_str(), i );
      }
//       printf( "i: 0x%02X, a: %d, value: %s\n", i, (bool)a, std::string( 1, i ).c_str() );
    }
  }




  delete t;
  t = 0;




















  if( fail == 0 )
  {
    printf( "Tag: OK\n" );
    return 0;
  }
  else
  {
    fprintf( stderr, "Tag: %d test(s) failed\n", fail );
    return 1;
  }

}
Exemplo n.º 15
0
  bool Adhoc::handleIqID( Stanza * stanza, int context )
  {
    if( context != ExecuteAdhocCommand || stanza->subtype() != StanzaIqResult )
      return false;

    AdhocTrackMap::iterator it = m_adhocTrackMap.begin();
    for( ; it != m_adhocTrackMap.end(); ++it )
    {
      if( (*it).second.context == context && (*it).second.remote == stanza->from() )
      {
        Tag *c = stanza->findChild( "command", "xmlns", XMLNS_ADHOC_COMMANDS );
        if( c )
        {
          const std::string& command = c->findAttribute( "node" );
          const std::string& id = c->findAttribute( "sessionid" );
          Tag *a = c->findChild( "actions" );
          int actions = ActionCancel;
          Adhoc::AdhocExecuteActions def = ActionCancel;
          if( a )
          {
            if( a->hasChild( "prev" ) )
              actions |= ActionPrevious;
            if( a->hasChild( "next" ) )
              actions |= ActionNext;
            if( a->hasChild( "complete" ) )
              actions |= ActionComplete;
            const std::string& d = a->findAttribute( "execute" );
            if( d == "next" )
              def = ActionNext;
            else if( d == "prev" )
              def = ActionPrevious;
            else if( d == "complete" )
              def = ActionComplete;
          }
          Tag *n = c->findChild( "note" );
          std::string note;
          AdhocNoteType type = AdhocNoteInfo;
          if( n )
          {
            note = n->cdata();
            if( n->hasAttribute( "type", "warn" ) )
              type = AdhocNoteWarn;
            else if( n->hasAttribute( "type", "error" ) )
              type = AdhocNoteError;
          }
          const std::string& s = c->findAttribute( "status" );
          AdhocCommandStatus status = AdhocCommandStatusUnknown;
          if( s == "executing" )
            status = AdhocCommandExecuting;
          else if( s == "completed" )
            status = AdhocCommandCompleted;
          else if( s == "canceled" )
            status = AdhocCommandCanceled;
          DataForm form;
          Tag *x = c->findChild( "x", "xmlns", XMLNS_X_DATA );
          if( x )
            form.parse( x );

          (*it).second.ah->handleAdhocExecutionResult( stanza->from(), command, status, id, form,
                                                       actions, def, note, type );
        }

        m_adhocTrackMap.erase( it );
        return true;
      }
    }

    return false;
  }
Exemplo n.º 16
0
	void whistleVcardHandler::handleIqID( const IQ& iq, int context )
	{
		switch( iq.subtype() )
		{
		case IQ::Result:
			{
				switch( context )
				{
				case VCardHandler::FetchVCard:
					{
						json::jobject jobj;
						json::jobject status = json::jobject();
						if (iq.findExtension( ExtVCard ))
						{
							boost::shared_ptr<gloox::Tag> ptag(iq.findExtension( ExtVCard )->tag());
							for (TagList::const_iterator cit = ptag->children().begin(); cit != ptag->children().end(); ++cit) {
								Tag* ptag = *cit;
								if (ptag->name() == s_VcardPrivacy)
								{
									jobj[ptag->name()] = json::jobject(ptag->cdata());
								}
								else if (ptag->name() == s_VcardStatus)
								{
									//获取vcard协议改变 新的解析iq
									Tag* presource = ptag->findChild("resource");
									Tag* pshow = ptag->findChild("show");
									if ( presource && pshow)
									{
										json::jobject itemdata;
										itemdata["resource"] = presource->cdata();
										itemdata["show"] = pshow->cdata();
										status.arr_push(itemdata);
									}
								}
								else
								{
									jobj[ptag->name()] = ptag->cdata();
								}
							}
							jobj[s_VcardJid] = iq.from().bare();

							gwhistleVcard::instance().handleVCard(iq.from(), jobj, status, waitCallback_);
						}
						break;
					}
				case VCardHandler::StoreVCard:
					gwhistleVcard::instance().handleVCardResult(VCardHandler::StoreVCard, iq.from());
					break;
				}
			}
			break;
		case IQ::Error:
			{
				switch( context )
				{
				case VCardHandler::FetchVCard:
					if (!waitCallback_.empty())
					{
						waitCallback_();
					}
					else
					{
						gwhistleVcard::instance().updatedVcard(iq.from());
					}
					break;
				case VCardHandler::StoreVCard:
					gwhistleVcard::instance().handleVCardResult( (VCardHandler::VCardContext)context,
						iq.from(),
						iq.error() ? iq.error()->error()
						: StanzaErrorUndefined );
					break;
				}
			}
		default:
			break;
		}
	}
Exemplo n.º 17
0
  IOData::IOData( const Tag* tag )
    : AdhocPlugin( ExtIOData ),
      m_in( 0 ), m_out( 0 ), m_error( 0 ),
      m_type( TypeInvalid )
  {
    if( !tag || !( tag->name() == "iodata" && tag->hasAttribute( XMLNS, XMLNS_IODATA ) ) )
      return;

    m_status.elapsed = -1;
    m_status.remaining = -1;
    m_status.percentage = -1;

    m_type = ioType( tag->findAttribute( "type" ) );
    Tag* m = 0;
    switch( m_type )
    {
      case TypeInput:
        m = tag->findChild( "in" );
        if( m )
          m_in = m->clone();
        break;
      case TypeIoSchemataResult:
        m = tag->findChild( "desc" );
        if( m )
          m_desc = m->cdata();

        m = tag->findChild( "out" );
        if( m )
          m_out = m->clone();

        m = tag->findChild( "in" );
        if( m )
          m_in = m->clone();
        break;
      case TypeOutput:
        m = tag->findChild( "out" );
        if( m )
          m_out = m->clone();
        break;
      case TypeError:
        m = tag->findChild( "error" );
        if( m )
          m_error = m->clone();
        break;
      case TypeStatus:
        m = tag->findChild( "status" );
        if( m )
        {
          Tag* t = m->findChild( "elapsed" );
          if( t )
            m_status.elapsed = atoi( t->cdata().c_str() );

          t = m->findChild( "remaining" );
          if( t )
            m_status.remaining = atoi( t->cdata().c_str() );

          t = m->findChild( "percentage" );
          if( t )
            m_status.percentage = atoi( t->cdata().c_str() );

          t = m->findChild( "information" );
          if( t )
            m_status.info = t->cdata();
        }
        break;
      case TypeIoSchemataGet:
      case TypeGetStatus:
      case TypeGetOutput:
      default:
        break;
    }

  }
Exemplo n.º 18
0
int main( int /*argc*/, char** /*argv*/ )
{
    int fail = 0;
    std::string name;
    Tag *t;
    DelayedDelivery *d;
    JID j( "abc@def/ghi" );
    Tag *x = new Tag( "delay", "reason" );
    x->addAttribute( "stamp", "invalidstamp" );
    x->addAttribute( "from", j.full() );
    x->addAttribute( "xmlns", XMLNS_DELAY );

    // -------
    name = "parsing 0 tag";
    d = new DelayedDelivery( 0 );
    if( d->tag() != 0 )
    {
        ++fail;
        printf( "test '%s' failed\n", name.c_str() );
    }
    delete d;
    d = 0;

    // -------
    name = "filled object/getters";
    d = new DelayedDelivery( j, "invalidstamp", "reason" );
    if( d->reason() != "reason" || d->stamp() != "invalidstamp" || d->from() != j )
    {
        ++fail;
        printf( "test '%s' failed\n", name.c_str() );
    }
    delete d;
    d = 0;
    t = 0;

    // -------
    name = "filled object/tag()";
    d = new DelayedDelivery( j, "invalidstamp", "reason" );
    t = d->tag();
    if( !t || t->name() != "delay" || !t->hasAttribute( "xmlns", XMLNS_DELAY )
            || !t->hasAttribute( "from", j.full() ) || !t->hasAttribute( "stamp", "invalidstamp" )
            || t->cdata() != "reason" )
    {
        ++fail;
        printf( "test '%s' failed\n", name.c_str() );
    }
    delete d;
    delete t;
    d = 0;
    t = 0;

    // -------
    name = "from Tag/getters";
    d = new DelayedDelivery( x );
    if( d->reason() != "reason" || d->stamp() != "invalidstamp" || d->from() != j )
    {
        ++fail;
        printf( "test '%s' failed\n", name.c_str() );
    }
    delete d;
    delete t;
    d = 0;
    t = 0;

    // -------
    name = "from Tag/tag()";
    d = new DelayedDelivery( x );
    t = d->tag();
    if( !t || t->name() != "delay" || !t->hasAttribute( "xmlns", XMLNS_DELAY )
            || !t->hasAttribute( "from", j.full() ) || !t->hasAttribute( "stamp", "invalidstamp" )
            || t->cdata() != "reason" )
    {
        ++fail;
        printf( "test '%s' failed\n", name.c_str() );
    }
    delete d;
    delete t;
    d = 0;
    t = 0;



    delete x;
    x = 0;



    if( fail == 0 )
    {
        printf( "DelayedDelivery: all tests passed\n" );
        return 0;
    }
    else
    {
        printf( "DelayedDelivery: %d test(s) failed\n", fail );
        return 1;
    }

}
Exemplo n.º 19
0
  bool Search::handleIqID( Stanza *stanza, int context )
  {
    TrackMap::iterator it = m_track.find( stanza->id() );
    if( it != m_track.end() )
    {
      switch( stanza->subtype() )
      {
        case StanzaIqResult:
          switch( context )
          {
            case FetchSearchFields:
            {
              Tag *q = stanza->findChild( "query" );
              if( q && q->hasAttribute( "xmlns", XMLNS_SEARCH ) )
              {
                Tag *x = q->findChild( "x", "xmlns", XMLNS_X_DATA );
                if( x )
                {
                  DataForm *df = new DataForm( x );
                  (*it).second->handleSearchFields( stanza->from(), df );
                }
                else
                {
                  int fields = 0;
                  std::string instructions;

                  if( q->hasChild( "first" ) )
                    fields |= SearchFieldFirst;
                  if( q->hasChild( "last" ) )
                    fields |= SearchFieldLast;
                  if( q->hasChild( "nick" ) )
                    fields |= SearchFieldNick;
                  if( q->hasChild( "email" ) )
                    fields |= SearchFieldEmail;
                  if( q->hasChild( "instructions" ) )
                    instructions = q->findChild( "instructions" )->cdata();

                  (*it).second->handleSearchFields( stanza->from(), fields, instructions );
                }
              }
              break;
            }
            case DoSearch:
            {
              Tag *q = stanza->findChild( "query" );
              if( q && q->hasAttribute( "xmlns", XMLNS_SEARCH ) )
              {
                Tag *x = q->findChild( "x", "xmlns", XMLNS_X_DATA );
                if( x )
                {
                  DataForm *df = new DataForm( x );
                  (*it).second->handleSearchResult( stanza->from(), df );
                }
                else
                {
                  SearchResultList e;
                  SearchFieldStruct s;
                  const Tag::TagList &l = q->children();
                  Tag::TagList::const_iterator itl = l.begin();
                  for( ; itl != l.end(); ++itl )
                  {
                    if( (*itl)->name() == "item" )
                    {
                      s.jid.setJID( (*itl)->findAttribute( "jid" ) );
                      Tag *t = 0;
                      if( ( t = (*itl)->findChild( "first" ) ) != 0 )
                        s.first = t->cdata();
                      if( ( t = (*itl)->findChild( "last" ) ) != 0 )
                        s.last = t->cdata();
                      if( ( t = (*itl)->findChild( "nick" ) ) != 0 )
                        s.nick = t->cdata();
                      if( ( t = (*itl)->findChild( "email" ) ) != 0 )
                        s.email = t->cdata();
                      e.push_back( s );
                    }
                  }

                  (*it).second->handleSearchResult( stanza->from(), e );
                }
              }
              break;
            }
          }
          break;
        case StanzaIqError:
          (*it).second->handleSearchError( stanza->from(), stanza );
          break;

        default:
          break;
      }

      m_track.erase( it );
    }

    return false;
  }
Exemplo n.º 20
0
bool GlooxRegisterHandler::handleIq(const Tag *iqTag) {
	Log("GlooxRegisterHandler", iqTag->findAttribute("from") << ": iq:register received (" << iqTag->findAttribute("type") << ")");

	JID from(iqTag->findAttribute("from"));

	if (CONFIG().protocol == "irc") {
		sendError(400, "bad-request", iqTag);
		return false;
	}
	
	AbstractUser *user = Transport::instance()->userManager()->getUserByJID(from.bare());
	if (!Transport::instance()->getConfiguration().enable_public_registration) {
		std::list<std::string> const &x = Transport::instance()->getConfiguration().allowedServers;
		if (std::find(x.begin(), x.end(), from.server()) == x.end()) {
			Log("GlooxRegisterHandler", "This user has no permissions to register an account");
			sendError(400, "bad-request", iqTag);
			return false;
		}
	}

	const char *_language = user ? user->getLang() : Transport::instance()->getConfiguration().language.c_str();

	// send registration form
	if (iqTag->findAttribute("type") == "get") {
		Tag *reply = new Tag( "iq" );
		reply->addAttribute( "id", iqTag->findAttribute("id") );
		reply->addAttribute( "type", "result" );
		reply->addAttribute( "to", iqTag->findAttribute("from") );
		reply->addAttribute( "from", Transport::instance()->jid() );
		Tag *query = new Tag( "query" );
		query->addAttribute( "xmlns", "jabber:iq:register" );
		UserRow res = Transport::instance()->sql()->getUserByJid(from.bare());

		std::string instructions = CONFIG().reg_instructions.empty() ? PROTOCOL()->text("instructions") : CONFIG().reg_instructions;
		std::string usernameField = CONFIG().reg_username_field.empty() ? PROTOCOL()->text("username") : CONFIG().reg_username_field;

		if (res.id == -1) {
			Log("GlooxRegisterHandler", "* sending registration form; user is not registered");
			query->addChild( new Tag("instructions", tr(_language, instructions)) );
			query->addChild( new Tag("username") );
			if (CONFIG().protocol != "twitter" && CONFIG().protocol != "bonjour")
				query->addChild( new Tag("password") );
		}
		else {
			Log("GlooxRegisterHandler", "* sending registration form; user is registered");
			query->addChild( new Tag("instructions", tr(_language, instructions)) );
			query->addChild( new Tag("registered") );
			query->addChild( new Tag("username", res.uin));
			if (CONFIG().protocol != "twitter" && CONFIG().protocol != "bonjour")
				query->addChild( new Tag("password"));
		}

		Tag *x = new Tag("x");
		x->addAttribute("xmlns", "jabber:x:data");
		x->addAttribute("type", "form");
		x->addChild( new Tag("title", tr(_language, _("Registration"))));
		x->addChild( new Tag("instructions", tr(_language, instructions)) );

		Tag *field = new Tag("field");
		field->addAttribute("type", "hidden");
		field->addAttribute("var", "FORM_TYPE");
		field->addChild( new Tag("value", "jabber:iq:register") );
		x->addChild(field);

		field = new Tag("field");
		field->addAttribute("type", "text-single");
		field->addAttribute("var", "username");
		field->addAttribute("label", tr(_language, usernameField));
		field->addChild( new Tag("required") );
		if (res.id!=-1)
			field->addChild( new Tag("value", res.uin) );
		x->addChild(field);

		if (CONFIG().protocol != "twitter" && CONFIG().protocol != "bonjour") {
			field = new Tag("field");
			field->addAttribute("type", "text-private");
			field->addAttribute("var", "password");
			field->addAttribute("label", tr(_language, _("Password")));
			x->addChild(field);
		}

		field = new Tag("field");
		field->addAttribute("type", "list-single");
		field->addAttribute("var", "language");
		field->addAttribute("label", tr(_language, _("Language")));
		if (res.id!=-1)
			field->addChild( new Tag("value", res.language) );
		else
			field->addChild( new Tag("value", Transport::instance()->getConfiguration().language) );
		x->addChild(field);

		std::map <std::string, std::string> languages = localization.getLanguages();
		for (std::map <std::string, std::string>::iterator it = languages.begin(); it != languages.end(); it++) {
			Tag *option = new Tag("option");
			option->addAttribute("label", (*it).second);
			option->addChild( new Tag("value", (*it).first) );
			field->addChild(option);
		}

		field = new Tag("field");
		field->addAttribute("type", "text-single");
		field->addAttribute("var", "encoding");
		field->addAttribute("label", tr(_language, _("Encoding")));
		if (res.id!=-1)
			field->addChild( new Tag("value", res.encoding) );
		else
			field->addChild( new Tag("value", Transport::instance()->getConfiguration().encoding) );
		x->addChild(field);

		if (res.id != -1) {
			field = new Tag("field");
			field->addAttribute("type", "boolean");
			field->addAttribute("var", "unregister");
			field->addAttribute("label", tr(_language, _("Remove your registration")));
			field->addChild( new Tag("value", "0") );
			x->addChild(field);
		}

		query->addChild(x);
		reply->addChild(query);
		Transport::instance()->send( reply );
		return true;
	}
	else if (iqTag->findAttribute("type") == "set") {
		bool remove = false;
		Tag *query;
		Tag *usernametag;
		Tag *passwordtag;
		Tag *languagetag;
		Tag *encodingtag;
		std::string username("");
		std::string password("");
		std::string language("");
		std::string encoding("");

		UserRow res = Transport::instance()->sql()->getUserByJid(from.bare());
		
		query = iqTag->findChild( "query" );
		if (!query) return true;

		Tag *xdata = query->findChild("x", "xmlns", "jabber:x:data");
		if (xdata) {
			if (query->hasChild( "remove" ))
				remove = true;
			for (std::list<Tag*>::const_iterator it = xdata->children().begin(); it != xdata->children().end(); ++it) {
				std::string key = (*it)->findAttribute("var");
				if (key.empty()) continue;

				Tag *v = (*it)->findChild("value");
				if (!v) continue;

				if (key == "username")
					username = v->cdata();
				else if (key == "password")
					password = v->cdata();
				else if (key == "language")
					language = v->cdata();
				else if (key == "encoding")
					encoding = v->cdata();
				else if (key == "unregister")
					remove = atoi(v->cdata().c_str());
			}
		}
		else {
			if (query->hasChild( "remove" ))
				remove = true;
			else {
				usernametag = query->findChild("username");
				passwordtag = query->findChild("password");
				languagetag = query->findChild("language");
				encodingtag = query->findChild("encoding");

				if (languagetag)
					language = languagetag->cdata();
				else
					language = Transport::instance()->getConfiguration().language;

				if (encodingtag)
					encoding = encodingtag->cdata();
				else
					encoding = Transport::instance()->getConfiguration().encoding;

				if (usernametag==NULL || (passwordtag==NULL && CONFIG().protocol != "twitter" && CONFIG().protocol != "bonjour")) {
					sendError(406, "not-acceptable", iqTag);
					return false;
				}
				else {
					username = usernametag->cdata();
					if (passwordtag)
						password = passwordtag->cdata();
					if (username.empty() || (password.empty() && CONFIG().protocol != "twitter" && CONFIG().protocol != "bonjour")) {
						sendError(406, "not-acceptable", iqTag);
						return false;
					}
				}
			}
		}

		if (Transport::instance()->getConfiguration().protocol == "xmpp") {
			// User tries to register himself.
			if ((JID(username).bare() == from.bare())) {
				sendError(406, "not-acceptable", iqTag);
				return false;
			}

			// User tries to register someone who's already registered.
			UserRow user_row = Transport::instance()->sql()->getUserByJid(JID(username).bare());
			if (user_row.id != -1) {
				sendError(406, "not-acceptable", iqTag);
				return false;
			}
		}

		if (remove) {
			unregisterUser(from.bare());

			Tag *reply = new Tag("iq");
			reply->addAttribute( "type", "result" );
			reply->addAttribute( "from", Transport::instance()->jid() );
			reply->addAttribute( "to", iqTag->findAttribute("from") );
			reply->addAttribute( "id", iqTag->findAttribute("id") );
			Transport::instance()->send( reply );

			return true;
		}

		// Register or change password

		std::string jid = from.bare();

		if (username.empty() || (password.empty() && CONFIG().protocol != "twitter" && CONFIG().protocol != "bonjour") || localization.getLanguages().find(language) == localization.getLanguages().end()) {
			sendError(406, "not-acceptable", iqTag);
			return false;
		}

		Transport::instance()->protocol()->prepareUsername(username);

		std::string newUsername(username);
		if (!CONFIG().username_mask.empty()) {
			newUsername = CONFIG().username_mask;
			replace(newUsername, "$username", username.c_str());
		}

		if (!Transport::instance()->protocol()->isValidUsername(newUsername)) {
			Log("GlooxRegisterHandler", "This is not valid username: "******"bad-request", iqTag);
			return false;
		}

#if GLIB_CHECK_VERSION(2,14,0)
		if (!CONFIG().reg_allowed_usernames.empty() &&
			!g_regex_match_simple(CONFIG().reg_allowed_usernames.c_str(), newUsername.c_str(),(GRegexCompileFlags) (G_REGEX_CASELESS | G_REGEX_EXTENDED), (GRegexMatchFlags) 0)) {
			Log("GlooxRegisterHandler", "This is not valid username: "******"bad-request", iqTag);
			return false;
		}
#endif
		if (res.id == -1) {
			res.jid = from.bare();
			res.uin = username;
			res.password = password;
			res.language = language;
			res.encoding = encoding;
			res.vip = 0;

			registerUser(res);
		}
		else {
			// change passwordhttp://soumar.jabbim.cz/phpmyadmin/index.php
			Log("GlooxRegisterHandler", "changing user password: "******", " << username);
			res.jid = from.bare();
			res.password = password;
			res.language = language;
			res.encoding = encoding;
			Transport::instance()->sql()->updateUser(res);
		}

		Tag *reply = new Tag( "iq" );
		reply->addAttribute( "id", iqTag->findAttribute("id") );
		reply->addAttribute( "type", "result" );
		reply->addAttribute( "to", iqTag->findAttribute("from") );
		reply->addAttribute( "from", Transport::instance()->jid() );
		Transport::instance()->send( reply );

		return true;
	}
	return false;
}