-
Notifications
You must be signed in to change notification settings - Fork 1
/
inputconnection.cpp
137 lines (104 loc) · 3.1 KB
/
inputconnection.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
#include "inputconnection.h"
#include "nntpproxy.h"
#include <regex>
#include <algorithm> //std::transform
InputConnection::InputConnection(qintptr aSocketDescriptor):
Connection(aSocketDescriptor, NntpProxy::isClientSSL(), true, "InputConnection")
{
connect(this, &InputConnection::connected, this, &InputConnection::doAuthentication);
#ifdef LOG_CONSTRUCTORS
_log("Constructor");
#endif
}
bool InputConnection::doAuthentication(){
_log("doAuthentication");
const std::regex & authReg = Nntp::getCmdRegex(Nntp::CMDS::authinfo);
std::smatch match;
std::string user;
std::string pass;
bool isAuthenticated = false;
for (int i=0; i < cAuthenticationTry; ++i){
do {
if (!iSocket->isOpen())
return false;
iSocket->waitForReadyRead();
} while (iSocket->isOpen() && !iSocket->canReadLine());
QByteArray lineArr = iSocket->readLine();
#ifdef LOG_INPUT_AUTH
QString str("Data Authentication: ");
str += lineArr.constData();
_log(str);
#endif
if(strcmp(lineArr.constData(), Nntp::QUIT) == 0){
iSocket->disconnectFromHost();
break;
}
std::string line(lineArr);
std::regex_match(line, match, authReg);
if (!match.size()){
iSocket->write(Nntp::getResponse(480));
continue;
}
std::string cmd = match[1];
std::transform(cmd.begin(), cmd.end(), cmd.begin(), ::tolower);
if (cmd == "user"){
user = match[2];
if (!pass.empty())
isAuthenticated = true;
} else {
pass = match[2];
if (!user.empty())
isAuthenticated = true;
}
if (isAuthenticated) {
break;
} else {
iSocket->write(Nntp::getResponse(381));
}
}
if (isAuthenticated)
emit authenticated(user, pass);
else {
#ifdef LOG_CONNECTION_ERRORS_BEFORE_EMIT_SIGNALS
_log("Wrong Authentication!");
#endif
emit socketError("Wrong Authentication!");
}
return isAuthenticated;
}
void InputConnection::readyRead()
{
if(iSocket->canReadLine()){
QByteArray line = iSocket->readLine();
#ifdef LOG_INPUT_DATA
QString str("Data In: ");
str += line;
_log(str);
#endif
if(strcmp(line.constData(), Nntp::QUIT) == 0){
closeConnection();
} else {
iOutputCon->write(line);
// iSocket->write(line);
}
}
}
void InputConnection::closeConnection(){
// Stop async read
Connection::closeConnection();
if (iOutputCon) {
iOutputCon->setOutput(Q_NULLPTR); // To avoid circular calls
iOutputCon->closeConnection();
iOutputCon = Q_NULLPTR;
}
// close input socket
if (iSocket && iSocket->isOpen())
iSocket->disconnectFromHost();
}
// On disconnection we remove the connection from the SessionManager
// it will stop the thread and delete the connection
void InputConnection::disconnected()
{
_log("> Disconnected");
emit closed();
}