-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientCommands.cpp
More file actions
127 lines (110 loc) · 3.42 KB
/
Copy pathClientCommands.cpp
File metadata and controls
127 lines (110 loc) · 3.42 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
#include "ChatApp.h"
#include "std_messages.h"
#include "command_guide.h"
#include <fstream>
#include <sstream>
#define FILE_PACKET_SIZE 600
#define ADMIN_PASS "123"
void ChatApp::initClientCommands()
{
using cm = CommandHandler;
//NORMAL COMMANDS
clientCH.addCommand("/leave", [&](cm::Params args) {
//tell other guy
connection.send("/leave");
connection.disconnect();
cmd.write(YOU_LEFT);
left = true;
});
clientCH.addCommand("/name", [&](cm::Params args) {
if (args[0].size() > 0) username = args[0];
});
clientCH.addCommand("/clear", [&](cm::Params args) {
system("CLS");
});
clientCH.addCommand("/admin", [&](cm::Params args) {
if (args[0] == ADMIN_PASS) {
admin = true;
}
});
clientCH.addCommand("/help", [=](cm::Params args) {
std::string result = "Avaialable Commands:\n";
for (int i = 0; i < commands.size(); i++) {
result.append(commands[i]);
result.push_back('\n');
}
if (admin) {
for (int i = 0; i < admin_commands.size(); i++) {
result.append(admin_commands[i]);
result.push_back('\n');
}
}
result.pop_back();
cmd.write(result);
});
//ADMIN COMMANDS
clientCH.addCommand("/shelp", [&](cm::Params args) {
std::string result = "Avaialable Server Commands:\n";
for (int i = 0; i < server_commands.size(); i++) {
result.append(server_commands[i]);
result.push_back('\n');
}
result.pop_back();
cmd.write(result);
}, true);
clientCH.addCommand("/bmode", [&](cm::Params args) {
msg_preprocess = [](std::string& str) {
str.append("]]");
str.insert(0, "/s [/batch [");
};
}, true);
clientCH.addCommand("/smode", [&](cm::Params args) {
msg_preprocess = [](std::string& str) {
str.push_back(']');
str.insert(0, "/s [");
};
}, true);
clientCH.addCommand("/dmode", [&](cm::Params args) {
msg_preprocess = [](std::string& str) {};
}, true);
clientCH.addCommand("/s", [&](cm::Params args) {
connection.send(args[0]);
}, true);
clientCH.addCommand("/download", [&](cm::Params args) {
connection.send("/c [/send_file [" + args[1] + "] [" + args[0] + "]]");
}, true);
clientCH.addCommand("/send_file", [&](cm::Params p_args) {
std::thread send_file([&](cm::Params args) {
std::ifstream file(args[0]);
if (file.is_open()) {
std::stringstream stream;
stream << file.rdbuf();
auto contents = stream.str();
if (contents.size() == 0) return;
int loads = contents.size() / FILE_PACKET_SIZE,
extra = contents.size() % FILE_PACKET_SIZE;
std::lock_guard lock(mutex);
int last_index = 0;
for (int i = 0; i < loads; i++) {
std::string packet = contents.substr(last_index, FILE_PACKET_SIZE);
last_index += FILE_PACKET_SIZE;
auto pack_size = packet.size();
connection.send("/save [" + args[1] + "] " + std::to_string(pack_size) + "[" + packet + "]");
}
if (extra > 0) {
std::string extra_content = contents.substr(last_index, extra);
auto extra_size = extra_content.size();
connection.send("/save [" + args[1] + "] " + std::to_string(extra_size) + "[" + extra_content + "]");
}
}
else {
std::lock_guard lock(mutex);
cmd.write("Unable to open file at: " + args[0]);
}
}, std::move(p_args));
send_file.detach();
}, true);
clientCH.addCommand("/ip", [&](cm::Params args) {
cmd.write(connection.getIp().toString());
}, true);
}