This repository was archived by the owner on Mar 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIRCppClient.cpp
More file actions
172 lines (152 loc) · 6.22 KB
/
IRCppClient.cpp
File metadata and controls
172 lines (152 loc) · 6.22 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 <SocketConnection.h>
#include <ClientSocket.h>
#include <thread>
#include <Logger.h>
#include <CmdProcessor.h>
#include <GUIReferences.h>
#include <fstream>
void WriteToConsole(std::string &msg, MessageList *msglist) {
using namespace ftxui;
if (msglist->size() + 1 > 30) {
msglist->pop_front();
}
msglist->push_back({Logger::getFormattedTime(), msg});
}
void dataListenThread(ClientSocket *client, MessageList *msglist, bool *isServerDead) {
printf("%s Started listening for data\n", Logger::getFormattedTime().c_str());
Client::CmdProcessor cmdProccessor(client);
while (client->IsServerAlive()) {
cmdProccessor.acceptMessage(client->ListenForData(), msglist, (HandlePrint) WriteToConsole);
}
// printf("%s Server dead, disconnecting\n", Logger::getFormattedTime().c_str());
*isServerDead = true;
}
int main() {
setlocale(0, "ru_RU.UTF-8");
system("chcp 65001");
system("cls");
using namespace ftxui;
// === LOGIN SCREEN ===
auto SCREEN = ScreenInteractive::Fullscreen();
std::fstream ConfigurationFile;
ConfigurationFile.open("IRCpp.cfg", std::fstream::in | std::fstream::app);
std::string nickname;
std::string ip;
if (ConfigurationFile) {
ConfigurationFile >> nickname;
ConfigurationFile >> ip;
}
if (ip.empty())
ip = "127.0.0.1:1376";
ConfigurationFile.close();
auto LoginProceedButton = Button("Log in", SCREEN.ExitLoopClosure());
Component IpInput, NicknameInput;
auto NicknameInputOptions = InputOption();
NicknameInputOptions.on_enter = [&] {
if (!nickname.empty())
IpInput->TakeFocus();
};
auto IpInputOptions = InputOption();
IpInputOptions.on_enter = [&] {
if (!ip.empty()) {
LoginProceedButton->TakeFocus();
LoginProceedButton->OnEvent(Event::Return);
}
};
NicknameInput = Input(&nickname, "", NicknameInputOptions);
IpInput = Input(&ip, "", IpInputOptions);
auto LoginScreenComponent = Container::Vertical({NicknameInput, IpInput, LoginProceedButton});
auto loginScreen = Renderer(LoginScreenComponent, [&] {
return vbox(
text(""),
text("Connect to IRCpp chat") | center,
text(""),
separator(),
hbox(
text(" "),
vbox(
text(""),
hbox(text(" Enter nickname ") | vcenter,
hbox(text(" "), NicknameInput->Render() | size(WIDTH, EQUAL, 25), text(" ")) |
border) | hcenter,
hbox(text(" Enter IP (ip:port) ") | vcenter,
hbox(text(" "), IpInput->Render() | size(WIDTH, EQUAL, 25), text(" ")) |
border) | hcenter,
color(Color::LightGreen, LoginProceedButton->Render()) | hcenter
) | flex,
text(" ")
) | flex
);
});
SCREEN.Loop(loginScreen);
while (nickname.empty() || ip.empty()) { SCREEN.Loop(loginScreen); }
ConfigurationFile.open("IRCpp.cfg", std::fstream::out | std::fstream::trunc);
ConfigurationFile << nickname << std::endl << ip;
ConfigurationFile.close();
bool isServerDead = false;
auto *socketConnection = new SocketConnection();
socketConnection->CreateClient(ip.substr(0, ip.find(':')).c_str(), ip.substr(ip.find(':') + 1).c_str());
auto *client = new ClientSocket(socketConnection);
MessageList msglist;
std::thread dataThread(dataListenThread, client, &msglist, &isServerDead);
client->SendData(Commands[CMD_JOIN] + " " + nickname);
std::string buf;
auto SendAction = [&] {
if (!buf.empty()) {
client->SendData(Commands[CMD_MESSAGE] + " " + buf);
buf.clear();
return true;
}
return false;
};
auto PartButton = Button("Part", SCREEN.ExitLoopClosure());
auto OKCloseButton = Button(" OK ", SCREEN.ExitLoopClosure());
auto SendButton = Button("Send", SendAction);
auto MessageInputOptions = InputOption();
// BUG: Field does not clear
MessageInputOptions.on_enter = SendAction;
auto MessageInput = Input(&buf, "", MessageInputOptions);
auto ChatScreenComponent = Container::Vertical({MessageInput, SendButton, PartButton, OKCloseButton});
auto chatScreen = Renderer(ChatScreenComponent, [&] {
if (isServerDead) {
return vbox(
text(""),
text(""),
vbox(
text(""),
color(Color::Red, text("Cannot connect to server!")) | center,
color(Color::GrayLight,
text(" Please check is server alive and reconnect manually ")) |
center,
text(""),
color(Color::BlueLight, OKCloseButton->Render()) | center,
text("")
) | border | center,
text(""),
text(""));
}
Elements chatbox;
for (auto &message: msglist)
chatbox.push_back(renderChatMessage(message));
return vbox(
text(""),
text("Connected to IRCpp Chat (" + ip + ") as " + nickname) | center,
separator(),
hbox(text(" "),
vbox(std::move(chatbox)) | frame | size(HEIGHT, LESS_THAN, 30) | size(HEIGHT, GREATER_THAN, 15) |
border | flex,
text(" ")),
hbox(text(" "), hbox(text(" "), MessageInput->Render(), text(" ")) | flex | border,
SendButton->Render(),
color(Color::Red, PartButton->Render()),
text(" "))
) | flex;
});
SCREEN.Loop(chatScreen);
client->SendData(Commands[CMD_PART]);
client->Shutdown();
// Terminating threads
if (dataThread.joinable())
dataThread.join();
return EXIT_SUCCESS;
}