15 #include "adhochandler.h"
16 #include "adhoccommandprovider.h"
21 #include "discohandler.h"
22 #include "clientbase.h"
23 #include "adhocplugin.h"
25 #include "mutexguard.h"
30 static const char* cmdActionStringValues[] =
32 "execute",
"cancel",
"prev",
"next",
"complete"
37 return util::lookup2( action, cmdActionStringValues );
40 static const char* cmdStatusStringValues[] =
42 "executing",
"completed",
"canceled"
47 return util::lookup( status, cmdStatusStringValues );
50 static const char* cmdNoteStringValues[] =
52 "info",
"warn",
"error"
57 return util::lookup( sev, cmdNoteStringValues );
62 : m_severity( InvalidSeverity )
64 if( !tag || tag->
name() !=
"note" )
67 m_severity = (Severity)util::deflookup( tag->
findAttribute(
"type" ), cmdNoteStringValues, Info );
68 m_note = tag->
cdata();
76 Tag* n =
new Tag(
"note", m_note );
93 m_plugin( plugin ), m_action( InvalidAction ), m_status( status ), m_actions( 0 )
101 m_plugin( plugin ), m_action( action ), m_actions( 0 )
106 Action executeAction,
int allowedActions,
109 m_plugin( plugin ), m_action( executeAction ), m_status( status ), m_actions( allowedActions )
141 ConstTagList::const_iterator it = l.begin();
142 for( ; it != l.end(); ++it )
143 m_notes.push_back(
new Note( (*it) ) );
152 m_plugin =
new IOData( x );
173 Tag* c =
new Tag(
"command" );
180 if( m_status != InvalidStatus )
185 Tag* actions =
new Tag( c,
"actions" );
187 if( m_action != InvalidAction )
192 if( ( m_actions & Previous ) == Previous )
193 new Tag( actions,
"prev" );
194 if( ( m_actions & Next ) == Next )
195 new Tag( actions,
"next" );
196 if( ( m_actions & Complete ) == Complete )
197 new Tag( actions,
"complete" );
203 if( m_action != InvalidAction )
205 if( m_status != InvalidStatus )
209 if ( !m_sessionid.empty() )
212 if( m_plugin && *m_plugin )
215 NoteList::const_iterator it = m_notes.begin();
216 for( ; it != m_notes.end(); ++it )
227 if( !m_parent || !m_parent->
disco() )
239 m_adhocTrackMapMutex.
lock();
240 m_adhocTrackMap.clear();
241 m_adhocTrackMapMutex.
unlock();
243 if( !m_parent || !m_parent->
disco() )
271 StringMap::const_iterator it = m_items.begin();
272 for( ; it != m_items.end(); ++it )
274 AdhocCommandProviderMap::const_iterator itp = m_adhocCommandProviders.find( (*it).first );
275 if( itp != m_adhocCommandProviders.end()
277 && (*itp).second->handleAdhocAccessRequest( from, (*it).first ) )
279 l.push_back(
new Disco::Item( m_parent->
jid(), (*it).first, (*it).second ) );
289 StringMap::const_iterator it = m_items.find( node );
292 it == m_items.end() ?
"Ad-Hoc Commands" : (*it).second ) );
302 if( !ac || ac->
node().empty())
305 AdhocCommandProviderMap::const_iterator it = m_adhocCommandProviders.find( ac->
node() );
306 if( it != m_adhocCommandProviders.end() )
309 m_activeSessions[sess] = iq.
id();
310 (*it).second->handleAdhocCommand( iq.
from(), *ac, sess );
319 if( context != ExecuteAdhocCommand )
322 m_adhocTrackMapMutex.
lock();
323 AdhocTrackMap::iterator it = m_adhocTrackMap.find( iq.
id() );
324 bool haveIdHandler = ( it != m_adhocTrackMap.end() );
325 m_adhocTrackMapMutex.
unlock();
326 if( !haveIdHandler || (*it).second.context != context
327 || (*it).second.remote != iq.
from() )
333 (*it).second.ah->handleAdhocError( iq.
from(), iq.
error(), (*it).second.handlerContext );
339 (*it).second.ah->handleAdhocExecutionResult( iq.
from(), *ac, (*it).second.handlerContext );
345 m_adhocTrackMapMutex.
lock();
346 m_adhocTrackMap.erase( it );
347 m_adhocTrackMapMutex.
unlock();
351 const std::string& name )
353 if( !m_parent || !m_parent->
disco() )
357 m_adhocCommandProviders[command] = acp;
358 m_items[command] = name;
363 if( context != CheckAdhocSupport )
368 AdhocTrackMap::iterator it = m_adhocTrackMap.begin();
369 for( ; it != m_adhocTrackMap.end() && (*it).second.context != context
370 && (*it).second.remote != from; ++it )
372 if( it == m_adhocTrackMap.end() )
376 m_adhocTrackMap.erase( it );
381 if( context != FetchAdhocCommands )
386 AdhocTrackMap::iterator it = m_adhocTrackMap.begin();
387 for( ; it != m_adhocTrackMap.end(); ++it )
389 if( (*it).second.context == context && (*it).second.remote == from )
393 Disco::ItemList::const_iterator it2 = l.begin();
394 for( ; it2 != l.end(); ++it2 )
396 commands[(*it2)->node()] = (*it2)->name();
398 (*it).second.ah->handleAdhocCommands( from, commands, (*it).second.handlerContext );
400 m_adhocTrackMap.erase( it );
409 for( AdhocTrackMap::iterator it = m_adhocTrackMap.begin(); it != m_adhocTrackMap.end(); )
411 if( (*it).second.context == context && (*it).second.remote == from )
413 (*it).second.ah->handleAdhocError( from, error, (*it).second.handlerContext );
420 m_adhocTrackMap.erase( it++ );
431 if( !remote || !ah || !m_parent || !m_parent->
disco() )
435 track.remote = remote;
436 track.context = CheckAdhocSupport;
438 track.handlerContext = context;
439 const std::string&
id = m_parent->
getID();
440 m_adhocTrackMapMutex.
lock();
441 m_adhocTrackMap[id] = track;
442 m_adhocTrackMapMutex.
unlock();
448 if( !remote || !ah || !m_parent || !m_parent->
disco() )
452 track.remote = remote;
453 track.context = FetchAdhocCommands;
455 track.handlerContext = context;
456 const std::string&
id = m_parent->
getID();
457 m_adhocTrackMapMutex.
lock();
458 m_adhocTrackMap[id] = track;
459 m_adhocTrackMapMutex.
unlock();
465 if( !remote || !command || !m_parent || !ah )
468 const std::string&
id = m_parent->
getID();
473 track.remote = remote;
474 track.context = ExecuteAdhocCommand;
477 track.handlerContext = context;
478 m_adhocTrackMapMutex.
lock();
479 m_adhocTrackMap[id] = track;
480 m_adhocTrackMapMutex.
unlock();
482 m_parent->
send( iq,
this, ExecuteAdhocCommand );
487 if( !remote || !command || !m_parent )
490 StringMap::iterator it = m_activeSessions.find( command->
sessionID() );
491 if( it == m_activeSessions.end() )
498 m_parent->
send( re );
499 m_activeSessions.erase( it );
504 if( !m_parent || !m_parent->
disco() )
508 m_adhocCommandProviders.erase( command );
509 m_items.erase( command );