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"
28 SmtpdSocket::SmtpdSocket(ISocketHandler
& h
)
38 void SmtpdSocket::OnAccept()
40 Send("220 ESMTP; \r\n");
44 void SmtpdSocket::OnLine(const std::string
& line
)
52 if (m_header_line
.size())
54 Parse
pa(m_header_line
, ":");
55 std::string key
= pa
.getword();
56 OnHeader(key
, pa
.getrest());
62 if (line
[0] == ' ' || line
[0] == '\t')
64 m_header_line
+= line
;
68 if (m_header_line
.size())
70 Parse
pa(m_header_line
, ":");
71 std::string key
= pa
.getword();
72 OnHeader(key
, pa
.getrest());
84 Send("550 Failed\r\n");
87 if (line
.size() && line
[0] == '.')
89 OnData(line
.substr(1));
98 std::string cmd
= Utility::ToUpper(pa
.getword());
102 if (!OnHello(pa
.getrest()))
104 Send("550 Failed\r\n");
109 Send("250 mail.alhem.net\r\n");
115 if (!OnHello(pa
.getrest()))
117 Send("550 Failed\r\n");
122 Send("250 mail.alhem.net\r\n");
128 OnAbort(SMTP_NO_HELLO
);
132 if (cmd
== "MAIL") // mail from:
135 pa
.getword(); // 'mail'
136 pa
.getword(); // 'from'
137 std::string email
= Utility::ToLower(pa
.getrest());
139 EmailAddress
addr( email
);
140 if (addr
.GetName().size() > 64)
142 OnAbort(SMTP_NAME_TOO_LONG
);
143 Send("500 Name too long.\r\n");
146 if (addr
.GetDomain().size() > 64)
148 OnAbort(SMTP_DOMAIN_TOO_LONG
);
149 Send("500 Domain too long.\r\n");
153 if (!OnMailFrom( addr
))
155 Send("550 Failed\r\n");
163 if (cmd
== "RCPT") // rcpt to:
166 pa
.getword(); // 'rcpt'
167 pa
.getword(); // 'to'
168 std::string email
= Utility::ToLower(pa
.getrest());
169 // %! reject based on user / domain?
170 EmailAddress
addr( email
);
172 if (addr
.GetName().size() > 64)
174 OnAbort(SMTP_NAME_TOO_LONG
);
175 Send("500 Name too long.\r\n");
178 if (addr
.GetDomain().size() > 64)
180 OnAbort(SMTP_DOMAIN_TOO_LONG
);
181 Send("500 Domain too long.\r\n");
185 if (!OnRcptTo( addr
))
187 Send("553 Failed\r\n");
197 Send("354 Enter mail, end with \".\" on a line by itself\r\n");
207 Send("250 OK\r\n"); // %! ???
213 Send("221 Bye Bye\r\n");
223 OnNotSupported(cmd
, pa
.getrest());