pcuser.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/platform.h>
  18. #include <kopano/stringutil.h>
  19. #include <kopano/pcuser.hpp>
  20. #include <sstream>
  21. namespace KC {
  22. template<int(*fnCmp)(const char*, const char*)>
  23. class StringComparer {
  24. public:
  25. StringComparer(const std::string &str): m_str(str) {}
  26. bool operator()(const std::string &other) const
  27. {
  28. return m_str.size() == other.size() && fnCmp(m_str.c_str(), other.c_str()) == 0;
  29. }
  30. private:
  31. const std::string &m_str;
  32. };
  33. objectid_t::objectid_t(const std::string &str)
  34. {
  35. std::string objclass;
  36. std::string objid;
  37. size_t pos;
  38. // sendas users are "encoded" like this in a string
  39. pos = str.find_first_of(';');
  40. if (pos == std::string::npos) {
  41. this->id = hex2bin(str);
  42. this->objclass = ACTIVE_USER;
  43. } else {
  44. objid.assign(str, pos + 1, str.size() - pos);
  45. objclass.assign(str, 0, pos);
  46. this->id = hex2bin(objid);
  47. this->objclass = (objectclass_t)atoi(objclass.c_str());
  48. }
  49. }
  50. bool objectid_t::operator==(const objectid_t &x) const
  51. {
  52. return this->objclass == x.objclass && this->id == x.id;
  53. }
  54. bool objectid_t::operator!=(const objectid_t &x) const
  55. {
  56. return this->objclass != x.objclass || this->id != x.id;
  57. }
  58. std::string objectid_t::tostring() const
  59. {
  60. return stringify(this->objclass) + ";" + bin2hex(this->id);
  61. }
  62. unsigned int objectdetails_t::GetPropInt(property_key_t propname) const
  63. {
  64. property_map::const_iterator item = m_mapProps.find(propname);
  65. return item == m_mapProps.cend() ? 0 : atoi(item->second.c_str());
  66. }
  67. bool objectdetails_t::GetPropBool(property_key_t propname) const
  68. {
  69. property_map::const_iterator item = m_mapProps.find(propname);
  70. return item == m_mapProps.cend() ? false : atoi(item->second.c_str());
  71. }
  72. std::string objectdetails_t::GetPropString(property_key_t propname) const
  73. {
  74. property_map::const_iterator item = m_mapProps.find(propname);
  75. return item == m_mapProps.cend() ? std::string() : item->second;
  76. }
  77. objectid_t objectdetails_t::GetPropObject(property_key_t propname) const
  78. {
  79. property_map::const_iterator item = m_mapProps.find(propname);
  80. return item == m_mapProps.cend() ? objectid_t() : objectid_t(item->second);
  81. }
  82. void objectdetails_t::SetPropInt(property_key_t propname, unsigned int value)
  83. {
  84. m_mapProps[propname].assign(stringify(value));
  85. }
  86. void objectdetails_t::SetPropBool(property_key_t propname, bool value)
  87. {
  88. m_mapProps[propname].assign(value ? "1" : "0");
  89. }
  90. void objectdetails_t::SetPropString(property_key_t propname,
  91. const std::string &value)
  92. {
  93. m_mapProps[propname].assign(value);
  94. }
  95. void objectdetails_t::SetPropListString(property_key_t propname,
  96. const std::list<std::string> &value)
  97. {
  98. m_mapMVProps[propname].assign(value.begin(), value.end());
  99. }
  100. void objectdetails_t::SetPropObject(property_key_t propname,
  101. const objectid_t &value)
  102. {
  103. m_mapProps[propname].assign(((objectid_t)value).tostring());
  104. }
  105. void objectdetails_t::AddPropInt(property_key_t propname, unsigned int value)
  106. {
  107. m_mapMVProps[propname].push_back(stringify(value));
  108. }
  109. void objectdetails_t::AddPropString(property_key_t propname,
  110. const std::string &value)
  111. {
  112. m_mapMVProps[propname].push_back(value);
  113. }
  114. void objectdetails_t::AddPropObject(property_key_t propname,
  115. const objectid_t &value)
  116. {
  117. m_mapMVProps[propname].push_back(((objectid_t)value).tostring());
  118. }
  119. std::list<unsigned int>
  120. objectdetails_t::GetPropListInt(property_key_t propname) const
  121. {
  122. property_mv_map::const_iterator mvitem = m_mapMVProps.find(propname);
  123. if (mvitem == m_mapMVProps.cend())
  124. return std::list<unsigned int>();
  125. std::list<unsigned int> l;
  126. for (const auto &i : mvitem->second)
  127. l.push_back(atoui(i.c_str()));
  128. return l;
  129. }
  130. std::list<std::string>
  131. objectdetails_t::GetPropListString(property_key_t propname) const
  132. {
  133. property_mv_map::const_iterator mvitem = m_mapMVProps.find(propname);
  134. if (mvitem != m_mapMVProps.cend())
  135. return mvitem->second;
  136. return std::list<std::string>();
  137. }
  138. std::list<objectid_t>
  139. objectdetails_t::GetPropListObject(property_key_t propname) const
  140. {
  141. property_mv_map::const_iterator mvitem = m_mapMVProps.find(propname);
  142. if (mvitem == m_mapMVProps.cend())
  143. return std::list<objectid_t>();
  144. std::list<objectid_t> l;
  145. for (const auto &i : mvitem->second)
  146. l.push_back(objectid_t(i));
  147. return l;
  148. }
  149. property_map objectdetails_t::GetPropMapAnonymous() const {
  150. property_map anonymous;
  151. for (const auto &iter : m_mapProps)
  152. if (((unsigned int)iter.first) & 0xffff0000)
  153. anonymous.insert(iter);
  154. return anonymous;
  155. }
  156. property_mv_map objectdetails_t::GetPropMapListAnonymous() const {
  157. property_mv_map anonymous;
  158. for (const auto &iter : m_mapMVProps)
  159. if (((unsigned int)iter.first) & 0xffff0000)
  160. anonymous.insert(iter);
  161. return anonymous;
  162. }
  163. bool objectdetails_t::HasProp(property_key_t propname) const
  164. {
  165. return m_mapProps.find(propname) != m_mapProps.end() || m_mapMVProps.find(propname) != m_mapMVProps.end();
  166. }
  167. bool objectdetails_t::PropListStringContains(property_key_t propname,
  168. const std::string &value, bool ignoreCase) const
  169. {
  170. const std::list<std::string> list = GetPropListString(propname);
  171. if (ignoreCase)
  172. return std::find_if(list.begin(), list.end(), StringComparer<strcasecmp>(value)) != list.end();
  173. return std::find_if(list.begin(), list.end(), StringComparer<strcmp>(value)) != list.end();
  174. }
  175. void objectdetails_t::ClearPropList(property_key_t propname)
  176. {
  177. m_mapMVProps[propname].clear();
  178. }
  179. void objectdetails_t::SetClass(objectclass_t objclass)
  180. {
  181. m_objclass = objclass;
  182. }
  183. objectclass_t objectdetails_t::GetClass() const {
  184. return m_objclass;
  185. }
  186. void objectdetails_t::MergeFrom(const objectdetails_t &from) {
  187. assert(this->m_objclass == from.m_objclass);
  188. for (const auto &p : from.m_mapProps)
  189. this->m_mapProps[p.first].assign(p.second);
  190. for (const auto &p : from.m_mapMVProps)
  191. this->m_mapMVProps[p.first].assign(p.second.cbegin(), p.second.cend());
  192. }
  193. /**
  194. * Get the size of this object
  195. *
  196. * @return Memory usage of this object in bytes
  197. */
  198. size_t objectdetails_t::GetObjectSize(void) const
  199. {
  200. size_t ulSize = sizeof(*this);
  201. ulSize += MEMORY_USAGE_MAP(m_mapProps.size(), property_map);
  202. for (const auto &p : m_mapProps)
  203. ulSize += MEMORY_USAGE_STRING(p.second);
  204. ulSize += MEMORY_USAGE_MAP(m_mapMVProps.size(), property_mv_map);
  205. for (const auto &p : m_mapMVProps)
  206. for (const auto &s : p.second)
  207. ulSize += MEMORY_USAGE_STRING(s);
  208. return ulSize;
  209. }
  210. std::string objectdetails_t::ToStr(void) const
  211. {
  212. std::string str;
  213. str = "propmap: ";
  214. for (auto i = m_mapProps.cbegin(); i != m_mapProps.cend(); ++i) {
  215. if (i != m_mapProps.cbegin())
  216. str += ", ";
  217. str+= stringify(i->first) + "='";
  218. str+= i->second + "'";
  219. }
  220. str += " mvpropmap: ";
  221. for (auto mvi = m_mapMVProps.cbegin();
  222. mvi != m_mapMVProps.cend(); ++mvi) {
  223. if (mvi != m_mapMVProps.begin())
  224. str += ", ";
  225. str += stringify(mvi->first) + "=(";
  226. for (auto istr = mvi->second.cbegin();
  227. istr != mvi->second.cend(); ++istr) {
  228. if (istr != mvi->second.cbegin())
  229. str += ", ";
  230. str += *istr;
  231. }
  232. str +=")";
  233. }
  234. return str;
  235. }
  236. serverdetails_t::serverdetails_t(const std::string &servername)
  237. : m_strServerName(servername)
  238. { }
  239. void serverdetails_t::SetHostAddress(const std::string &hostaddress) {
  240. m_strHostAddress = hostaddress;
  241. }
  242. void serverdetails_t::SetFilePath(const std::string &filepath) {
  243. m_strFilePath = filepath;
  244. }
  245. void serverdetails_t::SetHttpPort(unsigned port) {
  246. m_ulHttpPort = port;
  247. }
  248. void serverdetails_t::SetSslPort(unsigned port) {
  249. m_ulSslPort = port;
  250. }
  251. void serverdetails_t::SetProxyPath(const std::string &proxy) {
  252. m_strProxyPath = proxy;
  253. }
  254. const std::string& serverdetails_t::GetServerName() const {
  255. return m_strServerName;
  256. }
  257. const std::string& serverdetails_t::GetHostAddress() const {
  258. return m_strHostAddress;
  259. }
  260. unsigned serverdetails_t::GetHttpPort() const {
  261. return m_ulHttpPort;
  262. }
  263. unsigned serverdetails_t::GetSslPort() const {
  264. return m_ulSslPort;
  265. }
  266. std::string serverdetails_t::GetFilePath() const {
  267. if (!m_strFilePath.empty())
  268. return "file://"+m_strFilePath;
  269. return std::string();
  270. }
  271. std::string serverdetails_t::GetHttpPath() const {
  272. if (!m_strHostAddress.empty() && m_ulHttpPort > 0) {
  273. std::ostringstream oss;
  274. oss << "http://" << m_strHostAddress << ":" << m_ulHttpPort << "/";
  275. return oss.str();
  276. }
  277. return std::string();
  278. }
  279. std::string serverdetails_t::GetSslPath() const {
  280. if (!m_strHostAddress.empty() && m_ulSslPort > 0) {
  281. std::ostringstream oss;
  282. oss << "https://" << m_strHostAddress << ":" << m_ulSslPort << "/";
  283. return oss.str();
  284. }
  285. return std::string();
  286. }
  287. const std::string &serverdetails_t::GetProxyPath() const {
  288. return m_strProxyPath;
  289. }
  290. } /* namespace */