gloox  0.9.9.12
privacymanager.cpp
1 /*
2  Copyright (c) 2005-2008 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 
15 #include "privacymanager.h"
16 #include "clientbase.h"
17 
18 #ifndef _WIN32_WCE
19 # include <sstream>
20 #endif
21 
22 namespace gloox
23 {
24 
26  : m_parent( parent ), m_privacyListHandler( 0 )
27  {
28  if( m_parent )
29  m_parent->registerIqHandler( this, XMLNS_PRIVACY );
30  }
31 
33  {
34  if( m_parent )
35  {
36  m_parent->removeIqHandler( XMLNS_PRIVACY );
37  m_parent->removeIDHandler( this );
38  }
39  }
40 
42  {
43  const std::string& id = m_parent->getID();
44 
45  Tag *iq = new Tag( "iq" );
46  iq->addAttribute( "type", "get" );
47  iq->addAttribute( "id", id );
48  Tag *q = new Tag( iq, "query" );
49  q->addAttribute( "xmlns", XMLNS_PRIVACY );
50 
51  m_parent->trackID( this, id, PLRequestNames );
52  m_parent->send( iq );
53  return id;
54  }
55 
56  std::string PrivacyManager::requestList( const std::string& name )
57  {
58  const std::string& id = m_parent->getID();
59 
60  Tag *iq = new Tag( "iq" );
61  iq->addAttribute( "type", "get" );
62  iq->addAttribute( "id", id );
63  Tag *q = new Tag( iq, "query" );
64  q->addAttribute( "xmlns", XMLNS_PRIVACY );
65  Tag *l = new Tag( q, "list" );
66  l->addAttribute( "name", name );
67 
68  m_parent->trackID( this, id, PLRequestList );
69  m_parent->send( iq );
70  return id;
71  }
72 
73  std::string PrivacyManager::removeList( const std::string& name )
74  {
75  const std::string& id = m_parent->getID();
76 
77  Tag *iq = new Tag( "iq" );
78  iq->addAttribute( "type", "set" );
79  iq->addAttribute( "id", id );
80  Tag *q = new Tag( iq, "query" );
81  q->addAttribute( "xmlns", XMLNS_PRIVACY );
82  Tag *l = new Tag( q, "list" );
83  l->addAttribute( "name", name );
84 
85  m_parent->trackID( this, id, PLRemove );
86  m_parent->send( iq );
87  return id;
88  }
89 
90  std::string PrivacyManager::setDefault( const std::string& name )
91  {
92  const std::string& id = m_parent->getID();
93 
94  Tag *iq = new Tag( "iq" );
95  iq->addAttribute( "type", "set" );
96  iq->addAttribute( "id", id );
97  Tag *q = new Tag( iq, "query" );
98  q->addAttribute( "xmlns", XMLNS_PRIVACY );
99  Tag *d = new Tag( q, "default" );
100  d->addAttribute( "name", name );
101 
102  m_parent->trackID( this, id, PLDefault );
103  m_parent->send( iq );
104  return id;
105  }
106 
108  {
109  const std::string& id = m_parent->getID();
110 
111  Tag *iq = new Tag( "iq" );
112  iq->addAttribute( "type", "set" );
113  iq->addAttribute( "id", id );
114  Tag *q = new Tag( iq, "query" );
115  q->addAttribute( "xmlns", XMLNS_PRIVACY );
116  new Tag( q, "default" );
117 
118  m_parent->trackID( this, id, PLUnsetDefault );
119  m_parent->send( iq );
120  return id;
121  }
122 
123  std::string PrivacyManager::setActive( const std::string& name )
124  {
125  const std::string& id = m_parent->getID();
126 
127  Tag *iq = new Tag( "iq" );
128  iq->addAttribute( "type", "set" );
129  iq->addAttribute( "id", id );
130  Tag *q = new Tag( iq, "query" );
131  q->addAttribute( "xmlns", XMLNS_PRIVACY );
132  Tag *a = new Tag( q, "active" );
133  a->addAttribute( "name", name );
134 
135  m_parent->trackID( this, id, PLActivate );
136  m_parent->send( iq );
137  return id;
138  }
139 
141  {
142  const std::string& id = m_parent->getID();
143 
144  Tag *iq = new Tag( "iq" );
145  iq->addAttribute( "type", "set" );
146  iq->addAttribute( "id", id );
147  Tag *q = new Tag( iq, "query" );
148  q->addAttribute( "xmlns", XMLNS_PRIVACY );
149  new Tag( q, "active" );
150 
151  m_parent->trackID( this, id, PLUnsetActivate );
152  m_parent->send( iq );
153  return id;
154  }
155 
156  std::string PrivacyManager::store( const std::string& name,
157  const PrivacyListHandler::PrivacyList& list )
158  {
159  if( list.empty() )
160  return std::string();
161 
162  const std::string& id = m_parent->getID();
163 
164  Tag *iq = new Tag( "iq" );
165  iq->addAttribute( "type", "set" );
166  iq->addAttribute( "id", id );
167  Tag *q = new Tag( iq, "query" );
168  q->addAttribute( "xmlns", XMLNS_PRIVACY );
169  Tag *l = new Tag( q, "list" );
170  l->addAttribute( "name", name );
171 
172  int count = 0;
173  PrivacyListHandler::PrivacyList::const_iterator it = list.begin();
174  for( ; it != list.end(); ++it )
175  {
176  Tag *i = new Tag( l, "item" );
177 
178  switch( (*it).type() )
179  {
181  i->addAttribute( "type", "jid" );
182  break;
184  i->addAttribute( "type", "group" );
185  break;
187  i->addAttribute( "type", "subscription" );
188  break;
189  default:
190  break;
191  }
192 
193  switch( (*it).action() )
194  {
196  i->addAttribute( "action", "allow" );
197  break;
199  i->addAttribute( "action", "deny" );
200  break;
201  }
202 
203  int pType = (*it).packetType();
204  if( pType != 15 )
205  {
206  if( pType & PrivacyItem::PacketMessage )
207  new Tag( i, "message" );
208  if( pType & PrivacyItem::PacketPresenceIn )
209  new Tag( i, "presence-in" );
210  if( pType & PrivacyItem::PacketPresenceOut )
211  new Tag( i, "presence-out" );
212  if( pType & PrivacyItem::PacketIq )
213  new Tag( i, "iq" );
214  }
215 
216  i->addAttribute( "value", (*it).value() );
217  i->addAttribute( "order", ++count );
218  }
219 
220  m_parent->trackID( this, id, PLStore );
221  m_parent->send( iq );
222  return id;
223  }
224 
226  {
227  if( stanza->subtype() != StanzaIqSet || !m_privacyListHandler )
228  return false;
229 
230  Tag *l = stanza->findChild( "query" )->findChild( "list" );
231  if( l->hasAttribute( "name" ) )
232  {
233  const std::string& name = l->findAttribute( "name" );
234  m_privacyListHandler->handlePrivacyListChanged( name );
235 
236  Tag *iq = new Tag( "iq" );
237  iq->addAttribute( "type", "result" );
238  iq->addAttribute( "id", stanza->id() );
239  m_parent->send( iq );
240  return true;
241  }
242 
243  return false;
244  }
245 
246  bool PrivacyManager::handleIqID( Stanza *stanza, int context )
247  {
248  if( !m_privacyListHandler )
249  return false;
250 
251  switch( stanza->subtype() )
252  {
253  case StanzaIqResult:
254  switch( context )
255  {
256  case PLStore:
257  m_privacyListHandler->handlePrivacyListResult( stanza->id(), ResultStoreSuccess );
258  break;
259  case PLActivate:
260  m_privacyListHandler->handlePrivacyListResult( stanza->id(), ResultActivateSuccess );
261  break;
262  case PLDefault:
263  m_privacyListHandler->handlePrivacyListResult( stanza->id(), ResultDefaultSuccess );
264  break;
265  case PLRemove:
266  m_privacyListHandler->handlePrivacyListResult( stanza->id(), ResultRemoveSuccess );
267  break;
268  case PLRequestNames:
269  {
270  StringList lists;
271  std::string def;
272  std::string active;
273  Tag *q = stanza->findChild( "query" );
274  const Tag::TagList& l = q->children();
275  Tag::TagList::const_iterator it = l.begin();
276  for( ; it != l.end(); ++it )
277  {
278  const std::string& name = (*it)->findAttribute( "name" );
279  if( (*it)->name() == "default" )
280  def = name;
281  else if( (*it)->name() == "active" )
282  active = name;
283  else if( (*it)->name() == "list" )
284  lists.push_back( name );
285  }
286 
287  m_privacyListHandler->handlePrivacyListNames( def, active, lists );
288  break;
289  }
290  case PLRequestList:
291  {
293 
294  Tag *list = stanza->findChild( "query" )->findChild( "list" );
295  const std::string& name = list->findAttribute( "name" );
296  const Tag::TagList& l = list->children();
297  Tag::TagList::const_iterator it = l.begin();
298  for( ; it != l.end(); ++it )
299  {
302  int packetType = 0;
303 
304  const std::string& t = (*it)->findAttribute( "type" );
305  if( t == "jid" )
306  type = PrivacyItem::TypeJid;
307  else if( t == "group" )
308  type = PrivacyItem::TypeGroup;
309  else if( t == "subscription" )
311  else
313 
314  const std::string& a = (*it)->findAttribute( "action" );
315  if( a == "allow" )
316  action = PrivacyItem::ActionAllow;
317  else if( a == "deny" )
318  action = PrivacyItem::ActionDeny;
319  else
320  action = PrivacyItem::ActionAllow;
321 
322  const std::string& value = (*it)->findAttribute( "value" );
323 
324  const Tag::TagList& c = (*it)->children();
325  Tag::TagList::const_iterator it_c = c.begin();
326  for( ; it_c != c.end(); ++it_c )
327  {
328  if( (*it_c)->name() == "iq" )
329  packetType |= PrivacyItem::PacketIq;
330  else if( (*it_c)->name() == "presence-out" )
331  packetType |= PrivacyItem::PacketPresenceOut;
332  else if( (*it_c)->name() == "presence-in" )
333  packetType |= PrivacyItem::PacketPresenceIn;
334  else if( (*it_c)->name() == "message" )
335  packetType |= PrivacyItem::PacketMessage;
336  }
337 
338  PrivacyItem item( type, action, packetType, value );
339  items.push_back( item );
340  }
341  m_privacyListHandler->handlePrivacyList( name, items );
342  break;
343  }
344  }
345  break;
346 
347  case StanzaIqError:
348  {
349  switch( stanza->error() )
350  {
351  case StanzaErrorConflict:
352  m_privacyListHandler->handlePrivacyListResult( stanza->id(), ResultConflict );
353  break;
355  m_privacyListHandler->handlePrivacyListResult( stanza->id(), ResultItemNotFound );
356  break;
358  m_privacyListHandler->handlePrivacyListResult( stanza->id(), ResultBadRequest );
359  break;
360  default:
361  m_privacyListHandler->handlePrivacyListResult( stanza->id(), ResultUnknownError );
362  break;
363  }
364  break;
365  }
366 
367  default:
368  break;
369  }
370  return false;
371  }
372 
374  {
375  m_privacyListHandler = plh;
376  }
377 
379  {
380  m_privacyListHandler = 0;
381  }
382 
383 }