-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPMaildServerBase.cpp
More file actions
173 lines (137 loc) · 4.23 KB
/
PMaildServerBase.cpp
File metadata and controls
173 lines (137 loc) · 4.23 KB
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
#include "PMaildServerBase.hpp"
#include "PMaildServer.hpp"
#include "PMaildCore.hpp"
#include <QMetaObject>
PMaildServerBase::PMaildServerBase(QSslSocket *_sock, PMaildCore *_core, PMaildServer *_server) {
sock = _sock;
core = _core;
server = _server;
connect(sock, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
connect(sock, SIGNAL(destroyed(QObject*)), this, SLOT(deleteLater()));
connect(sock, SIGNAL(encrypted()), this, SLOT(socketSslReady()));
connect(this, SIGNAL(destroyed(QObject*)), sock, SLOT(deleteLater()));
connect(&timer, SIGNAL(timeout()), this, SLOT(timeout()));
connect(core, SIGNAL(destroyed(QObject*)), this, SLOT(deleteLater()));
connect(server, SIGNAL(destroyed(QObject*)), this, SLOT(deleteLater()));
status = INIT;
// timeout for receiving stuff
timer.setSingleShot(true);
timer.start(60000);
}
void PMaildServerBase::noSsl() {
connect(sock, SIGNAL(readyRead()), this, SLOT(socketReadyRead()));
connect(sock, SIGNAL(bytesWritten(qint64)), this, SLOT(socketBytesWritten(qint64)));
sock->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
status = VALID;
welcome();
// read data if any is already in the pipe
if (sock->bytesAvailable()) socketReadyRead();
}
bool PMaildServerBase::isSsl() {
return sock->isEncrypted();
}
void PMaildServerBase::ssl() {
if (status != INIT) return;
sock->startServerEncryption();
}
void PMaildServerBase::socketSslErrors(const QList<QSslError>&) {
qDebug("ssl error, giving up");
}
void PMaildServerBase::socketSslReady() {
if (status != INIT) return;
connect(sock, SIGNAL(readyRead()), this, SLOT(socketReadyRead()));
connect(sock, SIGNAL(bytesWritten(qint64)), this, SLOT(socketBytesWritten(qint64)));
sock->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
timer.start(60000);
status = VALID;
welcome();
// read data if any is already in the pipe
if (sock->bytesAvailable()) socketReadyRead();
}
void PMaildServerBase::socketDisconnected() {
sock->deleteLater();
deleteLater();
}
void PMaildServerBase::timeout() {
if (status == CLOSING) {
socketDisconnected();
} else {
close();
}
}
void PMaildServerBase::socketBytesWritten(qint64) {
flush();
}
void PMaildServerBase::socketReadyRead() {
if (status != VALID) return;
buf_in += sock->readAll();
parseInBuffer();
}
void PMaildServerBase::parseInBuffer() {
// default parseInBuffer() handler: split buffer per line, take first line, pass to parseInCommand
while(true) {
if (status != VALID) break;
int pos = buf_in.indexOf('\n');
if (pos == -1) break; // no full line in buffer
QByteArray cmd = buf_in.left(pos).trimmed();
buf_in.remove(0, pos+1);
if (!cmd.isEmpty())
parseInCommand(cmd);
}
// TODO: implement buf_in size check here, fail if buffer too big (protect against infinitely increasing buffer)
}
void PMaildServerBase::parseInCommand(const QByteArray &cmd) {
// default parseInCommand() handler: split by spaces and lookup local cmd table
QList<QByteArray> list = cmd.split(' ');
// search list for empty elements and drop these
for(auto i = list.begin(); i != list.end(); i++) {
if ((*i).isEmpty()) list.erase(i);
}
QByteArray method = "server_cmd_" + list.at(0).toLower();
// remove first entry
list.erase(list.begin());
if (!QMetaObject::invokeMethod(this, method.data(), Qt::DirectConnection, Q_ARG(QList<QByteArray>, list))) {
handleUnknownCommand();
}
}
void PMaildServerBase::close() {
if (status == CLOSING) return;
status = CLOSING;
flush();
}
void PMaildServerBase::flush() {
if (buf_out.size() == 0) {
if (status == CLOSING) sock->disconnectFromHost();
return;
}
qint64 res = sock->write(buf_out);
if (res == -1) {
// write error!
buf_out.clear();
sock->disconnectFromHost();
sock->deleteLater();
deleteLater();
return;
}
if (res == 0) return;
// give it up to 10 seconds to read all
if (status == CLOSING)
timer.start(10000);
if (res == buf_out.size()) { // all written
buf_out.clear();
if (status == CLOSING) sock->disconnectFromHost();
return;
}
buf_out.remove(0, res);
}
void PMaildServerBase::writeLine(const QByteArray &b) {
if (status != VALID) return;
buf_out.append(b);
buf_out.append("\r\n");
flush();
}
void PMaildServerBase::write(const QByteArray &b) {
if (status != VALID) return;
buf_out.append(b);
flush();
}