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

Merge pull request #24 from JFreegman/master

Changed statusmsg command to note & segfault fixes
This commit is contained in:
JFreegman 2013-09-03 17:10:01 -07:00
commit cd4e144aef
6 changed files with 78 additions and 68 deletions

View File

@ -104,10 +104,10 @@ static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t
nick[len - 1] = '\0';
snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num);
wprintw(ctx->history, "* Your partner changed nick to '%s'\n", nick);
wprintw(ctx->history, "* Chat partner changed nick to '%s'\n", nick);
}
static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len)
static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status, uint16_t len)
{
ChatContext *ctx = (ChatContext *) self->x;
struct tm *timeinfo = get_time();
@ -121,7 +121,7 @@ static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint1
status[len - 1] = '\0';
wprintw(ctx->history, "* Your partner changed status message to '%s'\n", status);
wprintw(ctx->history, "* Chat partner changed personal note to: %s\n", status);
}
/* check that the string has one non-space character */
@ -335,23 +335,21 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd)
return;
}
msg = strchr(status, ' ');
wprintw(ctx->history, "Status set to: %s\n", status_text);
tox_set_userstatus(m, status_kind);
if (msg == NULL) {
tox_set_userstatus(m, status_kind);
wprintw(ctx->history, "Status message set to: %s\n", status_text);
} else {
msg = strchr(status, ' ');
if (msg != NULL) {
msg++;
tox_set_userstatus(m, status_kind);
tox_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1);
wprintw(ctx->history, "Status message set to: %s, %s\n", status_text, msg);
wprintw(ctx->history, "Personal note set to: %s\n", msg);
}
}
else if (!strncmp(cmd, "/statusmsg ", strlen("/statusmsg "))) {
else if (!strncmp(cmd, "/note ", strlen("/note "))) {
char *msg = strchr(cmd, ' ');
msg++;
wprintw(ctx->history, "Status message set to: %s\n", msg);
wprintw(ctx->history, "Personal note set to: %s\n", msg);
tox_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1);
}
@ -417,8 +415,8 @@ void print_help(ChatContext *self)
wprintw(self->history, "Commands:\n");
wattroff(self->history, A_BOLD);
wprintw(self->history, " /status <type> <message> : Set your status\n");
wprintw(self->history, " /statusmsg <message> : Set your status message\n");
wprintw(self->history, " /status <type> <message> : Set your status with optional note\n");
wprintw(self->history, " /note <message> : Set a personal note\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");
@ -440,7 +438,7 @@ ToxWindow new_chat(Tox *m, int friendnum)
ret.onInit = &chat_onInit;
ret.onMessage = &chat_onMessage;
ret.onNickChange = &chat_onNickChange;
ret.onStatusChange = &chat_onStatusChange;
ret.onStatusMessageChange = &chat_onStatusMessageChange;
ret.onAction = &chat_onAction;
uint8_t nick[TOX_MAX_NAME_LENGTH] = {0};

View File

@ -51,7 +51,7 @@ void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t le
friends[num].name[len] = 0;
}
void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
void friendlist_onStatusMessageChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
{
if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num >= num_friends)
return;
@ -164,8 +164,8 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
wprintw(self->window, "Empty. Add some friends! :-)\n");
} else {
wattron(self->window, COLOR_PAIR(2) | A_BOLD);
wprintw(self->window, " * Open chat with up/down keys and enter. ");
wprintw(self->window, "Delete friends with the backspace key\n\n");
wprintw(self->window, " * Open chat with up/down keys and enter.\n");
wprintw(self->window, " * Delete friends with the backspace key.\n\n");
wattroff(self->window, COLOR_PAIR(2) | A_BOLD);
}
@ -237,7 +237,7 @@ ToxWindow new_friendlist()
ret.onMessage = &friendlist_onMessage;
ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message
ret.onNickChange = &friendlist_onNickChange;
ret.onStatusChange = &friendlist_onStatusChange;
ret.onStatusMessageChange = &friendlist_onStatusMessageChange;
strcpy(ret.title, "[friends]");
return ret;

