-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc.c
276 lines (220 loc) · 7.84 KB
/
irc.c
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "modules/modules.h"
#include "modules/constants.h"
#include "irc.h"
volatile bool gtfotime = false;
volatile bool input_thread_status = false;
uint32_t vms_condition_handler(uint32_t *signal_args, uint32_t *mech_args) {
uint32_t sig = signal_args[1];
if (sig == SS$_CONTROLY) {
gtfotime = true;
return SS$_CONTINUE;
}
return SS$_RESIGNAL;
}
void process_incoming_message(int client_socket, char *message, WINDOW *output) {
if (strncmp(message, "PING", 4) == 0) {
char pong[BUFFER_SIZE];
snprintf(pong, sizeof(pong), "PONG%s\r\n", message + 4);
waddstr(output, "Sending PONG response\n");
wrefresh(output);
write(client_socket, pong, strlen(pong));
} else if (strstr(message, " PRIVMSG ") != NULL) { // Check for PRIVMSG
privmsg_handler(client_socket, message, output);
} else {
waddstr(output, message);
waddstr(output, "\n");
wrefresh(output);
// Display unhandled input. Useful to find out what to implement next and/or see
// unhandled server messages and types!
}
}
typedef struct {
int client_socket;
WINDOW *entry_window;
char *channel;
} thread_data_t;
void *user_input_thread(void *arg) {
thread_data_t *data = (thread_data_t *)arg;
int client_socket = data->client_socket;
WINDOW *entry_window = data->entry_window;
char *channel = data->channel;
char input_message[BUFFER_SIZE];
char send_buffer[BUFFER_SIZE];
while (1) {
wmove(entry_window, 1, 0);
wclrtoeol(entry_window);
wrefresh(entry_window);
wgetstr(entry_window, input_message);
if (strlen(input_message) > 0) {
if (strcmp(input_message, "/quit") == 0) {
gtfotime = true;
break;
} else {
snprintf(send_buffer, BUFFER_SIZE, "PRIVMSG %s :%s\r\n", channel, input_message);
write(client_socket, send_buffer, strlen(send_buffer));
memset(input_message, 0, BUFFER_SIZE);
}
}
}
return NULL;
}
int process_initial_messages(int client_socket, WINDOW *output) {
char buffer[BUFFER_SIZE];
int registration_complete = 0;
while (!registration_complete) {
int n = recv(client_socket, buffer, sizeof(buffer) - 1, 0);
if (n < 0) {
perror("recv");
exit(1);
} else if (n == 0) {
break;
}
buffer[n] = '\0';
char *message_start = buffer;
char *message_end;
while ((message_end = strstr(message_start, "\r\n")) != NULL) {
*message_end = '\0';
process_incoming_message(client_socket, message_start, output);
if (strstr(message_start, " 001 ") != NULL) { // Welcome message received
registration_complete = 1;
}
message_start = message_end + 2;
}
}
return registration_complete;
}
void endwin_noreturn() {
endwin();
}
int main(int argc, char **argv) {
char *subprocess_flag = getenv("SUBPROCESS_FLAG");
if (subprocess_flag == NULL) {
pid_t pid = vfork();
if (pid < 0) {
perror("vfork");
exit(1);
}
struct dsc$descriptor_s cmd_desc;
if (pid == 0) {
printf("Subflag\n");
putenv("SUBPROCESS_FLAG=1");
printf("set nocontrol");
cmd_desc.dsc$w_length = strlen("SET NOCONTROL=Y");
cmd_desc.dsc$a_pointer = "SET NOCONTROL=Y";
cmd_desc.dsc$b_dtype = DSC$K_DTYPE_T;
cmd_desc.dsc$b_class = DSC$K_CLASS_S;
lib$spawn(&cmd_desc);
} else {
int status;
waitpid(pid, &status, 0);
if (status == 0) {
cmd_desc.dsc$w_length = strlen("SET CONTROL=Y");
cmd_desc.dsc$a_pointer = "SET CONTROL=Y";
cmd_desc.dsc$b_dtype = DSC$K_DTYPE_T;
cmd_desc.dsc$b_class = DSC$K_CLASS_S;
lib$spawn(&cmd_desc);
}
exit(0);
}
}
WINDOW *output_window;
WINDOW *entry_window;
initscr();
clear();
atexit(endwin_noreturn);
lib$establish(vms_condition_handler);
output_window = newwin(LINES - 2, COLS, 0, 0);
entry_window = newwin(2, COLS, LINES - 2, 0);
scrollok(output_window, TRUE);
wrapok(output_window, TRUE);
char banner[BANNER_LENGTH];
char banner_contents[512];
extern char *BANNER_DEFINITION;
extern char *BANNER_TOP;
sprintf(banner_contents, BANNER_DEFINITION, "NICK", "*PLACEHOLDER*", "CHANNEL");
sprintf(banner, BANNER_TOP, "placeholder", banner_contents);
mvwaddstr(entry_window, 0, 0, banner);
wrefresh(output_window);
wrefresh(entry_window);
struct hostent *server;
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE];
int client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket < 0) {
perror("socket");
exit(1);
}
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
memcpy(&server_addr.sin_addr.s_addr, server->h_addr, server->h_length);
server_addr.sin_port = htons(SERVER_PORT);
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("connect");
exit(1);
}
char nickname[BUFFER_SIZE];
mvwaddstr(entry_window, 1, 0, "Enter your nickname: ");
wgetstr(entry_window, nickname);
snprintf(buffer, BUFFER_SIZE, "NICK %s\r\n", nickname);
write(client_socket, buffer, strlen(buffer));
sprintf(banner_contents, BANNER_DEFINITION, nickname, "*PLACEHOLDER*", "CHANNEL");
sprintf(banner, BANNER_TOP, "placeholder", banner_contents);
wclear(entry_window);
mvwaddstr(entry_window, 0, 0, banner);
wrefresh(entry_window);
char username[BUFFER_SIZE];
mvwaddstr(entry_window, 1, 0, "Enter your username: ");
wrefresh(entry_window);
wgetstr(entry_window, username);
snprintf(buffer, BUFFER_SIZE, "USER %s 0 * :%s\r\n", username, username);
write(client_socket, buffer, strlen(buffer));
process_initial_messages(client_socket, output_window);
wclear(entry_window);
mvwaddstr(entry_window, 0, 0, banner);
char channel[BUFFER_SIZE];
mvwaddstr(entry_window, 1, 0, "Enter channel to join: ");
wrefresh(entry_window);
wgetstr(entry_window, channel);
snprintf(buffer, BUFFER_SIZE, "JOIN %s\r\n", channel);
write(client_socket, buffer, strlen(buffer));
memset(banner_contents, 0, strlen(banner_contents));
memset(banner, 0, strlen(banner));
sprintf(banner_contents, BANNER_DEFINITION, nickname, "*PLACEHOLDER*", channel);
sprintf(banner, BANNER_TOP, "placeholder", banner_contents);
wclear(entry_window);
mvwaddstr(entry_window, 0, 0, banner);
wrefresh(entry_window);
pthread_t input_thread;
thread_data_t thread_data = {client_socket, entry_window, channel};
if (pthread_create(&input_thread, NULL, user_input_thread, &thread_data) == 0) {
input_thread_status = true;
};
while (!gtfotime) {
int n = recv(client_socket, buffer, sizeof(buffer) - 1, 0);
if (n < 0) {
perror("recv");
exit(1);
} else if (n == 0) {
break;
}
buffer[n] = '\0';
char *message_start = buffer;
char *message_end;
while ((message_end = strstr(message_start, "\r\n")) != NULL) {
*message_end = '\0';
process_incoming_message(client_socket, message_start, output_window);
message_start = message_end + 2;
}
fflush(stdout);
}
if (input_thread_status) {
pthread_join(input_thread, NULL);
};
close(client_socket);
return 0;
}