Logo
~Sockets~
~Examples~
~Contact~


SmtpdSocket.h

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2007  Anders Hedstrom
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 */
00023 #ifndef _SOCKETS_SmtpdSocket_H
00024 #define _SOCKETS_SmtpdSocket_H
00025 
00026 #include "sockets-config.h"
00027 #include <string>
00028 #include "TcpSocket.h"
00029 
00030 #ifdef SOCKETS_NAMESPACE
00031 namespace SOCKETS_NAMESPACE {
00032 #endif
00033 
00034 
00036 class SmtpdSocket : public TcpSocket
00037 {
00038 protected:
00039         typedef enum {
00040                 SMTP_NO_HELLO,
00041                 SMTP_NAME_TOO_LONG,
00042                 SMTP_DOMAIN_TOO_LONG,
00043                 SMTP_QUIT,
00044         } reason_t;
00045 
00046 public:
00047         class EmailAddress {
00048         public:
00049                 EmailAddress(const std::string& str_in)
00050                 {
00051                         std::string str = str_in;
00052                         size_t i = str.find("<");
00053                         if (i != std::string::npos)
00054                                 str = str.substr(i + 1);
00055                         i = str.find("@");
00056                         if (i != std::string::npos)
00057                         {
00058                                 m_name = str.substr(0, i);
00059                                 str = str.substr(i + 1);
00060                                 i = str.find(">");
00061                                 if (i != std::string::npos)
00062                                         str = str.substr(0, i);
00063                                 m_domain = str;
00064                         }
00065                         while (m_name.size() && m_name[m_name.size() - 1] == ' ')
00066                                 m_name.resize(m_name.size() - 1);
00067                         while (m_domain.size() && m_domain[m_domain.size() - 1] == ' ')
00068                                 m_domain.resize(m_domain.size() - 1);
00069                         while (m_name.size() && m_name[0] == ' ')
00070                                 m_name = m_name.substr(1);
00071                         while (m_domain.size() && m_domain[0] == ' ')
00072                                 m_domain = m_domain.substr(1);
00073                 }
00074 
00075                 const std::string& GetName() const { return m_name; }
00076                 const std::string& GetDomain() const { return m_domain; }
00077 
00078                 std::string ToString() const { return m_name + "@" + m_domain; }
00079 
00080         private:
00081                 std::string m_name;
00082                 std::string m_domain;
00083         };
00084 
00085 public:
00086         SmtpdSocket(ISocketHandler&);
00087 
00088         void OnAccept();
00089         void OnLine(const std::string&);
00090 
00092         virtual bool OnHello(const std::string& domain) = 0;
00093 
00095         virtual bool OnMailFrom(const EmailAddress& addr) = 0;
00096 
00098         virtual bool OnRcptTo(const EmailAddress& addr) = 0;
00099 
00100         virtual void OnHeader(const std::string& key, const std::string& value) = 0;
00101 
00102         virtual void OnHeaderComplete() = 0;
00103 
00104         virtual void OnData(const std::string& line) = 0;
00105 
00107         virtual bool OnDataComplete() = 0;
00108 
00109         virtual void OnRset() = 0;
00110 
00111         virtual void OnAbort(reason_t) = 0;
00112 
00113         virtual void OnNotSupported(const std::string& cmd, const std::string& arg) = 0;
00114 
00115 private:
00116         std::string ToLower(const std::string& str);
00117         std::string ToUpper(const std::string& str);
00118 
00119         bool m_hello; // we need HELO or EHLO first of all
00120         bool m_data;
00121         bool m_header;
00122         std::string m_header_line;
00123 };
00124 
00125 
00126 #ifdef SOCKETS_NAMESPACE
00127 } // namespace SOCKETS_NAMESPACE {
00128 #endif
00129 
00130 #endif // _SOCKETS_SmtpdSocket_H
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4