mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Merge pull request #400 from JFreegman/master
Added actions/alternative type of messages
This commit is contained in:
commit
5024bab930
@ -247,6 +247,21 @@ uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message
|
||||
return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1 + sizeof(theid));
|
||||
}
|
||||
|
||||
/* send an action to an online friend
|
||||
return 1 if packet was successfully put into the send queue
|
||||
return 0 if it was not */
|
||||
int m_sendaction(int friendnumber, uint8_t *action, uint32_t length)
|
||||
{
|
||||
if (friendnumber < 0 || friendnumber >= numfriends)
|
||||
return 0;
|
||||
if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != FRIEND_ONLINE)
|
||||
return 0;
|
||||
uint8_t temp[MAX_DATA_SIZE];
|
||||
temp[0] = PACKET_ID_ACTION;
|
||||
memcpy(temp + 1, action, length);
|
||||
return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1);
|
||||
}
|
||||
|
||||
/* send a name packet to friendnumber
|
||||
length is the length with the NULL terminator*/
|
||||
static int m_sendname(int friendnumber, uint8_t * name, uint16_t length)
|
||||
@ -447,6 +462,14 @@ void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t))
|
||||
friend_message_isset = 1;
|
||||
}
|
||||
|
||||
static void (*friend_action)(int, uint8_t *, uint16_t);
|
||||
static uint8_t friend_action_isset = 0;
|
||||
void m_callback_action(void (*function)(int, uint8_t *, uint16_t))
|
||||
{
|
||||
friend_action = function;
|
||||
friend_action_isset = 1;
|
||||
}
|
||||
|
||||
static void (*friend_namechange)(int, uint8_t *, uint16_t);
|
||||
static uint8_t friend_namechange_isset = 0;
|
||||
void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t))
|
||||
@ -606,6 +629,11 @@ static void doFriends(void)
|
||||
(*friend_message)(i, temp + 5, len - 5);
|
||||
break;
|
||||
}
|
||||
case PACKET_ID_ACTION: {
|
||||
if (friend_action_isset)
|
||||
(*friend_action)(i, temp + 1, len - 1);
|
||||
break;
|
||||
}
|
||||
case PACKET_ID_RECEIPT: {
|
||||
uint32_t msgid;
|
||||
if (len < 1 + sizeof(msgid))
|
||||
|
@ -43,6 +43,7 @@ extern "C" {
|
||||
#define PACKET_ID_USERSTATUS 50
|
||||
#define PACKET_ID_RECEIPT 65
|
||||
#define PACKET_ID_MESSAGE 64
|
||||
#define PACKET_ID_ACTION 63
|
||||
|
||||
/* status definitions */
|
||||
#define FRIEND_ONLINE 4
|
||||
@ -122,6 +123,11 @@ int m_friendstatus(int friendnumber);
|
||||
uint32_t m_sendmessage(int friendnumber, uint8_t *message, uint32_t length);
|
||||
uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message, uint32_t length);
|
||||
|
||||
/* send an action to an online friend
|
||||
returns 1 if packet was successfully put into the send queue
|
||||
return 0 if it was not */
|
||||
int m_sendaction(int friendnumber, uint8_t *action, uint32_t length);
|
||||
|
||||
/* Set our nickname
|
||||
name must be a string of maximum MAX_NAME_LENGTH length.
|
||||
length must be at least 1 byte
|
||||
@ -178,6 +184,10 @@ void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
|
||||
function format is: function(int friendnumber, uint8_t * message, uint32_t length) */
|
||||
void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t));
|
||||
|
||||
/* set the function that will be executed when an action from a friend is received.
|
||||
function format is: function(int friendnumber, uint8_t * action, uint32_t length) */
|
||||
void m_callback_action(void (*function)(int, uint8_t *, uint16_t));
|
||||
|
||||
/* set the callback for name changes
|
||||
function(int friendnumber, uint8_t *newname, uint16_t length)
|
||||
you are not responsible for freeing newname */
|
||||
|
@ -27,7 +27,7 @@ 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, 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)
|
||||
{
|
||||
@ -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};
|
||||
time_t now;
|
||||
time(&now);
|
||||
struct tm * timeinfo;
|
||||
struct tm *timeinfo;
|
||||
timeinfo = localtime(&now);
|
||||
|
||||
if (ctx->friendnum != num)
|
||||
@ -59,6 +59,32 @@ static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len)
|
||||
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)
|
||||
{
|
||||
ChatContext *ctx = (ChatContext*) self->x;
|
||||
@ -108,7 +134,7 @@ static void chat_onKey(ToxWindow *self, int key)
|
||||
/* RETURN key: Execute command or print line */
|
||||
else if (key == '\n') {
|
||||
if (ctx->line[0] == '/')
|
||||
execute(self, ctx, ctx->line);
|
||||
execute(self, ctx, ctx->line, timeinfo);
|
||||
else {
|
||||
if (!string_is_empty(ctx->line)) {
|
||||
/* 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")) {
|
||||
wclear(self->window);
|
||||
@ -153,6 +179,33 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd)
|
||||
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 "))) {
|
||||
char *status = strchr(cmd, ' ');
|
||||
char *msg;
|
||||
@ -178,8 +231,7 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd)
|
||||
status_text = "BUSY";
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
else {
|
||||
wprintw(ctx->history, "Invalid status.\n");
|
||||
return;
|
||||
}
|
||||
@ -265,6 +317,7 @@ void print_help(ChatContext *self)
|
||||
|
||||
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");
|
||||
@ -285,6 +338,7 @@ ToxWindow new_chat(int friendnum)
|
||||
ret.onMessage = &chat_onMessage;
|
||||
ret.onNickChange = &chat_onNickChange;
|
||||
ret.onStatusChange = &chat_onStatusChange;
|
||||
ret.onAction = &chat_onAction;
|
||||
|
||||
uint8_t nick[MAX_NAME_LENGTH] = {0};
|
||||
getname(friendnum, (uint8_t*) &nick);
|
||||
|
@ -177,6 +177,7 @@ ToxWindow new_friendlist() {
|
||||
ret.onDraw = &friendlist_onDraw;
|
||||
ret.onInit = &friendlist_onInit;
|
||||
ret.onMessage = &friendlist_onMessage;
|
||||
ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message
|
||||
ret.onNickChange = &friendlist_onNickChange;
|
||||
ret.onStatusChange = &friendlist_onStatusChange;
|
||||
|
||||
|
@ -53,8 +53,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, "\n");
|
||||
wprintw(prompt->window, "Use \"accept %d\" to accept it.\n", n);
|
||||
wprintw(prompt->window, "\nWith the message: %s\n", data);
|
||||
wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n);
|
||||
|
||||
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (windows[i].onFriendRequest != NULL)
|
||||
windows[i].onFriendRequest(&windows[i], public_key, data, length);
|
||||
@ -63,7 +64,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)
|
||||
{
|
||||
wprintw(prompt->window, "\n(message) %d: %s\n", friendnumber, string);
|
||||
int i;
|
||||
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (windows[i].onMessage != NULL)
|
||||
@ -71,6 +71,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)
|
||||
{
|
||||
wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string);
|
||||
@ -126,6 +135,7 @@ static void init_tox()
|
||||
m_callback_friendmessage(on_message);
|
||||
m_callback_namechange(on_nickchange);
|
||||
m_callback_statusmessage(on_statuschange);
|
||||
m_callback_action(on_action);
|
||||
}
|
||||
|
||||
void init_window_status()
|
||||
@ -281,13 +291,13 @@ static void draw_bar()
|
||||
attron(A_BOLD);
|
||||
|
||||
odd = (odd+1) % blinkrate;
|
||||
if (windows[i].blink && (odd < (blinkrate/2))) {
|
||||
if (windows[i].blink && (odd < (blinkrate/2)))
|
||||
attron(COLOR_PAIR(3));
|
||||
}
|
||||
|
||||
printw(" %s", windows[i].title);
|
||||
if (windows[i].blink && (odd < (blinkrate/2))) {
|
||||
if (windows[i].blink && (odd < (blinkrate/2)))
|
||||
attroff(COLOR_PAIR(3));
|
||||
}
|
||||
|
||||
if (i == active_window) {
|
||||
attroff(A_BOLD);
|
||||
}
|
||||
@ -317,7 +327,6 @@ void set_active_window(int ch)
|
||||
i = (i + 1) % max;
|
||||
if (f_inf++ > max) { // infinite loop check
|
||||
endwin();
|
||||
clear();
|
||||
exit(2);
|
||||
}
|
||||
}
|
||||
@ -332,7 +341,6 @@ void set_active_window(int ch)
|
||||
if (--i < 0) i = max;
|
||||
if (f_inf++ > max) {
|
||||
endwin();
|
||||
clear();
|
||||
exit(2);
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ struct ToxWindow_ {
|
||||
void(*onMessage)(ToxWindow*, int, uint8_t*, uint16_t);
|
||||
void(*onNickChange)(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];
|
||||
|
||||
void* x;
|
||||
|
Loading…
x
Reference in New Issue
Block a user