2 ** \file SmtpdSocket.cpp
4 ** \author grymse@alhem.net
7 Copyright (C) 2007 Anders Hedstrom
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "SmtpdSocket.h"
27 #ifdef SOCKETS_NAMESPACE
28 namespace SOCKETS_NAMESPACE
{
31 SmtpdSocket::SmtpdSocket(ISocketHandler
& h
)
41 void SmtpdSocket::OnAccept()
43 Send("220 ESMTP; \r\n");
47 void SmtpdSocket::OnLine(const std::string
& line
)
55 if (m_header_line
.size())
57 Parse
pa(m_header_line
, ":");
58 std::string key
= pa
.getword();
59 OnHeader(key
, pa
.getrest());
65 if (line
[0] == ' ' || line
[0] == '\t')
67 m_header_line
+= line
;
71 if (m_header_line
.size())
73 Parse
pa(m_header_line
, ":");
74 std::string key
= pa
.getword();
75 OnHeader(key
, pa
.getrest());
87 Send("550 Failed\r\n");
90 if (line
.size() && line
[0] == '.')
92 OnData(line
.substr(1));
101 std::string cmd
= Utility::ToUpper(pa
.getword());
105 if (!OnHello(pa
.getrest()))
107 Send("550 Failed\r\n");
112 Send("250 mail.alhem.net\r\n");
118 if (!OnHello(pa
.getrest()))
120 Send("550 Failed\r\n");
125 Send("250 mail.alhem.net\r\n");
131 OnAbort(SMTP_NO_HELLO
);
135 if (cmd
== "MAIL") // mail from:
138 pa
.getword(); // 'mail'
139 pa
.getword(); // 'from'
140 std::string email
= Utility::ToLower(pa
.getrest());
142 EmailAddress
addr( email
);
143 if (addr
.GetName().size() > 64)
145 OnAbort(SMTP_NAME_TOO_LONG
);
146 Send("500 Name too long.\r\n");
149 if (addr
.GetDomain().size() > 64)
151 OnAbort(SMTP_DOMAIN_TOO_LONG
);
152 Send("500 Domain too long.\r\n");
156 if (!OnMailFrom( addr
))
158 Send("550 Failed\r\n");
166 if (cmd
== "RCPT") // rcpt to:
169 pa
.getword(); // 'rcpt'
170 pa
.getword(); // 'to'
171 std::string email
= Utility::ToLower(pa
.getrest());
172 // %! reject based on user / domain?
173 EmailAddress
addr( email
);
175 if (addr
.GetName().size() > 64)
177 OnAbort(SMTP_NAME_TOO_LONG
);
178 Send("500 Name too long.\r\n");
181 if (addr
.GetDomain().size() > 64)
183 OnAbort(SMTP_DOMAIN_TOO_LONG
);
184 Send("500 Domain too long.\r\n");
188 if (!OnRcptTo( addr
))
190 Send("553 Failed\r\n");
200 Send("354 Enter mail, end with \".\" on a line by itself\r\n");
210 Send("250 OK\r\n"); // %! ???
216 Send("221 Bye Bye\r\n");
226 OnNotSupported(cmd
, pa
.getrest());
231 #ifdef SOCKETS_NAMESPACE
232 } // namespace SOCKETS_NAMESPACE {