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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
/*
* Toxic -- Tox Curses Client
*/
#include <curses.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <time.h>
#include "../../core/Messenger.h"
#include "../../core/network.h"
#include "windows.h"
#define CURS_Y_OFFSET 3
typedef struct {
int friendnum;
char line[MAX_STR_SIZE];
size_t pos;
WINDOW* history;
WINDOW* linewin;
} ChatContext;
extern int active_window;
extern void del_window(ToxWindow *w, int f_num);
extern void fix_name(uint8_t *name);
void print_help(ChatContext *self);
void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd);
struct tm *get_time(void)
{
struct tm *timeinfo;
time_t now;
time(&now);
timeinfo = localtime(&now);
return timeinfo;
}
static void chat_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *msg, uint16_t len)
{
ChatContext *ctx = (ChatContext*) self->x;
uint8_t nick[MAX_NAME_LENGTH] = {0};
struct tm *timeinfo = get_time();
if (ctx->friendnum != num)
return;
getname(m, num, (uint8_t*) &nick);
msg[len-1] = '\0';
nick[MAX_NAME_LENGTH-1] = '\0';
fix_name(msg);
fix_name(nick);
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
wattron(ctx->history, COLOR_PAIR(4));
wprintw(ctx->history, "%s: ", nick);
wattroff(ctx->history, COLOR_PAIR(4));
wprintw(ctx->history, "%s\n", msg);
self->blink = true;
beep();
}
static void chat_onAction(ToxWindow *self, Messenger *m, int num, uint8_t *action, uint16_t len)
{
ChatContext *ctx = (ChatContext*) self->x;
struct tm *timeinfo = get_time();
if (ctx->friendnum != num)
return;
action[len-1] = '\0';
fix_name(action);
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
wattron(ctx->history, COLOR_PAIR(5));
wprintw(ctx->history, "%s\n", action);
wattroff(ctx->history, COLOR_PAIR(5));
self->blink = true;
beep();
}
static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len)
{
ChatContext *ctx = (ChatContext*) self->x;
struct tm *timeinfo = get_time();
if (ctx->friendnum != num)
return;
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
nick[len-1] = '\0';
fix_name(nick);
snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num);
wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, "* Your partner changed nick to '%s'\n", nick);
wattroff(ctx->history, COLOR_PAIR(3));
}
static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len)
{
ChatContext *ctx = (ChatContext*) self->x;
struct tm *timeinfo = get_time();
if (ctx->friendnum != num)
return;
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
status[len-1] = '\0';
fix_name(status);
snprintf(self->title, sizeof(self->title), "[%s (%d)]", status, num);
wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, "* Your partner changed status to '%s'\n", status);
wattroff(ctx->history, COLOR_PAIR(3));
}
/* check that the string has one non-space character */
int string_is_empty(char *string)
{
int rc = 0;
char *copy = strdup(string);
rc = ((strtok(copy, " ") == NULL) ? 1:0);
free(copy);
return rc;
}
static void chat_onKey(ToxWindow *self, Messenger *m, int key)
{
ChatContext *ctx = (ChatContext*) self->x;
struct tm *timeinfo = get_time();
int x, y, y2, x2;
getyx(self->window, y, x);
getmaxyx(self->window, y2, x2);
/* Add printable chars to buffer and print on input space */
if (isprint(key)) {
if (ctx->pos != sizeof(ctx->line)-1) {
mvwaddch(self->window, y, x, key);
ctx->line[ctx->pos++] = key;
ctx->line[ctx->pos] = '\0';
}
}
/* BACKSPACE key: Remove one character from line */
else if (key == 0x107 || key == 0x8 || key == 0x7f) {
if (ctx->pos > 0) {
ctx->line[--ctx->pos] = '\0';
if (x == 0)
mvwdelch(self->window, y-1, x2-1);
else
mvwdelch(self->window, y, x-1);
}
}
/* RETURN key: Execute command or print line */
else if (key == '\n') {
wclear(ctx->linewin);
wmove(self->window, y2-CURS_Y_OFFSET, 0);
wclrtobot(self->window);
if (ctx->line[0] == '/')
execute(self, ctx, m, ctx->line);
else {
/* make sure the string has at least non-space character */
if (!string_is_empty(ctx->line)) {
uint8_t selfname[MAX_NAME_LENGTH];
getself_name(m, selfname, sizeof(selfname));
fix_name(selfname);
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
wattron(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, "%s: ", selfname);
wattroff(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, "%s\n", ctx->line);
if (m_sendmessage(m, ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) == 0) {
wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, " * Failed to send message.\n");
wattroff(ctx->history, COLOR_PAIR(3));
}
}
}
ctx->line[0] = '\0';
ctx->pos = 0;
}
}
void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
{
if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) {
wclear(self->window);
wclear(ctx->history);
int x, y;
getmaxyx(self->window, y, x);
(void) x;
wmove(self->window, y-CURS_Y_OFFSET, 0);
}
else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h"))
print_help(ctx);
else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) {
endwin();
exit(0);
}
else if (!strncmp(cmd, "/me ", strlen("/me "))) {
struct tm *timeinfo = get_time();
char *action = strchr(cmd, ' ');
if (action == NULL) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
action++;
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
uint8_t selfname[MAX_NAME_LENGTH];
int len = getself_name(m, selfname, sizeof(selfname));
char msg[MAX_STR_SIZE-len-4];
snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t*) selfname, action);
wattron(ctx->history, COLOR_PAIR(5));
wprintw(ctx->history, msg);
wattroff(ctx->history, COLOR_PAIR(5));
if (m_sendaction(m, ctx->friendnum, (uint8_t*) msg, strlen(msg)+1) < 0) {
wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, " * Failed to send action\n");
wattroff(ctx->history, COLOR_PAIR(3));
}
}
else if (!strncmp(cmd, "/status ", strlen("/status "))) {
char *status = strchr(cmd, ' ');
char *msg;
char *status_text;
if (status == NULL) {
wprintw(ctx->history, "Invalid syntax.\n");
return;
}
status++;
USERSTATUS status_kind;
if (!strncmp(status, "online", strlen("online"))) {
status_kind = USERSTATUS_NONE;
status_text = "ONLINE";
}
else if (!strncmp(status, "away", strlen("away"))) {
status_kind = USERSTATUS_AWAY;
status_text = "AWAY";
}
else if (!strncmp(status, "busy", strlen("busy"))) {
status_kind = USERSTATUS_BUSY;
status_text = "BUSY";
}
else {
wprintw(ctx->history, "Invalid status.\n");
return;
}
msg = strchr(status, ' ');
if (msg == NULL) {
m_set_userstatus(m, status_kind);
wprintw(ctx->history, "Status set to: %s\n", status_text);
}
else {
msg++;
m_set_userstatus(m, status_kind);
m_set_statusmessage(m, ( uint8_t*) msg, strlen(msg)+1);
wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg);
}
}
else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
char *nick;
nick = strchr(cmd, ' ');
if (nick == NULL) {
wprintw(ctx->history, "Invalid syntax.\n");
return;
}
nick++;
setname(m, (uint8_t*) nick, strlen(nick)+1);
wprintw(ctx->history, "Nickname set to: %s\n", nick);
}
else if (!strcmp(cmd, "/myid")) {
char id[FRIEND_ADDRESS_SIZE*2+1] = {0};
int i;
uint8_t address[FRIEND_ADDRESS_SIZE];
getaddress(m, address);
for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) {
char xx[3];
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
strcat(id, xx);
}
wprintw(ctx->history, "Your ID: %s\n", id);
}
else if (strcmp(ctx->line, "/close") == 0) {
active_window = 0; // Go to prompt screen
int f_num = ctx->friendnum;
delwin(ctx->linewin);
del_window(self, f_num);
}
else
wprintw(ctx->history, "Invalid command.\n");
}
static void chat_onDraw(ToxWindow *self)
{
curs_set(1);
int x, y;
getmaxyx(self->window, y, x);
(void) y;
ChatContext *ctx = (ChatContext*) self->x;
mvwhline(ctx->linewin, 0, 0, '_', x);
wrefresh(self->window);
}
static void chat_onInit(ToxWindow *self, Messenger *m)
{
int x, y;
ChatContext *ctx = (ChatContext*) self->x;
getmaxyx(self->window, y, x);
ctx->history = subwin(self->window, y-4, x, 0, 0);
scrollok(ctx->history, 1);
ctx->linewin = subwin(self->window, 2, x, y-4, 0);
print_help(ctx);
wmove(self->window, y-CURS_Y_OFFSET, 0);
}
void print_help(ChatContext *self)
{
wattron(self->history, COLOR_PAIR(2) | A_BOLD);
wprintw(self->history, "Commands:\n");
wattroff(self->history, A_BOLD);
wprintw(self->history, " /status <type> <message> : Set your status\n");
wprintw(self->history, " /nick <nickname> : Set your nickname\n");
wprintw(self->history, " /me <action> : Do an action\n");
wprintw(self->history, " /myid : Print your ID\n");
wprintw(self->history, " /clear : Clear the screen\n");
wprintw(self->history, " /close : Close the current chat window\n");
wprintw(self->history, " /quit or /exit : Exit program\n");
wprintw(self->history, " /help : Print this message again\n\n");
wattroff(self->history, COLOR_PAIR(2));
}
ToxWindow new_chat(Messenger *m, int friendnum)
{
ToxWindow ret;
memset(&ret, 0, sizeof(ret));
ret.onKey = &chat_onKey;
ret.onDraw = &chat_onDraw;
ret.onInit = &chat_onInit;
ret.onMessage = &chat_onMessage;
ret.onNickChange = &chat_onNickChange;
ret.onStatusChange = &chat_onStatusChange;
ret.onAction = &chat_onAction;
uint8_t nick[MAX_NAME_LENGTH] = {0};
getname(m, friendnum, (uint8_t*) &nick);
fix_name(nick);
snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum);
ChatContext *x = calloc(1, sizeof(ChatContext));
x->friendnum = friendnum;
ret.x = (void*) x;
return ret;
}
|