PTLib  Version 2.18.8
 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 
27 #ifndef PTLIB_XMPP_H
28 #define PTLIB_XMPP_H
29 
30 #ifdef P_USE_PRAGMA
31 #pragma interface
32 #endif
33 
34 #include <ptlib.h>
35 
36 #if P_EXPAT
37 
38 #include <ptclib/pxml.h>
39 #include <ptlib/notifier_ext.h>
40 
41 
43 
44 namespace XMPP
45 {
48  extern const PCaselessString & LanguageTag();
49  extern const PCaselessString & NamespaceTag();
50  extern const PCaselessString & MessageStanzaTag();
51  extern const PCaselessString & PresenceStanzaTag();
52  extern const PCaselessString & IQStanzaTag();
53  extern const PCaselessString & IQQueryTag();
54 
55  class JID : public PObject
56  {
57  PCLASSINFO(JID, PObject);
58 
59  public:
60  JID(const char * jid = 0);
61  JID(const PString& jid);
62  JID(const PString& user, const PString& server, const PString& resource = PString::Empty());
63 
64  virtual Comparison Compare(
65  const PObject & obj
66  ) const;
67 
68  JID& operator=(
69  const PString & jid
70  );
71 
72  operator const PString&() const;
73 
74  virtual PObject * Clone() const { return new JID(m_JID); }
75 
76  PString GetUser() const { return m_User; }
77  PString GetServer() const { return m_Server; }
78 
79  virtual PString GetResource() const { return m_Resource; }
80 
81  virtual void SetUser(const PString& user);
82  virtual void SetServer(const PString& server);
83  virtual void SetResource(const PString& resource);
84 
85  virtual PBoolean IsBare() const { return m_Resource.IsEmpty(); }
86  virtual void PrintOn(ostream & strm) const;
87 
88  protected:
89  virtual void ParseJID(const PString& jid);
90  virtual void BuildJID() const;
91 
95 
96  mutable PString m_JID;
98  };
99 
100  // A bare jid is a jid with no resource
101  class BareJID : public JID
102  {
103  PCLASSINFO(BareJID, JID);
104 
105  public:
106  BareJID(const char * jid = 0) : JID(jid) { }
107  BareJID(const PString& jid) : JID(jid) { }
108  BareJID(const PString& user, const PString& server, const PString& resource = PString::Empty())
109  : JID(user, server, resource) { }
110 
111  virtual Comparison Compare(
112  const PObject & obj
113  ) const;
114 
116  const PString & jid
117  );
118 
119  virtual PObject * Clone() const { return new BareJID(m_JID); }
120  virtual PString GetResource() const { return PString::Empty(); }
121  virtual void SetResource(const PString&) { }
122  virtual PBoolean IsBare() const { return true; }
123  };
124 
131  {
132  PCLASSINFO(Transport, PIndirectChannel);
133 
134  public:
135  virtual PBoolean Open() = 0;
136  virtual PBoolean Close() = 0;
137  };
138 
139 
143  class Stream : public PIndirectChannel
144  {
145  PCLASSINFO(Stream, PIndirectChannel);
146 
147  public:
148  Stream(Transport * transport = 0);
149  ~Stream();
150 
151  virtual bool OnOpen() { m_OpenHandlers(*this, 0); return true; }
153 
154  virtual PBoolean Close();
155  virtual void OnClose() { m_CloseHandlers(*this, 0); }
157 
158  virtual PBoolean Write(const void * buf, PINDEX len);
159  virtual PBoolean Write(const PString& data);
160 // virtual PBoolean Write(const PXMLElement & pdu);
161  virtual PBoolean Write(const PXML & pdu);
162 
165  virtual PXMLElement * Read();
166 
170  virtual void Reset();
172 
173  protected:
178  };
179 
180 
181  class BaseStreamHandler : public PThread
182  {
183  PCLASSINFO(BaseStreamHandler, PThread);
184 
185  public:
188 
189  virtual PBoolean Start(Transport * transport = 0);
190  virtual PBoolean Stop(const PString& error = PString::Empty());
191 
192  void SetAutoReconnect(PBoolean b = true, long timeout = 1000);
193 
194  void AddNotifier(const PNotifierTemplate<PXMLElement&> & notifier) { m_ElementHandlers.Add(notifier); }
195 
196  Stream * GetStream() const { return m_Stream; }
197 
198  virtual PBoolean Write(const void * buf, PINDEX len);
199  virtual PBoolean Write(const PString& data);
200  virtual PBoolean Write(const PXML & pdu);
201  virtual void OnElement(PXMLElement& pdu);
202 
203  virtual void Main();
204 
205  protected:
208 
212 
214  };
215 
216 
221  class Stanza : public PXML
222  {
224 
225  public:
228  static const PCaselessString & IDTag();
229  static const PCaselessString & FromTag();
230  static const PCaselessString & ToTag();
231 
232  virtual PBoolean IsValid() const = 0;
233 
234  virtual PString GetID() const;
235  virtual PString GetFrom() const;
236  virtual PString GetTo() const;
237 
238  virtual void SetID(const PString& id);
239  virtual void SetFrom(const PString& from);
240  virtual void SetTo(const PString& to);
241 
242  virtual PXMLElement * GetElement(const PString& name, PINDEX i = 0);
243 
244  static PString GenerateID();
245  };
246 
247  PLIST(StanzaList, Stanza);
248 
249 
250  class Message : public Stanza
251  {
253 
254  public:
255  enum MessageType {
261  Unknown = 999
262  };
263 
266  static const PCaselessString & TypeTag();
267  static const PCaselessString & SubjectTag();
268  static const PCaselessString & BodyTag();
269  static const PCaselessString & ThreadTag();
270 
273  Message();
274 
279  Message(PXMLElement & pdu);
280 
281  virtual PBoolean IsValid() const;
282 
283  virtual MessageType GetType(PString * typeName = 0) const;
284  virtual PString GetLanguage() const;
285 
290  virtual PString GetSubject(const PString& lang = PString::Empty());
291  virtual PString GetBody(const PString& lang = PString::Empty());
292  virtual PString GetThread();
293 
294  virtual PXMLElement * GetSubjectElement(const PString& lang = PString::Empty());
295  virtual PXMLElement * GetBodyElement(const PString& lang = PString::Empty());
296 
297  virtual void SetType(MessageType type);
298  virtual void SetType(const PString& type); // custom, possibly non standard, type
299  virtual void SetLanguage(const PString& lang);
300 
301  virtual void SetSubject(const PString& subj, const PString& lang = PString::Empty());
302  virtual void SetBody(const PString& body, const PString& lang = PString::Empty());
303  virtual void SetThread(const PString& thrd);
304  };
305 
306 
307  class Presence : public Stanza
308  {
310 
311  public:
321  Unknown = 999
322  };
323 
324  enum ShowType {
329  XA,
330  Other = 999
331  };
332 
335  static const PCaselessString & TypeTag();
336  static const PCaselessString & ShowTag();
337  static const PCaselessString & StatusTag();
338  static const PCaselessString & PriorityTag();
339 
342  Presence();
343 
348  Presence(PXMLElement & pdu);
349 
350  virtual PBoolean IsValid() const;
351 
352  virtual PresenceType GetType(PString * typeName = 0) const;
353  virtual ShowType GetShow(PString * showName = 0) const;
354  virtual BYTE GetPriority() const;
355 
360  virtual PString GetStatus(const PString& lang = PString::Empty());
361  virtual PXMLElement * GetStatusElement(const PString& lang = PString::Empty());
362 
363  virtual void SetType(PresenceType type);
364  virtual void SetType(const PString& type); // custom, possibly non standard, type
365  virtual void SetShow(ShowType show);
366  virtual void SetShow(const PString& show); // custom, possibly non standard, type
367  virtual void SetPriority(BYTE priority);
368 
369  virtual void SetStatus(const PString& status, const PString& lang = PString::Empty());
370  };
371 
372 
373  class IQ : public Stanza
374  {
376 
377  public:
378  enum IQType {
383  Unknown = 999
384  };
385 
388  static const PCaselessString & TypeTag();
389 
390  IQ(IQType type);
391  IQ(PXMLElement & pdu);
392  ~IQ();
393 
394  virtual PBoolean IsValid() const;
395 
401  void SetProcessed() { m_Processed = true; }
403 
404  virtual IQType GetType(PString * typeName = 0) const;
405  virtual PXMLElement * GetBody();
406 
407  virtual void SetType(IQType type);
408  virtual void SetType(const PString& type); // custom, possibly non standard, type
409  virtual void SetBody(PXMLElement * body);
410 
411  // If the this message is response, returns a pointer to the
412  // original set/get message
413  virtual IQ * GetOriginalMessage() const { return m_OriginalIQ; }
414  virtual void SetOriginalMessage(IQ * iq);
415 
419  virtual IQ * BuildResult() const;
420 
423  virtual IQ * BuildError(const PString& type, const PString& code) const;
424 
426 
427  protected:
431  };
434  namespace Disco
435  {
436  class Item : public PObject
437  {
438  PCLASSINFO(Item, PObject);
439  public:
440  Item(PXMLElement * item);
441  Item(const PString& jid, const PString& node = PString::Empty());
442 
443  const JID& GetJID() const { return m_JID; }
444  const PString& GetNode() const { return m_Node; }
445 
446  PXMLElement * AsXML(PXMLElement * parent) const;
447 
448  protected:
449  const JID m_JID;
451  };
452 
454  public:
455  ItemList(PXMLElement * list);
456  PXMLElement * AsXML(PXMLElement * parent) const;
457  };
458 
459  class Identity : public PObject
460  {
461  PCLASSINFO(Identity, PObject);
462  public:
463  Identity(PXMLElement * identity);
464  Identity(const PString& category, const PString& type, const PString& name);
465 
466  const PString& GetCategory() const { return m_Category; }
467  const PString& GetType() const { return m_Type; }
468  const PString& GetName() const { return m_Name; }
469 
470  PXMLElement * AsXML(PXMLElement * parent) const;
471 
472  protected:
476  };
477 
479  public:
480  IdentityList(PXMLElement * list);
481  PXMLElement * AsXML(PXMLElement * parent) const;
482  };
483 
484  class Info : public PObject
485  {
486  PCLASSINFO(Info, PObject);
487  public:
488  Info(PXMLElement * info);
489 
490  IdentityList& GetIdentities() { return m_Identities; }
491  PStringSet& GetFeatures() { return m_Features; }
492 
493  PXMLElement * AsXML(PXMLElement * parent) const;
494 
495  protected:
498  };
499  } // namespace Disco
500 
501 }; // class XMPP
502 
503 
504 #endif // P_EXPAT
505 
506 #endif // PTLIB_XMPP_H
507 
508 // End of File ///////////////////////////////////////////////////////////////
static const PCaselessString & PriorityTag()
Definition: xmpp.h:383
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:106
static const PCaselessString & TypeTag()
Various constant strings.
virtual void SetStatus(const PString &status, const PString &lang=PString::Empty())
Definition: xmpp.h:260
PNotifierList m_OpenHandlers
Definition: xmpp.h:176
const PCaselessString & IQQueryTag()
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:51
virtual void SetType(MessageType type)
Definition: xmpp.h:317
void AddNotifier(const PNotifierTemplate< PXMLElement & > &notifier)
Definition: xmpp.h:194
Definition: pxml.h:599
virtual PXMLElement * Read()
Read a XMPP stanza from the stream.
const PString m_Category
Definition: xmpp.h:473
Definition: xmpp.h:329
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:2164
PString m_User
Definition: xmpp.h:92
virtual MessageType GetType(PString *typeName=0) const
resource
Definition: xmpp.h:109
bool m_Processed
Definition: xmpp.h:428
Definition: xmpp.h:261
virtual IQ * BuildResult() const
Creates a new response iq for this message (that must be of the set/get type!)
const PCaselessString & NamespaceTag()
static const PCaselessString & ToTag()
static const PCaselessString & ThreadTag()
virtual void SetThread(const PString &thrd)
const PString m_Type
Definition: xmpp.h:474
This class represents a XMPP stream, i.e.
Definition: xmpp.h:143
Stream * m_Stream
Definition: xmpp.h:209
BareJID(const PString &jid)
Definition: xmpp.h:107
virtual void SetBody(const PString &body, const PString &lang=PString::Empty())
virtual void SetShow(ShowType show)
PStringSet m_Features
Definition: xmpp.h:497
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:324
virtual PString GetResource() const
Definition: xmpp.h:120
virtual BYTE GetPriority() const
Definition: xmpp.h:313
virtual PBoolean Write(const void *buf, PINDEX len)
virtual void SetType(PresenceType type)
bool Add(const Notifier &handler)
Add a notifier to the list.
Definition: notifier_ext.h:266
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:2251
Definition: xmpp.h:373
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:45
This class is a variation of a string that ignores case.
Definition: pstring.h:2012
virtual PString GetID() const
Definition: xmpp.h:382
Definition: xmpp.h:459
const JID & GetJID() const
Definition: xmpp.h:443
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:259
virtual ShowType GetShow(PString *showName=0) const
virtual PString GetTo() const
PXMLStreamParser * m_Parser
Definition: xmpp.h:175
virtual PBoolean IsValid() const
Definition: xmpp.h:319
virtual PString GetStatus(const PString &lang=PString::Empty())
Get the status for the specified language.
BareJID & operator=(const PString &jid)
Definition: xmpp.h:320
bool m_AutoReconnect
Definition: xmpp.h:210
Message()
Construct a new empty message.
PBoolean m_IsDirty
Definition: xmpp.h:97
virtual PresenceType GetType(PString *typeName=0) const
Definition: xmpp.h:256
PNotifierList & CloseHandlers()
Definition: xmpp.h:156
static const PCaselessString & FromTag()
Definition: xmpp.h:330
PString GetServer() const
Definition: xmpp.h:77
Definition: xmpp.h:55
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:101
virtual IQ * GetOriginalMessage() const
Definition: xmpp.h:413
Definition: xmpp.h:316
IQType
Definition: xmpp.h:378
const PCaselessString & PresenceStanzaTag()
static const PCaselessString & StatusTag()
virtual PString GetFrom() const
const PString & GetType() const
Definition: xmpp.h:467
virtual void BuildJID() const
virtual PString GetLanguage() const
PNotifierList m_CloseHandlers
Definition: xmpp.h:177
const PString m_Node
Definition: xmpp.h:450
virtual void SetPriority(BYTE priority)
virtual PXMLElement * GetBody()
virtual void SetLanguage(const PString &lang)
IQ(IQType type)
IdentityList & GetIdentities()
Definition: xmpp.h:490
Presence()
Construct a new empty presence.
The PNotifier and PNotifierFunction classes build a completely type safe mechanism for calling arbitr...
Definition: notifier.h:109
virtual void SetBody(PXMLElement *body)
virtual void SetID(const PString &id)
virtual PString GetBody(const PString &lang=PString::Empty())
Stream * GetStream() const
Definition: xmpp.h:196
PXMLElement * AsXML(PXMLElement *parent) const
virtual PXMLElement * GetElement(const PString &name, PINDEX i=0)
static const PCaselessString & TypeTag()
Various constant strings.
virtual void SetResource(const PString &)
Definition: xmpp.h:121
Definition: xmpp.h:436
virtual void OnClose()
Definition: xmpp.h:155
const PCaselessString & LanguageTag()
Various constant strings.
Definition: xmpp.h:318
bool PBoolean
Definition: object.h:174
IdentityList m_Identities
Definition: xmpp.h:496
Definition: xmpp.h:328
virtual void SetResource(const PString &resource)
PString m_Server
Definition: xmpp.h:93
static const PCaselessString & BodyTag()
PXMLElement * AsXML(PXMLElement *parent) const
Definition: xmpp.h:325
The character string class.
Definition: pstring.h:108
Definition: xmpp.h:101
PXMLStreamParser * GetParser()
Definition: xmpp.h:171
virtual void Reset()
Reset the parser.
virtual PObject * Clone() const
Create a copy of the class on the heap.
Definition: xmpp.h:74
PString GetUser() const
Definition: xmpp.h:76
This is a set collection class of PString objects.
Definition: pstring.h:2821
Definition: xmpp.h:307
virtual PXMLElement * GetBodyElement(const PString &lang=PString::Empty())
Definition: xmpp.h:379
Definition: xmpp.h:257
Definition: pxml.h:398
virtual PBoolean IsValid() const
static PString GenerateID()
virtual PString GetResource() const
Definition: xmpp.h:79
Definition: xmpp.h:321
Definition: xmpp.h:258
This class defines a thread of execution in the system.
Definition: thread.h:66
Definition: xmpp.h:380
Definition: xmpp.h:327
Definition: xmpp.h:315
PBoolean HasBeenProcessed() const
Definition: xmpp.h:402
static const PString & Empty()
Return an empty string.
const PString m_Name
Definition: xmpp.h:475
virtual IQ * BuildError(const PString &type, const PString &code) const
Creates an error response for this message.
virtual PNotifierList GetResponseHandlers()
Definition: xmpp.h:425
JID & operator=(const PString &jid)
Definition: xmpp.h:326
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:221
virtual IQType GetType(PString *typeName=0) const
virtual PBoolean IsBare() const
Definition: xmpp.h:85
This interface is the base class of each XMPP transport class.
Definition: xmpp.h:130
virtual PObject * Clone() const
Create a copy of the class on the heap.
Definition: xmpp.h:119
server
Definition: xmpp.h:109
const PCaselessString & IQStanzaTag()
const PString & GetName() const
Definition: xmpp.h:468
Stream(Transport *transport=0)
virtual void SetSubject(const PString &subj, const PString &lang=PString::Empty())
PNotifierList m_ResponseHandlers
Definition: xmpp.h:430
PNotifierList & OpenHandlers()
Definition: xmpp.h:152
#define PDECLARE_LIST(cls, T)
Begin declaration of list class.
Definition: lists.h:467
PStringSet & GetFeatures()
Definition: xmpp.h:491
static const PCaselessString & TypeTag()
Various constant strings.
PNotifierListTemplate< PXMLElement & > m_ElementHandlers
Definition: xmpp.h:213
PLIST(StanzaList, Stanza)
PTimeInterval m_ReconnectTimeout
Definition: xmpp.h:211
virtual PBoolean IsBare() const
Definition: xmpp.h:122
Definition: xmpp.h:181
IQ * m_OriginalIQ
Definition: xmpp.h:429
virtual PBoolean IsValid() const
virtual void OnElement(PXMLElement &pdu)
Definition: xmpp.h:381
void SetProcessed()
This method signals that the message was taken care of If the stream handler, after firing all the n...
Definition: xmpp.h:401
MessageType
Definition: xmpp.h:255
const JID m_JID
Definition: xmpp.h:449
virtual void SetTo(const PString &to)
const PCaselessString & MessageStanzaTag()
const PString & GetNode() const
Definition: xmpp.h:444
Definition: xmpp.h:250
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
PString m_Resource
Definition: xmpp.h:94
virtual PBoolean IsValid() const =0
PresenceType
Definition: xmpp.h:312
PString m_JID
Definition: xmpp.h:96
virtual void PrintOn(ostream &strm) const
Output the contents of the object to the stream.
Item(PXMLElement *item)
virtual bool OnOpen()
This callback is executed when the Open() function is called with open channels.
Definition: xmpp.h:151
Definition: xmpp.h:314
virtual PBoolean Open()=0
const PString & GetCategory() const
Definition: xmpp.h:466
virtual PString GetThread()
IdentityList(PXMLElement *list)
Definition: xmpp.h:484
virtual void SetFrom(const PString &from)
PXML m_Document
Definition: xmpp.h:174
virtual PBoolean Start(Transport *transport=0)
virtual void SetOriginalMessage(IQ *iq)
virtual PXMLElement * GetSubjectElement(const PString &lang=PString::Empty())