15 #include "adhochandler.h"
16 #include "adhoccommandprovider.h"
19 #include "discohandler.h"
20 #include "clientbase.h"
23 #include "mutexguard.h"
28 static const char* cmdActionStringValues[] =
30 "execute",
"cancel",
"prev",
"next",
"complete"
35 return util::lookup2( action, cmdActionStringValues );
38 static const char* cmdStatusStringValues[] =
40 "executing",
"completed",
"canceled"
45 return util::lookup( status, cmdStatusStringValues );
48 static const char* cmdNoteStringValues[] =
50 "info",
"warn",
"error"
55 return util::lookup( sev, cmdNoteStringValues );
60 : m_severity( InvalidSeverity )
62 if( !tag || tag->
name() !=
"note" )
65 m_severity = (Severity)util::deflookup( tag->
findAttribute(
"type" ), cmdNoteStringValues, Info );
66 m_note = tag->
cdata();
74 Tag* n =
new Tag(
"note", m_note );
91 m_form( form ), m_action( InvalidAction ), m_status( status ), m_actions( 0 )
99 m_form( form ), m_action( action ), m_actions( 0 )
104 Action executeAction,
int allowedActions,
107 m_form( form ), m_action( executeAction ), m_status( status ), m_actions( allowedActions )
139 ConstTagList::const_iterator it = l.begin();
140 for( ; it != l.end(); ++it )
141 m_notes.push_back(
new Note( (*it) ) );
165 Tag* c =
new Tag(
"command" );
172 if( m_status != InvalidStatus )
177 Tag* actions =
new Tag( c,
"actions" );
179 if( m_action != InvalidAction )
184 if( ( m_actions & Previous ) == Previous )
185 new Tag( actions,
"prev" );
186 if( ( m_actions & Next ) == Next )
187 new Tag( actions,
"next" );
188 if( ( m_actions & Complete ) == Complete )
189 new Tag( actions,
"complete" );
195 if( m_action != InvalidAction )
197 if( m_status != InvalidStatus )
201 if ( !m_sessionid.empty() )
204 if( m_form && *m_form )
207 NoteList::const_iterator it = m_notes.begin();
208 for( ; it != m_notes.end(); ++it )
219 if( !m_parent || !m_parent->
disco() )
231 m_adhocTrackMapMutex.
lock();
232 m_adhocTrackMap.clear();
233 m_adhocTrackMapMutex.
unlock();
235 if( !m_parent || !m_parent->
disco() )
263 StringMap::const_iterator it = m_items.begin();
264 for( ; it != m_items.end(); ++it )
266 AdhocCommandProviderMap::const_iterator itp = m_adhocCommandProviders.find( (*it).first );
267 if( itp != m_adhocCommandProviders.end()
269 && (*itp).second->handleAdhocAccessRequest( from, (*it).first ) )
271 l.push_back(
new Disco::Item( m_parent->
jid(), (*it).first, (*it).second ) );
281 StringMap::const_iterator it = m_items.find( node );
284 it == m_items.end() ?
"Ad-Hoc Commands" : (*it).second ) );
294 if( !ac || ac->
node().empty())
297 AdhocCommandProviderMap::const_iterator it = m_adhocCommandProviders.find( ac->
node() );
298 if( it != m_adhocCommandProviders.end() )
301 m_activeSessions[sess] = iq.
id();
302 (*it).second->handleAdhocCommand( iq.
from(), *ac, sess );
311 if( context != ExecuteAdhocCommand )
314 m_adhocTrackMapMutex.
lock();
315 AdhocTrackMap::iterator it = m_adhocTrackMap.find( iq.
id() );
316 bool haveIdHandler = ( it != m_adhocTrackMap.end() );
317 m_adhocTrackMapMutex.
unlock();
318 if( !haveIdHandler || (*it).second.context != context
319 || (*it).second.remote != iq.
from() )
325 (*it).second.ah->handleAdhocError( iq.
from(), iq.
error(), (*it).second.handlerContext );
331 (*it).second.ah->handleAdhocExecutionResult( iq.
from(), *ac, (*it).second.handlerContext );
337 m_adhocTrackMapMutex.
lock();
338 m_adhocTrackMap.erase( it );
339 m_adhocTrackMapMutex.
unlock();
343 const std::string& name )
345 if( !m_parent || !m_parent->
disco() )
349 m_adhocCommandProviders[command] = acp;
350 m_items[command] = name;
355 if( context != CheckAdhocSupport )
360 AdhocTrackMap::iterator it = m_adhocTrackMap.begin();
361 for( ; it != m_adhocTrackMap.end() && (*it).second.context != context
362 && (*it).second.remote != from; ++it )
364 if( it == m_adhocTrackMap.end() )
368 m_adhocTrackMap.erase( it );
373 if( context != FetchAdhocCommands )
378 AdhocTrackMap::iterator it = m_adhocTrackMap.begin();
379 for( ; it != m_adhocTrackMap.end(); ++it )
381 if( (*it).second.context == context && (*it).second.remote == from )
385 Disco::ItemList::const_iterator it2 = l.begin();
386 for( ; it2 != l.end(); ++it2 )
388 commands[(*it2)->node()] = (*it2)->name();
390 (*it).second.ah->handleAdhocCommands( from, commands, (*it).second.handlerContext );
392 m_adhocTrackMap.erase( it );
402 AdhocTrackMap::iterator it = m_adhocTrackMap.begin();
403 for( ; it != m_adhocTrackMap.end(); ++it )
405 if( (*it).second.context == context && (*it).second.remote == from )
407 (*it).second.ah->handleAdhocError( from, error, (*it).second.handlerContext );
409 m_adhocTrackMap.erase( it );
416 if( !remote || !ah || !m_parent || !m_parent->
disco() )
420 track.remote = remote;
421 track.context = CheckAdhocSupport;
423 track.handlerContext = context;
424 const std::string&
id = m_parent->
getID();
425 m_adhocTrackMapMutex.
lock();
426 m_adhocTrackMap[id] = track;
427 m_adhocTrackMapMutex.
unlock();
433 if( !remote || !ah || !m_parent || !m_parent->
disco() )
437 track.remote = remote;
438 track.context = FetchAdhocCommands;
440 track.handlerContext = context;
441 const std::string&
id = m_parent->
getID();
442 m_adhocTrackMapMutex.
lock();
443 m_adhocTrackMap[id] = track;
444 m_adhocTrackMapMutex.
unlock();
450 if( !remote || !command || !m_parent || !ah )
453 const std::string&
id = m_parent->
getID();
458 track.remote = remote;
459 track.context = ExecuteAdhocCommand;
462 track.handlerContext = context;
463 m_adhocTrackMapMutex.
lock();
464 m_adhocTrackMap[id] = track;
465 m_adhocTrackMapMutex.
unlock();
467 m_parent->
send( iq,
this, ExecuteAdhocCommand );
472 if( !remote || !command || !m_parent )
475 StringMap::iterator it = m_activeSessions.find( command->
sessionID() );
476 if( it == m_activeSessions.end() )
483 m_parent->
send( re );
484 m_activeSessions.erase( it );
489 if( !m_parent || !m_parent->
disco() )
493 m_adhocCommandProviders.erase( command );
494 m_items.erase( command );