-
Notifications
You must be signed in to change notification settings - Fork 0
/
pop3.cpp
247 lines (238 loc) · 7.98 KB
/
pop3.cpp
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
#include "pop3.h"
#include "ui_pop3.h"
#include "Client.h"
bool Pop3Client::Connect(QString host, unsigned port)
{
m_sock.connectToHost(host,port);
if (!m_sock.waitForConnected(CONNECT_TIMEOUT))
{
return false;
}
QString response;
ReadResponse(false,response);
if (response.startsWith("+OK"))
state = Authorization;
else
return false;
return true;
}
bool Pop3Client::ReadResponse(bool isMultiline,QString& response)
{
char buff[1512+1]; // according to RFC1939 the response can only be 512 chars
bool couldRead = m_sock.waitForReadyRead( READ_TIMEOUT ) ;
//if (!couldRead) _ERROR("could not receive data: ");
bool complete=false;
bool completeLine=false;
unsigned int offset=0;
while (couldRead && !complete)
{
// qDebug() << "reading front\n";
if (offset >= sizeof(buff))
{
qDebug() << "avoiding buffer overflow, server is not RFC1939 compliant\n";
return false;
}
qint64 bytesRead = m_sock.readLine(buff + offset,sizeof(buff)-offset);
if (bytesRead == -1)
return false;
couldRead = bytesRead > 0;
completeLine = buff[offset+bytesRead-1]=='\n';
if (couldRead)
{
if (completeLine)
{
// qDebug() << "Line Complete\n";
offset = 0;
if (response.size() == 0)
{//first line, check for error
response.append(buff);
if (!response.startsWith("+OK"))
complete = true;
else
complete = !isMultiline;
}
else
{//NOT first line, check for byte-stuffing
//check if the response was complete
complete = ( strcmp(buff,".\r\n") == 0 );
if (!complete)
{
if (buff[0] == '.' && buff[1]== '.')
response.append(buff+1); //remove the stuffed byte and add to the response
else
response.append(buff); //add to the response
}
}
}
else
{
// qDebug() << "Line INComplete: " << buff <<"\n";
offset += bytesRead;
}
}
if (couldRead && !complete)
{
if (m_sock.bytesAvailable() <= 0)
{
// qDebug() << "waiting for data\n";
couldRead = m_sock.waitForReadyRead( READ_TIMEOUT ) ;
// qDebug() << "waiting for data finished, couldread: " << couldRead << "\n";
}
}
}
return couldRead && complete;
}
Pop3Client::Pop3Client(bool readOnly)
{
this->readOnly = readOnly;
state = NotConnected;
m_sock.blockSignals(true);
}
bool Pop3Client::Login(QString user, QString pwd)
{
if (!SendUser(user))
return false;
if (!SendPasswd(pwd))
return false;
state = Transaction;
return true;
}
bool Pop3Client::SendUser(QString& user)
{
QString res = doCommand("USER "+user+"\r\n",false);
if (res.startsWith("+OK"))
return true;
else
return false;
}
bool Pop3Client::SendPasswd(QString& pwd)
{
QString res = doCommand("PASS "+pwd+"\r\n",false);
if (res.startsWith("+OK"))
return true;
else
return false;
}
bool Pop3Client::Quit()
{
if (readOnly)
if (!ResetDeleted())
return false;
QString res = doCommand("QUIT\r\n",false);
if (res.startsWith("+OK"))
{
if (!m_sock.waitForDisconnected(DISCONNECT_TIMEOUT))
{
//_ERROR("Connection was not closed by server: ");
return false;
}
else
state = NotConnected;
return true;
}
else
{
return false;
}
}
QString Pop3Client::doCommand(QString command,bool isMultiline)
{
// qDebug() << "sending command: " << command << "\n";
QString response;
qint64 writeResult = m_sock.write(command.toAscii());
// if (writeResult != command.size()) qDebug()<<"Could not write all bytes: ";
if (writeResult > 0 && !m_sock.waitForBytesWritten(WRITE_TIMEOUT)) qDebug()<<"Could not write bytes from buffer: ";
if (!ReadResponse(isMultiline,response))
return "";
return response;
}
bool Pop3Client::GetMailboxStatus(int& nbMessages, int& totalSize)
{
QString res = doCommand("STAT\r\n",false);
if (res.startsWith("+OK"))
{
QStringList sl = res.split(' ',QString::SkipEmptyParts);
if (sl.count() < 3)
return false;
else
{
nbMessages = sl[1].toInt();
totalSize = sl[2].toInt();
}
return true;
}
else
return false;
}
bool Pop3Client::ResetDeleted()
{
QString res = doCommand("RSET\r\n",false);
if (res.startsWith("+OK"))
return true;
else
return false;
}
bool Pop3Client::NoOperation()
{
QString res = doCommand("NOOP\r\n",false);
if (res.startsWith("+OK"))
return true;
else
return false;
}
Pop3::Pop3(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Pop3)
{
ui->setupUi(this);
QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(MyEventHandler()));
QObject::connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(Authorize()));
client = new Pop3Client(true);
//QObject::connect(ui->pushButton_2, SIGNAL(clicked()), ui->textEdit_2, SLOT(setText(tr("Ïðîáóåì àâòîðèçîâàòüñÿ..."))));
}
bool Pop3Client::Delete(QString msgId)
{
if (readOnly)
return false;
QString res = doCommand("DELE "+msgId+"\r\n",false);
if (res.startsWith("+OK"))
return true;
else
return false;
}
bool Pop3Client::LoginWithDigest(QString user, QString digest)
{
QString res = doCommand("APOP "+user+" "+digest+"\r\n",false);
if (res.startsWith("+OK"))
return true;
else
return false;
}
void Pop3::MyEventHandler()
{
//model = new QString(this);
//QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(MyEventHandler()));
if (!client->Connect(ui->lineEdit->text(), 110))
{
ui->textEdit->setText(tr("Íå óäà¸òñÿ ïîäêëþ÷èòüñÿ ê ñåðâåðó"));
//ui->textEdit->setStyle(QStyle "color:red;");
}
else
{
ui->textEdit->setText(tr("Ïîäêëþ÷åíèå ê ñåðâåðó óñïåøíî âûïîëíåíî!"));
//ui->textEdit->setStyle("color:green;");
}
}
void Pop3::Authorize()
{
ui->textEdit_2->setText(tr("Ïðîáóåì àâòîðèçîâàòüñÿ..."));
if (!client->Login("ya.test.pop", "monstermail")) {
ui->textEdit_2->setText(tr("Íåâåðíûé ëîãèí èëè ïàðîëü!"));
} else {
ui->textEdit_2->setText(tr("Âû óñïåøíî àâòîðèçîâàíû!"));
}
}
Pop3::~Pop3()
{
delete ui;
}