PTLib  Version 2.10.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
xmpp.h
Go to the documentation of this file.
1 /*
2  * xmpp.h
3  *
4  * Extensible Messaging and Presence Protocol (XMPP) Core
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 2004 Reitek S.p.A.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Post Increment
23  *
24  * Contributor(s): ______________________________________.
25  *
26  * $Revision: 25387 $
27  * $Author: rjongbloed $
28  * $Date: 2011-03-22 22:51:09 -0500 (Tue, 22 Mar 2011) $
29  */
30 
31 #ifndef PTLIB_XMPP_H
32 #define PTLIB_XMPP_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 #include <ptlib.h>
39 
40 #if P_EXPAT
41 
42 #include <ptclib/pxml.h>
43 #include <ptlib/notifier_ext.h>
44 
45 
47 
48 namespace XMPP
49 {
52  extern const PCaselessString & LanguageTag();
53  extern const PCaselessString & NamespaceTag();
54  extern const PCaselessString & MessageStanzaTag();
55  extern const PCaselessString & PresenceStanzaTag();
56  extern const PCaselessString & IQStanzaTag();
57  extern const PCaselessString & IQQueryTag();
58 
59  class JID : public PObject
60  {
61  PCLASSINFO(JID, PObject);
62 
63  public:
64  JID(const char * jid = 0);
65  JID(const PString& jid);
66  JID(const PString& user, const PString& server, const PString& resource = PString::Empty());
67 
68  virtual Comparison Compare(
69  const PObject & obj
70  ) const;
71 
72  JID& operator=(
73  const PString & jid
74  );
75 
76  operator const PString&() const;
77 
78  virtual PObject * Clone() const { return new JID(m_JID); }
79 
80  PString GetUser() const { return m_User; }
81  PString GetServer() const { return m_Server; }
82 
83  virtual PString GetResource() const { return m_Resource; }
84 
85  virtual void SetUser(const PString& user);
86  virtual void SetServer(const PString& server);
87  virtual void SetResource(const PString& resource);
88 
89  virtual PBoolean IsBare() const { return m_Resource.IsEmpty(); }
90  virtual void PrintOn(ostream & strm) const;
91 
92  protected:
93  virtual void ParseJID(const PString& jid);
94  virtual void BuildJID() const;
95 
99 
100  mutable PString m_JID;
102  };
103 
104  // A bare jid is a jid with no resource
105  class BareJID : public JID
106  {
107  PCLASSINFO(BareJID, JID);
108 
109  public:
110  BareJID(const char * jid = 0) : JID(jid) { }
111  BareJID(const PString& jid) : JID(jid) { }
112  BareJID(const PString& user, const PString& server, const PString& resource = PString::Empty())
113  : JID(user, server, resource) { }
114 
115  virtual Comparison Compare(
116  const PObject & obj
117  ) const;
118 
120  const PString & jid
121  );
122 
123  virtual PObject * Clone() const { return new BareJID(m_JID); }
124  virtual PString GetResource() const { return PString::Empty(); }
125  virtual void SetResource(const PString&) { }
126  virtual PBoolean IsBare() const { return true; }
127  };
128 
135  {
136  PCLASSINFO(Transport, PIndirectChannel);
137 
138  public:
139  virtual PBoolean Open() = 0;
140  virtual PBoolean Close() = 0;
141  };
142 
143 
147  class Stream : public PIndirectChannel
148  {
149  PCLASSINFO(Stream, PIndirectChannel);
150 
151  public:
152  Stream(Transport * transport = 0);
153  ~Stream();
154 
155  virtual PBoolean OnOpen() { return m_OpenHandlers.Fire(*this); }
157 
158  virtual PBoolean Close();
159  virtual void OnClose() { m_CloseHandlers.Fire(*this); }
161 
162  virtual PBoolean Write(const void * buf, PINDEX len);
163  virtual PBoolean Write(const PString& data);
164  virtual PBoolean Write(const PXML& pdu);
165 
168  virtual PXML * Read();
169 
173  virtual void Reset();
175 
176  protected:
180  };
181 
182 
183  class BaseStreamHandler : public PThread
184  {
185  PCLASSINFO(BaseStreamHandler, PThread);
186 
187  public:
190 
191  virtual PBoolean Start(Transport * transport = 0);
192  virtual PBoolean Stop(const PString& error = PString::Empty());
193 
194  void SetAutoReconnect(PBoolean b = true, long timeout = 1000);
195 
197  Stream * GetStream() { return m_Stream; }
198 
199  virtual PBoolean Write(const void * buf, PINDEX len);
200  virtual PBoolean Write(const PString& data);
201  virtual PBoolean Write(const PXML& pdu);
202  virtual void OnElement(PXML& pdu);
203 
204  virtual void Main();
205 
206  protected:
209 
213 
215  };
216 
217 
222  class Stanza : public PXML
223  {
225 
226  public:
229  static const PCaselessString & IDTag();
230  static const PCaselessString & FromTag();
231  static const PCaselessString & ToTag();
232 
233  virtual PBoolean IsValid() const = 0;
234 
235  virtual PString GetID() const;
236  virtual PString GetFrom() const;
237  virtual PString GetTo() const;
238 
239  virtual void SetID(const PString& id);
240  virtual void SetFrom(const PString& from);
241  virtual void SetTo(const PString& to);
242 
243  virtual PXMLElement * GetElement(const PString& name, PINDEX i = 0);
244  virtual void AddElement(PXMLElement * elem);
245 
246  static PString GenerateID();
247  };
248 
249  PLIST(StanzaList, Stanza);
250 
251 
252  class Message : public Stanza
253  {
255 
256  public:
257  enum MessageType {
263  Unknown = 999
264  };
265 
268  static const PCaselessString & TypeTag();
269  static const PCaselessString & SubjectTag();
270  static const PCaselessString & BodyTag();
271  static const PCaselessString & ThreadTag();
272 
275  Message();
276 
281  Message(PXML& pdu);
282  Message(PXML * pdu);
283 
284  virtual PBoolean IsValid() const;
285  static PBoolean IsValid(const PXML * pdu);
286 
287  virtual MessageType GetType(PString * typeName = 0) const;
288  virtual PString GetLanguage() const;
289 
294  virtual PString GetSubject(const PString& lang = PString::Empty());
295  virtual PString GetBody(const PString& lang = PString::Empty());
296  virtual PString GetThread();
297 
298  virtual PXMLElement * GetSubjectElement(const PString& lang = PString::Empty());
299  virtual PXMLElement * GetBodyElement(const PString& lang = PString::Empty());
300 
301  virtual void SetType(MessageType type);
302  virtual void SetType(const PString& type); // custom, possibly non standard, type
303  virtual void SetLanguage(const PString& lang);
304 
305  virtual void SetSubject(const PString& subj, const PString& lang = PString::Empty());
306  virtual void SetBody(const PString& body, const PString& lang = PString::Empty());
307  virtual void SetThread(const PString& thrd);
308  };
309 
310 
311  class Presence : public Stanza
312  {
314 
315  public:
325  Unknown = 999
326  };
327 
328  enum ShowType {
333  XA,
334  Other = 999
335  };
336 
339  static const PCaselessString & TypeTag();
340  static const PCaselessString & ShowTag();
341  static const PCaselessString & StatusTag();
342  static const PCaselessString & PriorityTag();
343 
346  Presence();
347 
352  Presence(PXML& pdu);
353  Presence(PXML * pdu);
354 
355  virtual PBoolean IsValid() const;
356  static PBoolean IsValid(const PXML * pdu);
357 
358  virtual PresenceType GetType(PString * typeName = 0) const;
359  virtual ShowType GetShow(PString * showName = 0) const;
360  virtual BYTE GetPriority() const;
361 
366  virtual PString GetStatus(const PString& lang = PString::Empty());
367  virtual PXMLElement * GetStatusElement(const PString& lang = PString::Empty());
368 
369  virtual void SetType(PresenceType type);
370  virtual void SetType(const PString& type); // custom, possibly non standard, type
371  virtual void SetShow(ShowType show);
372  virtual void SetShow(const PString& show); // custom, possibly non standard, type
373  virtual void SetPriority(BYTE priority);
374 
375  virtual void SetStatus(const PString& status, const PString& lang = PString::Empty());
376  };
377 
378 
379  class IQ : public Stanza
380  {
382 
383  public:
384  enum IQType {
389  Unknown = 999
390  };
391 
394  static const PCaselessString & TypeTag();
395 
396  IQ(IQType type, PXMLElement * body = 0);
397  IQ(PXML& pdu);
398  IQ(PXML * pdu);
399  ~IQ();
400 
401  virtual PBoolean IsValid() const;
402  static PBoolean IsValid(const PXML * pdu);
403 
409  void SetProcessed() { m_Processed = true; }
411 
412  virtual IQType GetType(PString * typeName = 0) const;
413  virtual PXMLElement * GetBody();
414 
415  virtual void SetType(IQType type);
416  virtual void SetType(const PString& type); // custom, possibly non standard, type
417  virtual void SetBody(PXMLElement * body);
418 
419  // If the this message is response, returns a pointer to the
420  // original set/get message
421  virtual IQ * GetOriginalMessage() const { return m_OriginalIQ; }
422  virtual void SetOriginalMessage(IQ * iq);
423 
427  virtual IQ * BuildResult() const;
428 
431  virtual IQ * BuildError(const PString& type, const PString& code) const;
432 
434 
435  protected:
439  };
442  namespace Disco
443  {
444  class Item : public PObject
445  {
446  PCLASSINFO(Item, PObject);
447  public:
448  Item(PXMLElement * item);
449  Item(const PString& jid, const PString& node = PString::Empty());
450 
451  const JID& GetJID() const { return m_JID; }
452  const PString& GetNode() const { return m_Node; }
453 
454  PXMLElement * AsXML(PXMLElement * parent) const;
455 
456  protected:
457  const JID m_JID;
459  };
460 
462  public:
463  ItemList(PXMLElement * list);
464  PXMLElement * AsXML(PXMLElement * parent) const;
465  };
466 
467  class Identity : public PObject
468  {
469  PCLASSINFO(Identity, PObject);
470  public:
471  Identity(PXMLElement * identity);
472  Identity(const PString& category, const PString& type, const PString& name);
473 
474  const PString& GetCategory() const { return m_Category; }
475  const PString& GetType() const { return m_Type; }
476  const PString& GetName() const { return m_Name; }
477 
478  PXMLElement * AsXML(PXMLElement * parent) const;
479 
480  protected:
484  };
485 
487  public:
488  IdentityList(PXMLElement * list);
489  PXMLElement * AsXML(PXMLElement * parent) const;
490  };
491 
492  class Info : public PObject
493  {
494  PCLASSINFO(Info, PObject);
495  public:
496  Info(PXMLElement * info);
497 
498  IdentityList& GetIdentities() { return m_Identities; }
499  PStringSet& GetFeatures() { return m_Features; }
500 
501  PXMLElement * AsXML(PXMLElement * parent) const;
502 
503  protected:
506  };
507  } // namespace Disco
508 
509 }; // class XMPP
510 
511 
512 #endif // P_EXPAT
513 
514 #endif // PTLIB_XMPP_H
515 
516 // End of File ///////////////////////////////////////////////////////////////
static const PCaselessString & PriorityTag()
Definition: xmpp.h:389
PNotifierList & ElementHandlers()
Definition: xmpp.h:196
virtual void SetUser(const PString &user)
JID(const char *jid=0)
virtual void SetType(IQType type)
virtual PBoolean Close()=0
Close the channel.
void SetAutoReconnect(PBoolean b=true, long timeout=1000)
static const PCaselessString & SubjectTag()
BareJID(const char *jid=0)
Definition: xmpp.h:110
static const PCaselessString & TypeTag()
Various constant strings.
virtual void SetStatus(const PString &status, const PString &lang=PString::Empty())
Definition: xmpp.h:262
PNotifierList m_OpenHandlers
Definition: xmpp.h:178
const PCaselessString & IQQueryTag()
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:55
virtual void SetType(MessageType type)
Definition: xmpp.h:321
Definition: pxml.h:515
const PString m_Category
Definition: xmpp.h:481
Definition: xmpp.h:333
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
PString m_User
Definition: xmpp.h:96
virtual MessageType GetType(PString *typeName=0) const
resource
Definition: xmpp.h:113
Definition: xmpp.h:263
virtual IQ * BuildResult() const
Creates a new response iq for this message (that must be of the set/get type!)
Stream * GetStream()
Definition: xmpp.h:197
const PCaselessString & NamespaceTag()
static const PCaselessString & ToTag()
static const PCaselessString & ThreadTag()
virtual void SetThread(const PString &thrd)
const PString m_Type
Definition: xmpp.h:482
This class represents a XMPP stream, i.e.
Definition: xmpp.h:147
Stream * m_Stream
Definition: xmpp.h:210
BareJID(const PString &jid)
Definition: xmpp.h:111
virtual void SetBody(const PString &body, const PString &lang=PString::Empty())
virtual void SetShow(ShowType show)
PStringSet m_Features
Definition: xmpp.h:505
virtual PString GetSubject(const PString &lang=PString::Empty())
Get the subject for the specified language.
virtual PBoolean Stop(const PString &error=PString::Empty())
ShowType
Definition: xmpp.h:328
virtual PString GetResource() const
Definition: xmpp.h:124
virtual BYTE GetPriority() const
Definition: xmpp.h:317
virtual PBoolean Write(const void *buf, PINDEX len)
virtual void SetType(PresenceType type)
static const PCaselessString & IDTag()
Various constant strings.
PDECLARE_NOTIFIER(Stream, BaseStreamHandler, OnOpen)
ItemList(PXMLElement *list)
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:1184
Definition: xmpp.h:379
virtual void SetServer(const PString &server)
static const PCaselessString & ShowTag()
virtual PBoolean Close()
Close the channel.
virtual void ParseJID(const PString &jid)
This is a channel that operates indirectly through another channel(s).
Definition: indchan.h:49
This class is a variation of a string that ignores case.
Definition: pstring.h:1708
virtual PString GetID() const
Definition: xmpp.h:388
Definition: xmpp.h:467
virtual PBoolean OnOpen()
This callback is executed when the Open() function is called with open channels.
Definition: xmpp.h:155
const JID & GetJID() const
Definition: xmpp.h:451
virtual void Main()
User override function for the main execution routine of the thread.
virtual PXMLElement * GetStatusElement(const PString &lang=PString::Empty())
Definition: xmpp.h:261
virtual ShowType GetShow(PString *showName=0) const
virtual void AddElement(PXMLElement *elem)
virtual PString GetTo() const
PXMLStreamParser * m_Parser
Definition: xmpp.h:177
virtual PBoolean IsValid() const
Definition: xmpp.h:323
virtual PString GetStatus(const PString &lang=PString::Empty())
Get the status for the specified language.
BareJID & operator=(const PString &jid)
Definition: xmpp.h:324
PBoolean m_AutoReconnect
Definition: xmpp.h:211
Message()
Construct a new empty message.
PBoolean m_IsDirty
Definition: xmpp.h:101
virtual PresenceType GetType(PString *typeName=0) const
Definition: xmpp.h:258
PNotifierList & CloseHandlers()
Definition: xmpp.h:160
static const PCaselessString & FromTag()
Definition: xmpp.h:334
PString GetServer() const
Definition: xmpp.h:81
Definition: xmpp.h:59
virtual PBoolean IsEmpty() const
Determine if the string is empty.
virtual PBoolean Write(const void *buf, PINDEX len)
Low level write to the channel.
Definition: pxml.h:114
virtual IQ * GetOriginalMessage() const
Definition: xmpp.h:421
Definition: xmpp.h:320
BOOL PBoolean
Definition: object.h:102
IQType
Definition: xmpp.h:384
const PCaselessString & PresenceStanzaTag()
IQ(IQType type, PXMLElement *body=0)
static const PCaselessString & StatusTag()
virtual PString GetFrom() const
const PString & GetType() const
Definition: xmpp.h:475
virtual void BuildJID() const
virtual PString GetLanguage() const
PNotifierList m_CloseHandlers
Definition: xmpp.h:179
const PString m_Node
Definition: xmpp.h:458
virtual void SetPriority(BYTE priority)
virtual PXMLElement * GetBody()
virtual void SetLanguage(const PString &lang)
IdentityList & GetIdentities()
Definition: xmpp.h:498
Presence()
Construct a new empty presence.
virtual void SetBody(PXMLElement *body)
virtual void SetID(const PString &id)
virtual PString GetBody(const PString &lang=PString::Empty())
PXMLElement * AsXML(PXMLElement *parent) const
virtual PXMLElement * GetElement(const PString &name, PINDEX i=0)
virtual void OnElement(PXML &pdu)
static const PCaselessString & TypeTag()
Various constant strings.
virtual PXML * Read()
Read a XMPP stanza from the stream.
virtual void SetResource(const PString &)
Definition: xmpp.h:125
Definition: xmpp.h:444
virtual void OnClose()
Definition: xmpp.h:159
const PCaselessString & LanguageTag()
Various constant strings.
Definition: xmpp.h:322
IdentityList m_Identities
Definition: xmpp.h:504
Definition: xmpp.h:332
virtual void SetResource(const PString &resource)
PString m_Server
Definition: xmpp.h:97
static const PCaselessString & BodyTag()
PXMLElement * AsXML(PXMLElement *parent) const
Definition: xmpp.h:329
The character string class.
Definition: pstring.h:108
Definition: xmpp.h:105
PXMLStreamParser * GetParser()
Definition: xmpp.h:174
virtual void Reset()
Reset the parser.
virtual PObject * Clone() const
Create a copy of the class on the heap.
Definition: xmpp.h:78
PString GetUser() const
Definition: xmpp.h:80
This is a set collection class of PString objects.
Definition: pstring.h:2391
Definition: xmpp.h:311
virtual PXMLElement * GetBodyElement(const PString &lang=PString::Empty())
Definition: xmpp.h:385
Definition: xmpp.h:259
Definition: pxml.h:353
static PString Empty()
Return an empty string.
virtual PBoolean IsValid() const
static PString GenerateID()
PNotifierList m_ElementHandlers
Definition: xmpp.h:214
virtual PString GetResource() const
Definition: xmpp.h:83
Definition: xmpp.h:325
Definition: xmpp.h:260
This class defines a thread of execution in the system.
Definition: thread.h:66
Definition: xmpp.h:386
Definition: xmpp.h:331
Definition: xmpp.h:319
PBoolean HasBeenProcessed() const
Definition: xmpp.h:410
const PString m_Name
Definition: xmpp.h:483
virtual IQ * BuildError(const PString &type, const PString &code) const
Creates an error response for this message.
virtual PNotifierList GetResponseHandlers()
Definition: xmpp.h:433
JID & operator=(const PString &jid)
Definition: xmpp.h:330
virtual Comparison Compare(const PObject &obj) const
Compare the two objects and return their relative rank.
XMPP stanzas: the following classes represent the three stanzas (PDUs) defined by the xmpp protocol...
Definition: xmpp.h:222
virtual IQType GetType(PString *typeName=0) const
virtual PBoolean IsBare() const
Definition: xmpp.h:89
This interface is the base class of each XMPP transport class.
Definition: xmpp.h:134
virtual PObject * Clone() const
Create a copy of the class on the heap.
Definition: xmpp.h:123
server
Definition: xmpp.h:113
const PCaselessString & IQStanzaTag()
const PString & GetName() const
Definition: xmpp.h:476
Stream(Transport *transport=0)
virtual void SetSubject(const PString &subj, const PString &lang=PString::Empty())
PNotifierList m_ResponseHandlers
Definition: xmpp.h:438
PNotifierList & OpenHandlers()
Definition: xmpp.h:156
#define PDECLARE_LIST(cls, T)
Begin declaration of list class.
Definition: lists.h:455
PStringSet & GetFeatures()
Definition: xmpp.h:499
static const PCaselessString & TypeTag()
Various constant strings.
PLIST(StanzaList, Stanza)
PTimeInterval m_ReconnectTimeout
Definition: xmpp.h:212
virtual PBoolean IsBare() const
Definition: xmpp.h:126
Definition: xmpp.h:183
IQ * m_OriginalIQ
Definition: xmpp.h:437
virtual PBoolean IsValid() const
Definition: xmpp.h:387
void SetProcessed()
This method signals that the message was taken care of If the stream handler, after firing all the n...
Definition: xmpp.h:409
MessageType
Definition: xmpp.h:257
const JID m_JID
Definition: xmpp.h:457
virtual void SetTo(const PString &to)
const PCaselessString & MessageStanzaTag()
Definition: notifier_ext.h:102
const PString & GetNode() const
Definition: xmpp.h:452
Definition: xmpp.h:252
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
PString m_Resource
Definition: xmpp.h:98
virtual PBoolean IsValid() const =0
PresenceType
Definition: xmpp.h:316
PBoolean Fire(PObject &obj, INT val=0)
PString m_JID
Definition: xmpp.h:100
virtual void PrintOn(ostream &strm) const
Output the contents of the object to the stream.
Item(PXMLElement *item)
PBoolean m_Processed
Definition: xmpp.h:436
Definition: xmpp.h:318
virtual PBoolean Open()=0
const PString & GetCategory() const
Definition: xmpp.h:474
virtual PString GetThread()
IdentityList(PXMLElement *list)
Definition: xmpp.h:492
virtual void SetFrom(const PString &from)
virtual PBoolean Start(Transport *transport=0)
virtual void SetOriginalMessage(IQ *iq)
virtual PXMLElement * GetSubjectElement(const PString &lang=PString::Empty())