gloox  1.0.28
amp.cpp
1 /*
2  Copyright (c) 2006-2023 by Jakob Schröter <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 = static_cast<ConditionType>( util::lookup( condition, conditionValues ) );
66  m_action = static_cast<ActionType>( util::lookup( action, actionValues ) );
67  switch( m_condition )
68  {
69  case ConditionDeliver:
70  m_deliver = static_cast<DeliverType>( util::lookup( value, deliverValues ) );
71  break;
72  case ConditionExpireAt:
73  m_expireat = new std::string( value );
74  break;
76  m_matchresource = static_cast<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 = static_cast<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 }
Rule(DeliverType deliver, ActionType action)
Definition: amp.cpp:47
Tag * tag() const
Definition: amp.cpp:90
ActionType
Definition: amp.h:58
@ ActionInvalid
Definition: amp.h:64
const RuleList & rules() const
Definition: amp.h:200
void addRule(const Rule *rule)
Definition: amp.cpp:155
AMP(bool perhop=false)
Definition: amp.cpp:121
Status
Definition: amp.h:104
@ StatusInvalid
Definition: amp.h:107
ConditionType
Definition: amp.h:47
@ ConditionDeliver
Definition: amp.h:48
@ ConditionInvalid
Definition: amp.h:51
@ ConditionExpireAt
Definition: amp.h:49
@ ConditionMatchResource
Definition: amp.h:50
virtual const std::string & filterString() const
Definition: amp.cpp:161
MatchResourceType
Definition: amp.h:90
@ MatchResourceInvalid
Definition: amp.h:97
virtual ~AMP()
Virtual Destructor.
Definition: amp.cpp:150
DeliverType
Definition: amp.h:71
@ DeliverInvalid
Definition: amp.h:83
virtual Tag * tag() const
Definition: amp.cpp:167
const std::string & full() const
Definition: jid.h:61
This class abstracts a stanza extension, which is usually an XML child element in a specific namespac...
This is an abstraction of an XML element.
Definition: tag.h:47
const std::string xmlns() const
Definition: tag.cpp:543
bool addAttribute(Attribute *attr)
Definition: tag.cpp:354
ConstTagList findTagList(const std::string &expression) const
Definition: tag.cpp:811
void addChild(Tag *child)
Definition: tag.cpp:424
bool hasAttribute(const std::string &name, const std::string &value=EmptyString) const
Definition: tag.cpp:602
const std::string & findAttribute(const std::string &name) const
Definition: tag.cpp:589
const std::string & name() const
Definition: tag.h:394
bool setXmlns(const std::string &xmlns, const std::string &prefix=EmptyString)
Definition: tag.cpp:522
void clearList(std::list< T * > &L)
Definition: util.h:152
The namespace for the gloox library.
Definition: adhoc.cpp:28
std::list< const Tag * > ConstTagList
Definition: tag.h:36
const std::string XMLNS_AMP
Definition: gloox.cpp:31