Exemple #1
0
  void Adhoc::handleDiscoItemsResult( Stanza *stanza, int context )
  {
    if( context != FetchAdhocCommands )
      return;

    AdhocTrackMap::iterator it = m_adhocTrackMap.begin();
    for( ; it != m_adhocTrackMap.end(); ++it )
    {
      if( (*it).second.context == context && (*it).second.remote == stanza->from() )
      {
        Tag *q = stanza->findChild( "query", "xmlns", XMLNS_DISCO_ITEMS );
        if( q )
        {
          StringMap commands;
          const Tag::TagList& l = q->children();
          Tag::TagList::const_iterator itt = l.begin();
          for( ; itt != l.end(); ++itt )
          {
            const std::string& name = (*itt)->findAttribute( "name" );
            const std::string& node = (*itt)->findAttribute( "node" );
            if( (*itt)->name() == "item" && !name.empty() && !node.empty() )
            {
              commands[node] = name;
            }
          }
          (*it).second.ah->handleAdhocCommands( (*it).second.remote, commands );
        }

        m_adhocTrackMap.erase( it );
        break;
      }
    }
  }
Exemple #2
0
GSIOTInfo::GSIOTInfo( const Tag* tag )
	:StanzaExtension( ExtIot ), m_isInputDevice(false)
{
	if( !tag || tag->name() != "gsiot" || tag->xmlns() != XMLNS_GSIOT )
       return;

	Tag *d = tag->findChild("device");
	if(d)
	{
		Tag *ttypelist = d->findChild("typelist");
		if( ttypelist )
		{
			const TagList& l = ttypelist->children();
			TagList::const_iterator it = l.begin();
			for( ; it != l.end(); ++it )
			{
				if((*it)->name() != "type")
					continue;

				m_getForType.insert( (IOTDeviceType)atoi( (*it)->cdata().c_str() ) );
			}
		}
	}

	//const TagList& l = tag->children();
	//TagList::const_iterator it = l.begin();
	//for( ; it != l.end(); ++it )
	//{
	//	if((*it)->name() == "device"){
	//		this->m_deviceList.push_back(new GSIOTDevice(*it));
	//	}
	//}
}
Exemple #3
0
int TagModel::rowCount(const QModelIndex & parent) const {
    if (parent.column() >= COUNT_COL)
        return 0;

    if (!parent.isValid())
        return mTagRoot->children().size();
    Tag * tag = static_cast<Tag *>(parent.internalPointer());
    return tag->children().size();
}
Exemple #4
0
void XmppGSAuth_base::untagAuth_base( const Tag* tag )
{
	Tag *tAuth = tag->findChild("authority");

	if( tAuth )
	{
		if(tAuth->hasAttribute("method"))
		{
			m_srcmethod = tAuth->findAttribute("method");
			std::string method = m_srcmethod;
			g_toLowerCase( method );
			if( method == "add" )
			{
				m_method = defCfgOprt_AddModify;
			}
			else if( method == "edit" )
			{
				m_method = defCfgOprt_Modify;
			}
			else if( method == "delete" )
			{
				m_method = defCfgOprt_Delete;
			}
			else if( method == "getself" )
			{
				m_method = defCfgOprt_GetSelf;
			}
			else if( method == "getsimple" )
			{
				m_method = defCfgOprt_GetSimple;
			}
		}

		const TagList& l = tAuth->children();
		TagList::const_iterator it = l.begin();
		for( ; it != l.end(); ++it )
		{
			if((*it)->name() == "user")
			{
				GSIOTUser *pUser = new GSIOTUser(*it);
				std::string jid = pUser->GetKeyJid();

				if( GSIOTUserMgr::usermapFind( m_mapUser, jid ) == m_mapUser.end() )
				{
					GSIOTUserMgr::usermapInsert( m_mapUser, pUser );
				}
				else
				{
					delete pUser;
				}
			}
		}
	}
}
  bool SOCKS5BytestreamManager::handleIq( Stanza *stanza )
  {
    Tag* q = stanza->findChild( "query", "xmlns", XMLNS_BYTESTREAMS );
    if( !q || !m_socks5BytestreamHandler )
      return false;

    if( m_trackMap.find( stanza->id() ) != m_trackMap.end() )
      return false;

    switch( stanza->subtype() )
    {
      case StanzaIqSet:
      {
        const std::string& sid = q->findAttribute( "sid" );
        const std::string& mode = q->findAttribute( "mode" );
        if( haveStream( stanza->from() ) || sid.empty() || mode == "udp" )
        {
          rejectSOCKS5Bytestream( stanza->from(), stanza->id(), StanzaErrorNotAcceptable );
          return true;
        }
        AsyncS5BItem asi;
        Tag::TagList& l = q->children();
        Tag::TagList::const_iterator it = l.begin();
        for( ; it != l.end(); ++it )
        {
          if( (*it)->name() == "streamhost" && (*it)->hasAttribute( "jid" )
                && (*it)->hasAttribute( "host" ) && (*it)->hasAttribute( "port" ) )
          {
            StreamHost sh;
            sh.jid = (*it)->findAttribute( "jid" );
            sh.host = (*it)->findAttribute( "host" );
            sh.port = atoi( (*it)->findAttribute( "port" ).c_str() );
            asi.sHosts.push_back( sh );
          }
        }
        asi.id = stanza->id();
        asi.from = stanza->from();
        asi.incoming = true;
        m_asyncTrackMap[sid] = asi;
        m_socks5BytestreamHandler->handleIncomingSOCKS5BytestreamRequest( sid, stanza->from() );
        break;
      }
      case StanzaIqError:
        m_socks5BytestreamHandler->handleSOCKS5BytestreamError( stanza, std::string() );
        break;
      default:
        break;
    }

    return true;
  }
