1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-26 15:03:27 +01:00

added /me actions to toxic

This commit is contained in:
Jfreegman 2013-08-08 15:01:33 -04:00
parent 90b4f9b98c
commit 30377630ee
4 changed files with 79 additions and 15 deletions

66
chat.c
View File

@ -27,7 +27,7 @@ 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, char *cmd); void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo);
static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len) static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len)
{ {
@ -35,7 +35,7 @@ static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len)
uint8_t nick[MAX_NAME_LENGTH] = {0}; uint8_t nick[MAX_NAME_LENGTH] = {0};
time_t now; time_t now;
time(&now); time(&now);
struct tm * timeinfo; struct tm *timeinfo;
timeinfo = localtime(&now); timeinfo = localtime(&now);
if (ctx->friendnum != num) if (ctx->friendnum != num)
@ -59,6 +59,32 @@ static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len)
beep(); beep();
} }
static void chat_onAction(ToxWindow *self, int num, uint8_t *action, uint16_t len)
{
ChatContext *ctx = (ChatContext*) self->x;
time_t now;
time(&now);
struct tm *timeinfo;
timeinfo = localtime(&now);
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(4));
wprintw(ctx->history, "%s\n", action);
wattroff(ctx->history, COLOR_PAIR(4));
self->blink = true;
beep();
}
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;
@ -108,7 +134,7 @@ static void chat_onKey(ToxWindow *self, int key)
/* RETURN key: Execute command or print line */ /* RETURN key: Execute command or print line */
else if (key == '\n') { else if (key == '\n') {
if (ctx->line[0] == '/') if (ctx->line[0] == '/')
execute(self, ctx, ctx->line); execute(self, ctx, ctx->line, timeinfo);
else { else {
if (!string_is_empty(ctx->line)) { 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 */
@ -138,7 +164,7 @@ static void chat_onKey(ToxWindow *self, int key)
} }
} }
void execute(ToxWindow *self, ChatContext *ctx, char *cmd) void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo)
{ {
if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) {
wclear(self->window); wclear(self->window);
@ -153,6 +179,33 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd)
exit(0); exit(0);
} }
else if (!strncmp(cmd, "/me ", strlen("/me "))) {
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(selfname);
char msg[MAX_STR_SIZE-len-4];
snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t*) selfname, action);
wattron(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, msg);
wattroff(ctx->history, COLOR_PAIR(1));
if (m_sendaction(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 "))) { else if (!strncmp(cmd, "/status ", strlen("/status "))) {
char *status = strchr(cmd, ' '); char *status = strchr(cmd, ' ');
char *msg; char *msg;
@ -178,8 +231,7 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd)
status_text = "BUSY"; status_text = "BUSY";
} }
else else {
{
wprintw(ctx->history, "Invalid status.\n"); wprintw(ctx->history, "Invalid status.\n");
return; return;
} }
@ -265,6 +317,7 @@ void print_help(ChatContext *self)
wprintw(self->history, " /status <type> <message> : Set your status\n"); wprintw(self->history, " /status <type> <message> : Set your status\n");
wprintw(self->history, " /nick <nickname> : Set your nickname\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, " /myid : Print your ID\n");
wprintw(self->history, " /clear : Clear the screen\n"); wprintw(self->history, " /clear : Clear the screen\n");
wprintw(self->history, " /close : Close the current chat window\n"); wprintw(self->history, " /close : Close the current chat window\n");
@ -285,6 +338,7 @@ ToxWindow new_chat(int friendnum)
ret.onMessage = &chat_onMessage; ret.onMessage = &chat_onMessage;
ret.onNickChange = &chat_onNickChange; ret.onNickChange = &chat_onNickChange;
ret.onStatusChange = &chat_onStatusChange; ret.onStatusChange = &chat_onStatusChange;
ret.onAction = &chat_onAction;
uint8_t nick[MAX_NAME_LENGTH] = {0}; uint8_t nick[MAX_NAME_LENGTH] = {0};
getname(friendnum, (uint8_t*) &nick); getname(friendnum, (uint8_t*) &nick);

View File

@ -177,6 +177,7 @@ ToxWindow new_friendlist() {
ret.onDraw = &friendlist_onDraw; ret.onDraw = &friendlist_onDraw;
ret.onInit = &friendlist_onInit; ret.onInit = &friendlist_onInit;
ret.onMessage = &friendlist_onMessage; ret.onMessage = &friendlist_onMessage;
ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message
ret.onNickChange = &friendlist_onNickChange; ret.onNickChange = &friendlist_onNickChange;
ret.onStatusChange = &friendlist_onStatusChange; ret.onStatusChange = &friendlist_onStatusChange;

26
main.c
View File

@ -44,8 +44,9 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length)
wprintw(prompt->window, "%02x", public_key[i] & 0xff); wprintw(prompt->window, "%02x", public_key[i] & 0xff);
} }
wprintw(prompt->window, "\n"); wprintw(prompt->window, "\nWith the message: %s\n", data);
wprintw(prompt->window, "Use \"accept %d\" to accept it.\n", n); wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n);
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onFriendRequest != NULL) if (windows[i].onFriendRequest != NULL)
windows[i].onFriendRequest(&windows[i], public_key, data, length); windows[i].onFriendRequest(&windows[i], public_key, data, length);
@ -54,7 +55,6 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length)
void on_message(int friendnumber, uint8_t *string, uint16_t length) void on_message(int friendnumber, uint8_t *string, uint16_t length)
{ {
wprintw(prompt->window, "\n(message) %d: %s\n", friendnumber, string);
int i; int i;
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onMessage != NULL) if (windows[i].onMessage != NULL)
@ -62,6 +62,15 @@ void on_message(int friendnumber, uint8_t *string, uint16_t length)
} }
} }
void on_action(int friendnumber, uint8_t *string, uint16_t length)
{
int i;
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onAction != NULL)
windows[i].onAction(&windows[i], friendnumber, string, length);
}
}
void on_nickchange(int friendnumber, uint8_t *string, uint16_t length) void on_nickchange(int friendnumber, uint8_t *string, uint16_t length)
{ {
wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string); wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string);
@ -117,6 +126,7 @@ static void init_tox()
m_callback_friendmessage(on_message); m_callback_friendmessage(on_message);
m_callback_namechange(on_nickchange); m_callback_namechange(on_nickchange);
m_callback_statusmessage(on_statuschange); m_callback_statusmessage(on_statuschange);
m_callback_action(on_action);
} }
void init_window_status() void init_window_status()
@ -272,13 +282,13 @@ static void draw_bar()
attron(A_BOLD); attron(A_BOLD);
odd = (odd+1) % blinkrate; odd = (odd+1) % blinkrate;
if (windows[i].blink && (odd < (blinkrate/2))) { if (windows[i].blink && (odd < (blinkrate/2)))
attron(COLOR_PAIR(3)); attron(COLOR_PAIR(3));
}
printw(" %s", windows[i].title); printw(" %s", windows[i].title);
if (windows[i].blink && (odd < (blinkrate/2))) { if (windows[i].blink && (odd < (blinkrate/2)))
attroff(COLOR_PAIR(3)); attroff(COLOR_PAIR(3));
}
if (i == active_window) { if (i == active_window) {
attroff(A_BOLD); attroff(A_BOLD);
} }
@ -308,7 +318,6 @@ void set_active_window(int ch)
i = (i + 1) % max; i = (i + 1) % max;
if (f_inf++ > max) { // infinite loop check if (f_inf++ > max) { // infinite loop check
endwin(); endwin();
clear();
exit(2); exit(2);
} }
} }
@ -323,7 +332,6 @@ void set_active_window(int ch)
if (--i < 0) i = max; if (--i < 0) i = max;
if (f_inf++ > max) { if (f_inf++ > max) {
endwin(); endwin();
clear();
exit(2); exit(2);
} }
} }

View File

@ -24,6 +24,7 @@ struct ToxWindow_ {
void(*onMessage)(ToxWindow*, int, uint8_t*, uint16_t); void(*onMessage)(ToxWindow*, int, uint8_t*, uint16_t);
void(*onNickChange)(ToxWindow*, int, uint8_t*, uint16_t); void(*onNickChange)(ToxWindow*, int, uint8_t*, uint16_t);
void(*onStatusChange)(ToxWindow*, int, uint8_t*, uint16_t); void(*onStatusChange)(ToxWindow*, int, uint8_t*, uint16_t);
void(*onAction)(ToxWindow*, int, uint8_t*, uint16_t);
char title[256]; char title[256];
void* x; void* x;