Mixe for Privacy and Anonymity in the Internet
TermsAndConditions.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) The JAP-Team, JonDos GmbH
00003 
00004 All rights reserved.
00005 
00006 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright notice,
00010        this list of conditions and the following disclaimer in the documentation and/or
00011        other materials provided with the distribution.
00012     * Neither the name of the University of Technology Dresden, Germany, nor the name of
00013        the JonDos GmbH, nor the names of their contributors may be used to endorse or
00014        promote products derived from this software without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00017 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00018 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00019 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
00020 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00021 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00022 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00023 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 */
00028 #include "StdAfx.h"
00029 #include "TermsAndConditions.hpp"
00030 #include "CAMsg.hpp"
00031 #include "CACmdLnOptions.hpp"
00032 #include "CAUtil.hpp"
00033 
00042 void cleanupTnCMixAnswer(termsAndConditionMixAnswer_t *answer)
00043 {
00044   if( (answer != NULL) )
00045   {
00046     if((answer->xmlAnswer != NULL))
00047     {
00048       answer->xmlAnswer->release();
00049       answer->xmlAnswer = NULL;
00050     }
00051     answer->result = TC_UNFINISHED;
00052   }
00053 }
00054 
00055 void cleanupTnCTranslation(termsAndConditionsTranslation_t *tnCTranslation)
00056 {
00057   if(tnCTranslation != NULL)
00058   {
00059     tnCTranslation->tnc_id = NULL;
00060     tnCTranslation->tnc_template = NULL;
00061     if(tnCTranslation->tnc_customized != NULL)
00062     {
00063       tnCTranslation->tnc_customized->release();
00064     }
00065     tnCTranslation->tnc_customized = NULL;
00066     delete [] tnCTranslation->tnc_locale;
00067     tnCTranslation->tnc_locale = NULL;
00068   }
00069 }
00070 
00074 TermsAndConditions::TermsAndConditions(UINT8* id, UINT32 nrOfTranslations)
00075 {
00076   customizedSectionsOwner = createDOMDocument();
00077   /* The id of the Terms & Conditions is the operator ski. */
00078   if(id != NULL)
00079   {
00080     size_t idlen = strlen((char *) id);
00081     tnc_id = new UINT8[idlen+1];
00082     memset(tnc_id, 0, (idlen+1));
00083     memcpy(tnc_id, id, idlen);
00084   }
00085   else
00086   {
00087     tnc_id = NULL;
00088   }
00089 
00090   translations = (nrOfTranslations == 0) ? 1 : nrOfTranslations;
00091   currentTranslationIndex = 0;
00092   allTranslations = new termsAndConditionsTranslation_t *[translations];
00093   for (UINT32 i = 0; i < translations; i++)
00094   {
00095     allTranslations[i] = NULL;
00096   }
00097   synchLock = new CAMutex();
00098 }
00099 
00100 TermsAndConditions::~TermsAndConditions()
00101 {
00102   delete synchLock;
00103   synchLock = NULL;
00104   for (UINT32 i = 0; i < translations; i++)
00105   {
00106     if(allTranslations[i] != NULL)
00107     {
00108       cleanupTnCTranslation(allTranslations[i]);
00109       delete allTranslations[i];
00110       allTranslations[i] = NULL;
00111     }
00112   }
00113   delete [] allTranslations;
00114   allTranslations = NULL;
00115   delete [] tnc_id;
00116   tnc_id = NULL;
00117   translations =  0;
00118   currentTranslationIndex = 0;
00119   customizedSectionsOwner->release();
00120   customizedSectionsOwner = NULL;
00121 }
00122 
00128 const termsAndConditionsTranslation_t *TermsAndConditions::getTranslation(const UINT8 *locale)
00129 {
00130   if(locale == NULL)
00131   {
00132     return NULL;
00133   }
00134   const termsAndConditionsTranslation_t *foundEntry = NULL;
00135   for (UINT32 i = 0; i < translations; i++)
00136   {
00137     if(allTranslations[i] != NULL)
00138     {
00139       if( strncasecmp((const char *) locale, (char *) allTranslations[i]->tnc_locale, 2) == 0 )
00140       {
00141         foundEntry = allTranslations[i];
00142         break;
00143       }
00144     }
00145   }
00146   return (const termsAndConditionsTranslation_t *) foundEntry;
00147 }
00148 
00153 const DOMNode *TermsAndConditions::getTranslationTemplate(const UINT8 *locale)
00154 {
00155   const termsAndConditionsTranslation_t *foundEntry = getTranslation(locale);
00156   if(foundEntry != NULL)
00157   {
00158     return foundEntry->tnc_template;
00159   }
00160   return NULL;
00161 }
00162 
00167 const DOMNode *TermsAndConditions::getTranslationCustomizedSections(const UINT8 *locale)
00168 {
00169   const termsAndConditionsTranslation_t *foundEntry = getTranslation(locale);
00170   if(foundEntry != NULL)
00171   {
00172     return (const DOMNode *) foundEntry->tnc_customized;
00173   }
00174   return NULL;
00175 }
00176 
00181 void TermsAndConditions::addTranslation(const UINT8* locale, DOMNode *tnc_customized, DOMNode *tnc_template)
00182 {
00183   if(locale == NULL)
00184   {
00185     return;
00186   }
00187   termsAndConditionsTranslation_t *newEntry = (termsAndConditionsTranslation_t *) getTranslation(locale);
00188   if(newEntry == NULL)
00189   {
00190     newEntry = new termsAndConditionsTranslation_t;
00191     //import the customized sections to the internal T & C document to ensure it is not
00192     //released by it's former owner document.
00193     newEntry->tnc_customized = customizedSectionsOwner->importNode(tnc_customized, true);
00194     newEntry->tnc_template = tnc_template;
00195     newEntry->tnc_id = tnc_id;
00196     newEntry->tnc_locale = new UINT8[TMP_LOCALE_SIZE];
00197     memset(newEntry->tnc_locale, 0, TMP_LOCALE_SIZE);
00198     memcpy(newEntry->tnc_locale, locale, TMP_LOCALE_SIZE);
00199 
00200     if(currentTranslationIndex == translations)
00201     {
00202       //allocate new storage space.
00203       termsAndConditionsTranslation_t **newAllocatedSpace = new termsAndConditionsTranslation_t *[translations+RESIZE_STORE];
00204       memset(newAllocatedSpace, 0, (sizeof(termsAndConditionsTranslation_t *)*(translations+RESIZE_STORE)) );
00205       memcpy(newAllocatedSpace, allTranslations,
00206           (sizeof(termsAndConditionsTranslation_t *)*translations) );
00207       delete [] allTranslations;
00208       allTranslations = newAllocatedSpace;
00209       translations += RESIZE_STORE;
00210     }
00211     allTranslations[currentTranslationIndex] = newEntry;
00212     CAMsg::printMsg(LOG_DEBUG,"Adding translation [%s] for t&c %s to index %u\n", newEntry->tnc_locale, newEntry->tnc_id, currentTranslationIndex);
00213     setIndexToNextEmptySlot();
00214   }
00215   else
00216   {
00217     newEntry->tnc_customized->release();
00218     //same as above: avoid release by the former owner document of this node.
00219     newEntry->tnc_customized = customizedSectionsOwner->importNode(tnc_customized, true);
00220     newEntry->tnc_template = tnc_template;
00221   }
00222 }
00223 
00228 void TermsAndConditions::removeTranslation(const UINT8 *locale)
00229 {
00230   for (UINT32 i = 0; i < translations; i++)
00231   {
00232     if(allTranslations[i] != NULL)
00233     {
00234       if( strncasecmp((const char *) locale, (char *) allTranslations[i]->tnc_locale, 2) == 0 )
00235       {
00236         cleanupTnCTranslation(allTranslations[i]);
00237         delete allTranslations[i];
00238         allTranslations[i] = NULL;
00239         break;
00240       }
00241     }
00242   }
00243   setIndexToNextEmptySlot();
00244 }
00245 
00246 const UINT8* TermsAndConditions::getID()
00247 {
00248   return (const UINT8 *)  tnc_id;
00249 }
00250 
00251 
00252 void TermsAndConditions::setIndexToNextEmptySlot()
00253 {
00254   for (UINT32 i = 0; i < translations; i++)
00255   {
00256     if(allTranslations[i] == NULL)
00257     {
00258       currentTranslationIndex = i;
00259       return;
00260     }
00261   }
00262   /* Nothing is free */
00263   currentTranslationIndex = translations;
00264 }