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 );
401 for( AdhocTrackMap::iterator it = m_adhocTrackMap.begin(); it != m_adhocTrackMap.end(); )
403 if( (*it).second.context == context && (*it).second.remote == from )
405 (*it).second.ah->handleAdhocError( from, error, (*it).second.handlerContext );
412 m_adhocTrackMap.erase( it++ );
423 if( !remote || !ah || !m_parent || !m_parent->
disco() )
427 track.remote = remote;
428 track.context = CheckAdhocSupport;
430 track.handlerContext = context;
431 const std::string&
id = m_parent->
getID();
432 m_adhocTrackMapMutex.
lock();
433 m_adhocTrackMap[id] = track;
434 m_adhocTrackMapMutex.
unlock();
440 if( !remote || !ah || !m_parent || !m_parent->
disco() )
444 track.remote = remote;
445 track.context = FetchAdhocCommands;
447 track.handlerContext = context;
448 const std::string&
id = m_parent->
getID();
449 m_adhocTrackMapMutex.
lock();
450 m_adhocTrackMap[id] = track;
451 m_adhocTrackMapMutex.
unlock();
457 if( !remote || !command || !m_parent || !ah )
460 const std::string&
id = m_parent->
getID();
465 track.remote = remote;
466 track.context = ExecuteAdhocCommand;
469 track.handlerContext = context;
470 m_adhocTrackMapMutex.
lock();
471 m_adhocTrackMap[id] = track;
472 m_adhocTrackMapMutex.
unlock();
474 m_parent->
send( iq,
this, ExecuteAdhocCommand );
479 if( !remote || !command || !m_parent )
482 StringMap::iterator it = m_activeSessions.find( command->
sessionID() );
483 if( it == m_activeSessions.end() )
490 m_parent->
send( re );
491 m_activeSessions.erase( it );
496 if( !m_parent || !m_parent->
disco() )
500 m_adhocCommandProviders.erase( command );
501 m_items.erase( command );