-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpptalker.cc
401 lines (332 loc) · 8.9 KB
/
pptalker.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
PowerMail versatile mail receiver
Copyright (C) 2002 - 2009 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 <stdlib.h>
#include "pptalker.hh"
#include "session.hh"
#include "logger.hh"
#include "ahuexception.hh"
#include <sstream>
#include "misc.hh"
extern Logger L;
PPTalker::PPTalker(const string &dest, u_int16_t port, const string &password, int timeout) : d_inmsg(0)
{
d_session=0;
d_usage=0;
try {
d_ppname=dest+":"+itoa(port);
d_session=new Session(dest, port, timeout);
d_session->beVerbose();
string line;
d_session->getLine(line);
if(line.find("+OK")) {
d_session->close();
delete d_session;
throw PPTalkerException("Unable to connect: "+line);
}
d_session->putLine("pass "+password+"\n");
d_session->getLine(line);
if(line.find("+OK")) {
d_session->close();
delete d_session;
throw PPTalkerException("Unable to authenticate with password '"+password+"': "+line);
}
}
catch (SessionException &e){
if(d_session) {
d_session->close();
delete d_session;
}
throw PPTalkerException(e.d_reason);
}
}
PPTalker::~PPTalker()
{
if(d_session) {
d_session->putLine("quit\n");
d_session->close();
delete d_session;
d_session=0;
}
}
int PPTalker::addRecipient(const string &recip, const string &index)
{
string line;
try {
d_session->putLine("rcpt "+recip+" "+index+"\n");
d_session->getLine(line);
if(line.find("+OK"))
throw PPTalkerException("Unable to add recipient: '"+recip+"' "+line);
}
catch(AhuException &e) {
throw PPTalkerException("Session Error: "+e.d_reason);
}
return 0;
}
int PPTalker::delMsg(const string &recip, const string &index)
{
string line;
try {
d_session->putLine("del "+recip+" "+index+"\n");
d_session->getLine(line);
if(line.find("+OK"))
throw PPTalkerException("Unable to delete '"+index+"' from mailbox '"+
recip+"'");
}
catch(AhuException &e) {
throw PPTalkerException("Session Error: "+e.d_reason);
}
return 0;
}
void PPTalker::listMboxes(vector<string> &mboxes)
{
string line;
try {
d_session->putLine("dir\n");
d_session->getLine(line);
if(line.find("+OK"))
throw PPTalkerException("Unable to request listing of mboxes: "+line);
for(;;){
d_session->getLine(line);
stripLine(line);
if(line==".")
break;
mboxes.push_back(line);
}
}
catch(AhuException &e) {
throw PPTalkerException("Session Error listing mboxes: "+e.d_reason);
}
}
int PPTalker::nukeMbox(const string &mbox)
{
string line;
try {
d_session->putLine("nuke "+mbox+"\n");
d_session->getLine(line);
if(line.find("+OK"))
throw PPTalkerException("Unable to purge mailbox '"+mbox+"': "+line);
}
catch(AhuException &e) {
throw PPTalkerException("Session Error nuking mailbox: "+e.d_reason);
}
return 0;
}
void PPTalker::startData()
{
string line;
try {
d_session->putLine("put\n");
d_session->getLine(line);
}
catch(SessionException &e) {
throw PPTalkerException("Session Error adding a line: "+e.d_reason);
}
if(line.find("+OK"))
throw PPTalkerException("Unable start sending message data: "+line);
d_inmsg=true;
}
int PPTalker::msgAddLine(const string &line)
{
if(!d_inmsg++)
startData();
try {
d_session->putLine(line);
}
catch(AhuException &e) {
throw PPTalkerException("Session Error adding a line: "+e.d_reason);
}
return 0;
}
void PPTalker::setReadonly(bool flag)
{
try {
string line;
d_session->putLine(flag ? "readonly\n" : "readwrite\n");
d_session->getLine(line);
if(line.find("+OK"))
throw PPTalkerException("Unable to set readonly state: "+line);
}
catch(SessionException &e) {
throw PPTalkerException("Session Error during readonly change: "+e.d_reason);
}
}
void PPTalker::getMsgs(const string &mbox, mboxInfo_t& mbi)
{
try {
string line;
d_session->putLine("ls "+mbox+"\n");
d_session->getLine(line);
if(line.find("+OK")) {
d_usage--;
throw PPTalkerException("Unable to list mailbox '"+mbox+"': "+line);
}
d_session->getLine(line);
while(line!=".\n") {
msgInfo_t msgi;
istringstream i(line);
i>>msgi.index;
i>>msgi.size;
msgi.ppname=d_ppname;
mbi.push_back(msgi);
d_session->getLine(line);
// push back into mbi
}
d_usage--;
}
catch(SessionException &e) {
throw PPTalkerException("Session Error adding a line: "+e.d_reason);
}
}
void PPTalker::blastMessageFD(const string &address, const string &index, size_t length, int fd, int limit)
{
try {
string line;
d_session->putLine("get "+address+" "+index+"\n");
d_session->getLine(line);
if(line.find("+OK"))
throw PPTalkerException("Unable to retrieve message "+address+" "+index);
if(limit==-1)
line="+OK "+itoa(length)+"\r\n";
else
line="+OK\r\n";
writen(fd,line.c_str(),line.size());
bool inheaders=true;
int lineno=0;
for(;;) {
d_session->getLine(line);
if(limit>=0) {
if(inheaders && (line[0]=='\r' || line[1]=='\n'))
inheaders=false;
else if(!inheaders)
lineno++;
}
if(line[0]=='.' && (line[1]=='\r' || line[1]=='\n')) {
writen(fd, ".\r\n",3);
break;
}
if(limit<0 || lineno<=limit)
writen(fd, line.c_str(),line.length());
}
}
catch(SessionException &e) {
throw PPTalkerException("Session Error retrieving message: "+e.d_reason);
}
}
void PPTalker::stat(int *kbytes, int *inodes,double *load)
{
try {
d_session->putLine("stat\n");
string line;
d_session->getLine(line);
if(line.find("+OK"))
throw PPTalkerException("Remote indicated error accepting ping: "+line);
//+OK 122669 kbytes 1096077 inodes 1.13 load
vector<string>words;
stringtok(words,line);
if(words.size()<3)
throw PPTalkerException("Invalid response to stat request");
*kbytes=atoi(words[1].c_str());
if(inodes && words.size()>4)
*inodes=atoi(words[3].c_str());
if(load && words.size()>6)
*load=strtod(words[5].c_str(),0);
}
catch(SessionException &e) {
throw PPTalkerException("Session Error doing ping: "+e.d_reason);
}
}
void PPTalker::ping(bool &readonly)
{
try {
if(!d_session)
throw PPTalkerException("Unable to ping, no session");
d_session->putLine("ping\n");
string line;
d_session->getLine(line);
if(line.find("+OK"))
throw PPTalkerException("Remote indicated error accepting ping: "+line);
readonly=(line.find("READONLY")!=string::npos);
}
catch(SessionException &e) {
throw PPTalkerException("Session Error doing ping: "+e.d_reason);
}
}
int PPTalker::msgDone()
{
try {
d_session->putLine(".\n");
string line;
d_session->getLine(line);
if(line.find("+OK"))
throw PPTalkerException("Remote indicated error accepting message: "+line);
d_session->putLine("commit\n");
d_session->getLine(line);
if(line.find("+OK"))
throw PPTalkerException("Remote indicated error accepting committing message: "+line);
}
catch(AhuException &e) {
throw PPTalkerException("Session Error adding a line: "+e.d_reason);
}
return 0;
}
int PPTalker::msgRollback()
{
try {
d_session->putLine(".\n");
string line;
d_session->getLine(line);
if(line.find("+OK"))
return 0; // killed anyhow :-)
d_session->putLine("rollback\n");
d_session->getLine(line);
line.find("+OK");
}
catch(AhuException &e) {
}
return 0;
}
#ifdef DRIVER
void print(const string &s)
{
cout<<s<<endl;
}
int main()
{
L.toConsole(true);
try {
PPTalker PP(0,1100);
PP.addRecipient("[email protected]",itoa(time(0)));
PP.addRecipient("[email protected]",itoa(time(0)*2));
PP.msgAddLine("From: [email protected]");
PP.msgAddLine("To: [email protected]");
PP.msgAddLine("");
PP.msgAddLine("Hallo, hallo!");
PP.msgAddLine("Regel 2");
PP.msgDone();
PPTalker::mboxInfo_t data;
PP.getMsgs("[email protected]", data);
PPTalker::mboxInfo_t::const_iterator i;
for(i=data.begin();
i!=data.end();
++i)
cout<<"'"<<i->index<<"', "<<i->size<<endl;
--i;
PP.blastMessageFD("[email protected]", i->index,0);
}
catch(PPTalkerException &e) {
L<<"Fatal error: "<<e.getReason()<<endl;
}
}
#endif