View File

@ -94,7 +94,7 @@ static Tox *init_tox()
tox_callback_friendrequest(m, on_request, NULL);
tox_callback_friendmessage(m, on_message, NULL);
tox_callback_namechange(m, on_nickchange, NULL);
tox_callback_statusmessage(m, on_statuschange, NULL);
tox_callback_statusmessage(m, on_statusmessagechange, NULL);
tox_callback_action(m, on_action, NULL);
#ifdef __linux__
tox_setname(m, (uint8_t *) "Cool guy", sizeof("Cool guy"));

View File

@ -33,7 +33,7 @@ void cmd_nick(ToxWindow *, Tox *m, int, char **);
void cmd_mynick(ToxWindow *, Tox *m, int, char **);
void cmd_quit(ToxWindow *, Tox *m, int, char **);
void cmd_status(ToxWindow *, Tox *m, int, char **);
void cmd_statusmsg(ToxWindow *, Tox *m, int, char **);
void cmd_note(ToxWindow *, Tox *m, int, char **);
#define NUM_COMMANDS 14
@ -54,7 +54,7 @@ static struct {
{ "q", cmd_quit },
{ "quit", cmd_quit },
{ "status", cmd_status },
{ "statusmsg", cmd_statusmsg },
{ "note", cmd_note },
};
// XXX:
@ -109,6 +109,11 @@ void cmd_accept(ToxWindow *self, Tox *m, int argc, char **argv)
void cmd_add(ToxWindow *self, Tox *m, int argc, char **argv)
{
if (argc != 1 && argc != 2) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
uint8_t id_bin[TOX_FRIEND_ADDRESS_SIZE];
char xx[3];
uint32_t x;
@ -116,18 +121,18 @@ void cmd_add(ToxWindow *self, Tox *m, int argc, char **argv)
char *msg;
int i, num;
/* check arguments */
if (argv[2] && argv[2][0] != '\"') {
wprintw(self->window, "Strings must be enclosed in quotes.\n");
return;
}
if (argc != 1 && argc != 2) {
wprintw(self->window, "Invalid syntax.\n");
return;
}
id = argv[1];
msg = (argc == 2) ? argv[2] : "";
if (argc == 2) {
if (argv[2][0] != '\"') {
wprintw(self->window, "Messages must be enclosed in quotes.\n");
return;
}
msg = argv[2];
msg[strlen(++msg)-1] = L'\0';
} else
msg = "";
if (strlen(id) != 2 * TOX_FRIEND_ADDRESS_SIZE) {
wprintw(self->window, "Invalid ID length.\n");
@ -241,9 +246,9 @@ void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv)
wattroff(self->window, A_BOLD);
wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n");
wprintw(self->window, " add <id> <message> : Add friend\n");
wprintw(self->window, " status <type> <message> : Set your status\n");
wprintw(self->window, " statusmsg <message> : Set your status message\n");
wprintw(self->window, " add <id> <message> : Add friend with optional message\n");
wprintw(self->window, " status <type> <message> : Set your status with optional note\n");
wprintw(self->window, " note <message> : Set a personal note\n");
wprintw(self->window, " nick <nickname> : Set your nickname\n");
wprintw(self->window, " mynick : Print your current nickname\n");
wprintw(self->window, " accept <number> : Accept friend request\n");
@ -253,8 +258,8 @@ void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv)
wprintw(self->window, " clear : Clear this window\n");
wattron(self->window, A_BOLD);
wprintw(self->window, "NOTE: Strings must be enclosed in quotation marks.\n");
wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n");
wprintw(self->window, " * Messages must be enclosed in quotation marks.\n");
wprintw(self->window, " * Use the TAB key to navigate through the tabs.\n\n");
wattroff(self->window, A_BOLD);
wattroff(self->window, COLOR_PAIR(2));
@ -272,9 +277,10 @@ void cmd_msg(ToxWindow *self, Tox *m, int argc, char **argv)
id = argv[1];
msg = argv[2];
msg[strlen(++msg)-1] = L'\0';
if (tox_sendmessage(m, atoi(id), (uint8_t *) msg, strlen(msg) + 1) == 0)
wprintw(self->window, "Error occurred while sending message.\n");
wprintw(self->window, "Failed to send message.\n");
else
wprintw(self->window, "Message successfully sent.\n");
}
@ -325,18 +331,24 @@ void cmd_mynick(ToxWindow *self, Tox *m, int argc, char **argv)
void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv)
{
char *status, *status_text, *msg;
/* check arguments */
if (argv[2] && argv[2][0] != '\"') {
wprintw(self->window, "Strings must be enclosed in quotes.\n");
return;
}
if (argc != 1 && argc != 2) {
wprintw(self->window, "Wrong number of arguments.\n");
return;
}
char *status, *status_text;
char *msg = NULL;
/* check arguments */
if (argc == 2) {
msg = argv[2];
if (msg[0] != '\"') {
wprintw(self->window, "Messages must be enclosed in quotes.\n");
return;
}
}
status = argv[1];
TOX_USERSTATUS status_kind;
@ -355,36 +367,36 @@ void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv)
return;
}
msg = argv[2];
wprintw(self->window, "Status set to: %s\n", status_text);
tox_set_userstatus(m, status_kind);
if (msg == NULL) {
tox_set_userstatus(m, status_kind);
wprintw(self->window, "Status message set to: %s\n", status_text);
} else {
tox_set_userstatus(m, status_kind);
if (msg != NULL) {
msg[strlen(++msg)-1] = L'\0'; /* remove opening and closing quotes */
tox_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1);
wprintw(self->window, "Status message set to: %s, %s\n", status_text, msg);
wprintw(self->window, "Personal note set to: %s\n", msg);
}
}
void cmd_statusmsg(ToxWindow *self, Tox *m, int argc, char **argv)
void cmd_note(ToxWindow *self, Tox *m, int argc, char **argv)
{
char *msg;
/* check arguments */
if (argv[1] && argv[1][0] != '\"') {
wprintw(self->window, "Strings must be enclosed in quotes.\n");
return;
}
if (argc != 1) {
wprintw(self->window, "Wrong number of arguments.\n");
return;
}
char *msg;
/* check arguments */
if (argv[1] && argv[1][0] != '\"') {
wprintw(self->window, "Messages must be enclosed in quotes.\n");
return;
}
msg = argv[1];
msg[strlen(++msg)-1] = L'\0';
tox_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1);
wprintw(self->window, "Status message set to: %s\n", msg);
wprintw(self->window, "Personal note set to: %s\n", msg);
}
static void execute(ToxWindow *self, Tox *m, char *u_cmd)

