gloox  1.0.9
amp.cpp
1 /*
2  Copyright (c) 2006-2013 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 #include "amp.h"
15 #include "tag.h"
16 #include "util.h"
17 
18 namespace gloox
19 {
20 
21  static const char* conditionValues[] =
22  {
23  "deliver", "expire-at", "match-resource"
24  };
25 
26  static const char* actionValues[] =
27  {
28  "alert", "error", "drop", "notify"
29  };
30 
31  static const char* deliverValues[] =
32  {
33  "direct", "forward", "gateway", "none", "stored"
34  };
35 
36  static const char* matchResourceValues[] =
37  {
38  "any", "exact", "other"
39  };
40 
41  static const char* statusValues[] =
42  {
43  "alert", "notify"
44  };
45 
46  // ---- AMP::Rule ----
48  : m_condition( ConditionDeliver ), m_deliver( deliver ), m_action( action )
49  {
50  }
51 
52  AMP::Rule::Rule( const std::string& date, ActionType action )
53  : m_condition( ConditionExpireAt ), m_expireat( new std::string( date ) ), m_action( action )
54  {
55  }
56 
58  : m_condition( ConditionMatchResource ), m_matchresource( match ), m_action( action )
59  {
60  }
61 
62  AMP::Rule::Rule( const std::string& condition, const std::string& action,
63  const std::string& value )
64  {
65  m_condition = (ConditionType)util::lookup( condition, conditionValues );
66  m_action = (ActionType)util::lookup( action, actionValues );
67  switch( m_condition )
68  {
69  case ConditionDeliver:
70  m_deliver = (DeliverType)util::lookup( value, deliverValues );
71  break;
72  case ConditionExpireAt:
73  m_expireat = new std::string( value );
74  break;
76  m_matchresource = (MatchResourceType)util::lookup( value, matchResourceValues );
77  break;
78  default:
79  case ConditionInvalid: // shouldn't happen
80  break;
81  }
82  }
83 
85  {
86  if( m_condition == ConditionExpireAt && m_expireat )
87  delete m_expireat;
88  }
89 
91  {
92  if( m_condition == ConditionInvalid || m_action == ActionInvalid
93  || ( m_condition == ConditionDeliver && m_deliver == DeliverInvalid )
94  || ( m_condition == ConditionMatchResource && m_matchresource == MatchResourceInvalid )
95  || ( m_condition == ConditionExpireAt && !m_expireat ) )
96  return 0;
97 
98  Tag* rule = new Tag( "rule" );
99  rule->addAttribute( "condition", util::lookup( m_condition, conditionValues ) );
100  rule->addAttribute( "action", util::lookup( m_action, actionValues ) );
101 
102  switch( m_condition )
103  {
104  case ConditionDeliver:
105  rule->addAttribute( "value", util::lookup( m_deliver, deliverValues ) );
106  break;
107  case ConditionExpireAt:
108  rule->addAttribute( "value", *m_expireat );
109  break;
111  rule->addAttribute( "value", util::lookup( m_matchresource, matchResourceValues ) );
112  break;
113  default:
114  break;
115  }
116  return rule;
117  }
118  // ---- AMP::Rule ----
119 
120  // ---- AMP ----
121  AMP::AMP( bool perhop )
122  : StanzaExtension( ExtAMP ), m_perhop( perhop ), m_status( StatusInvalid )
123  {
124  m_valid = true;
125  }
126 
127  AMP::AMP( const Tag* tag )
128  : StanzaExtension( ExtAMP ), m_perhop( false )
129  {
130  if( !tag || tag->name() != "amp" || tag->xmlns() != XMLNS_AMP )
131  return;
132 
133  const ConstTagList& rules = tag->findTagList( "/amp/rule" );
134  ConstTagList::const_iterator it = rules.begin();
135  for( ; it != rules.end(); ++it )
136  {
137  m_rules.push_back( new Rule( (*it)->findAttribute( "condition" ),
138  (*it)->findAttribute( "action" ),
139  (*it)->findAttribute( "value" ) ) );
140  }
141 
142  m_from = tag->findAttribute( "from" );
143  m_to = tag->findAttribute( "to" );
144  m_status = (Status)util::lookup( tag->findAttribute( "status" ), statusValues );
145  if( tag->hasAttribute( "per-hop", "true" ) || tag->hasAttribute( "per-hop", "1" ) )
146  m_perhop = true;
147  m_valid = true;
148  }
149 
151  {
152  util::clearList( m_rules );
153  }
154 
155  void AMP::addRule( const Rule* rule )
156  {
157  if( rule )
158  m_rules.push_back( rule );
159  }
160 
161  const std::string& AMP::filterString() const
162  {
163  static const std::string filter = "/message/amp[@xmlns='" + XMLNS_AMP + "']";
164  return filter;
165  }
166 
167  Tag* AMP::tag() const
168  {
169  if( !m_valid || !m_rules.size() )
170  return 0;
171 
172  Tag* amp = new Tag( "amp" );
173  amp->setXmlns( XMLNS_AMP );
174  if( m_from )
175  amp->addAttribute( "from", m_from.full() );
176  if( m_to )
177  amp->addAttribute( "to", m_to.full() );
178  if( m_status != StatusInvalid )
179  amp->addAttribute( "status", util::lookup( m_status, statusValues ) );
180  if( m_perhop )
181  amp->addAttribute( "per-hop", "true" );
182  RuleList::const_iterator it = m_rules.begin();
183  for( ; it != m_rules.end(); ++it )
184  amp->addChild( (*it)->tag() );
185 
186  return amp;
187  }
188 
189 }