Guest User

Untitled

a guest
Feb 28th, 2022
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 40.24 KB | None | 0 0
  1. #include "otpch.h"
  2. #include "tools.h"
  3.  
  4. #include <iostream>
  5. #include <iomanip>
  6.  
  7. #include <cryptopp/sha.h>
  8. #include <cryptopp/md5.h>
  9. #include <cryptopp/adler32.h>
  10. #include <cryptopp/hmac.h>
  11.  
  12. #include <cryptopp/hex.h>
  13. #include <cryptopp/base64.h>
  14. #include <cryptopp/cryptlib.h>
  15.  
  16. #include "vocation.h"
  17. #include "configmanager.h"
  18.  
  19. extern ConfigManager g_config;
  20.  
  21. std::string transformToMD5(std::string plainText, bool upperCase)
  22. {
  23.     // Crypto++ MD5 object
  24.     CryptoPP::Weak::MD5 hash;
  25.  
  26.     // Use native byte instead of casting chars
  27.     byte digest[CryptoPP::Weak::MD5::DIGESTSIZE];
  28.  
  29.     // Do the actual calculation, require a byte value so we need a cast
  30.     hash.CalculateDigest(digest, (const byte*)plainText.c_str(), plainText.length());
  31.  
  32.     // Crypto++ HexEncoder object
  33.     CryptoPP::HexEncoder encoder;
  34.  
  35.     // Our output
  36.     std::string output;
  37.  
  38.     // Drop internal hex encoder and use this, returns uppercase by default
  39.     encoder.Attach(new CryptoPP::StringSink(output));
  40.     encoder.Put(digest, sizeof(digest));
  41.     encoder.MessageEnd();
  42.  
  43.     // Make sure we want uppercase
  44.     if(upperCase)
  45.         return output;
  46.  
  47.     // Convert to lowercase if needed
  48.     return asLowerCaseString(output);
  49. }
  50.  
  51. std::string transformToSHA1(std::string plainText, bool upperCase)
  52. {
  53.     // Crypto++ SHA1 object
  54.     CryptoPP::SHA1 hash;
  55.  
  56.     // Use native byte instead of casting chars
  57.     byte digest[CryptoPP::SHA1::DIGESTSIZE];
  58.  
  59.     // Do the actual calculation, require a byte value so we need a cast
  60.     hash.CalculateDigest(digest, (const byte*)plainText.c_str(), plainText.length());
  61.  
  62.     // Crypto++ HexEncoder object
  63.     CryptoPP::HexEncoder encoder;
  64.  
  65.     // Our output
  66.     std::string output;
  67.  
  68.     // Drop internal hex encoder and use this, returns uppercase by default
  69.     encoder.Attach(new CryptoPP::StringSink(output));
  70.     encoder.Put(digest, sizeof(digest));
  71.     encoder.MessageEnd();
  72.  
  73.     // Make sure we want uppercase
  74.     if(upperCase)
  75.         return output;
  76.  
  77.     // Convert to lowercase if needed
  78.     return asLowerCaseString(output);
  79. }
  80.  
  81. std::string transformToSHA256(std::string plainText, bool upperCase)
  82. {
  83.     // Crypto++ SHA256 object
  84.     CryptoPP::SHA256 hash;
  85.  
  86.     // Use native byte instead of casting chars
  87.     byte digest[CryptoPP::SHA256::DIGESTSIZE];
  88.  
  89.     // Do the actual calculation, require a byte value so we need a cast
  90.     hash.CalculateDigest(digest, (const byte*)plainText.c_str(), plainText.length());
  91.  
  92.     // Crypto++ HexEncoder object
  93.     CryptoPP::HexEncoder encoder;
  94.  
  95.     // Our output
  96.     std::string output;
  97.  
  98.     // Drop internal hex encoder and use this, returns uppercase by default
  99.     encoder.Attach(new CryptoPP::StringSink(output));
  100.     encoder.Put(digest, sizeof(digest));
  101.     encoder.MessageEnd();
  102.  
  103.     // Make sure we want uppercase
  104.     if(upperCase)
  105.         return output;
  106.  
  107.     // Convert to lowercase if needed
  108.     return asLowerCaseString(output);
  109. }
  110.  
  111. std::string transformToSHA512(std::string plainText, bool upperCase)
  112. {
  113.     // Crypto++ SHA512 object
  114.     CryptoPP::SHA512 hash;
  115.  
  116.     // Use native byte instead of casting chars
  117.     byte digest[CryptoPP::SHA512::DIGESTSIZE];
  118.  
  119.     // Do the actual calculation, require a byte value so we need a cast
  120.     hash.CalculateDigest(digest, (const byte*)plainText.c_str(), plainText.length());
  121.  
  122.     // Crypto++ HexEncoder object
  123.     CryptoPP::HexEncoder encoder;
  124.  
  125.     // Our output
  126.     std::string output;
  127.  
  128.     // Drop internal hex encoder and use this, returns uppercase by default
  129.     encoder.Attach(new CryptoPP::StringSink(output));
  130.     encoder.Put(digest, sizeof(digest));
  131.     encoder.MessageEnd();
  132.  
  133.     // Make sure we want uppercase
  134.     if(upperCase)
  135.         return output;
  136.  
  137.     // Convert to lowercase if needed
  138.     return asLowerCaseString(output);
  139. }
  140.  
  141. std::string transformToVAHash(std::string plainText, bool upperCase)
  142. {
  143.     std::string key = g_config.getString(ConfigManager::ENCRYPTION_KEY);
  144.     // This is basicaly a base64 string out of a sha512 lowcase string of the HMAC of the plaintext sha256 string with a configurated key
  145.     // Currently this removes all known weaknesses in the sha-2 implantation
  146.     // base64(HMAC<SHA512>(key, SHA256(plainText)));
  147.  
  148.     // Get SHA256
  149.     std::string sha256 = transformToSHA256(plainText, false);
  150.  
  151.     // This holds the HMAC
  152.     // Use native byte instead of casting chars
  153.     byte digest[CryptoPP::SHA512::DIGESTSIZE];
  154.  
  155.     // Do the actual calculation and setup, require a byte value so we need a cast on the key and the input
  156.     CryptoPP::HMAC<CryptoPP::SHA512>((const byte*)key.c_str(), key.length()).CalculateDigest(
  157.         digest, (const byte*)sha256.c_str(), CryptoPP::SHA256::DIGESTSIZE);
  158.  
  159.     // Crypto++ Base64Encoder object
  160.     CryptoPP::Base64Encoder encoder;
  161.  
  162.     // Our output
  163.     std::string output;
  164.  
  165.     // Encode to base64
  166.     encoder.Attach(new CryptoPP::StringSink(output));
  167.     encoder.Put(digest, sizeof(digest));
  168.     encoder.MessageEnd();
  169.  
  170.     // Make sure we want uppercase
  171.     if(upperCase)
  172.         return output;
  173.  
  174.     // Convert to lowercase if needed
  175.     return asLowerCaseString(output);
  176. }
  177.  
  178. void _encrypt(std::string& str, bool upperCase)
  179. {
  180.     switch(g_config.getNumber(ConfigManager::ENCRYPTION))
  181.     {
  182.         case ENCRYPTION_MD5:
  183.             str = transformToMD5(str, upperCase);
  184.             break;
  185.         case ENCRYPTION_SHA1:
  186.             str = transformToSHA1(str, upperCase);
  187.             break;
  188.         case ENCRYPTION_SHA256:
  189.             str = transformToSHA256(str, upperCase);
  190.             break;
  191.         case ENCRYPTION_SHA512:
  192.             str = transformToSHA512(str, upperCase);
  193.             break;
  194.         case ENCRYPTION_VAHASH:
  195.             str = transformToVAHash(str, upperCase);
  196.             break;
  197.         default:
  198.         {
  199.             if(upperCase)
  200.                 std::transform(str.begin(), str.end(), str.begin(), upchar);
  201.  
  202.             break;
  203.         }
  204.     }
  205. }
  206.  
  207. bool encryptTest(std::string plain, std::string& hash)
  208. {
  209.     std::transform(hash.begin(), hash.end(), hash.begin(), upchar);
  210.     _encrypt(plain, true);
  211.     return plain == hash;
  212. }
  213.  
  214. bool replaceString(std::string& text, const std::string& key, const std::string& value)
  215. {
  216.     if(text.find(key) == std::string::npos)
  217.         return false;
  218.  
  219.     std::string::size_type start = 0, pos = 0;
  220.     while((start = text.find(key, pos)) != std::string::npos)
  221.     {
  222.         text.replace(start, key.size(), value);
  223.         //text = text.substr(0, start) + value + text.substr(start + key.size());
  224.         pos = start + value.size();
  225.     }
  226.  
  227.     return true;
  228. }
  229.  
  230. void trim_right(std::string& source, const std::string& t)
  231. {
  232.     source.erase(source.find_last_not_of(t) + 1);
  233. }
  234.  
  235. void trim_left(std::string& source, const std::string& t)
  236. {
  237.     source.erase(0, source.find_first_not_of(t));
  238. }
  239.  
  240. void toLowerCaseString(std::string& source)
  241. {
  242.     std::transform(source.begin(), source.end(), source.begin(), tolower);
  243. }
  244.  
  245. void toUpperCaseString(std::string& source)
  246. {
  247.     std::transform(source.begin(), source.end(), source.begin(), upchar);
  248. }
  249.  
  250. std::string asLowerCaseString(const std::string& source)
  251. {
  252.     std::string s = source;
  253.     toLowerCaseString(s);
  254.     return s;
  255. }
  256.  
  257. std::string asUpperCaseString(const std::string& source)
  258. {
  259.     std::string s = source;
  260.     toUpperCaseString(s);
  261.     return s;
  262. }
  263.  
  264. bool booleanString(std::string source)
  265. {
  266.     toLowerCaseString(source);
  267.     return (source == "yes" || source == "true" || atoi(source.c_str()) > 0);
  268. }
  269.  
  270. bool readXMLInteger(xmlNodePtr node, const char* tag, int32_t& value)
  271. {
  272.     char* nodeValue = (char*)xmlGetProp(node, (xmlChar*)tag);
  273.     if(!nodeValue)
  274.         return false;
  275.  
  276.     value = atoi(nodeValue);
  277.     xmlFree(nodeValue);
  278.     return true;
  279. }
  280.  
  281. bool readXMLInteger64(xmlNodePtr node, const char* tag, int64_t& value)
  282. {
  283.     char* nodeValue = (char*)xmlGetProp(node, (xmlChar*)tag);
  284.     if(!nodeValue)
  285.         return false;
  286.  
  287.     value = atoll(nodeValue);
  288.     xmlFree(nodeValue);
  289.     return true;
  290. }
  291.  
  292. bool readXMLFloat(xmlNodePtr node, const char* tag, float& value)
  293. {
  294.     char* nodeValue = (char*)xmlGetProp(node, (xmlChar*)tag);
  295.     if(!nodeValue)
  296.         return false;
  297.  
  298.     value = atof(nodeValue);
  299.     xmlFree(nodeValue);
  300.     return true;
  301. }
  302.  
  303. bool readXMLString(xmlNodePtr node, const char* tag, std::string& value)
  304. {
  305.     char* nodeValue = (char*)xmlGetProp(node, (xmlChar*)tag);
  306.     if(!nodeValue)
  307.         return false;
  308.  
  309.     if(!utf8ToLatin1(nodeValue, value))
  310.         value = nodeValue;
  311.  
  312.     xmlFree(nodeValue);
  313.     return true;
  314. }
  315.  
  316. bool readXMLContentString(xmlNodePtr node, std::string& value)
  317. {
  318.     char* nodeValue = (char*)xmlNodeGetContent(node);
  319.     if(!nodeValue)
  320.         return false;
  321.  
  322.     if(!utf8ToLatin1(nodeValue, value))
  323.         value = nodeValue;
  324.  
  325.     xmlFree(nodeValue);
  326.     return true;
  327. }
  328.  
  329. bool parseXMLContentString(xmlNodePtr node, std::string& value)
  330. {
  331.     bool result = false;
  332.     std::string compareValue;
  333.     while(node)
  334.     {
  335.         if(xmlStrcmp(node->name, (const xmlChar*)"text") && node->type != XML_CDATA_SECTION_NODE)
  336.         {
  337.             node = node->next;
  338.             continue;
  339.         }
  340.  
  341.         if(!readXMLContentString(node, compareValue))
  342.         {
  343.             node = node->next;
  344.             continue;
  345.         }
  346.  
  347.         trim_left(compareValue, "\r");
  348.         trim_left(compareValue, "\n");
  349.         trim_left(compareValue, " ");
  350.         if(compareValue.length() > value.length())
  351.         {
  352.             value = compareValue;
  353.             if(!result)
  354.                 result = true;
  355.         }
  356.  
  357.         node = node->next;
  358.     }
  359.  
  360.     return result;
  361. }
  362.  
  363. std::string getLastXMLError()
  364. {
  365.     std::stringstream ss;
  366.     xmlErrorPtr lastError = xmlGetLastError();
  367.     if(lastError->line)
  368.         ss << "Line: " << lastError->line << ", ";
  369.  
  370.     ss << "Info: " << lastError->message << std::endl;
  371.     return ss.str();
  372. }
  373.  
  374. bool utf8ToLatin1(char* intext, std::string& outtext)
  375. {
  376.     outtext = "";
  377.     if(!intext)
  378.         return false;
  379.  
  380.     int32_t inlen = strlen(intext);
  381.     if(!inlen)
  382.         return false;
  383.  
  384.     int32_t outlen = inlen * 2;
  385.     uint8_t* outbuf = new uint8_t[outlen];
  386.  
  387.     int32_t res = UTF8Toisolat1(outbuf, &outlen, (uint8_t*)intext, &inlen);
  388.     if(res < 0)
  389.     {
  390.         delete[] outbuf;
  391.         return false;
  392.     }
  393.  
  394.     outbuf[outlen] = '\0';
  395.     outtext = (char*)outbuf;
  396.  
  397.     delete[] outbuf;
  398.     return true;
  399. }
  400.  
  401. StringVec explodeString(const std::string& string, const std::string& separator, bool trim/* = true*/)
  402. {
  403.     StringVec returnVector;
  404.     size_t start = 0, end = 0;
  405.     while((end = string.find(separator, start)) != std::string::npos)
  406.     {
  407.         std::string t = string.substr(start, end - start);
  408.         if(trim)
  409.             trimString(t);
  410.  
  411.         returnVector.push_back(t);
  412.         start = end + separator.size();
  413.     }
  414.  
  415.     returnVector.push_back(string.substr(start));
  416.     return returnVector;
  417. }
  418.  
  419. IntegerVec vectorAtoi(StringVec stringVector)
  420. {
  421.     IntegerVec returnVector;
  422.     for(StringVec::iterator it = stringVector.begin(); it != stringVector.end(); ++it)
  423.         returnVector.push_back(atoi((*it).c_str()));
  424.  
  425.     return returnVector;
  426. }
  427.  
  428. bool hasBitSet(uint32_t flag, uint32_t flags)
  429. {
  430.     return ((flags & flag) == flag);
  431. }
  432.  
  433. int32_t round(float v)
  434. {
  435.     int32_t t = (int32_t)std::floor(v);
  436.     if((v - t) > 0.5)
  437.         return t + 1;
  438.  
  439.     return t;
  440. }
  441.  
  442. uint32_t rand24b()
  443. {
  444.     return ((rand() << 12) ^ (rand())) & (0xFFFFFF);
  445. }
  446.  
  447. float box_muller(float m, float s)
  448. {
  449.     // normal random variate generator
  450.     // mean m, standard deviation s
  451.     float x1, x2, w, y1;
  452.     static float y2;
  453.  
  454.     static bool useLast = false;
  455.     if(useLast) // use value from previous call
  456.     {
  457.         y1 = y2;
  458.         useLast = false;
  459.         return (m + y1 * s);
  460.     }
  461.  
  462.     do
  463.     {
  464.         double r1 = (((float)(rand()) / RAND_MAX));
  465.         double r2 = (((float)(rand()) / RAND_MAX));
  466.  
  467.         x1 = 2.0 * r1 - 1.0;
  468.         x2 = 2.0 * r2 - 1.0;
  469.         w = x1 * x1 + x2 * x2;
  470.     }
  471.     while(w >= 1.0);
  472.     w = sqrt((-2.0 * log(w)) / w);
  473.  
  474.     y1 = x1 * w;
  475.     y2 = x2 * w;
  476.  
  477.     useLast = true;
  478.     return (m + y1 * s);
  479. }
  480.  
  481. int32_t random_range(int32_t lowestNumber, int32_t highestNumber, DistributionType_t type /*= DISTRO_UNIFORM*/)
  482. {
  483.     if(highestNumber == lowestNumber)
  484.         return lowestNumber;
  485.  
  486.     if(lowestNumber > highestNumber)
  487.         std::swap(lowestNumber, highestNumber);
  488.  
  489.     switch(type)
  490.     {
  491.         case DISTRO_UNIFORM:
  492.             return (lowestNumber + ((int32_t)rand24b() % (highestNumber - lowestNumber + 1)));
  493.         case DISTRO_NORMAL:
  494.             return (lowestNumber + int32_t(float(highestNumber - lowestNumber) * (float)std::min((float)1, std::max((float)0, box_muller(0.5, 0.25)))));
  495.         default:
  496.             break;
  497.     }
  498.  
  499.     const float randMax = 16777216;
  500.     return (lowestNumber + int32_t(float(highestNumber - lowestNumber) * float(1.f - sqrt((1.f * rand24b()) / randMax))));
  501. }
  502.  
  503. char upchar(char character)
  504. {
  505.     if((character >= 97 && character <= 122) || (character <= -1 && character >= -32))
  506.         character -= 32;
  507.  
  508.     return character;
  509. }
  510.  
  511. bool isNumber(char character)
  512. {
  513.     return (character >= 48 && character <= 57);
  514. }
  515.  
  516. bool isLowercaseLetter(char character)
  517. {
  518.     return (character >= 97 && character <= 122);
  519. }
  520.  
  521. bool isUppercaseLetter(char character)
  522. {
  523.     return (character >= 65 && character <= 90);
  524. }
  525.  
  526. bool isPasswordCharacter(char character)
  527. {
  528.     return ((character >= 33 && character <= 47) || (character >= 58 && character <= 64) || (character >= 91 && character <= 96) || (character >= 123 && character <= 126));
  529. }
  530.  
  531. bool isValidAccountName(std::string text)
  532. {
  533.     toLowerCaseString(text);
  534.  
  535.     uint32_t textLength = text.length();
  536.     for(uint32_t size = 0; size < textLength; size++)
  537.     {
  538.         if(!isLowercaseLetter(text[size]) && !isNumber(text[size]))
  539.             return false;
  540.     }
  541.  
  542.     return true;
  543. }
  544.  
  545. bool isValidPassword(std::string text)
  546. {
  547.     toLowerCaseString(text);
  548.  
  549.     uint32_t textLength = text.length();
  550.     for(uint32_t size = 0; size < textLength; size++)
  551.     {
  552.         if(!isLowercaseLetter(text[size]) && !isNumber(text[size]) && !isPasswordCharacter(text[size]))
  553.             return false;
  554.     }
  555.  
  556.     return true;
  557. }
  558.  
  559. bool isValidName(std::string text, bool forceUppercaseOnFirstLetter/* = true*/)
  560. {
  561.     uint32_t textLength = text.length(), lenBeforeSpace = 1, lenBeforeQuote = 1, lenBeforeDash = 1, repeatedCharacter = 0;
  562.     char lastChar = 32;
  563.     if(forceUppercaseOnFirstLetter)
  564.     {
  565.         if(!isUppercaseLetter(text[0]))
  566.             return false;
  567.     }
  568.     else if(!isLowercaseLetter(text[0]) && !isUppercaseLetter(text[0]))
  569.         return false;
  570.  
  571.     for(uint32_t size = 1; size < textLength; size++)
  572.     {
  573.         if(text[size] != 32)
  574.         {
  575.             lenBeforeSpace++;
  576.             if(text[size] != 39)
  577.                 lenBeforeQuote++;
  578.             else
  579.             {
  580.                 if(lenBeforeQuote <= 1 || size == textLength - 1 || text[size + 1] == 32)
  581.                     return false;
  582.  
  583.                 lenBeforeQuote = 0;
  584.             }
  585.  
  586.             if(text[size] != 45)
  587.                 lenBeforeDash++;
  588.             else
  589.             {
  590.                 if(lenBeforeDash <= 1 || size == textLength - 1 || text[size + 1] == 32)
  591.                     return false;
  592.  
  593.                 lenBeforeDash = 0;
  594.             }
  595.  
  596.             if(text[size] == lastChar)
  597.             {
  598.                 repeatedCharacter++;
  599.                 if(repeatedCharacter > 2)
  600.                     return false;
  601.             }
  602.             else
  603.                 repeatedCharacter = 0;
  604.  
  605.             lastChar = text[size];
  606.         }
  607.         else
  608.         {
  609.             if(lenBeforeSpace <= 1 || size == textLength - 1 || text[size + 1] == 32)
  610.                 return false;
  611.  
  612.             lenBeforeSpace = lenBeforeQuote = lenBeforeDash = 0;
  613.         }
  614.  
  615.         if(!(isLowercaseLetter(text[size]) || text[size] == 32 || text[size] == 39 || text[size] == 45
  616.             || (isUppercaseLetter(text[size]) && text[size - 1] == 32)))
  617.             return false;
  618.     }
  619.  
  620.     return true;
  621. }
  622.  
  623. bool isNumbers(std::string text)
  624. {
  625.     uint32_t textLength = text.length();
  626.     for(uint32_t size = 0; size < textLength; size++)
  627.     {
  628.         if(!isNumber(text[size]))
  629.             return false;
  630.     }
  631.  
  632.     return true;
  633. }
  634.  
  635. bool checkText(std::string text, std::string str)
  636. {
  637.     trimString(text);
  638.     return asLowerCaseString(text) == str;
  639. }
  640.  
  641. std::string generateRecoveryKey(int32_t fieldCount, int32_t fieldLenght, bool mixCase/* = false*/)
  642. {
  643.     std::stringstream key;
  644.     int32_t i = 0, j = 0, lastNumber = 99, number = 0;
  645.  
  646.     char character = 0, lastCharacter = 0;
  647.     bool madeNumber = false, madeCharacter = false;
  648.     do
  649.     {
  650.         do
  651.         {
  652.             madeNumber = madeCharacter = false;
  653.             if((mixCase && !random_range(0, 2)) || (!mixCase && (bool)random_range(0, 1)))
  654.             {
  655.                 number = random_range(2, 9);
  656.                 if(number != lastNumber)
  657.                 {
  658.                     key << number;
  659.                     lastNumber = number;
  660.                     madeNumber = true;
  661.                 }
  662.             }
  663.             else
  664.             {
  665.                 if(mixCase && (bool)random_range(0,1) )
  666.                     character = (char)random_range(97, 122);
  667.                 else
  668.                     character = (char)random_range(65, 90);
  669.  
  670.                 if(character != lastCharacter)
  671.                 {
  672.                     key << character;
  673.                     lastCharacter = character;
  674.                     madeCharacter = true;
  675.                 }
  676.             }
  677.         }
  678.         while((!madeCharacter && !madeNumber) ? true : (++j && j < fieldLenght));
  679.         lastCharacter = character = number = j = 0;
  680.  
  681.         lastNumber = 99;
  682.         if(i < fieldCount - 1)
  683.             key << "-";
  684.     }
  685.     while(++i && i < fieldCount);
  686.     return key.str();
  687. }
  688.  
  689. std::string trimString(std::string& str)
  690. {
  691.     str.erase(str.find_last_not_of(" ") + 1);
  692.     return str.erase(0, str.find_first_not_of(" "));
  693. }
  694.  
  695. std::string parseParams(tokenizer::iterator &it, tokenizer::iterator end)
  696. {
  697.     if(it == end)
  698.         return "";
  699.  
  700.     std::string tmp = (*it);
  701.     ++it;
  702.     if(tmp[0] == '"')
  703.     {
  704.         tmp.erase(0, 1);
  705.         while(it != end && tmp[tmp.length() - 1] != '"')
  706.         {
  707.             tmp += " " + (*it);
  708.             ++it;
  709.         }
  710.  
  711.         if(tmp.length() > 0 && tmp[tmp.length() - 1] == '"')
  712.             tmp.erase(tmp.length() - 1);
  713.     }
  714.  
  715.     return tmp;
  716. }
  717.  
  718. std::string formatDate(time_t _time/* = 0*/)
  719. {
  720.     if(!_time)
  721.         _time = time(NULL);
  722.  
  723.     const tm* tms = localtime(&_time);
  724.     std::stringstream s;
  725.     if(tms)
  726.         s << tms->tm_mday << "/" << (tms->tm_mon + 1) << "/" << (tms->tm_year + 1900) << " " << tms->tm_hour << ":" << tms->tm_min << ":" << tms->tm_sec;
  727.     else
  728.         s << "UNIX Time: " << (int32_t)_time;
  729.  
  730.     return s.str();
  731. }
  732.  
  733. std::string formatDateEx(time_t _time/* = 0*/, std::string format/* = "%d %b %Y, %H:%M:%S"*/)
  734. {
  735.     if(!_time)
  736.         _time = time(NULL);
  737.  
  738.     const tm* tms = localtime(&_time);
  739.     char buffer[100];
  740.     if(tms)
  741.         strftime(buffer, 25, format.c_str(), tms);
  742.     else
  743.         sprintf(buffer, "UNIX Time: %d", (int32_t)_time);
  744.  
  745.     return buffer;
  746. }
  747.  
  748. std::string formatTime(time_t _time/* = 0*/, bool ms/* = false*/)
  749. {
  750.     if(!_time)
  751.         _time = time(NULL);
  752.     else if(ms)
  753.         ms = false;
  754.  
  755.     const tm* tms = localtime(&_time);
  756.     std::stringstream s;
  757.     if(tms)
  758.     {
  759.         s << tms->tm_hour << ":" << tms->tm_min << ":";
  760.         if(tms->tm_sec < 10)
  761.             s << "0";
  762.  
  763.         s << tms->tm_sec;
  764.         if(ms)
  765.         {
  766.             timeb t;
  767.             ftime(&t);
  768.  
  769.             s << "."; // make it format zzz
  770.             if(t.millitm < 10)
  771.                 s << "0";
  772.  
  773.             if(t.millitm < 100)
  774.                 s << "0";
  775.  
  776.             s << t.millitm;
  777.         }
  778.     }
  779.     else
  780.         s << "UNIX Time: " << (int32_t)_time;
  781.  
  782.     return s.str();
  783. }
  784.  
  785. std::string convertIPAddress(uint32_t ip)
  786. {
  787.     char buffer[17];
  788.     sprintf(buffer, "%d.%d.%d.%d", ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, (ip >> 24));
  789.     return buffer;
  790. }
  791.  
  792. Skulls_t getSkulls(std::string strValue)
  793. {
  794.     std::string tmpStrValue = asLowerCaseString(strValue);
  795.     if(tmpStrValue == "black" || tmpStrValue == "5")
  796.         return SKULL_BLACK;
  797.  
  798.     if(tmpStrValue == "red" || tmpStrValue == "4")
  799.         return SKULL_RED;
  800.  
  801.     if(tmpStrValue == "white" || tmpStrValue == "3")
  802.         return SKULL_WHITE;
  803.  
  804.     if(tmpStrValue == "green" || tmpStrValue == "2")
  805.         return SKULL_GREEN;
  806.  
  807.     if(tmpStrValue == "yellow" || tmpStrValue == "1")
  808.         return SKULL_YELLOW;
  809.  
  810.     return SKULL_NONE;
  811. }
  812.  
  813. PartyShields_t getShields(std::string strValue)
  814. {
  815.     std::string tmpStrValue = asLowerCaseString(strValue);
  816.     if(tmpStrValue == "whitenoshareoff" || tmpStrValue == "10")
  817.         return SHIELD_YELLOW_NOSHAREDEXP;
  818.  
  819.     if(tmpStrValue == "blueshareoff" || tmpStrValue == "9")
  820.         return SHIELD_BLUE_NOSHAREDEXP;
  821.  
  822.     if(tmpStrValue == "yellowshareblink" || tmpStrValue == "8")
  823.         return SHIELD_YELLOW_NOSHAREDEXP_BLINK;
  824.  
  825.     if(tmpStrValue == "blueshareblink" || tmpStrValue == "7")
  826.         return SHIELD_BLUE_NOSHAREDEXP_BLINK;
  827.  
  828.     if(tmpStrValue == "yellowshareon" || tmpStrValue == "6")
  829.         return SHIELD_YELLOW_SHAREDEXP;
  830.  
  831.     if(tmpStrValue == "blueshareon" || tmpStrValue == "5")
  832.         return SHIELD_BLUE_SHAREDEXP;
  833.  
  834.     if(tmpStrValue == "yellow" || tmpStrValue == "4")
  835.         return SHIELD_YELLOW;
  836.  
  837.     if(tmpStrValue == "blue" || tmpStrValue == "3")
  838.         return SHIELD_BLUE;
  839.  
  840.     if(tmpStrValue == "whiteyellow" || tmpStrValue == "2")
  841.         return SHIELD_WHITEYELLOW;
  842.  
  843.     if(tmpStrValue == "whiteblue" || tmpStrValue == "1")
  844.         return SHIELD_WHITEBLUE;
  845.  
  846.     return SHIELD_NONE;
  847. }
  848.  
  849. GuildEmblems_t getEmblems(std::string strValue)
  850. {
  851.     std::string tmpStrValue = asLowerCaseString(strValue);
  852.     if(tmpStrValue == "blue" || tmpStrValue == "3")
  853.         return EMBLEM_BLUE;
  854.  
  855.     if(tmpStrValue == "red" || tmpStrValue == "2")
  856.         return EMBLEM_RED;
  857.  
  858.     if(tmpStrValue == "green" || tmpStrValue == "1")
  859.         return EMBLEM_GREEN;
  860.  
  861.     return EMBLEM_NONE;
  862. }
  863.  
  864. Direction getDirection(std::string string)
  865. {
  866.     if(string == "north" || string == "n" || string == "0")
  867.         return NORTH;
  868.  
  869.     if(string == "east" || string == "e" || string == "1")
  870.         return EAST;
  871.  
  872.     if(string == "south" || string == "s" || string == "2")
  873.         return SOUTH;
  874.  
  875.     if(string == "west" || string == "w" || string == "3")
  876.         return WEST;
  877.  
  878.     if(string == "southwest" || string == "south west" || string == "south-west" || string == "sw" || string == "4")
  879.         return SOUTHWEST;
  880.  
  881.     if(string == "southeast" || string == "south east" || string == "south-east" || string == "se" || string == "5")
  882.         return SOUTHEAST;
  883.  
  884.     if(string == "northwest" || string == "north west" || string == "north-west" || string == "nw" || string == "6")
  885.         return NORTHWEST;
  886.  
  887.     if(string == "northeast" || string == "north east" || string == "north-east" || string == "ne" || string == "7")
  888.         return NORTHEAST;
  889.  
  890.     return SOUTH;
  891. }
  892.  
  893. Direction getDirectionTo(Position pos1, Position pos2, bool extended/* = true*/)
  894. {
  895.     Direction direction = NORTH;
  896.     if(pos1.x > pos2.x)
  897.     {
  898.         direction = WEST;
  899.         if(extended)
  900.         {
  901.             if(pos1.y > pos2.y)
  902.                 direction = NORTHWEST;
  903.             else if(pos1.y < pos2.y)
  904.                 direction = SOUTHWEST;
  905.         }
  906.     }
  907.     else if(pos1.x < pos2.x)
  908.     {
  909.         direction = EAST;
  910.         if(extended)
  911.         {
  912.             if(pos1.y > pos2.y)
  913.                 direction = NORTHEAST;
  914.             else if(pos1.y < pos2.y)
  915.                 direction = SOUTHEAST;
  916.         }
  917.     }
  918.     else
  919.     {
  920.         if(pos1.y > pos2.y)
  921.             direction = NORTH;
  922.         else if(pos1.y < pos2.y)
  923.             direction = SOUTH;
  924.     }
  925.  
  926.     return direction;
  927. }
  928.  
  929. Direction getReverseDirection(Direction dir)
  930. {
  931.     switch(dir)
  932.     {
  933.         case NORTH:
  934.             return SOUTH;
  935.         case SOUTH:
  936.             return NORTH;
  937.         case WEST:
  938.             return EAST;
  939.         case EAST:
  940.             return WEST;
  941.         case SOUTHWEST:
  942.             return NORTHEAST;
  943.         case NORTHWEST:
  944.             return SOUTHEAST;
  945.         case NORTHEAST:
  946.             return SOUTHWEST;
  947.         case SOUTHEAST:
  948.             return NORTHWEST;
  949.     }
  950.  
  951.     return SOUTH;
  952. }
  953.  
  954. Position getNextPosition(Direction direction, Position pos)
  955. {
  956.     switch(direction)
  957.     {
  958.         case NORTH:
  959.             pos.y--;
  960.             break;
  961.         case SOUTH:
  962.             pos.y++;
  963.             break;
  964.         case WEST:
  965.             pos.x--;
  966.             break;
  967.         case EAST:
  968.             pos.x++;
  969.             break;
  970.         case SOUTHWEST:
  971.             pos.x--;
  972.             pos.y++;
  973.             break;
  974.         case NORTHWEST:
  975.             pos.x--;
  976.             pos.y--;
  977.             break;
  978.         case SOUTHEAST:
  979.             pos.x++;
  980.             pos.y++;
  981.             break;
  982.         case NORTHEAST:
  983.             pos.x++;
  984.             pos.y--;
  985.             break;
  986.     }
  987.  
  988.     return pos;
  989. }
  990.  
  991. struct AmmoTypeNames
  992. {
  993.     const char* name;
  994.     Ammo_t ammoType;
  995. };
  996.  
  997. struct MagicEffectNames
  998. {
  999.     const char* name;
  1000.     MagicEffect_t magicEffect;
  1001. };
  1002.  
  1003. struct ShootTypeNames
  1004. {
  1005.     const char* name;
  1006.     ShootEffect_t shootType;
  1007. };
  1008.  
  1009. struct CombatTypeNames
  1010. {
  1011.     const char* name;
  1012.     CombatType_t combatType;
  1013. };
  1014.  
  1015. struct AmmoActionNames
  1016. {
  1017.     const char* name;
  1018.     AmmoAction_t ammoAction;
  1019. };
  1020.  
  1021. struct FluidTypeNames
  1022. {
  1023.     const char* name;
  1024.     FluidTypes_t fluidType;
  1025. };
  1026.  
  1027. struct SkillIdNames
  1028. {
  1029.     const char* name;
  1030.     skills_t skillId;
  1031. };
  1032.  
  1033. MagicEffectNames magicEffectNames[] =
  1034. {
  1035.     {"redspark",        MAGIC_EFFECT_DRAW_BLOOD},
  1036.     {"bluebubble",      MAGIC_EFFECT_LOSE_ENERGY},
  1037.     {"poff",        MAGIC_EFFECT_POFF},
  1038.     {"yellowspark",     MAGIC_EFFECT_BLOCKHIT},
  1039.     {"explosionarea",   MAGIC_EFFECT_EXPLOSION_AREA},
  1040.     {"explosion",       MAGIC_EFFECT_EXPLOSION_DAMAGE},
  1041.     {"firearea",        MAGIC_EFFECT_FIRE_AREA},
  1042.     {"yellowbubble",    MAGIC_EFFECT_YELLOW_RINGS},
  1043.     {"greenbubble",     MAGIC_EFFECT_POISON_RINGS},
  1044.     {"blackspark",      MAGIC_EFFECT_HIT_AREA},
  1045.     {"teleport",        MAGIC_EFFECT_TELEPORT},
  1046.     {"energy",      MAGIC_EFFECT_ENERGY_DAMAGE},
  1047.     {"blueshimmer",     MAGIC_EFFECT_WRAPS_BLUE},
  1048.     {"redshimmer",      MAGIC_EFFECT_WRAPS_RED},
  1049.     {"greenshimmer",    MAGIC_EFFECT_WRAPS_GREEN},
  1050.     {"fire",        MAGIC_EFFECT_HITBY_FIRE},
  1051.     {"greenspark",      MAGIC_EFFECT_POISON},
  1052.     {"mortarea",        MAGIC_EFFECT_MORT_AREA},
  1053.     {"greennote",       MAGIC_EFFECT_SOUND_GREEN},
  1054.     {"rednote",     MAGIC_EFFECT_SOUND_RED},
  1055.     {"poison",      MAGIC_EFFECT_POISON_AREA},
  1056.     {"yellownote",      MAGIC_EFFECT_SOUND_YELLOW},
  1057.     {"purplenote",      MAGIC_EFFECT_SOUND_PURPLE},
  1058.     {"bluenote",        MAGIC_EFFECT_SOUND_BLUE},
  1059.     {"whitenote",       MAGIC_EFFECT_SOUND_WHITE},
  1060.     {"bubbles",     MAGIC_EFFECT_BUBBLES},
  1061.     {"dice",        MAGIC_EFFECT_CRAPS},
  1062.     {"giftwraps",       MAGIC_EFFECT_GIFT_WRAPS},
  1063.     {"yellowfirework",  MAGIC_EFFECT_FIREWORK_YELLOW},
  1064.     {"redfirework",     MAGIC_EFFECT_FIREWORK_RED},
  1065.     {"bluefirework",    MAGIC_EFFECT_FIREWORK_BLUE},
  1066.     {"stun",        MAGIC_EFFECT_STUN},
  1067.     {"sleep",       MAGIC_EFFECT_SLEEP},
  1068.     {"watercreature",   MAGIC_EFFECT_WATERCREATURE},
  1069.     {"groundshaker",    MAGIC_EFFECT_GROUNDSHAKER},
  1070.     {"hearts",      MAGIC_EFFECT_HEARTS},
  1071.     {"fireattack",      MAGIC_EFFECT_FIREATTACK},
  1072.     {"energyarea",      MAGIC_EFFECT_ENERGY_AREA},
  1073.     {"smallclouds",     MAGIC_EFFECT_SMALLCLOUDS},
  1074.     {"holydamage",      MAGIC_EFFECT_HOLYDAMAGE},
  1075.     {"bigclouds",       MAGIC_EFFECT_BIGCLOUDS},
  1076.     {"icearea",     MAGIC_EFFECT_ICEAREA},
  1077.     {"icetornado",      MAGIC_EFFECT_ICETORNADO},
  1078.     {"iceattack",       MAGIC_EFFECT_ICEATTACK},
  1079.     {"stones",      MAGIC_EFFECT_STONES},
  1080.     {"smallplants",     MAGIC_EFFECT_SMALLPLANTS},
  1081.     {"carniphila",      MAGIC_EFFECT_CARNIPHILA},
  1082.     {"purpleenergy",    MAGIC_EFFECT_PURPLEENERGY},
  1083.     {"yellowenergy",    MAGIC_EFFECT_YELLOWENERGY},
  1084.     {"holyarea",        MAGIC_EFFECT_HOLYAREA},
  1085.     {"bigplants",       MAGIC_EFFECT_BIGPLANTS},
  1086.     {"cake",        MAGIC_EFFECT_CAKE},
  1087.     {"giantice",        MAGIC_EFFECT_GIANTICE},
  1088.     {"watersplash",     MAGIC_EFFECT_WATERSPLASH},
  1089.     {"plantattack",     MAGIC_EFFECT_PLANTATTACK},
  1090.     {"tutorialarrow",   MAGIC_EFFECT_TUTORIALARROW},
  1091.     {"tutorialsquare",  MAGIC_EFFECT_TUTORIALSQUARE},
  1092.     {"mirrorhorizontal",    MAGIC_EFFECT_MIRRORHORIZONTAL},
  1093.     {"mirrorvertical",  MAGIC_EFFECT_MIRRORVERTICAL},
  1094.     {"skullhorizontal", MAGIC_EFFECT_SKULLHORIZONTAL},
  1095.     {"skullvertical",   MAGIC_EFFECT_SKULLVERTICAL},
  1096.     {"assassin",        MAGIC_EFFECT_ASSASSIN},
  1097.     {"stepshorizontal", MAGIC_EFFECT_STEPSHORIZONTAL},
  1098.     {"bloodysteps",     MAGIC_EFFECT_BLOODYSTEPS},
  1099.     {"stepsvertical",   MAGIC_EFFECT_STEPSVERTICAL},
  1100.     {"yalaharighost",   MAGIC_EFFECT_YALAHARIGHOST},
  1101.     {"bats",        MAGIC_EFFECT_BATS},
  1102.     {"smoke",       MAGIC_EFFECT_SMOKE},
  1103.     {"insects",     MAGIC_EFFECT_INSECTS},
  1104.     {"dragonhead",  MAGIC_EFFECT_DRAGONHEAD}
  1105. };
  1106.  
  1107. ShootTypeNames shootTypeNames[] =
  1108. {
  1109.     {"spear",       SHOOT_EFFECT_SPEAR},
  1110.     {"bolt",        SHOOT_EFFECT_BOLT},
  1111.     {"arrow",       SHOOT_EFFECT_ARROW},
  1112.     {"fire",        SHOOT_EFFECT_FIRE},
  1113.     {"energy",      SHOOT_EFFECT_ENERGY},
  1114.     {"poisonarrow",     SHOOT_EFFECT_POISONARROW},
  1115.     {"burstarrow",      SHOOT_EFFECT_BURSTARROW},
  1116.     {"throwingstar",    SHOOT_EFFECT_THROWINGSTAR},
  1117.     {"throwingknife",   SHOOT_EFFECT_THROWINGKNIFE},
  1118.     {"smallstone",      SHOOT_EFFECT_SMALLSTONE},
  1119.     {"death",       SHOOT_EFFECT_DEATH},
  1120.     {"largerock",       SHOOT_EFFECT_LARGEROCK},
  1121.     {"snowball",        SHOOT_EFFECT_SNOWBALL},
  1122.     {"powerbolt",       SHOOT_EFFECT_POWERBOLT},
  1123.     {"poison",      SHOOT_EFFECT_POISONFIELD},
  1124.     {"infernalbolt",    SHOOT_EFFECT_INFERNALBOLT},
  1125.     {"huntingspear",    SHOOT_EFFECT_HUNTINGSPEAR},
  1126.     {"enchantedspear",  SHOOT_EFFECT_ENCHANTEDSPEAR},
  1127.     {"redstar",     SHOOT_EFFECT_REDSTAR},
  1128.     {"greenstar",       SHOOT_EFFECT_GREENSTAR},
  1129.     {"royalspear",      SHOOT_EFFECT_ROYALSPEAR},
  1130.     {"sniperarrow",     SHOOT_EFFECT_SNIPERARROW},
  1131.     {"onyxarrow",       SHOOT_EFFECT_ONYXARROW},
  1132.     {"piercingbolt",    SHOOT_EFFECT_PIERCINGBOLT},
  1133.     {"whirlwindsword",  SHOOT_EFFECT_WHIRLWINDSWORD},
  1134.     {"whirlwindaxe",    SHOOT_EFFECT_WHIRLWINDAXE},
  1135.     {"whirlwindclub",   SHOOT_EFFECT_WHIRLWINDCLUB},
  1136.     {"etherealspear",   SHOOT_EFFECT_ETHEREALSPEAR},
  1137.     {"ice",         SHOOT_EFFECT_ICE},
  1138.     {"earth",       SHOOT_EFFECT_EARTH},
  1139.     {"holy",        SHOOT_EFFECT_HOLY},
  1140.     {"suddendeath",     SHOOT_EFFECT_SUDDENDEATH},
  1141.     {"flasharrow",      SHOOT_EFFECT_FLASHARROW},
  1142.     {"flammingarrow",   SHOOT_EFFECT_FLAMMINGARROW},
  1143.     {"flamingarrow",    SHOOT_EFFECT_FLAMMINGARROW},
  1144.     {"shiverarrow",     SHOOT_EFFECT_SHIVERARROW},
  1145.     {"energyball",      SHOOT_EFFECT_ENERGYBALL},
  1146.     {"smallice",        SHOOT_EFFECT_SMALLICE},
  1147.     {"smallholy",       SHOOT_EFFECT_SMALLHOLY},
  1148.     {"smallearth",      SHOOT_EFFECT_SMALLEARTH},
  1149.     {"eartharrow",      SHOOT_EFFECT_EARTHARROW},
  1150.     {"explosion",       SHOOT_EFFECT_EXPLOSION},
  1151.     {"cake",        SHOOT_EFFECT_CAKE}
  1152. };
  1153.  
  1154. CombatTypeNames combatTypeNames[] =
  1155. {
  1156.     {"physical",        COMBAT_PHYSICALDAMAGE},
  1157.     {"energy",      COMBAT_ENERGYDAMAGE},
  1158.     {"earth",       COMBAT_EARTHDAMAGE},
  1159.     {"fire",        COMBAT_FIREDAMAGE},
  1160.     {"undefined",       COMBAT_UNDEFINEDDAMAGE},
  1161.     {"lifedrain",       COMBAT_LIFEDRAIN},
  1162.     {"life drain",      COMBAT_LIFEDRAIN},
  1163.     {"manadrain",       COMBAT_MANADRAIN},
  1164.     {"mana drain",      COMBAT_MANADRAIN},
  1165.     {"healing",     COMBAT_HEALING},
  1166.     {"drown",       COMBAT_DROWNDAMAGE},
  1167.     {"ice",         COMBAT_ICEDAMAGE},
  1168.     {"holy",        COMBAT_HOLYDAMAGE},
  1169.     {"death",       COMBAT_DEATHDAMAGE}
  1170. };
  1171.  
  1172. AmmoTypeNames ammoTypeNames[] =
  1173. {
  1174.     {"spear",       AMMO_SPEAR},
  1175.     {"arrow",       AMMO_ARROW},
  1176.     {"poisonarrow",     AMMO_ARROW},
  1177.     {"burstarrow",      AMMO_ARROW},
  1178.     {"bolt",        AMMO_BOLT},
  1179.     {"powerbolt",       AMMO_BOLT},
  1180.     {"smallstone",      AMMO_STONE},
  1181.     {"largerock",       AMMO_STONE},
  1182.     {"throwingstar",    AMMO_THROWINGSTAR},
  1183.     {"throwingknife",   AMMO_THROWINGKNIFE},
  1184.     {"snowball",        AMMO_SNOWBALL},
  1185.     {"huntingspear",    AMMO_SPEAR},
  1186.     {"royalspear",      AMMO_SPEAR},
  1187.     {"enchantedspear",  AMMO_SPEAR},
  1188.     {"sniperarrow",     AMMO_ARROW},
  1189.     {"onyxarrow",       AMMO_ARROW},
  1190.     {"piercingbolt",    AMMO_BOLT},
  1191.     {"infernalbolt",    AMMO_BOLT},
  1192.     {"flasharrow",      AMMO_ARROW},
  1193.     {"flammingarrow",   AMMO_ARROW},
  1194.     {"flamingarrow",    AMMO_ARROW},
  1195.     {"shiverarrow",     AMMO_ARROW},
  1196.     {"eartharrow",      AMMO_ARROW},
  1197.     {"etherealspear",   AMMO_SPEAR}
  1198. };
  1199.  
  1200. AmmoActionNames ammoActionNames[] =
  1201. {
  1202.     {"move",        AMMOACTION_MOVE},
  1203.     {"moveback",        AMMOACTION_MOVEBACK},
  1204.     {"move back",       AMMOACTION_MOVEBACK},
  1205.     {"removecharge",    AMMOACTION_REMOVECHARGE},
  1206.     {"remove charge",   AMMOACTION_REMOVECHARGE},
  1207.     {"removecount",     AMMOACTION_REMOVECOUNT},
  1208.     {"remove count",    AMMOACTION_REMOVECOUNT}
  1209. };
  1210.  
  1211. FluidTypeNames fluidTypeNames[] =
  1212. {
  1213.     {"none",        FLUID_NONE},
  1214.     {"water",       FLUID_WATER},
  1215.     {"blood",       FLUID_BLOOD},
  1216.     {"beer",        FLUID_BEER},
  1217.     {"slime",       FLUID_SLIME},
  1218.     {"lemonade",        FLUID_LEMONADE},
  1219.     {"milk",        FLUID_MILK},
  1220.     {"mana",        FLUID_MANA},
  1221.     {"life",        FLUID_LIFE},
  1222.     {"oil",         FLUID_OIL},
  1223.     {"urine",       FLUID_URINE},
  1224.     {"coconutmilk",     FLUID_COCONUTMILK},
  1225.     {"coconut milk",    FLUID_COCONUTMILK},
  1226.     {"wine",        FLUID_WINE},
  1227.     {"mud",         FLUID_MUD},
  1228.     {"fruitjuice",      FLUID_FRUITJUICE},
  1229.     {"fruit juice",     FLUID_FRUITJUICE},
  1230.     {"lava",        FLUID_LAVA},
  1231.     {"rum",         FLUID_RUM},
  1232.     {"swamp",       FLUID_SWAMP},
  1233.     {"tea",         FLUID_TEA},
  1234.     {"mead",        FLUID_MEAD}
  1235. };
  1236.  
  1237. SkillIdNames skillIdNames[] =
  1238. {
  1239.     {"fist",        SKILL_FIST},
  1240.     {"club",        SKILL_CLUB},
  1241.     {"sword",       SKILL_SWORD},
  1242.     {"axe",         SKILL_AXE},
  1243.     {"distance",        SKILL_DIST},
  1244.     {"dist",        SKILL_DIST},
  1245.     {"shielding",       SKILL_SHIELD},
  1246.     {"shield",      SKILL_SHIELD},
  1247.     {"fishing",     SKILL_FISH},
  1248.     {"fish",        SKILL_FISH},
  1249.     {"level",       SKILL__LEVEL},
  1250.     {"magiclevel",      SKILL__MAGLEVEL},
  1251.     {"magic level",     SKILL__MAGLEVEL}
  1252. };
  1253.  
  1254. MagicEffect_t getMagicEffect(const std::string& strValue)
  1255. {
  1256.     for(uint32_t i = 0; i < sizeof(magicEffectNames) / sizeof(MagicEffectNames); ++i)
  1257.     {
  1258.         if(!strcasecmp(strValue.c_str(), magicEffectNames[i].name))
  1259.             return magicEffectNames[i].magicEffect;
  1260.     }
  1261.  
  1262.     return MAGIC_EFFECT_UNKNOWN;
  1263. }
  1264.  
  1265. ShootEffect_t getShootType(const std::string& strValue)
  1266. {
  1267.     for(uint32_t i = 0; i < sizeof(shootTypeNames) / sizeof(ShootTypeNames); ++i)
  1268.     {
  1269.         if(!strcasecmp(strValue.c_str(), shootTypeNames[i].name))
  1270.             return shootTypeNames[i].shootType;
  1271.     }
  1272.  
  1273.     return SHOOT_EFFECT_UNKNOWN;
  1274. }
  1275.  
  1276. CombatType_t getCombatType(const std::string& strValue)
  1277. {
  1278.     for(uint32_t i = 0; i < sizeof(combatTypeNames) / sizeof(CombatTypeNames); ++i)
  1279.     {
  1280.         if(!strcasecmp(strValue.c_str(), combatTypeNames[i].name))
  1281.             return combatTypeNames[i].combatType;
  1282.     }
  1283.  
  1284.     return COMBAT_NONE;
  1285. }
  1286.  
  1287. Ammo_t getAmmoType(const std::string& strValue)
  1288. {
  1289.     for(uint32_t i = 0; i < sizeof(ammoTypeNames) / sizeof(AmmoTypeNames); ++i)
  1290.     {
  1291.         if(!strcasecmp(strValue.c_str(), ammoTypeNames[i].name))
  1292.             return ammoTypeNames[i].ammoType;
  1293.     }
  1294.  
  1295.     return AMMO_NONE;
  1296. }
  1297.  
  1298. AmmoAction_t getAmmoAction(const std::string& strValue)
  1299. {
  1300.     for(uint32_t i = 0; i < sizeof(ammoActionNames) / sizeof(AmmoActionNames); ++i)
  1301.     {
  1302.         if(!strcasecmp(strValue.c_str(), ammoActionNames[i].name))
  1303.             return ammoActionNames[i].ammoAction;
  1304.     }
  1305.  
  1306.     return AMMOACTION_NONE;
  1307. }
  1308.  
  1309. FluidTypes_t getFluidType(const std::string& strValue)
  1310. {
  1311.     for(uint32_t i = 0; i < sizeof(fluidTypeNames) / sizeof(FluidTypeNames); ++i)
  1312.     {
  1313.         if(!strcasecmp(strValue.c_str(), fluidTypeNames[i].name))
  1314.             return fluidTypeNames[i].fluidType;
  1315.     }
  1316.  
  1317.     return FLUID_NONE;
  1318. }
  1319.  
  1320. skills_t getSkillId(const std::string& strValue)
  1321. {
  1322.     for(uint32_t i = 0; i < sizeof(skillIdNames) / sizeof(SkillIdNames); ++i)
  1323.     {
  1324.         if(!strcasecmp(strValue.c_str(), skillIdNames[i].name))
  1325.             return skillIdNames[i].skillId;
  1326.     }
  1327.  
  1328.     return SKILL_FIST;
  1329. }
  1330.  
  1331. std::string getCombatName(CombatType_t combatType)
  1332. {
  1333.     switch(combatType)
  1334.     {
  1335.         case COMBAT_PHYSICALDAMAGE:
  1336.             return "physical";
  1337.         case COMBAT_ENERGYDAMAGE:
  1338.             return "energy";
  1339.         case COMBAT_EARTHDAMAGE:
  1340.             return "earth";
  1341.         case COMBAT_FIREDAMAGE:
  1342.             return "fire";
  1343.         case COMBAT_UNDEFINEDDAMAGE:
  1344.             return "undefined";
  1345.         case COMBAT_LIFEDRAIN:
  1346.             return "life drain";
  1347.         case COMBAT_MANADRAIN:
  1348.             return "mana drain";
  1349.         case COMBAT_HEALING:
  1350.             return "healing";
  1351.         case COMBAT_DROWNDAMAGE:
  1352.             return "drown";
  1353.         case COMBAT_ICEDAMAGE:
  1354.             return "ice";
  1355.         case COMBAT_HOLYDAMAGE:
  1356.             return "holy";
  1357.         case COMBAT_DEATHDAMAGE:
  1358.             return "death";
  1359.         default:
  1360.             break;
  1361.     }
  1362.  
  1363.     return "unknown";
  1364. }
  1365.  
  1366. std::string getSkillName(uint16_t skillId, bool suffix/* = true*/)
  1367. {
  1368.     switch(skillId)
  1369.     {
  1370.         case SKILL_FIST:
  1371.         {
  1372.             std::string tmp = "fist";
  1373.             if(suffix)
  1374.                 tmp += " fighting";
  1375.  
  1376.             return tmp;
  1377.         }
  1378.         case SKILL_CLUB:
  1379.         {
  1380.             std::string tmp = "club";
  1381.             if(suffix)
  1382.                 tmp += " fighting";
  1383.  
  1384.             return tmp;
  1385.         }
  1386.         case SKILL_SWORD:
  1387.         {
  1388.             std::string tmp = "sword";
  1389.             if(suffix)
  1390.                 tmp += " fighting";
  1391.  
  1392.             return tmp;
  1393.         }
  1394.         case SKILL_AXE:
  1395.         {
  1396.             std::string tmp = "axe";
  1397.             if(suffix)
  1398.                 tmp += " fighting";
  1399.  
  1400.             return tmp;
  1401.         }
  1402.         case SKILL_DIST:
  1403.         {
  1404.             std::string tmp = "distance";
  1405.             if(suffix)
  1406.                 tmp += " fighting";
  1407.  
  1408.             return tmp;
  1409.         }
  1410.         case SKILL_SHIELD:
  1411.             return "shielding";
  1412.         case SKILL_FISH:
  1413.             return "fishing";
  1414.         case SKILL__MAGLEVEL:
  1415.             return "magic level";
  1416.         case SKILL__LEVEL:
  1417.             return "level";
  1418.         default:
  1419.             break;
  1420.     }
  1421.  
  1422.     return "unknown";
  1423. }
  1424.  
  1425. std::string getReason(int32_t reasonId)
  1426. {
  1427.     switch(reasonId)
  1428.     {
  1429.         case 0:
  1430.             return "Offensive Name";
  1431.         case 1:
  1432.             return "Invalid Name Format";
  1433.         case 2:
  1434.             return "Unsuitable Name";
  1435.         case 3:
  1436.             return "Name Inciting Rule Violation";
  1437.         case 4:
  1438.             return "Offensive Statement";
  1439.         case 5:
  1440.             return "Spamming";
  1441.         case 6:
  1442.             return "Illegal Advertising";
  1443.         case 7:
  1444.             return "Off-Topic Public Statement";
  1445.         case 8:
  1446.             return "Non-English Public Statement";
  1447.         case 9:
  1448.             return "Inciting Rule Violation";
  1449.         case 10:
  1450.             return "Bug Abuse";
  1451.         case 11:
  1452.             return "Game Weakness Abuse";
  1453.         case 12:
  1454.             return "Using Unofficial Software to Play";
  1455.         case 13:
  1456.             return "Hacking";
  1457.         case 14:
  1458.             return "Multi-Clienting";
  1459.         case 15:
  1460.             return "Account Trading or Sharing";
  1461.         case 16:
  1462.             return "Threatening Gamemaster";
  1463.         case 17:
  1464.             return "Pretending to Have Influence on Rule Enforcement";
  1465.         case 18:
  1466.             return "False Report to Gamemaster";
  1467.         case 19:
  1468.             return "Destructive Behaviour";
  1469.         case 20:
  1470.             return "Excessive Unjustified Player Killing";
  1471.         default:
  1472.             break;
  1473.     }
  1474.  
  1475.     return "Unknown Reason";
  1476. }
  1477.  
  1478. std::string getAction(ViolationAction_t actionId, bool ipBanishment)
  1479. {
  1480.     std::string action = "Unknown";
  1481.     switch(actionId)
  1482.     {
  1483.         case ACTION_NOTATION:
  1484.             action = "Notation";
  1485.             break;
  1486.         case ACTION_NAMEREPORT:
  1487.             action = "Name Report";
  1488.             break;
  1489.         case ACTION_BANISHMENT:
  1490.             action = "Banishment";
  1491.             break;
  1492.         case ACTION_BANREPORT:
  1493.             action = "Name Report + Banishment";
  1494.             break;
  1495.         case ACTION_BANFINAL:
  1496.             action = "Banishment + Final Warning";
  1497.             break;
  1498.         case ACTION_BANREPORTFINAL:
  1499.             action = "Name Report + Banishment + Final Warning";
  1500.             break;
  1501.         case ACTION_STATEMENT:
  1502.             action = "Statement Report";
  1503.             break;
  1504.         //internal use
  1505.         case ACTION_DELETION:
  1506.             action = "Deletion";
  1507.             break;
  1508.         case ACTION_NAMELOCK:
  1509.             action = "Name Lock";
  1510.             break;
  1511.         case ACTION_BANLOCK:
  1512.             action = "Name Lock + Banishment";
  1513.             break;
  1514.         case ACTION_BANLOCKFINAL:
  1515.             action = "Name Lock + Banishment + Final Warning";
  1516.             break;
  1517.         default:
  1518.             break;
  1519.     }
  1520.  
  1521.     if(ipBanishment)
  1522.         action += " + IP Banishment";
  1523.  
  1524.     return action;
  1525. }
  1526.  
  1527. std::string parseVocationString(StringVec vocStringVec)
  1528. {
  1529.     std::string str = "";
  1530.     if(!vocStringVec.empty())
  1531.     {
  1532.         for(StringVec::iterator it = vocStringVec.begin(); it != vocStringVec.end(); ++it)
  1533.         {
  1534.             if((*it) != vocStringVec.front())
  1535.             {
  1536.                 if((*it) != vocStringVec.back())
  1537.                     str += ", ";
  1538.                 else
  1539.                     str += " and ";
  1540.             }
  1541.  
  1542.             str += (*it);
  1543.             str += "s";
  1544.         }
  1545.     }
  1546.  
  1547.     return str;
  1548. }
  1549.  
  1550. bool parseVocationNode(xmlNodePtr vocationNode, VocationMap& vocationMap, StringVec& vocStringVec, std::string& errorStr)
  1551. {
  1552.     if(xmlStrcmp(vocationNode->name,(const xmlChar*)"vocation"))
  1553.         return true;
  1554.  
  1555.     int32_t vocationId = -1;
  1556.     std::string strValue, tmpStrValue;
  1557.     if(readXMLString(vocationNode, "name", strValue))
  1558.     {
  1559.         vocationId = Vocations::getInstance()->getVocationId(strValue);
  1560.         if(vocationId != -1)
  1561.         {
  1562.             vocationMap[vocationId] = true;
  1563.             int32_t promotedVocation = Vocations::getInstance()->getPromotedVocation(vocationId);
  1564.             if(promotedVocation != -1)
  1565.                 vocationMap[promotedVocation] = true;
  1566.         }
  1567.         else
  1568.         {
  1569.             errorStr = "Wrong vocation name: " + strValue;
  1570.             return false;
  1571.         }
  1572.     }
  1573.     else if(readXMLString(vocationNode, "id", strValue))
  1574.     {
  1575.         IntegerVec intVector;
  1576.         if(!parseIntegerVec(strValue, intVector))
  1577.         {
  1578.             errorStr = "Invalid vocation id - '" + strValue + "'";
  1579.             return false;
  1580.         }
  1581.  
  1582.         size_t size = intVector.size();
  1583.         for(size_t i = 0; i < size; ++i)
  1584.         {
  1585.             Vocation* vocation = Vocations::getInstance()->getVocation(intVector[i]);
  1586.             if(vocation && vocation->getName() != "")
  1587.             {
  1588.                 vocationId = vocation->getId();
  1589.                 strValue = vocation->getName();
  1590.  
  1591.                 vocationMap[vocationId] = true;
  1592.                 int32_t promotedVocation = Vocations::getInstance()->getPromotedVocation(vocationId);
  1593.                 if(promotedVocation != -1)
  1594.                     vocationMap[promotedVocation] = true;
  1595.             }
  1596.             else
  1597.             {
  1598.                 std::stringstream ss;
  1599.                 ss << "Wrong vocation id: " << intVector[i];
  1600.  
  1601.                 errorStr = ss.str();
  1602.                 return false;
  1603.             }
  1604.         }
  1605.     }
  1606.  
  1607.     if(vocationId != -1 && (!readXMLString(vocationNode, "showInDescription", tmpStrValue) || booleanString(tmpStrValue)))
  1608.         vocStringVec.push_back(asLowerCaseString(strValue));
  1609.  
  1610.     return true;
  1611. }
  1612.  
  1613. bool parseIntegerVec(std::string str, IntegerVec& intVector)
  1614. {
  1615.     StringVec strVector = explodeString(str, ";");
  1616.     IntegerVec tmpIntVector;
  1617.     for(StringVec::iterator it = strVector.begin(); it != strVector.end(); ++it)
  1618.     {
  1619.         tmpIntVector = vectorAtoi(explodeString((*it), "-"));
  1620.         if(!tmpIntVector[0] && it->substr(0, 1) != "0")
  1621.             continue;
  1622.  
  1623.         intVector.push_back(tmpIntVector[0]);
  1624.         if(tmpIntVector.size() > 1)
  1625.         {
  1626.             while(tmpIntVector[0] < tmpIntVector[1])
  1627.                 intVector.push_back(++tmpIntVector[0]);
  1628.         }
  1629.     }
  1630.  
  1631.     return true;
  1632. }
  1633.  
  1634. bool fileExists(const char* filename)
  1635. {
  1636.     FILE* f = fopen(filename, "rb");
  1637.     if(!f)
  1638.         return false;
  1639.  
  1640.     fclose(f);
  1641.     return true;
  1642. }
  1643.  
  1644. uint32_t adlerChecksum(uint8_t* data, size_t length)
  1645. {
  1646.     // Keep this check, rarely used I think
  1647.     if(length > NETWORK_MAX_SIZE || !length)
  1648.         return 0;
  1649.  
  1650.     // Crypto++ object
  1651.     CryptoPP::Adler32 adler;
  1652.     // Digest cash object, cast later
  1653.     byte digest[CryptoPP::Adler32::DIGESTSIZE];
  1654.  
  1655.     // Do the calculation now
  1656.     adler.CalculateDigest(digest, (const byte*)data, length);
  1657.     // return uint32_t cast type
  1658.     return (uint32_t)(((uint16_t)digest[0] << 8 | digest[1]) << 16) | ((uint16_t)digest[2] << 8 | digest[3]);
  1659. }
  1660.  
  1661. std::string getFilePath(FileType_t type, std::string name/* = ""*/)
  1662. {
  1663.     #ifdef __FILESYSTEM_HIERARCHY_STANDARD__
  1664.     std::string path = "/var/lib/tfs/";
  1665.     #endif
  1666.     std::string path = g_config.getString(ConfigManager::DATA_DIRECTORY);
  1667.     switch(type)
  1668.     {
  1669.         case FILE_TYPE_OTHER:
  1670.             path += name;
  1671.             break;
  1672.         case FILE_TYPE_XML:
  1673.             path += "XML/" + name;
  1674.             break;
  1675.         case FILE_TYPE_LOG:
  1676.             #ifndef __FILESYSTEM_HIERARCHY_STANDARD__
  1677.             path = g_config.getString(ConfigManager::LOGS_DIRECTORY) + name;
  1678.             #else
  1679.             path = "/var/log/tfs/" + name;
  1680.             #endif
  1681.             break;
  1682.         case FILE_TYPE_MOD:
  1683.         {
  1684.             #ifndef __FILESYSTEM_HIERARCHY_STANDARD__
  1685.             path = "mods/" + name;
  1686.             #else
  1687.             path = "/usr/share/tfs/" + name;
  1688.             #endif
  1689.             break;
  1690.         }
  1691.         case FILE_TYPE_CONFIG:
  1692.         {
  1693.             #if defined(__HOMEDIR_CONF__)
  1694.             if(fileExists("~/.tfs/" + name))
  1695.                 path = "~/.tfs/" + name;
  1696.             else
  1697.             #endif
  1698.             #if defined(__FILESYSTEM_HIERARCHY_STANDARD__)
  1699.                 path = "/etc/tfs/" + name;
  1700.             #else
  1701.                 path = name;
  1702.             #endif
  1703.             break;
  1704.         }
  1705.         default:
  1706.             std::clog << "> ERROR: Wrong file type!" << std::endl;
  1707.             break;
  1708.     }
  1709.     return path;
  1710. }
  1711.  
Add Comment
Please, Sign In to add comment