-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPullFinger.cpp
More file actions
240 lines (205 loc) · 7.39 KB
/
Copy pathPullFinger.cpp
File metadata and controls
240 lines (205 loc) · 7.39 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//standard library
#include <arpa/inet.h>
#include <gtk/gtk.h>
#include <iostream>
#include <netdb.h>
#include <netinet/in.h>
#include <stdexcept>
#include <string>
#include <sys/socket.h>
#include <unistd.h>
//custom library
#include "TextFileApp.h"
// Class responsible for fetching Finger information
class FingerClient {
public:
FingerClient() {
// Initialize variables
host = "";
user = "";
lastAddress = "";
}
std::string fetchFingerInfo() {
try {
// Create a socket for communication
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket == -1) {
throw std::runtime_error("Error creating socket.");
}
// Get host information
hostent *server = gethostbyname(host.c_str());
if (server == nullptr) {
throw std::runtime_error("Host not found.");
}
sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(79);
serverAddr.sin_addr = *(struct in_addr *)server->h_addr;
// Connect to the server
if (connect(clientSocket, (struct sockaddr *)&serverAddr,
sizeof(serverAddr)) == -1) {
close(clientSocket);
throw std::runtime_error("Error connecting to the server.");
}
// Send query
std::string query = user + "\r\n";
send(clientSocket, query.c_str(), query.size(), 0);
// Receive response
std::string response;
char buffer[1024];
while (true) {
ssize_t bytesRead = recv(clientSocket, buffer, sizeof(buffer), 0);
if (bytesRead <= 0) {
break;
}
response.append(buffer, bytesRead);
}
// Close the socket and return the response
close(clientSocket);
return response;
} catch (const std::exception &e) {
return "Error: " + std::string(e.what());
}
}
std::string validateAndSetURL(const std::string &input) {
// Check for "finger " format
size_t fingerPos = input.find("finger ");
if (fingerPos != std::string::npos) {
std::string address = input.substr(fingerPos + 7);
if (address.empty()) {
"example.com/user";
return "Invalid URL format!\n\nSupported URL Formats:\n\n 'finger user@host'\n 'finger://host/user'\n 'host/user'";
}
// Parse user and host
size_t atIndex = address.find('@');
if (atIndex != std::string::npos) {
user = address.substr(0, atIndex);
host = address.substr(atIndex + 1);
lastAddress = "finger://" + address;
return "";
}
}
// Check for "://" format
size_t schemePos = input.find("://");
if (schemePos != std::string::npos) {
std::string hostUser = input.substr(schemePos + 3);
size_t slashPos = hostUser.find('/');
if (slashPos == std::string::npos) {
return "Invalid URL format!\n\nSupported URL Formats:\n\n 'finger user@host'\n 'finger://host/user'\n 'host/user'";
}
host = hostUser.substr(0, slashPos);
user = hostUser.substr(slashPos + 1);
lastAddress = input;
return "";
} else {
// Check for "host/user" format
size_t slashPos = input.find('/');
if (slashPos == std::string::npos) {
return "Invalid URL format!\n\nSupported URL Formats:\n\n 'finger user@host'\n 'finger://host/user'\n 'host/user'";
}
host = input.substr(0, slashPos);
user = input.substr(slashPos + 1);
lastAddress = "finger://" + input;
return "";
}
}
std::string getLastAddress() const { return lastAddress; }
private:
std::string host;
std::string user;
std::string lastAddress;
};
// Class responsible for managing the GUI application
class FingerApp {
public:
FingerApp() {
// Initialize GTK
gtk_init(NULL, NULL);
// Create main window and layout
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
input_entry = gtk_entry_new();
response_label = gtk_label_new(NULL);
scrolled_window = gtk_scrolled_window_new(NULL, NULL);
// Configure window
gtk_window_set_title(GTK_WINDOW(window), "Pullfinger");
gtk_window_set_default_size(GTK_WINDOW(window), 700, 480);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
// Initialize FingerClient and GUI components
fingerClient = FingerClient();
gtk_container_add(GTK_CONTAINER(vbox), input_entry);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
gtk_container_add(GTK_CONTAINER(scrolled_window), response_label);
gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
// Create the bookmarks button
bookmarksButton = gtk_button_new_with_label("Bookmarks");
g_signal_connect(bookmarksButton, "clicked", G_CALLBACK(openBookmarks),
this);
gtk_container_add(GTK_CONTAINER(vbox), bookmarksButton);
// Connect callback for input entry activation
g_signal_connect(input_entry, "activate", G_CALLBACK(input_entry_activated),
this);
// Update initial response label
updateResponseLabel(
"Supported URL Formats:\n\n 'finger user@host'\n 'finger://host/user'\n 'host/user'\n\n(Hit Enter to Process)");
// Show all GUI elements
gtk_widget_show_all(window);
}
// Start GTK main loop
void run() { gtk_main(); }
private:
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *input_entry;
GtkWidget *response_label;
GtkWidget *scrolled_window;
GtkWidget *bookmarksButton;
FingerClient fingerClient;
// Update the response label with text
void updateResponseLabel(const std::string &text) {
gtk_label_set_text(GTK_LABEL(response_label), text.c_str());
// Get the PangoContext for the response_label
PangoContext *context = gtk_widget_get_pango_context(response_label);
// Get the system's monospaced font
PangoFontDescription *fontDesc =
pango_context_get_font_description(context);
// Set the font description to be monospaced (you can set other properties
// as needed)
pango_font_description_set_family(fontDesc, "Monospace");
// Apply the font description to the response_label
pango_context_set_font_description(context, fontDesc);
gtk_widget_show_all(window);
}
// Callback function for input entry activation
static void input_entry_activated(GtkEntry *entry, gpointer data) {
FingerApp *app = static_cast<FingerApp *>(data);
const gchar *input = gtk_entry_get_text(GTK_ENTRY(entry));
// Validate input and set URL
std::string error_message = app->fingerClient.validateAndSetURL(input);
if (!error_message.empty()) {
app->updateResponseLabel(error_message);
return;
}
// Fetch and update response if the address is changed
if (app->fingerClient.getLastAddress() !=
app->fingerClient.validateAndSetURL(input)) {
std::string response = app->fingerClient.fetchFingerInfo();
app->updateResponseLabel(response);
}
}
// Callback function for the bookmarks button
static void openBookmarks(GtkButton *button, gpointer data) {
// Open the TextFileApp window
TextFileApp textFileApp;
textFileApp.run();
}
};
// Main function
int main(int argc, char *argv[]) {
FingerApp app;
app.run();
return 0;
}