diff --git a/src/api.c b/src/api.c index c7fd15a95..0909f2e8c 100644 --- a/src/api.c +++ b/src/api.c @@ -37,7 +37,7 @@ #ifdef PYTHON #include "python_api.h" -Tox *user_tox; +Toxic *user_toxic; static WINDOW *cur_window; static ToxWindow *self_window; @@ -58,33 +58,33 @@ FriendsList api_get_friendslist(void) char *api_get_nick(void) { - size_t len = tox_self_get_name_size(user_tox); + size_t len = tox_self_get_name_size(user_toxic->tox); uint8_t *name = malloc(len + 1); if (name == NULL) { return NULL; } - tox_self_get_name(user_tox, name); + tox_self_get_name(user_toxic->tox, name); name[len] = '\0'; return (char *) name; } Tox_User_Status api_get_status(void) { - return tox_self_get_status(user_tox); + return tox_self_get_status(user_toxic->tox); } char *api_get_status_message(void) { - size_t len = tox_self_get_status_message_size(user_tox); + size_t len = tox_self_get_status_message_size(user_toxic->tox); uint8_t *status = malloc(len + 1); if (status == NULL) { return NULL; } - tox_self_get_status_message(user_tox, status); + tox_self_get_status_message(user_toxic->tox, status); status[len] = '\0'; return (char *) status; } @@ -113,7 +113,7 @@ void api_send(const char *msg) void api_execute(const char *input, int mode) { self_window = get_active_window(); - execute(cur_window, self_window, user_tox, input, mode); + execute(cur_window, self_window, user_toxic, input, mode); } int do_plugin_command(int num_args, char (*args)[MAX_STR_SIZE]) diff --git a/src/audio_call.c b/src/audio_call.c index 2ffd06e69..d5b3b3f3f 100644 --- a/src/audio_call.c +++ b/src/audio_call.c @@ -67,7 +67,7 @@ void on_call_state(ToxAV *av, uint32_t friend_number, uint32_t state, void *user void on_audio_receive_frame(ToxAV *av, uint32_t friend_number, int16_t const *pcm, size_t sample_count, uint8_t channels, uint32_t sampling_rate, void *user_data); -void callback_recv_invite(Tox *tox, uint32_t friend_number); +void callback_recv_invite(Toxic *toxic, uint32_t friend_number); void callback_recv_ringing(uint32_t friend_number); void callback_recv_starting(uint32_t friend_number); void callback_recv_ending(uint32_t friend_number); @@ -86,13 +86,13 @@ static void print_err(ToxWindow *self, const char *error_str) line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", error_str); } -ToxAV *init_audio(ToxWindow *self, Tox *tox) +ToxAV *init_audio(ToxWindow *self, Toxic *toxic) { Toxav_Err_New error; CallControl.audio_errors = ae_None; CallControl.prompt = self; - CallControl.av = toxav_new(tox, &error); + toxic->av = toxav_new(toxic->tox, &error); CallControl.audio_enabled = true; CallControl.default_audio_bit_rate = 64; @@ -104,26 +104,27 @@ ToxAV *init_audio(ToxWindow *self, Tox *tox) CallControl.default_video_bit_rate = 0; CallControl.video_frame_duration = 0; - if (!CallControl.av) { + if (toxic->av == NULL) { CallControl.audio_errors |= ae_StartingCoreAudio; line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to init ToxAV"); - return NULL; } if (init_devices() == de_InternalError) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to init devices"); - toxav_kill(CallControl.av); - - return CallControl.av = NULL; + toxav_kill(toxic->av); + toxic->av = NULL; + return NULL; } - toxav_callback_call(CallControl.av, on_call, tox); - toxav_callback_call_state(CallControl.av, on_call_state, NULL); - toxav_callback_audio_receive_frame(CallControl.av, on_audio_receive_frame, NULL); - toxav_callback_audio_bit_rate(CallControl.av, audio_bit_rate_callback, NULL); + toxav_callback_call(toxic->av, on_call, (void *) toxic); + toxav_callback_call_state(toxic->av, on_call_state, NULL); + toxav_callback_audio_receive_frame(toxic->av, on_audio_receive_frame, NULL); + toxav_callback_audio_bit_rate(toxic->av, audio_bit_rate_callback, NULL); - return CallControl.av; + CallControl.av = toxic->av; + + return toxic->av; } void read_device_callback(const int16_t *captured, uint32_t size, void *data) @@ -272,14 +273,14 @@ static int stop_transmission(Call *call, uint32_t friend_number) return 0; } -void terminate_audio(void) +void terminate_audio(ToxAV *av) { for (int i = 0; i < CallControl.max_calls; ++i) { stop_transmission(&CallControl.calls[i], i); } - if (CallControl.av) { - toxav_kill(CallControl.av); + if (av) { + toxav_kill(av); } terminate_devices(); @@ -300,7 +301,7 @@ void on_call(ToxAV *av, uint32_t friend_number, bool audio_enabled, bool video_e { UNUSED_VAR(av); - Tox *tox = (Tox *) user_data; + Toxic *toxic = (Toxic *) user_data; Call *call = &CallControl.calls[friend_number]; init_call(call); @@ -315,7 +316,7 @@ void on_call(ToxAV *av, uint32_t friend_number, bool audio_enabled, bool video_e call->state |= TOXAV_FRIEND_CALL_STATE_SENDING_V; } - callback_recv_invite(tox, friend_number); + callback_recv_invite(toxic, friend_number); } void on_call_state(ToxAV *av, uint32_t friend_number, uint32_t state, void *user_data) @@ -394,8 +395,12 @@ void audio_bit_rate_callback(ToxAV *av, uint32_t friend_number, uint32_t audio_b toxav_audio_set_bit_rate(av, friend_number, audio_bit_rate, NULL); } -void callback_recv_invite(Tox *tox, uint32_t friend_number) +void callback_recv_invite(Toxic *toxic, uint32_t friend_number) { + if (toxic == NULL) { + return; + } + if (friend_number >= Friends.max_idx) { return; } @@ -405,14 +410,14 @@ void callback_recv_invite(Tox *tox, uint32_t friend_number) return; } - Friends.list[friend_number].chatwin = add_window(tox, new_chat(tox, Friends.list[friend_number].num)); + Friends.list[friend_number].chatwin = add_window(toxic, new_chat(toxic->tox, Friends.list[friend_number].num)); } const Call *call = &CallControl.calls[friend_number]; for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i] != NULL && windows[i]->onInvite != NULL && windows[i]->num == friend_number) { - windows[i]->onInvite(windows[i], CallControl.av, friend_number, call->state); + windows[i]->onInvite(windows[i], NULL, friend_number, call->state); } } } @@ -422,7 +427,7 @@ void callback_recv_ringing(uint32_t friend_number) for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i] != NULL && windows[i]->onRinging != NULL && windows[i]->num == friend_number) { - windows[i]->onRinging(windows[i], CallControl.av, friend_number, call->state); + windows[i]->onRinging(windows[i], NULL, friend_number, call->state); } } } @@ -432,8 +437,7 @@ void callback_recv_starting(uint32_t friend_number) for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i] != NULL && windows[i]->onStarting != NULL && windows[i]->num == friend_number) { - windows[i]->onStarting(windows[i], CallControl.av, friend_number, call->state); - + windows[i]->onStarting(windows[i], NULL, friend_number, call->state); start_call(windows[i], call); } } @@ -444,7 +448,7 @@ void callback_recv_ending(uint32_t friend_number) for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i] != NULL && windows[i]->onEnding != NULL && windows[i]->num == friend_number) { - windows[i]->onEnding(windows[i], CallControl.av, friend_number, call->state); + windows[i]->onEnding(windows[i], NULL, friend_number, call->state); } } } @@ -454,7 +458,7 @@ void callback_call_started(uint32_t friend_number) for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i] != NULL && windows[i]->onStart != NULL && windows[i]->num == friend_number) { - windows[i]->onStart(windows[i], CallControl.av, friend_number, call->state); + windows[i]->onStart(windows[i], NULL, friend_number, call->state); start_call(windows[i], call); } @@ -466,7 +470,7 @@ void callback_call_canceled(uint32_t friend_number) for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i] != NULL && windows[i]->onCancel != NULL && windows[i]->num == friend_number) { - windows[i]->onCancel(windows[i], CallControl.av, friend_number, call->state); + windows[i]->onCancel(windows[i], NULL, friend_number, call->state); } } } @@ -476,7 +480,7 @@ void callback_call_rejected(uint32_t friend_number) for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i] != NULL && windows[i]->onReject != NULL && windows[i]->num == friend_number) { - windows[i]->onReject(windows[i], CallControl.av, friend_number, call->state); + windows[i]->onReject(windows[i], NULL, friend_number, call->state); } } } @@ -486,7 +490,7 @@ void callback_call_ended(uint32_t friend_number) for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i] != NULL && windows[i]->onEnd != NULL && windows[i]->num == friend_number) { - windows[i]->onEnd(windows[i], CallControl.av, friend_number, call->state); + windows[i]->onEnd(windows[i], NULL, friend_number, call->state); } } } @@ -499,18 +503,21 @@ void callback_call_ended(uint32_t friend_number) /* * Commands from chat_commands.h */ -void cmd_call(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_call(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); UNUSED_VAR(argv); + if (toxic == NULL) { + return; + } + if (argc != 0) { print_err(self, "Unknown arguments."); return; } - if (!CallControl.av) { + if (toxic->av == NULL) { print_err(self, "ToxAV not supported!"); return; } @@ -532,12 +539,15 @@ void cmd_call(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[ place_call(self); } -void cmd_answer(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_answer(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); UNUSED_VAR(argv); + if (toxic == NULL) { + return; + } + Toxav_Err_Answer error; if (argc != 0) { @@ -545,7 +555,7 @@ void cmd_answer(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv return; } - if (!CallControl.av) { + if (toxic->av == NULL) { print_err(self, "Audio not supported!"); return; } @@ -557,7 +567,7 @@ void cmd_answer(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv return; } - toxav_answer(CallControl.av, self->num, call->audio_bit_rate, call->video_bit_rate, &error); + toxav_answer(toxic->av, self->num, call->audio_bit_rate, call->video_bit_rate, &error); if (error != TOXAV_ERR_ANSWER_OK) { if (error == TOXAV_ERR_ANSWER_FRIEND_NOT_CALLING) { @@ -579,18 +589,21 @@ void cmd_answer(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv callback_recv_starting(self->num); } -void cmd_reject(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_reject(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); UNUSED_VAR(argv); + if (toxic == NULL) { + return; + } + if (argc != 0) { print_err(self, "Unknown arguments."); return; } - if (!CallControl.av) { + if (toxic->av == NULL) { print_err(self, "Audio not supported!"); return; } @@ -603,20 +616,23 @@ void cmd_reject(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv } /* Manually send a cancel call control because call hasn't started */ - toxav_call_control(CallControl.av, self->num, TOXAV_CALL_CONTROL_CANCEL, NULL); + toxav_call_control(toxic->av, self->num, TOXAV_CALL_CONTROL_CANCEL, NULL); cancel_call(call); /* Callback will print status... */ callback_call_rejected(self->num); } -void cmd_hangup(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_hangup(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); UNUSED_VAR(argv); - if (!CallControl.av) { + if (toxic == NULL) { + return; + } + + if (toxic->av == NULL) { print_err(self, "Audio not supported!"); return; } @@ -636,10 +652,10 @@ void cmd_hangup(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv stop_current_call(self); } -void cmd_list_devices(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_list_devices(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); + UNUSED_VAR(toxic); if (argc != 1) { if (argc < 1) { @@ -673,10 +689,10 @@ void cmd_list_devices(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char } /* This changes primary device only */ -void cmd_change_device(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_change_device(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); + UNUSED_VAR(toxic); if (argc != 2) { if (argc < 1) { @@ -720,10 +736,10 @@ void cmd_change_device(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char } } -void cmd_mute(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_mute(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); + UNUSED_VAR(toxic); if (argc != 1) { print_err(self, "Specify type: \"/mute in\" or \"/mute out\"."); @@ -762,10 +778,10 @@ void cmd_mute(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[ return; } -void cmd_sense(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_sense(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); + UNUSED_VAR(toxic); if (argc != 1) { if (argc < 1) { @@ -796,10 +812,13 @@ void cmd_sense(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv) return; } -void cmd_bitrate(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_bitrate(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); + + if (toxic == NULL) { + return; + } Call *call = &CallControl.calls[self->num]; @@ -828,7 +847,7 @@ void cmd_bitrate(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*arg } Toxav_Err_Bit_Rate_Set error; - toxav_audio_set_bit_rate(CallControl.av, self->num, bit_rate, &error); + toxav_audio_set_bit_rate(toxic->av, self->num, bit_rate, &error); if (error != TOXAV_ERR_BIT_RATE_SET_OK) { if (error == TOXAV_ERR_BIT_RATE_SET_SYNC) { diff --git a/src/audio_call.h b/src/audio_call.h index aae4f513b..a0b811f05 100644 --- a/src/audio_call.h +++ b/src/audio_call.h @@ -95,8 +95,8 @@ struct CallControl { extern struct CallControl CallControl; /* You will have to pass pointer to first member of 'windows' declared in windows.c */ -ToxAV *init_audio(ToxWindow *self, Tox *tox); -void terminate_audio(void); +ToxAV *init_audio(ToxWindow *self, Toxic *toxic); +void terminate_audio(ToxAV *av); bool init_call(Call *call); diff --git a/src/autocomplete.c b/src/autocomplete.c index 4a99a511f..bce010a1e 100644 --- a/src/autocomplete.c +++ b/src/autocomplete.c @@ -38,10 +38,10 @@ #include "toxic.h" #include "windows.h" -static void print_ac_matches(ToxWindow *self, Tox *tox, char **list, size_t n_matches) +static void print_ac_matches(ToxWindow *self, Toxic *toxic, char **list, size_t n_matches) { - if (tox != NULL) { - execute(self->chatwin->history, self, tox, "/clear", GLOBAL_COMMAND_MODE); + if (toxic != NULL) { + execute(self->chatwin->history, self, toxic, "/clear", GLOBAL_COMMAND_MODE); } for (size_t i = 0; i < n_matches; ++i) { @@ -317,7 +317,7 @@ static bool is_partial_match(const char *s, const char *p, size_t p_len) * Returns -1 if no matches or more than one match. */ #define MAX_DIRS 75 -int dir_match(ToxWindow *self, Tox *tox, const wchar_t *line, const wchar_t *cmd) +int dir_match(ToxWindow *self, Toxic *toxic, const wchar_t *line, const wchar_t *cmd) { char b_path[MAX_STR_SIZE + 1]; char b_name[MAX_STR_SIZE + 1]; @@ -382,7 +382,7 @@ int dir_match(ToxWindow *self, Tox *tox, const wchar_t *line, const wchar_t *cmd if (dircount > 1) { qsort(dirnames, dircount, sizeof(char *), qsort_ptr_char_array_helper); - print_ac_matches(self, tox, dirnames, dircount); + print_ac_matches(self, toxic, dirnames, dircount); } int ret = complete_path(self, (const char *const *) dirnames, dircount); diff --git a/src/autocomplete.h b/src/autocomplete.h index e026af5c2..44c9419b5 100644 --- a/src/autocomplete.h +++ b/src/autocomplete.h @@ -47,6 +47,6 @@ int complete_line(ToxWindow *self, const char **list, size_t n_items); * Returns the diff between old len and new len of ctx->line on success. * Returns -1 if no matches or more than one match. */ -int dir_match(ToxWindow *self, Tox *tox, const wchar_t *line, const wchar_t *cmd); +int dir_match(ToxWindow *self, Toxic *toxic, const wchar_t *line, const wchar_t *cmd); #endif /* AUTOCOMPLETE_H */ diff --git a/src/bootstrap.c b/src/bootstrap.c index 06477b4a4..bc0df018d 100644 --- a/src/bootstrap.c +++ b/src/bootstrap.c @@ -669,8 +669,8 @@ static void DHT_bootstrap(Tox *tox) /* Manages connection to the Tox DHT network. */ void do_tox_connection(Tox *tox) { - static time_t last_bootstrap_time = 0; - bool connected = prompt_selfConnectionStatus() != TOX_CONNECTION_NONE; + static time_t last_bootstrap_time = 0; // TODO: Put this in Toxic + const bool connected = prompt_selfConnectionStatus() != TOX_CONNECTION_NONE; if (!connected && timed_out(last_bootstrap_time, TRY_BOOTSTRAP_INTERVAL)) { DHT_bootstrap(tox); diff --git a/src/chat.c b/src/chat.c index 2b9e909a4..836b44afb 100644 --- a/src/chat.c +++ b/src/chat.c @@ -205,16 +205,21 @@ static void recv_action_helper(ToxWindow *self, const char *action, const char * } } -static void chat_onMessage(ToxWindow *self, Tox *tox, uint32_t num, Tox_Message_Type type, const char *msg, size_t len) +static void chat_onMessage(ToxWindow *self, Toxic *toxic, uint32_t num, Tox_Message_Type type, const char *msg, + size_t len) { UNUSED_VAR(len); + if (toxic == NULL || self == NULL) { + return; + } + if (self->num != num) { return; } char nick[TOX_MAX_NAME_LENGTH]; - get_nick_truncate(tox, nick, num); + get_nick_truncate(toxic->tox, nick, num); if (type == TOX_MESSAGE_TYPE_NORMAL) { recv_message_helper(self, msg, nick); @@ -230,8 +235,14 @@ static void chat_onMessage(ToxWindow *self, Tox *tox, uint32_t num, Tox_Message_ static void chat_pause_file_transfers(uint32_t friendnum); static void chat_resume_file_senders(ToxWindow *self, Tox *tox, uint32_t fnum); -static void chat_onConnectionChange(ToxWindow *self, Tox *tox, uint32_t num, Tox_Connection connection_status) +static void chat_onConnectionChange(ToxWindow *self, Toxic *toxic, uint32_t num, Tox_Connection connection_status) { + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (self->num != num) { return; } @@ -252,7 +263,7 @@ static void chat_onConnectionChange(ToxWindow *self, Tox *tox, uint32_t num, Tox if (prev_status == TOX_CONNECTION_NONE) { chat_resume_file_senders(self, tox, num); - file_send_queue_check(self, tox, self->num); + file_send_queue_check(self, toxic, self->num); msg = "has come online"; line_info_add(self, true, nick, NULL, CONNECTION, 0, GREEN, msg); @@ -618,13 +629,19 @@ static bool valid_file_name(const char *filename, size_t length) return true; } -static void chat_onFileRecv(ToxWindow *self, Tox *tox, uint32_t friendnum, uint32_t filenumber, uint64_t file_size, +static void chat_onFileRecv(ToxWindow *self, Toxic *toxic, uint32_t friendnum, uint32_t filenumber, uint64_t file_size, const char *filename, size_t name_length) { if (self->num != friendnum) { return; } + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + /* first check if we need to resume a broken transfer */ if (chat_resume_broken_ft(self, tox, friendnum, filenumber)) { return; @@ -724,17 +741,21 @@ static void chat_onFileRecv(ToxWindow *self, Tox *tox, uint32_t friendnum, uint3 char cmd[MAX_STR_SIZE]; snprintf(cmd, sizeof(cmd), "/savefile %zu", ft->index); - execute(self->window, self, tox, cmd, CHAT_COMMAND_MODE); + execute(self->window, self, toxic, cmd, CHAT_COMMAND_MODE); } else { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Type '/savefile %zu' to accept the file transfer.", ft->index); } } -static void chat_onConferenceInvite(ToxWindow *self, Tox *tox, int32_t friendnumber, uint8_t type, +static void chat_onConferenceInvite(ToxWindow *self, Toxic *toxic, int32_t friendnumber, uint8_t type, const char *conference_pub_key, uint16_t length) { + if (toxic == NULL || self == NULL) { + return; + } + if (self->num != friendnumber) { return; } @@ -743,10 +764,11 @@ static void chat_onConferenceInvite(ToxWindow *self, Tox *tox, int32_t friendnum free(Friends.list[friendnumber].conference_invite.key); } - char *k = malloc(length); + char *k = malloc(length * sizeof(char)); if (k == NULL) { - exit_toxic_err("Failed in chat_onConferenceInvite", FATALERR_MEMORY); + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Conference invite failed (OOM)"); + return; } memcpy(k, conference_pub_key, length); @@ -756,7 +778,7 @@ static void chat_onConferenceInvite(ToxWindow *self, Tox *tox, int32_t friendnum Friends.list[friendnumber].conference_invite.type = type; char name[TOX_MAX_NAME_LENGTH]; - get_nick_truncate(tox, name, friendnumber); + get_nick_truncate(toxic->tox, name, friendnumber); const char *description = type == TOX_CONFERENCE_TYPE_AV ? "an audio conference" : "a conference"; @@ -772,11 +794,16 @@ static void chat_onConferenceInvite(ToxWindow *self, Tox *tox, int32_t friendnum line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Type \"/cjoin\" to join the chat."); } -static void chat_onGroupInvite(ToxWindow *self, Tox *tox, uint32_t friendnumber, const char *invite_data, size_t length, +static void chat_onGroupInvite(ToxWindow *self, Toxic *toxic, uint32_t friendnumber, const char *invite_data, + size_t length, const char *group_name, size_t group_name_length) { UNUSED_VAR(group_name_length); + if (self == NULL || toxic == NULL) { + return; + } + if (self->num != friendnumber) { return; } @@ -785,7 +812,7 @@ static void chat_onGroupInvite(ToxWindow *self, Tox *tox, uint32_t friendnumber, free(Friends.list[friendnumber].group_invite.data); } - Friends.list[friendnumber].group_invite.data = malloc(length * sizeof(uint8_t)); + Friends.list[friendnumber].group_invite.data = malloc(length * sizeof(char)); if (Friends.list[friendnumber].group_invite.data == NULL) { return; @@ -797,7 +824,7 @@ static void chat_onGroupInvite(ToxWindow *self, Tox *tox, uint32_t friendnumber, sound_notify(self, generic_message, NT_WNDALERT_2 | user_settings->bell_on_invite, NULL); char name[TOX_MAX_NAME_LENGTH]; - get_nick_truncate(tox, name, friendnumber); + get_nick_truncate(toxic->tox, 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"); @@ -811,9 +838,13 @@ static void chat_onGroupInvite(ToxWindow *self, Tox *tox, uint32_t friendnumber, } #ifdef GAMES -void chat_onGameInvite(ToxWindow *self, Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length) +void chat_onGameInvite(ToxWindow *self, Toxic *toxic, uint32_t friend_number, const uint8_t *data, size_t length) { - if (!self || self->num != friend_number) { + if (toxic == NULL || self == NULL) { + return; + } + + if (self->num != friend_number) { return; } @@ -821,7 +852,7 @@ void chat_onGameInvite(ToxWindow *self, Tox *tox, uint32_t friend_number, const return; } - uint8_t version = data[0]; + const uint8_t version = data[0]; if (version != GAME_NETWORKING_VERSION) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, @@ -865,7 +896,7 @@ void chat_onGameInvite(ToxWindow *self, Tox *tox, uint32_t friend_number, const Friends.list[friend_number].game_invite.data_length = data_length; char name[TOX_MAX_NAME_LENGTH]; - get_nick_truncate(tox, name, friend_number); + get_nick_truncate(toxic->tox, name, friend_number); if (self->active_box != -1) { box_notify2(self, generic_message, NT_WNDALERT_2 | user_settings->bell_on_invite, self->active_box, @@ -885,12 +916,12 @@ void chat_onGameInvite(ToxWindow *self, Tox *tox, uint32_t friend_number, const /* AV Stuff */ #ifdef AUDIO -void chat_onInvite(ToxWindow *self, ToxAV *av, uint32_t friend_number, int state) +void chat_onInvite(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) { - UNUSED_VAR(av); + UNUSED_VAR(toxic); UNUSED_VAR(state); - if (!self || self->num != friend_number) { + if (self == NULL || self->num != friend_number) { return; } @@ -910,9 +941,9 @@ void chat_onInvite(ToxWindow *self, ToxAV *av, uint32_t friend_number, int state } } -void chat_onRinging(ToxWindow *self, ToxAV *av, uint32_t friend_number, int state) +void chat_onRinging(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) { - UNUSED_VAR(av); + UNUSED_VAR(toxic); UNUSED_VAR(state); if (!self || self->num != friend_number) { @@ -930,9 +961,9 @@ void chat_onRinging(ToxWindow *self, ToxAV *av, uint32_t friend_number, int stat #endif /* SOUND_NOTIFY */ } -void chat_onStarting(ToxWindow *self, ToxAV *av, uint32_t friend_number, int state) +void chat_onStarting(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) { - UNUSED_VAR(av); + UNUSED_VAR(toxic); UNUSED_VAR(state); if (!self || self->num != friend_number) { @@ -951,9 +982,9 @@ void chat_onStarting(ToxWindow *self, ToxAV *av, uint32_t friend_number, int sta #endif /* SOUND_NOTIFY */ } -void chat_onEnding(ToxWindow *self, ToxAV *av, uint32_t friend_number, int state) +void chat_onEnding(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) { - UNUSED_VAR(av); + UNUSED_VAR(toxic); UNUSED_VAR(state); if (!self || self->num != friend_number) { @@ -970,9 +1001,9 @@ void chat_onEnding(ToxWindow *self, ToxAV *av, uint32_t friend_number, int state #endif /* SOUND_NOTIFY */ } -void chat_onError(ToxWindow *self, ToxAV *av, uint32_t friend_number, int state) +void chat_onError(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) { - UNUSED_VAR(av); + UNUSED_VAR(toxic); UNUSED_VAR(state); if (!self || self->num != friend_number) { @@ -987,9 +1018,9 @@ void chat_onError(ToxWindow *self, ToxAV *av, uint32_t friend_number, int state) #endif /* SOUND_NOTIFY */ } -void chat_onStart(ToxWindow *self, ToxAV *av, uint32_t friend_number, int state) +void chat_onStart(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) { - UNUSED_VAR(av); + UNUSED_VAR(toxic); UNUSED_VAR(state); if (!self || self->num != friend_number) { @@ -1008,9 +1039,9 @@ void chat_onStart(ToxWindow *self, ToxAV *av, uint32_t friend_number, int state) #endif /* SOUND_NOTIFY */ } -void chat_onCancel(ToxWindow *self, ToxAV *av, uint32_t friend_number, int state) +void chat_onCancel(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) { - UNUSED_VAR(av); + UNUSED_VAR(toxic); UNUSED_VAR(state); if (!self || self->num != friend_number) { @@ -1026,9 +1057,9 @@ void chat_onCancel(ToxWindow *self, ToxAV *av, uint32_t friend_number, int state #endif /* SOUND_NOTIFY */ } -void chat_onReject(ToxWindow *self, ToxAV *av, uint32_t friend_number, int state) +void chat_onReject(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) { - UNUSED_VAR(av); + UNUSED_VAR(toxic); UNUSED_VAR(state); if (!self || self->num != friend_number) { @@ -1043,9 +1074,9 @@ void chat_onReject(ToxWindow *self, ToxAV *av, uint32_t friend_number, int state #endif /* SOUND_NOTIFY */ } -void chat_onEnd(ToxWindow *self, ToxAV *av, uint32_t friend_number, int state) +void chat_onEnd(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) { - UNUSED_VAR(av); + UNUSED_VAR(toxic); UNUSED_VAR(state); if (!self || self->num != friend_number) { @@ -1177,8 +1208,14 @@ static void send_action(ToxWindow *self, ChatContext *ctx, Tox *tox, char *actio /* * Return true if input is recognized by handler */ -bool chat_onKey(ToxWindow *self, Tox *tox, wint_t key, bool ltr) +bool chat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) { + if (toxic == NULL || self == NULL) { + return false; + } + + Tox *tox = toxic->tox; + ChatContext *ctx = self->chatwin; StatusBar *statusbar = self->stb; @@ -1223,14 +1260,14 @@ bool chat_onKey(ToxWindow *self, Tox *tox, wint_t key, bool ltr) /* TODO: make this not suck */ if (wcsncmp(ctx->line, L"/sendfile ", wcslen(L"/sendfile ")) == 0) { - diff = dir_match(self, tox, ctx->line, L"/sendfile"); + diff = dir_match(self, toxic, ctx->line, L"/sendfile"); } else if (wcsncmp(ctx->line, L"/avatar ", wcslen(L"/avatar ")) == 0) { - diff = dir_match(self, tox, ctx->line, L"/avatar"); + diff = dir_match(self, toxic, ctx->line, L"/avatar"); } #ifdef PYTHON else if (wcsncmp(ctx->line, L"/run ", wcslen(L"/run ")) == 0) { - diff = dir_match(self, tox, ctx->line, L"/run"); + diff = dir_match(self, toxic, ctx->line, L"/run"); } #endif @@ -1277,7 +1314,7 @@ bool chat_onKey(ToxWindow *self, Tox *tox, wint_t key, bool ltr) } else if (strncmp(line, "/me ", strlen("/me ")) == 0) { send_action(self, ctx, tox, line + strlen("/me ")); } else { - execute(ctx->history, self, tox, line, CHAT_COMMAND_MODE); + execute(ctx->history, self, toxic, line, CHAT_COMMAND_MODE); } } else if (line[0]) { char selfname[TOX_MAX_NAME_LENGTH]; @@ -1543,10 +1580,16 @@ static void chat_init_log(ToxWindow *self, Tox *tox, const char *self_nick) } } -static void chat_onInit(ToxWindow *self, Tox *tox) +static void chat_onInit(ToxWindow *self, Toxic *toxic) { curs_set(1); + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + int x2; int y2; getmaxyx(self->window, y2, x2); @@ -1599,7 +1642,7 @@ static void chat_onInit(ToxWindow *self, Tox *tox) chat_init_log(self, tox, nick); - execute(ctx->history, self, tox, "/log", GLOBAL_COMMAND_MODE); // Print log status to screen + execute(ctx->history, self, toxic, "/log", GLOBAL_COMMAND_MODE); // Print log status to screen scrollok(ctx->history, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0); diff --git a/src/chat_commands.c b/src/chat_commands.c index 5d4f43b4c..f8ddc3546 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -37,10 +37,14 @@ extern ToxWindow *prompt; extern FriendsList Friends; -void cmd_autoaccept_files(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_autoaccept_files(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); + UNUSED_VAR(toxic); + + if (self == NULL) { + return; + } const char *msg; const bool auto_accept_files = friend_get_auto_accept_files(self->num); @@ -71,10 +75,16 @@ void cmd_autoaccept_files(WINDOW *window, ToxWindow *self, Tox *tox, int argc, c line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, msg); } -void cmd_cancelfile(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_cancelfile(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (argc < 2) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Requires type in|out and the file ID."); return; @@ -121,10 +131,16 @@ void cmd_cancelfile(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (* close_file_transfer(self, tox, ft, TOX_FILE_CONTROL_CANCEL, msg, silent); } -void cmd_conference_invite(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_conference_invite(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (argc < 1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Conference number required."); return; @@ -147,12 +163,18 @@ void cmd_conference_invite(WINDOW *window, ToxWindow *self, Tox *tox, int argc, line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invited contact to Conference %ld.", conferencenum); } -void cmd_conference_join(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_conference_join(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); UNUSED_VAR(argc); UNUSED_VAR(argv); + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (get_num_active_windows() >= MAX_WINDOWS_NUM) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, " * Warning: Too many windows are open."); return; @@ -196,7 +218,7 @@ void cmd_conference_join(WINDOW *window, ToxWindow *self, Tox *tox, int argc, ch return; } - if (init_conference_win(tox, conferencenum, type, NULL, 0) == -1) { + if (init_conference_win(toxic, conferencenum, type, NULL, 0) == -1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Conference window failed to initialize."); tox_conference_delete(tox, conferencenum, NULL); return; @@ -213,8 +235,14 @@ void cmd_conference_join(WINDOW *window, ToxWindow *self, Tox *tox, int argc, ch #endif } -void cmd_group_accept(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_group_accept(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (get_num_active_windows() >= MAX_WINDOWS_NUM) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, " * Warning: Too many windows are open."); return; @@ -253,15 +281,21 @@ void cmd_group_accept(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char return; } - if (init_groupchat_win(tox, groupnumber, NULL, 0, Group_Join_Type_Join) == -1) { + if (init_groupchat_win(toxic, groupnumber, NULL, 0, Group_Join_Type_Join) == -1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Group chat window failed to initialize."); tox_group_leave(tox, groupnumber, NULL, 0, NULL); return; } } -void cmd_group_invite(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_group_invite(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (argc < 1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Group number required."); return; @@ -286,10 +320,13 @@ void cmd_group_invite(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char #ifdef GAMES -void cmd_game_join(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_game_join(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); + + if (toxic == NULL || self == NULL) { + return; + } if (!Friends.list[self->num].game_invite.pending) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending game invite."); @@ -306,7 +343,7 @@ void cmd_game_join(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*a uint8_t *data = Friends.list[self->num].game_invite.data; size_t length = Friends.list[self->num].game_invite.data_length; - int ret = game_initialize(self, tox, type, id, data, length, false); + const int ret = game_initialize(self, toxic, type, id, data, length, false); switch (ret) { case 0: { @@ -335,10 +372,16 @@ void cmd_game_join(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*a #endif // GAMES -void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_savefile(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (argc < 1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "File ID required."); return; @@ -414,10 +457,16 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*ar } } -void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_sendfile(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + const char *errmsg = NULL; if (argc < 1) { diff --git a/src/chat_commands.h b/src/chat_commands.h index 58d82d97c..de906c14a 100644 --- a/src/chat_commands.h +++ b/src/chat_commands.h @@ -26,32 +26,32 @@ #include "toxic.h" #include "windows.h" -void cmd_autoaccept_files(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_cancelfile(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_conference_invite(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_conference_join(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_group_accept(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_group_invite(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_game_join(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]); +void cmd_autoaccept_files(WINDOW *window, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_cancelfile(WINDOW *window, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_conference_invite(WINDOW *, ToxWindow *, Toxic*, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_conference_join(WINDOW *, ToxWindow *, Toxic*, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_group_accept(WINDOW *window, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_group_invite(WINDOW *window, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_game_join(WINDOW *, ToxWindow *, Toxic*, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_savefile(WINDOW *, ToxWindow *, Toxic*, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_sendfile(WINDOW *, ToxWindow *, Toxic*, int argc, char (*argv)[MAX_STR_SIZE]); #ifdef AUDIO -void cmd_call(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_answer(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_reject(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_hangup(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_cancel(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_ccur_device(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_mute(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_sense(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_bitrate(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_call(WINDOW *, ToxWindow *, Toxic*, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_answer(WINDOW *, ToxWindow *, Toxic*, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_reject(WINDOW *, ToxWindow *, Toxic*, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_hangup(WINDOW *, ToxWindow *, Toxic*, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_cancel(WINDOW *, ToxWindow *, Toxic*, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_ccur_device(WINDOW *, ToxWindow *, Toxic*, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_mute(WINDOW *, ToxWindow *, Toxic*, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_sense(WINDOW *, ToxWindow *, Toxic*, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_bitrate(WINDOW *, ToxWindow *, Toxic*, int argc, char (*argv)[MAX_STR_SIZE]); #endif /* AUDIO */ #ifdef VIDEO -void cmd_vcall(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_video(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_res(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_vcall(WINDOW *, ToxWindow *, Toxic*, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_video(WINDOW *, ToxWindow *, Toxic*, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_res(WINDOW *, ToxWindow *, Toxic*, int argc, char (*argv)[MAX_STR_SIZE]); #endif /* VIDEO */ #endif /* CHAT_COMMANDS_H */ diff --git a/src/conference.c b/src/conference.c index e250b7d93..eacc213af 100644 --- a/src/conference.c +++ b/src/conference.c @@ -162,8 +162,14 @@ static void kill_conference_window(ToxWindow *self) del_window(self); } -static void init_conference_logging(ToxWindow *self, Tox *tox, uint32_t conferencenum) +static void init_conference_logging(ToxWindow *self, Toxic *toxic, uint32_t conferencenum) { + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + ChatContext *ctx = self->chatwin; char my_id[TOX_ADDRESS_SIZE]; @@ -187,11 +193,15 @@ static void init_conference_logging(ToxWindow *self, Tox *tox, uint32_t conferen } } - execute(ctx->history, self, tox, "/log", GLOBAL_COMMAND_MODE); // print log state to screen + execute(ctx->history, self, toxic, "/log", GLOBAL_COMMAND_MODE); // print log state to screen } -int init_conference_win(Tox *tox, uint32_t conferencenum, uint8_t type, const char *title, size_t length) +int init_conference_win(Toxic *toxic, uint32_t conferencenum, uint8_t type, const char *title, size_t length) { + if (toxic == NULL) { + return -1; + } + if (conferencenum > MAX_CONFERENCE_NUM) { return -1; } @@ -205,7 +215,7 @@ int init_conference_win(Tox *tox, uint32_t conferencenum, uint8_t type, const ch // probably it so happens that this will (at least typically) be // the case, because toxic and tox maintain the indices in // parallel ways. But it isn't guaranteed by the API. - conferences[i].chatwin = add_window(tox, self); + conferences[i].chatwin = add_window(toxic, self); conferences[i].active = true; conferences[i].num_peers = 0; conferences[i].type = type; @@ -221,7 +231,7 @@ int init_conference_win(Tox *tox, uint32_t conferencenum, uint8_t type, const ch conference_set_title(self, conferencenum, title, length); - init_conference_logging(self, tox, conferencenum); + init_conference_logging(self, toxic, conferencenum); if (i == max_conference_index) { ++max_conference_index; @@ -834,8 +844,14 @@ static int sidebar_offset(uint32_t conferencenum) /* * Return true if input is recognized by handler */ -static bool conference_onKey(ToxWindow *self, Tox *tox, wint_t key, bool ltr) +static bool conference_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) { + if (toxic == NULL || self == NULL) { + return false; + } + + Tox *tox = toxic->tox; + ChatContext *ctx = self->chatwin; int x, y, y2, x2; @@ -901,12 +917,12 @@ static bool conference_onKey(ToxWindow *self, Tox *tox, wint_t key, bool ltr) free(complete_strs); } } else if (wcsncmp(ctx->line, L"/avatar ", wcslen(L"/avatar ")) == 0) { - diff = dir_match(self, tox, ctx->line, L"/avatar"); + diff = dir_match(self, toxic, ctx->line, L"/avatar"); } #ifdef PYTHON else if (wcsncmp(ctx->line, L"/run ", wcslen(L"/run ")) == 0) { - diff = dir_match(self, tox, ctx->line, L"/run"); + diff = dir_match(self, toxic, ctx->line, L"/run"); } #endif @@ -981,7 +997,7 @@ static bool conference_onKey(ToxWindow *self, Tox *tox, wint_t key, bool ltr) } else if (strncmp(line, "/me ", strlen("/me ")) == 0) { send_conference_action(self, ctx, tox, line + strlen("/me ")); } else { - execute(ctx->history, self, tox, line, CONFERENCE_COMMAND_MODE); + execute(ctx->history, self, toxic, line, CONFERENCE_COMMAND_MODE); } } else if (line[0]) { Tox_Err_Conference_Send_Message err; @@ -1181,8 +1197,12 @@ static void conference_onDraw(ToxWindow *self, Tox *tox) } } -static void conference_onInit(ToxWindow *self, Tox *tox) +static void conference_onInit(ToxWindow *self, Toxic *toxic) { + if (self == NULL) { + return; + } + int x2, y2; getmaxyx(self->window, y2, x2); diff --git a/src/conference.h b/src/conference.h index 1076fb3a4..65b83ffab 100644 --- a/src/conference.h +++ b/src/conference.h @@ -84,7 +84,7 @@ typedef struct { /* Frees all Toxic associated data structures for a conference (does not call tox_conference_delete() ) */ void free_conference(ToxWindow *self, uint32_t conferencenum); -int init_conference_win(Tox *tox, uint32_t conferencenum, uint8_t type, const char *title, size_t length); +int init_conference_win(Toxic *toxic, uint32_t conferencenum, uint8_t type, const char *title, size_t length); /* destroys and re-creates conference window with or without the peerlist */ void redraw_conference_win(ToxWindow *self); diff --git a/src/conference_commands.c b/src/conference_commands.c index 12e9f7b89..a88651c9b 100644 --- a/src/conference_commands.c +++ b/src/conference_commands.c @@ -35,10 +35,16 @@ static void print_err(ToxWindow *self, const char *error_str) line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", error_str); } -void cmd_conference_set_title(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_conference_set_title(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + Tox_Err_Conference_Title err; char title[CONFERENCE_MAX_TITLE_LENGTH + 1]; @@ -93,10 +99,16 @@ void cmd_conference_set_title(WINDOW *window, ToxWindow *self, Tox *tox, int arg } #ifdef AUDIO -void cmd_enable_audio(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_enable_audio(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + bool enable; if (argc == 1 && !strcasecmp(argv[1], "on")) { @@ -116,10 +128,16 @@ void cmd_enable_audio(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char } } -void cmd_conference_mute(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_conference_mute(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (argc < 1) { if (conference_mute_self(self->num)) { print_err(self, "Toggled self audio mute status"); @@ -153,10 +171,14 @@ void cmd_conference_mute(WINDOW *window, ToxWindow *self, Tox *tox, int argc, ch } } -void cmd_conference_sense(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_conference_sense(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); + UNUSED_VAR(toxic); + + if (self == NULL) { + return; + } if (argc == 0) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Current VAD threshold: %.1f", @@ -184,10 +206,14 @@ void cmd_conference_sense(WINDOW *window, ToxWindow *self, Tox *tox, int argc, c } } -void cmd_conference_push_to_talk(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_conference_push_to_talk(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); + UNUSED_VAR(toxic); + + if (self == NULL) { + return; + } bool enable; diff --git a/src/conference_commands.h b/src/conference_commands.h index bdc900b9c..310eeea22 100644 --- a/src/conference_commands.h +++ b/src/conference_commands.h @@ -26,11 +26,11 @@ #include "toxic.h" #include "windows.h" -void cmd_conference_set_title(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_enable_audio(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_conference_mute(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_conference_sense(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_conference_push_to_talk(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_conference_set_title(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_enable_audio(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_conference_mute(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_conference_sense(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_conference_push_to_talk(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); #endif /* CONFERENCE_COMMANDS_H */ diff --git a/src/execute.c b/src/execute.c index e3562b30b..2bf25b3c9 100644 --- a/src/execute.c +++ b/src/execute.c @@ -37,7 +37,7 @@ struct cmd_func { const char *name; - void (*func)(WINDOW *w, ToxWindow *, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); + void (*func)(WINDOW *w, ToxWindow *, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]); }; static struct cmd_func global_commands[] = { @@ -251,12 +251,12 @@ static int parse_command(const char *input, char (*args)[MAX_STR_SIZE]) * Returns 0 on match. * Returns 1 on no match */ -static int do_command(WINDOW *w, ToxWindow *self, Tox *tox, int num_args, struct cmd_func *commands, +static int do_command(WINDOW *w, ToxWindow *self, Toxic *toxic, int num_args, struct cmd_func *commands, char (*args)[MAX_STR_SIZE]) { for (size_t i = 0; commands[i].name != NULL; ++i) { if (strcmp(args[0], commands[i].name) == 0) { - (commands[i].func)(w, self, tox, num_args - 1, args); + (commands[i].func)(w, self, toxic, num_args - 1, args); return 0; } } @@ -264,7 +264,7 @@ static int do_command(WINDOW *w, ToxWindow *self, Tox *tox, int num_args, struct return 1; } -void execute(WINDOW *w, ToxWindow *self, Tox *tox, const char *input, int mode) +void execute(WINDOW *w, ToxWindow *self, Toxic *toxic, const char *input, int mode) { if (string_is_empty(input)) { return; @@ -284,7 +284,7 @@ void execute(WINDOW *w, ToxWindow *self, Tox *tox, const char *input, int mode) */ switch (mode) { case CHAT_COMMAND_MODE: { - if (do_command(w, self, tox, num_args, chat_commands, args) == 0) { + if (do_command(w, self, toxic, num_args, chat_commands, args) == 0) { return; } @@ -292,7 +292,7 @@ void execute(WINDOW *w, ToxWindow *self, Tox *tox, const char *input, int mode) } case CONFERENCE_COMMAND_MODE: { - if (do_command(w, self, tox, num_args, conference_commands, args) == 0) { + if (do_command(w, self, toxic, num_args, conference_commands, args) == 0) { return; } @@ -300,7 +300,7 @@ void execute(WINDOW *w, ToxWindow *self, Tox *tox, const char *input, int mode) } case GROUPCHAT_COMMAND_MODE: { - if (do_command(w, self, tox, num_args, groupchat_commands, args) == 0) { + if (do_command(w, self, toxic, num_args, groupchat_commands, args) == 0) { return; } @@ -308,7 +308,7 @@ void execute(WINDOW *w, ToxWindow *self, Tox *tox, const char *input, int mode) } } - if (do_command(w, self, tox, num_args, global_commands, args) == 0) { + if (do_command(w, self, toxic, num_args, global_commands, args) == 0) { return; } diff --git a/src/execute.h b/src/execute.h index e3e12c9b0..45226781a 100644 --- a/src/execute.h +++ b/src/execute.h @@ -35,6 +35,6 @@ enum { GROUPCHAT_COMMAND_MODE, }; -void execute(WINDOW *w, ToxWindow *self, Tox *tox, const char *input, int mode); +void execute(WINDOW *w, ToxWindow *self, Toxic *toxic, const char *input, int mode); #endif /* EXECUTE_H */ diff --git a/src/file_transfers.c b/src/file_transfers.c index 5f607e410..2599e112c 100644 --- a/src/file_transfers.c +++ b/src/file_transfers.c @@ -288,7 +288,7 @@ int file_send_queue_add(uint32_t friendnumber, const char *file_path, size_t len #define FILE_TRANSFER_SEND_CMD "/sendfile " #define FILE_TRANSFER_SEND_LEN (sizeof(FILE_TRANSFER_SEND_CMD) - 1) -void file_send_queue_check(ToxWindow *self, Tox *tox, uint32_t friendnumber) +void file_send_queue_check(ToxWindow *self, Toxic *toxic, uint32_t friendnumber) { for (size_t i = 0; i < MAX_FILES; ++i) { PendingFileTransfer *pending_slot = &Friends.list[friendnumber].file_send_queue[i]; @@ -300,7 +300,7 @@ void file_send_queue_check(ToxWindow *self, Tox *tox, uint32_t friendnumber) char command[TOX_MAX_FILENAME_LENGTH + FILE_TRANSFER_SEND_LEN + 1]; snprintf(command, sizeof(command), "%s%s", FILE_TRANSFER_SEND_CMD, pending_slot->file_path); - execute(self->window, self, tox, command, CHAT_COMMAND_MODE); + execute(self->window, self, toxic, command, CHAT_COMMAND_MODE); *pending_slot = (PendingFileTransfer) { 0, diff --git a/src/file_transfers.h b/src/file_transfers.h index f6e0a341f..c4d2167ab 100644 --- a/src/file_transfers.h +++ b/src/file_transfers.h @@ -120,7 +120,7 @@ struct FileTransfer *new_file_transfer(ToxWindow *window, uint32_t friendnumber, int file_send_queue_add(uint32_t friendnumber, const char *file_path, size_t length); /* Initiates all file transfers from the file send queue for friend designated by `friendnumber`. */ -void file_send_queue_check(ToxWindow *self, Tox *tox, uint32_t friendnumber); +void file_send_queue_check(ToxWindow *self, Toxic *toxic, uint32_t friendnumber); /* Removes the `index`-th item from the file send queue for `friendnumber`. * diff --git a/src/friendlist.c b/src/friendlist.c index 4989c6913..9b723dc5e 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -161,9 +161,9 @@ static int save_blocklist(char *path) return -1; } - int i, count = 0; + int count = 0; - for (i = 0; i < Blocked.max_idx; ++i) { + for (int i = 0; i < Blocked.max_idx; ++i) { if (count > Blocked.num_blocked) { free(data); return -1; @@ -253,7 +253,7 @@ int load_blocklist(char *path) return -1; } - off_t len = file_size(path); + const off_t len = file_size(path); if (len == 0) { fclose(fp); @@ -279,7 +279,7 @@ int load_blocklist(char *path) return -1; } - int num = len / sizeof(BlockedFriend); + const int num = len / sizeof(BlockedFriend); Blocked.max_idx = num; realloc_blocklist(num); @@ -374,13 +374,19 @@ static void update_friend_last_online(uint32_t num, time_t timestamp) &Friends.list[num].last_online.tm); } -static void friendlist_onMessage(ToxWindow *self, Tox *tox, uint32_t num, Tox_Message_Type type, const char *str, +static void friendlist_onMessage(ToxWindow *self, Toxic *toxic, uint32_t num, Tox_Message_Type type, const char *str, size_t length) { UNUSED_VAR(self); UNUSED_VAR(type); UNUSED_VAR(length); + if (toxic == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (num >= Friends.max_idx) { return; } @@ -390,7 +396,7 @@ static void friendlist_onMessage(ToxWindow *self, Tox *tox, uint32_t num, Tox_Me } if (get_num_active_windows() < MAX_WINDOWS_NUM) { - Friends.list[num].chatwin = add_window(tox, new_chat(tox, Friends.list[num].num)); + Friends.list[num].chatwin = add_window(toxic, new_chat(tox, Friends.list[num].num)); return; } @@ -402,10 +408,14 @@ static void friendlist_onMessage(ToxWindow *self, Tox *tox, uint32_t num, Tox_Me sound_notify(prompt, notif_error, NT_WNDALERT_1, NULL); } -static void friendlist_onConnectionChange(ToxWindow *self, Tox *tox, uint32_t num, Tox_Connection connection_status) +static void friendlist_onConnectionChange(ToxWindow *self, Toxic *toxic, uint32_t num, Tox_Connection connection_status) { UNUSED_VAR(self); + if (toxic == NULL) { + return; + } + if (num >= Friends.max_idx) { return; } @@ -415,14 +425,14 @@ static void friendlist_onConnectionChange(ToxWindow *self, Tox *tox, uint32_t nu } else if (Friends.list[num].connection_status == TOX_CONNECTION_NONE) { ++Friends.num_online; - if (avatar_send(tox, num) == -1) { + if (avatar_send(toxic->tox, num) == -1) { fprintf(stderr, "avatar_send failed for friend %u\n", num); } } Friends.list[num].connection_status = connection_status; update_friend_last_online(num, get_unix_time()); - store_data(tox, DATA_FILE); + store_data(toxic); sort_friendlist_index(); } @@ -585,13 +595,19 @@ static void friendlist_add_blocked(uint32_t fnum, uint32_t bnum) #ifdef GAMES -static void friendlist_onGameInvite(ToxWindow *self, Tox *tox, uint32_t friend_number, const uint8_t *data, +static void friendlist_onGameInvite(ToxWindow *self, Toxic *toxic, uint32_t friend_number, const uint8_t *data, size_t length) { UNUSED_VAR(self); UNUSED_VAR(data); UNUSED_VAR(length); + if (toxic == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (friend_number >= Friends.max_idx) { return; } @@ -601,7 +617,7 @@ static void friendlist_onGameInvite(ToxWindow *self, Tox *tox, uint32_t friend_n } if (get_num_active_windows() < MAX_WINDOWS_NUM) { - Friends.list[friend_number].chatwin = add_window(tox, new_chat(tox, Friends.list[friend_number].num)); + Friends.list[friend_number].chatwin = add_window(toxic, new_chat(tox, Friends.list[friend_number].num)); return; } @@ -616,7 +632,7 @@ static void friendlist_onGameInvite(ToxWindow *self, Tox *tox, uint32_t friend_n #endif // GAMES -static void friendlist_onFileRecv(ToxWindow *self, Tox *tox, uint32_t num, uint32_t filenum, +static void friendlist_onFileRecv(ToxWindow *self, Toxic *toxic, uint32_t num, uint32_t filenum, uint64_t file_size, const char *filename, size_t name_length) { UNUSED_VAR(self); @@ -624,6 +640,8 @@ static void friendlist_onFileRecv(ToxWindow *self, Tox *tox, uint32_t num, uint3 UNUSED_VAR(filename); UNUSED_VAR(name_length); + Tox *tox = toxic->tox; + if (num >= Friends.max_idx) { return; } @@ -633,7 +651,7 @@ static void friendlist_onFileRecv(ToxWindow *self, Tox *tox, uint32_t num, uint3 } if (get_num_active_windows() < MAX_WINDOWS_NUM) { - Friends.list[num].chatwin = add_window(tox, new_chat(tox, Friends.list[num].num)); + Friends.list[num].chatwin = add_window(toxic, new_chat(tox, Friends.list[num].num)); return; } @@ -648,7 +666,7 @@ static void friendlist_onFileRecv(ToxWindow *self, Tox *tox, uint32_t num, uint3 sound_notify(prompt, notif_error, NT_WNDALERT_1, NULL); } -static void friendlist_onConferenceInvite(ToxWindow *self, Tox *tox, int32_t num, uint8_t type, +static void friendlist_onConferenceInvite(ToxWindow *self, Toxic *toxic, int32_t num, uint8_t type, const char *conference_pub_key, uint16_t length) { @@ -657,6 +675,12 @@ static void friendlist_onConferenceInvite(ToxWindow *self, Tox *tox, int32_t num UNUSED_VAR(conference_pub_key); UNUSED_VAR(length); + if (toxic == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (num >= Friends.max_idx) { return; } @@ -666,7 +690,7 @@ static void friendlist_onConferenceInvite(ToxWindow *self, Tox *tox, int32_t num } if (get_num_active_windows() < MAX_WINDOWS_NUM) { - Friends.list[num].chatwin = add_window(tox, new_chat(tox, Friends.list[num].num)); + Friends.list[num].chatwin = add_window(toxic, new_chat(tox, Friends.list[num].num)); return; } @@ -679,13 +703,19 @@ static void friendlist_onConferenceInvite(ToxWindow *self, Tox *tox, int32_t num sound_notify(prompt, notif_error, NT_WNDALERT_1, NULL); } -static void friendlist_onGroupInvite(ToxWindow *self, Tox *tox, uint32_t num, const char *data, size_t length, +static void friendlist_onGroupInvite(ToxWindow *self, Toxic *toxic, uint32_t num, const char *data, size_t length, const char *group_name, size_t group_name_length) { UNUSED_VAR(self); UNUSED_VAR(data); UNUSED_VAR(length); + if (toxic == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (num >= Friends.max_idx) { return; } @@ -695,7 +725,7 @@ static void friendlist_onGroupInvite(ToxWindow *self, Tox *tox, uint32_t num, co } if (get_num_active_windows() < MAX_WINDOWS_NUM) { - Friends.list[num].chatwin = add_window(tox, new_chat(tox, Friends.list[num].num)); + Friends.list[num].chatwin = add_window(toxic, new_chat(tox, Friends.list[num].num)); return; } @@ -724,8 +754,14 @@ static void select_friend(wint_t key, int *selected, int num) } } -static void delete_friend(Tox *tox, uint32_t f_num) +static void delete_friend(Toxic *toxic, uint32_t f_num) { + if (toxic == NULL) { + return; + } + + Tox *tox = toxic->tox; + kill_all_file_transfers_friend(tox, f_num); kill_avatar_file_transfers_friend(tox, f_num); @@ -778,7 +814,7 @@ static void delete_friend(Tox *tox, uint32_t f_num) --Friends.num_selected; } - store_data(tox, DATA_FILE); + store_data(toxic); } /* activates delete friend popup */ @@ -789,17 +825,17 @@ static void del_friend_activate(uint32_t f_num) PendingDelete.num = f_num; } -static void delete_blocked_friend(uint32_t bnum); +static void delete_blocked_friend(Toxic *toxic, uint32_t bnum); /* deactivates delete friend popup and deletes friend if instructed */ -static void del_friend_deactivate(Tox *tox, wint_t key) +static void del_friend_deactivate(Toxic *toxic, wint_t key) { if (key == L'y') { if (blocklist_view == 0) { - delete_friend(tox, PendingDelete.num); + delete_friend(toxic, PendingDelete.num); sort_friendlist_index(); } else { - delete_blocked_friend(PendingDelete.num); + delete_blocked_friend(toxic, PendingDelete.num); sort_blocklist_index(); } } @@ -845,7 +881,7 @@ static void draw_del_popup(void) } /* deletes contact from blocked list */ -static void delete_blocked_friend(uint32_t bnum) +static void delete_blocked_friend(Toxic *toxic, uint32_t bnum) { clear_blocklist_index(bnum); @@ -860,7 +896,7 @@ static void delete_blocked_friend(uint32_t bnum) --Blocked.num_blocked; Blocked.max_idx = i; realloc_blocklist(i); - save_blocklist(BLOCK_FILE); + save_blocklist(toxic->client_data.block_path); if (Blocked.num_blocked && Blocked.num_selected == Blocked.num_blocked) { --Blocked.num_selected; @@ -868,7 +904,7 @@ static void delete_blocked_friend(uint32_t bnum) } /* deletes contact from friendlist and puts in blocklist */ -void block_friend(Tox *tox, uint32_t fnum) +static void block_friend(Toxic *toxic, uint32_t fnum) { if (Friends.num_friends == 0) { return; @@ -895,8 +931,8 @@ void block_friend(Tox *tox, uint32_t fnum) ++Blocked.max_idx; } - delete_friend(tox, fnum); - save_blocklist(BLOCK_FILE); + delete_friend(toxic, fnum); + save_blocklist(toxic->client_data.block_path); sort_blocklist_index(); sort_friendlist_index(); @@ -905,14 +941,14 @@ void block_friend(Tox *tox, uint32_t fnum) } /* removes friend from blocklist, puts back in friendlist */ -static void unblock_friend(Tox *tox, uint32_t bnum) +static void unblock_friend(Toxic *toxic, uint32_t bnum) { if (Blocked.num_blocked <= 0) { return; } Tox_Err_Friend_Add err; - uint32_t friendnum = tox_friend_add_norequest(tox, (uint8_t *) Blocked.list[bnum].pub_key, &err); + uint32_t friendnum = tox_friend_add_norequest(toxic->tox, (uint8_t *) Blocked.list[bnum].pub_key, &err); if (err != TOX_ERR_FRIEND_ADD_OK) { line_info_add(prompt, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to unblock friend (error %d)", err); @@ -920,7 +956,7 @@ static void unblock_friend(Tox *tox, uint32_t bnum) } friendlist_add_blocked(friendnum, bnum); - delete_blocked_friend(bnum); + delete_blocked_friend(toxic, bnum); sort_blocklist_index(); sort_friendlist_index(); } @@ -928,8 +964,13 @@ static void unblock_friend(Tox *tox, uint32_t bnum) /* * Return true if input is recognized by handler */ -static bool friendlist_onKey(ToxWindow *self, Tox *tox, wint_t key, bool ltr) +static bool friendlist_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) { + if (toxic == NULL || self == NULL) { + return false; + } + + Tox *tox = toxic->tox; if (self->help->active) { help_onKey(self, key); @@ -960,7 +1001,7 @@ static bool friendlist_onKey(ToxWindow *self, Tox *tox, wint_t key, bool ltr) /* lock screen and force decision on deletion popup */ if (PendingDelete.active) { if (key == L'y' || key == L'n') { - del_friend_deactivate(tox, key); + del_friend_deactivate(toxic, key); } return true; @@ -980,7 +1021,7 @@ static bool friendlist_onKey(ToxWindow *self, Tox *tox, wint_t key, bool ltr) if (Friends.list[f].chatwin != -1) { set_active_window_index(Friends.list[f].chatwin); } else if (get_num_active_windows() < MAX_WINDOWS_NUM) { - Friends.list[f].chatwin = add_window(tox, new_chat(tox, Friends.list[f].num)); + Friends.list[f].chatwin = add_window(toxic, new_chat(tox, Friends.list[f].num)); set_active_window_index(Friends.list[f].chatwin); } else { const char *msg = "* Warning: Too many windows are open."; @@ -996,9 +1037,9 @@ static bool friendlist_onKey(ToxWindow *self, Tox *tox, wint_t key, bool ltr) case L'b': if (!blocklist_view) { - block_friend(tox, f); + block_friend(toxic, f); } else { - unblock_friend(tox, f); + unblock_friend(toxic, f); } break; @@ -1328,9 +1369,13 @@ static void friendlist_onDraw(ToxWindow *self, Tox *tox) } } -void friendlist_onInit(ToxWindow *self, Tox *tox) +void friendlist_onInit(ToxWindow *self, Toxic *toxic) { - UNUSED_VAR(tox); + UNUSED_VAR(toxic); + + if (self == NULL) { + return; + } int x2; int y2; @@ -1353,7 +1398,7 @@ void disable_chatwin(uint32_t f_num) } #ifdef AUDIO -static void friendlist_onAV(ToxWindow *self, ToxAV *av, uint32_t friend_number, int state) +static void friendlist_onAV(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) { UNUSED_VAR(self); @@ -1361,12 +1406,12 @@ static void friendlist_onAV(ToxWindow *self, ToxAV *av, uint32_t friend_number, return; } - Tox *tox = toxav_get_tox(av); + Tox *tox = toxic->tox; if (Friends.list[friend_number].chatwin == -1) { if (get_num_active_windows() < MAX_WINDOWS_NUM) { if (state != TOXAV_FRIEND_CALL_STATE_FINISHED) { - Friends.list[friend_number].chatwin = add_window(tox, new_chat(tox, Friends.list[friend_number].num)); + Friends.list[friend_number].chatwin = add_window(toxic, new_chat(tox, Friends.list[friend_number].num)); set_active_window_index(Friends.list[friend_number].chatwin); } } else { diff --git a/src/friendlist.h b/src/friendlist.h index d78814462..4e6d3c139 100644 --- a/src/friendlist.h +++ b/src/friendlist.h @@ -113,7 +113,7 @@ typedef struct { extern FriendsList Friends; ToxWindow *new_friendlist(void); -void friendlist_onInit(ToxWindow *self, Tox *tox); +void friendlist_onInit(ToxWindow *self, Toxic *toxic); void disable_chatwin(uint32_t f_num); int get_friendnum(uint8_t *name); int load_blocklist(char *data); diff --git a/src/game_base.c b/src/game_base.c index 051ee6a9a..d39707915 100644 --- a/src/game_base.c +++ b/src/game_base.c @@ -250,7 +250,7 @@ static int game_initialize_type(GameData *game, const uint8_t *data, size_t leng return ret; } -int game_initialize(const ToxWindow *parent, Tox *tox, GameType type, uint32_t id, const uint8_t *multiplayer_data, +int game_initialize(const ToxWindow *parent, Toxic *toxic, GameType type, uint32_t id, const uint8_t *multiplayer_data, size_t length, bool self_host) { int max_x; @@ -259,7 +259,7 @@ int game_initialize(const ToxWindow *parent, Tox *tox, GameType type, uint32_t i max_y -= (CHATBOX_HEIGHT + WINDOW_BAR_HEIGHT); - ToxWindow *self = game_new_window(tox, type, parent->num); + ToxWindow *self = game_new_window(toxic->tox, type, parent->num); if (self == NULL) { return -4; @@ -267,7 +267,7 @@ int game_initialize(const ToxWindow *parent, Tox *tox, GameType type, uint32_t i GameData *game = self->game; - int window_id = add_window(tox, self); + const int window_id = add_window(toxic, self); if (window_id == -1) { free(game); @@ -291,7 +291,7 @@ int game_initialize(const ToxWindow *parent, Tox *tox, GameType type, uint32_t i game->is_multiplayer = true; } - game->tox = tox; + game->toxic = toxic; game->window_shape = GW_ShapeSquare; game->parent_max_x = max_x; game->parent_max_y = max_y; @@ -777,10 +777,10 @@ void game_onDraw(ToxWindow *self, Tox *tox) game_draw_messages(game, true); } -bool game_onKey(ToxWindow *self, Tox *tox, wint_t key, bool is_printable) +bool game_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool is_printable) { UNUSED_VAR(is_printable); - UNUSED_VAR(tox); + UNUSED_VAR(toxic); GameData *game = self->game; @@ -819,9 +819,13 @@ bool game_onKey(ToxWindow *self, Tox *tox, wint_t key, bool is_printable) return true; } -void game_onInit(ToxWindow *self, Tox *tox) +void game_onInit(ToxWindow *self, Toxic *toxic) { - UNUSED_VAR(tox); + UNUSED_VAR(toxic); + + if (self == NULL) { + return; + } int max_x; int max_y; @@ -1180,7 +1184,7 @@ int game_packet_send(const GameData *game, const uint8_t *data, size_t length, G Tox_Err_Friend_Custom_Packet err; - if (!tox_friend_send_lossless_packet(game->tox, game->friend_number, packet, packet_length, &err)) { + if (!tox_friend_send_lossless_packet(game->toxic->tox, game->friend_number, packet, packet_length, &err)) { fprintf(stderr, "failed to send game packet: error %d\n", err); return -1; } diff --git a/src/game_base.h b/src/game_base.h index cfb1823cc..afac26c89 100644 --- a/src/game_base.h +++ b/src/game_base.h @@ -154,7 +154,7 @@ struct GameData { int window_id; WINDOW *window; - Tox *tox; // must be locked with Winthread mutex + Toxic *toxic; // must be locked with Winthread mutex GameWindowShape window_shape; @@ -230,7 +230,7 @@ void game_set_cb_on_packet(GameData *game, cb_game_on_packet *func, void *cb_dat * Return -3 if multiplayer game is being initialized outside of a contact's window. * Return -4 on other failure. */ -int game_initialize(const ToxWindow *self, Tox *tox, GameType type, uint32_t id, const uint8_t *multiplayer_data, +int game_initialize(const ToxWindow *self, Toxic *toxic, GameType type, uint32_t id, const uint8_t *multiplayer_data, size_t length, bool self_host); /* diff --git a/src/global_commands.c b/src/global_commands.c index 6f746d0e1..40f241fcf 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -44,10 +44,16 @@ #endif /* command functions */ -void cmd_accept(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_accept(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (argc < 1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Request ID required."); return; @@ -73,7 +79,7 @@ void cmd_accept(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv return; } else { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Friend request accepted."); - on_friend_added(tox, friendnum, true); + on_friend_added(toxic, friendnum, true); } FrndRequests.request[req] = (struct friend_request) { @@ -92,12 +98,12 @@ void cmd_accept(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv --FrndRequests.num_requests; } -void cmd_add_helper(ToxWindow *self, Tox *tox, const char *id_bin, const char *msg) +void cmd_add_helper(ToxWindow *self, Toxic *toxic, const char *id_bin, const char *msg) { const char *errmsg; Tox_Err_Friend_Add err; - uint32_t f_num = tox_friend_add(tox, (const uint8_t *) id_bin, (const uint8_t *) msg, strlen(msg), &err); + uint32_t f_num = tox_friend_add(toxic->tox, (const uint8_t *) id_bin, (const uint8_t *) msg, strlen(msg), &err); switch (err) { case TOX_ERR_FRIEND_ADD_TOO_LONG: @@ -130,7 +136,7 @@ void cmd_add_helper(ToxWindow *self, Tox *tox, const char *id_bin, const char *m case TOX_ERR_FRIEND_ADD_OK: errmsg = "Friend request sent."; - on_friend_added(tox, f_num, true); + on_friend_added(toxic, f_num, true); break; case TOX_ERR_FRIEND_ADD_NULL: @@ -144,10 +150,16 @@ void cmd_add_helper(ToxWindow *self, Tox *tox, const char *id_bin, const char *m line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, errmsg); } -void cmd_add(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_add(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (argc < 1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Tox ID or address required."); return; @@ -177,7 +189,7 @@ void cmd_add(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[M const bool valid_id_size = arg_length >= TOX_ADDRESS_SIZE * 2; // arg_length may include invite message if (is_domain) { - if (!name_lookup(self, tox, id_bin, id, msg)) { + if (!name_lookup(self, toxic, id_bin, id, msg)) { return; } } else if (!valid_id_size) { @@ -206,13 +218,19 @@ void cmd_add(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[M return; } - cmd_add_helper(self, tox, id_bin, msg); + cmd_add_helper(self, toxic, id_bin, msg); } -void cmd_avatar(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_avatar(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (argc != 1 || strlen(argv[1]) < 3) { avatar_unset(tox); line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Avatar has been unset."); @@ -242,20 +260,30 @@ void cmd_avatar(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Avatar set to '%s'", filename); } -void cmd_clear(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_clear(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { - UNUSED_VAR(tox); + UNUSED_VAR(toxic); UNUSED_VAR(argc); UNUSED_VAR(argv); + if (self == NULL) { + return; + } + line_info_clear(self->chatwin->hst); force_refresh(window); } -void cmd_connect(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_connect(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (argc != 3) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Require: "); return; @@ -301,10 +329,14 @@ void cmd_connect(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*arg } } -void cmd_decline(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_decline(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); + UNUSED_VAR(toxic); + + if (self == NULL) { + return; + } if (argc < 1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Request ID required."); @@ -341,10 +373,14 @@ void cmd_decline(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*arg #ifdef GAMES -void cmd_game(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_game(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); + if (toxic == NULL || self == NULL) { + return; + } + if (argc < 1) { game_list_print(self); return; @@ -363,7 +399,7 @@ void cmd_game(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[ } const unsigned int id = rand_not_secure(); - const int ret = game_initialize(self, tox, type, id, NULL, 0, true); + const int ret = game_initialize(self, toxic, type, id, NULL, 0, true); switch (ret) { case 0: { @@ -396,10 +432,16 @@ void cmd_game(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[ #endif // GAMES -void cmd_conference(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_conference(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (get_num_active_windows() >= MAX_WINDOWS_NUM) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, " * Warning: Too many windows are open."); return; @@ -447,7 +489,7 @@ void cmd_conference(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (* #endif } - if (init_conference_win(tox, conferencenum, type, NULL, 0) == -1) { + if (init_conference_win(toxic, conferencenum, type, NULL, 0) == -1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Conference window failed to initialize."); tox_conference_delete(tox, conferencenum, NULL); return; @@ -466,8 +508,15 @@ void cmd_conference(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (* line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Conference [%d] created.", conferencenum); } -void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_groupchat(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (get_num_active_windows() >= MAX_WINDOWS_NUM) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, " * Warning: Too many windows are open."); return; @@ -521,7 +570,7 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*a return; } - int init = init_groupchat_win(tox, groupnumber, name, len, Group_Join_Type_Create); + const int init = init_groupchat_win(toxic, groupnumber, name, len, Group_Join_Type_Create); if (init == -1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Group chat window failed to initialize."); @@ -533,8 +582,14 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*a } } -void cmd_join(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_join(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (get_num_active_windows() >= MAX_WINDOWS_NUM) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, " * Warning: Too many windows are open."); return; @@ -598,7 +653,7 @@ void cmd_join(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[ return; } - int init = init_groupchat_win(tox, groupnumber, NULL, 0, Group_Join_Type_Join); + const int init = init_groupchat_win(toxic, groupnumber, NULL, 0, Group_Join_Type_Join); if (init == -1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Group chat window failed to initialize."); @@ -606,9 +661,14 @@ void cmd_join(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[ } } -void cmd_log(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_log(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); + UNUSED_VAR(toxic); + + if (self == NULL) { + return; + } const char *msg; struct chatlog *log = self->chatwin->log; @@ -646,15 +706,19 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[M line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, msg); } -void cmd_myid(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_myid(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); UNUSED_VAR(argc); UNUSED_VAR(argv); + if (toxic == NULL || self == NULL) { + return; + } + char id_string[TOX_ADDRESS_SIZE * 2 + 1]; char bin_id[TOX_ADDRESS_SIZE]; - tox_self_get_address(tox, (uint8_t *) bin_id); + tox_self_get_address(toxic->tox, (uint8_t *) bin_id); if (tox_id_bytes_to_str(bin_id, sizeof(bin_id), id_string, sizeof(id_string)) == -1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to print ID."); @@ -665,10 +729,16 @@ void cmd_myid(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[ } #ifdef QRCODE -void cmd_myqr(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_myqr(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + char id_string[TOX_ADDRESS_SIZE * 2 + 1]; char bin_id[TOX_ADDRESS_SIZE]; tox_self_get_address(tox, (uint8_t *) bin_id); @@ -683,7 +753,7 @@ void cmd_myqr(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[ size_t nick_len = tox_self_get_name_size(tox); nick[nick_len] = '\0'; - size_t data_file_len = strlen(DATA_FILE); + const size_t data_file_len = strlen(toxic->client_data.data_path); char *dir = malloc(data_file_len + 1); if (dir == NULL) { @@ -691,7 +761,7 @@ void cmd_myqr(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[ return; } - size_t dir_len = get_base_dir(DATA_FILE, data_file_len, dir); + const size_t dir_len = get_base_dir(toxic->client_data.data_path, data_file_len, dir); #ifdef QRPNG @@ -760,10 +830,16 @@ void cmd_myqr(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[ } #endif /* QRCODE */ -void cmd_nick(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_nick(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (argc < 1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Input required."); return; @@ -784,20 +860,31 @@ void cmd_nick(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[ tox_self_set_name(tox, (uint8_t *) nick, len, NULL); prompt_update_nick(prompt, nick); - store_data(tox, DATA_FILE); + store_data(toxic); } -void cmd_note(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_note(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); + UNUSED_VAR(self); + + if (toxic == NULL || self == NULL) { + return; + } const char *note = argc >= 1 ? argv[1] : ""; - prompt_update_statusmessage(prompt, tox, note); + prompt_update_statusmessage(prompt, toxic->tox, note); } -void cmd_nospam(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_nospam(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + long int nospam = (long int)rand_not_secure(); // the nospam isn't cryptographically sensitive if (argc > 0) { @@ -813,7 +900,7 @@ void cmd_nospam(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv tox_self_set_nospam(tox, (uint32_t) nospam); line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Your new Tox ID is:"); - cmd_myid(window, self, tox, 0, NULL); + cmd_myid(window, self, toxic, 0, NULL); line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, ""); line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Any services that relied on your old ID will need to be updated manually."); @@ -821,33 +908,41 @@ void cmd_nospam(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv old_nospam); } -void cmd_prompt_help(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_prompt_help(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); + UNUSED_VAR(toxic); UNUSED_VAR(argc); UNUSED_VAR(argv); + if (self == NULL) { + return; + } + help_init_menu(self); } -void cmd_quit(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_quit(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); UNUSED_VAR(argc); UNUSED_VAR(argv); UNUSED_VAR(self); - exit_toxic_success(tox); + exit_toxic_success(toxic); } -void cmd_requests(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_requests(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); + UNUSED_VAR(toxic); UNUSED_VAR(argc); UNUSED_VAR(argv); + if (self == NULL) { + return; + } + if (FrndRequests.num_requests == 0) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending friend requests."); return; @@ -878,10 +973,16 @@ void cmd_requests(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*ar } } -void cmd_status(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_status(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + const char *errmsg; lock_status(); diff --git a/src/global_commands.h b/src/global_commands.h index b023d4811..e7f44b6c4 100644 --- a/src/global_commands.h +++ b/src/global_commands.h @@ -26,46 +26,46 @@ #include "toxic.h" #include "windows.h" -void cmd_accept(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_add(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_avatar(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_clear(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_conference(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 *tox, 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 *tox, 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_accept(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_add(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_avatar(WINDOW *window, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_clear(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_conference(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_connect(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_decline(WINDOW *window, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_groupchat(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_join(WINDOW *window, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_log(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_myid(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); #ifdef QRCODE -void cmd_myqr(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_myqr(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]); #endif /* QRCODE */ -void cmd_nick(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_note(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_nospam(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_prompt_help(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_quit(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_requests(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_status(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_nick(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_note(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_nospam(WINDOW *window, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_prompt_help(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_quit(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_requests(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_status(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_add_helper(ToxWindow *self, Tox *tox, const char *id_bin, const char *msg); +void cmd_add_helper(ToxWindow *self, Toxic *, const char *id_bin, const char *msg); #ifdef AUDIO -void cmd_list_devices(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_change_device(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_list_devices(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_change_device(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); #endif /* AUDIO */ #ifdef VIDEO -void cmd_list_video_devices(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_change_video_device(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_list_video_devices(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_change_video_device(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); #endif /* VIDEO */ #ifdef PYTHON -void cmd_run(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_run(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); #endif /* PYTHON */ #ifdef GAMES -void cmd_game(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_game(WINDOW *window, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); #endif /* GAMES */ #endif /* GLOBAL_COMMANDS_H */ diff --git a/src/groupchat_commands.c b/src/groupchat_commands.c index 2be95e911..f445985cd 100644 --- a/src/groupchat_commands.c +++ b/src/groupchat_commands.c @@ -32,14 +32,20 @@ extern GroupChat groupchats[MAX_GROUPCHAT_NUM]; -void cmd_chatid(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_chatid(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + UNUSED_VAR(window); + + if (toxic == NULL || self == NULL) { + return; + } + char chatid[TOX_GROUP_CHAT_ID_SIZE * 2 + 1] = {0}; char chat_public_key[TOX_GROUP_CHAT_ID_SIZE]; Tox_Err_Group_State_Queries err; - if (!tox_group_get_chat_id(tox, self->num, (uint8_t *) chat_public_key, &err)) { + if (!tox_group_get_chat_id(toxic->tox, self->num, (uint8_t *) chat_public_key, &err)) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve the Chat ID (error %d).", err); return; } @@ -53,10 +59,16 @@ void cmd_chatid(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", chatid); } -void cmd_disconnect(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_disconnect(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + UNUSED_VAR(window); + + if (toxic == NULL || self == NULL) { + return; + } + Tox_Err_Group_Disconnect err; - tox_group_disconnect(tox, self->num, &err); + tox_group_disconnect(toxic->tox, self->num, &err); switch (err) { case TOX_ERR_GROUP_DISCONNECT_OK: { @@ -76,10 +88,14 @@ void cmd_disconnect(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (* } } -void cmd_group_nick(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_group_nick(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); + if (toxic == NULL || self == NULL) { + return; + } + if (argc < 1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Input required."); return; @@ -97,13 +113,19 @@ void cmd_group_nick(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (* len = MIN(len, TOXIC_MAX_NAME_LENGTH - 1); nick[len] = '\0'; - set_nick_this_group(self, tox, nick, len); + set_nick_this_group(self, toxic->tox, nick, len); - store_data(tox, DATA_FILE); + store_data(toxic); } -void cmd_ignore(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_ignore(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + UNUSED_VAR(window); + + if (toxic == NULL || self == NULL) { + return; + } + if (argc < 1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); return; @@ -117,7 +139,7 @@ void cmd_ignore(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv } Tox_Err_Group_Set_Ignore err; - tox_group_set_ignore(tox, self->num, peer_id, true, &err); + tox_group_set_ignore(toxic->tox, self->num, peer_id, true, &err); switch (err) { case TOX_ERR_GROUP_SET_IGNORE_OK: { @@ -145,8 +167,16 @@ void cmd_ignore(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv group_toggle_peer_ignore(self->num, peer_id, true); } -void cmd_kick(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_kick(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + UNUSED_VAR(window); + + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (argc < 1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); return; @@ -196,8 +226,15 @@ void cmd_kick(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[ } } -void cmd_list(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_list(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + UNUSED_VAR(window); + UNUSED_VAR(toxic); + + if (self == NULL) { + return; + } + GroupChat *chat = get_groupchat(self->num); if (!chat) { @@ -224,8 +261,16 @@ void cmd_list(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[ } } -void cmd_mod(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_mod(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + UNUSED_VAR(window); + + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (argc < 1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); return; @@ -282,8 +327,16 @@ void cmd_mod(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[M } } -void cmd_unmod(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_unmod(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + UNUSED_VAR(window); + + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (argc < 1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); return; @@ -341,8 +394,14 @@ void cmd_unmod(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv) } } -void cmd_set_passwd(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_set_passwd(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + UNUSED_VAR(window); + + if (toxic == NULL || self == NULL) { + return; + } + const char *passwd = NULL; size_t len = 0; @@ -352,7 +411,7 @@ void cmd_set_passwd(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (* } Tox_Err_Group_Founder_Set_Password err; - tox_group_founder_set_password(tox, self->num, (uint8_t *) passwd, len, &err); + tox_group_founder_set_password(toxic->tox, self->num, (uint8_t *) passwd, len, &err); switch (err) { case TOX_ERR_GROUP_FOUNDER_SET_PASSWORD_OK: { @@ -383,8 +442,16 @@ void cmd_set_passwd(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (* } } -void cmd_set_peerlimit(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_set_peerlimit(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + UNUSED_VAR(window); + + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + int maxpeers = 0; if (argc < 1) { @@ -428,8 +495,16 @@ void cmd_set_peerlimit(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char } } -void cmd_set_voice(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_set_voice(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + UNUSED_VAR(window); + + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + Tox_Group_Voice_State voice_state; if (argc < 1) { @@ -500,8 +575,16 @@ void cmd_set_voice(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*a } } -void cmd_set_privacy(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_set_privacy(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + UNUSED_VAR(window); + + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + const char *pstate_str = NULL; Tox_Group_Privacy_State privacy_state; @@ -550,8 +633,16 @@ void cmd_set_privacy(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char ( } } -void cmd_set_topic_lock(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_set_topic_lock(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + UNUSED_VAR(window); + + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + Tox_Group_Topic_Lock topic_lock; const char *tlock_str = NULL; @@ -600,8 +691,16 @@ void cmd_set_topic_lock(WINDOW *window, ToxWindow *self, Tox *tox, int argc, cha } } -void cmd_silence(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_silence(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + UNUSED_VAR(window); + + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (argc < 1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); return; @@ -658,8 +757,16 @@ void cmd_silence(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*arg } } -void cmd_unsilence(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_unsilence(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + UNUSED_VAR(window); + + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (argc < 1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); return; @@ -721,8 +828,16 @@ void cmd_unsilence(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*a } } -void cmd_rejoin(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_rejoin(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + UNUSED_VAR(window); + + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + Tox_Err_Group_Reconnect err; if (!tox_group_reconnect(tox, self->num, &err)) { @@ -735,8 +850,16 @@ void cmd_rejoin(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv groupchat_rejoin(self, tox); } -void cmd_set_topic(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_set_topic(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + UNUSED_VAR(window); + + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (argc < 1) { Tox_Err_Group_State_Queries err; size_t tlen = tox_group_get_topic_size(tox, self->num, &err); @@ -805,8 +928,14 @@ void cmd_set_topic(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*a write_to_log(tmp_event, self_nick, self->chatwin->log, true); } -void cmd_unignore(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_unignore(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + UNUSED_VAR(window); + + if (toxic == NULL || self == NULL) { + return; + } + if (argc < 1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); return; @@ -820,7 +949,7 @@ void cmd_unignore(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*ar } Tox_Err_Group_Set_Ignore err; - tox_group_set_ignore(tox, self->num, peer_id, false, &err); + tox_group_set_ignore(toxic->tox, self->num, peer_id, false, &err); switch (err) { case TOX_ERR_GROUP_SET_IGNORE_OK: { @@ -848,8 +977,16 @@ void cmd_unignore(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*ar group_toggle_peer_ignore(self->num, peer_id, false); } -void cmd_whois(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_whois(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { + UNUSED_VAR(window); + + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (argc < 1) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); return; diff --git a/src/groupchat_commands.h b/src/groupchat_commands.h index 7a2992a79..4850aac52 100644 --- a/src/groupchat_commands.h +++ b/src/groupchat_commands.h @@ -26,26 +26,25 @@ #include "windows.h" #include "toxic.h" -void cmd_chatid(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_disconnect(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_group_nick(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_ignore(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_kick(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_list(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_mod(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_prune(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_set_passwd(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_set_peerlimit(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_set_privacy(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_set_voice(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_set_topic_lock(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_rejoin(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_set_topic(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_silence(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_unsilence(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_unignore(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_unmod(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_whois(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_chatid(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_disconnect(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_group_nick(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_ignore(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_kick(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_list(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_mod(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_prune(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_set_passwd(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_set_peerlimit(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_set_privacy(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_set_voice(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_set_topic_lock(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_rejoin(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_set_topic(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_silence(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_unsilence(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_unignore(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_unmod(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_whois(WINDOW *, ToxWindow *, Toxic *, int argc, char (*argv)[MAX_STR_SIZE]); #endif /* GROUPCHAT_COMMANDS_H */ - diff --git a/src/groupchats.c b/src/groupchats.c index a956cf4ac..ec33627e5 100644 --- a/src/groupchats.c +++ b/src/groupchats.c @@ -121,7 +121,7 @@ static const char *group_cmd_list[] = { GroupChat groupchats[MAX_GROUPCHAT_NUM]; static ToxWindow *new_group_chat(Tox *tox, uint32_t groupnumber, const char *groupname, int length); -static void groupchat_set_group_name(ToxWindow *self, Tox *tox, uint32_t groupnumber); +static void groupchat_set_group_name(ToxWindow *self, Toxic *toxic, uint32_t groupnumber); static void group_update_name_list(uint32_t groupnumber); static void groupchat_onGroupPeerJoin(ToxWindow *self, Tox *tox, uint32_t groupnumber, uint32_t peer_id); static int realloc_peer_list(uint32_t groupnumber, uint32_t n); @@ -280,8 +280,14 @@ void exit_groupchat(ToxWindow *self, Tox *tox, uint32_t groupnumber, const char /* * Initializes groupchat log. This should only be called after we have the group name. */ -static void init_groupchat_log(ToxWindow *self, Tox *tox, uint32_t groupnumber) +static void init_groupchat_log(ToxWindow *self, Toxic *toxic, uint32_t groupnumber) { + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + GroupChat *chat = get_groupchat(groupnumber); if (!chat) { @@ -317,7 +323,7 @@ static void init_groupchat_log(ToxWindow *self, Tox *tox, uint32_t groupnumber) } } - execute(ctx->history, self, tox, "/log", GLOBAL_COMMAND_MODE); // print log state to screen + execute(ctx->history, self, toxic, "/log", GLOBAL_COMMAND_MODE); // print log state to screen } /* Creates a new toxic groupchat window associated with groupnumber. @@ -327,13 +333,20 @@ static void init_groupchat_log(ToxWindow *self, Tox *tox, uint32_t groupnumber) * Returns -2 if the groupnumber is already in use. This usually means that the client has * been kicked and needs to close the chat window before opening a new one. */ -int init_groupchat_win(Tox *tox, uint32_t groupnumber, const char *groupname, size_t length, Group_Join_Type join_type) +int init_groupchat_win(Toxic *toxic, uint32_t groupnumber, const char *groupname, size_t length, + Group_Join_Type join_type) { + if (toxic == NULL) { + return -1; + } + + Tox *tox = toxic->tox; + ToxWindow *self = new_group_chat(tox, groupnumber, groupname, length); for (int i = 0; i <= max_groupchat_index; ++i) { if (!groupchats[i].active) { - groupchats[i].chatwin = add_window(tox, self); + groupchats[i].chatwin = add_window(toxic, self); groupchats[i].active = true; groupchats[i].groupnumber = groupnumber; groupchats[i].num_peers = 0; @@ -344,7 +357,7 @@ int init_groupchat_win(Tox *tox, uint32_t groupnumber, const char *groupname, si } set_active_window_index(groupchats[i].chatwin); - store_data(tox, DATA_FILE); + store_data(toxic); Tox_Err_Group_Self_Query err; uint32_t peer_id = tox_group_self_get_peer_id(tox, groupnumber, &err); @@ -355,7 +368,7 @@ int init_groupchat_win(Tox *tox, uint32_t groupnumber, const char *groupname, si } if (join_type == Group_Join_Type_Create || join_type == Group_Join_Type_Load) { - groupchat_set_group_name(self, tox, groupnumber); + groupchat_set_group_name(self, toxic, groupnumber); } groupchat_onGroupPeerJoin(self, tox, groupnumber, peer_id); @@ -1249,8 +1262,14 @@ void groupchat_onGroupPeerExit(ToxWindow *self, Tox *tox, uint32_t groupnumber, group_update_name_list(groupnumber); } -static void groupchat_set_group_name(ToxWindow *self, Tox *tox, uint32_t groupnumber) +static void groupchat_set_group_name(ToxWindow *self, Toxic *toxic, uint32_t groupnumber) { + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + GroupChat *chat = get_groupchat(groupnumber); if (!chat) { @@ -1277,12 +1296,18 @@ static void groupchat_set_group_name(ToxWindow *self, Tox *tox, uint32_t groupnu if (len > 0) { set_window_title(self, chat->group_name, len); - init_groupchat_log(self, tox, groupnumber); + init_groupchat_log(self, toxic, groupnumber); } } -static void groupchat_onGroupSelfJoin(ToxWindow *self, Tox *tox, uint32_t groupnumber) +static void groupchat_onGroupSelfJoin(ToxWindow *self, Toxic *toxic, uint32_t groupnumber) { + if (toxic == NULL || self == NULL) { + return; + } + + Tox *tox = toxic->tox; + if (self->num != groupnumber) { return; } @@ -1316,7 +1341,7 @@ static void groupchat_onGroupSelfJoin(ToxWindow *self, Tox *tox, uint32_t groupn line_info_add(self, true, NULL, NULL, SYS_MSG, 1, MAGENTA, "-!- Topic set to: %s", topic); if (chat->group_name_length == 0) { - groupchat_set_group_name(self, tox, groupnumber); + groupchat_set_group_name(self, toxic, groupnumber); } /* Update own role since it may have changed while we were offline */ @@ -1672,8 +1697,14 @@ static void send_group_prvt_message(ToxWindow *self, Tox *tox, uint32_t groupnum /* * Return true if input is recognized by handler */ -static bool groupchat_onKey(ToxWindow *self, Tox *tox, wint_t key, bool ltr) +static bool groupchat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) { + if (self == NULL || toxic == NULL) { + return false; + } + + Tox *tox = toxic->tox; + ChatContext *ctx = self->chatwin; GroupChat *chat = get_groupchat(self->num); @@ -1726,7 +1757,7 @@ static bool groupchat_onKey(ToxWindow *self, Tox *tox, wint_t key, bool ltr) if (ctx->line[0] != L'/' || wcschr(ctx->line, L' ') != NULL) { diff = complete_line(self, (const char **) chat->name_list, chat->num_peers); } else if (wcsncmp(ctx->line, L"/avatar \"", wcslen(L"/avatar \"")) == 0) { - diff = dir_match(self, tox, ctx->line, L"/avatar"); + diff = dir_match(self, toxic, ctx->line, L"/avatar"); } else { diff = complete_line(self, group_cmd_list, sizeof(group_cmd_list) / sizeof(char *)); } @@ -1793,7 +1824,7 @@ static bool groupchat_onKey(ToxWindow *self, Tox *tox, wint_t key, bool ltr) } else if (strncmp(line, "/whisper ", strlen("/whisper ")) == 0) { send_group_prvt_message(self, tox, self->num, line + 9, ctx->len - 9); } else { - execute(ctx->history, self, tox, line, GROUPCHAT_COMMAND_MODE); + execute(ctx->history, self, toxic, line, GROUPCHAT_COMMAND_MODE); } } else if (line[0]) { send_group_message(self, tox, self->num, line, TOX_MESSAGE_TYPE_NORMAL); @@ -1966,8 +1997,14 @@ static void groupchat_onDraw(ToxWindow *self, Tox *tox) } } -static void groupchat_onInit(ToxWindow *self, Tox *tox) +static void groupchat_onInit(ToxWindow *self, Toxic *toxic) { + UNUSED_VAR(toxic); + + if (self == NULL) { + return; + } + int x2, y2; getmaxyx(self->window, y2, x2); diff --git a/src/groupchats.h b/src/groupchats.h index 087d942fc..5fa4f7f89 100644 --- a/src/groupchats.h +++ b/src/groupchats.h @@ -71,7 +71,7 @@ typedef struct { } GroupChat; void exit_groupchat(ToxWindow *self, Tox *tox, uint32_t groupnumber, const char *partmessage, size_t length); -int init_groupchat_win(Tox *tox, uint32_t groupnumber, const char *groupname, size_t length, Group_Join_Type join_type); +int init_groupchat_win(Toxic *toxic, uint32_t groupnumber, const char *groupname, size_t length, Group_Join_Type join_type); void set_nick_this_group(ToxWindow *self, Tox *tox, const char *new_nick, size_t length); void set_status_all_groups(Tox *tox, uint8_t status); int get_peer_index(uint32_t groupnumber, uint32_t peer_id); diff --git a/src/name_lookup.c b/src/name_lookup.c index ab5f8de9f..f87f30fb5 100644 --- a/src/name_lookup.c +++ b/src/name_lookup.c @@ -46,7 +46,7 @@ static struct Nameservers { } Nameservers; static struct thread_data { - Tox *tox; + Toxic *toxic; ToxWindow *self; char id_bin[TOX_ADDRESS_SIZE]; char addr[MAX_STR_SIZE]; @@ -386,7 +386,7 @@ void *lookup_thread_func(void *data) } pthread_mutex_lock(&Winthread.lock); - cmd_add_helper(self, t_data.tox, t_data.id_bin, t_data.msg); + cmd_add_helper(self, t_data.toxic, t_data.id_bin, t_data.msg); pthread_mutex_unlock(&Winthread.lock); on_exit: @@ -402,7 +402,7 @@ void *lookup_thread_func(void *data) * * Returns true on success. */ -bool name_lookup(ToxWindow *self, Tox *tox, const char *id_bin, const char *addr, const char *message) +bool name_lookup(ToxWindow *self, Toxic *toxic, const char *id_bin, const char *addr, const char *message) { if (t_data.disabled) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "nameservers list is empty or does not exist."); @@ -418,7 +418,7 @@ bool name_lookup(ToxWindow *self, Tox *tox, const char *id_bin, const char *addr snprintf(t_data.addr, sizeof(t_data.addr), "%s", addr); snprintf(t_data.msg, sizeof(t_data.msg), "%s", message); t_data.self = self; - t_data.tox = tox; + t_data.toxic = toxic; t_data.busy = true; if (pthread_attr_init(&lookup_thread.attr) != 0) { diff --git a/src/name_lookup.h b/src/name_lookup.h index 5833a1370..c01c7f31c 100644 --- a/src/name_lookup.h +++ b/src/name_lookup.h @@ -36,6 +36,6 @@ int name_lookup_init(int curl_init_status); * * Returns true on success. */ -bool name_lookup(ToxWindow *self, Tox *tox, const char *id_bin, const char *addr, const char *message); +bool name_lookup(ToxWindow *self, Toxic *toxic, const char *id_bin, const char *addr, const char *message); #endif /* NAME_LOOKUP */ diff --git a/src/prompt.c b/src/prompt.c index fefc0c065..18a16be17 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -199,8 +199,12 @@ static int add_friend_request(const char *public_key, const char *data) /* * Return true if input is recognized by handler */ -static bool prompt_onKey(ToxWindow *self, Tox *tox, wint_t key, bool ltr) +static bool prompt_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) { + if (toxic == NULL || self == NULL) { + return false; + } + ChatContext *ctx = self->chatwin; int x, y, y2, x2; @@ -245,12 +249,12 @@ static bool prompt_onKey(ToxWindow *self, Tox *tox, wint_t key, bool ltr) int diff = -1; if (wcsncmp(ctx->line, L"/avatar ", wcslen(L"/avatar ")) == 0) { - diff = dir_match(self, tox, ctx->line, L"/avatar"); + diff = dir_match(self, toxic, ctx->line, L"/avatar"); } #ifdef PYTHON else if (wcsncmp(ctx->line, L"/run ", wcslen(L"/run ")) == 0) { - diff = dir_match(self, tox, ctx->line, L"/run"); + diff = dir_match(self, toxic, ctx->line, L"/run"); } #endif @@ -295,7 +299,7 @@ static bool prompt_onKey(ToxWindow *self, Tox *tox, wint_t key, bool ltr) line_info_add(self, false, NULL, NULL, PROMPT, 0, 0, "%s", line); } - execute(ctx->history, self, tox, line, GLOBAL_COMMAND_MODE); + execute(ctx->history, self, toxic, line, GLOBAL_COMMAND_MODE); } } @@ -482,12 +486,17 @@ static void prompt_onDraw(ToxWindow *self, Tox *tox) } } -static void prompt_onConnectionChange(ToxWindow *self, Tox *tox, uint32_t friendnum, Tox_Connection connection_status) +static void prompt_onConnectionChange(ToxWindow *self, Toxic *toxic, uint32_t friendnum, + Tox_Connection connection_status) { + if (toxic == NULL || self == NULL) { + return; + } + ChatContext *ctx = self->chatwin; char nick[TOX_MAX_NAME_LENGTH] = {0}; /* stop removing this initiation */ - get_nick_truncate(tox, nick, friendnum); + get_nick_truncate(toxic->tox, nick, friendnum); if (!nick[0]) { snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME); @@ -654,10 +663,14 @@ static void prompt_init_log(ToxWindow *self, Tox *tox, const char *self_name) } } -static void prompt_onInit(ToxWindow *self, Tox *tox) +static void prompt_onInit(ToxWindow *self, Toxic *toxic) { curs_set(1); + if (toxic == NULL || self == NULL) { + return; + } + int y2; int x2; getmaxyx(self->window, y2, x2); @@ -681,7 +694,7 @@ static void prompt_onInit(ToxWindow *self, Tox *tox) line_info_init(ctx->hst); - prompt_init_log(self, tox, self->name); + prompt_init_log(self, toxic->tox, self->name); scrollok(ctx->history, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0); diff --git a/src/python_api.c b/src/python_api.c index e17b4870c..11eb79440 100644 --- a/src/python_api.c +++ b/src/python_api.c @@ -27,7 +27,7 @@ #include "execute.h" -extern Tox *user_tox; +extern Toxic *user_toxic; static struct python_registered_func { char *name; @@ -279,9 +279,9 @@ void terminate_python(void) Py_Finalize(); } -void init_python(Tox *tox) +void init_python(Toxic *toxic) { - user_tox = tox; + user_toxic = toxic; PyImport_AppendInittab("toxic_api", PyInit_toxic_api); Py_Initialize(); } diff --git a/src/term_mplex.c b/src/term_mplex.c index 6542d0e51..9a6e0b277 100644 --- a/src/term_mplex.c +++ b/src/term_mplex.c @@ -377,7 +377,7 @@ static int mplex_is_detached(void) return gnu_screen_is_detached() || tmux_is_detached(); } -static void mplex_timer_handler(Tox *tox) +static void mplex_timer_handler(Toxic *toxic) { Tox_User_Status current_status, new_status; const char *new_note; @@ -389,7 +389,7 @@ static void mplex_timer_handler(Tox *tox) int detached = mplex_is_detached(); pthread_mutex_lock(&Winthread.lock); - current_status = tox_self_get_status(tox); + current_status = tox_self_get_status(toxic->tox); pthread_mutex_unlock(&Winthread.lock); if (auto_away_active && current_status == TOX_USER_STATUS_AWAY && !detached) { @@ -401,8 +401,8 @@ static void mplex_timer_handler(Tox *tox) prev_status = current_status; new_status = TOX_USER_STATUS_AWAY; pthread_mutex_lock(&Winthread.lock); - size_t slen = tox_self_get_status_message_size(tox); - tox_self_get_status_message(tox, (uint8_t *) prev_note); + size_t slen = tox_self_get_status_message_size(toxic->tox); + tox_self_get_status_message(toxic->tox, (uint8_t *) prev_note); prev_note[slen] = '\0'; pthread_mutex_unlock(&Winthread.lock); new_note = user_settings->mplex_away_note; @@ -418,8 +418,8 @@ static void mplex_timer_handler(Tox *tox) snprintf(note_str, sizeof(status_str), "/note %s", new_note); pthread_mutex_lock(&Winthread.lock); - execute(prompt->chatwin->history, prompt, tox, status_str, GLOBAL_COMMAND_MODE); - execute(prompt->chatwin->history, prompt, tox, note_str, GLOBAL_COMMAND_MODE); + execute(prompt->chatwin->history, prompt, toxic, status_str, GLOBAL_COMMAND_MODE); + execute(prompt->chatwin->history, prompt, toxic, note_str, GLOBAL_COMMAND_MODE); pthread_mutex_unlock(&Winthread.lock); } @@ -428,21 +428,21 @@ static void mplex_timer_handler(Tox *tox) void *mplex_timer_thread(void *data) { - Tox *tox = (Tox *) data; + Toxic *toxic = (Toxic *) data; while (true) { sleep(MPLEX_TIMER_INTERVAL); - mplex_timer_handler(tox); + mplex_timer_handler(toxic); } } -int init_mplex_away_timer(Tox *tox) +int init_mplex_away_timer(Toxic *toxic) { - if (! detect_mplex()) { + if (!detect_mplex()) { return 0; } - if (! user_settings->mplex_away) { + if (!user_settings->mplex_away) { return 0; } @@ -451,7 +451,7 @@ int init_mplex_away_timer(Tox *tox) return -1; } - if (pthread_create(&mplex_tid, NULL, mplex_timer_thread, (void *)tox) != 0) { + if (pthread_create(&mplex_tid, NULL, mplex_timer_thread, (void *)toxic) != 0) { return -1; } diff --git a/src/term_mplex.h b/src/term_mplex.h index 693958f76..7aaa9d82a 100644 --- a/src/term_mplex.h +++ b/src/term_mplex.h @@ -27,7 +27,7 @@ * yes, it initializes a timer which periodically checks the attached/detached * state of the terminal and updates away status accordingly. */ -int init_mplex_away_timer(Tox *tox); +int init_mplex_away_timer(Toxic *toxic); void lock_status(void); void unlock_status(void); diff --git a/src/toxic.c b/src/toxic.c index 8ef714858..b305a3529 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -76,7 +76,6 @@ #ifdef VIDEO #include "video_call.h" #endif /* VIDEO */ -static ToxAV *av; #endif /* AUDIO */ #ifdef PYTHON @@ -89,16 +88,11 @@ static ToxAV *av; #endif /* Export for use in Callbacks */ -char *DATA_FILE = NULL; -char *BLOCK_FILE = NULL; ToxWindow *prompt = NULL; #define DATANAME "toxic_profile.tox" #define BLOCKNAME "toxic_blocklist" -#define MIN_PASSWORD_LEN 6 -#define MAX_PASSWORD_LEN 64 - struct Winthread Winthread; static struct cqueue_thread cqueue_thread; struct arg_opts arg_opts; @@ -107,20 +101,10 @@ struct arg_opts arg_opts; static struct av_thread av_thread; #endif -typedef struct Toxic { - Tox *tox; -} Toxic; - // This struct is not thread safe. It should only ever be written to from the main thread // before any other thread that uses it is initialized. struct user_settings *user_settings = NULL; -static struct user_password { - bool data_is_encrypted; - char pass[MAX_PASSWORD_LEN + 1]; - int len; -} user_password; - static time_t last_signal_time; static void catch_SIGINT(int sig) @@ -165,40 +149,39 @@ static void init_signal_catchers(void) void free_global_data(void) { - if (DATA_FILE) { - free(DATA_FILE); - DATA_FILE = NULL; - } - - if (BLOCK_FILE) { - free(BLOCK_FILE); - BLOCK_FILE = NULL; - } - if (user_settings) { free(user_settings); user_settings = NULL; } } -void exit_toxic_success(Tox *tox) +void kill_toxic(Toxic *toxic) { - store_data(tox, DATA_FILE); + Client_Data *client_data = &toxic->client_data; - user_password = (struct user_password) { - 0 - }; + free(client_data->data_path); + free(client_data->block_path); + free(toxic); +} + +void exit_toxic_success(Toxic *toxic) +{ + if (toxic == NULL) { + exit(EXIT_FAILURE); + } + + store_data(toxic); terminate_notify(); - kill_all_file_transfers(tox); - kill_all_windows(tox); + kill_all_file_transfers(toxic->tox); + kill_all_windows(toxic->tox); #ifdef AUDIO #ifdef VIDEO terminate_video(); #endif /* VIDEO */ - terminate_audio(); + terminate_audio(toxic->av); #endif /* AUDIO */ #ifdef PYTHON @@ -206,7 +189,7 @@ void exit_toxic_success(Tox *tox) #endif /* PYTHON */ free_global_data(); - tox_kill(tox); + tox_kill(toxic->tox); if (arg_opts.log_fp != NULL) { fclose(arg_opts.log_fp); @@ -220,6 +203,8 @@ void exit_toxic_success(Tox *tox) terminate_x11focus(); #endif /* X11 */ + kill_toxic(toxic); + exit(EXIT_SUCCESS); } @@ -513,19 +498,31 @@ static void load_friendlist(Tox *tox) sort_friendlist_index(); } -static void load_groups(Tox *tox) +static void load_groups(Toxic *toxic) { + if (toxic == NULL) { + return; + } + + Tox *tox = toxic->tox; + size_t numgroups = tox_group_get_number_groups(tox); for (size_t i = 0; i < numgroups; ++i) { - if (init_groupchat_win(tox, i, NULL, 0, Group_Join_Type_Load) != 0) { + if (init_groupchat_win(toxic, i, NULL, 0, Group_Join_Type_Load) != 0) { tox_group_leave(tox, i, NULL, 0, NULL); } } } -static void load_conferences(Tox *tox) +static void load_conferences(Toxic *toxic) { + if (toxic == NULL) { + return; + } + + Tox *tox = toxic->tox; + size_t num_chats = tox_conference_get_chatlist_size(tox); if (num_chats == 0) { @@ -573,7 +570,7 @@ static void load_conferences(Tox *tox) title[length] = 0; - int win_idx = init_conference_win(tox, conferencenum, type, (const char *) title, length); + const int win_idx = init_conference_win(toxic, conferencenum, type, (const char *) title, length); if (win_idx == -1) { tox_conference_delete(tox, conferencenum, NULL); @@ -683,7 +680,7 @@ static int password_eval(char *buf, int size) } /* Ask user if they would like to encrypt the data file and set password */ -static void first_time_encrypt(const char *msg) +static void first_time_encrypt(Client_Data *client_data, const char *msg) { char ch[256] = {0}; @@ -714,10 +711,10 @@ static void first_time_encrypt(const char *msg) while (valid_password == false) { fflush(stdout); // Flush all before user input - len = password_prompt(user_password.pass, sizeof(user_password.pass)); - user_password.len = len; + len = password_prompt(client_data->pass, sizeof(client_data->pass)); + client_data->pass_len = len; - if (strcasecmp(user_password.pass, "q") == 0) { + if (strcasecmp(client_data->pass, "q") == 0) { exit(0); } @@ -728,13 +725,13 @@ static void first_time_encrypt(const char *msg) if (string_is_empty(passconfirm)) { printf("Enter password again "); - snprintf(passconfirm, sizeof(passconfirm), "%s", user_password.pass); + snprintf(passconfirm, sizeof(passconfirm), "%s", client_data->pass); continue; } - if (strcmp(user_password.pass, passconfirm) != 0) { + if (strcmp(client_data->pass, passconfirm) != 0) { memset(passconfirm, 0, sizeof(passconfirm)); - memset(user_password.pass, 0, sizeof(user_password.pass)); + memset(client_data->pass, 0, sizeof(client_data->pass)); printf("Passwords don't match. Try again. "); continue; } @@ -742,9 +739,9 @@ static void first_time_encrypt(const char *msg) valid_password = true; } - queue_init_message("Data file '%s' is encrypted", DATA_FILE); + queue_init_message("Data file '%s' is encrypted", client_data->data_path); memset(passconfirm, 0, sizeof(passconfirm)); - user_password.data_is_encrypted = true; + client_data->is_encrypted = true; } clear_screen(); @@ -756,13 +753,15 @@ static void first_time_encrypt(const char *msg) * Return -1 on error. */ #define TEMP_PROFILE_EXT ".tmp" -int store_data(Tox *tox, const char *path) +int store_data(const Toxic *toxic) { + const char *path = toxic->client_data.data_path; + if (path == NULL) { return -1; } - size_t temp_buf_size = strlen(path) + strlen(TEMP_PROFILE_EXT) + 1; + const size_t temp_buf_size = strlen(path) + strlen(TEMP_PROFILE_EXT) + 1; char *temp_path = malloc(temp_buf_size); if (temp_path == NULL) { @@ -778,7 +777,7 @@ int store_data(Tox *tox, const char *path) return -1; } - size_t data_len = tox_get_savedata_size(tox); + const size_t data_len = tox_get_savedata_size(toxic->tox); char *data = malloc(data_len * sizeof(char)); if (data == NULL) { @@ -787,9 +786,11 @@ int store_data(Tox *tox, const char *path) return -1; } - tox_get_savedata(tox, (uint8_t *) data); + tox_get_savedata(toxic->tox, (uint8_t *) data); + + const Client_Data *client_data = &toxic->client_data; - if (user_password.data_is_encrypted && !arg_opts.unencrypt_data) { + if (client_data->is_encrypted && !arg_opts.unencrypt_data) { size_t enc_len = data_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH; char *enc_data = malloc(enc_len * sizeof(char)); @@ -801,8 +802,8 @@ int store_data(Tox *tox, const char *path) } Tox_Err_Encryption err; - tox_pass_encrypt((uint8_t *) data, data_len, (uint8_t *) user_password.pass, user_password.len, - (uint8_t *) enc_data, &err); + tox_pass_encrypt((uint8_t *) data, data_len, (uint8_t *) toxic->client_data.pass, + toxic->client_data.pass_len, (uint8_t *) enc_data, &err); if (err != TOX_ERR_ENCRYPTION_OK) { fprintf(stderr, "tox_pass_encrypt() failed with error %d\n", err); @@ -931,17 +932,17 @@ static void init_tox_options(struct Tox_Options *tox_opts) } } -/* Returns a new Tox object on success. - * If object fails to initialize the toxic process will terminate. +/* + * Loads a Tox instance. + * + * Return true on success. */ -static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts, Tox_Err_New *new_err) +static bool load_tox(Toxic *toxic, struct Tox_Options *tox_opts, Tox_Err_New *new_err) { - Tox *tox = NULL; - - FILE *fp = fopen(data_path, "rb"); + FILE *fp = fopen(toxic->client_data.data_path, "rb"); if (fp != NULL) { /* Data file exists */ - off_t len = file_size(data_path); + off_t len = file_size(toxic->client_data.data_path); if (len == 0) { fclose(fp); @@ -961,7 +962,7 @@ static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts, Tox_Err_New exit_toxic_err("failed in load_tox", FATALERR_FILEOP); } - bool is_encrypted = tox_is_data_encrypted((uint8_t *) data); + const bool is_encrypted = tox_is_data_encrypted((uint8_t *) data); /* attempt to encrypt an already encrypted data file */ if (arg_opts.encrypt_data && is_encrypted) { @@ -970,15 +971,18 @@ static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts, Tox_Err_New exit_toxic_err("failed in load_tox", FATALERR_ENCRYPT); } + Client_Data *client_data = &toxic->client_data; + if (arg_opts.unencrypt_data && is_encrypted) { - queue_init_message("Data file '%s' has been unencrypted", data_path); + queue_init_message("Data file '%s' has been unencrypted", client_data->data_path); } else if (arg_opts.unencrypt_data) { - queue_init_message("Warning: passed --unencrypt-data option with unencrypted data file '%s'", data_path); + queue_init_message("Warning: passed --unencrypt-data option with unencrypted data file '%s'", + client_data->data_path); } if (is_encrypted) { if (!arg_opts.unencrypt_data) { - user_password.data_is_encrypted = true; + client_data->is_encrypted = true; } size_t pwlen = 0; @@ -1002,14 +1006,14 @@ static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts, Tox_Err_New fflush(stdout); // Flush before prompts so the user sees the question/message if (pweval) { - pwlen = password_eval(user_password.pass, sizeof(user_password.pass)); + pwlen = password_eval(client_data->pass, sizeof(client_data->pass)); } else { - pwlen = password_prompt(user_password.pass, sizeof(user_password.pass)); + pwlen = password_prompt(client_data->pass, sizeof(client_data->pass)); } - user_password.len = pwlen; + client_data->pass_len = pwlen; - if (strcasecmp(user_password.pass, "q") == 0) { + if (strcasecmp(client_data->pass, "q") == 0) { fclose(fp); free(plain); free(data); @@ -1025,20 +1029,20 @@ static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts, Tox_Err_New } Tox_Err_Decryption pwerr; - tox_pass_decrypt((uint8_t *) data, len, (uint8_t *) user_password.pass, pwlen, + tox_pass_decrypt((uint8_t *) data, len, (uint8_t *) client_data->pass, pwlen, (uint8_t *) plain, &pwerr); if (pwerr == TOX_ERR_DECRYPTION_OK) { tox_options_set_savedata_type(tox_opts, TOX_SAVEDATA_TYPE_TOX_SAVE); tox_options_set_savedata_data(tox_opts, (uint8_t *) plain, plain_len); - tox = tox_new(tox_opts, new_err); + toxic->tox = tox_new(tox_opts, new_err); - if (tox == NULL) { + if (toxic->tox == NULL) { fclose(fp); free(data); free(plain); - return NULL; + return false; } break; @@ -1060,39 +1064,39 @@ static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts, Tox_Err_New tox_options_set_savedata_type(tox_opts, TOX_SAVEDATA_TYPE_TOX_SAVE); tox_options_set_savedata_data(tox_opts, (uint8_t *) data, len); - tox = tox_new(tox_opts, new_err); + toxic->tox = tox_new(tox_opts, new_err); - if (tox == NULL) { + if (toxic->tox == NULL) { fclose(fp); free(data); - return NULL; + return false; } } fclose(fp); free(data); } else { /* Data file does not/should not exist */ - if (file_exists(data_path)) { + if (file_exists(toxic->client_data.data_path)) { exit_toxic_err("failed in load_tox", FATALERR_FILEOP); } tox_options_set_savedata_type(tox_opts, TOX_SAVEDATA_TYPE_NONE); - tox = tox_new(tox_opts, new_err); + toxic->tox = tox_new(tox_opts, new_err); - if (tox == NULL) { - return NULL; + if (toxic->tox == NULL) { + return false; } - if (store_data(tox, data_path) == -1) { + if (store_data(toxic) == -1) { exit_toxic_err("failed in load_tox", FATALERR_FILEOP); } } - return tox; + return true;; } -static Tox *load_toxic(char *data_path) +static bool load_toxic(Toxic *toxic) { Tox_Err_Options_New options_new_err; struct Tox_Options *tox_opts = tox_options_new(&options_new_err); @@ -1104,32 +1108,43 @@ static Tox *load_toxic(char *data_path) init_tox_options(tox_opts); Tox_Err_New new_err; - Tox *tox = load_tox(data_path, tox_opts, &new_err); + + if (!load_tox(toxic, tox_opts, &new_err)) { + return false; + } if (new_err == TOX_ERR_NEW_PORT_ALLOC && tox_options_get_ipv6_enabled(tox_opts)) { queue_init_message("Falling back to ipv4"); tox_options_set_ipv6_enabled(tox_opts, false); - tox = load_tox(data_path, tox_opts, &new_err); + + if (!load_tox(toxic, tox_opts, &new_err)) { + return false; + } } - if (tox == NULL) { - return NULL; + if (toxic->tox == NULL) { + return false; } if (new_err != TOX_ERR_NEW_OK) { queue_init_message("tox_new returned non-fatal error %d", new_err); } - init_tox_callbacks(tox); - load_friendlist(tox); - load_blocklist(BLOCK_FILE); + init_tox_callbacks(toxic->tox); + load_friendlist(toxic->tox); - if (tox_self_get_name_size(tox) == 0) { - tox_self_set_name(tox, (uint8_t *) "Toxic User", strlen("Toxic User"), NULL); + if (load_blocklist(toxic->client_data.block_path) == -1) { + queue_init_message("Failed to load block list"); + } + + if (tox_self_get_name_size(toxic->tox) == 0) { + tox_self_set_name(toxic->tox, (uint8_t *) "Toxic User", strlen("Toxic User"), NULL); } tox_options_free(tox_opts); - return tox; + + fprintf(stderr, "a%p\n", (void *)toxic->tox); + return true; } static void do_toxic(Toxic *toxic) @@ -1141,7 +1156,7 @@ static void do_toxic(Toxic *toxic) return; } - tox_iterate(toxic->tox, toxic); + tox_iterate(toxic->tox, (void *) toxic); do_tox_connection(toxic->tox); pthread_mutex_unlock(&Winthread.lock); @@ -1182,7 +1197,7 @@ static void poll_interface_refresh_flag(void) void *thread_winref(void *data) { - Tox *tox = (Tox *) data; + Toxic *toxic = (Toxic *) data; uint8_t draw_count = 0; @@ -1190,7 +1205,7 @@ void *thread_winref(void *data) while (true) { draw_count++; - draw_active_window(tox); + draw_active_window(toxic); if (Winthread.flag_resize) { on_window_resize(); @@ -1202,7 +1217,7 @@ void *thread_winref(void *data) if (Winthread.sig_exit_toxic) { pthread_mutex_lock(&Winthread.lock); - exit_toxic_success(tox); + exit_toxic_success(toxic); } poll_interface_refresh_flag(); @@ -1211,7 +1226,7 @@ void *thread_winref(void *data) void *thread_cqueue(void *data) { - Tox *tox = (Tox *) data; + Toxic *toxic = (Toxic *) data; while (true) { pthread_mutex_lock(&Winthread.lock); @@ -1223,7 +1238,7 @@ void *thread_cqueue(void *data) cqueue_check_unread(toxwin); if (get_friend_connection_status(toxwin->num) != TOX_CONNECTION_NONE) { - cqueue_try_send(toxwin, tox); + cqueue_try_send(toxwin, toxic->tox); } } } @@ -1289,7 +1304,7 @@ static void set_default_opts(void) arg_opts.proxy_type = TOX_PROXY_TYPE_NONE; } -static void parse_args(int argc, char *argv[]) +static void parse_args(Toxic *toxic, int argc, char *argv[]) { set_default_opts(); @@ -1366,34 +1381,36 @@ static void parse_args(int argc, char *argv[]) arg_opts.use_custom_data = 1; - if (DATA_FILE) { - free(DATA_FILE); - DATA_FILE = NULL; + Client_Data *client_data = &toxic->client_data; + + if (client_data->data_path) { + free(client_data->data_path); + client_data->data_path = NULL; } - if (BLOCK_FILE) { - free(BLOCK_FILE); - BLOCK_FILE = NULL; + if (client_data->block_path) { + free(client_data->block_path); + client_data->block_path = NULL; } - DATA_FILE = malloc(strlen(optarg) + 1); + client_data->data_path = malloc(strlen(optarg) + 1); - if (DATA_FILE == NULL) { + if (client_data->data_path == NULL) { exit_toxic_err("failed in parse_args", FATALERR_MEMORY); } - strcpy(DATA_FILE, optarg); + strcpy(client_data->data_path, optarg); - BLOCK_FILE = malloc(strlen(optarg) + strlen("-blocklist") + 1); + client_data->block_path = malloc(strlen(optarg) + strlen("-blocklist") + 1); - if (BLOCK_FILE == NULL) { + if (client_data->block_path == NULL) { exit_toxic_err("failed in parse_args", FATALERR_MEMORY); } - strcpy(BLOCK_FILE, optarg); - strcat(BLOCK_FILE, "-blocklist"); + strcpy(client_data->block_path, optarg); + strcat(client_data->block_path, "-blocklist"); - queue_init_message("Using '%s' data file", DATA_FILE); + queue_init_message("Using '%s' data file", client_data->data_path); break; } @@ -1536,7 +1553,7 @@ static void parse_args(int argc, char *argv[]) * * Exits the process with an error on failure. */ -static void init_default_data_files(void) +static void init_default_data_files(Client_Data *client_data) { if (arg_opts.use_custom_data) { return; @@ -1551,33 +1568,33 @@ static void init_default_data_files(void) int config_err = create_user_config_dirs(user_config_dir); if (config_err == -1) { - DATA_FILE = strdup(DATANAME); - BLOCK_FILE = strdup(BLOCKNAME); + client_data->data_path = strdup(DATANAME); + client_data->block_path = strdup(BLOCKNAME); - if (DATA_FILE == NULL || BLOCK_FILE == NULL) { + if (client_data->data_path == NULL || client_data->block_path == NULL) { exit_toxic_err("failed in init_default_data_files()", FATALERR_MEMORY); } } else { - DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(DATANAME) + 1); - BLOCK_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(BLOCKNAME) + 1); + client_data->data_path = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(DATANAME) + 1); + client_data->block_path = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(BLOCKNAME) + 1); - if (DATA_FILE == NULL || BLOCK_FILE == NULL) { + if (client_data->data_path == NULL || client_data->block_path == NULL) { exit_toxic_err("failed in init_default_data_files()", FATALERR_MEMORY); } - strcpy(DATA_FILE, user_config_dir); - strcat(DATA_FILE, CONFIGDIR); - strcat(DATA_FILE, DATANAME); + strcpy(client_data->data_path, user_config_dir); + strcat(client_data->data_path, CONFIGDIR); + strcat(client_data->data_path, DATANAME); - strcpy(BLOCK_FILE, user_config_dir); - strcat(BLOCK_FILE, CONFIGDIR); - strcat(BLOCK_FILE, BLOCKNAME); + strcpy(client_data->block_path, user_config_dir); + strcat(client_data->block_path, CONFIGDIR); + strcat(client_data->block_path, BLOCKNAME); } free(user_config_dir); } -static Toxic *toxic_init(Tox *tox) +static Toxic *toxic_init(void) { Toxic *toxic = calloc(1, sizeof(Toxic)); @@ -1585,8 +1602,6 @@ static Toxic *toxic_init(Tox *tox) return NULL; } - toxic->tox = tox; - return toxic; } @@ -1597,7 +1612,13 @@ int main(int argc, char **argv) srand(time(NULL)); // We use rand() for trivial/non-security related things - parse_args(argc, argv); + Toxic *toxic = toxic_init(); + + if (toxic == NULL) { + exit_toxic_err("failed in main", FATALERR_TOXIC_INIT); + } + + parse_args(toxic, argc, argv); /* Use the -b flag to enable stderr */ if (!arg_opts.debug) { @@ -1612,14 +1633,14 @@ int main(int argc, char **argv) queue_init_message("Warning: Using --unencrypt-data and --encrypt-data simultaneously has no effect"); } - init_default_data_files(); + init_default_data_files(&toxic->client_data); - bool datafile_exists = file_exists(DATA_FILE); + const bool datafile_exists = file_exists(toxic->client_data.data_path); if (!datafile_exists && !arg_opts.unencrypt_data) { - first_time_encrypt("Creating new data file. Would you like to encrypt it? Y/n (q to quit)"); + first_time_encrypt(&toxic->client_data, "Creating new data file. Would you like to encrypt it? Y/n (q to quit)"); } else if (arg_opts.encrypt_data) { - first_time_encrypt("Encrypt existing data file? Y/n (q to quit)"); + first_time_encrypt(&toxic->client_data, "Encrypt existing data file? Y/n (q to quit)"); } @@ -1655,28 +1676,20 @@ int main(int argc, char **argv) #endif /* X11 */ - Tox *tox = load_toxic(DATA_FILE); - - if (tox == NULL) { + if (!load_toxic(toxic)) { exit_toxic_err("Failed in main", FATALERR_TOX_INIT); } - Toxic *toxic = toxic_init(tox); - - if (toxic == NULL) { - exit_toxic_err("failed in main", FATALERR_TOXIC_INIT); - } - if (arg_opts.encrypt_data && !datafile_exists) { arg_opts.encrypt_data = 0; } init_term(); - prompt = init_windows(toxic->tox); + prompt = init_windows(toxic); prompt_init_statusbar(prompt, toxic->tox, !datafile_exists); - load_groups(toxic->tox); - load_conferences(toxic->tox); + load_groups(toxic); + load_conferences(toxic); set_active_window_index(0); if (pthread_mutex_init(&Winthread.lock, NULL) != 0) { @@ -1685,23 +1698,23 @@ int main(int argc, char **argv) #ifdef AUDIO - av = init_audio(prompt, toxic->tox); + toxic->av = init_audio(prompt, toxic); - if (av == NULL) { + if (toxic->av == NULL) { queue_init_message("Failed to init audio"); } #ifdef VIDEO - init_video(prompt, toxic->tox); + init_video(prompt, toxic); - if (av == NULL) { + if (toxic->av == NULL) { queue_init_message("Failed to init video"); } #endif /* VIDEO */ /* AV thread */ - if (pthread_create(&av_thread.tid, NULL, thread_av, (void *)av) != 0) { + if (pthread_create(&av_thread.tid, NULL, thread_av, (void *) toxic->av) != 0) { exit_toxic_err("failed in main", FATALERR_THREAD_CREATE); } @@ -1717,12 +1730,12 @@ int main(int argc, char **argv) #endif /* AUDIO */ /* thread for ncurses UI */ - if (pthread_create(&Winthread.tid, NULL, thread_winref, (void *)toxic->tox) != 0) { + if (pthread_create(&Winthread.tid, NULL, thread_winref, (void *) toxic) != 0) { exit_toxic_err("failed in main", FATALERR_THREAD_CREATE); } /* thread for message queue */ - if (pthread_create(&cqueue_thread.tid, NULL, thread_cqueue, (void *)toxic->tox) != 0) { + if (pthread_create(&cqueue_thread.tid, NULL, thread_cqueue, (void *) toxic) != 0) { exit_toxic_err("failed in main", FATALERR_THREAD_CREATE); } @@ -1736,7 +1749,7 @@ int main(int argc, char **argv) init_notify(60, user_settings->notification_timeout); /* screen/tmux auto-away timer */ - if (init_mplex_away_timer(toxic->tox) == -1) { + if (init_mplex_away_timer(toxic) == -1) { queue_init_message("Failed to init mplex auto-away."); } @@ -1756,7 +1769,7 @@ int main(int argc, char **argv) /* set user avatar from config file. if no path is supplied tox_unset_avatar is called */ char avatarstr[PATH_MAX + 11]; snprintf(avatarstr, sizeof(avatarstr), "/avatar %s", user_settings->avatar_path); - execute(prompt->chatwin->history, prompt, toxic->tox, avatarstr, GLOBAL_COMMAND_MODE); + execute(prompt->chatwin->history, prompt, toxic, avatarstr, GLOBAL_COMMAND_MODE); time_t last_save = get_unix_time(); @@ -1768,7 +1781,7 @@ int main(int argc, char **argv) if (user_settings->autosave_freq > 0 && timed_out(last_save, user_settings->autosave_freq)) { pthread_mutex_lock(&Winthread.lock); - if (store_data(toxic->tox, DATA_FILE) != 0) { + if (store_data(toxic) != 0) { line_info_add(prompt, false, NULL, NULL, SYS_MSG, 0, RED, "WARNING: Failed to save to data file"); } diff --git a/src/toxic.h b/src/toxic.h index 49d2b1ecc..4a481c1f9 100644 --- a/src/toxic.h +++ b/src/toxic.h @@ -111,6 +111,27 @@ typedef enum _FATAL_ERRS { Uncomment if necessary */ /* #define URXVT_FIX */ +#define MIN_PASSWORD_LEN 6 +#define MAX_PASSWORD_LEN 64 + +typedef struct Client_Data { + bool is_encrypted; + char pass[MAX_PASSWORD_LEN + 1]; + int pass_len; + char *data_path; + char *block_path; +} Client_Data; + +typedef struct ToxAV ToxAV; + +typedef struct Toxic { + Tox *tox; +#ifdef AUDIO + ToxAV *av; +#endif + Client_Data client_data; +} Toxic; + void lock_status(void); void unlock_status(void); @@ -119,10 +140,10 @@ void flag_interface_refresh(void); /* Sets ncurses refresh rate. Lower values make it refresh more often. */ void set_window_refresh_rate(size_t refresh_rate); -void exit_toxic_success(Tox *tox) __attribute__((__noreturn__)); +void exit_toxic_success(Toxic *toxic) __attribute__((__noreturn__)); void exit_toxic_err(const char *errmsg, int errcode) __attribute__((__noreturn__)); -int store_data(Tox *tox, const char *path); +int store_data(const Toxic *toxic); /* callbacks */ void on_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata); @@ -132,7 +153,7 @@ void on_friend_message(Tox *tox, uint32_t friendnumber, Tox_Message_Type type, c void on_friend_name(Tox *tox, uint32_t friendnumber, const uint8_t *string, size_t length, void *userdata); void on_friend_status(Tox *tox, uint32_t friendnumber, Tox_User_Status status, void *userdata); void on_friend_status_message(Tox *tox, uint32_t friendnumber, const uint8_t *string, size_t length, void *userdata); -void on_friend_added(Tox *tox, uint32_t friendnumber, bool sort); +void on_friend_added(Toxic *toxic, uint32_t friendnumber, bool sort); void on_conference_message(Tox *tox, uint32_t conferencenumber, uint32_t peernumber, Tox_Message_Type type, const uint8_t *message, size_t length, void *userdata); void on_conference_invite(Tox *tox, uint32_t friendnumber, Tox_Conference_Type type, const uint8_t *conference_pub_key, @@ -181,7 +202,4 @@ void on_group_moderation(Tox *tox, uint32_t groupnumber, uint32_t source_peernum Tox_Group_Mod_Event type, void *userdata); void on_group_voice_state(Tox *tox, uint32_t groupnumber, Tox_Group_Voice_State voice_state, void *userdata); -extern char *DATA_FILE; -extern char *BLOCK_FILE; - #endif /* TOXIC_H */ diff --git a/src/video_call.c b/src/video_call.c index 235cc1519..d76f30775 100644 --- a/src/video_call.c +++ b/src/video_call.c @@ -54,39 +54,33 @@ static void print_err(ToxWindow *self, const char *error_str) line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", error_str); } -ToxAV *init_video(ToxWindow *self, Tox *tox) +ToxAV *init_video(ToxWindow *self, const Toxic *toxic) { - UNUSED_VAR(tox); - CallControl.video_errors = ve_None; CallControl.video_enabled = true; CallControl.default_video_bit_rate = 0; CallControl.video_frame_duration = 10; - if (!CallControl.av) { + if (toxic->av == NULL) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Video failed to init with ToxAV instance"); - return NULL; } - if (init_video_devices(CallControl.av) == vde_InternalError) { + if (init_video_devices(toxic->av) == vde_InternalError) { line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to init video devices"); - return NULL; } - toxav_callback_video_receive_frame(CallControl.av, on_video_receive_frame, &CallControl); - toxav_callback_video_bit_rate(CallControl.av, on_video_bit_rate, &CallControl); + toxav_callback_video_receive_frame(toxic->av, on_video_receive_frame, &CallControl); + toxav_callback_video_bit_rate(toxic->av, on_video_bit_rate, &CallControl); - return CallControl.av; + return toxic->av; } void terminate_video(void) { - int i; - - for (i = 0; i < CallControl.max_calls; ++i) { + for (int i = 0; i < CallControl.max_calls; ++i) { Call *this_call = &CallControl.calls[i]; stop_video_transmission(this_call, i); @@ -266,18 +260,21 @@ void callback_video_end(uint32_t friend_number) /* * Commands from chat_commands.h */ -void cmd_vcall(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_vcall(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); UNUSED_VAR(argv); + if (toxic == NULL) { + return; + } + if (argc != 0) { print_err(self, "Unknown arguments."); return; } - if (!CallControl.av) { + if (toxic->av == NULL) { print_err(self, "ToxAV not supported!"); return; } @@ -301,12 +298,15 @@ void cmd_vcall(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv) place_call(self); } -void cmd_video(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_video(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); UNUSED_VAR(argv); + if (toxic == NULL) { + return; + } + Call *this_call = &CallControl.calls[self->num]; if (argc != 0) { @@ -314,7 +314,7 @@ void cmd_video(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv) return; } - if (!CallControl.av) { + if (toxic->av == NULL) { print_err(self, "ToxAV not supported!"); return; } @@ -337,10 +337,13 @@ void cmd_video(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv) } } -void cmd_res(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_res(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); + + if (toxic == NULL) { + return; + } Call *call = &CallControl.calls[self->num]; @@ -377,17 +380,17 @@ void cmd_res(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[M call->video_width = width; call->video_height = height; call->video_bit_rate = DEFAULT_VIDEO_BIT_RATE; - start_video_transmission(self, CallControl.av, call); + start_video_transmission(self, toxic->av, call); } else { CallControl.default_video_width = width; CallControl.default_video_height = height; } } -void cmd_list_video_devices(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_list_video_devices(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); + UNUSED_VAR(toxic); if (argc != 1) { if (argc < 1) { @@ -418,10 +421,10 @@ void cmd_list_video_devices(WINDOW *window, ToxWindow *self, Tox *tox, int argc, } /* This changes primary video device only */ -void cmd_change_video_device(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +void cmd_change_video_device(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(tox); + UNUSED_VAR(toxic); if (argc != 2) { if (argc < 1) { diff --git a/src/video_call.h b/src/video_call.h index 341ca3e0c..0db58b4df 100644 --- a/src/video_call.h +++ b/src/video_call.h @@ -30,7 +30,7 @@ #include "video_device.h" /* You will have to pass pointer to first member of 'windows' declared in windows.c */ -ToxAV *init_video(ToxWindow *self, Tox *tox); +ToxAV *init_video(ToxWindow *self, const Toxic *toxic); void terminate_video(void); int start_video_transmission(ToxWindow *self, ToxAV *av, Call *call); int stop_video_transmission(Call *call, int friend_number); diff --git a/src/windows.c b/src/windows.c index 21631abb6..f754283d6 100644 --- a/src/windows.c +++ b/src/windows.c @@ -63,13 +63,17 @@ void on_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *data, void on_friend_connection_status(Tox *tox, uint32_t friendnumber, Tox_Connection connection_status, void *userdata) { - UNUSED_VAR(userdata); + Toxic *toxic = (Toxic *) userdata; - on_avatar_friend_connection_status(tox, friendnumber, connection_status); + if (toxic == NULL) { + return; + } + + on_avatar_friend_connection_status(toxic->tox, friendnumber, connection_status); for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i] != NULL && windows[i]->onConnectionChange != NULL) { - windows[i]->onConnectionChange(windows[i], tox, friendnumber, connection_status); + windows[i]->onConnectionChange(windows[i], toxic, friendnumber, connection_status); } } @@ -96,21 +100,25 @@ void on_friend_typing(Tox *tox, uint32_t friendnumber, bool is_typing, void *use void on_friend_message(Tox *tox, uint32_t friendnumber, Tox_Message_Type type, const uint8_t *string, size_t length, void *userdata) { - UNUSED_VAR(userdata); + Toxic *toxic = (Toxic *) userdata; char msg[MAX_STR_SIZE + 1]; length = copy_tox_str(msg, sizeof(msg), (const char *) string, length); for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i] != NULL && windows[i]->onMessage != NULL) { - windows[i]->onMessage(windows[i], tox, friendnumber, type, msg, length); + windows[i]->onMessage(windows[i], toxic, friendnumber, type, msg, length); } } } void on_friend_name(Tox *tox, uint32_t friendnumber, const uint8_t *string, size_t length, void *userdata) { - UNUSED_VAR(userdata); + const Toxic *toxic = (Toxic *) userdata; + + if (toxic == NULL) { + return; + } char nick[TOXIC_MAX_NAME_LENGTH + 1]; length = copy_tox_str(nick, sizeof(nick), (const char *) string, length); @@ -124,7 +132,7 @@ void on_friend_name(Tox *tox, uint32_t friendnumber, const uint8_t *string, size flag_interface_refresh(); - store_data(tox, DATA_FILE); + store_data(toxic); } void on_friend_status_message(Tox *tox, uint32_t friendnumber, const uint8_t *string, size_t length, void *userdata) @@ -158,15 +166,20 @@ void on_friend_status(Tox *tox, uint32_t friendnumber, Tox_User_Status status, v flag_interface_refresh(); } -void on_friend_added(Tox *tox, uint32_t friendnumber, bool sort) +// TODO: This isn't a proper tox callback. Refactor with friendlist. +void on_friend_added(Toxic *toxic, uint32_t friendnumber, bool sort) { + if (toxic == NULL) { + return; + } + for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i] != NULL && windows[i]->onFriendAdded != NULL) { - windows[i]->onFriendAdded(windows[i], tox, friendnumber, sort); + windows[i]->onFriendAdded(windows[i], toxic->tox, friendnumber, sort); } } - store_data(tox, DATA_FILE); + store_data(toxic); } void on_conference_message(Tox *tox, uint32_t conferencenumber, uint32_t peernumber, Tox_Message_Type type, @@ -187,11 +200,15 @@ void on_conference_message(Tox *tox, uint32_t conferencenumber, uint32_t peernum void on_conference_invite(Tox *tox, uint32_t friendnumber, Tox_Conference_Type type, const uint8_t *conference_pub_key, size_t length, void *userdata) { - UNUSED_VAR(userdata); + Toxic *toxic = (Toxic *) userdata; + + if (toxic == NULL) { + return; + } for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i] != NULL && windows[i]->onConferenceInvite != NULL) { - windows[i]->onConferenceInvite(windows[i], tox, friendnumber, type, (const char *) conference_pub_key, length); + windows[i]->onConferenceInvite(windows[i], toxic, friendnumber, type, (const char *) conference_pub_key, length); } } } @@ -307,7 +324,11 @@ void on_file_recv_control(Tox *tox, uint32_t friendnumber, uint32_t filenumber, void on_file_recv(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uint32_t kind, uint64_t file_size, const uint8_t *filename, size_t filename_length, void *userdata) { - UNUSED_VAR(userdata); + Toxic *toxic = (Toxic *) userdata; + + if (toxic == NULL) { + return; + } /* We don't care about receiving avatars */ if (kind != TOX_FILE_KIND_DATA) { @@ -317,7 +338,7 @@ void on_file_recv(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uint32_t for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i] != NULL && windows[i]->onFileRecv != NULL) { - windows[i]->onFileRecv(windows[i], tox, friendnumber, filenumber, file_size, (const char *) filename, + windows[i]->onFileRecv(windows[i], toxic, friendnumber, filenumber, file_size, (const char *) filename, filename_length); } } @@ -336,13 +357,17 @@ void on_friend_read_receipt(Tox *tox, uint32_t friendnumber, uint32_t receipt, v void on_lossless_custom_packet(Tox *tox, uint32_t friendnumber, const uint8_t *data, size_t length, void *userdata) { - UNUSED_VAR(userdata); - if (length == 0 || data == NULL) { return; } - uint8_t type = data[0]; + Toxic *toxic = (Toxic *) userdata; + + if (toxic == NULL) { + return; + } + + const uint8_t type = data[0]; switch (type) { #ifdef GAMES @@ -352,7 +377,7 @@ void on_lossless_custom_packet(Tox *tox, uint32_t friendnumber, const uint8_t *d ToxWindow *window = windows[i]; if (window != NULL && window->onGameInvite != NULL) { - window->onGameInvite(window, tox, friendnumber, data + 1, length - 1); + window->onGameInvite(window, toxic, friendnumber, data + 1, length - 1); } } @@ -364,7 +389,7 @@ void on_lossless_custom_packet(Tox *tox, uint32_t friendnumber, const uint8_t *d ToxWindow *window = windows[i]; if (window != NULL && window->onGameData != NULL) { - window->onGameData(window, tox, friendnumber, data + 1, length - 1); + window->onGameData(window, toxic->tox, friendnumber, data + 1, length - 1); } } @@ -384,12 +409,18 @@ void on_group_invite(Tox *tox, uint32_t friendnumber, const uint8_t *invite_data const uint8_t *group_name, size_t group_name_length, void *userdata) { + Toxic *toxic = (Toxic *) userdata; + + if (toxic == NULL) { + return; + } + char gname[MAX_STR_SIZE + 1]; group_name_length = copy_tox_str(gname, sizeof(gname), (const char *) group_name, group_name_length); for (size_t i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i] != NULL && windows[i]->onGroupInvite != NULL) { - windows[i]->onGroupInvite(windows[i], tox, friendnumber, (char *) invite_data, length, gname, group_name_length); + windows[i]->onGroupInvite(windows[i], toxic, friendnumber, (char *) invite_data, length, gname, group_name_length); } } } @@ -531,9 +562,11 @@ void on_group_nick_change(Tox *tox, uint32_t groupnumber, uint32_t peer_id, cons void on_group_self_join(Tox *tox, uint32_t groupnumber, void *userdata) { + Toxic *toxic = (Toxic *) userdata; + for (size_t i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i] != NULL && windows[i]->onGroupSelfJoin != NULL) { - windows[i]->onGroupSelfJoin(windows[i], tox, groupnumber); + windows[i]->onGroupSelfJoin(windows[i], toxic, groupnumber); } } } @@ -568,7 +601,7 @@ void on_group_voice_state(Tox *tox, uint32_t groupnumber, Tox_Group_Voice_State /* CALLBACKS END */ -int add_window(Tox *tox, ToxWindow *w) +int add_window(Toxic *toxic, ToxWindow *w) { if (LINES < 2) { return -1; @@ -593,7 +626,7 @@ int add_window(Tox *tox, ToxWindow *w) windows[i] = w; if (w->onInit) { - w->onInit(w, tox); + w->onInit(w, toxic); } ++num_active_windows; @@ -662,7 +695,7 @@ void del_window(ToxWindow *w) } } -ToxWindow *init_windows(Tox *tox) +ToxWindow *init_windows(Toxic *toxic) { if (COLS <= CHATBOX_HEIGHT + WINDOW_BAR_HEIGHT) { exit_toxic_err("add_window() for prompt failed in init_windows", FATALERR_WININIT); @@ -670,13 +703,13 @@ ToxWindow *init_windows(Tox *tox) prompt = new_prompt(); - int n_prompt = add_window(tox, prompt); + int n_prompt = add_window(toxic, prompt); if (n_prompt < 0) { exit_toxic_err("add_window() for prompt failed in init_windows", FATALERR_WININIT); } - if (add_window(tox, new_friendlist()) == -1) { + if (add_window(toxic, new_friendlist()) == -1) { exit_toxic_err("add_window() for friendlist failed in init_windows", FATALERR_WININIT); } @@ -968,8 +1001,12 @@ static wint_t get_input_sequence_code(void) return -1; } -void draw_active_window(Tox *tox) +void draw_active_window(Toxic *toxic) { + if (toxic == NULL) { + return; + } + ToxWindow *a = windows[active_window_index]; if (a == NULL) { @@ -979,19 +1016,19 @@ void draw_active_window(Tox *tox) pthread_mutex_lock(&Winthread.lock); a->alert = WINDOW_ALERT_NONE; a->pending_messages = 0; - bool flag_refresh = Winthread.flag_refresh; + const bool flag_refresh = Winthread.flag_refresh; pthread_mutex_unlock(&Winthread.lock); if (flag_refresh) { touchwin(a->window); - a->onDraw(a, tox); + a->onDraw(a, toxic->tox); wrefresh(a->window); } #ifdef AUDIO else if (a->is_call && timed_out(a->chatwin->infobox.lastupdate, 1)) { touchwin(a->window); - a->onDraw(a, tox); + a->onDraw(a, toxic->tox); wrefresh(a->window); } @@ -1002,7 +1039,7 @@ void draw_active_window(Tox *tox) if (a->type == WINDOW_TYPE_GAME) { if (!flag_refresh) { // we always want to be continously refreshing game windows touchwin(a->window); - a->onDraw(a, tox); + a->onDraw(a, toxic->tox); wrefresh(a->window); } @@ -1020,7 +1057,7 @@ void draw_active_window(Tox *tox) set_next_window(ch); } - a->onKey(a, tox, ch, false); // we lock only when necessary in the onKey callback + a->onKey(a, toxic, ch, false); // we lock only when necessary in the onKey callback return; } @@ -1043,7 +1080,7 @@ void draw_active_window(Tox *tox) return; } else if ((printable == 0) && (a->type != WINDOW_TYPE_FRIEND_LIST)) { pthread_mutex_lock(&Winthread.lock); - bool input_ret = a->onKey(a, tox, ch, (bool) printable); + bool input_ret = a->onKey(a, toxic, ch, (bool) printable); pthread_mutex_unlock(&Winthread.lock); if (input_ret) { @@ -1059,7 +1096,7 @@ void draw_active_window(Tox *tox) } pthread_mutex_lock(&Winthread.lock); - a->onKey(a, tox, ch, (bool) printable); + a->onKey(a, toxic, ch, (bool) printable); pthread_mutex_unlock(&Winthread.lock); } diff --git a/src/windows.h b/src/windows.h index 80aa125f7..4239a571f 100644 --- a/src/windows.h +++ b/src/windows.h @@ -164,36 +164,36 @@ typedef struct GameData GameData; struct ToxWindow { /* ncurses */ - bool(*onKey)(ToxWindow *, Tox *, wint_t, bool); + bool(*onKey)(ToxWindow *, Toxic *, wint_t, bool); void(*onDraw)(ToxWindow *, Tox *); - void(*onInit)(ToxWindow *, Tox *); + void(*onInit)(ToxWindow *, Toxic *); /* toxcore */ void(*onFriendRequest)(ToxWindow *, Tox *, const char *, const char *, size_t); void(*onFriendAdded)(ToxWindow *, Tox *, uint32_t, bool); - void(*onConnectionChange)(ToxWindow *, Tox *, uint32_t, Tox_Connection); - void(*onMessage)(ToxWindow *, Tox *, uint32_t, Tox_Message_Type, const char *, size_t); + void(*onConnectionChange)(ToxWindow *, Toxic *, uint32_t, Tox_Connection); + void(*onMessage)(ToxWindow *, Toxic *, uint32_t, Tox_Message_Type, const char *, size_t); void(*onNickChange)(ToxWindow *, Tox *, uint32_t, const char *, size_t); void(*onStatusChange)(ToxWindow *, Tox *, uint32_t, Tox_User_Status); void(*onStatusMessageChange)(ToxWindow *, uint32_t, const char *, size_t); void(*onConferenceMessage)(ToxWindow *, Tox *, uint32_t, uint32_t, Tox_Message_Type, const char *, size_t); - void(*onConferenceInvite)(ToxWindow *, Tox *, int32_t, uint8_t, const char *, uint16_t); + void(*onConferenceInvite)(ToxWindow *, Toxic *, int32_t, uint8_t, const char *, uint16_t); void(*onConferenceNameListChange)(ToxWindow *, Tox *, uint32_t); void(*onConferencePeerNameChange)(ToxWindow *, Tox *, uint32_t, uint32_t, const char *, size_t); void(*onConferenceTitleChange)(ToxWindow *, Tox *, uint32_t, uint32_t, const char *, size_t); void(*onFileChunkRequest)(ToxWindow *, Tox *, uint32_t, uint32_t, uint64_t, size_t); void(*onFileRecvChunk)(ToxWindow *, Tox *, uint32_t, uint32_t, uint64_t, const char *, size_t); void(*onFileControl)(ToxWindow *, Tox *, uint32_t, uint32_t, Tox_File_Control); - void(*onFileRecv)(ToxWindow *, Tox *, uint32_t, uint32_t, uint64_t, const char *, size_t); + void(*onFileRecv)(ToxWindow *, Toxic *, uint32_t, uint32_t, uint64_t, const char *, size_t); void(*onTypingChange)(ToxWindow *, Tox *, uint32_t, bool); void(*onReadReceipt)(ToxWindow *, Tox *, uint32_t, uint32_t); #ifdef GAMES - void(*onGameInvite)(ToxWindow *, Tox *, uint32_t, const uint8_t *, size_t); + void(*onGameInvite)(ToxWindow *, Toxic *, uint32_t, const uint8_t *, size_t); void(*onGameData)(ToxWindow *, Tox *, uint32_t, const uint8_t *, size_t); #endif // GAMES - void(*onGroupInvite)(ToxWindow *, Tox *, uint32_t, const char *, size_t, const char *, size_t); + void(*onGroupInvite)(ToxWindow *, Toxic *, uint32_t, const char *, size_t, const char *, size_t); void(*onGroupMessage)(ToxWindow *, Tox *, uint32_t, uint32_t, TOX_MESSAGE_TYPE, const char *, size_t); void(*onGroupPrivateMessage)(ToxWindow *, Tox *, uint32_t, uint32_t, const char *, size_t); void(*onGroupPeerJoin)(ToxWindow *, Tox *, uint32_t, uint32_t); @@ -206,22 +206,22 @@ struct ToxWindow { void(*onGroupPrivacyState)(ToxWindow *, Tox *, uint32_t, Tox_Group_Privacy_State); void(*onGroupTopicLock)(ToxWindow *, Tox *, uint32_t, Tox_Group_Topic_Lock); void(*onGroupPassword)(ToxWindow *, Tox *, uint32_t, const char *, size_t); - void(*onGroupSelfJoin)(ToxWindow *, Tox *, uint32_t); + void(*onGroupSelfJoin)(ToxWindow *, Toxic *, uint32_t); void(*onGroupRejected)(ToxWindow *, Tox *, uint32_t, Tox_Group_Join_Fail); void(*onGroupModeration)(ToxWindow *, Tox *, uint32_t, uint32_t, uint32_t, Tox_Group_Mod_Event); void(*onGroupVoiceState)(ToxWindow *, Tox *, uint32_t, Tox_Group_Voice_State); #ifdef AUDIO - void(*onInvite)(ToxWindow *, ToxAV *, uint32_t, int); - void(*onRinging)(ToxWindow *, ToxAV *, uint32_t, int); - void(*onStarting)(ToxWindow *, ToxAV *, uint32_t, int); - void(*onEnding)(ToxWindow *, ToxAV *, uint32_t, int); - void(*onError)(ToxWindow *, ToxAV *, uint32_t, int); - void(*onStart)(ToxWindow *, ToxAV *, uint32_t, int); - void(*onCancel)(ToxWindow *, ToxAV *, uint32_t, int); - void(*onReject)(ToxWindow *, ToxAV *, uint32_t, int); - void(*onEnd)(ToxWindow *, ToxAV *, uint32_t, int); + void(*onInvite)(ToxWindow *, Toxic *, uint32_t, int); + void(*onRinging)(ToxWindow *, Toxic *, uint32_t, int); + void(*onStarting)(ToxWindow *, Toxic *, uint32_t, int); + void(*onEnding)(ToxWindow *, Toxic *, uint32_t, int); + void(*onError)(ToxWindow *, Toxic *, uint32_t, int); + void(*onStart)(ToxWindow *, Toxic *, uint32_t, int); + void(*onCancel)(ToxWindow *, Toxic *, uint32_t, int); + void(*onReject)(ToxWindow *, Toxic *, uint32_t, int); + void(*onEnd)(ToxWindow *, Toxic *, uint32_t, int); void(*onWriteDevice)(ToxWindow *, Tox *, uint32_t, int, const int16_t *, unsigned int, uint8_t, unsigned int); bool is_call; @@ -328,9 +328,9 @@ struct Help { bool active; }; -ToxWindow *init_windows(Tox *tox); -void draw_active_window(Tox *tox); -int add_window(Tox *tox, ToxWindow *w); +ToxWindow *init_windows(Toxic *toxic); +void draw_active_window(Toxic *toxic); +int add_window(Toxic *toxic, ToxWindow *w); void del_window(ToxWindow *w); void set_active_window_index(uint8_t index); int get_num_active_windows(void);