4 * Extensible Messaging and Presence Protocol (XMPP) Core
6 * Portable Windows Library
8 * Copyright (c) 2004 Reitek S.p.A.
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/
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
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Post Increment
24 * Contributor(s): ______________________________________.
27 * Revision 1.6 2007/04/10 05:08:46 rjongbloed
28 * Fixed issue with use of static C string variables in DLL environment,
29 * must use functional interface for correct initialisation.
31 * Revision 1.5 2005/11/30 12:47:37 csoutheren
32 * Removed tabs, reformatted some code, and changed tags for Doxygen
34 * Revision 1.4 2004/11/11 07:34:50 csoutheren
35 * Added #include <ptlib.h>
37 * Revision 1.3 2004/05/09 07:23:46 rjongbloed
38 * More work on XMPP, thanks Federico Pinna and Reitek S.p.A.
40 * Revision 1.2 2004/04/26 01:51:57 rjongbloed
41 * More implementation of XMPP, thanks a lot to Federico Pinna & Reitek S.p.A.
43 * Revision 1.1 2004/04/22 12:31:00 rjongbloed
44 * Added PNotifier extensions and XMPP (Jabber) support,
45 * thanks to Federico Pinna and Reitek S.p.A.
61 #include <ptclib/pxml.h>
62 #include <ptlib/notifier_ext.h>
65 ///////////////////////////////////////////////////////
69 /** Various constant strings
71 extern const PString
& LanguageTag();
72 extern const PString
& NamespaceTag();
73 extern const PString
& MessageStanzaTag();
74 extern const PString
& PresenceStanzaTag();
75 extern const PString
& IQStanzaTag();
76 extern const PString
& IQQueryTag();
78 class JID
: public PObject
80 PCLASSINFO(JID
, PObject
);
83 JID(const char * jid
= 0);
84 JID(const PString
& jid
);
85 JID(const PString
& user
, const PString
& server
, const PString
& resource
= PString::Empty());
87 virtual Comparison
Compare(
88 const PObject
& obj
///< Object to compare against.
92 const PString
& jid
///< New JID to assign.
95 operator const PString
&() const;
97 virtual PObject
* Clone() const { return new JID(m_JID
); }
99 PString
GetUser() const { return m_User
; }
100 PString
GetServer() const { return m_Server
; }
102 virtual PString
GetResource() const { return m_Resource
; }
104 virtual void SetUser(const PString
& user
);
105 virtual void SetServer(const PString
& server
);
106 virtual void SetResource(const PString
& resource
);
108 virtual BOOL
IsBare() const { return m_Resource
.IsEmpty(); }
109 virtual void PrintOn(ostream
& strm
) const;
112 virtual void ParseJID(const PString
& jid
);
113 virtual void BuildJID() const;
119 mutable PString m_JID
;
120 mutable BOOL m_IsDirty
;
123 // A bare jid is a jid with no resource
124 class BareJID
: public JID
126 PCLASSINFO(BareJID
, JID
);
129 BareJID(const char * jid
= 0) : JID(jid
) { }
130 BareJID(const PString
& jid
) : JID(jid
) { }
131 BareJID(const PString
& user
, const PString
& server
, const PString
& resource
= PString::Empty())
132 : JID(user
, server
, resource
) { }
134 virtual Comparison
Compare(
135 const PObject
& obj
///< Object to compare against.
139 const PString
& jid
///< New JID to assign.
142 virtual PObject
* Clone() const { return new BareJID(m_JID
); }
143 virtual PString
GetResource() const { return PString::Empty(); }
144 virtual void SetResource(const PString
&) { }
145 virtual BOOL
IsBare() const { return TRUE
; }
148 /** This interface is the base class of each XMPP transport class
150 Derived classes might include an XMPP TCP transport as well as
151 classes to handle XMPP incapsulated in SIP messages.
153 class Transport
: public PIndirectChannel
155 PCLASSINFO(Transport
, PIndirectChannel
);
158 virtual BOOL
Open() = 0;
159 virtual BOOL
Close() = 0;
163 /** This class represents a XMPP stream, i.e. a XML message exchange
164 between XMPP entities
166 class Stream
: public PIndirectChannel
168 PCLASSINFO(Stream
, PIndirectChannel
);
171 Stream(Transport
* transport
= 0);
174 virtual BOOL
OnOpen() { return m_OpenHandlers
.Fire(*this); }
175 PNotifierList
& OpenHandlers() { return m_OpenHandlers
; }
177 virtual BOOL
Close();
178 virtual void OnClose() { m_CloseHandlers
.Fire(*this); }
179 PNotifierList
& CloseHandlers() { return m_CloseHandlers
; }
181 virtual BOOL
Write(const void * buf
, PINDEX len
);
182 virtual BOOL
Write(const PString
& data
);
183 virtual BOOL
Write(const PXML
& pdu
);
185 /** Read a XMPP stanza from the stream
187 virtual PXML
* Read();
189 /** Reset the parser. The will delete and re-instantiate the
192 virtual void Reset();
193 PXMLStreamParser
* GetParser() { return m_Parser
; }
196 PXMLStreamParser
* m_Parser
;
197 PNotifierList m_OpenHandlers
;
198 PNotifierList m_CloseHandlers
;
202 class BaseStreamHandler
: public PThread
204 PCLASSINFO(BaseStreamHandler
, PThread
);
208 ~BaseStreamHandler();
210 virtual BOOL
Start(Transport
* transport
= 0);
211 virtual BOOL
Stop(const PString
& error
= PString::Empty());
213 void SetAutoReconnect(BOOL b
= TRUE
, long timeout
= 1000);
215 PNotifierList
& ElementHandlers() { return m_ElementHandlers
; }
216 Stream
* GetStream() { return m_Stream
; }
218 virtual BOOL
Write(const void * buf
, PINDEX len
);
219 virtual BOOL
Write(const PString
& data
);
220 virtual BOOL
Write(const PXML
& pdu
);
221 virtual void OnElement(PXML
& pdu
);
226 PDECLARE_NOTIFIER(Stream
, BaseStreamHandler
, OnOpen
);
227 PDECLARE_NOTIFIER(Stream
, BaseStreamHandler
, OnClose
);
230 BOOL m_AutoReconnect
;
231 PTimeInterval m_ReconnectTimeout
;
233 PNotifierList m_ElementHandlers
;
237 /** XMPP stanzas: the following classes represent the three
238 stanzas (PDUs) defined by the xmpp protocol
241 class Stanza
: public PXML
243 PCLASSINFO(Stanza
, PXML
)
246 /** Various constant strings
248 static const PString
& IDTag();
249 static const PString
& FromTag();
250 static const PString
& ToTag();
252 virtual BOOL
IsValid() const = 0;
254 virtual PString
GetID() const;
255 virtual PString
GetFrom() const;
256 virtual PString
GetTo() const;
258 virtual void SetID(const PString
& id
);
259 virtual void SetFrom(const PString
& from
);
260 virtual void SetTo(const PString
& to
);
262 virtual PXMLElement
* GetElement(const PString
& name
, PINDEX i
= 0);
263 virtual void AddElement(PXMLElement
* elem
);
265 static PString
GenerateID();
268 PLIST(StanzaList
, Stanza
);
271 class Message
: public Stanza
273 PCLASSINFO(Message
, Stanza
)
285 /** Various constant strings
287 static const PString
& TypeTag();
288 static const PString
& SubjectTag();
289 static const PString
& BodyTag();
290 static const PString
& ThreadTag();
292 /** Construct a new empty message
296 /** Construct a message from a (received) xml PDU.
297 The root of the pdu MUST be a message stanza.
298 NOTE: the root of the pdu is cloned.
303 virtual BOOL
IsValid() const;
304 static BOOL
IsValid(const PXML
* pdu
);
306 virtual MessageType
GetType(PString
* typeName
= 0) const;
307 virtual PString
GetLanguage() const;
309 /** Get the subject for the specified language. The default subject (if any)
310 is returned in case no language is specified or a matching one cannot be
313 virtual PString
GetSubject(const PString
& lang
= PString::Empty());
314 virtual PString
GetBody(const PString
& lang
= PString::Empty());
315 virtual PString
GetThread();
317 virtual PXMLElement
* GetSubjectElement(const PString
& lang
= PString::Empty());
318 virtual PXMLElement
* GetBodyElement(const PString
& lang
= PString::Empty());
320 virtual void SetType(MessageType type
);
321 virtual void SetType(const PString
& type
); // custom, possibly non standard, type
322 virtual void SetLanguage(const PString
& lang
);
324 virtual void SetSubject(const PString
& subj
, const PString
& lang
= PString::Empty());
325 virtual void SetBody(const PString
& body
, const PString
& lang
= PString::Empty());
326 virtual void SetThread(const PString
& thrd
);
330 class Presence
: public Stanza
332 PCLASSINFO(Presence
, Stanza
)
356 /** Various constant strings
358 static const PString
& TypeTag();
359 static const PString
& ShowTag();
360 static const PString
& StatusTag();
361 static const PString
& PriorityTag();
363 /** Construct a new empty presence
367 /** Construct a presence from a (received) xml PDU.
368 The root of the pdu MUST be a presence stanza.
369 NOTE: the root of the pdu is cloned.
372 Presence(PXML
* pdu
);
374 virtual BOOL
IsValid() const;
375 static BOOL
IsValid(const PXML
* pdu
);
377 virtual PresenceType
GetType(PString
* typeName
= 0) const;
378 virtual ShowType
GetShow(PString
* showName
= 0) const;
379 virtual BYTE
GetPriority() const;
381 /** Get the status for the specified language. The default status (if any)
382 is returned in case no language is specified or a matching one cannot be
385 virtual PString
GetStatus(const PString
& lang
= PString::Empty());
386 virtual PXMLElement
* GetStatusElement(const PString
& lang
= PString::Empty());
388 virtual void SetType(PresenceType type
);
389 virtual void SetType(const PString
& type
); // custom, possibly non standard, type
390 virtual void SetShow(ShowType show
);
391 virtual void SetShow(const PString
& show
); // custom, possibly non standard, type
392 virtual void SetPriority(BYTE priority
);
394 virtual void SetStatus(const PString
& status
, const PString
& lang
= PString::Empty());
398 class IQ
: public Stanza
400 PCLASSINFO(IQ
, Stanza
)
411 /** Various constant strings
413 static const PString
& TypeTag();
415 IQ(IQType type
, PXMLElement
* body
= 0);
420 virtual BOOL
IsValid() const;
421 static BOOL
IsValid(const PXML
* pdu
);
423 /** This method signals that the message was taken care of
424 If the stream handler, after firing all the notifiers finds
425 that an iq set/get pdu has not being processed, it returns
426 an error to the sender
428 void SetProcessed() { m_Processed
= TRUE
; }
429 BOOL
HasBeenProcessed() const { return m_Processed
; }
431 virtual IQType
GetType(PString
* typeName
= 0) const;
432 virtual PXMLElement
* GetBody();
434 virtual void SetType(IQType type
);
435 virtual void SetType(const PString
& type
); // custom, possibly non standard, type
436 virtual void SetBody(PXMLElement
* body
);
438 // If the this message is response, returns a pointer to the
439 // original set/get message
440 virtual IQ
* GetOriginalMessage() const { return m_OriginalIQ
; }
441 virtual void SetOriginalMessage(IQ
* iq
);
443 /** Creates a new response iq for this message (that must
444 be of the set/get type!)
446 virtual IQ
* BuildResult() const;
448 /** Creates an error response for this message
450 virtual IQ
* BuildError(const PString
& type
, const PString
& code
) const;
452 virtual PNotifierList
GetResponseHandlers() { return m_ResponseHandlers
; }
457 PNotifierList m_ResponseHandlers
;
459 /** JEP-0030 Service Discovery classes
463 class Item
: public PObject
465 PCLASSINFO(Item
, PObject
);
467 Item(PXMLElement
* item
);
468 Item(const PString
& jid
, const PString
& node
= PString::Empty());
470 const JID
& GetJID() const { return m_JID
; }
471 const PString
& GetNode() const { return m_Node
; }
473 PXMLElement
* AsXML(PXMLElement
* parent
) const;
477 const PString m_Node
;
480 PDECLARE_LIST(ItemList
, Item
)
482 ItemList(PXMLElement
* list
);
483 PXMLElement
* AsXML(PXMLElement
* parent
) const;
486 class Identity
: public PObject
488 PCLASSINFO(Identity
, PObject
);
490 Identity(PXMLElement
* identity
);
491 Identity(const PString
& category
, const PString
& type
, const PString
& name
);
493 const PString
& GetCategory() const { return m_Category
; }
494 const PString
& GetType() const { return m_Type
; }
495 const PString
& GetName() const { return m_Name
; }
497 PXMLElement
* AsXML(PXMLElement
* parent
) const;
500 const PString m_Category
;
501 const PString m_Type
;
502 const PString m_Name
;
505 PDECLARE_LIST(IdentityList
, Identity
)
507 IdentityList(PXMLElement
* list
);
508 PXMLElement
* AsXML(PXMLElement
* parent
) const;
511 class Info
: public PObject
513 PCLASSINFO(Info
, PObject
);
515 Info(PXMLElement
* info
);
517 IdentityList
& GetIdentities() { return m_Identities
; }
518 PStringSet
& GetFeatures() { return m_Features
; }
520 PXMLElement
* AsXML(PXMLElement
* parent
) const;
523 IdentityList m_Identities
;
524 PStringSet m_Features
;
535 // End of File ///////////////////////////////////////////////////////////////