Exemple #6
0
 Tag *MRIMProtocol::getVCardTag(AbstractUser *user, GList *vcardEntries) {
     PurpleNotifyUserInfoEntry *vcardEntry;
     std::string label;
     std::string firstName;
     std::string lastName;
     std::string header;
  
     Tag *N = new Tag("N");
     Tag *head = new Tag("ADR");
     Tag *vcard = new Tag( "vCard" );
     vcard->addAttribute( "xmlns", "vcard-temp" );
  
     while (vcardEntries) {
         vcardEntry = (PurpleNotifyUserInfoEntry *)(vcardEntries->data);
         if (purple_notify_user_info_entry_get_label(vcardEntry) && purple_notify_user_info_entry_get_value(vcardEntry)){
             label=(std::string)purple_notify_user_info_entry_get_label(vcardEntry);
             Log("vcard label", label << " => " << (std::string)purple_notify_user_info_entry_get_value(vcardEntry));
             if (label=="Firstname"){
                 N->addChild( new Tag("GIVEN", (std::string)purple_notify_user_info_entry_get_value(vcardEntry)));
                 firstName = (std::string)purple_notify_user_info_entry_get_value(vcardEntry);
             }
             else if (label=="Lastname"){
                 N->addChild( new Tag("FAMILY", (std::string)purple_notify_user_info_entry_get_value(vcardEntry)));
                 lastName = (std::string)purple_notify_user_info_entry_get_value(vcardEntry);
             }
             else if (label=="Nickname"){
                 vcard->addChild( new Tag("NICKNAME", (std::string)purple_notify_user_info_entry_get_value(vcardEntry)));
             }
             else if (label=="Sex"){
                 vcard->addChild( new Tag("GENDER", (std::string)purple_notify_user_info_entry_get_value(vcardEntry)));
             }
             else if (label=="Birthday"){
                 vcard->addChild( new Tag("BDAY", (std::string)purple_notify_user_info_entry_get_value(vcardEntry)));
             }
            else if (label=="E-Mail"){
                vcard->addChild( new Tag("UID", (std::string)purple_notify_user_info_entry_get_value(vcardEntry)));
            }
         }
         vcardEntries = vcardEntries->next;
     }
     if (!firstName.empty() || !lastName.empty())
         vcard->addChild( new Tag("FN", firstName + " " + lastName));
         // add photo ant N if any
         if(!N->children().empty())
             vcard->addChild(N);
  
     return vcard;
 }
