gloox  1.0.1
adhoc.h
1 /*
2  Copyright (c) 2004-2012 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 #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,
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
185 
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  DataForm* form = 0 );
212 
222  Command( const std::string& node, const std::string& sessionid, Status status,
223  DataForm* form = 0 );
224 
237  Command( const std::string& node, const std::string& sessionid, Status status,
238  Action executeAction, int allowedActions = Complete,
239  DataForm* form = 0 );
240 
250  Command( const std::string& node, Action action,
251  DataForm* form = 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 
313  const DataForm* form() const { return m_form; }
314 
315  // reimplemented from StanzaExtension
316  virtual const std::string& filterString() const;
317 
318  // reimplemented from StanzaExtension
319  virtual StanzaExtension* newInstance( const Tag* tag ) const
320  {
321  return new Command( tag );
322  }
323 
324  // reimplemented from StanzaExtension
325  virtual Tag* tag() const;
326 
327  // reimplemented from StanzaExtension
328  virtual StanzaExtension* clone() const
329  {
330  Command* c = new Command();
331 
332  NoteList::const_iterator it = m_notes.begin();
333  for( ; it != m_notes.end(); ++it )
334  c->m_notes.push_back( new Note( *(*it) ) );
335 
336  c->m_node = m_node;
337  c->m_sessionid = m_sessionid;
338  c->m_form = m_form ? static_cast<DataForm*>( m_form->clone() ) : 0;
339  c->m_action = m_action;
340  c->m_status = m_status;
341  c->m_actions = m_actions;
342 
343  return c;
344  }
345 
346  private:
347 #ifdef ADHOC_COMMANDS_TEST
348  public:
349 #endif
350  NoteList m_notes;
351 
352  std::string m_node;
353  std::string m_sessionid;
354  DataForm* m_form;
355  Action m_action;
356  Status m_status;
357  int m_actions;
358  };
359 
365  Adhoc( ClientBase* parent );
366 
370  virtual ~Adhoc();
371 
378  void checkSupport( const JID& remote, AdhocHandler* ah, int context = 0 );
379 
387  void getCommands( const JID& remote, AdhocHandler* ah, int context = 0 );
388 
400  void execute( const JID& remote, const Adhoc::Command* command, AdhocHandler* ah, int context = 0 );
401 
414  void respond( const JID& remote, const Adhoc::Command* command, const Error* error = 0 );
415 
423  void registerAdhocCommandProvider( AdhocCommandProvider* acp, const std::string& command,
424  const std::string& name );
425 
431  void removeAdhocCommandProvider( const std::string& command );
432 
433  // reimplemented from DiscoNodeHandler
434  virtual StringList handleDiscoNodeFeatures( const JID& from, const std::string& node );
435 
436  // reimplemented from DiscoNodeHandler
437  virtual Disco::IdentityList handleDiscoNodeIdentities( const JID& from,
438  const std::string& node );
439 
440  // reimplemented from DiscoNodeHandler
441  virtual Disco::ItemList handleDiscoNodeItems( const JID& from, const JID& to, const std::string& node );
442 
443  // reimplemented from IqHandler
444  virtual bool handleIq( const IQ& iq );
445 
446  // reimplemented from IqHandler
447  virtual void handleIqID( const IQ& iq, int context );
448 
449  // reimplemented from DiscoHandler
450  virtual void handleDiscoInfo( const JID& from, const Disco::Info& info, int context );
451 
452  // reimplemented from DiscoHandler
453  virtual void handleDiscoItems( const JID& from, const Disco::Items& items, int context );
454 
455  // reimplemented from DiscoHandler
456  virtual void handleDiscoError( const JID& from, const Error* error, int context );
457 
458  private:
459 #ifdef ADHOC_TEST
460  public:
461 #endif
462  typedef std::map<const std::string, AdhocCommandProvider*> AdhocCommandProviderMap;
463  AdhocCommandProviderMap m_adhocCommandProviders;
464 
465  enum AdhocContext
466  {
467  CheckAdhocSupport,
468  FetchAdhocCommands,
469  ExecuteAdhocCommand
470  };
471 
472  struct TrackStruct
473  {
474  JID remote;
475  AdhocContext context;
476  std::string session;
477  AdhocHandler* ah;
478  int handlerContext;
479  };
480  typedef std::map<std::string, TrackStruct> AdhocTrackMap;
481  AdhocTrackMap m_adhocTrackMap;
482  util::Mutex m_adhocTrackMapMutex;
483 
484  ClientBase* m_parent;
485 
486  StringMap m_items;
487  StringMap m_activeSessions;
488 
489  };
490 
491 }
492 
493 #endif // ADHOC_H__