Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

MailHeaders.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // MailHeaders.cpp
  2. // Copyright (C) 2008 ARM Research Labs, LLC.
  3. // See www.armresearch.com for the copyright terms.
  4. //
  5. // This file contains the functions to extract the mail header and
  6. // body from the string returned by SNFMilterEngine::XHeaders().
  7. #include <stdexcept>
  8. #include <sstream>
  9. #include "MailHeaders.hpp"
  10. using namespace std;
  11. const string DefaultSMTPENDL = "\r\n"; ///< Default SMTP endline.
  12. const string DefaultLibmilterENDL = "\n "; ///< Default libmilter endline.
  13. const string DefaultPrefix = "X-"; ///< Default start of header line.
  14. MailHeaders::MailHeaders() :
  15. SMTPENDL(DefaultSMTPENDL),
  16. LibMilterENDL(DefaultLibmilterENDL) {
  17. }
  18. MailHeaders::~MailHeaders() {
  19. }
  20. void
  21. MailHeaders::loadHeaders(const string &HeaderBuffer) {
  22. Buffer = HeaderBuffer;
  23. }
  24. bool
  25. MailHeaders::getHeaderName(string *HeaderName) {
  26. if (Buffer.empty()) { // No header if the buffer is empty.
  27. return false;
  28. }
  29. string::size_type endIndex = Buffer.find(SMTPENDL);
  30. if (string::npos == endIndex) { // No SMPENDL.
  31. ostringstream Temp;
  32. Temp << "Invalid header line (no SMTP end-of-line): '"
  33. << Buffer << "'.";
  34. throw std::invalid_argument(Temp.str());
  35. }
  36. string Line(Buffer, 0, endIndex); // First line of the header.
  37. string::size_type colonIndex = Line.find(":");
  38. if (string::npos == colonIndex) { // No ":".
  39. ostringstream Temp;
  40. Temp << "Invalid header line (no ':'): '"
  41. << Buffer << "'.";
  42. throw std::invalid_argument(Temp.str());
  43. }
  44. HeaderName->assign(Line, 0, colonIndex);
  45. if (0 == HeaderName->size()) { // Null header name.
  46. ostringstream Temp;
  47. Temp << "Invalid header line (no name): '"
  48. << Buffer << "'.";
  49. throw std::invalid_argument(Temp.str());
  50. }
  51. Buffer.erase(0, colonIndex + 1); // Erase the header name and ":".
  52. return true;
  53. }
  54. bool
  55. MailHeaders::getHeaderLine(string *HeaderLine) {
  56. if (Buffer.empty()) { // No line if the buffer is empty.
  57. return false;
  58. }
  59. string::size_type EndIndex = Buffer.find(SMTPENDL);
  60. if (string::npos == EndIndex) { // No SMPENDL.
  61. ostringstream Temp;
  62. Temp << "Invalid header line (no SMTP end-of-line): '"
  63. << Buffer << "'.";
  64. throw std::invalid_argument(Temp.str());
  65. }
  66. HeaderLine->assign(Buffer, 0, EndIndex); // Tentative line of the body.
  67. string::size_type XIndex = HeaderLine->find("X-");
  68. if (XIndex == 0) { // Does it begin with "X-"?
  69. return false; // Yes; it's not the body line.
  70. }
  71. Buffer.erase(0, EndIndex + SMTPENDL.size()); // Erase line and SMTPENDL.
  72. return true;
  73. }
  74. bool
  75. MailHeaders::getNameBody(string *HeaderName, string *FormattedHeaderBody) {
  76. if (!getHeaderName(HeaderName)) { // Get the header.
  77. return false; // No more headers.
  78. }
  79. FormattedHeaderBody->clear();
  80. string HeaderLine; // One line of the body.
  81. bool FirstTime = true;
  82. while (getHeaderLine(&HeaderLine)) { // While this header has lines...
  83. if (FirstTime) {
  84. *FormattedHeaderBody = HeaderLine;
  85. FirstTime = false;
  86. } else {
  87. *FormattedHeaderBody += LibMilterENDL;
  88. *FormattedHeaderBody += HeaderLine;
  89. }
  90. }
  91. return true;
  92. }