gloox  1.0.28
lastactivity.cpp
1 /*
2  Copyright (c) 2005-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 #include "lastactivity.h"
16 #include "disco.h"
17 #include "discohandler.h"
18 #include "clientbase.h"
19 #include "error.h"
20 #include "lastactivityhandler.h"
21 
22 #include <cstdlib>
23 
24 namespace gloox
25 {
26 
27  // ---- LastActivity::Query ----
29  : StanzaExtension( ExtLastActivity ), m_seconds( -1 )
30  {
31  if( !tag || tag->name() != "query" || tag->xmlns() != XMLNS_LAST )
32  return;
33 
34  if( tag->hasAttribute( "seconds" ) )
35  m_seconds = atoi( tag->findAttribute( "seconds" ).c_str() );
36 
37  m_status = tag->cdata();
38  }
39 
40  LastActivity::Query::Query( const std::string& _status, long _seconds )
41  : StanzaExtension( ExtLastActivity ), m_seconds( _seconds ),
42  m_status( _status )
43  {
44  }
45 
47  {
48  }
49 
50  const std::string& LastActivity::Query::filterString() const
51  {
52  static const std::string filter = "/iq/query[@xmlns='" + XMLNS_LAST + "']"
53  "|/presence/query[@xmlns='" + XMLNS_LAST + "']";
54  return filter;
55  }
56 
58  {
59  Tag* t = new Tag( "query" );
60  t->setXmlns( XMLNS_LAST );
61  t->addAttribute( "seconds", m_seconds );
62  t->setCData( m_status );
63  return t;
64  }
65  // ---- ~LastActivity::Query ----
66 
67  // ---- LastActivity ----
69  : m_lastActivityHandler( 0 ), m_parent( parent ),
70  m_active( time ( 0 ) )
71  {
72  if( m_parent )
73  {
74  m_parent->registerStanzaExtension( new Query() );
75  m_parent->registerIqHandler( this, ExtLastActivity );
76  m_parent->disco()->addFeature( XMLNS_LAST );
77  }
78  }
79 
81  {
82  if( m_parent )
83  {
84  m_parent->disco()->removeFeature( XMLNS_LAST );
85  m_parent->removeIqHandler( this, ExtLastActivity );
86  m_parent->removeIDHandler( this );
87  }
88  }
89 
90  void LastActivity::query( const JID& jid )
91  {
92  IQ iq( IQ::Get, jid, m_parent->getID() );
93  iq.addExtension( new Query() );
94  m_parent->send( iq, this, 0 );
95  }
96 
97  bool LastActivity::handleIq( const IQ& iq )
98  {
99  const Query* q = iq.findExtension<Query>( ExtLastActivity );
100  if( !q || iq.subtype() != IQ::Get )
101  return false;
102 
103  IQ re( IQ::Result, iq.from(), iq.id() );
104  re.addExtension( new Query( EmptyString, static_cast<long>( time( 0 ) - m_active ) ) );
105  m_parent->send( re );
106 
107  return true;
108  }
109 
110  void LastActivity::handleIqID( const IQ& iq, int /*context*/ )
111  {
112  if( !m_lastActivityHandler )
113  return;
114 
115  if( iq.subtype() == IQ::Result )
116  {
117  const Query* q = iq.findExtension<Query>( ExtLastActivity );
118  if( !q || q->seconds() < 0 )
119  return;
120 
121  m_lastActivityHandler->handleLastActivityResult( iq.from(), q->seconds(), q->status() );
122  }
123  else if( iq.subtype() == IQ::Error && iq.error() )
124  m_lastActivityHandler->handleLastActivityError( iq.from(), iq.error()->error() );
125  }
126 
128  {
129  m_active = time( 0 );
130  }
131 
132 }
This is the common base class for a Jabber/XMPP Client and a Jabber Component.
Definition: clientbase.h:79
const std::string getID()
virtual Disco * disco() const
Definition: clientbase.h:233
void removeIqHandler(IqHandler *ih, int exttype)
void removeIDHandler(IqHandler *ih)
void send(Tag *tag)
void registerIqHandler(IqHandler *ih, int exttype)
void registerStanzaExtension(StanzaExtension *ext)
void removeFeature(const std::string &feature)
Definition: disco.h:430
void addFeature(const std::string &feature)
Definition: disco.h:422
StanzaError error() const
Definition: error.h:75
An abstraction of an IQ stanza.
Definition: iq.h:34
IqType subtype() const
Definition: iq.h:74
@ Error
Definition: iq.h:49
@ Result
Definition: iq.h:48
@ Get
Definition: iq.h:45
An abstraction of a JID.
Definition: jid.h:31
virtual void handleLastActivityResult(const JID &jid, long seconds, const std::string &status)=0
virtual void handleLastActivityError(const JID &jid, StanzaError error)=0
This is an abstraction of a LastActivity Query that can be used in XEP-0012 as well as XEP-0256.
Definition: lastactivity.h:53
Query(const Tag *tag=0)
virtual const std::string & filterString() const
const std::string & status() const
Definition: lastactivity.h:85
virtual Tag * tag() const
virtual void handleIqID(const IQ &iq, int context)
virtual bool handleIq(const IQ &iq)
LastActivity(ClientBase *parent)
void query(const JID &jid)
This class abstracts a stanza extension, which is usually an XML child element in a specific namespac...
void addExtension(const StanzaExtension *se)
Definition: stanza.cpp:52
const Error * error() const
Definition: stanza.cpp:47
const std::string & id() const
Definition: stanza.h:63
const JID & from() const
Definition: stanza.h:51
const StanzaExtension * findExtension(int type) const
Definition: stanza.cpp:57
This is an abstraction of an XML element.
Definition: tag.h:47
bool setCData(const std::string &cdata)
Definition: tag.cpp:447
const std::string xmlns() const
Definition: tag.cpp:543
bool addAttribute(Attribute *attr)
Definition: tag.cpp:354
bool hasAttribute(const std::string &name, const std::string &value=EmptyString) const
Definition: tag.cpp:602
const std::string cdata() const
Definition: tag.cpp:497
const std::string & findAttribute(const std::string &name) const
Definition: tag.cpp:589
const std::string & name() const
Definition: tag.h:394
bool setXmlns(const std::string &xmlns, const std::string &prefix=EmptyString)
Definition: tag.cpp:522
The namespace for the gloox library.
Definition: adhoc.cpp:28
const std::string XMLNS_LAST
Definition: gloox.cpp:45
const std::string EmptyString
Definition: gloox.cpp:124
@ ExtLastActivity