gloox  1.0.28
adhoc.h
1 /*
2  Copyright (c) 2004-2023 by Jakob Schröter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 
15 #ifndef ADHOC_H__
16 #define ADHOC_H__
17 
18 #include "adhocplugin.h"
19 #include "disco.h"
20 #include "disconodehandler.h"
21 #include "discohandler.h"
22 #include "iqhandler.h"
23 #include "stanzaextension.h"
24 #include "mutex.h"
25 
26 #include <string>
27 #include <list>
28 #include <map>
29 
30 namespace gloox
31 {
32 
33  class ClientBase;
34  class Stanza;
35  class AdhocHandler;
36  class AdhocCommandProvider;
37 
81  class GLOOX_API Adhoc : public DiscoNodeHandler, public DiscoHandler, public IqHandler
82  {
83  public:
91  class GLOOX_API Command : public StanzaExtension
92  {
93  friend class Adhoc;
94 
95  public:
96 
100  enum Action
101  {
102  Execute = 1,
104  Cancel = 2,
105  Previous = 4,
107  Next = 8,
109  Complete = 16,
110  InvalidAction = 32
111  };
112 
116  enum Status
117  {
121  InvalidStatus
122  };
123 
130  class GLOOX_API Note
131  {
132 
133  friend class Command;
134 
135  public:
139  enum Severity
140  {
141  Info,
143  Warning,
145  Error,
147  InvalidSeverity
148  };
149 
155  Note( Severity sev, const std::string& note )
156  : m_severity( sev ), m_note( note ) {}
157 
161  ~Note() {}
162 
167  Severity severity() const { return m_severity; }
168 
173  const std::string& content() const { return m_note; }
174 
179  Tag* tag() const;
180 
181  private:
182 #ifdef ADHOC_COMMANDS_TEST
183  public:
184 #endif
189  Note( const Tag* tag );
190 
191  Severity m_severity;
192  std::string m_note;
193  };
194 
198  typedef std::list<const Note*> NoteList;
199 
210  Command( const std::string& node, const std::string& sessionid, Action action,
211  AdhocPlugin* plugin = 0 );
212 
222  Command( const std::string& node, const std::string& sessionid, Status status,
223  AdhocPlugin* plugin = 0 );
224 
237  Command( const std::string& node, const std::string& sessionid, Status status,
238  Action executeAction, int allowedActions = Complete,
239  AdhocPlugin* plugin = 0 );
240 
250  Command( const std::string& node, Action action,
251  AdhocPlugin* plugin = 0 );
252 
257  Command( const Tag* tag = 0 );
258 
262  virtual ~Command();
263 
268  const std::string& node() const { return m_node; }
269 
274  const std::string& sessionID() const { return m_sessionid; }
275 
281  Status status() const { return m_status; }
282 
287  Action action() const { return m_action; }
288 
294  int actions() const { return m_actions; }
295 
300  const NoteList& notes() const { return m_notes; }
301 
307  void addNote( const Note* note ) { m_notes.push_back( note ); }
308 
314  GLOOX_DEPRECATED const AdhocPlugin* form() const { return m_plugin; }
315 
320  const AdhocPlugin* plugin() const { return m_plugin; }
321 
322  // reimplemented from StanzaExtension
323  virtual const std::string& filterString() const;
324 
325  // reimplemented from StanzaExtension
326  virtual StanzaExtension* newInstance( const Tag* tag ) const
327  {
328  return new Command( tag );
329  }
330 
331  // reimplemented from StanzaExtension
332  virtual Tag* tag() const;
333 
334  // reimplemented from StanzaExtension
335  virtual StanzaExtension* clone() const
336  {
337  Command* c = new Command();
338 
339  NoteList::const_iterator it = m_notes.begin();
340  for( ; it != m_notes.end(); ++it )
341  c->m_notes.push_back( new Note( *(*it) ) );
342 
343  c->m_node = m_node;
344  c->m_sessionid = m_sessionid;
345  c->m_plugin = m_plugin ? static_cast<AdhocPlugin*>( m_plugin->clone() ) : 0;
346  c->m_action = m_action;
347  c->m_status = m_status;
348  c->m_actions = m_actions;
349 
350  return c;
351  }
352 
353  private:
354 #ifdef ADHOC_COMMANDS_TEST
355  public:
356 #endif
357  NoteList m_notes;
358 
359  std::string m_node;
360  std::string m_sessionid;
361  AdhocPlugin* m_plugin;
362  Action m_action;
363  Status m_status;
364  int m_actions;
365  };
366 
372  Adhoc( ClientBase* parent );
373 
377  virtual ~Adhoc();
378 
385  void checkSupport( const JID& remote, AdhocHandler* ah, int context = 0 );
386 
394  void getCommands( const JID& remote, AdhocHandler* ah, int context = 0 );
395 
407  void execute( const JID& remote, const Adhoc::Command* command, AdhocHandler* ah, int context = 0 );
408 
421  void respond( const JID& remote, const Adhoc::Command* command, const Error* error = 0 );
422 
430  void registerAdhocCommandProvider( AdhocCommandProvider* acp, const std::string& command,
431  const std::string& name );
432 
438  void removeAdhocCommandProvider( const std::string& command );
439 
440  // reimplemented from DiscoNodeHandler
441  virtual StringList handleDiscoNodeFeatures( const JID& from, const std::string& node );
442 
443  // reimplemented from DiscoNodeHandler
444  virtual Disco::IdentityList handleDiscoNodeIdentities( const JID& from,
445  const std::string& node );
446 
447  // reimplemented from DiscoNodeHandler
448  virtual Disco::ItemList handleDiscoNodeItems( const JID& from, const JID& to, const std::string& node );
449 
450  // reimplemented from IqHandler
451  virtual bool handleIq( const IQ& iq );
452 
453  // reimplemented from IqHandler
454  virtual void handleIqID( const IQ& iq, int context );
455 
456  // reimplemented from DiscoHandler
457  virtual void handleDiscoInfo( const JID& from, const Disco::Info& info, int context );
458 
459  // reimplemented from DiscoHandler
460  virtual void handleDiscoItems( const JID& from, const Disco::Items& items, int context );
461 
462  // reimplemented from DiscoHandler
463  virtual void handleDiscoError( const JID& from, const Error* error, int context );
464 
465  private:
466 #ifdef ADHOC_TEST
467  public:
468 #endif
469  typedef std::map<const std::string, AdhocCommandProvider*> AdhocCommandProviderMap;
470  AdhocCommandProviderMap m_adhocCommandProviders;
471 
472  enum AdhocContext
473  {
474  CheckAdhocSupport,
475  FetchAdhocCommands,
476  ExecuteAdhocCommand
477  };
478 
479  struct TrackStruct
480  {
481  JID remote;
482  AdhocContext context;
483  std::string session;
484  AdhocHandler* ah;
485  int handlerContext;
486  };
487  typedef std::map<std::string, TrackStruct> AdhocTrackMap;
488  AdhocTrackMap m_adhocTrackMap;
489  util::Mutex m_adhocTrackMapMutex;
490 
491  ClientBase* m_parent;
492 
493  StringMap m_items;
494  StringMap m_activeSessions;
495 
496  };
497 
498 }
499 
500 #endif // ADHOC_H__
A virtual interface for an Ad-hoc Command Provider according to XEP-0050.
A virtual interface for an Ad-hoc Command users according to XEP-0050.
Definition: adhochandler.h:33
A base class for Adhoc Command plugins (DataForm, IO Data, ...).
Definition: adhocplugin.h:39
const std::string & content() const
Definition: adhoc.h:173
Severity severity() const
Definition: adhoc.h:167
Note(Severity sev, const std::string &note)
Definition: adhoc.h:155
An abstraction of an Adhoc Command element (from Adhoc Commands, XEP-0050) as a StanzaExtension.
Definition: adhoc.h:92
GLOOX_DEPRECATED const AdhocPlugin * form() const
Definition: adhoc.h:314
const AdhocPlugin * plugin() const
Definition: adhoc.h:320
void addNote(const Note *note)
Definition: adhoc.h:307
const std::string & node() const
Definition: adhoc.h:268
const std::string & sessionID() const
Definition: adhoc.h:274
const NoteList & notes() const
Definition: adhoc.h:300
virtual StanzaExtension * newInstance(const Tag *tag) const
Definition: adhoc.h:326
int actions() const
Definition: adhoc.h:294
virtual StanzaExtension * clone() const
Definition: adhoc.h:335
Status status() const
Definition: adhoc.h:281
std::list< const Note * > NoteList
Definition: adhoc.h:198
Action action() const
Definition: adhoc.h:287
This class implements a provider for XEP-0050 (Ad-hoc Commands).
Definition: adhoc.h:82
This is the common base class for a Jabber/XMPP Client and a Jabber Component.
Definition: clientbase.h:79
A virtual interface that enables objects to receive Service Discovery (XEP-0030) events.
Definition: discohandler.h:35
Derived classes can be registered as NodeHandlers for certain nodes with the Disco object.
An abstraction of a Disco Info element (from Service Discovery, XEP-0030) as a StanzaExtension.
Definition: disco.h:66
An abstraction of a Disco query element (from Service Discovery, XEP-0030) in the disco::items namesp...
Definition: disco.h:276
std::list< Identity * > IdentityList
Definition: disco.h:51
std::list< Item * > ItemList
Definition: disco.h:261
A stanza error abstraction implemented as a StanzaExtension.
Definition: error.h:35
An abstraction of an IQ stanza.
Definition: iq.h:34
A virtual interface which can be reimplemented to receive IQ stanzas.
Definition: iqhandler.h:32
An abstraction of a JID.
Definition: jid.h:31
This class abstracts a stanza extension, which is usually an XML child element in a specific namespac...
This is an abstraction of an XML element.
Definition: tag.h:47
The namespace for the gloox library.
Definition: adhoc.cpp:28
std::list< std::string > StringList
Definition: gloox.h:1251
std::map< std::string, std::string > StringMap
Definition: gloox.h:1261