Merge branch 'master' of https://github.com/JFreegman/ProjectTox-Core into JFreegman-master

This commit is contained in:
irungentoo 2013-08-13 07:20:36 -04:00
commit d0cd78fbea
2 changed files with 64 additions and 31 deletions

View File

@ -29,16 +29,22 @@ extern int active_window;
extern void del_window(ToxWindow *w, int f_num); extern void del_window(ToxWindow *w, int f_num);
extern void fix_name(uint8_t *name); extern void fix_name(uint8_t *name);
void print_help(ChatContext *self); void print_help(ChatContext *self);
void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd, struct tm *timeinfo); 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) static void chat_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *msg, uint16_t len)
{ {
ChatContext *ctx = (ChatContext*) self->x; ChatContext *ctx = (ChatContext*) self->x;
uint8_t nick[MAX_NAME_LENGTH] = {0}; uint8_t nick[MAX_NAME_LENGTH] = {0};
time_t now; struct tm *timeinfo = get_time();
time(&now);
struct tm *timeinfo;
timeinfo = localtime(&now);
if (ctx->friendnum != num) if (ctx->friendnum != num)
return; return;
@ -64,10 +70,7 @@ static void chat_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *msg,
static void chat_onAction(ToxWindow *self, Messenger *m, int num, uint8_t *action, uint16_t len) static void chat_onAction(ToxWindow *self, Messenger *m, int num, uint8_t *action, uint16_t len)
{ {
ChatContext *ctx = (ChatContext*) self->x; ChatContext *ctx = (ChatContext*) self->x;
time_t now; struct tm *timeinfo = get_time();
time(&now);
struct tm *timeinfo;
timeinfo = localtime(&now);
if (ctx->friendnum != num) if (ctx->friendnum != num)
return; return;
@ -79,9 +82,9 @@ static void chat_onAction(ToxWindow *self, Messenger *m, int num, uint8_t *actio
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2)); wattroff(ctx->history, COLOR_PAIR(2));
wattron(ctx->history, COLOR_PAIR(4)); wattron(ctx->history, COLOR_PAIR(5));
wprintw(ctx->history, "%s\n", action); wprintw(ctx->history, "%s\n", action);
wattroff(ctx->history, COLOR_PAIR(4)); wattroff(ctx->history, COLOR_PAIR(5));
self->blink = true; self->blink = true;
beep(); beep();
@ -90,20 +93,41 @@ static void chat_onAction(ToxWindow *self, Messenger *m, int num, uint8_t *actio
static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len) static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len)
{ {
ChatContext *ctx = (ChatContext*) self->x; ChatContext *ctx = (ChatContext*) self->x;
struct tm *timeinfo = get_time();
if (ctx->friendnum != num) if (ctx->friendnum != num)
return; 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'; nick[len-1] = '\0';
fix_name(nick); fix_name(nick);
snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num); snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num);
wattron(ctx->history, COLOR_PAIR(3)); wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, " * Your partner changed nick to '%s'\n", nick); wprintw(ctx->history, "* Your partner changed nick to '%s'\n", nick);
wattroff(ctx->history, COLOR_PAIR(3)); wattroff(ctx->history, COLOR_PAIR(3));
} }
static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len) 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));
} }
@ -120,10 +144,7 @@ int string_is_empty(char *string)
static void chat_onKey(ToxWindow *self, Messenger *m, int key) static void chat_onKey(ToxWindow *self, Messenger *m, int key)
{ {
ChatContext *ctx = (ChatContext*) self->x; ChatContext *ctx = (ChatContext*) self->x;
time_t now; struct tm *timeinfo = get_time();
time(&now);
struct tm * timeinfo;
timeinfo = localtime(&now);
int x, y, y2, x2; int x, y, y2, x2;
getyx(self->window, y, x); getyx(self->window, y, x);
@ -155,30 +176,34 @@ static void chat_onKey(ToxWindow *self, Messenger *m, int key)
wmove(self->window, y2-CURS_Y_OFFSET, 0); wmove(self->window, y2-CURS_Y_OFFSET, 0);
wclrtobot(self->window); wclrtobot(self->window);
if (ctx->line[0] == '/') if (ctx->line[0] == '/')
execute(self, ctx, m, ctx->line, timeinfo); execute(self, ctx, m, ctx->line);
else { else {
if (!string_is_empty(ctx->line)) {
/* make sure the string has at least non-space character */ /* 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);
fix_name(selfname);
wattron(ctx->history, COLOR_PAIR(2)); wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2)); wattroff(ctx->history, COLOR_PAIR(2));
wattron(ctx->history, COLOR_PAIR(1)); wattron(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, "you: ", ctx->line); wprintw(ctx->history, "%s: ", selfname);
wattroff(ctx->history, COLOR_PAIR(1)); wattroff(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, "%s\n", ctx->line); wprintw(ctx->history, "%s\n", ctx->line);
}
if (m_sendmessage(m, ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) == 0) { if (m_sendmessage(m, ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) == 0) {
wattron(ctx->history, COLOR_PAIR(3)); wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, " * Failed to send message.\n"); wprintw(ctx->history, " * Failed to send message.\n");
wattroff(ctx->history, COLOR_PAIR(3)); wattroff(ctx->history, COLOR_PAIR(3));
} }
} }
}
ctx->line[0] = '\0'; ctx->line[0] = '\0';
ctx->pos = 0; ctx->pos = 0;
} }
} }
void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd, struct tm *timeinfo) void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
{ {
if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) {
wclear(self->window); wclear(self->window);
@ -198,6 +223,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd, struct
} }
else if (!strncmp(cmd, "/me ", strlen("/me "))) { else if (!strncmp(cmd, "/me ", strlen("/me "))) {
struct tm *timeinfo = get_time();
char *action = strchr(cmd, ' '); char *action = strchr(cmd, ' ');
if (action == NULL) { if (action == NULL) {
wprintw(self->window, "Invalid syntax.\n"); wprintw(self->window, "Invalid syntax.\n");
@ -214,9 +240,9 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd, struct
char msg[MAX_STR_SIZE-len-4]; char msg[MAX_STR_SIZE-len-4];
snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t*) selfname, action); snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t*) selfname, action);
wattron(ctx->history, COLOR_PAIR(1)); wattron(ctx->history, COLOR_PAIR(5));
wprintw(ctx->history, msg); wprintw(ctx->history, msg);
wattroff(ctx->history, COLOR_PAIR(1)); wattroff(ctx->history, COLOR_PAIR(5));
if (m_sendaction(m, ctx->friendnum, (uint8_t*) msg, strlen(msg)+1) < 0) { if (m_sendaction(m, ctx->friendnum, (uint8_t*) msg, strlen(msg)+1) < 0) {
wattron(ctx->history, COLOR_PAIR(3)); wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, " * Failed to send action\n"); wprintw(ctx->history, " * Failed to send action\n");
@ -274,6 +300,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd, struct
wprintw(ctx->history, "Invalid syntax.\n"); wprintw(ctx->history, "Invalid syntax.\n");
return; return;
} }
nick++; nick++;
setname(m, (uint8_t*) nick, strlen(nick)+1); setname(m, (uint8_t*) nick, strlen(nick)+1);
wprintw(ctx->history, "Nickname set to: %s\n", nick); wprintw(ctx->history, "Nickname set to: %s\n", nick);

View File

@ -124,6 +124,7 @@ static void init_term()
init_pair(2, COLOR_CYAN, COLOR_BLACK); init_pair(2, COLOR_CYAN, COLOR_BLACK);
init_pair(3, COLOR_RED, COLOR_BLACK); init_pair(3, COLOR_RED, COLOR_BLACK);
init_pair(4, COLOR_BLUE, COLOR_BLACK); init_pair(4, COLOR_BLUE, COLOR_BLACK);
init_pair(5, COLOR_YELLOW, COLOR_BLACK);
} }
refresh(); refresh();
} }
@ -139,6 +140,7 @@ static void init_tox()
m_callback_namechange(m, on_nickchange, NULL); m_callback_namechange(m, on_nickchange, NULL);
m_callback_statusmessage(m, on_statuschange, NULL); m_callback_statusmessage(m, on_statuschange, NULL);
m_callback_action(m, on_action, NULL); m_callback_action(m, on_action, NULL);
setname(m, (uint8_t*) "n00b", strlen("n00b")+1);
} }
#define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */ #define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */
@ -179,7 +181,7 @@ int init_connection(void)
dht.port = htons(atoi(port)); dht.port = htons(atoi(port));
uint32_t resolved_address = resolve_addr(ip); uint32_t resolved_address = resolve_addr(ip);
if (resolved_address == 0) if (resolved_address == 0)
return 4; return 0;
dht.ip.i = resolved_address; dht.ip.i = resolved_address;
unsigned char *binary_string = hex_string_to_bin(key); unsigned char *binary_string = hex_string_to_bin(key);
DHT_bootstrap(dht, binary_string); DHT_bootstrap(dht, binary_string);
@ -250,10 +252,15 @@ static void init_windows()
static void do_tox() static void do_tox()
{ {
static int conn_try = 0; static int conn_try = 0;
static int conn_err = 0;
static bool dht_on = false; static bool dht_on = false;
if (!dht_on && !DHT_isconnected() && !(conn_try++ % 100)) { if (!dht_on && !DHT_isconnected() && !(conn_try++ % 100)) {
init_connection(); if (!conn_err) {
conn_err = init_connection();
wprintw(prompt->window, "\nEstablishing connection...\n"); wprintw(prompt->window, "\nEstablishing connection...\n");
if (conn_err)
wprintw(prompt->window, "\nAuto-connect failed with error code %d\n", conn_err);
}
} }
else if (!dht_on && DHT_isconnected()) { else if (!dht_on && DHT_isconnected()) {
dht_on = true; dht_on = true;
@ -262,7 +269,6 @@ static void do_tox()
else if (dht_on && !DHT_isconnected()) { else if (dht_on && !DHT_isconnected()) {
dht_on = false; dht_on = false;
wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n"); wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n");
init_connection();
} }
doMessenger(m); doMessenger(m);
} }