gloox  1.0
adhoc.h
1 /*
2  Copyright (c) 2004-2009 by Jakob Schroeter <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 "dataform.h"
19 #include "disco.h"
20 #include "disconodehandler.h"
21 #include "discohandler.h"
22 #include "iqhandler.h"
23 #include "stanzaextension.h"
24 
25 #include <string>
26 #include <list>
27 #include <map>
28 
29 namespace gloox
30 {
31 
32  class ClientBase;
33  class Stanza;
34  class AdhocHandler;
35  class AdhocCommandProvider;
36 
80  class GLOOX_API Adhoc : public DiscoNodeHandler, public DiscoHandler, public IqHandler
81  {
82  public:
90  class GLOOX_API Command : public StanzaExtension
91  {
92  friend class Adhoc;
93 
94  public:
95 
99  enum Action
100  {
101  Execute = 1,
103  Cancel = 2,
104  Previous = 4,
106  Next = 8,
108  Complete = 16,
109  InvalidAction = 32
110  };
111 
115  enum Status
116  {
120  InvalidStatus
121  };
122 
129  class GLOOX_API Note
130  {
131 
132  friend class Command;
133 
134  public:
138  enum Severity
139  {
140  Info,
142  Warning,
146  InvalidSeverity
147  };
148 
154  Note( Severity sev, const std::string& note )
155  : m_severity( sev ), m_note( note ) {}
156 
160  ~Note() {}
161 
166  Severity severity() const { return m_severity; }
167 
172  const std::string& content() const { return m_note; }
173 
178  Tag* tag() const;
179 
180  private:
181 #ifdef ADHOC_COMMANDS_TEST
182  public:
183 #endif
184 
188  Note( const Tag* tag );
189 
190  Severity m_severity;
191  std::string m_note;
192  };
193 
197  typedef std::list<const Note*> NoteList;
198 
209  Command( const std::string& node, const std::string& sessionid, Action action,
210  DataForm* form = 0 );
211 
221  Command( const std::string& node, const std::string& sessionid, Status status,
222  DataForm* form = 0 );
223 
236  Command( const std::string& node, const std::string& sessionid, Status status,
237  Action executeAction, int allowedActions = Complete,
238  DataForm* form = 0 );
239 
249  Command( const std::string& node, Action action,
250  DataForm* form = 0 );
251 
256  Command( const Tag* tag = 0 );
257 
261  virtual ~Command();
262 
267  const std::string& node() const { return m_node; }
268 
273  const std::string& sessionID() const { return m_sessionid; }
274 
280  Status status() const { return m_status; }
281 
286  Action action() const { return m_action; }
287 
293  int actions() const { return m_actions; }
294 
299  const NoteList& notes() const { return m_notes; }
300 
306  void addNote( const Note* note ) { m_notes.push_back( note ); }
307 
312  const DataForm* form() const { return m_form; }
313 
314  // reimplemented from StanzaExtension
315  virtual const std::string& filterString() const;
316 
317  // reimplemented from StanzaExtension
318  virtual StanzaExtension* newInstance( const Tag* tag ) const
319  {
320  return new Command( tag );
321  }
322 
323  // reimplemented from StanzaExtension
324  virtual Tag* tag() const;
325 
326  // reimplemented from StanzaExtension
327  virtual StanzaExtension* clone() const
328  {
329  Command* c = new Command();
330 
331  NoteList::const_iterator it = m_notes.begin();
332  for( ; it != m_notes.end(); ++it )
333  c->m_notes.push_back( new Note( *(*it) ) );
334 
335  c->m_node = m_node;
336  c->m_sessionid = m_sessionid;
337  c->m_form = m_form ? static_cast<DataForm*>( m_form->clone() ) : 0;
338  c->m_action = m_action;
339  c->m_status = m_status;
340  c->m_actions = m_actions;
341 
342  return c;
343  }
344 
345  private:
346 #ifdef ADHOC_COMMANDS_TEST
347  public:
348 #endif
349  NoteList m_notes;
350 
351  std::string m_node;
352  std::string m_sessionid;
353  DataForm* m_form;
354  Action m_action;
355  Status m_status;
356  int m_actions;
357  };
358 
364  Adhoc( ClientBase* parent );
365 
369  virtual ~Adhoc();
370 
376  void checkSupport( const JID& remote, AdhocHandler* ah );
377 
384  void getCommands( const JID& remote, AdhocHandler* ah );
385 
396  void execute( const JID& remote, const Adhoc::Command* command, AdhocHandler* ah );
397 
410  void respond( const JID& remote, const Adhoc::Command* command, const Error* error = 0 );
411 
419  void registerAdhocCommandProvider( AdhocCommandProvider* acp, const std::string& command,
420  const std::string& name );
421 
427  void removeAdhocCommandProvider( const std::string& command );
428 
429  // reimplemented from DiscoNodeHandler
430  virtual StringList handleDiscoNodeFeatures( const JID& from, const std::string& node );
431 
432  // reimplemented from DiscoNodeHandler
433  virtual Disco::IdentityList handleDiscoNodeIdentities( const JID& from,
434  const std::string& node );
435 
436  // reimplemented from DiscoNodeHandler
437  virtual Disco::ItemList handleDiscoNodeItems( const JID& from, const JID& to, const std::string& node );
438 
439  // reimplemented from IqHandler
440  virtual bool handleIq( const IQ& iq );
441 
442  // reimplemented from IqHandler
443  virtual void handleIqID( const IQ& iq, int context );
444 
445  // reimplemented from DiscoHandler
446  virtual void handleDiscoInfo( const JID& from, const Disco::Info& info, int context );
447 
448  // reimplemented from DiscoHandler
449  virtual void handleDiscoItems( const JID& from, const Disco::Items& items, int context );
450 
451  // reimplemented from DiscoHandler
452  virtual void handleDiscoError( const JID& from, const Error* error, int context );
453 
454  private:
455 #ifdef ADHOC_TEST
456  public:
457 #endif
458  typedef std::map<const std::string, AdhocCommandProvider*> AdhocCommandProviderMap;
459  AdhocCommandProviderMap m_adhocCommandProviders;
460 
461  enum AdhocContext
462  {
463  CheckAdhocSupport,
464  FetchAdhocCommands,
465  ExecuteAdhocCommand
466  };
467 
468  struct TrackStruct
469  {
470  JID remote;
471  AdhocContext context;
472  std::string session;
473  AdhocHandler* ah;
474  };
475  typedef std::map<std::string, TrackStruct> AdhocTrackMap;
476  AdhocTrackMap m_adhocTrackMap;
477 
478  ClientBase* m_parent;
479 
480  StringMap m_items;
481  StringMap m_activeSessions;
482 
483  };
484 
485 }
486 
487 #endif // ADHOC_H__