View File

@ -33,7 +33,7 @@ struct ToxWindow_ {
void(*onFriendRequest)(ToxWindow *, uint8_t *, uint8_t *, uint16_t);
void(*onMessage)(ToxWindow *, Tox *, int, uint8_t *, uint16_t);
void(*onNickChange)(ToxWindow *, int, uint8_t *, uint16_t);
void(*onStatusChange)(ToxWindow *, int, uint8_t *, uint16_t);
void(*onStatusMessageChange)(ToxWindow *, int, uint8_t *, uint16_t);
void(*onAction)(ToxWindow *, Tox *, int, uint8_t *, uint16_t);
char title[256];
@ -47,7 +47,7 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userd
void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void on_action(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void on_statuschange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void on_friendadded(Tox *m, int friendnumber);
ToxWindow *init_windows();
void draw_active_window(Tox *m);

View File

@ -69,14 +69,14 @@ void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, v
}
}
void on_statuschange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
{
wprintw(prompt->window, "\n(message change) %d: %s\n", friendnumber, string);
wprintw(prompt->window, "\n(note change) %d: %s\n", friendnumber, string);
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].onStatusChange != NULL)
windows[i].onStatusChange(&windows[i], friendnumber, string, length);
if (windows[i].onStatusMessageChange != NULL)
windows[i].onStatusMessageChange(&windows[i], friendnumber, string, length);
}
}