From 26dda8dbf919ac9e31fab5639198ba10b9624f14 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sun, 4 Jan 2015 01:56:37 -0500 Subject: [PATCH] implement new groupchats --- src/chat.c | 96 +++------ src/chat_commands.c | 67 +------ src/chat_commands.h | 2 - src/execute.c | 8 +- src/execute.h | 2 +- src/friendlist.c | 30 --- src/friendlist.h | 8 - src/global_commands.c | 88 ++++++-- src/global_commands.h | 1 + src/group_commands.c | 51 +++-- src/group_commands.h | 3 +- src/groupchat.c | 453 ++++++++++++++++++++++-------------------- src/groupchat.h | 13 +- src/help.c | 28 +-- src/line_info.c | 2 +- src/line_info.h | 2 +- src/misc_tools.c | 16 +- src/toxic.c | 50 +++-- src/toxic.h | 17 +- src/windows.c | 119 +++++++++-- src/windows.h | 23 ++- 21 files changed, 563 insertions(+), 516 deletions(-) diff --git a/src/chat.c b/src/chat.c index 88ae56a..a47aefc 100644 --- a/src/chat.c +++ b/src/chat.c @@ -64,9 +64,9 @@ static void kill_infobox(ToxWindow *self); #endif /* AUDIO */ #ifdef AUDIO -#define AC_NUM_CHAT_COMMANDS 27 +#define AC_NUM_CHAT_COMMANDS 26 #else -#define AC_NUM_CHAT_COMMANDS 20 +#define AC_NUM_CHAT_COMMANDS 19 #endif /* AUDIO */ /* Array of chat command names used for tab completion. */ @@ -81,7 +81,6 @@ static const char chat_cmd_list[AC_NUM_CHAT_COMMANDS][MAX_CMDNAME_SIZE] = { { "/exit" }, { "/group" }, { "/help" }, - { "/invite" }, { "/join" }, { "/log" }, { "/myid" }, @@ -160,12 +159,12 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int32_t num, const char *msg line_info_add(self, timefrmt, nick, NULL, IN_MSG, 0, 0, "%s", msg); write_to_log(msg, nick, ctx->log, false); - - if (self->active_box != -1) - box_notify2(self, generic_message, NT_WNDALERT_1 | NT_NOFOCUS, self->active_box, "%s", msg); - else + + if (self->active_box != -1) + box_notify2(self, generic_message, NT_WNDALERT_1 | NT_NOFOCUS, self->active_box, "%s", msg); + else box_notify(self, generic_message, NT_WNDALERT_1 | NT_NOFOCUS, &self->active_box, nick, "%s", msg); - + } static void chat_resume_file_transfers(Tox *m, int fnum); @@ -188,7 +187,7 @@ static void chat_onConnectionChange(ToxWindow *self, Tox *m, int32_t num, uint8_ if (status == 1) { /* Friend goes online */ statusbar->is_online = true; - Friends.list[num].is_typing = user_settings->show_typing_other == SHOW_TYPING_ON + Friends.list[num].is_typing = user_settings->show_typing_other == SHOW_TYPING_ON ? tox_get_is_typing(m, num) : 0; chat_resume_file_transfers(m, num); @@ -233,7 +232,7 @@ static void chat_onAction(ToxWindow *self, Tox *m, int32_t num, const char *acti line_info_add(self, timefrmt, nick, NULL, IN_ACTION, 0, 0, "%s", action); write_to_log(action, nick, ctx->log, true); - + if (self->active_box != -1) box_notify2(self, generic_message, NT_WNDALERT_1 | NT_NOFOCUS, self->active_box, "* %s %s", nick, action ); else @@ -352,10 +351,10 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t strcpy(Friends.list[num].file_receiver[filenum].filename, filename); if (self->active_box != -1) - box_notify2(self, transfer_pending, NT_WNDALERT_0 | NT_NOFOCUS, self->active_box, + box_notify2(self, transfer_pending, NT_WNDALERT_0 | NT_NOFOCUS, self->active_box, "Incoming file: %s", filename ); else - box_notify(self, transfer_pending, NT_WNDALERT_0 | NT_NOFOCUS, &self->active_box, self->name, + box_notify(self, transfer_pending, NT_WNDALERT_0 | NT_NOFOCUS, &self->active_box, self->name, "Incoming file: %s", filename ); } @@ -478,7 +477,7 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int32_t num, uint8_t rec snprintf(msg, sizeof(msg), "File transfer for '%s' failed.", filename); if (self->active_box != -1) - box_notify2(self, error, NT_NOFOCUS | NT_WNDALERT_2, + box_notify2(self, error, NT_NOFOCUS | NT_WNDALERT_2, self->active_box, "File transfer for '%s' failed!", filename ); else box_notify(self, error, NT_NOFOCUS | NT_WNDALERT_2, &self->active_box, @@ -507,7 +506,7 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int32_t num, uint8_t rec if (self->active_box != -1) box_notify2(self, transfer_completed, NT_NOFOCUS | NT_WNDALERT_2, self->active_box, "%s", msg); else - box_notify(self, transfer_completed, NT_NOFOCUS | NT_WNDALERT_2, &self->active_box, + box_notify(self, transfer_completed, NT_NOFOCUS | NT_WNDALERT_2, &self->active_box, self->name, "%s", msg); break; @@ -561,40 +560,6 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int32_t num, uint8_t filenu Friends.list[num].file_receiver[filenum].bytes_recv += length; } -static void chat_onGroupInvite(ToxWindow *self, Tox *m, int32_t friendnumber, uint8_t type, const char *group_pub_key, - uint16_t length) -{ - if (self->num != friendnumber) - return; - - if (Friends.list[friendnumber].group_invite.key != NULL) - free(Friends.list[friendnumber].group_invite.key); - - char *k = malloc(length); - - if (k == NULL) - exit_toxic_err("Failed in chat_onGroupInvite", FATALERR_MEMORY); - - memcpy(k, group_pub_key, length); - Friends.list[friendnumber].group_invite.key = k; - Friends.list[friendnumber].group_invite.pending = true; - Friends.list[friendnumber].group_invite.length = length; - Friends.list[friendnumber].group_invite.type = type; - - sound_notify(self, generic_message, NT_WNDALERT_2, NULL); - - char name[TOX_MAX_NAME_LENGTH]; - get_nick_truncate(m, name, friendnumber); - - if (self->active_box != -1) - box_silent_notify2(self, NT_WNDALERT_2 | NT_NOFOCUS, self->active_box, "invites you to join group chat"); - else - box_silent_notify(self, NT_WNDALERT_2 | NT_NOFOCUS, &self->active_box, name, "invites you to join group chat"); - - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s has invited you to a group chat.", name); - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Type \"/join\" to join the chat."); -} - /* Av Stuff */ #ifdef AUDIO @@ -604,14 +569,14 @@ void chat_onInvite (ToxWindow *self, ToxAv *av, int call_index) return; /* call_index is set here and reset on call end */ - + self->call_idx = call_index; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Incoming audio call! Type: \"/answer\" or \"/reject\""); - + if (self->ringing_sound == -1) sound_notify(self, call_incoming, NT_LOOP, &self->ringing_sound); - - + + if (self->active_box != -1) box_silent_notify2(self, NT_NOFOCUS | NT_WNDALERT_0, self->active_box, "Incoming audio call!"); else @@ -624,7 +589,7 @@ void chat_onRinging (ToxWindow *self, ToxAv *av, int call_index) return; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Ringing...type \"/hangup\" to cancel it."); - + #ifdef SOUND_NOTIFY if (self->ringing_sound == -1) sound_notify(self, call_outgoing, NT_LOOP, &self->ringing_sound); @@ -639,7 +604,7 @@ void chat_onStarting (ToxWindow *self, ToxAv *av, int call_index) init_infobox(self); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Call started! Type: \"/hangup\" to end it."); - + #ifdef SOUND_NOTIFY stop_sound(self->ringing_sound); #endif /* SOUND_NOTIFY */ @@ -653,7 +618,7 @@ void chat_onEnding (ToxWindow *self, ToxAv *av, int call_index) kill_infobox(self); self->call_idx = -1; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Call ended!"); - + #ifdef SOUND_NOTIFY stop_sound(self->ringing_sound); #endif /* SOUND_NOTIFY */ @@ -666,7 +631,7 @@ void chat_onError (ToxWindow *self, ToxAv *av, int call_index) self->call_idx = -1; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Error!"); - + #ifdef SOUND_NOTIFY stop_sound(self->ringing_sound); #endif /* SOUND_NOTIFY */ @@ -680,7 +645,7 @@ void chat_onStart (ToxWindow *self, ToxAv *av, int call_index) init_infobox(self); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Call started! Type: \"/hangup\" to end it."); - + #ifdef SOUND_NOTIFY stop_sound(self->ringing_sound); #endif /* SOUND_NOTIFY */ @@ -694,7 +659,7 @@ void chat_onCancel (ToxWindow *self, ToxAv *av, int call_index) kill_infobox(self); self->call_idx = -1; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Call canceled!"); - + #ifdef SOUND_NOTIFY stop_sound(self->ringing_sound); #endif /* SOUND_NOTIFY */ @@ -707,7 +672,7 @@ void chat_onReject (ToxWindow *self, ToxAv *av, int call_index) self->call_idx = -1; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Rejected!"); - + #ifdef SOUND_NOTIFY stop_sound(self->ringing_sound); #endif /* SOUND_NOTIFY */ @@ -721,7 +686,7 @@ void chat_onEnd (ToxWindow *self, ToxAv *av, int call_index) kill_infobox(self); self->call_idx = -1; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Call ended!"); - + #ifdef SOUND_NOTIFY stop_sound(self->ringing_sound); #endif /* SOUND_NOTIFY */ @@ -734,7 +699,7 @@ void chat_onRequestTimeout (ToxWindow *self, ToxAv *av, int call_index) self->call_idx = -1; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No answer!"); - + #ifdef SOUND_NOTIFY stop_sound(self->ringing_sound); #endif /* SOUND_NOTIFY */ @@ -748,7 +713,7 @@ void chat_onPeerTimeout (ToxWindow *self, ToxAv *av, int call_index) kill_infobox(self); self->call_idx = -1; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Peer disconnected; call ended!"); - + #ifdef SOUND_NOTIFY stop_sound(self->ringing_sound); #endif /* SOUND_NOTIFY */ @@ -1140,7 +1105,6 @@ ToxWindow new_chat(Tox *m, int32_t friendnum) ret.onMessage = &chat_onMessage; ret.onConnectionChange = &chat_onConnectionChange; ret.onTypingChange = & chat_onTypingChange; - ret.onGroupInvite = &chat_onGroupInvite; ret.onNickChange = &chat_onNickChange; ret.onStatusChange = &chat_onStatusChange; ret.onStatusMessageChange = &chat_onStatusMessageChange; @@ -1162,14 +1126,14 @@ ToxWindow new_chat(Tox *m, int32_t friendnum) ret.onEnd = &chat_onEnd; ret.onRequestTimeout = &chat_onRequestTimeout; ret.onPeerTimeout = &chat_onPeerTimeout; - + ret.call_idx = -1; ret.device_selection[0] = ret.device_selection[1] = -1; ret.ringing_sound = -1; #endif /* AUDIO */ - + ret.active_box = -1; - + char nick[TOX_MAX_NAME_LENGTH]; int n_len = get_nick_truncate(m, nick, friendnum); set_window_title(&ret, nick, n_len); diff --git a/src/chat_commands.c b/src/chat_commands.c index a510cfa..48de478 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -58,7 +58,7 @@ void cmd_cancelfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*ar if (strcasecmp(inoutstr, "in") == 0) { /* cancel an incoming file transfer */ if (!Friends.list[self->num].file_receiver[filenum].active) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid file ID."); - return; + return; } const char *filepath = Friends.list[self->num].file_receiver[filenum].filename; @@ -80,7 +80,7 @@ void cmd_cancelfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*ar if (!match) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid file ID."); - return; + return; } const char *filename = file_senders[i].filename; @@ -94,67 +94,6 @@ void cmd_cancelfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*ar } } -void cmd_groupinvite(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) -{ - if (argc < 1) { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group number required."); - return; - } - - int groupnum = atoi(argv[1]); - - if (groupnum == 0 && strcmp(argv[1], "0")) { /* atoi returns 0 value on invalid input */ - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid group number."); - return; - } - - if (tox_invite_friend(m, self->num, groupnum) == -1) { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to invite contact to group."); - return; - } - - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invited contact to Group %d.", groupnum); -} - -void cmd_join_group(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) -{ - if (get_num_active_windows() >= MAX_WINDOWS_NUM) { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, " * Warning: Too many windows are open."); - return; - } - - const char *groupkey = Friends.list[self->num].group_invite.key; - uint16_t length = Friends.list[self->num].group_invite.length; - uint8_t type = Friends.list[self->num].group_invite.type; - - if (!Friends.list[self->num].group_invite.pending) { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending group chat invite."); - return; - } - - int groupnum = -1; - - if (type == TOX_GROUPCHAT_TYPE_TEXT) - groupnum = tox_join_groupchat(m, self->num, (uint8_t *) groupkey, length); -#ifdef AUDIO - else - groupnum = toxav_join_av_groupchat(m, self->num, (uint8_t *) groupkey, length, - write_device_callback_group, NULL); -#endif - - if (groupnum == -1) { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat instance failed to initialize."); - return; - } - - if (init_groupchat_win(prompt, m, groupnum, type) == -1) { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat window failed to initialize."); - tox_del_groupchat(m, groupnum); - return; - } - -} - void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { if (argc < 1) { @@ -272,7 +211,7 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv char sizestr[32]; bytes_convert_str(sizestr, sizeof(sizestr), filesize); - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Sending file [%d]: '%s' (%s)", filenum, filename, sizestr); ++num_active_file_senders; diff --git a/src/chat_commands.h b/src/chat_commands.h index c66b77a..9c87ede 100644 --- a/src/chat_commands.h +++ b/src/chat_commands.h @@ -27,8 +27,6 @@ #include "toxic.h" void cmd_cancelfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_groupinvite(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_join_group(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_savefile(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_sendfile(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); diff --git a/src/execute.c b/src/execute.c index 9e83a83..ea64117 100644 --- a/src/execute.c +++ b/src/execute.c @@ -49,6 +49,7 @@ static struct cmd_func global_commands[] = { { "/exit", cmd_quit }, { "/group", cmd_groupchat }, { "/help", cmd_prompt_help }, + { "/join", cmd_join }, { "/log", cmd_log }, { "/myid", cmd_myid }, { "/nick", cmd_nick }, @@ -66,8 +67,6 @@ static struct cmd_func global_commands[] = { static struct cmd_func chat_commands[] = { { "/cancel", cmd_cancelfile }, - { "/invite", cmd_groupinvite }, - { "/join", cmd_join_group }, { "/savefile", cmd_savefile }, { "/sendfile", cmd_sendfile }, #ifdef AUDIO @@ -82,8 +81,8 @@ static struct cmd_func chat_commands[] = { }; static struct cmd_func group_commands[] = { - { "/title", cmd_set_title }, - + { "/topic", cmd_set_topic }, + { "/chatid", cmd_chatid }, #ifdef AUDIO { "/mute", cmd_mute }, { "/sense", cmd_sense }, @@ -177,6 +176,7 @@ void execute(WINDOW *w, ToxWindow *self, Tox *m, const char *input, int mode) case GROUPCHAT_COMMAND_MODE: if (do_command(w, self, m, num_args, group_commands, args) == 0) return; + break; } diff --git a/src/execute.h b/src/execute.h index 03c121b..b755c8b 100644 --- a/src/execute.h +++ b/src/execute.h @@ -32,7 +32,7 @@ enum { GLOBAL_COMMAND_MODE, CHAT_COMMAND_MODE, GROUPCHAT_COMMAND_MODE, -}; +} COMMAND_MODE; void execute(WINDOW *w, ToxWindow *self, Tox *m, const char *input, int mode); diff --git a/src/friendlist.c b/src/friendlist.c index 6ce0cc9..ba925f0 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -112,13 +112,6 @@ static void realloc_blocklist(int n) void kill_friendlist(void) { - int i; - - for (i = 0; i < Friends.max_idx; ++i) { - if (Friends.list[i].active && Friends.list[i].group_invite.key != NULL) - free(Friends.list[i].group_invite.key); - } - realloc_blocklist(0); realloc_friends(0); } @@ -483,25 +476,6 @@ static void friendlist_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, u } } -static void friendlist_onGroupInvite(ToxWindow *self, Tox *m, int32_t num, uint8_t type, const char *group_pub_key, - uint16_t length) -{ - if (num >= Friends.max_idx) - return; - - if (Friends.list[num].chatwin == -1) { - if (get_num_active_windows() < MAX_WINDOWS_NUM) { - Friends.list[num].chatwin = add_window(m, new_chat(m, Friends.list[num].num)); - } else { - char nick[TOX_MAX_NAME_LENGTH]; - get_nick_truncate(m, nick, num); - line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, RED, - "* Group chat invite from %s failed: too many windows are open.", nick); - sound_notify(prompt, error, NT_WNDALERT_1, NULL); - } - } -} - /* move friendlist/blocklist cursor up and down */ static void select_friend(ToxWindow *self, wint_t key, int *selected, int num) { @@ -527,9 +501,6 @@ static void delete_friend(Tox *m, int32_t f_num) } } - if (Friends.list[f_num].group_invite.key != NULL) - free(Friends.list[f_num].group_invite.key); - tox_del_friend(m, f_num); memset(&Friends.list[f_num], 0, sizeof(ToxicFriend)); @@ -1075,7 +1046,6 @@ ToxWindow new_friendlist(void) ret.onStatusChange = &friendlist_onStatusChange; ret.onStatusMessageChange = &friendlist_onStatusMessageChange; ret.onFileSendRequest = &friendlist_onFileSendRequest; - ret.onGroupInvite = &friendlist_onGroupInvite; #ifdef AUDIO ret.onInvite = &friendlist_onAv; diff --git a/src/friendlist.h b/src/friendlist.h index 3beb633..599d4a7 100644 --- a/src/friendlist.h +++ b/src/friendlist.h @@ -48,13 +48,6 @@ struct LastOnline { char hour_min_str[TIME_STR_SIZE]; /* holds 12/24-hour time string e.g. "10:43 PM" */ }; -struct GroupChatInvite { - char *key; - uint16_t length; - uint8_t type; - bool pending; -}; - typedef struct { char name[TOXIC_MAX_NAME_LENGTH + 1]; int namelength; @@ -70,7 +63,6 @@ typedef struct { uint8_t status; struct LastOnline last_online; struct FileReceiver file_receiver[MAX_FILES]; - struct GroupChatInvite group_invite; uint8_t active_file_receivers; } ToxicFriend; diff --git a/src/global_commands.c b/src/global_commands.c index b2321b9..a513115 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -179,7 +179,7 @@ void cmd_add(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX } cmd_add_helper(self, m, id_bin, msg); - } else { /* assume id is a username@domain address and do DNS lookup */ + } else { /* assume id is a username@domain address */ dns3_lookup(self, m, id_bin, id, msg); } } @@ -326,42 +326,90 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg } if (argc < 1) { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Please specify group type: text | audio"); + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Error: Please specify the group name."); return; } - uint8_t type; + const char *tmp_name = argv[1]; + int len = strlen(tmp_name); - if (!strcasecmp(argv[1], "audio")) - type = TOX_GROUPCHAT_TYPE_AV; - else if (!strcasecmp(argv[1], "text")) - type = TOX_GROUPCHAT_TYPE_TEXT; - else { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Valid group types are: text | audio"); + if (len == 0 || len > TOX_MAX_GROUP_NAME_LENGTH) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Error: Invalid group name."); return; } - int groupnum = -1; + char name[TOX_MAX_GROUP_NAME_LENGTH]; - if (type == TOX_GROUPCHAT_TYPE_TEXT) - groupnum = tox_add_groupchat(m); -#ifdef AUDIO - else - groupnum = toxav_add_av_groupchat(m, write_device_callback_group, NULL); -#endif + if (argv[1][0] == '\"') { /* remove opening and closing quotes */ + snprintf(name, sizeof(name), "%s", &argv[1][1]); + len -= 2; + name[len] = '\0'; + } else { + snprintf(name, sizeof(name), "%s", argv[1]); + } + + int groupnum = tox_group_new(m, (uint8_t *) name, len); if (groupnum == -1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat instance failed to initialize."); return; } - if (init_groupchat_win(prompt, m, groupnum, type) == -1) { + if (init_groupchat_win(prompt, m, groupnum, name, len) == -1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat window failed to initialize."); - tox_del_groupchat(m, groupnum); + tox_group_delete(m, groupnum, NULL, 0); + return; + } +} + +void cmd_join(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) +{ + if (get_num_active_windows() >= MAX_WINDOWS_NUM) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, " * Warning: Too many windows are open."); return; } - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat [%d] created.", groupnum); + if (argc < 1) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Error: Group chat ID is required."); + return; + } + + const char *chat_id = argv[1]; + + if (strlen(chat_id) != TOX_GROUP_CHAT_ID_SIZE * 2) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Error: Invalid chat ID"); + return; + } + + char id_bin[TOX_GROUP_CHAT_ID_SIZE] = {0}; + + size_t i; + char xch[3]; + uint32_t x; + + for (i = 0; i < TOX_GROUP_CHAT_ID_SIZE; ++i) { + xch[0] = chat_id[2 * i]; + xch[1] = chat_id[2 * i + 1]; + xch[2] = '\0'; + + if (sscanf(xch, "%02x", &x) != 1) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid chat ID."); + return; + } + + id_bin[i] = x; + } + + if (tox_group_new_join(m, (uint8_t *) id_bin) == -1) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat instance failed to initialize."); + return; + } + + if (init_groupchat_win(prompt, m, self->num, NULL, 0) == -1) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat window failed to initialize."); + tox_group_delete(m, self->num, NULL, 0); + return; + } } void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) @@ -524,7 +572,7 @@ void cmd_requests(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv } void cmd_status(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) -{ +{ bool have_note = false; const char *errmsg; diff --git a/src/global_commands.h b/src/global_commands.h index 9d3f34b..6f2ff03 100644 --- a/src/global_commands.h +++ b/src/global_commands.h @@ -33,6 +33,7 @@ void cmd_clear(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE void cmd_connect(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_decline(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_groupchat(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_join(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_log(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_myid(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_nick(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); diff --git a/src/group_commands.c b/src/group_commands.c index cc8374a..8f54c2a 100644 --- a/src/group_commands.c +++ b/src/group_commands.c @@ -28,50 +28,65 @@ #include "misc_tools.h" #include "log.h" -void cmd_set_title(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_set_topic(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { - char title[MAX_STR_SIZE]; + char topic[MAX_STR_SIZE]; if (argc < 1) { - int tlen = tox_group_get_title(m, self->num, (uint8_t *) title, TOX_MAX_NAME_LENGTH); + int tlen = tox_group_get_topic(m, self->num, (uint8_t *) topic); - if (tlen != -1) { - title[tlen] = '\0'; - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Title is set to: %s", title); + if (tlen > 0) { + topic[tlen] = '\0'; + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Topic is set to: %s", topic); } else { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Title is not set"); + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Topic is not set"); } return; } if (argv[1][0] != '\"') { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Title must be enclosed in quotes."); + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Topic must be enclosed in quotes."); return; } /* remove opening and closing quotes */ - snprintf(title, sizeof(title), "%s", &argv[1][1]); - int len = strlen(title) - 1; - title[len] = '\0'; + snprintf(topic, sizeof(topic), "%s", &argv[1][1]); + int len = strlen(topic) - 1; + topic[len] = '\0'; - if (tox_group_set_title(m, self->num, (uint8_t *) title, len) != 0) { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to set title."); + if (tox_group_set_topic(m, self->num, (uint8_t *) topic, len) != 0) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to set topic."); return; } - set_window_title(self, title, len); - char timefrmt[TIME_STR_SIZE]; char selfnick[TOX_MAX_NAME_LENGTH]; get_time_str(timefrmt, sizeof(timefrmt)); - uint16_t sn_len = tox_get_self_name(m, (uint8_t *) selfnick); + int sn_len = tox_group_get_self_name(m, self->num, (uint8_t *) selfnick); selfnick[sn_len] = '\0'; - line_info_add(self, timefrmt, selfnick, NULL, NAME_CHANGE, 0, 0, " set the group title to: %s", title); + line_info_add(self, timefrmt, selfnick, NULL, NAME_CHANGE, 0, 0, " set the group topic to: %s", topic); char tmp_event[MAX_STR_SIZE]; - snprintf(tmp_event, sizeof(tmp_event), "set title to %s", title); + snprintf(tmp_event, sizeof(tmp_event), "set topic to %s", topic); write_to_log(tmp_event, selfnick, self->chatwin->log, true); } + +void cmd_chatid(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) +{ + char chatid[TOX_GROUP_CHAT_ID_SIZE * 2 + 1] = {0}; + char chat_public_key[TOX_GROUP_CHAT_ID_SIZE]; + tox_group_get_invite_key(m, self->num, (uint8_t *) chat_public_key); + + size_t i; + + for (i = 0; i < TOX_GROUP_CHAT_ID_SIZE; ++i) { + char xx[3]; + snprintf(xx, sizeof(xx), "%02X", chat_public_key[i] & 0xff); + strcat(chatid, xx); + } + + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", chatid); +} diff --git a/src/group_commands.h b/src/group_commands.h index 5d9a1b6..17c3eeb 100644 --- a/src/group_commands.h +++ b/src/group_commands.h @@ -26,6 +26,7 @@ #include "windows.h" #include "toxic.h" -void cmd_set_title(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_set_topic(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_chatid(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]); #endif /* GROUP_COMMANDS_H */ diff --git a/src/groupchat.c b/src/groupchat.c index e568947..80a5988 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -70,16 +70,17 @@ extern struct user_settings *user_settings; extern struct Winthread Winthread; #ifdef AUDIO -#define AC_NUM_GROUP_COMMANDS 22 +#define AC_NUM_GROUP_COMMANDS 24 #else -#define AC_NUM_GROUP_COMMANDS 18 +#define AC_NUM_GROUP_COMMANDS 20 #endif /* AUDIO */ -/* Array of groupchat command names used for tab completion. */ +/* groupchat command names used for tab completion. */ static const char group_cmd_list[AC_NUM_GROUP_COMMANDS][MAX_CMDNAME_SIZE] = { { "/accept" }, { "/add" }, { "/avatar" }, + { "/chatid" }, { "/clear" }, { "/close" }, { "/connect" }, @@ -87,6 +88,7 @@ static const char group_cmd_list[AC_NUM_GROUP_COMMANDS][MAX_CMDNAME_SIZE] = { { "/exit" }, { "/group" }, { "/help" }, + { "/join" }, { "/log" }, { "/myid" }, { "/nick" }, @@ -94,7 +96,7 @@ static const char group_cmd_list[AC_NUM_GROUP_COMMANDS][MAX_CMDNAME_SIZE] = { { "/quit" }, { "/requests" }, { "/status" }, - { "/title" }, + { "/topic" }, #ifdef AUDIO @@ -106,45 +108,32 @@ static const char group_cmd_list[AC_NUM_GROUP_COMMANDS][MAX_CMDNAME_SIZE] = { #endif /* AUDIO */ }; +ToxWindow new_group_chat(Tox *m, int groupnum, const char *groupname, int length); + #ifdef AUDIO static int group_audio_open_out_device(int groupnum); static int group_audio_close_out_device(int groupnum); #endif /* AUDIO */ -int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum, uint8_t type) +int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum, const char *groupname, int length) { if (groupnum > MAX_GROUPCHAT_NUM) return -1; - ToxWindow self = new_group_chat(m, groupnum); + ToxWindow self = new_group_chat(m, groupnum, groupname, length); + int i; for (i = 0; i <= max_groupchat_index; ++i) { if (!groupchats[i].active) { groupchats[i].chatwin = add_window(m, self); groupchats[i].active = true; - groupchats[i].num_peers = 0; - groupchats[i].type = type; - groupchats[i].start_time = get_unix_time(); - groupchats[i].peer_names = malloc(sizeof(uint8_t) * TOX_MAX_NAME_LENGTH); - groupchats[i].oldpeer_names = malloc(sizeof(uint8_t) * TOX_MAX_NAME_LENGTH); - groupchats[i].peer_name_lengths = malloc(sizeof(uint16_t)); - groupchats[i].oldpeer_name_lengths = malloc(sizeof(uint16_t)); + groupchats[i].peer_name_lengths = malloc(sizeof(uint32_t)); - if (groupchats[i].peer_names == NULL || groupchats[i].oldpeer_names == NULL - || groupchats[i].peer_name_lengths == NULL || groupchats[i].oldpeer_name_lengths == NULL) + if (groupchats[i].peer_names == NULL || groupchats[i].peer_name_lengths == NULL) exit_toxic_err("failed in init_groupchat_win", FATALERR_MEMORY); - memcpy(&groupchats[i].oldpeer_names[0], UNKNOWN_NAME, sizeof(UNKNOWN_NAME)); - groupchats[i].oldpeer_name_lengths[0] = (uint16_t) strlen(UNKNOWN_NAME); - -#ifdef AUDIO - if (type == TOX_GROUPCHAT_TYPE_AV) - if (group_audio_open_out_device(i) == -1) - fprintf(stderr, "Group Audio failed to init\n"); -#endif /* AUDIO */ - set_active_window(groupchats[i].chatwin); if (i == max_groupchat_index) @@ -172,17 +161,16 @@ static void kill_groupchat_window(ToxWindow *self) del_window(self); } -void close_groupchat(ToxWindow *self, Tox *m, int groupnum) +void close_groupchat(ToxWindow *self, Tox *m, int groupnum, const char *partmessage, int length) { - tox_del_groupchat(m, groupnum); + tox_group_delete(m, groupnum, (const uint8_t *) partmessage, (uint16_t) length); + #ifdef AUDIO group_audio_close_out_device(groupnum); #endif free(groupchats[groupnum].peer_names); - free(groupchats[groupnum].oldpeer_names); free(groupchats[groupnum].peer_name_lengths); - free(groupchats[groupnum].oldpeer_name_lengths); memset(&groupchats[groupnum], 0, sizeof(GroupChat)); int i; @@ -244,15 +232,15 @@ static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int get_group_nick_truncate(m, nick, peernum, groupnum); char selfnick[TOX_MAX_NAME_LENGTH]; - uint16_t sn_len = tox_get_self_name(m, (uint8_t *) selfnick); + uint16_t sn_len = tox_group_get_self_name(m, groupnum, (uint8_t *) selfnick); selfnick[sn_len] = '\0'; - int nick_clr = strcmp(nick, selfnick) == 0 ? GREEN : CYAN; + int nick_clr = CYAN; /* Only play sound if mentioned by someone else */ if (strcasestr(msg, selfnick) && strcmp(selfnick, nick)) { sound_notify(self, generic_message, NT_WNDALERT_0, NULL); - + if (self->active_box != -1) box_silent_notify2(self, NT_NOFOCUS, self->active_box, "%s %s", nick, msg); else @@ -283,12 +271,12 @@ static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int p get_group_nick_truncate(m, nick, peernum, groupnum); char selfnick[TOX_MAX_NAME_LENGTH]; - uint16_t n_len = tox_get_self_name(m, (uint8_t *) selfnick); + uint16_t n_len = tox_group_get_self_name(m, groupnum, (uint8_t *) selfnick); selfnick[n_len] = '\0'; if (strcasestr(action, selfnick)) { sound_notify(self, generic_message, NT_WNDALERT_0, NULL); - + if (self->active_box != -1) box_silent_notify2(self, NT_NOFOCUS, self->active_box, "* %s %s", nick, action ); else @@ -305,52 +293,61 @@ static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int p write_to_log(action, nick, ctx->log, true); } -static void groupchat_onGroupTitleChange(ToxWindow *self, Tox *m, int groupnum, int peernum, const char *title, - uint8_t length) +static void groupchat_onGroupPrivateMessage(ToxWindow *self, Tox *m, int groupnum, uint32_t peernum, + const char *action, uint16_t len) +{ + if (self->num != groupnum) + return; + + ChatContext *ctx = self->chatwin; + + char nick[TOX_MAX_NAME_LENGTH]; + get_group_nick_truncate(m, nick, peernum, groupnum); + + char timefrmt[TIME_STR_SIZE]; + get_time_str(timefrmt, sizeof(timefrmt)); + + line_info_add(self, timefrmt, nick, NULL, IN_MSG, 0, RED, "%s", action); + write_to_log(action, nick, ctx->log, false); + sound_notify(self, silent, NT_WNDALERT_1, NULL); +} + +static void groupchat_onGroupTopicChange(ToxWindow *self, Tox *m, int groupnum, uint32_t peernum, + const char *topic, uint16_t length) { ChatContext *ctx = self->chatwin; if (self->num != groupnum) return; - set_window_title(self, title, length); - char timefrmt[TIME_STR_SIZE]; get_time_str(timefrmt, sizeof(timefrmt)); - /* don't announce title when we join the room */ - if (!timed_out(groupchats[self->num].start_time, get_unix_time(), GROUP_EVENT_WAIT)) - return; - char nick[TOX_MAX_NAME_LENGTH]; get_group_nick_truncate(m, nick, peernum, groupnum); - line_info_add(self, timefrmt, nick, NULL, NAME_CHANGE, 0, 0, " set the group title to: %s", title); + line_info_add(self, timefrmt, nick, NULL, NAME_CHANGE, 0, 0, " set the group topic to: %s", topic); char tmp_event[MAX_STR_SIZE]; - snprintf(tmp_event, sizeof(tmp_event), "set title to %s", title); + snprintf(tmp_event, sizeof(tmp_event), "set topic to %s", topic); write_to_log(tmp_event, nick, ctx->log, true); } -/* Puts two copies of peerlist/lengths in chat instance */ -static void copy_peernames(int gnum, uint8_t peerlist[][TOX_MAX_NAME_LENGTH], uint16_t lengths[], int npeers) +/* Copies peer names/lengths */ +static void copy_peernames(int gnum, uint8_t peerlist[][TOX_MAX_NAME_LENGTH], uint32_t lengths[], int npeers) { - /* Assumes these are initiated in init_groupchat_win */ free(groupchats[gnum].peer_names); - free(groupchats[gnum].oldpeer_names); free(groupchats[gnum].peer_name_lengths); - free(groupchats[gnum].oldpeer_name_lengths); int N = TOX_MAX_NAME_LENGTH; - groupchats[gnum].peer_names = malloc(sizeof(uint8_t) * npeers * N); - groupchats[gnum].oldpeer_names = malloc(sizeof(uint8_t) * npeers * N); - groupchats[gnum].peer_name_lengths = malloc(sizeof(uint16_t) * npeers); - groupchats[gnum].oldpeer_name_lengths = malloc(sizeof(uint16_t) * npeers); + groupchats[gnum].peer_names = calloc(1, sizeof(uint8_t) * npeers * N); + groupchats[gnum].peer_name_lengths = calloc(1, sizeof(uint32_t) * npeers); - if (groupchats[gnum].peer_names == NULL || groupchats[gnum].oldpeer_names == NULL - || groupchats[gnum].peer_name_lengths == NULL || groupchats[gnum].oldpeer_name_lengths == NULL) { + if (groupchats[gnum].peer_names == NULL || groupchats[gnum].peer_name_lengths == NULL) exit_toxic_err("failed in copy_peernames", FATALERR_MEMORY); - } + + if (npeers == 0) + return; uint16_t u_len = strlen(UNKNOWN_NAME); int i; @@ -366,184 +363,199 @@ static void copy_peernames(int gnum, uint8_t peerlist[][TOX_MAX_NAME_LENGTH], ui groupchats[gnum].peer_names[i * N + n_len] = '\0'; groupchats[gnum].peer_name_lengths[i] = n_len; filter_str((char *) &groupchats[gnum].peer_names[i * N], n_len); - } - } - - memcpy(groupchats[gnum].oldpeer_names, groupchats[gnum].peer_names, N * npeers); - memcpy(groupchats[gnum].oldpeer_name_lengths, groupchats[gnum].peer_name_lengths, sizeof(uint16_t) * npeers); -} - -struct group_add_thrd { - Tox *m; - ToxWindow *self; - int peernum; - int groupnum; - uint64_t timestamp; - pthread_t tid; - pthread_attr_t attr; -}; - -/* Waits GROUP_EVENT_WAIT seconds for a new peer to set their name before announcing them */ -void *group_add_wait(void *data) -{ - struct group_add_thrd *thrd = (struct group_add_thrd *) data; - ToxWindow *self = thrd->self; - Tox *m = thrd->m; - char peername[TOX_MAX_NAME_LENGTH]; - - /* keep polling for a name that differs from the default until we run out of time */ - while (true) { - usleep(100000); - - pthread_mutex_lock(&Winthread.lock); - get_group_nick_truncate(m, peername, thrd->peernum, thrd->groupnum); - - if (strcmp(peername, DEFAULT_TOX_NAME) || timed_out(thrd->timestamp, get_unix_time(), GROUP_EVENT_WAIT)) { - pthread_mutex_unlock(&Winthread.lock); - break; } - - pthread_mutex_unlock(&Winthread.lock); } - - const char *event = "has joined the room"; - char timefrmt[TIME_STR_SIZE]; - get_time_str(timefrmt, sizeof(timefrmt)); - - pthread_mutex_lock(&Winthread.lock); - line_info_add(self, timefrmt, (char *) peername, NULL, CONNECTION, 0, GREEN, event); - write_to_log(event, (char *) peername, self->chatwin->log, true); - pthread_mutex_unlock(&Winthread.lock); - - pthread_attr_destroy(&thrd->attr); - free(thrd); - pthread_exit(NULL); } -static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnum, int peernum, uint8_t change) +static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnum) { if (self->num != groupnum) return; - if (groupnum > max_groupchat_index) - return; + int num_peers = tox_group_get_number_peers(m, groupnum); + groupchats[groupnum].num_peers = num_peers; - groupchats[groupnum].num_peers = tox_group_number_peers(m, groupnum); - int num_peers = groupchats[groupnum].num_peers; - - if (peernum > num_peers) - return; - - /* get old peer name before updating name list */ - uint8_t oldpeername[TOX_MAX_NAME_LENGTH]; - - if (change != TOX_CHAT_CHANGE_PEER_ADD) { - memcpy(oldpeername, &groupchats[groupnum].oldpeer_names[peernum * TOX_MAX_NAME_LENGTH], - sizeof(oldpeername)); - uint16_t old_n_len = groupchats[groupnum].oldpeer_name_lengths[peernum]; - oldpeername[old_n_len] = '\0'; - } - - /* Update name/len lists */ uint8_t tmp_peerlist[num_peers][TOX_MAX_NAME_LENGTH]; - uint16_t tmp_peerlens[num_peers]; + uint32_t tmp_peerlens[num_peers]; - if (tox_group_get_names(m, groupnum, tmp_peerlist, tmp_peerlens, num_peers) == -1) { - memset(tmp_peerlist, 0, sizeof(tmp_peerlist)); - memset(tmp_peerlens, 0, sizeof(tmp_peerlens)); - } + if (tox_group_get_names(m, groupnum, tmp_peerlist, tmp_peerlens, num_peers) == -1) + return; copy_peernames(groupnum, tmp_peerlist, tmp_peerlens, num_peers); +} - /* get current peername then sort namelist */ - uint8_t peername[TOX_MAX_NAME_LENGTH]; +static void groupchat_onGroupPeerJoin(ToxWindow *self, Tox *m, int groupnum, uint32_t peernum) +{ + if (groupnum != self->num) + return; - if (change != TOX_CHAT_CHANGE_PEER_DEL) { - uint16_t n_len = groupchats[groupnum].peer_name_lengths[peernum]; - memcpy(peername, &groupchats[groupnum].peer_names[peernum * TOX_MAX_NAME_LENGTH], sizeof(peername)); - peername[n_len] = '\0'; - } + if (peernum > groupchats[groupnum].num_peers) + return; - qsort(groupchats[groupnum].peer_names, groupchats[groupnum].num_peers, TOX_MAX_NAME_LENGTH, qsort_strcasecmp_hlpr); + char name[TOX_MAX_NAME_LENGTH]; + get_group_nick_truncate(m, name, peernum, groupnum); - ChatContext *ctx = self->chatwin; - - const char *event; char timefrmt[TIME_STR_SIZE]; get_time_str(timefrmt, sizeof(timefrmt)); - switch (change) { - case TOX_CHAT_CHANGE_PEER_ADD: - if (!timed_out(groupchats[groupnum].start_time, get_unix_time(), GROUP_EVENT_WAIT)) - break; + line_info_add(self, timefrmt, name, NULL, CONNECTION, 0, GREEN, "has joined the room"); - struct group_add_thrd *thrd = malloc(sizeof(struct group_add_thrd)); - thrd->m = m; - thrd->peernum = peernum; - thrd->groupnum = groupnum; - thrd->self = self; - thrd->timestamp = get_unix_time(); - - if (pthread_attr_init(&thrd->attr) != 0) { - free(thrd); - return; - } - - if (pthread_attr_setdetachstate(&thrd->attr, PTHREAD_CREATE_DETACHED) != 0) { - pthread_attr_destroy(&thrd->attr); - free(thrd); - return; - } - - if (pthread_create(&thrd->tid, &thrd->attr, group_add_wait, (void *) thrd) != 0) { - pthread_attr_destroy(&thrd->attr); - free(thrd); - return; - } - - break; - - case TOX_CHAT_CHANGE_PEER_DEL: - event = "has left the room"; - line_info_add(self, timefrmt, (char *) oldpeername, NULL, CONNECTION, 0, RED, event); - - if (groupchats[self->num].side_pos > 0) - --groupchats[self->num].side_pos; - - write_to_log(event, (char *) oldpeername, ctx->log, true); - break; - - case TOX_CHAT_CHANGE_PEER_NAME: - if (!timed_out(groupchats[self->num].start_time, get_unix_time(), GROUP_EVENT_WAIT)) - return; - - /* ignore initial name change (TODO: this is a bad way to do this) */ - if (strcmp((char *) oldpeername, DEFAULT_TOX_NAME) == 0) - return; - - event = " is now known as "; - line_info_add(self, timefrmt, (char *) oldpeername, (char *) peername, NAME_CHANGE, 0, 0, event); - - char tmp_event[TOXIC_MAX_NAME_LENGTH * 2 + 32]; - snprintf(tmp_event, sizeof(tmp_event), "is now known as %s", (char *) peername); - write_to_log(tmp_event, (char *) oldpeername, ctx->log, true); - break; - } + char log_str[TOXIC_MAX_NAME_LENGTH + 32]; + snprintf(log_str, sizeof(log_str), "%s has joined the room", name); + write_to_log(log_str, name, self->chatwin->log, true); sound_notify(self, silent, NT_WNDALERT_2, NULL); } -static void send_group_action(ToxWindow *self, ChatContext *ctx, Tox *m, char *action) +static void groupchat_onGroupPeerExit(ToxWindow *self, Tox *m, int groupnum, uint32_t peernum, + const char *partmessage, uint16_t len) { + if (groupnum != self->num) + return; + + if (peernum > groupchats[groupnum].num_peers) + return; + + char name[TOX_MAX_NAME_LENGTH]; + get_group_nick_truncate(m, name, peernum, groupnum); + + char timefrmt[TIME_STR_SIZE]; + get_time_str(timefrmt, sizeof(timefrmt)); + + line_info_add(self, timefrmt, name, NULL, CONNECTION, 0, GREEN, "has left the room (%s)", partmessage); + + char log_str[TOXIC_MAX_NAME_LENGTH + MAX_STR_SIZE]; + snprintf(log_str, sizeof(log_str), "%s has left the room (%s)", name, partmessage); + + write_to_log(log_str, name, self->chatwin->log, true); + sound_notify(self, silent, NT_WNDALERT_2, NULL); +} + +static void groupchat_onGroupSelfJoin(ToxWindow *self, Tox *m, int groupnum) +{ + if (groupnum != self->num) + return; + + char groupname[TOX_MAX_GROUP_NAME_LENGTH]; + int len = tox_group_get_group_name(m, groupnum, (uint8_t *) groupname); + + if (len > 0) + set_window_title(self, groupname, len); + + char topic[TOX_MAX_GROUP_TOPIC_LENGTH]; + tox_group_get_topic(m, groupnum, (uint8_t *) topic); + + char timefrmt[TIME_STR_SIZE]; + get_time_str(timefrmt, sizeof(timefrmt)); + + line_info_add(self, timefrmt, NULL, NULL, SYS_MSG, 0, 0, "Connected."); + line_info_add(self, timefrmt, NULL, NULL, SYS_MSG, 0, 0, "Topic set to: %s", topic); +} + +static void groupchat_onGroupSelfTimeout(ToxWindow *self, Tox *m, int groupnum) +{ + if (groupnum != self->num) + return; + + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Disconnected from group. Attempting to reconnect..."); +} + +static void groupchat_onGroupOpCertificate(ToxWindow *self, Tox *m, int groupnum, uint32_t src_peernum, + uint32_t tgt_peernum, uint8_t type) +{ + if (groupnum != self->num) + return; + + char src_name[TOX_MAX_NAME_LENGTH]; + get_group_nick_truncate(m, src_name, src_peernum, groupnum); + + char tgt_name[TOX_MAX_NAME_LENGTH]; + get_group_nick_truncate(m, tgt_name, tgt_peernum, groupnum); + + const char *msg = NULL; + + switch (type) { + case TOX_GC_BAN: + msg = "has banned"; + break; + case TOX_GC_PROMOTE_OP: + msg = "has given operator status to"; + break; + case TOX_GC_REVOKE_OP: + msg = "has removed operator status from"; + break; + case TOX_GC_SILENCE: + msg = "has silenced"; + break; + default: + return; + } + + char timefrmt[TIME_STR_SIZE]; + get_time_str(timefrmt, sizeof(timefrmt)); + line_info_add(self, timefrmt, src_name, tgt_name, NAME_CHANGE, 0, MAGENTA, "%s", msg); +} + +static void groupchat_onGroupNickChange(ToxWindow *self, Tox *m, int groupnum, uint32_t peernum, + const char *newnick, uint16_t len) +{ + if (groupnum != self->num) + return; + + char oldnick[TOX_MAX_NAME_LENGTH]; + get_group_nick_truncate(m, oldnick, peernum, groupnum); + + char timefrmt[TIME_STR_SIZE]; + get_time_str(timefrmt, sizeof(timefrmt)); + line_info_add(self, timefrmt, oldnick, (char *) newnick, NAME_CHANGE, 0, MAGENTA, "is now known as"); +} + + +static void send_group_message(ToxWindow *self, Tox *m, int groupnum, const char *msg) +{ + ChatContext *ctx = self->chatwin; + + if (tox_group_message_send(m, self->num, (uint8_t *) msg, strlen(msg)) == -1) { + const char *errmsg = " * Failed to send message."; + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, errmsg); + return; + } + + char selfname[TOX_MAX_NAME_LENGTH]; + uint16_t len = tox_group_get_self_name(m, groupnum, (uint8_t *) selfname); + selfname[len] = '\0'; + + char timefrmt[TIME_STR_SIZE]; + get_time_str(timefrmt, sizeof(timefrmt)); + + line_info_add(self, timefrmt, selfname, NULL, OUT_MSG_READ, 0, 0, msg); + write_to_log(msg, selfname, ctx->log, false); +} + +static void send_group_action(ToxWindow *self, Tox *m, int groupnum, char *action) +{ + ChatContext *ctx = self->chatwin; + if (action == NULL) { wprintw(ctx->history, "Invalid syntax.\n"); return; } - if (tox_group_action_send(m, self->num, (uint8_t *) action, strlen(action)) == -1) { + if (tox_group_action_send(m, groupnum, (uint8_t *) action, strlen(action)) == -1) { const char *errmsg = " * Failed to send action."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, errmsg); + return; } + + char selfname[TOX_MAX_NAME_LENGTH]; + uint16_t len = tox_group_get_self_name(m, groupnum, (uint8_t *) selfname); + selfname[len] = '\0'; + + char timefrmt[TIME_STR_SIZE]; + get_time_str(timefrmt, sizeof(timefrmt)); + + line_info_add(self, timefrmt, selfname, NULL, OUT_ACTION_READ, 0, 0, action); + write_to_log(action, selfname, ctx->log, true); } static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr) @@ -579,7 +591,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr) /* TODO: make this not suck */ if (ctx->line[0] != L'/' || wcscmp(ctx->line, L"/me") == 0) { - diff = complete_line(self, groupchats[self->num].peer_names, groupchats[self->num].num_peers, + diff = complete_line(self, groupchats[self->num].peer_names, groupchats[self->num].num_peers, TOX_MAX_NAME_LENGTH); } else if (wcsncmp(ctx->line, L"/avatar \"", wcslen(L"/avatar \"")) == 0) { diff = dir_match(self, m, ctx->line, L"/avatar"); @@ -618,19 +630,16 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr) add_line_to_hist(ctx); if (line[0] == '/') { - if (strcmp(line, "/close") == 0) { - close_groupchat(self, m, self->num); + if (strncmp(line, "/close", 6) == 0) { + close_groupchat(self, m, self->num, line + 6, ctx->len - 6); return; } else if (strncmp(line, "/me ", strlen("/me ")) == 0) { - send_group_action(self, ctx, m, line + strlen("/me ")); + send_group_action(self, m, self->num, line + strlen("/me ")); } else { execute(ctx->history, self, m, line, GROUPCHAT_COMMAND_MODE); } } else if (!string_is_empty(line)) { - if (tox_group_message_send(m, self->num, (uint8_t *) line, strlen(line)) == -1) { - const char *errmsg = " * Failed to send message."; - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, errmsg); - } + send_group_message(self, m, self->num, line); } wclear(ctx->linewin); @@ -727,13 +736,21 @@ static void groupchat_onInit(ToxWindow *self, Tox *m) execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE); + char selfname[TOX_MAX_NAME_LENGTH]; + uint16_t len = tox_get_self_name(m, (uint8_t *) selfname); + selfname[len] = '\0'; + tox_group_set_name(m, self->num, (uint8_t *) selfname, len); + + uint8_t status = tox_get_self_user_status(m); + tox_group_set_status(m, self->num, status); + scrollok(ctx->history, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0); } #ifdef AUDIO -static int group_audio_open_out_device(int groupnum) +static int group_audio_open_out_device(int groupnum) { char dname[MAX_STR_SIZE]; get_primary_device_name(output, dname, sizeof(dname)); @@ -821,13 +838,13 @@ static int group_audio_write(int peernum, int groupnum, const int16_t *pcm, unsi ALint state; alGetSourcei(groupchats[groupnum].audio.source, AL_SOURCE_STATE, &state); - if (state != AL_PLAYING) + if (state != AL_PLAYING) alSourcePlay(groupchats[groupnum].audio.source); return 0; } -static void groupchat_onWriteDevice(ToxWindow *self, Tox *m, int groupnum, int peernum, const int16_t *pcm, +static void groupchat_onWriteDevice(ToxWindow *self, Tox *m, int groupnum, int peernum, const int16_t *pcm, unsigned int samples, uint8_t channels, unsigned int sample_rate) { return; @@ -849,7 +866,7 @@ static void groupchat_onWriteDevice(ToxWindow *self, Tox *m, int groupnum, int p } #endif /* AUDIO */ -ToxWindow new_group_chat(Tox *m, int groupnum) +ToxWindow new_group_chat(Tox *m, int groupnum, const char *groupname, int length) { ToxWindow ret; memset(&ret, 0, sizeof(ret)); @@ -862,14 +879,24 @@ ToxWindow new_group_chat(Tox *m, int groupnum) ret.onInit = &groupchat_onInit; ret.onGroupMessage = &groupchat_onGroupMessage; ret.onGroupNamelistChange = &groupchat_onGroupNamelistChange; + ret.onGroupPrivateMessage = &groupchat_onGroupPrivateMessage; ret.onGroupAction = &groupchat_onGroupAction; - ret.onGroupTitleChange = &groupchat_onGroupTitleChange; + ret.onGroupPeerJoin = &groupchat_onGroupPeerJoin; + ret.onGroupPeerExit = &groupchat_onGroupPeerExit; + ret.onGroupTopicChange = &groupchat_onGroupTopicChange; + ret.onGroupOpCertificate = &groupchat_onGroupOpCertificate; + ret.onGroupNickChange = &groupchat_onGroupNickChange; + ret.onGroupSelfJoin = &groupchat_onGroupSelfJoin; + ret.onGroupSelfTimeout = &groupchat_onGroupSelfTimeout; #ifdef AUDIO ret.onWriteDevice = &groupchat_onWriteDevice; #endif - snprintf(ret.name, sizeof(ret.name), "Group %d", groupnum); + if (groupname && length) + set_window_title(&ret, groupname, length); + else + snprintf(ret.name, sizeof(ret.name), "Group %d", groupnum); ChatContext *chatwin = calloc(1, sizeof(ChatContext)); Help *help = calloc(1, sizeof(Help)); diff --git a/src/groupchat.h b/src/groupchat.h index 59d59c8..1118e58 100644 --- a/src/groupchat.h +++ b/src/groupchat.h @@ -61,26 +61,19 @@ struct GAudio { typedef struct { int chatwin; bool active; - uint8_t type; int num_peers; int side_pos; /* current position of the sidebar - used for scrolling up and down */ - uint64_t start_time; uint8_t *peer_names; - uint8_t *oldpeer_names; - uint16_t *peer_name_lengths; - uint16_t *oldpeer_name_lengths; - + uint32_t *peer_name_lengths; #ifdef AUDIO struct GAudio audio; #endif } GroupChat; -void close_groupchat(ToxWindow *self, Tox *m, int groupnum); -int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum, uint8_t type); +void close_groupchat(ToxWindow *self, Tox *m, int groupnum, const char *partmessage, int length); +int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum, const char *groupname, int length); /* destroys and re-creates groupchat window with or without the peerlist */ void redraw_groupchat_win(ToxWindow *self); -ToxWindow new_group_chat(Tox *m, int groupnum); - #endif /* #define GROUPCHAT_H */ diff --git a/src/help.c b/src/help.c index 0a97697..3087c35 100644 --- a/src/help.c +++ b/src/help.c @@ -150,9 +150,10 @@ static void help_draw_global(ToxWindow *self) wprintw(win, " /connect : Manually connect to a DHT node\n"); wprintw(win, " /status : Set status with optional note\n"); wprintw(win, " /note : Set a personal note\n"); + wprintw(win, " /group : Create a group chat\n"); + wprintw(win, " /join : Join a group chat\n"); wprintw(win, " /nick : Set your nickname\n"); wprintw(win, " /log or : Enable/disable logging\n"); - wprintw(win, " /group : Create a group chat where type: text | audio\n"); wprintw(win, " /myid : Print your Tox ID\n"); wprintw(win, " /clear : Clear window history\n"); wprintw(win, " /close : Close the current chat window\n"); @@ -183,8 +184,6 @@ static void help_draw_chat(ToxWindow *self) wprintw(win, "Chat Commands:\n"); wattroff(win, A_BOLD | COLOR_PAIR(RED)); - wprintw(win, " /invite : Invite contact to a group chat\n"); - wprintw(win, " /join : Join a pending group chat\n"); wprintw(win, " /sendfile : Send a file\n"); wprintw(win, " /savefile : Receive a file\n"); wprintw(win, " /cancel : Cancel file transfer where type: in|out\n"); @@ -219,7 +218,7 @@ static void help_draw_keys(ToxWindow *self) wprintw(win, "Key bindings:\n"); wattroff(win, A_BOLD | COLOR_PAIR(RED)); - wprintw(win, " Ctrl+O and Ctrl+P : Navigate through the tabs\n"); + wprintw(win, " Ctrl+O and Ctrl+P : Navigate through the tabs\n"); wprintw(win, " Page Up and Page Down : Scroll window history one line\n"); wprintw(win, " Ctrl+F and Ctrl+V : Scroll window history half a page\n"); wprintw(win, " Ctrl+H : Move to the bottom of window history\n"); @@ -243,14 +242,17 @@ static void help_draw_group(ToxWindow *self) wprintw(win, "Group commands:\n"); wattroff(win, A_BOLD | COLOR_PAIR(RED)); - wprintw(win, " /title : Set group title (show current title if no msg)\n\n"); + wprintw(win, " /topic : Set group topic (show current topic if no msg)\n"); + wprintw(win, " /chatid : Print the group chat id to share with others.\n\n"); +#ifdef AUDIO wattron(win, A_BOLD); wprintw(win, " Audio commands:\n"); wattroff(win, A_BOLD); wprintw(win, " /mute : Mute active device where type: in | out\n"); wprintw(win, " /sense : VAD sensitivity threshold\n\n"); +#endif /* AUDIO */ help_draw_bottom_menu(win); @@ -290,28 +292,30 @@ void help_onKey(ToxWindow *self, wint_t key) case 'c': #ifdef AUDIO - help_init_window(self, 19, 80); + help_init_window(self, 18, 80); #else - help_init_window(self, 9, 80); + help_init_window(self, 8, 80); #endif self->help->type = HELP_CHAT; break; case 'g': #ifdef AUDIO - help_init_window(self, 24, 80); + help_init_window(self, 25, 80); #else - help_init_window(self, 20, 80); + help_init_window(self, 21, 80); #endif self->help->type = HELP_GLOBAL; break; -#ifdef AUDIO /* remove if/when we add non-audio group commands */ case 'r': - help_init_window(self, 10, 80); +#ifdef AUDIO + help_init_window(self, 11, 80); +#else + help_init_window(self, 7, 80); +#endif self->help->type = HELP_GROUP; break; -#endif case 'f': help_init_window(self, 10, 80); diff --git a/src/line_info.c b/src/line_info.c index 6c6f872..baca619 100644 --- a/src/line_info.c +++ b/src/line_info.c @@ -222,7 +222,7 @@ void line_info_add(ToxWindow *self, const char *timestr, const char *name1, cons } /* adds a single queue item to hst if possible. only called once per call to line_info_print() */ -static void line_info_check_queue(ToxWindow *self) +static void line_info_check_queue(ToxWindow *self) { struct history *hst = self->chatwin->hst; struct line_info *line = line_info_ret_queue(hst); diff --git a/src/line_info.h b/src/line_info.h index cb91365..c9bdd39 100644 --- a/src/line_info.h +++ b/src/line_info.h @@ -35,7 +35,7 @@ enum { SYS_MSG, IN_MSG, OUT_MSG, - OUT_MSG_READ, /* for sent messages that have received a read reply. don't set this with line_info_add */ + OUT_MSG_READ, /* for sent messages that have received a read reply. */ IN_ACTION, OUT_ACTION, OUT_ACTION_READ, /* same as OUT_MSG_READ but for actions */ diff --git a/src/misc_tools.c b/src/misc_tools.c index 0b977b2..62cdd2c 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -199,9 +199,9 @@ int valid_nick(const char *nick) for (i = 0; nick[i]; ++i) { if ((nick[i] == ' ' && nick[i + 1] == ' ') - || nick[i] == '/' - || nick[i] == '\n' - || nick[i] == '\t' + || nick[i] == '/' + || nick[i] == '\n' + || nick[i] == '\t' || nick[i] == '\v' || nick[i] == '\r') @@ -281,7 +281,7 @@ int get_nick_truncate(Tox *m, char *buf, int friendnum) /* same as get_nick_truncate but for groupchats */ int get_group_nick_truncate(Tox *m, char *buf, int peernum, int groupnum) { - int len = tox_group_peername(m, groupnum, peernum, (uint8_t *) buf); + int len = tox_group_get_peer_name(m, groupnum, peernum, (uint8_t *) buf); if (len == -1) { strcpy(buf, UNKNOWN_NAME); @@ -372,7 +372,7 @@ off_t file_size(const char *path) return st.st_size; } -/* compares the first size bytes of fp to signature. +/* compares the first size bytes of fp to signature. Returns 0 if they are the same, 1 if they differ, and -1 on error. On success this function will seek back to the beginning of fp */ @@ -395,11 +395,7 @@ int check_file_signature(const char *signature, size_t size, FILE *fp) void set_window_title(ToxWindow *self, const char *title, int len) { char cpy[TOXIC_MAX_NAME_LENGTH + 1]; - - if (self->is_groupchat) /* keep groupnumber in title */ - snprintf(cpy, sizeof(cpy), "%d %s", self->num, title); - else - snprintf(cpy, sizeof(cpy), "%s", title); + snprintf(cpy, sizeof(cpy), "%s", title); if (len > MAX_WINDOW_NAME_LENGTH) { strcpy(&cpy[MAX_WINDOW_NAME_LENGTH - 3], "..."); diff --git a/src/toxic.c b/src/toxic.c index 23046b8..f2a0936 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -136,14 +136,14 @@ void exit_toxic_success(Tox *m) tox_kill(m); endwin(); - + #ifdef X11 /* We have to terminate xtra last coz reasons * Please don't call this anywhere else coz trust me */ terminate_xtra(); #endif /* X11 */ - + exit(EXIT_SUCCESS); } @@ -306,15 +306,21 @@ static Tox *init_tox(void) tox_callback_user_status(m, on_statuschange, NULL); tox_callback_status_message(m, on_statusmessagechange, NULL); tox_callback_friend_action(m, on_action, NULL); - tox_callback_group_invite(m, on_groupinvite, NULL); - tox_callback_group_message(m, on_groupmessage, NULL); - tox_callback_group_action(m, on_groupaction, NULL); - tox_callback_group_namelist_change(m, on_group_namelistchange, NULL); - tox_callback_group_title(m, on_group_titlechange, NULL); tox_callback_file_send_request(m, on_file_sendrequest, NULL); tox_callback_file_control(m, on_file_control, NULL); tox_callback_file_data(m, on_file_data, NULL); tox_callback_read_receipt(m, on_read_receipt, NULL); + tox_callback_group_message(m, on_group_message, NULL); + tox_callback_group_action(m, on_group_action, NULL); + tox_callback_group_private_message(m, on_group_private_message, NULL); + tox_callback_group_op_certificate(m, on_group_op_certificate, NULL); + tox_callback_group_peerlist_update(m, on_group_namelistchange, NULL); + tox_callback_group_peer_join(m, on_group_peer_join, NULL); + tox_callback_group_peer_exit(m, on_group_peer_exit, NULL); + tox_callback_group_nick_change(m, on_group_nick_change, NULL); + tox_callback_group_topic_change(m, on_group_topic_change, NULL); + tox_callback_group_self_join(m, on_group_self_join, NULL); + tox_callback_group_self_timeout(m, on_group_self_timeout, NULL); tox_set_name(m, (uint8_t *) "Toxic User", strlen("Toxic User")); @@ -518,7 +524,7 @@ static void first_time_encrypt(const char *msg) system("clear"); printf("%s ", msg); - if (!strcasecmp(ch, "y\n") || !strcasecmp(ch, "n\n") || !strcasecmp(ch, "yes\n") + if (!strcasecmp(ch, "y\n") || !strcasecmp(ch, "n\n") || !strcasecmp(ch, "yes\n") || !strcasecmp(ch, "no\n") || !strcasecmp(ch, "q\n")) break; @@ -1043,37 +1049,37 @@ int main(int argc, char *argv[]) if (init_xtra(DnD_callback) == -1) queue_init_message("X failed to initialize"); #endif - + Tox *m = init_tox(); - + if (m == NULL) - exit_toxic_err("failed in main", FATALERR_NETWORKINIT); - + exit_toxic_err("failed in main", FATALERR_NETWORKINIT); + if (!arg_opts.ignore_data_file) { if (arg_opts.encrypt_data && !datafile_exists) arg_opts.encrypt_data = 0; - + load_data(m, DATA_FILE); - + } - + init_term(); prompt = init_windows(m); prompt_init_statusbar(prompt, m); - + /* thread for ncurses stuff */ if (pthread_mutex_init(&Winthread.lock, NULL) != 0) exit_toxic_err("failed in main", FATALERR_MUTEX_INIT); - + if (pthread_create(&Winthread.tid, NULL, thread_winref, (void *) m) != 0) exit_toxic_err("failed in main", FATALERR_THREAD_CREATE); - + /* thread for message queue */ if (pthread_create(&cqueue_thread.tid, NULL, thread_cqueue, (void *) m) != 0) exit_toxic_err("failed in main", FATALERR_THREAD_CREATE); - + #ifdef AUDIO - + av = init_audio(prompt, m); /* audio thread */ @@ -1088,11 +1094,11 @@ int main(int argc, char *argv[]) queue_init_message("Failed to init audio devices"); #endif /* AUDIO */ - + init_notify(60, 3000); const char *msg; - + if (config_err) { msg = "Unable to determine configuration directory. Defaulting to 'data' for data file..."; queue_init_message("%s", msg); diff --git a/src/toxic.h b/src/toxic.h index 8c38498..5527060 100644 --- a/src/toxic.h +++ b/src/toxic.h @@ -103,11 +103,6 @@ void on_nickchange(Tox *m, int32_t friendnumber, const uint8_t *string, uint16_t void on_statuschange(Tox *m, int32_t friendnumber, uint8_t status, void *userdata); void on_statusmessagechange(Tox *m, int32_t friendnumber, const uint8_t *string, uint16_t length, void *userdata); void on_friendadded(Tox *m, int32_t friendnumber, bool sort); -void on_groupmessage(Tox *m, int groupnumber, int peernumber, const uint8_t *message, uint16_t length, void *userdata); -void on_groupaction(Tox *m, int groupnumber, int peernumber, const uint8_t *action, uint16_t length, void *userdata); -void on_groupinvite(Tox *m, int32_t friendnumber, uint8_t type, const uint8_t *group_pub_key, uint16_t length, void *userdata); -void on_group_namelistchange(Tox *m, int groupnumber, int peernumber, uint8_t change, void *userdata); -void on_group_titlechange(Tox *m, int groupnumber, int peernumber, const uint8_t *title, uint8_t length, void *userdata); void on_file_sendrequest(Tox *m, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, const uint8_t *pathname, uint16_t pathname_length, void *userdata); void on_file_control(Tox *m, int32_t friendnumber, uint8_t receive_send, uint8_t filenumber, uint8_t control_type, @@ -116,6 +111,18 @@ void on_file_data(Tox *m, int32_t friendnumber, uint8_t filenumber, const uint8_ void on_typing_change(Tox *m, int32_t friendnumber, uint8_t is_typing, void *userdata); void on_read_receipt(Tox *m, int32_t, uint32_t, void *userdata); +void on_group_message(Tox *m, int groupnumber, uint32_t peernumber, const uint8_t *message, uint16_t length, void *userdata); +void on_group_action(Tox *m, int groupnumber, uint32_t peernumber, const uint8_t *action, uint16_t length, void *userdata); +void on_group_private_message(Tox *m, int groupnumber, uint32_t peernumber, const uint8_t *message, uint16_t length, void *userdata); +void on_group_namelistchange(Tox *m, int groupnumber, void *userdata); +void on_group_peer_join(Tox *m, int groupnumber, uint32_t peernumber, void *userdata); +void on_group_peer_exit(Tox *m, int groupnumber, uint32_t peernumber, const uint8_t *partmsg, uint16_t length, void *userdata); +void on_group_topic_change(Tox *m, int groupnumber, uint32_t peernumber, const uint8_t *topic, uint16_t length, void *userdata); +void on_group_nick_change(Tox *m, int groupnumber, uint32_t peernumber, const uint8_t *newname, uint16_t length, void *userdata); +void on_group_op_certificate(Tox *m, int groupnumber, uint32_t src_peernum, uint32_t tgt_peernum, uint8_t cert_type, void *userdata); +void on_group_self_join(Tox *m, int groupnumber, void *userdata); +void on_group_self_timeout(Tox *m, int groupnumber, void *userdata); + #ifdef AUDIO void write_device_callback_group(Tox *m, int groupnum, int peernum, const int16_t *pcm, unsigned int samples, uint8_t channels, unsigned int sample_rate, void *arg); diff --git a/src/windows.c b/src/windows.c index 8282840..21c9215 100644 --- a/src/windows.c +++ b/src/windows.c @@ -161,8 +161,8 @@ void on_friendadded(Tox *m, int32_t friendnumber, bool sort) store_data(m, DATA_FILE); } -void on_groupmessage(Tox *m, int groupnumber, int peernumber, const uint8_t *message, uint16_t length, - void *userdata) +void on_group_message(Tox *m, int groupnumber, uint32_t peernumber, const uint8_t *message, uint16_t length, + void *userdata) { char msg[MAX_STR_SIZE + 1]; length = copy_tox_str(msg, sizeof(msg), (const char *) message, length); @@ -175,8 +175,8 @@ void on_groupmessage(Tox *m, int groupnumber, int peernumber, const uint8_t *mes } } -void on_groupaction(Tox *m, int groupnumber, int peernumber, const uint8_t *action, uint16_t length, - void *userdata) +void on_group_action(Tox *m, int groupnumber, uint32_t peernumber, const uint8_t *action, uint16_t length, + void *userdata) { char msg[MAX_STR_SIZE + 1]; length = copy_tox_str(msg, sizeof(msg), (const char *) action, length); @@ -189,38 +189,117 @@ void on_groupaction(Tox *m, int groupnumber, int peernumber, const uint8_t *acti } } -void on_groupinvite(Tox *m, int32_t friendnumber, uint8_t type, const uint8_t *group_pub_key, uint16_t length, - void *userdata) +void on_group_private_message(Tox *m, int groupnumber, uint32_t peernumber, const uint8_t *message, uint16_t length, + void *userdata) { + char msg[MAX_STR_SIZE + 1]; + length = copy_tox_str(msg, sizeof(msg), (const char *) message, length); + int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) { - if (windows[i].onGroupInvite != NULL) - windows[i].onGroupInvite(&windows[i], m, friendnumber, type, (char *) group_pub_key, length); + if (windows[i].onGroupPrivateMessage != NULL) + windows[i].onGroupPrivateMessage(&windows[i], m, groupnumber, peernumber, msg, length); } } -void on_group_namelistchange(Tox *m, int groupnumber, int peernumber, uint8_t change, void *userdata) +void on_group_namelistchange(Tox *m, int groupnumber, void *userdata) { int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i].onGroupNamelistChange != NULL) - windows[i].onGroupNamelistChange(&windows[i], m, groupnumber, peernumber, change); + windows[i].onGroupNamelistChange(&windows[i], m, groupnumber); } } -void on_group_titlechange(Tox *m, int groupnumber, int peernumber, const uint8_t *title, uint8_t length, - void *userdata) +void on_group_peer_join(Tox *m, int groupnumber, uint32_t peernumber, void *userdata) { - char data[MAX_STR_SIZE + 1]; - length = copy_tox_str(data, sizeof(data), (const char *) title, length); + int i; + + for (i = 0; i < MAX_WINDOWS_NUM; ++i) { + if (windows[i].onGroupPeerJoin != NULL) + windows[i].onGroupPeerJoin(&windows[i], m, groupnumber, peernumber); + } +} + +void on_group_peer_exit(Tox *m, int groupnumber, uint32_t peernumber, const uint8_t *partmsg, uint16_t length, + void *userdata) +{ + char msg[MAX_STR_SIZE + 1]; + + if (length == 0 || !partmsg) { + strcpy(msg, "Quit."); + length = 5; + } else { + length = copy_tox_str(msg, sizeof(msg), (const char *) partmsg, length); + } int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) { - if (windows[i].onGroupTitleChange != NULL) - windows[i].onGroupTitleChange(&windows[i], m, groupnumber, peernumber, data, length); + if (windows[i].onGroupPeerExit != NULL) + windows[i].onGroupPeerExit(&windows[i], m, groupnumber, peernumber, msg, length); + } +} + +void on_group_topic_change(Tox *m, int groupnumber, uint32_t peernumber, const uint8_t *topic, uint16_t length, + void *userdata) +{ + char data[MAX_STR_SIZE + 1]; + length = copy_tox_str(data, sizeof(data), (const char *) topic, length); + + int i; + + for (i = 0; i < MAX_WINDOWS_NUM; ++i) { + if (windows[i].onGroupTopicChange != NULL) + windows[i].onGroupTopicChange(&windows[i], m, groupnumber, peernumber, data, length); + } +} + +void on_group_nick_change(Tox *m, int groupnumber, uint32_t peernumber, const uint8_t *newname, uint16_t length, + void *userdata) +{ + char name[TOXIC_MAX_NAME_LENGTH + 1]; + length = copy_tox_str(name, sizeof(name), (const char *) newname, length); + filter_str(name, length); + + int i; + + for (i = 0; i < MAX_WINDOWS_NUM; ++i) { + if (windows[i].onGroupNickChange != NULL) + windows[i].onGroupNickChange(&windows[i], m, groupnumber, peernumber, name, length); + } +} + +void on_group_op_certificate(Tox *m, int groupnumber, uint32_t src_peernum, uint32_t tgt_peernum, uint8_t cert_type, + void *userdata) +{ + int i; + + for (i = 0; i < MAX_WINDOWS_NUM; ++i) { + if (windows[i].onGroupOpCertificate != NULL) + windows[i].onGroupOpCertificate(&windows[i], m, groupnumber, src_peernum, tgt_peernum, cert_type); + } +} + +void on_group_self_join(Tox *m, int groupnumber, void *userdata) +{ + int i; + + for (i = 0; i < MAX_WINDOWS_NUM; ++i) { + if (windows[i].onGroupSelfJoin != NULL) + windows[i].onGroupSelfJoin(&windows[i], m, groupnumber); + } +} + +void on_group_self_timeout(Tox *m, int groupnumber, void *userdata) +{ + int i; + + for (i = 0; i < MAX_WINDOWS_NUM; ++i) { + if (windows[i].onGroupSelfTimeout != NULL) + windows[i].onGroupSelfTimeout(&windows[i], m, groupnumber); } } @@ -270,14 +349,14 @@ void on_read_receipt(Tox *m, int32_t friendnumber, uint32_t receipt, void *userd } #ifdef AUDIO -void write_device_callback_group(Tox *m, int groupnum, int peernum, const int16_t *pcm, unsigned int samples, +void write_device_callback_group(Tox *m, int groupnum, int peernumber, const int16_t *pcm, unsigned int samples, uint8_t channels, unsigned int sample_rate, void *arg) { int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i].onWriteDevice != NULL) - windows[i].onWriteDevice(&windows[i], m, groupnum, peernum, pcm, samples, channels, samples); + windows[i].onWriteDevice(&windows[i], m, groupnum, peernumber, pcm, samples, channels, samples); } } #endif /* AUDIO */ @@ -529,7 +608,7 @@ void draw_active_window(Tox *m) } } -/* refresh inactive windows to prevent scrolling bugs. +/* refresh inactive windows to prevent scrolling bugs. call at least once per second */ void refresh_inactive_windows(void) { @@ -575,7 +654,7 @@ void kill_all_windows(Tox *m) if (windows[i].is_chat) kill_chat_window(&windows[i], m); else if (windows[i].is_groupchat) - close_groupchat(&windows[i], m, i); + close_groupchat(&windows[i], m, i, "Quit.", 5); } kill_prompt_window(prompt); diff --git a/src/windows.h b/src/windows.h index 73f027c..a368b6c 100644 --- a/src/windows.h +++ b/src/windows.h @@ -117,17 +117,24 @@ struct ToxWindow { void(*onStatusChange)(ToxWindow *, Tox *, int32_t, uint8_t); void(*onStatusMessageChange)(ToxWindow *, int32_t, const char *, uint16_t); void(*onAction)(ToxWindow *, Tox *, int32_t, const char *, uint16_t); - void(*onGroupMessage)(ToxWindow *, Tox *, int, int, const char *, uint16_t); - void(*onGroupAction)(ToxWindow *, Tox *, int, int, const char *, uint16_t); - void(*onGroupInvite)(ToxWindow *, Tox *, int32_t, uint8_t, const char *, uint16_t); - void(*onGroupNamelistChange)(ToxWindow *, Tox *, int, int, uint8_t); - void(*onGroupTitleChange)(ToxWindow *, Tox *, int, int, const char *, uint8_t); void(*onFileSendRequest)(ToxWindow *, Tox *, int32_t, uint8_t, uint64_t, const char *, uint16_t); void(*onFileControl)(ToxWindow *, Tox *, int32_t, uint8_t, uint8_t, uint8_t, const char *, uint16_t); void(*onFileData)(ToxWindow *, Tox *, int32_t, uint8_t, const char *, uint16_t); void(*onTypingChange)(ToxWindow *, Tox *, int32_t, uint8_t); void(*onReadReceipt)(ToxWindow *, Tox *, int32_t, uint32_t); + void(*onGroupMessage)(ToxWindow *, Tox *, int, int, const char *, uint16_t); + void(*onGroupAction)(ToxWindow *, Tox *, int, int, const char *, uint16_t); + void(*onGroupPrivateMessage)(ToxWindow *, Tox *, int, uint32_t, const char *, uint16_t); + void(*onGroupOpCertificate)(ToxWindow *, Tox *, int, uint32_t, uint32_t, uint8_t); + void(*onGroupNamelistChange)(ToxWindow *, Tox *, int); + void(*onGroupPeerJoin)(ToxWindow *, Tox *, int, uint32_t); + void(*onGroupPeerExit)(ToxWindow *, Tox *, int, uint32_t, const char *, uint16_t); + void(*onGroupNickChange)(ToxWindow *, Tox *, int, uint32_t, const char *, uint16_t); + void(*onGroupTopicChange)(ToxWindow *, Tox *, int, uint32_t, const char *, uint16_t); + void(*onGroupSelfJoin)(ToxWindow *, Tox *, int); + void(*onGroupSelfTimeout)(ToxWindow *, Tox *, int); + #ifdef AUDIO void(*onInvite)(ToxWindow *, ToxAv *, int); @@ -143,7 +150,7 @@ struct ToxWindow { void(*onPeerTimeout)(ToxWindow *, ToxAv *, int); void(*onWriteDevice)(ToxWindow *, Tox *, int, int, const int16_t *, unsigned int, uint8_t, unsigned int); - int call_idx; /* If in a call will have this index set, otherwise it's -1. + int call_idx; /* If in a call will have this index set, otherwise it's -1. * Don't modify outside av callbacks. */ int device_selection[2]; /* -1 if not set, if set uses these selections instead of primary device */ @@ -151,7 +158,7 @@ struct ToxWindow { #endif /* AUDIO */ int active_box; /* For box notify */ - + char name[TOXIC_MAX_NAME_LENGTH + 1]; int32_t num; /* corresponds to friendnumber in chat windows */ bool active; @@ -252,7 +259,7 @@ void on_window_resize(void); void force_refresh(WINDOW *w); ToxWindow *get_window_ptr(int i); -/* refresh inactive windows to prevent scrolling bugs. +/* refresh inactive windows to prevent scrolling bugs. call at least once per second */ void refresh_inactive_windows(void);