16 #include "rosterprovider.h"
19 #include <gloox/adhoc.h>
20 #include <gloox/dataform.h>
21 #include <gloox/dataformfield.h>
22 #include <gloox/error.h>
24 #include <gloox/jid.h>
25 #include <gloox/logsink.h>
26 #include <gloox/mutexguard.h>
27 #include <gloox/sha.h>
28 #include <gloox/util.h>
38 const gloox::JID& _name )
39 :
ComponentBase( _router, cm, _name, _logInstance ), m_rosterProvider( rp ),
40 m_threading( threading )
46 gloox::util::clearList( m_plugins );
47 gloox::util::clearList( m_queue );
48 SessionList::iterator it = m_sessions.begin();
49 for( ; it != m_sessions.end(); ++it )
51 delete (*it).second.presence;
52 (*it).second.presence = 0;
58 if( !m_threading && !m_queue.size() )
63 m_queueSemaphore.
wait();
64 gloox::Tag* t = m_queue.front();
67 m_queueMutex.unlock();
69 }
while( m_threading );
74 if( jid.username().empty() || jid.server().empty() )
77 "Cannot create session for " + jid.full()
78 +
". Need a username and server." );
82 if( jid.resource().empty() )
86 sha.feed( jid.full() );
87 sha.feed( gloox::util::int2string( time( 0 ) ) );
88 jid.setResource( sha.hex() );
91 m_sessionMutex.lock();
92 SessionList::iterator it = m_sessions.find( jid.full() );
93 if( it != m_sessions.end() )
95 const std::string tid = (*it).second.id;
96 m_sessionMutex.unlock();
98 "Session for " + jid.full() +
" already " "exists. Replacing by new session." );
99 gloox::Tag* d =
new gloox::Tag(
"disconnect" );
101 gloox::Tag* c =
new gloox::Tag( d,
"conflict" );
102 c->setXmlns( gloox::XMLNS_XMPP_STREAM );
103 gloox::Tag* t =
new gloox::Tag( c,
"text",
"conflicting resource connected." );
104 t->setXmlns( gloox::XMLNS_XMPP_STREAM );
105 t->addAttribute(
"xml:lang",
"en" );
107 j.setResource( tid );
108 d->addAttribute(
"to", j.full() );
110 m_sessionMutex.lock();
116 m_sessions.insert( std::make_pair( jid.full(), s ) );
117 m_sessionMutex.unlock();
119 "Created session for " + jid.full()
124 const SM::SessionList SM::findSessions(
const gloox::JID& jid )
127 m_sessionMutex.lock();
128 SessionList::iterator it = m_sessions.begin();
129 for( ; it != m_sessions.end(); ++it )
131 gloox::JID j( (*it).first );
132 if( j.bare() == jid.bare() )
133 sl.insert( std::make_pair( (*it).first, (*it).second ) );
135 m_sessionMutex.unlock();
143 printf(
"--------------------removing sessions for client instance %s\n", jid.c_str() );
145 gloox::util::MutexGuard mg( m_sessionMutex );
146 SessionList::iterator it = m_sessions.find( jid );
147 if( it != m_sessions.end() )
149 delete (*it).second.presence;
150 m_sessions.erase( it );
159 if( !m_rosterProvider || !pres )
162 const gloox::JID& from( pres->findAttribute(
"from" ) );
165 printf(
"dispatching presence for %s\n", from.full().c_str() );
169 SessionList::iterator itsl = m_sessions.find( from.full() );
170 if( itsl != m_sessions.end() )
172 first = !( (*itsl).second.presence );
176 Roster::const_iterator it = rm.begin();
177 for( ; it != rm.end(); ++it )
180 printf(
"looking for sessions for %s ...", (*it).first.c_str() );
182 const SessionList cl = findSessions( (*it).first );
186 printf(
" found %d\n", cl.size() );
188 SessionList::const_iterator its = cl.begin();
189 for( ; its != cl.end(); ++its )
192 if( (*it).second == gloox::S10nBoth
193 || (*it).second == gloox::S10nFrom )
197 printf(
"in for: stamping 'to': %s\n", (*its).first.c_str() );
199 p->addAttribute(
"to", (*its).first );
202 if( first && (*its).second.presence
203 && ( (*it).second == gloox::S10nBoth
204 || (*it).second == gloox::S10nTo ) )
206 p = (*its).second.presence->clone();
208 printf(
"in for/if: stamping 'to': %s\n", from.full().c_str() );
210 p->addAttribute(
"to", from.full() );
217 printf(
" none found\n" );
221 if( itsl != m_sessions.end() )
223 delete (*itsl).second.presence;
224 (*itsl).second.presence = pres->clone();
233 gloox::JID jid( msg->findAttribute(
"to" ) );
234 const SessionList sl = findSessions( jid );
235 SessionList::const_iterator it = sl.begin();
236 for( ; it != sl.end(); ++it )
238 gloox::Tag* m = msg->clone();
239 m->addAttribute(
"to", (*it).first );
249 void SM::handleIncomingTag( gloox::Tag* tag )
255 m_queue.push_back( tag );
256 m_queueMutex.unlock();
257 m_queueSemaphore.
post();
260 void SM::handleTag( gloox::Tag* tag )
268 bool handled =
false;
269 PluginList::const_iterator it = m_plugins.begin();
270 for( ; it != m_plugins.end(); ++it )
272 const std::string& xpath = (*it)->filterString();
273 const gloox::Tag* x = tag->findTag( xpath );
274 if( x && (*it)->handleTag( x ) )
284 printf(
"!_____________!!!!!!!!!!!!______returningError for id: %s\n", tag->findAttribute(
"id" ).c_str() );
306 m_router.removeDomain(
this, domain,
false );
313 m_queue.push_back( 0 );
314 m_queueMutex.unlock();