-
Notifications
You must be signed in to change notification settings - Fork 1
/
vtcc.c
291 lines (233 loc) · 6.53 KB
/
vtcc.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "vtcc.h"
static void set_up_screen(void);
static void add_line(const char* line);
static const char* get_colour(int level);
static size_t textlen(const char* str);
int running = 1;
CyanChat* cc = NULL;
struct winsize w;
CCClient client = {
NULL,
&on_userlist,
&on_broadcast_msg,
&on_private_msg
};
int main(int argc, char** argv) {
char* host = "cho.cyan.com";
unsigned short port = 1813;
int c;
fd_set set;
set_up_screen();
while ((c = getopt(argc, argv, "h:p:")) != -1) {
switch(c) {
case 'h':
host = optarg;
break;
case 'p':
port = atoi(optarg);
break;
default:
break;
}
}
cc = cc_new(&client);
cc_connect(cc, host, port);
rl_callback_handler_install(NULL, &on_input);
while (running) {
int ccsock = cc_get_socket(cc);
FD_ZERO(&set);
FD_SET(ccsock, &set);
FD_SET(STDIN_FILENO, &set);
select(ccsock + 1, &set, NULL, NULL, NULL);
if (FD_ISSET(STDIN_FILENO, &set)) {
rl_callback_read_char();
}
if (FD_ISSET(ccsock, &set)) {
int nread = 0;
char* buf = malloc(1024);
char* bp = buf;
int bsize = 1024;
while (bsize && (nread = read(ccsock, bp, bsize)) > 0) {
bp += nread;
bsize -= nread;
if (*(bp - 1) == '\n' || bsize == 0) {
*bp = '\0';
break;
}
}
if (nread == 0) {
} else if (nread > 0) {
char* saveptr = NULL;
char* line = strtok_r(buf, "\n", &saveptr);
while (line != NULL) {
cc_parse(cc, line);
line = strtok_r(saveptr, "\n", &saveptr);
}
}
free(buf);
}
}
rl_callback_handler_remove();
cc_free(&cc);
printf("\033[H");
printf("\033[2J");
fflush(stdout);
return 0;
}
void set_up_screen(void) {
size_t i;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
printf("\033[?6l");
printf("\033[?7h");
printf("\033[H");
printf("\033[2J");
printf("\033[?4h");
printf("\033[3;%dr", w.ws_row);
printf("\033[B");
for (i = 0; i < w.ws_col; i++) {
printf("─");
}
printf("\033[H");
printf("\033[2K");
fflush(stdout);
}
void add_line(const char* line) {
size_t length = 0;
int lines = 1;
length = textlen(line);
if (length > w.ws_col) {
lines = 1 + (length / w.ws_col);
}
printf("\033[s");
printf("\033[3;1H");
printf("\033[%dL", lines);
printf("%s", line);
printf("\033[u");
fflush(stdout);
}
const char* get_colour(int level) {
switch (level) {
case 0:
return kWhite;
case 1:
return kCyan;
case 2:
return kGreen;
case 4:
return kGold;
default:
return kRed;
}
}
size_t textlen(const char* str) {
size_t count = 0;
size_t i = 0;
int in_escape = 0;
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == '\033') {
in_escape = 1;
} else if (in_escape && str[i] == 'm') {
in_escape = 0;
} else if (in_escape) {
/* Do NOT increment count */
} else {
count++;
}
}
return count;
}
void on_input(char* line) {
if (line == NULL) {
running = 0;
return;
}
printf("\033[H");
printf("\033[2K");
fflush(stdout);
if (strlen(line) == 0) {
return;
}
if (strstr(line, "/nick ") == line) {
cc_set_nickname(cc, line + 6);
} else if (strstr(line, "/quit") == line) {
running = 0;
return;
} else {
if (!cc_has_nickname(cc)) {
add_line("Not Logged in!");
} else if(strstr(line, "/pm ") == line) {
char* saveptr = NULL;
char* display = malloc(strlen(line) + 64);
char* name = strtok_r(line + 4, ",", &saveptr);
CCUser* user = cc_user_from_username(name);
cc_send_private(cc, user, saveptr);
sprintf(display, "%sSent Private Message to %s[%s]%s %s",
kRed, get_colour(cc_user_get_level(user)),
cc_user_get_nickname(user), kNormal, saveptr);
add_line(display);
free(display);
cc_user_free(&user);
} else {
cc_send_broadcast(cc, line);
}
}
}
/* CyanChat callbacks */
void on_userlist(size_t count, CCUser** users) {
size_t i = 0;
char* output = NULL;
if (count == 0) {
return;
}
output = malloc((count * 56) + 16);
sprintf(output, "Chat Users:");
for (i = 0; i < count; i++) {
const char* nickname = cc_user_get_nickname(users[i]);
int level = cc_user_get_level(users[i]);
char* printed = malloc(56);
sprintf(printed, " %s[%s]%s",
get_colour(level), nickname, kNormal);
strncat(output, printed, 56);
free(printed);
}
add_line(output);
free(output);
}
void on_broadcast_msg(CCUser* user, int type, const char* msg) {
const char* nick = NULL;
int level = 0;
char* output = NULL;
nick = cc_user_get_nickname(user);
level = cc_user_get_level(user);
output = malloc(strlen(nick) + strlen(msg) + 92);
switch (type) {
case 2:
sprintf(output, "%s\\\\\\\\\\ %s[%s]%s %s %s/////%s",
kGreen, get_colour(level), nick, kNormal, msg,
kGreen, kNormal);
break;
case 3:
sprintf(output, "%s///// %s[%s]%s %s %s\\\\\\\\\\%s",
kGreen, get_colour(level), nick, kNormal, msg,
kGreen, kNormal);
break;
default:
sprintf(output, "%s[%s]%s %s",
get_colour(level), nick, kNormal, msg);
}
add_line(output);
free(output);
}
void on_private_msg(CCUser* user, int type, const char* msg) {
const char* nick = NULL;
int level = 0;
char* output = NULL;
const char* prefix = "Private Message from";
nick = cc_user_get_nickname(user);
level = cc_user_get_level(user);
output = malloc(strlen(prefix) + strlen(nick) + strlen(msg) + 92);
sprintf(output, "%s%s %s[%s]%s: %s",
kMagenta, prefix, get_colour(level), nick, kNormal, msg);
add_line(output);
free(output);
}