IMAPClient.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * @file IMAPClient.h
  3. * @brief libcurl wrapper for IMAP operations
  4. *
  5. * @author Mohamed Amine Mzoughi <mohamed-amine.mzoughi@laposte.net>
  6. * @date 2017-01-02
  7. *
  8. * Updated by acetone in 2023 for QtEmailFetcher
  9. */
  10. #include "IMAPClient.h"
  11. CIMAPClient::CIMAPClient(LogFnCallback oLogger) :
  12. CMailClient(oLogger),
  13. m_pstrText(nullptr),
  14. m_eOperationType(IMAP_NOOP),
  15. m_eMailProperty(MailProperty::Flagged),
  16. m_eSearchOption(SearchOption::FLAGGED)
  17. {
  18. }
  19. bool CIMAPClient::CleanupSession()
  20. {
  21. m_pstrText = nullptr;
  22. return CMailClient::CleanupSession();
  23. }
  24. bool CIMAPClient::List(std::string& strList, const std::string& strFolderName)
  25. {
  26. m_strFolderName = strFolderName;
  27. m_pstrText = &strList;
  28. m_eOperationType = IMAP_LIST;
  29. return Perform();
  30. }
  31. bool CIMAPClient::ListSubFolders(std::string& strList)
  32. {
  33. m_pstrText = &strList;
  34. m_eOperationType = IMAP_LSUB;
  35. return Perform();
  36. }
  37. bool CIMAPClient::SendString(const std::string& strMail)
  38. {
  39. m_strMail = strMail;
  40. m_eOperationType = IMAP_SEND_STRING;
  41. return Perform();
  42. }
  43. bool CIMAPClient::SendFile(const std::string& strPath)
  44. {
  45. m_strLocalFile = strPath;
  46. m_eOperationType = IMAP_SEND_FILE;
  47. return Perform();
  48. }
  49. bool CIMAPClient::GetString(const std::string& strMsgNumber, std::string& strOutput)
  50. {
  51. m_strMsgNumber = strMsgNumber;
  52. m_pstrText = &strOutput;
  53. m_eOperationType = IMAP_RETR_STRING;
  54. return Perform();
  55. }
  56. bool CIMAPClient::GetFile(const std::string& strMsgNumber, const std::string& strFilePath)
  57. {
  58. m_strMsgNumber = strMsgNumber;
  59. m_strLocalFile = strFilePath;
  60. m_eOperationType = IMAP_RETR_FILE;
  61. return Perform();
  62. }
  63. bool CIMAPClient::DeleteFolder(const std::string& strFolderName)
  64. {
  65. m_strFolderName = strFolderName;
  66. m_eOperationType = IMAP_DELETE_FOLDER;
  67. return Perform();
  68. }
  69. bool CIMAPClient::Noop()
  70. {
  71. m_eOperationType = IMAP_NOOP;
  72. return Perform();
  73. }
  74. bool CIMAPClient::CopyMail(const std::string& strMsgNumber, const std::string& strFolderName)
  75. {
  76. m_strMsgNumber = strMsgNumber;
  77. m_strFolderName = strFolderName;
  78. m_eOperationType = IMAP_COPY;
  79. return Perform();
  80. }
  81. bool CIMAPClient::CreateFolder(const std::string& strFolderName)
  82. {
  83. m_strFolderName = strFolderName;
  84. m_eOperationType = IMAP_CREATE;
  85. return Perform();
  86. }
  87. bool CIMAPClient::SetMailProperty(const std::string& strMsgNumber, MailProperty eNewProperty)
  88. {
  89. m_strMsgNumber = strMsgNumber;
  90. m_eMailProperty = eNewProperty;
  91. m_eOperationType = IMAP_STORE;
  92. return Perform();
  93. }
  94. bool CIMAPClient::Search(std::string& strRes, SearchOption eSearchOption)
  95. {
  96. m_pstrText = &strRes;
  97. m_eSearchOption = eSearchOption;
  98. m_eOperationType = IMAP_SEARCH;
  99. return Perform();
  100. }
  101. bool CIMAPClient::InfoFolder(std::string& strFolderName, std::string& strInfo)
  102. {
  103. m_strFolderName = strFolderName;
  104. m_pstrText = &strInfo;
  105. m_eOperationType = IMAP_INFO_FOLDER;
  106. return Perform();
  107. }
  108. void CIMAPClient::ParseURL(std::string& strURL)
  109. {
  110. std::string strTmp = strURL;
  111. std::transform(strTmp.begin(), strTmp.end(), strTmp.begin(), ::toupper);
  112. if (strTmp.compare(0, 8, "IMAPS://") == 0 || strTmp.compare(0, 7, "IMAP://") == 0)
  113. {
  114. if (m_eSslTlsFlags != SslTlsFlag::ENABLE_SSL)
  115. m_eSslTlsFlags = SslTlsFlag::ENABLE_SSL;
  116. }
  117. else if (m_eSslTlsFlags == SslTlsFlag::ENABLE_SSL)
  118. {
  119. strURL.insert(0, "imaps://");
  120. }
  121. else
  122. strURL.insert(0, "imap://");
  123. /* append a '/' to the end of the URL so that we can easily add the message number
  124. * at the end of it */
  125. if (strURL.at(strURL.length() - 1) != '/')
  126. strURL += '/';
  127. }
  128. /**
  129. * @brief configures the curl session according to requested
  130. * IMAp operation.
  131. *
  132. *
  133. * @retval true Successfully configured the curl session.
  134. * @retval false The configuration couldn't be performed.
  135. *
  136. */
  137. bool CIMAPClient::PrePerform()
  138. {
  139. std::string strRequestURL(m_strURL);
  140. std::string strCmd; // for SEARCH or STORE
  141. //curl_off_t fsize;
  142. //size_t uFileSize = 0;
  143. //size_t uCountLF = 0;
  144. m_ssString.clear();
  145. switch (m_eOperationType)
  146. {
  147. case IMAP_SEND_STRING:
  148. /* This will create a new message 100. Note that you should perform an
  149. * EXAMINE command to obtain the UID of the next message to create and a
  150. * SELECT to ensure you are creating the message in the OUTBOX. */
  151. strRequestURL += m_strMsgNumber;
  152. m_ssString.str(m_strMail);
  153. /* LF will be replaced by CRLF when sending the mail.
  154. * This must be taken in consideration in the total size of the payload */
  155. /*uCountLF = std::count_if(m_strMail.cbegin(), m_strMail.cend(),
  156. [](const char c) { return c == '\n'; });*/
  157. //curl_easy_setopt(m_pCurlSession, CURLOPT_INFILESIZE, uCountLF + m_strMail.length());
  158. curl_easy_setopt(m_pCurlSession, CURLOPT_READFUNCTION, ReadLineFromStringStreamCallback);
  159. curl_easy_setopt(m_pCurlSession, CURLOPT_READDATA, &m_ssString);
  160. curl_easy_setopt(m_pCurlSession, CURLOPT_UPLOAD, 1L);
  161. break;
  162. case IMAP_SEND_FILE:
  163. if (!m_strLocalFile.empty())
  164. {
  165. // Request file size
  166. /*struct stat file_info;
  167. if (stat(m_strLocalFile.c_str(), &file_info))
  168. {
  169. if (m_eSettingsFlags & ENABLE_LOG)
  170. m_oLog(StringFormat("[IMAPClient][Error] Unable to request local file size "
  171. "%s : %s - in CIMAPClient::PrePerform() in case IMAP_SEND_FILE.", m_strLocalFile.c_str(),
  172. strerror(errno)));
  173. return false;
  174. }
  175. fsize = (curl_off_t)file_info.st_size;*/
  176. m_fLocalFile.open(m_strLocalFile, std::fstream::in);
  177. // LF will be replaced by CRLF when sending the mail
  178. /*uCountLF = std::count_if((std::istreambuf_iterator<char>(m_fLocalFile)),
  179. std::istreambuf_iterator<char>(),
  180. [&uFileSize](const char c) -> bool
  181. { ++uFileSize; return c == '\n'; });*/
  182. if (m_fLocalFile)
  183. {
  184. m_fLocalFile.seekg(0);
  185. curl_easy_setopt(m_pCurlSession, CURLOPT_READFUNCTION, CMailClient::ReadLineFromFileStreamCallback);
  186. curl_easy_setopt(m_pCurlSession, CURLOPT_READDATA, &m_fLocalFile);
  187. curl_easy_setopt(m_pCurlSession, CURLOPT_UPLOAD, 1L);
  188. //curl_easy_setopt(m_pCurlSession, CURLOPT_INFILESIZE_LARGE, uFileSize + uCountLF);
  189. }
  190. else
  191. {
  192. if (m_eSettingsFlags & ENABLE_LOG)
  193. m_oLog(StringFormat("[IMAPClient][Error] Unable to open local file %s in CIMAPClient::PrePerform()"
  194. "in case IMAP_SEND_FILE.", m_strLocalFile.c_str()));
  195. return false;
  196. }
  197. }
  198. else
  199. return false;
  200. break;
  201. case IMAP_NOOP:
  202. /* Set the NOOP command */
  203. curl_easy_setopt(m_pCurlSession, CURLOPT_CUSTOMREQUEST, "NOOP");
  204. /* Do not perform a transfer as NOOP returns no data */
  205. curl_easy_setopt(m_pCurlSession, CURLOPT_NOBODY, 1L);
  206. break;
  207. case IMAP_LIST:
  208. if (m_pstrText != nullptr)
  209. {
  210. if (!m_strFolderName.empty())
  211. strRequestURL += m_strFolderName;
  212. curl_easy_setopt(m_pCurlSession, CURLOPT_WRITEFUNCTION, &CMailClient::WriteInStringCallback);
  213. curl_easy_setopt(m_pCurlSession, CURLOPT_WRITEDATA, m_pstrText);
  214. }
  215. else
  216. return false;
  217. break;
  218. case IMAP_DELETE_FOLDER:
  219. /* You can specify the message either in the URL or DELE command */
  220. if (!m_strFolderName.empty())
  221. {
  222. /* Set the DELE command */
  223. curl_easy_setopt(m_pCurlSession, CURLOPT_CUSTOMREQUEST, ("DELETE " + m_strFolderName).c_str());
  224. }
  225. else
  226. return false;
  227. break;
  228. case IMAP_RETR_STRING:
  229. if (!m_strMsgNumber.empty())
  230. strRequestURL += "INBOX;MAILINDEX=" + m_strMsgNumber;
  231. else
  232. return false;
  233. /* This will retrieve message 'm_strMsgNumber' from the user's mailbox */
  234. if (m_pstrText != nullptr)
  235. {
  236. curl_easy_setopt(m_pCurlSession, CURLOPT_WRITEFUNCTION, &CMailClient::WriteInStringCallback);
  237. curl_easy_setopt(m_pCurlSession, CURLOPT_WRITEDATA, m_pstrText);
  238. }
  239. else
  240. return false;
  241. break;
  242. case IMAP_RETR_FILE:
  243. if (!m_strMsgNumber.empty())
  244. strRequestURL += "INBOX/;MAILINDEX=" + m_strMsgNumber;
  245. else
  246. return false;
  247. m_fLocalFile.open(m_strLocalFile, std::fstream::out | std::fstream::binary | std::fstream::trunc);
  248. if (m_fLocalFile)
  249. {
  250. curl_easy_setopt(m_pCurlSession, CURLOPT_WRITEFUNCTION, &CMailClient::WriteToFileCallback);
  251. curl_easy_setopt(m_pCurlSession, CURLOPT_WRITEDATA, &m_fLocalFile);
  252. }
  253. else
  254. {
  255. if (m_eSettingsFlags & ENABLE_LOG)
  256. m_oLog(StringFormat("[IMAPClient][Error] Unable to open local file %s in CIMAPClient::PrePerform()"
  257. " in case IMAP_RETR_FILE.", m_strLocalFile.c_str()));
  258. return false;
  259. }
  260. break;
  261. case IMAP_INFO_FOLDER:
  262. if (m_pstrText != nullptr)
  263. {
  264. curl_easy_setopt(m_pCurlSession, CURLOPT_WRITEFUNCTION, &CMailClient::WriteInStringCallback);
  265. curl_easy_setopt(m_pCurlSession, CURLOPT_WRITEDATA, m_pstrText);
  266. }
  267. else
  268. return false;
  269. /* Set the EXAMINE command specifing the mailbox folder */
  270. curl_easy_setopt(m_pCurlSession, CURLOPT_CUSTOMREQUEST, ("EXAMINE " + m_strFolderName).c_str());
  271. break;
  272. case IMAP_LSUB:
  273. if (m_pstrText != nullptr)
  274. {
  275. curl_easy_setopt(m_pCurlSession, CURLOPT_WRITEFUNCTION, &CMailClient::WriteInStringCallback);
  276. curl_easy_setopt(m_pCurlSession, CURLOPT_WRITEDATA, m_pstrText);
  277. }
  278. else
  279. return false;
  280. /* Set the LSUB command. Note the syntax is very similar to that of a LIST
  281. command. */
  282. curl_easy_setopt(m_pCurlSession, CURLOPT_CUSTOMREQUEST, "LSUB \"\" *");
  283. break;
  284. case IMAP_COPY:
  285. if (!m_strMsgNumber.empty() && !m_strFolderName.empty())
  286. {
  287. strRequestURL += "INBOX";
  288. /* Set the COPY command specifing the message ID and destination folder */
  289. curl_easy_setopt(m_pCurlSession, CURLOPT_CUSTOMREQUEST,
  290. ("COPY "+ m_strMsgNumber + " " + m_strFolderName).c_str());
  291. /* Note that to perform a move operation you will need to perform the copy,
  292. * then mark the original mail as Deleted and EXPUNGE or CLOSE. Please see
  293. * imap-store.c for more information on deleting messages. */
  294. }
  295. else
  296. return false;
  297. break;
  298. case IMAP_CREATE:
  299. if (!m_strFolderName.empty())
  300. {
  301. /* Set the CREATE command specifing the new folder name */
  302. curl_easy_setopt(m_pCurlSession, CURLOPT_CUSTOMREQUEST, ("CREATE " + m_strFolderName).c_str());
  303. }
  304. else
  305. return false;
  306. break;
  307. case IMAP_SEARCH:
  308. if (m_pstrText != nullptr)
  309. {
  310. curl_easy_setopt(m_pCurlSession, CURLOPT_WRITEFUNCTION, &CMailClient::WriteInStringCallback);
  311. curl_easy_setopt(m_pCurlSession, CURLOPT_WRITEDATA, m_pstrText);
  312. }
  313. else
  314. return false;
  315. strRequestURL += "INBOX";
  316. if (m_eSearchOption == SearchOption::ANSWERED)
  317. strCmd = "ANSWERED";
  318. else if (m_eSearchOption == SearchOption::DELETED)
  319. strCmd = "DELETED";
  320. else if (m_eSearchOption == SearchOption::DRAFT)
  321. strCmd = "DRAFT";
  322. else if (m_eSearchOption == SearchOption::FLAGGED)
  323. strCmd = "FLAGGED";
  324. else if (m_eSearchOption == SearchOption::NEW)
  325. strCmd = "NEW";
  326. else if (m_eSearchOption == SearchOption::RECENT)
  327. strCmd = "RECENT";
  328. else if (m_eSearchOption == SearchOption::SEEN)
  329. strCmd = "SEEN";
  330. else if (m_eSearchOption == SearchOption::UNSEEN)
  331. strCmd = "UNSEEN";
  332. else
  333. {
  334. return false;
  335. }
  336. /* Set the SEARCH command specifing what we want to search for. Note that
  337. * this can contain a message sequence set and a number of search criteria
  338. * keywords including flags such as ANSWERED, DELETED, DRAFT, FLAGGED, NEW,
  339. * RECENT and SEEN. For more information about the search criteria please
  340. * see RFC-3501 section 6.4.4. */
  341. curl_easy_setopt(m_pCurlSession, CURLOPT_CUSTOMREQUEST, ("SEARCH " + strCmd).c_str());
  342. break;
  343. case IMAP_STORE:
  344. if (!m_strMsgNumber.empty())
  345. {
  346. if (m_eMailProperty == MailProperty::Deleted)
  347. strCmd = "Deleted";
  348. else if (m_eMailProperty == MailProperty::Seen)
  349. strCmd = "Seen";
  350. else if (m_eMailProperty == MailProperty::Answered)
  351. strCmd = "Answered";
  352. else if (m_eMailProperty == MailProperty::Flagged)
  353. strCmd = "Flagged";
  354. else if (m_eMailProperty == MailProperty::Draft)
  355. strCmd = "Draft";
  356. else if (m_eMailProperty == MailProperty::Recent)
  357. strCmd = "Recent";
  358. else
  359. {
  360. return false;
  361. }
  362. strRequestURL += "INBOX";
  363. /* Set the STORE command with the Deleted flag for message m_strMsgNumber */
  364. curl_easy_setopt(m_pCurlSession, CURLOPT_CUSTOMREQUEST,
  365. ("STORE " + m_strMsgNumber + " +Flags \\" + strCmd).c_str());
  366. }
  367. else
  368. return false;
  369. break;
  370. default:
  371. if (m_eSettingsFlags & ENABLE_LOG)
  372. m_oLog("[IMAPClient][Error] Unknown operation.");
  373. break;
  374. }
  375. curl_easy_setopt(m_pCurlSession, CURLOPT_URL, strRequestURL.c_str());
  376. return true;
  377. }
  378. /**
  379. * @brief performs operations that need to be done after performing
  380. * an IMAP request.
  381. *
  382. *
  383. * @retval true Successfully performed post request operations.
  384. * @retval false The post request operations couldn't be performed.
  385. *
  386. */
  387. bool CIMAPClient::PostPerform(CURLcode ePerformCode)
  388. {
  389. if (m_eOperationType == IMAP_SEND_FILE || m_eOperationType == IMAP_RETR_FILE)
  390. if (m_fLocalFile.is_open())
  391. m_fLocalFile.close();
  392. if (m_eOperationType == IMAP_STORE)
  393. {
  394. if (ePerformCode != CURLE_OK)
  395. {
  396. /* Set the EXPUNGE command, although you can use the CLOSE command if you
  397. * don't want to know the result of the STORE */
  398. curl_easy_setopt(m_pCurlSession, CURLOPT_CUSTOMREQUEST, "EXPUNGE");
  399. /* Perform the second custom request */
  400. ePerformCode = curl_easy_perform(m_pCurlSession);
  401. /* Check for errors */
  402. if (ePerformCode != CURLE_OK)
  403. if (m_eSettingsFlags & ENABLE_LOG)
  404. m_oLog(StringFormat(LOG_ERROR_CURL_PEFORM_FAILURE_FORMAT, ePerformCode, curl_easy_strerror(ePerformCode)));
  405. }
  406. }
  407. return true;
  408. }