glooxd  0.3-svn
rostermanager.cpp
1 /*
2  Copyright (c) 2009 by Jakob Schroeter <js@camaya.net>
3  This file is part of the glooxd library. http://camaya.net/glooxd
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 #include "rosterprovider.h"
15 #include "rostermanager.h"
16 #include "taghandler.h"
17 
18 #include <gloox/tag.h>
19 
20 #include <cstdio>
21 
22 namespace glooxd
23 {
24 
26  RosterProvider* rp )
27  : Plugin( sm, router ), m_handler( rp )
28  {
29  }
30 
32  {
33  }
34 
35  const std::string& RosterManager::filterString() const
36  {
37  static const std::string filter = "/iq[/iq/query[@xmlns='" + gloox::XMLNS_ROSTER + "']]"
38  "|/presence[@type='subscribe']"
39  "|/presence[@type='unsubscribe']"
40  "|/presence[@type='subscribed']"
41  "|/presence[@type='unsubscribed']";
42  return filter;
43  }
44 
45  const gloox::TagList RosterManager::getRoster( const gloox::JID& jid )
46  {
47  gloox::TagList tl;
48  if( m_handler )
49  {
50  const Roster& rm = m_handler->getRoster( jid );
51  Roster::const_iterator it = rm.begin();
52  for( ; it != rm.end(); ++it )
53  {
54  gloox::Tag* i = new gloox::Tag( "item" );
55  i->addAttribute( "jid", (*it).first );
56  switch( (*it).second )
57  {
58  case gloox::S10nBoth:
59  i->addAttribute( "subscription", "both" );
60  break;
61  case gloox::S10nNone:
62  i->addAttribute( "subscription", "none" );
63  break;
64  case gloox::S10nFrom:
65  i->addAttribute( "subscription", "from" );
66  break;
67  case gloox::S10nTo:
68  i->addAttribute( "subscription", "to" );
69  break;
70  default:
71  break;
72  }
73  tl.push_back( i );
74  }
75  }
76  return tl;
77  }
78 
79  bool RosterManager::handleTag( const gloox::Tag* tag )
80  {
81  if( !tag )
82  return false;
83 
84  printf( "received a roszter tag!!!!!!!!!! %s\n", tag->xml().c_str() );
85 
86  if( tag->hasAttribute( gloox::TYPE, "get" ) )
87  {
88  gloox::Tag* re = new gloox::Tag( "iq" );
89  re->addAttribute( "id", tag->findAttribute( "id" ) );
90  re->addAttribute( "to", tag->findAttribute( "from" ) );
91  re->addAttribute( gloox::TYPE, "result" );
92  gloox::Tag* q = new gloox::Tag( re, "query" );
93  q->setXmlns( gloox::XMLNS_ROSTER );
94  const gloox::TagList& r = getRoster( tag->findAttribute( "from" ) );
95  gloox::TagList::const_iterator it = r.begin();
96  for( ; it != r.end(); ++it )
97  q->addChild( (*it) );
98 
99  m_router.handleIncomingTag( re );
100  return true;
101  }
102 
103  return false;
104  }
105 
106 }