ECSearchClient.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 <sys/un.h>
  19. #include <sys/socket.h>
  20. #include <kopano/ECChannel.h>
  21. #include <kopano/ECDefs.h>
  22. #include <kopano/stringutil.h>
  23. #include "ECSearchClient.h"
  24. namespace KC {
  25. ECSearchClient::ECSearchClient(const char *szIndexerPath, unsigned int ulTimeOut)
  26. : ECChannelClient(szIndexerPath, ":;")
  27. {
  28. m_ulTimeout = ulTimeOut;
  29. }
  30. ECRESULT ECSearchClient::GetProperties(setindexprops_t &setProps)
  31. {
  32. ECRESULT er;
  33. std::vector<std::string> lstResponse;
  34. std::vector<std::string> lstProps;
  35. er = DoCmd("PROPS", lstResponse);
  36. if (er != erSuccess)
  37. return er;
  38. setProps.clear();
  39. if (lstResponse.empty())
  40. return erSuccess; // No properties
  41. lstProps = tokenize(lstResponse[0], " ");
  42. for (const auto &s : lstProps)
  43. setProps.insert(atoui(s.c_str()));
  44. return erSuccess;
  45. }
  46. /**
  47. * Output SCOPE command
  48. *
  49. * Specifies the scope of the search.
  50. *
  51. * @param strServer[in] Server GUID to search in
  52. * @param strStore[in] Store GUID to search in
  53. * @param lstFolders[in] List of folders to search in. As a special case, no folders means 'all folders'
  54. * @return result
  55. */
  56. ECRESULT ECSearchClient::Scope(const std::string &strServer,
  57. const std::string &strStore, const std::list<unsigned int> &lstFolders)
  58. {
  59. ECRESULT er;
  60. std::vector<std::string> lstResponse;
  61. std::string strScope;
  62. er = Connect();
  63. if (er != erSuccess)
  64. return er;
  65. strScope = "SCOPE " + strServer + " " + strStore;
  66. for (const auto i : lstFolders)
  67. strScope += " " + stringify(i);
  68. er = DoCmd(strScope, lstResponse);
  69. if (er != erSuccess)
  70. return er;
  71. if (!lstResponse.empty())
  72. return KCERR_BAD_VALUE;
  73. return erSuccess;
  74. }
  75. /**
  76. * Output FIND command
  77. *
  78. * The FIND command specifies which term to look for in which fields. When multiple
  79. * FIND commands are issued, items must match ALL of the terms.
  80. *
  81. * @param setFields[in] Fields to match (may match any of these fields)
  82. * @param strTerm[in] Term to match (utf-8 encoded)
  83. * @return result
  84. */
  85. ECRESULT ECSearchClient::Find(const std::set<unsigned int> &setFields,
  86. const std::string &strTerm)
  87. {
  88. ECRESULT er;
  89. std::vector<std::string> lstResponse;
  90. std::string strFind;
  91. strFind = "FIND";
  92. for (const auto i : setFields)
  93. strFind += " " + stringify(i);
  94. strFind += ":";
  95. strFind += strTerm;
  96. er = DoCmd(strFind, lstResponse);
  97. if (er != erSuccess)
  98. return er;
  99. if (!lstResponse.empty())
  100. return KCERR_BAD_VALUE;
  101. return erSuccess;
  102. }
  103. /**
  104. * Run the search query
  105. *
  106. * @param lstMatches[out] List of matches as hierarchy IDs
  107. * @return result
  108. */
  109. ECRESULT ECSearchClient::Query(std::list<unsigned int> &lstMatches)
  110. {
  111. ECRESULT er;
  112. std::vector<std::string> lstResponse;
  113. std::vector<std::string> lstResponseIds;
  114. lstMatches.clear();
  115. er = DoCmd("QUERY", lstResponse);
  116. if (er != erSuccess)
  117. return er;
  118. if (lstResponse.empty())
  119. return erSuccess; /* no matches */
  120. lstResponseIds = tokenize(lstResponse[0], " ");
  121. for (unsigned int i = 0; i < lstResponseIds.size(); ++i)
  122. lstMatches.push_back(atoui(lstResponseIds[i].c_str()));
  123. return erSuccess;
  124. }
  125. /**
  126. * Do a full search query
  127. *
  128. * This function actually executes a number of commands:
  129. *
  130. * SCOPE <serverid> <storeid> <folder1> ... <folderN>
  131. * FIND <field1> ... <fieldN> : <term>
  132. * QUERY
  133. *
  134. * @param lpServerGuid[in] Server GUID to search in
  135. * @param lpStoreGuid[in] Store GUID to search in
  136. * @param lstFolders[in] List of folders to search in
  137. * @param lstSearches[in] List of searches that items should match (AND)
  138. * @param lstMatches[out] Output of matching items
  139. * @return result
  140. */
  141. ECRESULT ECSearchClient::Query(GUID *lpServerGuid, GUID *lpStoreGuid, std::list<unsigned int>& lstFolders, std::list<SIndexedTerm> &lstSearches, std::list<unsigned int> &lstMatches, std::string &suggestion)
  142. {
  143. ECRESULT er;
  144. std::string strServer = bin2hex(sizeof(GUID), (unsigned char *)lpServerGuid);
  145. std::string strStore = bin2hex(sizeof(GUID), (unsigned char *)lpStoreGuid);
  146. er = Scope(strServer, strStore, lstFolders);
  147. if (er != erSuccess)
  148. return er;
  149. for (const auto &i : lstSearches)
  150. Find(i.setFields, i.strTerm);
  151. er = Suggest(suggestion);
  152. if (er != erSuccess)
  153. return er;
  154. return Query(lstMatches);
  155. }
  156. ECRESULT ECSearchClient::Suggest(std::string &suggestion)
  157. {
  158. std::vector<std::string> resp;
  159. ECRESULT er = DoCmd("SUGGEST", resp);
  160. if (er != erSuccess)
  161. return er;
  162. if (resp.size() < 1)
  163. return KCERR_CALL_FAILED;
  164. suggestion = std::move(resp[0]);
  165. if (suggestion[0] == ' ')
  166. suggestion.erase(0, 1);
  167. return erSuccess;
  168. }
  169. ECRESULT ECSearchClient::SyncRun()
  170. {
  171. std::vector<std::string> lstVoid;
  172. return DoCmd("SYNCRUN", lstVoid);
  173. }
  174. } /* namespace */