-
Notifications
You must be signed in to change notification settings - Fork 9
/
smtptalker.cc
158 lines (139 loc) · 3.72 KB
/
smtptalker.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
PowerMail versatile mail receiver
Copyright (C) 2002-2007 PowerDNS.COM BV
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "smtptalker.hh"
#include "session.hh"
#include "logger.hh"
#include "ahuexception.hh"
#include <sstream>
#include "misc.hh"
extern Logger L;
SmtpTalker::SmtpTalker(const string &dest, u_int16_t port, const string &from, int timeout) : d_inmsg(0)
{
d_session=0;
try {
d_session=new Session(dest, port, timeout);
d_session->beVerbose();
string line;
d_session->getLine(line);
if(line.find("220 ")) {
d_session->close();
delete d_session;
d_session=0;
throw SmtpTalkerException("Unable to connect: "+line);
}
d_session->putLine("HELO "+getHostname()+"\r\n");
d_session->getLine(line);
if(line.find("2")) {
d_session->close();
delete d_session;
d_session=0;
throw SmtpTalkerException("Sent HELO, did not get correct response: "+line);
}
d_session->putLine("MAIL From:<"+from+">\r\n");
d_session->getLine(line);
if(line.find("2")) {
d_session->close();
delete d_session;
d_session=0;
throw SmtpTalkerException("Sent HELO, did not get correct response: "+line);
}
}
catch (SessionException &e){
if(d_session) {
d_session->close();
delete d_session;
d_session=0;
}
throw SmtpTalkerException("Session error connecting: "+e.d_reason);
}
}
SmtpTalker::~SmtpTalker()
{
if(d_session) {
d_session->close();
delete d_session;
d_session=0;
}
}
int SmtpTalker::addRecipient(const string &recip, const string &)
{
if(d_inmsg)
throw SmtpTalkerException("SmtpTalker accepts only a single recipient at a time!");
string line;
try {
d_session->putLine("rcpt to:<"+recip+">\r\n");
d_session->getLine(line);
if(line.find("2"))
throw SmtpTalkerException("Unable to add recipient: '"+recip+"' "+line);
startData();
}
catch(SessionException &e) {
throw SmtpTalkerException("Session Error: "+e.d_reason);
}
return 0;
}
void SmtpTalker::startData()
{
if(d_inmsg)
return;
string line;
try {
d_session->putLine("DATA\r\n");
d_session->getLine(line);
}
catch(SessionException &e) {
throw SmtpTalkerException("Session Error adding a line: "+e.d_reason);
}
if(line.find("3"))
throw SmtpTalkerException("Unable start sending message data: "+line);
d_inmsg++;
}
int SmtpTalker::msgAddLine(const string &line)
{
if(!d_inmsg++)
startData();
try {
d_session->putLine(line);
}
catch(SessionException &e) {
throw SmtpTalkerException("Session Error adding a line: "+e.d_reason);
}
return 0;
}
int SmtpTalker::msgDone()
{
try {
d_session->putLine(".\r\n");
string line;
d_session->getLine(line);
if(line.find("2")) {
stripLine(line);
throw SmtpTalkerException("Remote indicated error accepting message: "+line);
}
}
catch(SessionException &e) {
throw SmtpTalkerException("Session Error committing message: "+e.d_reason);
}
return 0;
}
int SmtpTalker::msgRollback()
{
try {
d_session->close();
}
catch(AhuException &e) {
}
return 0;
}