OPAL  Version 3.14.3
sippres.h
Go to the documentation of this file.
1 /*
2  * sippres.h
3  *
4  * SIP Presence classes for Opal
5  *
6  * Open Phone Abstraction Library (OPAL)
7  *
8  * Copyright (c) 2009 Post Increment
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 Open Phone Abstraction Library.
21  *
22  * The Initial Developer of the Original Code is Post Increment
23  *
24  * Contributor(s): ______________________________________.
25  *
26  * $Revision: 29772 $
27  * $Author: rjongbloed $
28  * $Date: 2013-05-23 19:03:58 +1000 (Thu, 23 May 2013) $
29  */
30 
31 #ifndef OPAL_SIP_SIPPRES_H
32 #define OPAL_SIP_SIPPRES_H
33 
34 #include <ptlib.h>
35 #include <opal_config.h>
36 
37 
38 #if OPAL_SIP_PRESENCE
39 
40 #include <opal/pres_ent.h>
41 #include <ptclib/pxml.h>
42 #include <sip/sipep.h>
43 #include <ptlib/notifier_ext.h>
44 
45 
46 class XCAPClient : public PHTTPClient
47 {
48  public:
49  struct ElementSelector {
50  ElementSelector(
51  const PString & name = PString::Empty(),
52  const PString & position = PString::Empty()
53  ) : m_name(name)
54  , m_position(position)
55  { PAssert(!m_name.IsEmpty(), PInvalidParameter); }
56 
57  ElementSelector(
58  const PString & name,
59  const PString & attribute,
60  const PString & value
61  ) : m_name(name)
62  , m_attribute(attribute)
63  , m_value(value)
64  { PAssert(!m_name.IsEmpty(), PInvalidParameter); }
65 
66  ElementSelector(
67  const PString & name,
68  const PString & position,
69  const PString & attribute,
70  const PString & value
71  ) : m_name(name)
72  , m_position(position)
73  , m_attribute(attribute)
74  , m_value(value)
75  { PAssert(!m_name.IsEmpty(), PInvalidParameter); }
76 
77  PString AsString() const;
78 
79  PString m_name;
80  PString m_position;
81  PString m_attribute;
82  PString m_value;
83  };
84 
85  class NodeSelector : public std::list<ElementSelector>
86  {
87  public:
88  NodeSelector()
89  { }
90  NodeSelector(
91  const ElementSelector & selector
92  ) { push_back(selector); }
93  NodeSelector(
94  const ElementSelector & selector1,
95  const ElementSelector & selector2
96  ) { push_back(selector1); push_back(selector2); }
97  NodeSelector(
98  const ElementSelector & selector1,
99  const ElementSelector & selector2,
100  const ElementSelector & selector3
101  ) { push_back(selector1); push_back(selector2); push_back(selector3); }
102 
103  void AddElement(
104  const PString & name,
105  const PString & position = PString::Empty()
106  ) { push_back(ElementSelector(name, position)); }
107 
108  void AddElement(
109  const PString & name,
110  const PString & attribute,
111  const PString & value
112  ) { push_back(ElementSelector(name, attribute, value)); }
113 
114  void AddElement(
115  const PString & name,
116  const PString & position,
117  const PString & attribute,
118  const PString & value
119  ) { push_back(ElementSelector(name, position, attribute, value)); }
120 
121  void SetNamespace(
122  const PString & space,
123  const PString & alias = PString::Empty()
124  ) { PAssert(!space.IsEmpty(), PInvalidParameter); m_namespaces[alias] = space; }
125 
126  void AddToURL(
127  PURL & url
128  ) const;
129 
130  protected:
131  std::map<PString, PString> m_namespaces;
132  };
133 
134 
135  XCAPClient();
136 
137  bool GetXml(
138  PXML & xml
139  ) { return GetXml(BuildURL(), xml); }
140 
141  bool GetXml(
142  const PURL & url,
143  PXML & xml
144  );
145 
146  bool PutXml(
147  const PXML & xml
148  ) { return PutXml(BuildURL(), xml); }
149 
150  bool PutXml(
151  const PURL & url,
152  const PXML & xml
153  );
154 
155  bool DeleteXml() { return DeleteDocument(BuildURL()); }
156 
157 
158  PURL BuildURL();
159 
160 
161  void SetRoot(
162  const PURL & server
163  ) { m_root = server; }
164  const PURL & GetRoot() const { return m_root; }
165 
166  void SetApplicationUniqueID(
167  const PString & id
168  ) { m_auid = id; }
169  const PString & GetApplicationUniqueID() const { return m_auid; }
170 
171  void SetGlobal() { m_global = true; }
172  bool IsGlobal() const { return m_global; }
173 
174  void SetUserIdentifier(
175  const PString & id
176  ) { m_global = false; m_xui = id; }
177  const PString & GetUserIdentifier() const { return m_xui; }
178 
179  void SetFilename(
180  const PString & fn
181  ) { m_filename = fn; }
182  const PString & GetFilename() const { return m_filename; }
183 
184  void SetNode(
185  const NodeSelector & node
186  ) { m_node = node; }
187  const NodeSelector & GetNode() const { return m_node; }
188  void ClearNode() { m_node.clear(); }
189 
190  void SetContentType(
191  const PString & type
192  ) { m_contentType = type; }
193  const PString & GetContentType() const { return m_contentType; }
194 
195  protected:
196  PURL m_root;
197  PString m_auid;
198  bool m_global;
199  PString m_xui;
200  PString m_filename;
201  NodeSelector m_node;
202  PString m_contentType;
203 };
204 
205 
206 class SIPWatcherInfoCommand : public OpalPresentityCommand {
207  public:
208  SIPWatcherInfoCommand(bool unsubscribe = false) : m_unsubscribe(unsubscribe) { }
209 
210  bool m_unsubscribe;
211 };
212 
213 
216 class SIP_Presentity : public OpalPresentityWithCommandThread, public PValidatedNotifierTarget
217 {
218  PCLASSINFO(SIP_Presentity, OpalPresentityWithCommandThread);
219 
220  public:
221  SIP_Presentity();
222  SIP_Presentity(const SIP_Presentity & other);
223  ~SIP_Presentity();
224 
225  virtual PObject * Clone() const { return new SIP_Presentity(*this); }
226 
227  enum SubProtocol {
228  // Note order is important
229  e_PeerToPeer,
230  e_WithAgent,
231  e_XCAP,
232  e_OMA
233  };
234 
235  static const PCaselessString & PIDFEntityKey();
236  static const PCaselessString & SubProtocolKey();
237  static const PCaselessString & PresenceAgentKey();
238  static const PCaselessString & TransportKey();
239  static const PCaselessString & XcapRootKey();
240  static const PCaselessString & XcapAuthIdKey();
241  static const PCaselessString & XcapPasswordKey();
242  static const PCaselessString & XcapAuthAuidKey();
243  static const PCaselessString & XcapAuthFileKey();
244  static const PCaselessString & XcapBuddyListKey();
245 
246  virtual PStringArray GetAttributeNames() const;
247  virtual PStringArray GetAttributeTypes() const;
248 
249  virtual bool Open();
250  virtual bool Close();
251  virtual BuddyStatus GetBuddyListEx(BuddyList & buddies);
252  virtual BuddyStatus SetBuddyListEx(const BuddyList & buddies);
253  virtual BuddyStatus DeleteBuddyListEx();
254  virtual BuddyStatus GetBuddyEx(BuddyInfo & buddy);
255  virtual BuddyStatus SetBuddyEx(const BuddyInfo & buddy);
256  virtual BuddyStatus DeleteBuddyEx(const PURL & presentity);
257  virtual BuddyStatus SubscribeBuddyListEx(PINDEX & successful, bool subscribe = true);
258 
259  SIPEndPoint & GetEndpoint() { return *m_endpoint; }
260 
261  void Internal_SendLocalPresence(const OpalSetLocalPresenceCommand & cmd);
262  void Internal_SubscribeToPresence(const OpalSubscribeToPresenceCommand & cmd);
263  void Internal_SubscribeToWatcherInfo(const SIPWatcherInfoCommand & cmd);
264  void Internal_AuthorisationRequest(const OpalAuthorisationRequestCommand & cmd);
265 
266  unsigned GetExpiryTime() const;
267 
268  protected:
269  PDECLARE_VALIDATED_NOTIFIER2(SIPSubscribeHandler, SIP_Presentity, OnPresenceSubscriptionStatus, const SIPSubscribe::SubscriptionStatus &);
270  PDECLARE_VALIDATED_NOTIFIER2(SIPSubscribeHandler, SIP_Presentity, OnPresenceNotify, SIPSubscribe::NotifyCallbackInfo &);
271  PDECLARE_VALIDATED_NOTIFIER2(SIPSubscribeHandler, SIP_Presentity, OnWatcherInfoSubscriptionStatus, const SIPSubscribe::SubscriptionStatus &);
272  PDECLARE_VALIDATED_NOTIFIER2(SIPSubscribeHandler, SIP_Presentity, OnWatcherInfoNotify, SIPSubscribe::NotifyCallbackInfo &);
273  void OnReceivedWatcherStatus(PXMLElement * watcher);
274  void SetPIDFEntity(PURL & entity);
275  bool ChangeAuthNode(XCAPClient & xcap, const OpalAuthorisationRequestCommand & cmd);
276  void InitRootXcap(XCAPClient & xcap);
277  void InitBuddyXcap(
278  XCAPClient & xcap,
279  const PString & entryName = PString::Empty(),
280  const PString & listName = PString::Empty()
281  );
282 
283  SIPEndPoint * m_endpoint;
284  SubProtocol m_subProtocol;
285  SIPURL m_presenceAgentURL;
286  PURL m_defaultRootURL;
287  PString m_watcherSubscriptionAOR;
288  int m_watcherInfoVersion;
289  PString m_publishedTupleId;
290 
291  typedef std::map<PString, PString> StringMap;
292  StringMap m_watcherAorById;
293  StringMap m_presenceIdByAor;
294  StringMap m_presenceAorById;
295  StringMap m_authorisationIdByAor;
296 
297  private:
298  void operator=(const SIP_Presentity &) { }
299 };
300 
301 
302 #endif // OPAL_SIP_PRESENCE
303 
304 #endif // OPAL_SIP_SIPPRES_H