qtimapclient.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * This file is part of QtEmailFetcher project: Email parser for C++ Qt.
  3. *
  4. * GPLv3+ (c) acetone, 2023
  5. */
  6. #include "qtimapclient.h"
  7. #include <QDebug>
  8. #include <QRegularExpression>
  9. #include <QDebug>
  10. QtImapClient::QtImapClient() : m_imapClient ([this](const std::string& strLogMsg) {
  11. qWarning() << "QtImapClient backend:" << strLogMsg.c_str();
  12. m_backEndErrors.push_back(strLogMsg.c_str());
  13. })
  14. {}
  15. QtImapClient::~QtImapClient()
  16. {
  17. m_imapClient.CleanupSession();
  18. }
  19. bool QtImapClient::checkUnseen(QList<unsigned int> &result)
  20. {
  21. if (not initConnection())
  22. {
  23. m_errorString = "Connection initialize failed";
  24. return false;
  25. }
  26. std::string out;
  27. bool status = m_imapClient.Search(out, CIMAPClient::SearchOption::UNSEEN);
  28. if (not status)
  29. {
  30. m_errorString = "Fetch unseen messages failed";
  31. return false;
  32. }
  33. static const QRegularExpression onlyNumbers("[^ 0-9]");
  34. QString string = QString::fromStdString(out.c_str());
  35. string.remove(onlyNumbers);
  36. while (string.contains(" ")) string.remove(" ");
  37. QStringList strList = string.split(' ');
  38. if (not result.isEmpty()) result.clear();
  39. QStringListIterator iter(strList);
  40. while (iter.hasNext())
  41. {
  42. bool ok = false;
  43. auto val = iter.next().toUInt(&ok);
  44. if (ok)
  45. {
  46. result.push_back(val);
  47. }
  48. }
  49. return true;
  50. }
  51. QSharedPointer<EmailDocument> QtImapClient::fetch(unsigned int mailIndex)
  52. {
  53. if (not initConnection())
  54. {
  55. m_errorString = "Connection initialize failed";
  56. return nullptr;
  57. }
  58. std::string output;
  59. bool fetchStatus = m_imapClient.GetString(std::to_string(mailIndex), output);
  60. if (not fetchStatus)
  61. {
  62. m_errorString = "Fetching failed";
  63. return nullptr;
  64. }
  65. QSharedPointer<EmailDocument> document(new EmailDocument);
  66. document->parse(output.c_str());
  67. return document;
  68. }
  69. bool QtImapClient::initConnection()
  70. {
  71. QMutexLocker lock (&m_mtxInit);
  72. if (m_inited) return true;
  73. CMailClient::SslTlsFlag ssl;
  74. if (m_connectionType == ConnectionType::PLAIN_TEXT)
  75. {
  76. ssl = CMailClient::SslTlsFlag::NO_SSLTLS;
  77. }
  78. else if (m_connectionType == ConnectionType::SSL)
  79. {
  80. ssl = CMailClient::SslTlsFlag::ENABLE_SSL;
  81. }
  82. else if (m_connectionType == ConnectionType::START_TLS)
  83. {
  84. ssl = CMailClient::SslTlsFlag::ENABLE_TLS;
  85. }
  86. else
  87. {
  88. qCritical() << __FUNCTION__ << "Unknown SSL status";
  89. }
  90. if (not m_proxy.isEmpty())
  91. {
  92. m_imapClient.SetProxy(m_proxy.toStdString());
  93. }
  94. m_inited = m_imapClient.InitSession((m_hostname + ":" + QString::number(m_port)).toStdString(),
  95. m_username.toStdString(), m_password.toStdString(),
  96. CMailClient::SettingsFlag::ALL_FLAGS, ssl);
  97. return m_inited;
  98. }