Exemple #7
0
XmppGSManager::XmppGSManager( const Tag* tag )
	:StanzaExtension(ExtIotManager), m_method(defCfgOprt_Unknown)
{
	if( !tag || tag->name() != "gsiot" || tag->xmlns() != XMLNS_GSIOT_MANAGER )
		return;

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

	if( 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;
			}
		}

		const TagList& l = tmgr->children();
		TagList::const_iterator it = l.begin();
		for( ; it != l.end(); ++it )
		{
			if((*it)->name() == "device")
			{
				m_devices.push_back( new GSIOTDevice(*it) );
			}
		}
	}
}
Exemple #8
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() );
		} 
	}
}
Exemple #9
0
XmppGSTalk::XmppGSTalk( const Tag* tag )
	:StanzaExtension(ExtIotTalk), m_cmd(defTalkCmd_Unknown), m_result(true)
{
	if( !tag || tag->name() != "gstalk" || tag->xmlns() != XMLNS_GSIOT_TALK )
		return;

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

	if( tmgr )
	{
		if( tmgr->findChild("command") )
		{
			m_strSrcCmd = tmgr->findChild("command")->cdata();
			std::string strcmd = m_strSrcCmd;
			g_toLowerCase( strcmd );
			if( strcmd == "request" )
			{
				m_cmd = defTalkCmd_request;
			}
			else if( strcmd == "accept" )
			{
				m_cmd = defTalkCmd_accept;
			}
			else if( strcmd == "reject" )
			{
				m_cmd = defTalkCmd_reject;
			}
			else if( strcmd == "session" )
			{
				m_cmd = defTalkCmd_session;
			}
			else if( strcmd == "adddev" )
			{
				m_cmd = defTalkCmd_adddev;
			}
			else if( strcmd == "removedev" )
			{
				m_cmd = defTalkCmd_removedev;
			}
			else if( strcmd == "keepalive" )
			{
				m_cmd = defTalkCmd_keepalive;
			}
			else if( strcmd == "quit" )
			{
				m_cmd = defTalkCmd_quit;
			}
			else if( strcmd == "forcequit" )
			{
				m_cmd = defTalkCmd_forcequit;
			}
			else
			{
				m_cmd = defTalkCmd_Unknown;
			}
		}

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

		Tag *tdevicelist = tmgr->findChild("devicelist");
		if( tdevicelist )
		{
			const TagList& l = tdevicelist->children();
			TagList::const_iterator it = l.begin();
			for( ; it != l.end(); ++it )
			{
				Tag *tDev = (*it);
				if( tDev->name() == "device" )
				{
					if( tDev->hasAttribute("type") && tDev->hasAttribute("id") )
					{
						IOTDeviceType dev_type = (IOTDeviceType)atoi( tDev->findAttribute("type").c_str() );
						int dev_id = atoi( tDev->findAttribute("id").c_str() );

						m_vecdev.push_back( GSIOTDeviceKey(dev_type,dev_id) );
					}
				}
			}
		}
	}
}
int main( int /*argc*/, char** /*argv*/ )
{
  int fail = 0;
  std::string name;
  DataFormField *f;

  // -------
  name = "empty field";
  f = new DataFormField();
  if( f->type() != DataFormField::TypeTextSingle )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  delete f;
  f = 0;

  // -------
  name = "TypeBoolean field";
  f = new DataFormField( DataFormField::TypeBoolean );
  if( f->type() != DataFormField::TypeBoolean )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  delete f;
  f = 0;

  // -------
  name = "2nd ctor";
  f = new DataFormField( "fieldName", "fieldValue", "fieldLabel", DataFormField::TypeBoolean );
  if( f->type() != DataFormField::TypeBoolean || f->name() != "fieldName" ||
      f->value() != "fieldValue" || f->label() != "fieldLabel" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  delete f;
  f = 0;

  // -------
  name = "parse 0";
  f = new DataFormField( 0 );
  if( f->type() != DataFormField::TypeInvalid )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  delete f;
  f = 0;
  Tag*t;

  // -------
  name = "set name";
  f = new DataFormField();
  f->setName( name );
  if( f->name() != name )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  delete f;
  f = 0;

  // -------
  name = "set required";
  f = new DataFormField();
  bool req = true;
  f->setRequired( req );
  if( f->required() != req )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  delete f;
  f = 0;

  // -------
  name = "set label";
  f = new DataFormField();
  f->setLabel( name );
  if( f->label() != name )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  delete f;
  f = 0;

  // -------
  name = "set value";
  f = new DataFormField();
  f->setValue( name );
  if( f->value() != name )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  delete f;
  f = 0;

  // -------
  name = "set values";
  f = new DataFormField();
  StringList val;
  val.push_back( "val 1" );
  val.push_back( "val 2" );
  val.push_back( "val 3" );
  f->setValues( val );
  if( f->values() != val )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  delete f;
  f = 0;

  // -------
  name = "set values";
  f = new DataFormField();
  StringMultiMap opt;
  opt.insert( std::make_pair( "lock", "1" ) );
  opt.insert( std::make_pair( "stock", "1" ) );
  opt.insert( std::make_pair( "smoking barrel", "2" ) );
  f->setOptions( opt );
  if( f->options() != opt )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
  }
  delete f;
  f = 0;

  // -------
  name = "parse Tag 1";
  t = new Tag( "field");
  t->addAttribute( "type", "fixed" );
  new Tag( t, "value", "abc" );
  f = new DataFormField( t );
  Tag *ft = f->tag();
  if( *ft != *t )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
    printf( "f->tag(): %s\n", f->tag()->xml().c_str() );
    printf( "       t: %s\n", t->xml().c_str() );
  }
  delete f;
  delete t;
  delete ft;
  f = 0;

  // -------
  t = new Tag( "field");
  t->addAttribute( "type", "list-multi" );
  t->addAttribute( "label", "blabla label" );
  t->addAttribute( "var", "features" );
  Tag *o = new Tag( t, "option" );
  o->addAttribute( "label", "lock" );
  new Tag( o, "value", "lock" );
  o = new Tag( t, "option" );
  o->addAttribute( "label", "stock" );
  new Tag( o, "value", "stock" );
  o = new Tag( t, "option" );
  o->addAttribute( "label", "smoking barrel" );
  new Tag( o, "value", "smoking barrel" );
  new Tag( t, "value", "lock" );
  new Tag( t, "value", "stock" );
  f = new DataFormField( t );
  Tag *r = f->tag();
  name = "parse Tag 2.1";
  if( r->name() != "field" || !r->hasAttribute( "type", "list-multi" ) )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
    printf( "f->tag(): %s\n", f->tag()->xml().c_str() );
    printf( "       t: %s\n", t->xml().c_str() );
  }

  name = "parse Tag 2.2";
  if( !r->hasAttribute( "label", "blabla label" ) || !r->hasAttribute( "var", "features" ) )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
    printf( "f->tag(): %s\n", f->tag()->xml().c_str() );
    printf( "       t: %s\n", t->xml().c_str() );
  }

  name = "parse Tag 2.3";
  if( !r->hasChild( "option" ) || !r->findChild( "option" )->hasAttribute( "label", "lock" ) )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
    printf( "f->tag(): %s\n", f->tag()->xml().c_str() );
    printf( "       t: %s\n", t->xml().c_str() );
  }

  name = "parse Tag 2.4";
  if( !r->hasChild( "option", "label", "stock" ) )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
    printf( "f->tag(): %s\n", f->tag()->xml().c_str() );
    printf( "       t: %s\n", t->xml().c_str() );
  }

  name = "parse Tag 2.5";
  if( !r->hasChild( "option", "label", "smoking barrel" ) )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
    printf( "f->tag(): %s\n", f->tag()->xml().c_str() );
    printf( "       t: %s\n", t->xml().c_str() );
  }

  name = "parse Tag 2.6";
  if( !r->findChild( "option" )->findChild( "value" ) )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
    printf( "f->tag(): %s\n", f->tag()->xml().c_str() );
    printf( "       t: %s\n", t->xml().c_str() );
  }

  name = "parse Tag 2.7";
  if( r->findChild( "option" )->findChild( "value" )->cdata() != "lock" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
    printf( "f->tag(): %s\n", f->tag()->xml().c_str() );
    printf( "       t: %s\n", t->xml().c_str() );
  }

  name = "parse Tag 2.8";
  TagList l = r->children();
  TagList::const_iterator it = l.begin();
  for( ; it != l.end(); ++it )
  {
    if( (*it)->name() == "option" && ( !(*it)->hasChildWithCData( "value", "lock" ) &&
          !(*it)->hasChildWithCData( "value", "stock" ) &&
          !(*it)->hasChildWithCData( "value", "smoking barrel" ) ) )
    {
      ++fail;
      fprintf( stderr, "test '%s' failed\n", name.c_str() );
      printf( "f->tag(): %s\n", f->tag()->xml().c_str() );
      printf( "       t: %s\n", t->xml().c_str() );
    }
  }

  name = "parse Tag 2.9";
  if( !r->hasChildWithCData( "value", "lock" ) || !r->hasChildWithCData( "value", "stock" ) )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
    printf( "f->tag(): %s\n", f->tag()->xml().c_str() );
    printf( "       t: %s\n", t->xml().c_str() );
  }
  delete f;
  delete t;
  delete r;
  f = 0;



  // -------
  name = "boolean duplicate <value/>";
  f = new DataFormField( DataFormField::TypeBoolean );
  f->setName( "name" );
  f->setValue( "1" );
  f->setLabel( "label" );
  t = f->tag();
  if( t->children().size() != 1 || t->xml() != "<field type='boolean' var='name' "
                                               "label='label'><value>1</value></field>" )
  {
    ++fail;
    fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t->xml().c_str() );
  }
  delete f;
  delete t;
  f = 0;
  t = 0;









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

}
Exemple #11
0
  void RosterManager::extractItems( Tag *tag, bool isPush )
  {
    Tag *t = tag->findChild( "query" );
    const Tag::TagList& l = t->children();
    Tag::TagList::const_iterator it = l.begin();
    for( ; it != l.end(); ++it )
    {
      if( (*it)->name() == "item" )
      {
        StringList gl;
        if( (*it)->hasChild( "group" ) )
        {
          const Tag::TagList& g = (*it)->children();
          Tag::TagList::const_iterator it_g = g.begin();
          for( ; it_g != g.end(); ++it_g )
          {
            gl.push_back( (*it_g)->cdata() );
          }
        }

        const JID& jid = (*it)->findAttribute( "jid" );
        Roster::iterator it_d = m_roster.find( jid.bare() );
        if( it_d != m_roster.end() )
        {
          (*it_d).second->setName( (*it)->findAttribute( "name" ) );
          const std::string& sub = (*it)->findAttribute( "subscription" );
          if( sub == "remove" )
          {
            delete (*it_d).second;
            m_roster.erase( it_d );
            if( m_rosterListener )
              m_rosterListener->handleItemRemoved( jid );
            continue;
          }
          const std::string& ask = (*it)->findAttribute( "ask" );
          bool a = false;
          if( !ask.empty() )
            a = true;
          (*it_d).second->setSubscription( sub, a );
          (*it_d).second->setGroups( gl );
          (*it_d).second->setSynchronized();

          if( isPush && m_rosterListener )
            m_rosterListener->handleItemUpdated( jid );
        }
        else
        {
          const std::string& sub = (*it)->findAttribute( "subscription" );
          if( sub == "remove" )
            continue;
          const std::string& name = (*it)->findAttribute( "name" );
          const std::string& ask = (*it)->findAttribute( "ask" );
          bool a = false;
          if( !ask.empty() )
            a = true;

          StringList caps;
          add( jid.bare(), name, gl, caps, sub, a );
          if( isPush && m_rosterListener )
            m_rosterListener->handleItemAdded( jid );
        }
      }
    }
  }
Exemple #12
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;
}
Exemple #13
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;
  }