Advertisement
Ghostriax-Atrocity

SMTP.h

Apr 11th, 2015
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. #ifndef SMTP_H
  2. #define SMTP_H
  3.  
  4. #define SMTP_CONNECT_TIMEOUT 7
  5. class SMTP
  6. {
  7. public:
  8. enum AddressType
  9. {
  10. TO = 0,
  11. CC,
  12. BCC,
  13. FROM,
  14. RETURN
  15. };
  16. // A few of those could use bitmasks...
  17. enum Status
  18. {
  19. NOTHING = 0,
  20. CONNECTED, // Just the raw TCP Connection
  21. BANNER_RECVIVED,
  22. EHLO_ACCEPTED,
  23. AUTHENTICATED,
  24. SENDER_ACCEPTED,
  25. RECIPENTS_ACCEPTED,
  26. DATA_ACCEPTED,
  27. DELIVERY_SUCCESSFUL
  28. };
  29. enum Errors
  30. {
  31. NONE = 0,
  32. CANNOT_CONNECT,
  33. REJECT_ON_CONNECT,
  34. HELO_NOT_ACCEPTED,
  35. SSL_HELO_NOT_ACCEPTED,
  36. LOGIN_NOT_POSSIBLE,
  37. CREDENTIALS_WRONG,
  38. SSL_ERROR_SERVER_NOT_READY,
  39. SSL_ERROR_CONNECT,
  40. SENDER_NOT_ACCEPTED,
  41. RECIPENTS_NOT_ACCEPTED,
  42. DATA_NOT_ACCEPTED,
  43. SPAM_MESSAGE_DETECTED,
  44. GREYLISTED,
  45. CONNECTION_DROPPED
  46. };
  47. struct Address
  48. {
  49. std::string a;
  50. std::string friendlyName;
  51. };
  52.  
  53. SMTP();
  54. ~SMTP();
  55. bool connect(std::string server, std::string ehloName, bool useAuthenticatedPort);
  56. bool connect(std::vector<MX_Information> servers, std::string ehloName, bool useAuthenticatedConnection);
  57.  
  58. VOID add_address(Address address, AddressType type);
  59.  
  60. bool send();
  61.  
  62. bool supports_feature(std::string feature);
  63. void print_supported_functions()
  64. {
  65. for(std::vector<std::string>::iterator i = supportedFunctions.begin(); i != supportedFunctions.end(); i++)
  66. printf("%s\n", i->c_str());
  67. }
  68.  
  69. char* get_reply_code()
  70. {
  71. buffer[3] = 0;
  72. return buffer;
  73. }
  74.  
  75. char* get_reply_message()
  76. {
  77. return &buffer[4];
  78. }
  79.  
  80. VOID send_NOOP();
  81.  
  82. // Crypto & Auth
  83. bool is_login_possible();
  84. bool is_TLS_possible();
  85. bool switch_to_TLS();
  86. bool authenticate(std::string user, std::string password);
  87.  
  88. //Fixme: Remove. Only used in TestSMTPError (Todo: Make TestSMTPError a friend?)
  89. void send_command(std::string command)
  90. {
  91. sock->Send(command);
  92. recv_reply();
  93. }
  94.  
  95. void add_attachment(std::string data, std::string name, std::string type);
  96. void add_attachment(const unsigned char* data, size_t len, std::string name, std::string type);
  97.  
  98. void set_subject(std::string subj){subject = subj;}
  99. void set_message(std::string msg){message = msg;}
  100. void set_HTML_message(std::string msg){htmlMessage = msg;}
  101. bool is_connected()
  102. {
  103. return connected;
  104. }
  105. int get_error()
  106. {
  107. if(sock->GetConnectionClosed())
  108. currentError = CONNECTION_DROPPED;
  109. return currentError;
  110. }
  111. int get_status()
  112. {
  113. return currentStatus;
  114. }
  115. private:
  116. std::string subject;
  117. std::string message, htmlMessage;
  118. struct Attachment
  119. {
  120. std::string data;
  121. std::string name;
  122. std::string type;
  123. };
  124. std::vector<Attachment> attachments;
  125.  
  126. // Used later when switching to SSL
  127. std::string ehloName;
  128. std::string server;
  129.  
  130. bool connected;
  131. bool authenticated;
  132. bool usingSSL;
  133. Status currentStatus;
  134. Errors currentError;
  135.  
  136. char errorCode[4];
  137.  
  138. AbstractSocket* sock;
  139.  
  140. std::vector<Address> toAddress;
  141. std::vector<Address> ccAddress;
  142. std::vector<Address> bccAddress;
  143. Address fromAddress;
  144. Address returnAddress;
  145.  
  146. std::vector<std::string> supportedFunctions;
  147. std::string heloMessage;
  148.  
  149. char buffer[0x1000];
  150. size_t length;
  151.  
  152. bool get_supported_functions();
  153.  
  154. VOID recv_reply();
  155. bool is_successful();
  156. bool is_successful(char* successStatusCode);
  157.  
  158. void get_date(std::string& res);
  159. // TODO: Header template?
  160. void build_envelope_header(std::string& header);
  161.  
  162. void format_address(Address& a, std::string& res);
  163. // TODO: Find out which triggered the error? Which error?
  164. bool send_recipents(std::vector<Address>& addresses);
  165. void add_envelope_recipent(std::string recipentType, std::vector<Address>& address, std::string& res);
  166.  
  167. // Computes the boundary from the message or the from Address
  168. std::string get_boundary(std::string seed);
  169. void build_message_body(std::string& body);
  170. };
  171. #endif // SMTP_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement