| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #ifndef GUARD_SMTP_CLIENT_SMTP_CLIENT_HH |
|---|
| 21 | #define GUARD_SMTP_CLIENT_SMTP_CLIENT_HH |
|---|
| 22 | |
|---|
| 23 | #include <boost/asio.hpp> |
|---|
| 24 | #include <boost/thread/mutex.hpp> |
|---|
| 25 | #include <boost/enable_shared_from_this.hpp> |
|---|
| 26 | #include <boost/noncopyable.hpp> |
|---|
| 27 | #include <list> |
|---|
| 28 | #include <string> |
|---|
| 29 | |
|---|
| 30 | namespace smtp_client |
|---|
| 31 | { |
|---|
| 32 | class smtp_client : private boost::noncopyable |
|---|
| 33 | , public boost::enable_shared_from_this<smtp_client> |
|---|
| 34 | { |
|---|
| 35 | struct envelope |
|---|
| 36 | { |
|---|
| 37 | std::string sender; |
|---|
| 38 | std::string recipient; |
|---|
| 39 | std::string data; |
|---|
| 40 | }; |
|---|
| 41 | |
|---|
| 42 | class connection : private boost::noncopyable |
|---|
| 43 | { |
|---|
| 44 | public: |
|---|
| 45 | typedef boost::shared_ptr<connection> ptr; |
|---|
| 46 | connection(boost::shared_ptr<smtp_client>); |
|---|
| 47 | static void start(ptr); |
|---|
| 48 | private: |
|---|
| 49 | boost::shared_ptr<smtp_client> sc; |
|---|
| 50 | |
|---|
| 51 | boost::asio::ip::tcp::resolver res; |
|---|
| 52 | boost::asio::ip::tcp::resolver::query que; |
|---|
| 53 | boost::asio::ip::tcp::socket serv; |
|---|
| 54 | |
|---|
| 55 | enum state_type { BANNER, |
|---|
| 56 | EHLO, |
|---|
| 57 | HELO, |
|---|
| 58 | RSET, |
|---|
| 59 | MAIL_FROM, |
|---|
| 60 | RCPT_TO, |
|---|
| 61 | DATA, |
|---|
| 62 | DATA_354, |
|---|
| 63 | QUIT |
|---|
| 64 | } state; |
|---|
| 65 | boost::shared_ptr<envelope> cur; |
|---|
| 66 | |
|---|
| 67 | boost::asio::streambuf readbuf; |
|---|
| 68 | |
|---|
| 69 | static void resolved( |
|---|
| 70 | ptr, |
|---|
| 71 | boost::system::error_code, |
|---|
| 72 | boost::asio::ip::tcp::resolver::iterator); |
|---|
| 73 | static void connected( |
|---|
| 74 | ptr self, |
|---|
| 75 | boost::asio::ip::tcp::resolver::iterator, |
|---|
| 76 | boost::system::error_code ec); |
|---|
| 77 | static void async_read_response(ptr self); |
|---|
| 78 | static void got_response(ptr self, |
|---|
| 79 | std::string resp, |
|---|
| 80 | boost::system::error_code ec, |
|---|
| 81 | size_t bytes); |
|---|
| 82 | static void send_line(ptr self, std::string line); |
|---|
| 83 | static void send_raw(ptr self, std::string raw); |
|---|
| 84 | static void send_raw_no_log(ptr self, std::string raw); |
|---|
| 85 | static void sent( |
|---|
| 86 | ptr self, |
|---|
| 87 | boost::shared_ptr<boost::asio::const_buffers_1>, |
|---|
| 88 | boost::system::error_code ec, |
|---|
| 89 | size_t); |
|---|
| 90 | }; |
|---|
| 91 | |
|---|
| 92 | boost::asio::io_service &ios; |
|---|
| 93 | boost::shared_ptr<connection> active_connection; |
|---|
| 94 | |
|---|
| 95 | boost::mutex m; |
|---|
| 96 | std::list<boost::shared_ptr<envelope> > ready; |
|---|
| 97 | std::list<boost::shared_ptr<envelope> > deferred; |
|---|
| 98 | |
|---|
| 99 | void async_run_queue(); |
|---|
| 100 | public: |
|---|
| 101 | smtp_client(boost::asio::io_service &ios) |
|---|
| 102 | : ios(ios) |
|---|
| 103 | {} |
|---|
| 104 | |
|---|
| 105 | void send_mail(const std::list<std::string> &recipients, |
|---|
| 106 | std::string message); |
|---|
| 107 | void tick(bool); |
|---|
| 108 | }; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | #endif |
|---|