-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.cpp
More file actions
147 lines (114 loc) · 5.2 KB
/
Copy pathcommands.cpp
File metadata and controls
147 lines (114 loc) · 5.2 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
#include <string>
#include <format>
#include <arpa/inet.h>
#include <filesystem>
#include <fstream>
#include "commands.hpp"
#include "colors.hpp"
#include "util.hpp"
namespace Commands {
void info(Context &context) {
context.reply = std::format("nickname:{}, address:{}, socket:{}",
context.client.nickname, context.client.address, context.client.socket
);
}
void upload(Context &context) {
if (context.args.size() != 3) {
context.reply = "[ERROR] Invalid arguments! Usage: upload <file> <nickname>";
return;
}
std::string filename = context.args[1];
std::string sender = context.client.nickname;
std::string recipient = context.args[2];
ClientData client = context.client;
std::string reply = std::format("SIG upload {} {}", filename, recipient);
send(client.socket, reply.c_str(), reply.size(), 0);
char buffer[1024]; // MAX UPLOAD / DOWNLOAD
int bytes_recieved = recv(client.socket, buffer, sizeof(buffer) - 1, 0);
if (bytes_recieved < 0) {
Print::error("Client failed to upload file.");
context.reply = "[ERROR] Upload failed.";
} else {
Print::info(std::format("Incoming file data from {}...", sender));
}
buffer[bytes_recieved] = '\0';
FileData data = {.filename=filename, .recipient=recipient, .sender=sender};
context.cache_manager->write_file(filename, std::vector<uint8_t>(buffer, buffer + bytes_recieved), data);
Print::info(std::format("{} uploaded to {} with data: {{filename={}, recipient={}, sender={}}}",
filename, "./cache", filename, recipient, sender));
context.reply = "File upload complete! You can see your uploads by typing `uploads`.";
}
void download(Context &context) {
if (context.args.size() != 2) {
context.reply = "[ERROR] Invalid arguments! Usage: download <filename>";
return;
}
std::string filename = context.args[1];
ClientData client = context.client;
std::vector<FileData> files = context.cache_manager->files;
std::optional<FileData> target_file;
for (FileData file : files) {
if (file.filename == filename && client.nickname == file.recipient) {
target_file = file;
}
}
if (!target_file) {
context.reply = "[ERROR] No such file found. Type `downloads` to see what files you can download.";
return;
}
std::string signal = "SIG download " + filename;
send(client.socket, signal.c_str(), signal.size(), 0);
std::filesystem::path path = std::filesystem::path("cache") / target_file->filename;
std::ifstream file(path, std::ios::binary);
std::vector<uint8_t> file_bytes(std::istreambuf_iterator<char>(file), {});
send(client.socket, file_bytes.data(), file_bytes.size(), 0);
context.should_reply = false;
}
void downloads(Context &context) {
std::vector<FileData> files = context.cache_manager->files;
std::vector<FileData> available_files;
std::stringstream stream;
for (FileData file : files) {
if (file.recipient == context.client.nickname) {
available_files.push_back(file);
}
}
if (available_files.empty()) {
context.reply = Colors::RED + "You have no downloads for this session." + Colors::RESET;
return;
}
stream << std::format("Your avaialble downloads this session ({}):\n", available_files.size());
for (int i = 0; i < available_files.size(); i++) {
FileData file = available_files[i];
stream << Colors::GRAY << "- " << Colors::RESET << file.filename << " " << Colors::GRAY
<< "(from " << file.sender << ")" << (i == available_files.size() ? "" : "\n");
}
context.reply = stream.str() + Colors::RESET;
}
void uploads(Context &context) {
std::vector<FileData> files = context.cache_manager->files;
std::vector<FileData> client_files;
std::stringstream stream;
for (FileData file : files) {
if (file.sender == context.client.nickname) {
client_files.push_back(file);
}
}
if (client_files.empty()) {
context.reply = Colors::RED + "You have uploaded no files this session." + Colors::RESET;
return;
}
stream << std::format("Your file uploads for this session ({}):\n", client_files.size());
for (int i = 0; i < client_files.size(); i++) {
FileData file = client_files[i];
stream << Colors::GRAY << "- " << Colors::RESET << file.filename << " " << Colors::GRAY
<< "(for " << file.recipient << ")" << (i == client_files.size() ? "" : "\n");
}
context.reply = stream.str() + Colors::RESET;
}
void quit(Context &context) {
context.exit_after = true;
context.reply = "You are being disconnected from the server...";
Print::info(std::format("{} ({}) is being disconnected...", context.client.nickname, context.client.address));
}
}