1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-12 23:33:03 +01:00

Merge pull request #319 from stqism/master

Manually merged #314 and #317, resolving errors if both merged
This commit is contained in:
irungentoo 2013-08-04 07:18:20 -07:00
commit 19efe3727a
2 changed files with 93 additions and 24 deletions

98
chat.c
View File

@ -26,6 +26,8 @@ typedef struct {
} ChatContext;
extern void fix_name(uint8_t* name);
void print_help(ChatContext* self);
void execute(ToxWindow* self, ChatContext* ctx, char* cmd);
static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) {
ChatContext* ctx = (ChatContext*) self->x;
@ -49,6 +51,7 @@ static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len)
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));
@ -97,41 +100,93 @@ static void chat_onKey(ToxWindow* self, int key) {
struct tm * timeinfo;
timeinfo = localtime(&now);
/* PRINTABLE characters: Add to line */
if(isprint(key)) {
if(ctx->pos != sizeof(ctx->line)-1) {
ctx->line[ctx->pos++] = key;
ctx->line[ctx->pos] = '\0';
}
}
/* RETURN key: Execute command or print line */
else if(key == '\n') {
if(!string_is_empty(ctx->line)) {
/* make sure the string has at least non-space character */
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattron(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, "you: ", ctx->line);
wattroff(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, "%s\n", ctx->line);
if (ctx->line[0] == '/')
execute(self, ctx, ctx->line);
else {
if(!string_is_empty(ctx->line)) {
/* make sure the string has at least non-space character */
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, "you: ", ctx->line);
wattroff(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, "%s\n", ctx->line);
}
if(m_sendmessage(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;
}
ctx->line[0] = '\0';
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';
}
}
}
void execute(ToxWindow* self, ChatContext* ctx, char* cmd)
{
if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) {
wclear(self->window);
wclear(ctx->history);
}
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, "/status ", strlen("/status "))) {
char* msg;
msg = strchr(cmd, ' ');
if(msg == NULL) {
wprintw(ctx->history, "Invalid syntax.\n");
return;
}
msg++;
m_set_userstatus((uint8_t*) msg, strlen(msg)+1);
wprintw(ctx->history, "Status set to: %s\n", 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((uint8_t*) nick, strlen(nick)+1);
wprintw(ctx->history, "Nickname set to: %s\n", nick);
}
else if(!strcmp(cmd, "/myid")) {
char id[32*2 + 1] = {0};
int i;
for (i = 0; i < 32; i++) {
char xx[3];
snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff);
strcat(id, xx);
}
wprintw(ctx->history, "Your ID: %s\n", id);
}
else
wprintw(ctx->history, "Invalid command.\n");
}
static void chat_onDraw(ToxWindow* self) {
@ -164,6 +219,21 @@ static void chat_onInit(ToxWindow* self) {
ctx->linewin = subwin(self->window, 2, x, y - 3, 0);
}
void print_help(ChatContext* self) {
wattron(self->history, COLOR_PAIR(2) | A_BOLD);
wprintw(self->history, "\nCommands:\n");
wattroff(self->history, A_BOLD);
wprintw(self->history, " /status <message> : Set your status\n");
wprintw(self->history, " /nick <nickname> : Set your nickname\n");
wprintw(self->history, " /myid : Print your ID\n");
wprintw(self->history, " /clear : Clear the screen\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(int friendnum) {
ToxWindow ret;
@ -186,6 +256,6 @@ ToxWindow new_chat(int friendnum) {
x->friendnum = friendnum;
ret.x = (void*) x;
free(x);
return ret;
}

View File

@ -89,7 +89,9 @@ static void execute(ToxWindow* self, char* cmd) {
}
dht.ip.i = resolved_address;
DHT_bootstrap(dht, hex_string_to_bin(key));
unsigned char *binary_string = hex_string_to_bin(key);
DHT_bootstrap(dht, binary_string);
free(binary_string);
}
else if(!strncmp(cmd, "add ", strlen("add "))) {
uint8_t id_bin[32];
@ -148,7 +150,7 @@ static void execute(ToxWindow* self, char* cmd) {
wprintw(self->window, "Friend request already sent.\n");
break;
case -5:
wprintw(self->window, "[i] Undefined error when adding friend.\n");
wprintw(self->window, "Undefined error when adding friend.\n");
break;
default:
wprintw(self->window, "Friend added as %d.\n", num);
@ -178,12 +180,13 @@ static void execute(ToxWindow* self, char* cmd) {
nick = strchr(cmd, ' ');
if(nick == NULL) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
nick++;
setname((uint8_t*) nick, strlen(nick)+1);
wprintw(self->window, "Nickname set to: %s.\n", nick);
wprintw(self->window, "Nickname set to: %s\n", nick);
}
else if(!strcmp(cmd, "myid")) {
char id[32*2 + 1] = {0};
@ -195,7 +198,7 @@ static void execute(ToxWindow* self, char* cmd) {
strcat(id, xx);
}
wprintw(self->window, "%s\n", id);
wprintw(self->window, "Your ID: %s\n", id);
}
else if(!strncmp(cmd, "accept ", strlen("accept "))) {
char* id;
@ -251,20 +254,18 @@ static void execute(ToxWindow* self, char* cmd) {
wprintw(self->window, "Message successfully sent.\n");
}
}
else {
wprintw(self->window, "Invalid syntax.\n");
wprintw(self->window, "Invalid command.\n");
}
}
static void prompt_onKey(ToxWindow* self, int key) {
// PRINTABLE characters: Add to line.
if(isprint(key)) {
if(prompt_buf_pos == (sizeof(prompt_buf) - 1)) {
return;
}
prompt_buf[prompt_buf_pos++] = key;
prompt_buf[prompt_buf_pos] = 0;
}
@ -273,14 +274,12 @@ static void prompt_onKey(ToxWindow* self, int key) {
else if(key == '\n') {
wprintw(self->window, "\n");
execute(self, prompt_buf);
prompt_buf_pos = 0;
prompt_buf[0] = 0;
}
// BACKSPACE key: Remove one character from line.
else if(key == 0x107 || key == 0x8 || key == 0x7f) {
if(prompt_buf_pos != 0) {
prompt_buf[--prompt_buf_pos] = 0;
}