gloox 1.0
|
00001 /* 00002 Copyright (c) 2006-2009 by Jakob Schroeter <js@camaya.net> 00003 This file is part of the gloox library. http://camaya.net/gloox 00004 00005 This software is distributed under a license. The full license 00006 agreement can be found in the file LICENSE in this distribution. 00007 This software may not be copied, modified, sold or distributed 00008 other than expressed in the named license agreement. 00009 00010 This software is distributed without any warranty. 00011 */ 00012 00013 00014 #include "amp.h" 00015 #include "tag.h" 00016 #include "util.h" 00017 00018 namespace gloox 00019 { 00020 00021 static const char* conditionValues[] = 00022 { 00023 "deliver", "expire-at", "match-resource" 00024 }; 00025 00026 static const char* actionValues[] = 00027 { 00028 "alert", "error", "drop", "notify" 00029 }; 00030 00031 static const char* deliverValues[] = 00032 { 00033 "direct", "forward", "gateway", "none", "stored" 00034 }; 00035 00036 static const char* matchResourceValues[] = 00037 { 00038 "any", "exact", "other" 00039 }; 00040 00041 static const char* statusValues[] = 00042 { 00043 "alert", "notify" 00044 }; 00045 00046 // ---- AMP::Rule ---- 00047 AMP::Rule::Rule( DeliverType deliver, ActionType action ) 00048 : m_condition( ConditionDeliver ), m_deliver( deliver ), m_action( action ) 00049 { 00050 } 00051 00052 AMP::Rule::Rule( const std::string& date, ActionType action ) 00053 : m_condition( ConditionExpireAt ), m_expireat( new std::string( date ) ), m_action( action ) 00054 { 00055 } 00056 00057 AMP::Rule::Rule( MatchResourceType match, ActionType action ) 00058 : m_condition( ConditionMatchResource ), m_matchresource( match ), m_action( action ) 00059 { 00060 } 00061 00062 AMP::Rule::Rule( const std::string& condition, const std::string& action, 00063 const std::string& value ) 00064 { 00065 m_condition = (ConditionType)util::lookup( condition, conditionValues ); 00066 m_action = (ActionType)util::lookup( action, actionValues ); 00067 switch( m_condition ) 00068 { 00069 case ConditionDeliver: 00070 m_deliver = (DeliverType)util::lookup( value, deliverValues ); 00071 break; 00072 case ConditionExpireAt: 00073 m_expireat = new std::string( value ); 00074 break; 00075 case ConditionMatchResource: 00076 m_matchresource = (MatchResourceType)util::lookup( value, matchResourceValues ); 00077 break; 00078 default: 00079 case ConditionInvalid: // shouldn't happen 00080 break; 00081 } 00082 } 00083 00084 AMP::Rule::~Rule() 00085 { 00086 if( m_condition == ConditionExpireAt && m_expireat ) 00087 delete m_expireat; 00088 } 00089 00090 Tag* AMP::Rule::tag() const 00091 { 00092 if( m_condition == ConditionInvalid || m_action == ActionInvalid 00093 || ( m_condition == ConditionDeliver && m_deliver == DeliverInvalid ) 00094 || ( m_condition == ConditionMatchResource && m_matchresource == MatchResourceInvalid ) 00095 || ( m_condition == ConditionExpireAt && !m_expireat ) ) 00096 return 0; 00097 00098 Tag* rule = new Tag( "rule" ); 00099 rule->addAttribute( "condition", util::lookup( m_condition, conditionValues ) ); 00100 rule->addAttribute( "action", util::lookup( m_action, actionValues ) ); 00101 00102 switch( m_condition ) 00103 { 00104 case ConditionDeliver: 00105 rule->addAttribute( "value", util::lookup( m_deliver, deliverValues ) ); 00106 break; 00107 case ConditionExpireAt: 00108 rule->addAttribute( "value", *m_expireat ); 00109 break; 00110 case ConditionMatchResource: 00111 rule->addAttribute( "value", util::lookup( m_matchresource, matchResourceValues ) ); 00112 break; 00113 default: 00114 break; 00115 } 00116 return rule; 00117 } 00118 // ---- AMP::Rule ---- 00119 00120 // ---- AMP ---- 00121 AMP::AMP( bool perhop ) 00122 : StanzaExtension( ExtAMP ), m_perhop( perhop ), m_status( StatusInvalid ) 00123 { 00124 m_valid = true; 00125 } 00126 00127 AMP::AMP( const Tag* tag ) 00128 : StanzaExtension( ExtAMP ), m_perhop( false ) 00129 { 00130 if( !tag || tag->name() != "amp" || tag->xmlns() != XMLNS_AMP ) 00131 return; 00132 00133 const ConstTagList& rules = tag->findTagList( "/amp/rule" ); 00134 ConstTagList::const_iterator it = rules.begin(); 00135 for( ; it != rules.end(); ++it ) 00136 { 00137 m_rules.push_back( new Rule( (*it)->findAttribute( "condition" ), 00138 (*it)->findAttribute( "action" ), 00139 (*it)->findAttribute( "value" ) ) ); 00140 } 00141 00142 m_from = tag->findAttribute( "from" ); 00143 m_to = tag->findAttribute( "to" ); 00144 m_status = (Status)util::lookup( tag->findAttribute( "status" ), statusValues ); 00145 if( tag->hasAttribute( "per-hop", "true" ) || tag->hasAttribute( "per-hop", "1" ) ) 00146 m_perhop = true; 00147 m_valid = true; 00148 } 00149 00150 AMP::~AMP() 00151 { 00152 util::clearList( m_rules ); 00153 } 00154 00155 void AMP::addRule( const Rule* rule ) 00156 { 00157 if( rule ) 00158 m_rules.push_back( rule ); 00159 } 00160 00161 const std::string& AMP::filterString() const 00162 { 00163 static const std::string filter = "/message/amp[@xmlns='" + XMLNS_AMP + "']"; 00164 return filter; 00165 } 00166 00167 Tag* AMP::tag() const 00168 { 00169 if( !m_valid || !m_rules.size() ) 00170 return 0; 00171 00172 Tag* amp = new Tag( "amp" ); 00173 amp->setXmlns( XMLNS_AMP ); 00174 if( m_from ) 00175 amp->addAttribute( "from", m_from.full() ); 00176 if( m_to ) 00177 amp->addAttribute( "to", m_to.full() ); 00178 if( m_status != StatusInvalid ) 00179 amp->addAttribute( "status", util::lookup( m_status, statusValues ) ); 00180 if( m_perhop ) 00181 amp->addAttribute( "per-hop", "true" ); 00182 RuleList::const_iterator it = m_rules.begin(); 00183 for( ; it != m_rules.end(); ++it ) 00184 amp->addChild( (*it)->tag() ); 00185 00186 return amp; 00187 } 00188 00189 }