# xmpp.h

Go to the documentation of this file.

    00001 /*
    00002  * xmpp.h
    00003  *
    00004  * Extensible Messaging and Presence Protocol (XMPP) Core
    00005  *
    00006  * Portable Windows Library
    00007  *
    00008  * Copyright (c) 2004 Reitek S.p.A.
    00009  *
    00010  * The contents of this file are subject to the Mozilla Public License
    00011  * Version 1.0 (the "License"); you may not use this file except in
    00012  * compliance with the License. You may obtain a copy of the License at
    00013  * http://www.mozilla.org/MPL/
    00014  *
    00015  * Software distributed under the License is distributed on an "AS IS"
    00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
    00017  * the License for the specific language governing rights and limitations
    00018  * under the License.
    00019  *
    00020  * The Original Code is Portable Windows Library.
    00021  *
    00022  * The Initial Developer of the Original Code is Post Increment
    00023  *
    00024  * Contributor(s): ______________________________________.
    00025  *
    00026  * $Revision: 25387 $
    00027  * $Author: rjongbloed $
    00028  * $Date: 2011-03-22 22:51:09 -0500 (Tue, 22 Mar 2011) $
    00029  */
    00030
    00031 #ifndef PTLIB_XMPP_H
    00032 #define PTLIB_XMPP_H
    00033
    00034 #ifdef P_USE_PRAGMA
    00035 #pragma interface
    00036 #endif
    00037
    00038 #include <ptlib.h>
    00039
    00040 #if P_EXPAT
    00041
    00042 #include <ptclib/pxml.h>
    00043 #include <ptlib/notifier_ext.h>
    00044
    00045
    00047
    00048 namespace XMPP
    00049 {
    00052   extern const PCaselessString & LanguageTag();
    00053   extern const PCaselessString & NamespaceTag();
    00054   extern const PCaselessString & MessageStanzaTag();
    00055   extern const PCaselessString & PresenceStanzaTag();
    00056   extern const PCaselessString & IQStanzaTag();
    00057   extern const PCaselessString & IQQueryTag();
    00058
    00059   class JID : public PObject
    00060   {
    00061     PCLASSINFO(JID, PObject);
    00062
    00063   public:
    00064     JID(const char * jid = 0);
    00065     JID(const PString& jid);
    00066     JID(const PString& user, const PString& server, const PString& resource = PString::Empty());
    00067
    00068     virtual Comparison Compare(
    00069       const PObject & obj
    00070       ) const;
    00071
    00072     JID& operator=(
    00073       const PString & jid
    00074       );
    00075
    00076     operator const PString&() const;
    00077
    00078     virtual PObject * Clone() const { return new JID(m_JID); }
    00079
    00080     PString   GetUser() const         { return m_User; }
    00081     PString   GetServer() const       { return m_Server; }
    00082
    00083     virtual PString GetResource() const { return m_Resource; }
    00084
    00085     virtual void SetUser(const PString& user);
    00086     virtual void SetServer(const PString& server);
    00087     virtual void SetResource(const PString& resource);
    00088
    00089     virtual PBoolean IsBare() const       { return m_Resource.IsEmpty(); }
    00090     virtual void PrintOn(ostream & strm) const;
    00091
    00092   protected:
    00093     virtual void ParseJID(const PString& jid);
    00094     virtual void BuildJID() const;
    00095
    00096     PString   m_User;
    00097     PString   m_Server;
    00098     PString   m_Resource;
    00099
    00100     mutable PString m_JID;
    00101     mutable PBoolean    m_IsDirty;
    00102   };
    00103
    00104   // A bare jid is a jid with no resource
    00105   class BareJID : public JID
    00106   {
    00107     PCLASSINFO(BareJID, JID);
    00108
    00109   public:
    00110     BareJID(const char * jid = 0) : JID(jid) { }
    00111     BareJID(const PString& jid) : JID(jid) { }
    00112     BareJID(const PString& user, const PString& server, const PString& resource = PString::Empty())
    00113       : JID(user, server, resource) { }
    00114
    00115     virtual Comparison Compare(
    00116       const PObject & obj
    00117       ) const;
    00118
    00119     BareJID& operator=(
    00120       const PString & jid
    00121       );
    00122
    00123     virtual PObject * Clone() const { return new BareJID(m_JID); }
    00124     virtual PString GetResource() const { return PString::Empty(); }
    00125     virtual void SetResource(const PString&) { }
    00126     virtual PBoolean IsBare() const { return true; }
    00127   };
    00128
    00134   class Transport : public PIndirectChannel
    00135   {
    00136     PCLASSINFO(Transport, PIndirectChannel);
    00137
    00138   public:
    00139     virtual PBoolean Open() = 0;
    00140     virtual PBoolean Close() = 0;
    00141   };
    00142
    00143
    00147   class Stream : public PIndirectChannel
    00148   {
    00149     PCLASSINFO(Stream, PIndirectChannel);
    00150
    00151   public:
    00152     Stream(Transport * transport = 0);
    00153     ~Stream();
    00154
    00155     virtual PBoolean        OnOpen()            { return m_OpenHandlers.Fire(*this); }
    00156     PNotifierList&      OpenHandlers()      { return m_OpenHandlers; }
    00157
    00158     virtual PBoolean        Close();
    00159     virtual void        OnClose()           { m_CloseHandlers.Fire(*this); }
    00160     PNotifierList&      CloseHandlers()     { return m_CloseHandlers; }
    00161
    00162     virtual PBoolean        Write(const void * buf, PINDEX len);
    00163     virtual PBoolean        Write(const PString& data);
    00164     virtual PBoolean        Write(const PXML& pdu);
    00165
    00168     virtual PXML *      Read();
    00169
    00173     virtual void        Reset();
    00174     PXMLStreamParser *  GetParser()     { return m_Parser; }
    00175
    00176   protected:
    00177     PXMLStreamParser *  m_Parser;
    00178     PNotifierList       m_OpenHandlers;
    00179     PNotifierList       m_CloseHandlers;
    00180   };
    00181
    00182
    00183   class BaseStreamHandler : public PThread
    00184   {
    00185     PCLASSINFO(BaseStreamHandler, PThread);
    00186
    00187   public:
    00188     BaseStreamHandler();
    00189     ~BaseStreamHandler();
    00190
    00191     virtual PBoolean        Start(Transport * transport = 0);
    00192     virtual PBoolean        Stop(const PString& error = PString::Empty());
    00193
    00194     void                SetAutoReconnect(PBoolean b = true, long timeout = 1000);
    00195
    00196     PNotifierList&      ElementHandlers()   { return m_ElementHandlers; }
    00197     Stream *            GetStream()         { return m_Stream; }
    00198
    00199     virtual PBoolean        Write(const void * buf, PINDEX len);
    00200     virtual PBoolean        Write(const PString& data);
    00201     virtual PBoolean        Write(const PXML& pdu);
    00202     virtual void        OnElement(PXML& pdu);
    00203
    00204     virtual void        Main();
    00205
    00206   protected:
    00207     PDECLARE_NOTIFIER(Stream, BaseStreamHandler, OnOpen);
    00208     PDECLARE_NOTIFIER(Stream, BaseStreamHandler, OnClose);
    00209
    00210     Stream *        m_Stream;
    00211     PBoolean            m_AutoReconnect;
    00212     PTimeInterval   m_ReconnectTimeout;
    00213
    00214     PNotifierList   m_ElementHandlers;
    00215   };
    00216
    00217
    00222   class Stanza : public PXML
    00223   {
    00224     PCLASSINFO(Stanza, PXML)
    00225
    00226   public:
    00229     static const PCaselessString & IDTag();
    00230     static const PCaselessString & FromTag();
    00231     static const PCaselessString & ToTag();
    00232
    00233     virtual PBoolean IsValid() const = 0;
    00234
    00235     virtual PString GetID() const;
    00236     virtual PString GetFrom() const;
    00237     virtual PString GetTo() const;
    00238
    00239     virtual void SetID(const PString& id);
    00240     virtual void SetFrom(const PString& from);
    00241     virtual void SetTo(const PString& to);
    00242
    00243     virtual PXMLElement * GetElement(const PString& name, PINDEX i = 0);
    00244     virtual void AddElement(PXMLElement * elem);
    00245
    00246     static PString GenerateID();
    00247   };
    00248
    00249   PLIST(StanzaList, Stanza);
    00250
    00251
    00252   class Message : public Stanza
    00253   {
    00254     PCLASSINFO(Message, Stanza)
    00255
    00256   public:
    00257     enum MessageType {
    00258       Normal,
    00259       Chat,
    00260       Error,
    00261       GroupChat,
    00262       HeadLine,
    00263       Unknown = 999
    00264     };
    00265
    00268     static const PCaselessString & TypeTag();
    00269     static const PCaselessString & SubjectTag();
    00270     static const PCaselessString & BodyTag();
    00271     static const PCaselessString & ThreadTag();
    00272
    00275     Message();
    00276
    00281     Message(PXML& pdu);
    00282     Message(PXML * pdu);
    00283
    00284     virtual PBoolean IsValid() const;
    00285     static PBoolean IsValid(const PXML * pdu);
    00286
    00287     virtual MessageType GetType(PString * typeName = 0) const;
    00288     virtual PString     GetLanguage() const;
    00289
    00294     virtual PString GetSubject(const PString& lang = PString::Empty());
    00295     virtual PString GetBody(const PString& lang = PString::Empty());
    00296     virtual PString GetThread();
    00297
    00298     virtual PXMLElement * GetSubjectElement(const PString& lang = PString::Empty());
    00299     virtual PXMLElement * GetBodyElement(const PString& lang = PString::Empty());
    00300
    00301     virtual void SetType(MessageType type);
    00302     virtual void SetType(const PString& type); // custom, possibly non standard, type
    00303     virtual void SetLanguage(const PString& lang);
    00304
    00305     virtual void SetSubject(const PString& subj, const PString& lang = PString::Empty());
    00306     virtual void SetBody(const PString& body, const PString& lang = PString::Empty());
    00307     virtual void SetThread(const PString& thrd);
    00308   };
    00309
    00310
    00311   class Presence : public Stanza
    00312   {
    00313     PCLASSINFO(Presence, Stanza)
    00314
    00315   public:
    00316     enum PresenceType {
    00317       Available,
    00318       Unavailable,
    00319       Subscribe,
    00320       Subscribed,
    00321       Unsubscribe,
    00322       Unsubscribed,
    00323       Probe,
    00324       Error,
    00325       Unknown = 999
    00326     };
    00327
    00328     enum ShowType {
    00329       Online,
    00330       Away,
    00331       Chat,
    00332       DND,
    00333       XA,
    00334       Other = 999
    00335     };
    00336
    00339     static const PCaselessString & TypeTag();
    00340     static const PCaselessString & ShowTag();
    00341     static const PCaselessString & StatusTag();
    00342     static const PCaselessString & PriorityTag();
    00343
    00346     Presence();
    00347
    00352     Presence(PXML& pdu);
    00353     Presence(PXML * pdu);
    00354
    00355     virtual PBoolean IsValid() const;
    00356     static PBoolean IsValid(const PXML * pdu);
    00357
    00358     virtual PresenceType GetType(PString * typeName = 0) const;
    00359     virtual ShowType     GetShow(PString * showName = 0) const;
    00360     virtual BYTE         GetPriority() const;
    00361
    00366     virtual PString GetStatus(const PString& lang = PString::Empty());
    00367     virtual PXMLElement * GetStatusElement(const PString& lang = PString::Empty());
    00368
    00369     virtual void SetType(PresenceType type);
    00370     virtual void SetType(const PString& type); // custom, possibly non standard, type
    00371     virtual void SetShow(ShowType show);
    00372     virtual void SetShow(const PString& show); // custom, possibly non standard, type
    00373     virtual void SetPriority(BYTE priority);
    00374
    00375     virtual void SetStatus(const PString& status, const PString& lang = PString::Empty());
    00376   };
    00377
    00378
    00379   class IQ : public Stanza
    00380   {
    00381     PCLASSINFO(IQ, Stanza)
    00382
    00383   public:
    00384     enum IQType {
    00385       Get,
    00386       Set,
    00387       Result,
    00388       Error,
    00389       Unknown = 999
    00390     };
    00391
    00394     static const PCaselessString & TypeTag();
    00395
    00396     IQ(IQType type, PXMLElement * body = 0);
    00397     IQ(PXML& pdu);
    00398     IQ(PXML * pdu);
    00399     ~IQ();
    00400
    00401     virtual PBoolean IsValid() const;
    00402     static PBoolean IsValid(const PXML * pdu);
    00403
    00409     void SetProcessed()             { m_Processed = true; }
    00410     PBoolean HasBeenProcessed() const   { return m_Processed; }
    00411
    00412     virtual IQType        GetType(PString * typeName = 0) const;
    00413     virtual PXMLElement * GetBody();
    00414
    00415     virtual void SetType(IQType type);
    00416     virtual void SetType(const PString& type); // custom, possibly non standard, type
    00417     virtual void SetBody(PXMLElement * body);
    00418
    00419     // If the this message is response, returns a pointer to the
    00420     // original set/get message
    00421     virtual IQ *  GetOriginalMessage() const      { return m_OriginalIQ; }
    00422     virtual void  SetOriginalMessage(IQ * iq);
    00423
    00427     virtual IQ *  BuildResult() const;
    00428
    00431     virtual IQ *  BuildError(const PString& type, const PString& code) const;
    00432
    00433     virtual PNotifierList GetResponseHandlers()   { return m_ResponseHandlers; }
    00434
    00435   protected:
    00436     PBoolean            m_Processed;
    00437     IQ *            m_OriginalIQ;
    00438     PNotifierList   m_ResponseHandlers;
    00439   };
    00442   namespace Disco
    00443   {
    00444     class Item : public PObject
    00445     {
    00446       PCLASSINFO(Item, PObject);
    00447     public:
    00448       Item(PXMLElement * item);
    00449       Item(const PString& jid, const PString& node = PString::Empty());
    00450
    00451       const JID&      GetJID() const    { return m_JID; }
    00452       const PString&  GetNode() const   { return m_Node; }
    00453
    00454       PXMLElement *   AsXML(PXMLElement * parent) const;
    00455
    00456     protected:
    00457       const JID     m_JID;
    00458       const PString m_Node;
    00459     };
    00460
    00461     PDECLARE_LIST(ItemList, Item)
    00462     public:
    00463       ItemList(PXMLElement * list);
    00464       PXMLElement * AsXML(PXMLElement * parent) const;
    00465     };
    00466
    00467     class Identity : public PObject
    00468     {
    00469       PCLASSINFO(Identity, PObject);
    00470     public:
    00471       Identity(PXMLElement * identity);
    00472       Identity(const PString& category, const PString& type, const PString& name);
    00473
    00474       const PString&  GetCategory() const   { return m_Category; }
    00475       const PString&  GetType() const       { return m_Type; }
    00476       const PString&  GetName() const       { return m_Name; }
    00477
    00478       PXMLElement *   AsXML(PXMLElement * parent) const;
    00479
    00480     protected:
    00481       const PString m_Category;
    00482       const PString m_Type;
    00483       const PString m_Name;
    00484     };
    00485
    00486     PDECLARE_LIST(IdentityList, Identity)
    00487     public:
    00488       IdentityList(PXMLElement * list);
    00489       PXMLElement * AsXML(PXMLElement * parent) const;
    00490     };
    00491
    00492     class Info : public PObject
    00493     {
    00494       PCLASSINFO(Info, PObject);
    00495     public:
    00496       Info(PXMLElement * info);
    00497
    00498       IdentityList&   GetIdentities() { return m_Identities; }
    00499       PStringSet&     GetFeatures()   { return m_Features; }
    00500
    00501       PXMLElement *   AsXML(PXMLElement * parent) const;
    00502
    00503     protected:
    00504       IdentityList  m_Identities;
    00505       PStringSet    m_Features;
    00506     };
    00507   } // namespace Disco
    00508
    00509 }; // class XMPP
    00510
    00511
    00512 #endif  // P_EXPAT
    00513
    00514 #endif  // PTLIB_XMPP_H
    00515
    00516 // End of File ///////////////////////////////////////////////////////////////