From b435a71f669df76945575324440704b098201b7a Mon Sep 17 00:00:00 2001 From: jfreegman Date: Mon, 22 Jan 2024 16:51:52 -0500 Subject: [PATCH] refactor: client settings object is no longer global --- src/api.c | 28 ++-- src/api.h | 2 +- src/audio_call.c | 288 +++++++++++++++++++-------------- src/audio_call.h | 4 +- src/audio_device.c | 20 +-- src/audio_device.h | 8 +- src/autocomplete.c | 25 +-- src/autocomplete.h | 3 +- src/avatars.c | 26 +-- src/avatars.h | 6 +- src/bootstrap.c | 24 +-- src/bootstrap.h | 2 +- src/chat.c | 329 +++++++++++++++++++++----------------- src/chat_commands.c | 135 +++++++++------- src/conference.c | 144 ++++++++++------- src/conference.h | 7 +- src/conference_commands.c | 72 +++++---- src/execute.c | 2 +- src/file_transfers.c | 28 ++-- src/file_transfers.h | 8 +- src/friendlist.c | 88 +++++----- src/friendlist.h | 2 +- src/game_base.c | 38 +++-- src/game_base.h | 4 +- src/global_commands.c | 229 ++++++++++++++------------ src/groupchat_commands.c | 283 ++++++++++++++++++-------------- src/groupchats.c | 299 ++++++++++++++++++---------------- src/groupchats.h | 3 +- src/input.c | 56 ++++--- src/input.h | 6 +- src/line_info.c | 73 ++++----- src/line_info.h | 11 +- src/log.c | 44 ++--- src/log.h | 14 +- src/message_queue.c | 7 +- src/message_queue.h | 2 +- src/misc_tools.c | 16 +- src/misc_tools.h | 8 +- src/name_lookup.c | 61 +++---- src/notify.c | 74 +++++---- src/notify.h | 19 ++- src/prompt.c | 131 ++++++++------- src/prompt.h | 6 +- src/qr_code.c | 2 +- src/settings.c | 10 +- src/settings.h | 10 +- src/term_mplex.c | 5 +- src/toxic.c | 194 +++++++++++----------- src/toxic.h | 70 +------- src/toxic_constants.h | 92 +++++++++++ src/toxic_strings.c | 4 +- src/toxic_strings.h | 2 +- src/video_call.c | 134 +++++++++------- src/video_call.h | 7 +- src/video_device.c | 30 ++-- src/video_device.h | 13 +- src/windows.c | 51 +++--- src/windows.h | 7 +- 58 files changed, 1815 insertions(+), 1451 deletions(-) create mode 100644 src/toxic_constants.h diff --git a/src/api.c b/src/api.c index 1f1509319..55c7be01d 100644 --- a/src/api.c +++ b/src/api.c @@ -48,7 +48,7 @@ void api_display(const char *const msg) } self_window = get_active_window(); - line_info_add(self_window, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); + line_info_add(self_window, user_toxic->c_config, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); } FriendsList api_get_friendslist(void) @@ -105,7 +105,7 @@ void api_send(const char *msg) snprintf((char *) self_window->chatwin->line, sizeof(self_window->chatwin->line), "%s", msg); add_line_to_hist(self_window->chatwin); - int id = line_info_add(self_window, true, name, NULL, OUT_MSG, 0, 0, "%s", msg); + const int id = line_info_add(self_window, user_toxic->c_config, true, name, NULL, OUT_MSG, 0, 0, "%s", msg); cqueue_add(self_window->chatwin->cqueue, msg, strlen(msg), OUT_MSG, id); free(name); } @@ -138,7 +138,11 @@ void draw_handler_help(WINDOW *win) void cmd_run(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { - UNUSED_VAR(toxic); + if (toxic == NULL || self == NULL) { + return; + } + + const Client_Config *c_config = toxic->c_config; FILE *fp; const char *error_str; @@ -153,7 +157,7 @@ void cmd_run(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg error_str = "Only one argument allowed."; } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, error_str); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, error_str); return; } @@ -162,7 +166,7 @@ void cmd_run(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg if (fp == NULL) { error_str = "Path does not exist."; - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, error_str); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, error_str); return; } @@ -170,19 +174,23 @@ void cmd_run(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg fclose(fp); } -void invoke_autoruns(WINDOW *window, ToxWindow *self) +void invoke_autoruns(WINDOW *window, ToxWindow *self, const char *autorun_path) { char abspath_buf[PATH_MAX + 256]; char err_buf[PATH_MAX + 128]; - if (user_settings->autorun_path[0] == '\0') { + if (autorun_path == NULL) { + return; + } + + if (string_is_empty(autorun_path)) { return; } - DIR *d = opendir(user_settings->autorun_path); + DIR *d = opendir(autorun_path); if (d == NULL) { - snprintf(err_buf, sizeof(err_buf), "Autorun path does not exist: %s", user_settings->autorun_path); + snprintf(err_buf, sizeof(err_buf), "Autorun path does not exist: %s", autorun_path); api_display(err_buf); return; } @@ -197,7 +205,7 @@ void invoke_autoruns(WINDOW *window, ToxWindow *self) size_t path_len = strlen(dir->d_name); if (!strcmp(dir->d_name + path_len - 3, ".py")) { - snprintf(abspath_buf, sizeof(abspath_buf), "%s%s", user_settings->autorun_path, dir->d_name); + snprintf(abspath_buf, sizeof(abspath_buf), "%s%s", autorun_path, dir->d_name); FILE *fp = fopen(abspath_buf, "r"); if (fp == NULL) { diff --git a/src/api.h b/src/api.h index 2b44e9750..9276c63c9 100644 --- a/src/api.h +++ b/src/api.h @@ -37,7 +37,7 @@ int do_plugin_command(int num_args, char (*args)[MAX_STR_SIZE]); int num_registered_handlers(void); int help_max_width(void); void draw_handler_help(WINDOW *win); -void invoke_autoruns(WINDOW *w, ToxWindow *self); +void invoke_autoruns(WINDOW *w, ToxWindow *self, const char *autorun_path); void cmd_run(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]); #endif /* API_H */ diff --git a/src/audio_call.c b/src/audio_call.c index d5b3b3f3f..4a2ca5a20 100644 --- a/src/audio_call.c +++ b/src/audio_call.c @@ -68,26 +68,31 @@ void on_audio_receive_frame(ToxAV *av, uint32_t friend_number, int16_t const *pc uint8_t channels, uint32_t sampling_rate, void *user_data); 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); -void callback_call_started(uint32_t friend_number); -void callback_call_canceled(uint32_t friend_number); -void callback_call_rejected(uint32_t friend_number); -void callback_call_ended(uint32_t friend_number); +void callback_recv_ringing(Toxic *toxic, uint32_t friend_number); +void callback_recv_starting(Toxic *toxic, uint32_t friend_number); +void callback_call_started(Toxic *toxic, uint32_t friend_number); +void callback_call_canceled(Toxic *toxic, uint32_t friend_number); +void callback_call_rejected(Toxic *toxic, uint32_t friend_number); +void callback_call_ended(Toxic *toxic, uint32_t friend_number); void write_device_callback(uint32_t friend_number, const int16_t *PCM, uint16_t sample_count, uint8_t channels, uint32_t sample_rate); void audio_bit_rate_callback(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, void *user_data); -static void print_err(ToxWindow *self, const char *error_str) +static void print_err(ToxWindow *self, const Client_Config *c_config, const char *error_str) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", error_str); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", error_str); } ToxAV *init_audio(ToxWindow *self, Toxic *toxic) { + if (toxic == NULL) { + return NULL; + } + + const Client_Config *c_config = toxic->c_config; + Toxav_Err_New error; CallControl.audio_errors = ae_None; CallControl.prompt = self; @@ -98,7 +103,7 @@ ToxAV *init_audio(ToxWindow *self, Toxic *toxic) CallControl.default_audio_bit_rate = 64; CallControl.audio_sample_rate = 48000; CallControl.audio_frame_duration = 20; - CallControl.audio_channels = user_settings->chat_audio_channels; + CallControl.audio_channels = c_config->chat_audio_channels; CallControl.video_enabled = false; CallControl.default_video_bit_rate = 0; @@ -106,23 +111,23 @@ ToxAV *init_audio(ToxWindow *self, Toxic *toxic) if (toxic->av == NULL) { CallControl.audio_errors |= ae_StartingCoreAudio; - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to init ToxAV"); + line_info_add(self, c_config, 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"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to init devices"); toxav_kill(toxic->av); toxic->av = NULL; return NULL; } toxav_callback_call(toxic->av, on_call, (void *) toxic); - toxav_callback_call_state(toxic->av, on_call_state, NULL); + toxav_callback_call_state(toxic->av, on_call_state, (void *) toxic); toxav_callback_audio_receive_frame(toxic->av, on_audio_receive_frame, NULL); toxav_callback_audio_bit_rate(toxic->av, audio_bit_rate_callback, NULL); - CallControl.av = toxic->av; + CallControl.av = toxic->av; // TODO: get rid of this return toxic->av; } @@ -191,41 +196,52 @@ static bool cancel_call(Call *call) return true; } -static int start_transmission(ToxWindow *self, Call *call) +static int start_transmission(ToxWindow *self, Toxic *toxic, Call *call) { - if (!self || !CallControl.av) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to prepare audio transmission"); + if (self == NULL || toxic == NULL) { + return -1; + } + + const Client_Config *c_config = toxic->c_config; + + if (toxic->av == NULL) { + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to prepare audio transmission"); return -1; } DeviceError error = open_input_device(&call->in_idx, read_device_callback, &self->num, - CallControl.audio_sample_rate, CallControl.audio_frame_duration, CallControl.audio_channels); + CallControl.audio_sample_rate, CallControl.audio_frame_duration, + CallControl.audio_channels, c_config->VAD_threshold); if (error != de_None) { if (error == de_FailedStart) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to start audio input device"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to start audio input device"); } if (error == de_InternalError) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Internal error with opening audio input device"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Internal error with opening audio input device"); } } if (open_output_device(&call->out_idx, - CallControl.audio_sample_rate, CallControl.audio_frame_duration, CallControl.audio_channels) != de_None) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to open audio output device!"); + CallControl.audio_sample_rate, + CallControl.audio_frame_duration, + CallControl.audio_channels, + c_config->VAD_threshold) != de_None) { + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to open audio output device!"); } return 0; } -static void start_call(ToxWindow *self, Call *call) +static void start_call(ToxWindow *self, Toxic *toxic, Call *call) { if (call->status != cs_Pending) { return; } - if (start_transmission(self, call) != 0) { + if (start_transmission(self, toxic, call) != 0) { return; } @@ -238,7 +254,7 @@ static void start_call(ToxWindow *self, Call *call) } if (call->video_bit_rate) { - start_video_transmission(self, CallControl.av, call); + start_video_transmission(self, toxic, call); } #endif @@ -321,8 +337,13 @@ void on_call(ToxAV *av, uint32_t friend_number, bool audio_enabled, bool video_e void on_call_state(ToxAV *av, uint32_t friend_number, uint32_t state, void *user_data) { + Toxic *toxic = (Toxic *) user_data; + + if (toxic == NULL) { + return; + } + UNUSED_VAR(av); - UNUSED_VAR(user_data); Call *call = &CallControl.calls[friend_number]; @@ -336,12 +357,13 @@ void on_call_state(ToxAV *av, uint32_t friend_number, uint32_t state, void *user case TOXAV_FRIEND_CALL_STATE_ERROR: case TOXAV_FRIEND_CALL_STATE_FINISHED: if (state == TOXAV_FRIEND_CALL_STATE_ERROR) { - line_info_add(CallControl.prompt, false, NULL, NULL, SYS_MSG, 0, 0, "ToxAV callstate error!"); + line_info_add(CallControl.prompt, toxic->c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "ToxAV callstate error!"); } if (call->status == cs_Pending) { cancel_call(call); - callback_call_rejected(friend_number); + callback_call_rejected(toxic, friend_number); } else { #ifdef VIDEO @@ -350,7 +372,7 @@ void on_call_state(ToxAV *av, uint32_t friend_number, uint32_t state, void *user #endif /* VIDEO */ stop_transmission(call, friend_number); - callback_call_ended(friend_number); + callback_call_ended(toxic, friend_number); } break; @@ -358,7 +380,7 @@ void on_call_state(ToxAV *av, uint32_t friend_number, uint32_t state, void *user default: if (call->status == cs_Pending) { /* Start answered call */ - callback_call_started(friend_number); + callback_call_started(toxic, friend_number); } #ifdef VIDEO @@ -417,80 +439,70 @@ void callback_recv_invite(Toxic *toxic, uint32_t 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], NULL, friend_number, call->state); + windows[i]->onInvite(windows[i], toxic, friend_number, call->state); } } } -void callback_recv_ringing(uint32_t friend_number) +void callback_recv_ringing(Toxic *toxic, uint32_t friend_number) { const Call *call = &CallControl.calls[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], NULL, friend_number, call->state); + windows[i]->onRinging(windows[i], toxic, friend_number, call->state); } } } -void callback_recv_starting(uint32_t friend_number) +void callback_recv_starting(Toxic *toxic, uint32_t friend_number) { Call *call = &CallControl.calls[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], NULL, friend_number, call->state); - start_call(windows[i], call); + windows[i]->onStarting(windows[i], toxic, friend_number, call->state); + start_call(windows[i], toxic, call); } } } -void callback_recv_ending(uint32_t friend_number) -{ - const Call *call = &CallControl.calls[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], NULL, friend_number, call->state); - } - } -} -void callback_call_started(uint32_t friend_number) +void callback_call_started(Toxic *toxic, uint32_t friend_number) { Call *call = &CallControl.calls[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], NULL, friend_number, call->state); + windows[i]->onStart(windows[i], toxic, friend_number, call->state); - start_call(windows[i], call); + start_call(windows[i], toxic, call); } } } -void callback_call_canceled(uint32_t friend_number) +void callback_call_canceled(Toxic *toxic, uint32_t friend_number) { const Call *call = &CallControl.calls[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], NULL, friend_number, call->state); + windows[i]->onCancel(windows[i], toxic, friend_number, call->state); } } } -void callback_call_rejected(uint32_t friend_number) +void callback_call_rejected(Toxic *toxic, uint32_t friend_number) { const Call *call = &CallControl.calls[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], NULL, friend_number, call->state); + windows[i]->onReject(windows[i], toxic, friend_number, call->state); } } } -void callback_call_ended(uint32_t friend_number) +void callback_call_ended(Toxic *toxic, uint32_t friend_number) { const Call *call = &CallControl.calls[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], NULL, friend_number, call->state); + windows[i]->onEnd(windows[i], toxic, friend_number, call->state); } } } @@ -508,35 +520,37 @@ void cmd_call(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar UNUSED_VAR(window); UNUSED_VAR(argv); - if (toxic == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; + if (argc != 0) { - print_err(self, "Unknown arguments."); + print_err(self, c_config, "Unknown arguments."); return; } if (toxic->av == NULL) { - print_err(self, "ToxAV not supported!"); + print_err(self, c_config, "ToxAV not supported!"); return; } if (!self->stb->connection) { - print_err(self, "Friend is offline."); + print_err(self, c_config, "Friend is offline."); return; } Call *call = &CallControl.calls[self->num]; if (call->status != cs_None) { - print_err(self, "Already calling."); + print_err(self, c_config, "Already calling."); return; } init_call(call); - place_call(self); + place_call(self, toxic); } void cmd_answer(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) @@ -544,26 +558,28 @@ void cmd_answer(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* UNUSED_VAR(window); UNUSED_VAR(argv); - if (toxic == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; + Toxav_Err_Answer error; if (argc != 0) { - print_err(self, "Unknown arguments."); + print_err(self, c_config, "Unknown arguments."); return; } if (toxic->av == NULL) { - print_err(self, "Audio not supported!"); + print_err(self, c_config, "Audio not supported!"); return; } Call *call = &CallControl.calls[self->num]; if (call->status != cs_Pending) { - print_err(self, "No incoming call!"); + print_err(self, c_config, "No incoming call!"); return; } @@ -571,22 +587,22 @@ void cmd_answer(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* if (error != TOXAV_ERR_ANSWER_OK) { if (error == TOXAV_ERR_ANSWER_FRIEND_NOT_CALLING) { - print_err(self, "No incoming call!"); + print_err(self, c_config, "No incoming call!"); } else if (error == TOXAV_ERR_ANSWER_CODEC_INITIALIZATION) { - print_err(self, "Failed to initialize codecs!"); + print_err(self, c_config, "Failed to initialize codecs!"); } else if (error == TOXAV_ERR_ANSWER_FRIEND_NOT_FOUND) { - print_err(self, "Friend not found!"); + print_err(self, c_config, "Friend not found!"); } else if (error == TOXAV_ERR_ANSWER_INVALID_BIT_RATE) { - print_err(self, "Invalid bit rate!"); + print_err(self, c_config, "Invalid bit rate!"); } else { - print_err(self, "Internal error!"); + print_err(self, c_config, "Internal error!"); } return; } /* Callback will print status... */ - callback_recv_starting(self->num); + callback_recv_starting(toxic, self->num); } void cmd_reject(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) @@ -594,24 +610,26 @@ void cmd_reject(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* UNUSED_VAR(window); UNUSED_VAR(argv); - if (toxic == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; + if (argc != 0) { - print_err(self, "Unknown arguments."); + print_err(self, c_config, "Unknown arguments."); return; } if (toxic->av == NULL) { - print_err(self, "Audio not supported!"); + print_err(self, c_config, "Audio not supported!"); return; } Call *call = &CallControl.calls[self->num]; if (call->status != cs_Pending) { - print_err(self, "No incoming call!"); + print_err(self, c_config, "No incoming call!"); return; } @@ -620,7 +638,7 @@ void cmd_reject(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* cancel_call(call); /* Callback will print status... */ - callback_call_rejected(self->num); + callback_call_rejected(toxic, self->num); } void cmd_hangup(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) @@ -628,40 +646,47 @@ void cmd_hangup(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* UNUSED_VAR(window); UNUSED_VAR(argv); - if (toxic == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; + if (toxic->av == NULL) { - print_err(self, "Audio not supported!"); + print_err(self, c_config, "Audio not supported!"); return; } if (argc != 0) { - print_err(self, "Unknown arguments."); + print_err(self, c_config, "Unknown arguments."); return; } Call *call = &CallControl.calls[self->num]; if (call->status == cs_None) { - print_err(self, "Not in a call."); + print_err(self, c_config, "Not in a call."); return; } - stop_current_call(self); + stop_current_call(self, toxic); } void cmd_list_devices(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(toxic); + + if (toxic == NULL || self == NULL) { + return; + } + + const Client_Config *c_config = toxic->c_config; if (argc != 1) { if (argc < 1) { - print_err(self, "Type must be specified!"); + print_err(self, c_config, "Type must be specified!"); } else { - print_err(self, "Only one argument allowed!"); + print_err(self, c_config, "Only one argument allowed!"); } return; @@ -678,29 +703,34 @@ void cmd_list_devices(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, c } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid type: %s", argv[1]); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid type: %s", argv[1]); return; } // Refresh device list. get_al_device_names(); - print_al_devices(self, type); + print_al_devices(self, c_config, type); } /* This changes primary device only */ void cmd_change_device(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(toxic); + + if (toxic == NULL || self == NULL) { + return; + } + + const Client_Config *c_config = toxic->c_config; if (argc != 2) { if (argc < 1) { - print_err(self, "Type must be specified!"); + print_err(self, c_config, "Type must be specified!"); } else if (argc < 2) { - print_err(self, "Must have id!"); + print_err(self, c_config, "Must have id!"); } else { - print_err(self, "Only two arguments allowed!"); + print_err(self, c_config, "Only two arguments allowed!"); } return; @@ -717,7 +747,7 @@ void cmd_change_device(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid type: %s", argv[1]); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid type: %s", argv[1]); return; } @@ -726,12 +756,12 @@ void cmd_change_device(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, long int selection = strtol(argv[2], &end, 10); if (*end) { - print_err(self, "Invalid input"); + print_err(self, c_config, "Invalid input"); return; } if (set_al_device(type, selection) == de_InvalidSelection) { - print_err(self, "Invalid selection!"); + print_err(self, c_config, "Invalid selection!"); return; } } @@ -739,10 +769,15 @@ void cmd_change_device(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, void cmd_mute(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(toxic); + + if (toxic == NULL || self == NULL) { + return; + } + + const Client_Config *c_config = toxic->c_config; if (argc != 1) { - print_err(self, "Specify type: \"/mute in\" or \"/mute out\"."); + print_err(self, c_config, "Specify type: \"/mute in\" or \"/mute out\"."); return; } @@ -757,7 +792,7 @@ void cmd_mute(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid type: %s", argv[1]); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid type: %s", argv[1]); return; } @@ -781,13 +816,18 @@ void cmd_mute(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar void cmd_sense(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(toxic); + + if (toxic == NULL || self == NULL) { + return; + } + + const Client_Config *c_config = toxic->c_config; if (argc != 1) { if (argc < 1) { - print_err(self, "Must have value!"); + print_err(self, c_config, "Must have value!"); } else { - print_err(self, "Only two arguments allowed!"); + print_err(self, c_config, "Only two arguments allowed!"); } return; @@ -797,7 +837,7 @@ void cmd_sense(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*a float value = strtof(argv[1], &end); if (*end) { - print_err(self, "Invalid input"); + print_err(self, c_config, "Invalid input"); return; } @@ -816,25 +856,27 @@ void cmd_bitrate(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char ( { UNUSED_VAR(window); - if (toxic == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; + Call *call = &CallControl.calls[self->num]; if (call->status != cs_Active) { - print_err(self, "Must be in a call"); + print_err(self, c_config, "Must be in a call"); return; } if (argc == 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Current audio encoding bitrate: %u", call->audio_bit_rate); return; } if (argc > 1) { - print_err(self, "Too many arguments."); + print_err(self, c_config, "Too many arguments."); return; } @@ -842,7 +884,7 @@ void cmd_bitrate(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char ( const long int bit_rate = strtol(argv[1], &end, 10); if (*end || bit_rate < 0 || bit_rate > UINT32_MAX) { - print_err(self, "Invalid input"); + print_err(self, c_config, "Invalid input"); return; } @@ -851,15 +893,15 @@ void cmd_bitrate(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char ( if (error != TOXAV_ERR_BIT_RATE_SET_OK) { if (error == TOXAV_ERR_BIT_RATE_SET_SYNC) { - print_err(self, "Synchronization error occured"); + print_err(self, c_config, "Synchronization error occured"); } else if (error == TOXAV_ERR_BIT_RATE_SET_INVALID_BIT_RATE) { - print_err(self, "Invalid audio bit rate value (valid is 6-510)"); + print_err(self, c_config, "Invalid audio bit rate value (valid is 6-510)"); } else if (error == TOXAV_ERR_BIT_RATE_SET_FRIEND_NOT_FOUND) { - print_err(self, "Friend not found"); + print_err(self, c_config, "Friend not found"); } else if (error == TOXAV_ERR_BIT_RATE_SET_FRIEND_NOT_IN_CALL) { - print_err(self, "Friend is not in the call"); + print_err(self, c_config, "Friend is not in the call"); } else { - print_err(self, "Unknown error"); + print_err(self, c_config, "Unknown error"); } return; @@ -870,7 +912,7 @@ void cmd_bitrate(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char ( return; } -void place_call(ToxWindow *self) +void place_call(ToxWindow *self, Toxic *toxic) { Call *call = &CallControl.calls[self->num]; @@ -878,39 +920,41 @@ void place_call(ToxWindow *self) return; } + const Client_Config *c_config = toxic->c_config; + Toxav_Err_Call error; - toxav_call(CallControl.av, self->num, call->audio_bit_rate, call->video_bit_rate, &error); + toxav_call(toxic->av, self->num, call->audio_bit_rate, call->video_bit_rate, &error); if (error != TOXAV_ERR_CALL_OK) { if (error == TOXAV_ERR_CALL_FRIEND_ALREADY_IN_CALL) { - print_err(self, "Already in a call!"); + print_err(self, c_config, "Already in a call!"); } else if (error == TOXAV_ERR_CALL_MALLOC) { - print_err(self, "Memory allocation issue"); + print_err(self, c_config, "Memory allocation issue"); } else if (error == TOXAV_ERR_CALL_FRIEND_NOT_FOUND) { - print_err(self, "Friend number invalid"); + print_err(self, c_config, "Friend number invalid"); } else if (error == TOXAV_ERR_CALL_FRIEND_NOT_CONNECTED) { - print_err(self, "Friend is valid but not currently connected"); + print_err(self, c_config, "Friend is valid but not currently connected"); } else { - print_err(self, "Internal error!"); + print_err(self, c_config, "Internal error!"); } cancel_call(call); return; } - callback_recv_ringing(self->num); + callback_recv_ringing(toxic, self->num); } -void stop_current_call(ToxWindow *self) +void stop_current_call(ToxWindow *self, Toxic *toxic) { Call *call = &CallControl.calls[self->num]; if (call->status == cs_Pending) { - 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_call_canceled(self->num); + callback_call_canceled(toxic, self->num); } else { #ifdef VIDEO @@ -919,7 +963,7 @@ void stop_current_call(ToxWindow *self) #endif /* VIDEO */ stop_transmission(call, self->num); - callback_call_ended(self->num); + callback_call_ended(toxic, self->num); } } diff --git a/src/audio_call.h b/src/audio_call.h index a0b811f05..0831e60d0 100644 --- a/src/audio_call.h +++ b/src/audio_call.h @@ -100,8 +100,8 @@ void terminate_audio(ToxAV *av); bool init_call(Call *call); -void place_call(ToxWindow *self); -void stop_current_call(ToxWindow *self); +void place_call(ToxWindow *self, Toxic *toxic); +void stop_current_call(ToxWindow *self, Toxic *toxic); void init_friend_AV(uint32_t index); void del_friend_AV(uint32_t index); diff --git a/src/audio_device.c b/src/audio_device.c index 642838b4b..d0aaba637 100644 --- a/src/audio_device.c +++ b/src/audio_device.c @@ -40,7 +40,6 @@ #include #include -extern struct user_settings *user_settings; extern struct Winthread Winthread; typedef struct FrameInfo { @@ -484,7 +483,7 @@ DeviceError set_al_device(DeviceType type, int32_t selection) } static DeviceError open_device(DeviceType type, uint32_t *device_idx, DataHandleCallback cb, void *cb_data, - uint32_t sample_rate, uint32_t frame_duration, uint8_t channels) + uint32_t sample_rate, uint32_t frame_duration, uint8_t channels, double VAD_threshold) { if (channels != 1 && channels != 2) { return de_UnsupportedMode; @@ -529,8 +528,8 @@ static DeviceError open_device(DeviceType type, uint32_t *device_idx, DataHandle device->cb_data = cb_data; #ifdef AUDIO - if (user_settings->VAD_threshold >= 0.0) { - device->VAD_threshold = user_settings->VAD_threshold; + if (VAD_threshold >= 0.0) { + device->VAD_threshold = VAD_threshold; } #else @@ -550,14 +549,15 @@ static DeviceError open_device(DeviceType type, uint32_t *device_idx, DataHandle } DeviceError open_input_device(uint32_t *device_idx, DataHandleCallback cb, void *cb_data, uint32_t sample_rate, - uint32_t frame_duration, uint8_t channels) + uint32_t frame_duration, uint8_t channels, double VAD_threshold) { - return open_device(input, device_idx, cb, cb_data, sample_rate, frame_duration, channels); + return open_device(input, device_idx, cb, cb_data, sample_rate, frame_duration, channels, VAD_threshold); } -DeviceError open_output_device(uint32_t *device_idx, uint32_t sample_rate, uint32_t frame_duration, uint8_t channels) +DeviceError open_output_device(uint32_t *device_idx, uint32_t sample_rate, uint32_t frame_duration, uint8_t channels, + double VAD_threshold) { - return open_device(output, device_idx, 0, 0, sample_rate, frame_duration, channels); + return open_device(output, device_idx, 0, 0, sample_rate, frame_duration, channels, VAD_threshold); } DeviceError close_device(DeviceType type, uint32_t device_idx) @@ -769,10 +769,10 @@ float get_input_volume(void) return ret; } -void print_al_devices(ToxWindow *self, DeviceType type) +void print_al_devices(ToxWindow *self, const Client_Config *c_config, DeviceType type) { for (int i = 0; i < audio_state->num_al_devices[type]; ++i) { - line_info_add(self, false, NULL, NULL, SYS_MSG, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, audio_state->current_al_device_name[type] && strcmp(audio_state->current_al_device_name[type], audio_state->al_device_names[type][i]) == 0 ? 1 : 0, 0, "%d: %s", i, audio_state->al_device_names[type][i]); diff --git a/src/audio_device.h b/src/audio_device.h index c02f56dd1..016cb9475 100644 --- a/src/audio_device.h +++ b/src/audio_device.h @@ -33,6 +33,7 @@ #define MAX_OPENAL_DEVICES 32 #define MAX_DEVICES 32 +#include "settings.h" #include "windows.h" typedef enum DeviceType { @@ -76,8 +77,9 @@ DeviceError set_al_device(DeviceType type, int32_t selection); /* Start device */ DeviceError open_input_device(uint32_t *device_idx, DataHandleCallback cb, void *cb_data, - uint32_t sample_rate, uint32_t frame_duration, uint8_t channels); -DeviceError open_output_device(uint32_t *device_idx, uint32_t sample_rate, uint32_t frame_duration, uint8_t channels); + uint32_t sample_rate, uint32_t frame_duration, uint8_t channels, double VAD_threshold); +DeviceError open_output_device(uint32_t *device_idx, uint32_t sample_rate, uint32_t frame_duration, uint8_t channels, + double VAD_threshold); /* Stop device */ DeviceError close_device(DeviceType type, uint32_t device_idx); @@ -89,7 +91,7 @@ DeviceError write_out(uint32_t device_idx, const int16_t *data, uint32_t length, /* return current input volume as float in range 0.0-100.0 */ float get_input_volume(void); -void print_al_devices(ToxWindow *self, DeviceType type); +void print_al_devices(ToxWindow *self, const Client_Config *c_config, DeviceType type); DeviceError selection_valid(DeviceType type, int32_t selection); #endif /* AUDIO_DEVICE_H */ diff --git a/src/autocomplete.c b/src/autocomplete.c index bce010a1e..223dc58cd 100644 --- a/src/autocomplete.c +++ b/src/autocomplete.c @@ -38,17 +38,17 @@ #include "toxic.h" #include "windows.h" -static void print_ac_matches(ToxWindow *self, Toxic *toxic, char **list, size_t n_matches) +static void print_ac_matches(ToxWindow *self, Toxic *toxic, char **list, size_t n_matches, bool have_matches) { - if (toxic != NULL) { + if (have_matches) { execute(self->chatwin->history, self, toxic, "/clear", GLOBAL_COMMAND_MODE); } for (size_t i = 0; i < n_matches; ++i) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", list[i]); + line_info_add(self, toxic->c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", list[i]); } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, ""); + line_info_add(self, toxic->c_config, false, NULL, NULL, SYS_MSG, 0, 0, ""); } /* puts match in match buffer. if more than one match, add first n chars that are identical. @@ -96,7 +96,8 @@ static size_t get_str_match(ToxWindow *self, char *match, size_t match_sz, const * * Note: This function should not be called directly. Use complete_line() and complete_path() instead. */ -static int complete_line_helper(ToxWindow *self, const char *const *list, const size_t n_items, bool dir_search) +static int complete_line_helper(ToxWindow *self, Toxic *toxic, const char *const *list, const size_t n_items, + bool dir_search) { ChatContext *ctx = self->chatwin; @@ -177,7 +178,7 @@ static int complete_line_helper(ToxWindow *self, const char *const *list, const } if (!dir_search && n_matches > 1) { - print_ac_matches(self, NULL, matches, n_matches); + print_ac_matches(self, toxic, matches, n_matches, false); } char match[MAX_STR_SIZE]; @@ -259,14 +260,14 @@ static int complete_line_helper(ToxWindow *self, const char *const *list, const return diff; } -int complete_line(ToxWindow *self, const char *const *list, size_t n_items) +int complete_line(ToxWindow *self, Toxic *toxic, const char *const *list, size_t n_items) { - return complete_line_helper(self, list, n_items, false); + return complete_line_helper(self, toxic, list, n_items, false); } -static int complete_path(ToxWindow *self, const char *const *list, const size_t n_items) +static int complete_path(ToxWindow *self, Toxic *toxic, const char *const *list, const size_t n_items) { - return complete_line_helper(self, list, n_items, true); + return complete_line_helper(self, toxic, list, n_items, true); } /* Transforms a tab complete starting with the shorthand "~" into the full home directory. */ @@ -382,10 +383,10 @@ int dir_match(ToxWindow *self, Toxic *toxic, const wchar_t *line, const wchar_t if (dircount > 1) { qsort(dirnames, dircount, sizeof(char *), qsort_ptr_char_array_helper); - print_ac_matches(self, toxic, dirnames, dircount); + print_ac_matches(self, toxic, dirnames, dircount, true); } - int ret = complete_path(self, (const char *const *) dirnames, dircount); + const int ret = complete_path(self, toxic, (const char *const *) dirnames, dircount); free_ptr_array((void **) dirnames); diff --git a/src/autocomplete.h b/src/autocomplete.h index 44c9419b5..7c2de57aa 100644 --- a/src/autocomplete.h +++ b/src/autocomplete.h @@ -23,6 +23,7 @@ #ifndef AUTOCOMPLETE_H #define AUTOCOMPLETE_H +#include "toxic.h" #include "windows.h" /* @@ -39,7 +40,7 @@ * * Note: This function should not be called directly. Use complete_line() and complete_path() instead. */ -int complete_line(ToxWindow *self, const char **list, size_t n_items); +int complete_line(ToxWindow *self, Toxic *toxic, const char **list, size_t n_items); /* Attempts to match /command "" line to matching directories. * If there is only one match the line is auto-completed. diff --git a/src/avatars.c b/src/avatars.c index 821cc514a..b018cfb95 100644 --- a/src/avatars.c +++ b/src/avatars.c @@ -180,14 +180,14 @@ void avatar_unset(Tox *tox) avatar_send_all(tox); } -void on_avatar_friend_connection_status(Tox *tox, uint32_t friendnumber, Tox_Connection connection_status) +void on_avatar_friend_connection_status(Toxic *toxic, uint32_t friendnumber, Tox_Connection connection_status) { if (connection_status == TOX_CONNECTION_NONE) { - kill_avatar_file_transfers_friend(tox, friendnumber); + kill_avatar_file_transfers_friend(toxic, friendnumber); } } -void on_avatar_file_control(Tox *tox, struct FileTransfer *ft, Tox_File_Control control) +void on_avatar_file_control(Toxic *toxic, struct FileTransfer *ft, Tox_File_Control control) { switch (control) { case TOX_FILE_CONTROL_RESUME: @@ -204,30 +204,34 @@ void on_avatar_file_control(Tox *tox, struct FileTransfer *ft, Tox_File_Control break; case TOX_FILE_CONTROL_CANCEL: - close_file_transfer(NULL, tox, ft, -1, NULL, silent); + close_file_transfer(NULL, toxic, ft, -1, NULL, silent); break; } } -void on_avatar_chunk_request(Tox *tox, struct FileTransfer *ft, uint64_t position, size_t length) +void on_avatar_chunk_request(Toxic *toxic, struct FileTransfer *ft, uint64_t position, size_t length) { + if (toxic == NULL) { + return; + } + if (ft->state != FILE_TRANSFER_STARTED) { return; } if (length == 0) { - close_file_transfer(NULL, tox, ft, -1, NULL, silent); + close_file_transfer(NULL, toxic, ft, -1, NULL, silent); return; } if (ft->file == NULL) { - close_file_transfer(NULL, tox, ft, TOX_FILE_CONTROL_CANCEL, NULL, silent); + close_file_transfer(NULL, toxic, ft, TOX_FILE_CONTROL_CANCEL, NULL, silent); return; } if (ft->position != position) { if (fseek(ft->file, position, SEEK_SET) == -1) { - close_file_transfer(NULL, tox, ft, TOX_FILE_CONTROL_CANCEL, NULL, silent); + close_file_transfer(NULL, toxic, ft, TOX_FILE_CONTROL_CANCEL, NULL, silent); return; } @@ -237,20 +241,20 @@ void on_avatar_chunk_request(Tox *tox, struct FileTransfer *ft, uint64_t positio uint8_t *send_data = malloc(length); if (send_data == NULL) { - close_file_transfer(NULL, tox, ft, TOX_FILE_CONTROL_CANCEL, NULL, silent); + close_file_transfer(NULL, toxic, ft, TOX_FILE_CONTROL_CANCEL, NULL, silent); return; } size_t send_length = fread(send_data, 1, length, ft->file); if (send_length != length) { - close_file_transfer(NULL, tox, ft, TOX_FILE_CONTROL_CANCEL, NULL, silent); + close_file_transfer(NULL, toxic, ft, TOX_FILE_CONTROL_CANCEL, NULL, silent); free(send_data); return; } Tox_Err_File_Send_Chunk err; - tox_file_send_chunk(tox, ft->friendnumber, ft->filenumber, position, send_data, send_length, &err); + tox_file_send_chunk(toxic->tox, ft->friendnumber, ft->filenumber, position, send_data, send_length, &err); free(send_data); diff --git a/src/avatars.h b/src/avatars.h index 7af9a534b..8cd4335c9 100644 --- a/src/avatars.h +++ b/src/avatars.h @@ -48,8 +48,8 @@ int avatar_set(Tox *tox, const char *path, size_t length); */ void avatar_unset(Tox *tox); -void on_avatar_chunk_request(Tox *tox, struct FileTransfer *ft, uint64_t position, size_t length); -void on_avatar_file_control(Tox *tox, struct FileTransfer *ft, Tox_File_Control control); -void on_avatar_friend_connection_status(Tox *tox, uint32_t friendnumber, Tox_Connection connection_status); +void on_avatar_chunk_request(Toxic *toxic, struct FileTransfer *ft, uint64_t position, size_t length); +void on_avatar_file_control(Toxic *toxic, struct FileTransfer *ft, Tox_File_Control control); +void on_avatar_friend_connection_status(Toxic *toxic, uint32_t friendnumber, Tox_Connection connection_status); #endif /* AVATARS_H */ diff --git a/src/bootstrap.c b/src/bootstrap.c index bc0df018d..0cebfb118 100644 --- a/src/bootstrap.c +++ b/src/bootstrap.c @@ -39,8 +39,6 @@ #include "settings.h" #include "windows.h" -extern struct user_settings *user_settings; - /* URL that we get the JSON encoded nodes list from. */ #define NODES_LIST_URL "https://nodes.tox.chat/json" @@ -148,9 +146,9 @@ static bool node_is_offline(unsigned long long int last_ping) * This will be the case if the file is empty, has an invalid format, * or if the file is older than the given timeout. */ -static bool nodeslist_needs_update(const char *nodes_path) +static bool nodeslist_needs_update(const char *nodes_path, int update_frequency) { - if (user_settings->nodeslist_update_freq <= 0) { + if (update_frequency <= 0) { return false; } @@ -183,7 +181,7 @@ static bool nodeslist_needs_update(const char *nodes_path) pthread_mutex_unlock(&thread_data.lock); pthread_mutex_lock(&Winthread.lock); - bool is_timeout = timed_out(last_scan, user_settings->nodeslist_update_freq * 24 * 60 * 60); + bool is_timeout = timed_out(last_scan, update_frequency * 24 * 60 * 60); pthread_mutex_unlock(&Winthread.lock); if (is_timeout) { @@ -314,9 +312,9 @@ static int curl_fetch_nodes_JSON(struct Recv_Curl_Data *recv_data) * Return -4 if data could not be written to disk. * Return -5 if memory allocation fails. */ -static int update_DHT_nodeslist(const char *nodes_path) +static int update_DHT_nodeslist(const char *nodes_path, int update_frequency) { - if (!nodeslist_needs_update(nodes_path)) { + if (!nodeslist_needs_update(nodes_path, update_frequency)) { return 0; } @@ -513,7 +511,11 @@ static int extract_node(const char *line, struct Node *node) /* Loads the DHT nodeslist to memory from json encoded nodes file. */ void *load_nodeslist_thread(void *data) { - UNUSED_VAR(data); + const Client_Config *c_config = (Client_Config *) data; + + if (c_config == NULL) { + goto on_exit; + } char nodes_path[PATH_MAX]; get_nodeslist_path(nodes_path, sizeof(nodes_path)); @@ -530,7 +532,7 @@ void *load_nodeslist_thread(void *data) goto on_exit; } - int update_err = update_DHT_nodeslist(nodes_path); + const int update_err = update_DHT_nodeslist(nodes_path, c_config->nodeslist_update_freq); if (update_err < 0) { fprintf(stderr, "update_DHT_nodeslist() failed with error %d\n", update_err); @@ -598,7 +600,7 @@ void *load_nodeslist_thread(void *data) * Return -4 if pthread fails to set detached state. * Return -5 if thread creation fails. */ -int load_DHT_nodeslist(void) +int load_DHT_nodeslist(const Client_Config *c_config) { if (thread_data.active) { return -1; @@ -618,7 +620,7 @@ int load_DHT_nodeslist(void) thread_data.active = true; - if (pthread_create(&thread_data.tid, &thread_data.attr, load_nodeslist_thread, NULL) != 0) { + if (pthread_create(&thread_data.tid, &thread_data.attr, load_nodeslist_thread, (void *) c_config) != 0) { thread_data.active = false; return -5; } diff --git a/src/bootstrap.h b/src/bootstrap.h index 653d9b35c..d1a6d97d0 100644 --- a/src/bootstrap.h +++ b/src/bootstrap.h @@ -37,6 +37,6 @@ void do_tox_connection(Tox *tox); * Return -4 if pthread fails to set detached state. * Return -5 if thread creation fails. */ -int load_DHT_nodeslist(void); +int load_DHT_nodeslist(const Client_Config *c_config); #endif /* BOOTSTRAP_H */ diff --git a/src/chat.c b/src/chat.c index 33f0e8597..f3c014668 100644 --- a/src/chat.c +++ b/src/chat.c @@ -58,7 +58,7 @@ #endif /* AUDIO */ #ifdef AUDIO -static void init_infobox(ToxWindow *self); +static void init_infobox(ToxWindow *self, double VAD_threshold); static void kill_infobox(ToxWindow *self); #endif /* AUDIO */ @@ -127,16 +127,16 @@ static const char *chat_cmd_list[] = { #endif /* PYTHON */ }; -static void set_self_typingstatus(ToxWindow *self, Tox *tox, bool is_typing) +static void set_self_typingstatus(ToxWindow *self, Toxic *toxic, bool is_typing) { - if (user_settings->show_typing_self == SHOW_TYPING_OFF) { + if (toxic->c_config->show_typing_self == SHOW_TYPING_OFF) { return; } ChatContext *ctx = self->chatwin; Tox_Err_Set_Typing err; - tox_self_set_typing(tox, self->num, is_typing, &err); + tox_self_set_typing(toxic->tox, self->num, is_typing, &err); if (err != TOX_ERR_SET_TYPING_OK) { fprintf(stderr, "Warning: tox_self_set_typing() failed with error %d\n", err); @@ -146,16 +146,16 @@ static void set_self_typingstatus(ToxWindow *self, Tox *tox, bool is_typing) ctx->self_is_typing = is_typing; } -void kill_chat_window(ToxWindow *self, Tox *tox) +void kill_chat_window(ToxWindow *self, Toxic *toxic) { ChatContext *ctx = self->chatwin; StatusBar *statusbar = self->stb; #ifdef AUDIO - stop_current_call(self); + stop_current_call(self, toxic); #endif /* AUDIO */ - kill_all_file_transfers_friend(tox, self->num); + kill_all_file_transfers_friend(toxic, self->num); log_disable(ctx->log); line_info_cleanup(ctx->hst); cqueue_cleanup(ctx->cqueue); @@ -171,37 +171,39 @@ void kill_chat_window(ToxWindow *self, Tox *tox) disable_chatwin(self->num); kill_notifs(self->active_box); - del_window(self); + del_window(self, toxic->c_config); } -static void recv_message_helper(ToxWindow *self, const char *msg, const char *nick) +static void recv_message_helper(ToxWindow *self, const Client_Config *c_config, const char *msg, + const char *nick) { ChatContext *ctx = self->chatwin; - line_info_add(self, true, nick, NULL, IN_MSG, 0, 0, "%s", msg); - write_to_log(msg, nick, ctx->log, false); + line_info_add(self, c_config, true, nick, NULL, IN_MSG, 0, 0, "%s", msg); + write_to_log(ctx->log, c_config, msg, nick, false); if (self->active_box != -1) { - box_notify2(self, generic_message, NT_WNDALERT_1 | NT_NOFOCUS | user_settings->bell_on_message, + box_notify2(self, c_config, generic_message, NT_WNDALERT_1 | NT_NOFOCUS | c_config->bell_on_message, self->active_box, "%s", msg); } else { - box_notify(self, generic_message, NT_WNDALERT_1 | NT_NOFOCUS | user_settings->bell_on_message, + box_notify(self, c_config, generic_message, NT_WNDALERT_1 | NT_NOFOCUS | c_config->bell_on_message, &self->active_box, nick, "%s", msg); } } -static void recv_action_helper(ToxWindow *self, const char *action, const char *nick) +static void recv_action_helper(ToxWindow *self, const Client_Config *c_config, const char *action, + const char *nick) { ChatContext *ctx = self->chatwin; - line_info_add(self, true, nick, NULL, IN_ACTION, 0, 0, "%s", action); - write_to_log(action, nick, ctx->log, true); + line_info_add(self, c_config, true, nick, NULL, IN_ACTION, 0, 0, "%s", action); + write_to_log(ctx->log, c_config, action, nick, true); if (self->active_box != -1) { - box_notify2(self, generic_message, NT_WNDALERT_1 | NT_NOFOCUS | user_settings->bell_on_message, + box_notify2(self, c_config, generic_message, NT_WNDALERT_1 | NT_NOFOCUS | c_config->bell_on_message, self->active_box, "* %s %s", nick, action); } else { - box_notify(self, generic_message, NT_WNDALERT_1 | NT_NOFOCUS | user_settings->bell_on_message, + box_notify(self, c_config, generic_message, NT_WNDALERT_1 | NT_NOFOCUS | c_config->bell_on_message, &self->active_box, self->name, "* %s %s", nick, action); } } @@ -215,6 +217,8 @@ static void chat_onMessage(ToxWindow *self, Toxic *toxic, uint32_t num, Tox_Mess return; } + const Client_Config *c_config = toxic->c_config; + if (self->num != num) { return; } @@ -223,18 +227,18 @@ static void chat_onMessage(ToxWindow *self, Toxic *toxic, uint32_t num, Tox_Mess get_nick_truncate(toxic->tox, nick, num); if (type == TOX_MESSAGE_TYPE_NORMAL) { - recv_message_helper(self, msg, nick); + recv_message_helper(self, c_config, msg, nick); return; } if (type == TOX_MESSAGE_TYPE_ACTION) { - recv_action_helper(self, msg, nick); + recv_action_helper(self, c_config, msg, nick); return; } } 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_resume_file_senders(ToxWindow *self, const Toxic *toxic, uint32_t fnum); static void chat_onConnectionChange(ToxWindow *self, Toxic *toxic, uint32_t num, Tox_Connection connection_status) { @@ -243,6 +247,7 @@ static void chat_onConnectionChange(ToxWindow *self, Toxic *toxic, uint32_t num, } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (self->num != num) { return; @@ -258,29 +263,29 @@ static void chat_onConnectionChange(ToxWindow *self, Toxic *toxic, uint32_t num, Tox_Connection prev_status = statusbar->connection; statusbar->connection = connection_status; - if (user_settings->show_connection_msg == SHOW_WELCOME_MSG_OFF) { + if (c_config->show_connection_msg == SHOW_WELCOME_MSG_OFF) { return; } if (prev_status == TOX_CONNECTION_NONE) { - chat_resume_file_senders(self, tox, num); + chat_resume_file_senders(self, toxic, num); file_send_queue_check(self, toxic, self->num); msg = "has come online"; - line_info_add(self, true, nick, NULL, CONNECTION, 0, GREEN, msg); - write_to_log(msg, nick, ctx->log, true); + line_info_add(self, c_config, true, nick, NULL, CONNECTION, 0, GREEN, msg); + write_to_log(ctx->log, c_config, msg, nick, true); } else if (connection_status == TOX_CONNECTION_NONE) { Friends.list[num].is_typing = false; if (self->chatwin->self_is_typing) { - set_self_typingstatus(self, tox, false); + set_self_typingstatus(self, toxic, false); } chat_pause_file_transfers(num); msg = "has gone offline"; - line_info_add(self, true, nick, NULL, DISCONNECTION, 0, RED, msg); - write_to_log(msg, nick, ctx->log, true); + line_info_add(self, c_config, true, nick, NULL, DISCONNECTION, 0, RED, msg); + write_to_log(ctx->log, c_config, msg, nick, true); } } @@ -366,7 +371,7 @@ static void chat_onReadReceipt(ToxWindow *self, Toxic *toxic, uint32_t num, uint return; } - cqueue_remove(self, toxic->tox, receipt); + cqueue_remove(self, toxic, receipt); } /* Stops active file transfers for this friend. Called when a friend goes offline */ @@ -390,7 +395,7 @@ static void chat_pause_file_transfers(uint32_t friendnum) } /* Tries to resume broken file senders. Called when a friend comes online */ -static void chat_resume_file_senders(ToxWindow *self, Tox *tox, uint32_t friendnum) +static void chat_resume_file_senders(ToxWindow *self, const Toxic *toxic, uint32_t friendnum) { for (size_t i = 0; i < MAX_FILES; ++i) { struct FileTransfer *ft = &Friends.list[friendnum].file_sender[i]; @@ -400,13 +405,13 @@ static void chat_resume_file_senders(ToxWindow *self, Tox *tox, uint32_t friendn } Tox_Err_File_Send err; - ft->filenumber = tox_file_send(tox, friendnum, TOX_FILE_KIND_DATA, ft->file_size, ft->file_id, + ft->filenumber = tox_file_send(toxic->tox, friendnum, TOX_FILE_KIND_DATA, ft->file_size, ft->file_id, (uint8_t *) ft->file_name, strlen(ft->file_name), &err); if (err != TOX_ERR_FILE_SEND_OK) { char msg[MAX_STR_SIZE]; snprintf(msg, sizeof(msg), "File transfer for '%s' failed.", ft->file_name); - close_file_transfer(self, tox, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); + close_file_transfer(self, toxic, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); continue; } } @@ -441,20 +446,20 @@ static void chat_onFileChunkRequest(ToxWindow *self, Toxic *toxic, uint32_t frie if (length == 0) { snprintf(msg, sizeof(msg), "File '%s' successfully sent.", ft->file_name); print_progress_bar(self, ft->bps, 100.0, ft->line_id); - close_file_transfer(self, tox, ft, -1, msg, transfer_completed); + close_file_transfer(self, toxic, ft, -1, msg, transfer_completed); return; } if (ft->file == NULL) { snprintf(msg, sizeof(msg), "File transfer for '%s' failed: Null file pointer.", ft->file_name); - close_file_transfer(self, tox, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); + close_file_transfer(self, toxic, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); return; } if (ft->position != position) { if (fseek(ft->file, position, SEEK_SET) == -1) { snprintf(msg, sizeof(msg), "File transfer for '%s' failed: Seek fail.", ft->file_name); - close_file_transfer(self, tox, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); + close_file_transfer(self, toxic, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); return; } @@ -465,7 +470,7 @@ static void chat_onFileChunkRequest(ToxWindow *self, Toxic *toxic, uint32_t frie if (send_data == NULL) { snprintf(msg, sizeof(msg), "File transfer for '%s' failed: Out of memory.", ft->file_name); - close_file_transfer(self, tox, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); + close_file_transfer(self, toxic, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); return; } @@ -473,7 +478,7 @@ static void chat_onFileChunkRequest(ToxWindow *self, Toxic *toxic, uint32_t frie if (send_length != length) { snprintf(msg, sizeof(msg), "File transfer for '%s' failed: Read fail.", ft->file_name); - close_file_transfer(self, tox, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); + close_file_transfer(self, toxic, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); free(send_data); return; } @@ -501,8 +506,6 @@ static void chat_onFileRecvChunk(ToxWindow *self, Toxic *toxic, uint32_t friendn return; } - Tox *tox = toxic->tox; - if (friendnum != self->num) { return; } @@ -522,19 +525,19 @@ static void chat_onFileRecvChunk(ToxWindow *self, Toxic *toxic, uint32_t friendn if (length == 0) { snprintf(msg, sizeof(msg), "File '%s' successfully received.", ft->file_name); print_progress_bar(self, ft->bps, 100.0, ft->line_id); - close_file_transfer(self, tox, ft, -1, msg, transfer_completed); + close_file_transfer(self, toxic, ft, -1, msg, transfer_completed); return; } if (ft->file == NULL) { snprintf(msg, sizeof(msg), "File transfer for '%s' failed: Invalid file pointer.", ft->file_name); - close_file_transfer(self, tox, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); + close_file_transfer(self, toxic, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); return; } if (fwrite(data, length, 1, ft->file) != 1) { snprintf(msg, sizeof(msg), "File transfer for '%s' failed: Write fail.", ft->file_name); - close_file_transfer(self, tox, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); + close_file_transfer(self, toxic, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); return; } @@ -549,6 +552,8 @@ static void chat_onFileControl(ToxWindow *self, Toxic *toxic, uint32_t friendnum return; } + const Client_Config *c_config = toxic->c_config; + if (friendnum != self->num) { return; } @@ -563,9 +568,10 @@ static void chat_onFileControl(ToxWindow *self, Toxic *toxic, uint32_t friendnum case TOX_FILE_CONTROL_RESUME: { /* transfer is accepted */ if (ft->state == FILE_TRANSFER_PENDING) { ft->state = FILE_TRANSFER_STARTED; - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer [%zu] for '%s' accepted.", + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer [%zu] for '%s' accepted.", ft->index, ft->file_name); - sound_notify(self, silent, NT_NOFOCUS | user_settings->bell_on_filetrans_accept | NT_WNDALERT_2, NULL); + sound_notify(self, c_config, silent, + NT_NOFOCUS | c_config->bell_on_filetrans_accept | NT_WNDALERT_2, NULL); } else if (ft->state == FILE_TRANSFER_PAUSED) { /* transfer is resumed */ ft->state = FILE_TRANSFER_STARTED; } @@ -581,7 +587,7 @@ static void chat_onFileControl(ToxWindow *self, Toxic *toxic, uint32_t friendnum case TOX_FILE_CONTROL_CANCEL: { char msg[MAX_STR_SIZE]; snprintf(msg, sizeof(msg), "File transfer for '%s' was aborted.", ft->file_name); - close_file_transfer(self, toxic->tox, ft, -1, msg, notif_error); + close_file_transfer(self, toxic, ft, -1, msg, notif_error); break; } } @@ -641,7 +647,7 @@ static bool chat_resume_broken_ft(ToxWindow *self, Toxic *toxic, uint32_t friend on_error: snprintf(msg, sizeof(msg), "File transfer for '%s' failed.", ft->file_name); - close_file_transfer(self, tox, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); + close_file_transfer(self, toxic, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); return false; } @@ -685,6 +691,7 @@ static void chat_onFileRecv(ToxWindow *self, Toxic *toxic, uint32_t friendnum, u } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (self->num != friendnum) { return; @@ -699,17 +706,18 @@ static void chat_onFileRecv(ToxWindow *self, Toxic *toxic, uint32_t friendnum, u if (ft == NULL) { tox_file_control(tox, friendnum, filenumber, TOX_FILE_CONTROL_CANCEL, NULL); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer request failed: Too many concurrent file transfers."); return; } char sizestr[32]; bytes_convert_str(sizestr, sizeof(sizestr), file_size); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer request for '%s' (%s)", filename, sizestr); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer request for '%s' (%s)", filename, + sizestr); if (!valid_file_name(filename, name_length)) { - close_file_transfer(self, tox, ft, TOX_FILE_CONTROL_CANCEL, "File transfer failed: Invalid file name.", notif_error); + close_file_transfer(self, toxic, ft, TOX_FILE_CONTROL_CANCEL, "File transfer failed: Invalid file name.", notif_error); return; } @@ -717,22 +725,22 @@ static void chat_onFileRecv(ToxWindow *self, Toxic *toxic, uint32_t friendnum, u char *file_path = malloc(file_path_buf_size); if (file_path == NULL) { - close_file_transfer(self, tox, ft, TOX_FILE_CONTROL_CANCEL, "File transfer failed: Out of memory.", notif_error); + close_file_transfer(self, toxic, ft, TOX_FILE_CONTROL_CANCEL, "File transfer failed: Out of memory.", notif_error); return; } size_t path_len = name_length; /* use specified download path in config if possible */ - if (!string_is_empty(user_settings->download_path)) { - snprintf(file_path, file_path_buf_size, "%s%s", user_settings->download_path, filename); - path_len += strlen(user_settings->download_path); + if (!string_is_empty(c_config->download_path)) { + snprintf(file_path, file_path_buf_size, "%s%s", c_config->download_path, filename); + path_len += strlen(c_config->download_path); } else { snprintf(file_path, file_path_buf_size, "%s", filename); } if (path_len >= file_path_buf_size || path_len >= sizeof(ft->file_path) || name_length >= sizeof(ft->file_name)) { - close_file_transfer(self, tox, ft, TOX_FILE_CONTROL_CANCEL, "File transfer failed: File path too long.", notif_error); + close_file_transfer(self, toxic, ft, TOX_FILE_CONTROL_CANCEL, "File transfer failed: File path too long.", notif_error); free(file_path); return; } @@ -752,7 +760,7 @@ static void chat_onFileRecv(ToxWindow *self, Toxic *toxic, uint32_t friendnum, u size_t d_len = strlen(d); if (path_len + d_len >= file_path_buf_size) { - close_file_transfer(self, tox, ft, TOX_FILE_CONTROL_CANCEL, "File transfer failed: File path too long.", notif_error); + close_file_transfer(self, toxic, ft, TOX_FILE_CONTROL_CANCEL, "File transfer failed: File path too long.", notif_error); free(file_path); return; } @@ -761,7 +769,7 @@ static void chat_onFileRecv(ToxWindow *self, Toxic *toxic, uint32_t friendnum, u file_path[path_len + d_len] = '\0'; if (++count > 99) { // If there are this many duplicate file names we should probably give up - close_file_transfer(self, tox, ft, TOX_FILE_CONTROL_CANCEL, "File transfer failed: invalid file path.", notif_error); + close_file_transfer(self, toxic, ft, TOX_FILE_CONTROL_CANCEL, "File transfer failed: invalid file path.", notif_error); free(file_path); return; } @@ -775,23 +783,23 @@ static void chat_onFileRecv(ToxWindow *self, Toxic *toxic, uint32_t friendnum, u free(file_path); if (self->active_box != -1) { - box_notify2(self, transfer_pending, NT_WNDALERT_0 | NT_NOFOCUS | user_settings->bell_on_filetrans, + box_notify2(self, c_config, transfer_pending, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_filetrans, self->active_box, "Incoming file: %s", filename); } else { - box_notify(self, transfer_pending, NT_WNDALERT_0 | NT_NOFOCUS | user_settings->bell_on_filetrans, + box_notify(self, c_config, transfer_pending, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_filetrans, &self->active_box, self->name, "Incoming file: %s", filename); } const bool auto_accept_files = friend_get_auto_accept_files(friendnum); if (auto_accept_files) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Auto-accepting file transfer %zu", ft->index); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Auto-accepting file transfer %zu", ft->index); char cmd[MAX_STR_SIZE]; snprintf(cmd, sizeof(cmd), "/savefile %zu", ft->index); execute(self->window, self, toxic, cmd, CHAT_COMMAND_MODE); } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Type '/savefile %zu' to accept the file transfer.", ft->index); } } @@ -804,6 +812,8 @@ static void chat_onConferenceInvite(ToxWindow *self, Toxic *toxic, int32_t frien return; } + const Client_Config *c_config = toxic->c_config; + if (self->num != friendnumber) { return; } @@ -815,7 +825,7 @@ static void chat_onConferenceInvite(ToxWindow *self, Toxic *toxic, int32_t frien char *k = malloc(length * sizeof(char)); if (k == NULL) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Conference invite failed (OOM)"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Conference invite failed (OOM)"); return; } @@ -831,15 +841,16 @@ static void chat_onConferenceInvite(ToxWindow *self, Toxic *toxic, int32_t frien const char *description = type == TOX_CONFERENCE_TYPE_AV ? "an audio conference" : "a conference"; if (self->active_box != -1) { - box_notify2(self, generic_message, NT_WNDALERT_2 | user_settings->bell_on_invite, self->active_box, + box_notify2(self, c_config, generic_message, NT_WNDALERT_2 | c_config->bell_on_invite, self->active_box, "invites you to join %s", description); } else { - box_notify(self, generic_message, NT_WNDALERT_2 | user_settings->bell_on_invite, &self->active_box, name, + box_notify(self, c_config, generic_message, NT_WNDALERT_2 | c_config->bell_on_invite, &self->active_box, + name, "invites you to join %s", description); } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s has invited you to a conference.", name); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Type \"/cjoin\" to join the chat."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s has invited you to a conference.", name); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Type \"/cjoin\" to join the chat."); } static void chat_onGroupInvite(ToxWindow *self, Toxic *toxic, uint32_t friendnumber, const char *invite_data, @@ -852,6 +863,8 @@ static void chat_onGroupInvite(ToxWindow *self, Toxic *toxic, uint32_t friendnum return; } + const Client_Config *c_config = toxic->c_config; + if (self->num != friendnumber) { return; } @@ -869,19 +882,22 @@ static void chat_onGroupInvite(ToxWindow *self, Toxic *toxic, uint32_t friendnum memcpy(Friends.list[friendnumber].group_invite.data, invite_data, length); Friends.list[friendnumber].group_invite.length = length; - sound_notify(self, generic_message, NT_WNDALERT_2 | user_settings->bell_on_invite, NULL); + sound_notify(self, c_config, generic_message, NT_WNDALERT_2 | c_config->bell_on_invite, NULL); char name[TOX_MAX_NAME_LENGTH]; 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"); + box_silent_notify2(self, c_config, NT_WNDALERT_2 | NT_NOFOCUS, self->active_box, + "invites you to join group chat"); } else { - box_silent_notify(self, NT_WNDALERT_2 | NT_NOFOCUS, &self->active_box, name, "invites you to join group chat"); + box_silent_notify(self, c_config, NT_WNDALERT_2 | NT_NOFOCUS, &self->active_box, name, + "invites you to join group chat"); } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s has invited you to join group chat \"%s\"", name, group_name); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s has invited you to join group chat \"%s\"", + name, group_name); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Type \"/gaccept \" to join the chat (password is optional)."); } @@ -892,6 +908,8 @@ void chat_onGameInvite(ToxWindow *self, Toxic *toxic, uint32_t friend_number, co return; } + const Client_Config *c_config = toxic->c_config; + if (self->num != friend_number) { return; } @@ -903,7 +921,7 @@ void chat_onGameInvite(ToxWindow *self, Toxic *toxic, uint32_t friend_number, co const uint8_t version = data[0]; if (version != GAME_NETWORKING_VERSION) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Game invite failed. Friend has network protocol version %d, you have version %d.", version, GAME_NETWORKING_VERSION); return; } @@ -947,16 +965,18 @@ void chat_onGameInvite(ToxWindow *self, Toxic *toxic, uint32_t friend_number, co 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, + box_notify2(self, c_config, generic_message, NT_WNDALERT_2 | c_config->bell_on_invite, self->active_box, "invites you to play %s", game_string); } else { - box_notify(self, generic_message, NT_WNDALERT_2 | user_settings->bell_on_invite, &self->active_box, name, + box_notify(self, c_config, generic_message, NT_WNDALERT_2 | c_config->bell_on_invite, &self->active_box, + name, "invites you to play %s", game_string); } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s has invited you to a game of %s.", name, game_string); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Type \"/play\" to join the game."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s has invited you to a game of %s.", name, + game_string); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Type \"/play\" to join the game."); } #endif // GAMES @@ -966,44 +986,57 @@ void chat_onGameInvite(ToxWindow *self, Toxic *toxic, uint32_t friend_number, co void chat_onInvite(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) { - UNUSED_VAR(toxic); UNUSED_VAR(state); - if (self == NULL || self->num != friend_number) { + if (toxic == NULL || self == NULL) { + return; + } + + const Client_Config *c_config = toxic->c_config; + + if (self->num != friend_number) { return; } /* call is flagged active here */ self->is_call = true; - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Incoming audio call! Type: \"/answer\" or \"/reject\""); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Incoming audio call! Type: \"/answer\" or \"/reject\""); if (self->ringing_sound == -1) { - sound_notify(self, call_incoming, NT_LOOP | user_settings->bell_on_invite, &self->ringing_sound); + sound_notify(self, c_config, call_incoming, NT_LOOP | c_config->bell_on_invite, &self->ringing_sound); } if (self->active_box != -1) { - box_silent_notify2(self, NT_NOFOCUS | NT_WNDALERT_0, self->active_box, "Incoming audio call!"); + box_silent_notify2(self, c_config, NT_NOFOCUS | NT_WNDALERT_0, self->active_box, "Incoming audio call!"); } else { - box_silent_notify(self, NT_NOFOCUS | NT_WNDALERT_0, &self->active_box, self->name, "Incoming audio call!"); + box_silent_notify(self, c_config, NT_NOFOCUS | NT_WNDALERT_0, &self->active_box, self->name, + "Incoming audio call!"); } } void chat_onRinging(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) { - UNUSED_VAR(toxic); UNUSED_VAR(state); - if (!self || self->num != friend_number) { + if (self == NULL || toxic == NULL) { return; } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Ringing...type \"/hangup\" to cancel it."); + const Client_Config *c_config = toxic->c_config; + + if (self->num != friend_number) { + return; + } + + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Ringing...type \"/hangup\" to cancel it."); #ifdef SOUND_NOTIFY if (self->ringing_sound == -1) { - sound_notify(self, call_outgoing, NT_LOOP, &self->ringing_sound); + sound_notify(self, c_config, call_outgoing, NT_LOOP, &self->ringing_sound); } #endif /* SOUND_NOTIFY */ @@ -1011,16 +1044,21 @@ void chat_onRinging(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int s void chat_onStarting(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) { - UNUSED_VAR(toxic); UNUSED_VAR(state); - if (!self || self->num != friend_number) { + if (self == NULL || self->num != friend_number) { return; } - init_infobox(self); + if (toxic == NULL) { + return; + } + + const Client_Config *c_config = toxic->c_config; + + init_infobox(self, c_config->VAD_threshold); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Call started! Type: \"/hangup\" to end it."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Call started! Type: \"/hangup\" to end it."); /* call is flagged active here */ self->is_call = true; @@ -1030,36 +1068,20 @@ void chat_onStarting(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int #endif /* SOUND_NOTIFY */ } -void chat_onEnding(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) +void chat_onError(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) { - UNUSED_VAR(toxic); UNUSED_VAR(state); if (!self || self->num != friend_number) { return; } - kill_infobox(self); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Call ended!"); - - self->is_call = false; - -#ifdef SOUND_NOTIFY - stop_sound(self->ringing_sound); -#endif /* SOUND_NOTIFY */ -} - -void chat_onError(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) -{ - UNUSED_VAR(toxic); - UNUSED_VAR(state); - - if (!self || self->num != friend_number) { + if (toxic == NULL) { return; } self->is_call = false; - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Error!"); + line_info_add(self, toxic->c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Error!"); #ifdef SOUND_NOTIFY stop_sound(self->ringing_sound); @@ -1068,19 +1090,24 @@ void chat_onError(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int sta void chat_onStart(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) { - UNUSED_VAR(toxic); UNUSED_VAR(state); if (!self || self->num != friend_number) { return; } + if (toxic == NULL) { + return; + } + + const Client_Config *c_config = toxic->c_config; + /* call is flagged active here */ self->is_call = true; - init_infobox(self); + init_infobox(self, c_config->VAD_threshold); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Call started! Type: \"/hangup\" to end it."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Call started! Type: \"/hangup\" to end it."); #ifdef SOUND_NOTIFY stop_sound(self->ringing_sound); @@ -1089,16 +1116,19 @@ void chat_onStart(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int sta void chat_onCancel(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) { - UNUSED_VAR(toxic); UNUSED_VAR(state); if (!self || self->num != friend_number) { return; } + if (toxic == NULL) { + return; + } + self->is_call = false; kill_infobox(self); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Call canceled!"); + line_info_add(self, toxic->c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Call canceled!"); #ifdef SOUND_NOTIFY stop_sound(self->ringing_sound); @@ -1107,14 +1137,17 @@ void chat_onCancel(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int st void chat_onReject(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) { - UNUSED_VAR(toxic); UNUSED_VAR(state); if (!self || self->num != friend_number) { return; } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Rejected!"); + if (toxic == NULL) { + return; + } + + line_info_add(self, toxic->c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Rejected!"); self->is_call = false; #ifdef SOUND_NOTIFY @@ -1124,15 +1157,18 @@ void chat_onReject(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int st void chat_onEnd(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state) { - UNUSED_VAR(toxic); UNUSED_VAR(state); if (!self || self->num != friend_number) { return; } + if (toxic == NULL) { + return; + } + kill_infobox(self); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Call ended!"); + line_info_add(self, toxic->c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Call ended!"); self->is_call = false; #ifdef SOUND_NOTIFY @@ -1140,7 +1176,7 @@ void chat_onEnd(ToxWindow *self, Toxic *toxic, uint32_t friend_number, int state #endif /* SOUND_NOTIFY */ } -static void init_infobox(ToxWindow *self) +static void init_infobox(ToxWindow *self, double VAD_threshold) { ChatContext *ctx = self->chatwin; @@ -1159,7 +1195,7 @@ static void init_infobox(ToxWindow *self) ctx->infobox.win = newwin(INFOBOX_HEIGHT, INFOBOX_WIDTH + 1, 1, x2 - INFOBOX_WIDTH); ctx->infobox.starttime = get_unix_time(); - ctx->infobox.vad_lvl = user_settings->VAD_threshold; + ctx->infobox.vad_lvl = VAD_threshold; ctx->infobox.active = true; strcpy(ctx->infobox.timestr, "00"); } @@ -1237,19 +1273,19 @@ static void draw_infobox(ToxWindow *self) #endif /* AUDIO */ -static void send_action(ToxWindow *self, ChatContext *ctx, Tox *tox, char *action) +static void send_action(ToxWindow *self, ChatContext *ctx, Toxic *toxic, char *action) { if (action == NULL) { return; } char selfname[TOX_MAX_NAME_LENGTH]; - tox_self_get_name(tox, (uint8_t *) selfname); + tox_self_get_name(toxic->tox, (uint8_t *) selfname); - size_t len = tox_self_get_name_size(tox); + const size_t len = tox_self_get_name_size(toxic->tox); selfname[len] = '\0'; - int id = line_info_add(self, true, selfname, NULL, OUT_ACTION, 0, 0, "%s", action); + const int id = line_info_add(self, toxic->c_config, true, selfname, NULL, OUT_ACTION, 0, 0, "%s", action); cqueue_add(ctx->cqueue, action, strlen(action), OUT_ACTION, id); } @@ -1263,6 +1299,7 @@ bool chat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; StatusBar *statusbar = self->stb; @@ -1287,20 +1324,20 @@ bool chat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) } if (ltr || key == L'\n') { /* char is printable */ - input_new_char(self, key, x, x2); + input_new_char(self, c_config, key, x, x2); if (ctx->line[0] != '/' && !ctx->self_is_typing && statusbar->connection != TOX_CONNECTION_NONE) { - set_self_typingstatus(self, tox, true); + set_self_typingstatus(self, toxic, true); } return true; } - if (line_info_onKey(self, key)) { + if (line_info_onKey(self, c_config, key)) { return true; } - int input_ret = input_handle(self, key, x, x2); + bool input_ret = input_handle(self, c_config, key, x, x2); if (key == L'\t' && ctx->len > 1 && ctx->line[0] == '/') { /* TAB key: auto-complete */ input_ret = true; @@ -1326,9 +1363,9 @@ bool chat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) "away", "busy", }; - diff = complete_line(self, status_cmd_list, sizeof(status_cmd_list) / sizeof(char *)); + diff = complete_line(self, toxic, status_cmd_list, sizeof(status_cmd_list) / sizeof(char *)); } else { - diff = complete_line(self, chat_cmd_list, sizeof(chat_cmd_list) / sizeof(char *)); + diff = complete_line(self, toxic, chat_cmd_list, sizeof(chat_cmd_list) / sizeof(char *)); } if (diff != -1) { @@ -1337,7 +1374,7 @@ bool chat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) ctx->start = wlen < x2 ? 0 : wlen - x2 + 1; } } else { - sound_notify(self, notif_error, 0, NULL); + sound_notify(self, c_config, notif_error, 0, NULL); } } else if (key == L'\r') { @@ -1357,10 +1394,10 @@ bool chat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) if (line[0] == '/') { if (strcmp(line, "/close") == 0) { - kill_chat_window(self, tox); + kill_chat_window(self, toxic); return input_ret; } else if (strncmp(line, "/me ", strlen("/me ")) == 0) { - send_action(self, ctx, tox, line + strlen("/me ")); + send_action(self, ctx, toxic, line + strlen("/me ")); } else { execute(ctx->history, self, toxic, line, CHAT_COMMAND_MODE); } @@ -1371,10 +1408,10 @@ bool chat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) size_t len = tox_self_get_name_size(tox); selfname[len] = '\0'; - int id = line_info_add(self, true, selfname, NULL, OUT_MSG, 0, 0, "%s", line); + int id = line_info_add(self, c_config, true, selfname, NULL, OUT_MSG, 0, 0, "%s", line); cqueue_add(ctx->cqueue, line, strlen(line), OUT_MSG, id); } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, " * Failed to parse message."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, " * Failed to parse message."); } } @@ -1384,7 +1421,7 @@ bool chat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) } if (ctx->len <= 0 && ctx->self_is_typing) { - set_self_typingstatus(self, tox, false); + set_self_typingstatus(self, toxic, false); } return input_ret; @@ -1410,7 +1447,7 @@ static void chat_onDraw(ToxWindow *self, Toxic *toxic) pthread_mutex_lock(&Winthread.lock); - line_info_print(self); + line_info_print(self, toxic->c_config); Tox_Connection connection = statusbar->connection; Tox_User_Status status = statusbar->status; @@ -1610,25 +1647,26 @@ static void chat_onDraw(ToxWindow *self, Toxic *toxic) pthread_mutex_unlock(&Winthread.lock); } -static void chat_init_log(ToxWindow *self, Tox *tox, const char *self_nick) +static void chat_init_log(ToxWindow *self, Toxic *toxic, const char *self_nick) { ChatContext *ctx = self->chatwin; + const Client_Config *c_config = toxic->c_config; char myid[TOX_ADDRESS_SIZE]; - tox_self_get_address(tox, (uint8_t *) myid); + tox_self_get_address(toxic->tox, (uint8_t *) myid); - if (log_init(ctx->log, self_nick, myid, Friends.list[self->num].pub_key, LOG_TYPE_CHAT) != 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to initialize chat log."); + if (log_init(ctx->log, c_config, self_nick, myid, Friends.list[self->num].pub_key, LOG_TYPE_CHAT) != 0) { + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to initialize chat log."); return; } - if (load_chat_history(self, ctx->log) != 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to load chat history."); + if (load_chat_history(ctx->log, self, c_config) != 0) { + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to load chat history."); } if (Friends.list[self->num].logging_on) { if (log_enable(ctx->log) != 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to enable chat log."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to enable chat log."); } } } @@ -1697,14 +1735,14 @@ static void chat_onInit(ToxWindow *self, Toxic *toxic) self->colour = tab_name_colour > 0 ? tab_name_colour : WHITE_BAR_FG; Friends.list[self->num].logging_on = friend_config_get_autolog(self->num); - chat_init_log(self, tox, nick); + chat_init_log(self, toxic, nick); 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); - line_info_print(self); + line_info_print(self, toxic->c_config); } ToxWindow *new_chat(Tox *tox, uint32_t friendnum) @@ -1738,7 +1776,6 @@ ToxWindow *new_chat(Tox *tox, uint32_t friendnum) ret->onInvite = &chat_onInvite; ret->onRinging = &chat_onRinging; ret->onStarting = &chat_onStarting; - ret->onEnding = &chat_onEnding; ret->onError = &chat_onError; ret->onStart = &chat_onStart; ret->onCancel = &chat_onCancel; diff --git a/src/chat_commands.c b/src/chat_commands.c index 6df224e15..66509f331 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -40,12 +40,13 @@ extern FriendsList Friends; void cmd_autoaccept_files(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(toxic); - if (self == NULL) { + if (self == NULL || toxic == NULL) { return; } + const Client_Config *c_config = toxic->c_config; + const char *msg; const bool auto_accept_files = friend_get_auto_accept_files(self->num); @@ -56,7 +57,7 @@ void cmd_autoaccept_files(WINDOW *window, ToxWindow *self, Toxic *toxic, int arg msg = "Auto-file accept for this friend is disabled; type \"/autoaccept on\" to enable"; } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, msg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, msg); return; } @@ -72,7 +73,7 @@ void cmd_autoaccept_files(WINDOW *window, ToxWindow *self, Toxic *toxic, int arg msg = "Invalid option. Use \"/autoaccept on\" and \"/autoaccept off\" to toggle auto-file accept"; } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, msg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, msg); } void cmd_cancelfile(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) @@ -83,10 +84,10 @@ void cmd_cancelfile(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, cha return; } - Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (argc < 2) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Requires type in|out and the file ID."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Requires type in|out and the file ID."); return; } @@ -95,13 +96,13 @@ void cmd_cancelfile(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, cha long int idx = strtol(argv[2], NULL, 10); if ((idx == 0 && strcmp(argv[2], "0")) || idx >= MAX_FILES || idx < 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid file ID."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid file ID."); return; } // first check transfer queue if (file_send_queue_remove(self->num, idx) == 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Pending file transfer removed from queue"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Pending file transfer removed from queue"); return; } @@ -113,22 +114,22 @@ void cmd_cancelfile(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, cha } else if (strcasecmp(inoutstr, "out") == 0) { ft = get_file_transfer_struct_index(self->num, idx, FILE_TRANSFER_SEND); } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Type must be 'in' or 'out'."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Type must be 'in' or 'out'."); return; } if (ft == NULL) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid file ID."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid file ID."); return; } if (ft->state == FILE_TRANSFER_INACTIVE) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid file ID."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid file ID."); return; } snprintf(msg, sizeof(msg), "File transfer for '%s' aborted.", ft->file_name); - close_file_transfer(self, tox, ft, TOX_FILE_CONTROL_CANCEL, msg, silent); + close_file_transfer(self, toxic, ft, TOX_FILE_CONTROL_CANCEL, msg, silent); } void cmd_conference_invite(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) @@ -140,27 +141,30 @@ void cmd_conference_invite(WINDOW *window, ToxWindow *self, Toxic *toxic, int ar } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Conference number required."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Conference number required."); return; } long int conferencenum = strtol(argv[1], NULL, 10); if ((conferencenum == 0 && strcmp(argv[1], "0")) || conferencenum < 0 || conferencenum == LONG_MAX) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid conference number."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid conference number."); return; } Tox_Err_Conference_Invite err; if (!tox_conference_invite(tox, self->num, conferencenum, &err)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to invite contact to conference (error %d)", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Failed to invite contact to conference (error %d)", err); return; } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invited contact to Conference %ld.", conferencenum); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invited contact to Conference %ld.", + conferencenum); } void cmd_conference_join(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) @@ -174,9 +178,10 @@ void cmd_conference_join(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; 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."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, " * Warning: Too many windows are open."); return; } @@ -185,7 +190,7 @@ void cmd_conference_join(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc uint8_t type = Friends.list[self->num].conference_invite.type; if (!Friends.list[self->num].conference_invite.pending) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending conference invite."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "No pending conference invite."); return; } @@ -196,7 +201,8 @@ void cmd_conference_join(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc conferencenum = tox_conference_join(tox, self->num, (const uint8_t *) conferencekey, length, &err); if (err != TOX_ERR_CONFERENCE_JOIN_OK) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Conference instance failed to initialize (error %d)", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Conference instance failed to initialize (error %d)", err); return; } } else if (type == TOX_CONFERENCE_TYPE_AV) { @@ -205,21 +211,23 @@ void cmd_conference_join(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc audio_conference_callback, NULL); if (conferencenum == (uint32_t) -1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Audio conference instance failed to initialize"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Audio conference instance failed to initialize"); return; } #else - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Audio support disabled by compile-time option."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Audio support disabled by compile-time option."); return; #endif } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Unknown conference type %d", type); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Unknown conference type %d", type); return; } 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."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Conference window failed to initialize."); tox_conference_delete(tox, conferencenum, NULL); return; } @@ -227,8 +235,9 @@ void cmd_conference_join(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc #ifdef AUDIO if (type == TOX_CONFERENCE_TYPE_AV) { - if (!init_conference_audio_input(tox, conferencenum)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Audio capture failed; use \"/audio on\" to try again."); + if (!init_conference_audio_input(toxic, conferencenum)) { + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Audio capture failed; use \"/audio on\" to try again."); } } @@ -242,14 +251,15 @@ void cmd_group_accept(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, c } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; 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."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, " * Warning: Too many windows are open."); return; } if (Friends.list[self->num].group_invite.length == 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending group invite"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "No pending group invite"); return; } @@ -262,7 +272,7 @@ void cmd_group_accept(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, c } if (passwd_len > TOX_GROUP_MAX_PASSWORD_SIZE) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to join group: Password too long."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to join group: Password too long."); return; } @@ -277,12 +287,12 @@ void cmd_group_accept(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, c (const uint8_t *) passwd, passwd_len, &err); if (err != TOX_ERR_GROUP_INVITE_ACCEPT_OK) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to join group (error %d).", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to join group (error %d).", err); return; } 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."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Group chat window failed to initialize."); tox_group_leave(tox, groupnumber, NULL, 0, NULL); return; } @@ -295,27 +305,29 @@ void cmd_group_invite(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, c } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Group number required."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Group number required."); return; } int groupnumber = atoi(argv[1]); if (groupnumber == 0 && strcmp(argv[1], "0")) { /* atoi returns 0 value on invalid input */ - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid group number."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid group number."); return; } Tox_Err_Group_Invite_Friend err; if (!tox_group_invite_friend(tox, groupnumber, self->num, &err)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to invite contact to group (error %d).", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to invite contact to group (error %d).", + err); return; } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invited contact to Group %d.", groupnumber); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invited contact to Group %d.", groupnumber); } #ifdef GAMES @@ -328,13 +340,15 @@ void cmd_game_join(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char return; } + const Client_Config *c_config = toxic->c_config; + if (!Friends.list[self->num].game_invite.pending) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending game invite."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "No pending game invite."); return; } 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."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, " * Warning: Too many windows are open."); return; } @@ -354,17 +368,18 @@ void cmd_game_join(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char } case -1: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Window is too small. Try enlarging your window."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Window is too small. Try enlarging your window."); return; } case -2: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Game failed to initialize (network error)"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Game failed to initialize (network error)"); return; } default: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Game failed to initialize (error %d)", ret); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Game failed to initialize (error %d)", ret); return; } } @@ -381,34 +396,35 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "File ID required."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "File ID required."); return; } long int idx = strtol(argv[1], NULL, 10); if ((idx == 0 && strcmp(argv[1], "0")) || idx < 0 || idx >= MAX_FILES) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with ID %ld", idx); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with ID %ld", idx); return; } FileTransfer *ft = get_file_transfer_struct_index(self->num, idx, FILE_TRANSFER_RECV); if (ft == NULL) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with ID %ld.", idx); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with ID %ld.", idx); return; } if (ft->state != FILE_TRANSFER_PENDING) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with ID %ld.", idx); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with ID %ld.", idx); return; } if ((ft->file = fopen(ft->file_path, "a")) == NULL) { const char *msg = "File transfer failed: Invalid download path."; - close_file_transfer(self, tox, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); + close_file_transfer(self, toxic, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); return; } @@ -419,14 +435,15 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char goto on_recv_error; } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Saving file [%ld] as: '%s'", idx, ft->file_path); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Saving file [%ld] as: '%s'", idx, + ft->file_path); const bool auto_accept_files = friend_get_auto_accept_files(self->num); const uint32_t line_skip = auto_accept_files ? 4 : 2; char progline[MAX_STR_SIZE]; init_progress_bar(progline); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", progline); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", progline); ft->line_id = self->chatwin->hst->line_end->id + line_skip; ft->state = FILE_TRANSFER_STARTED; @@ -436,23 +453,23 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char switch (err) { case TOX_ERR_FILE_CONTROL_FRIEND_NOT_FOUND: - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed: Friend not found."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed: Friend not found."); return; case TOX_ERR_FILE_CONTROL_FRIEND_NOT_CONNECTED: - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed: Friend is not online."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed: Friend is not online."); return; case TOX_ERR_FILE_CONTROL_NOT_FOUND: - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed: Invalid filenumber."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed: Invalid filenumber."); return; case TOX_ERR_FILE_CONTROL_SENDQ: - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed: Connection error."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed: Connection error."); return; default: - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed (error %d)\n", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed (error %d)\n", err); return; } } @@ -466,11 +483,12 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; const char *errmsg = NULL; if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "File path required."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "File path required."); return; } @@ -479,21 +497,21 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char int path_len = strlen(path); if (path_len >= MAX_STR_SIZE) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "File path exceeds character limit."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "File path exceeds character limit."); return; } FILE *file_to_send = fopen(path, "r"); if (file_to_send == NULL) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "File `%s` not found.", path); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "File `%s` not found.", path); return; } off_t filesize = file_size(path); if (filesize <= 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid file."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid file."); fclose(file_to_send); return; } @@ -523,7 +541,8 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char char sizestr[32]; bytes_convert_str(sizestr, sizeof(sizestr), filesize); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Sending file [%d]: '%s' (%s)", filenum, file_name, sizestr); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Sending file [%d]: '%s' (%s)", filenum, + file_name, sizestr); return; @@ -564,7 +583,7 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char } } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", msg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", msg); return; } @@ -585,6 +604,6 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char } } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", errmsg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", errmsg); tox_file_control(tox, self->num, filenum, TOX_FILE_CONTROL_CANCEL, NULL); } diff --git a/src/conference.c b/src/conference.c index 817e68ea0..f223bdddf 100644 --- a/src/conference.c +++ b/src/conference.c @@ -68,7 +68,6 @@ static ConferenceChat conferences[MAX_CONFERENCE_NUM]; static int max_conference_index = 0; -extern struct user_settings *user_settings; extern struct Winthread Winthread; /* Array of conference command names used for tab completion. */ @@ -140,7 +139,7 @@ void conference_set_title(ToxWindow *self, uint32_t conferencesnum, const char * set_window_title(self, title, length); } -static void kill_conference_window(ToxWindow *self) +static void kill_conference_window(ToxWindow *self, const Client_Config *c_config) { if (self == NULL) { return; @@ -160,7 +159,7 @@ static void kill_conference_window(ToxWindow *self) free(self->help); kill_notifs(self->active_box); - del_window(self); + del_window(self, c_config); } static void init_conference_logging(ToxWindow *self, Toxic *toxic, uint32_t conferencenum) @@ -170,6 +169,7 @@ static void init_conference_logging(ToxWindow *self, Toxic *toxic, uint32_t conf } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; @@ -179,18 +179,18 @@ static void init_conference_logging(ToxWindow *self, Toxic *toxic, uint32_t conf char conference_id[TOX_CONFERENCE_ID_SIZE]; tox_conference_get_id(tox, conferencenum, (uint8_t *) conference_id); - if (log_init(ctx->log, conferences[self->num].title, my_id, conference_id, LOG_TYPE_CHAT) != 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Warning: Log failed to initialize."); + if (log_init(ctx->log, c_config, conferences[self->num].title, my_id, conference_id, LOG_TYPE_CHAT) != 0) { + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Warning: Log failed to initialize."); return; } - if (load_chat_history(self, ctx->log) != 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to load chat history."); + if (load_chat_history(ctx->log, self, c_config) != 0) { + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to load chat history."); } - if (user_settings->autolog == AUTOLOG_ON) { + if (c_config->autolog == AUTOLOG_ON) { if (log_enable(ctx->log) != 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to enable chat log."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to enable chat log."); } } @@ -225,7 +225,7 @@ int init_conference_win(Toxic *toxic, uint32_t conferencenum, uint8_t type, cons conferences[i].last_sent_audio = 0; #ifdef AUDIO - conferences[i].push_to_talk_enabled = user_settings->push_to_talk; + conferences[i].push_to_talk_enabled = toxic->c_config->push_to_talk; #endif set_active_window_index(conferences[i].chatwin); @@ -242,7 +242,7 @@ int init_conference_win(Toxic *toxic, uint32_t conferencenum, uint8_t type, cons } } - kill_conference_window(self); + kill_conference_window(self, toxic->c_config); return -1; } @@ -258,7 +258,7 @@ static void free_peer(ConferencePeer *peer) #endif } -void free_conference(ToxWindow *self, uint32_t conferencenum) +void free_conference(ToxWindow *self, const Client_Config *c_config, uint32_t conferencenum) { ConferenceChat *chat = &conferences[conferencenum]; @@ -293,16 +293,16 @@ void free_conference(ToxWindow *self, uint32_t conferencenum) } max_conference_index = i; - kill_conference_window(self); + kill_conference_window(self, c_config); } static void delete_conference(ToxWindow *self, Toxic *toxic, uint32_t conferencenum) { tox_conference_delete(toxic->tox, conferencenum, NULL); - free_conference(self, conferencenum); + free_conference(self, toxic->c_config, conferencenum); } -void conference_rename_log_path(Tox *tox, uint32_t conferencenum, const char *new_title) +void conference_rename_log_path(Toxic *toxic, uint32_t conferencenum, const char *new_title) { ConferenceChat *chat = &conferences[conferencenum]; @@ -311,12 +311,12 @@ void conference_rename_log_path(Tox *tox, uint32_t conferencenum, const char *ne } char myid[TOX_ADDRESS_SIZE]; - tox_self_get_address(tox, (uint8_t *) myid); + tox_self_get_address(toxic->tox, (uint8_t *) myid); char conference_id[TOX_CONFERENCE_ID_SIZE]; - tox_conference_get_id(tox, conferencenum, (uint8_t *) conference_id); + tox_conference_get_id(toxic->tox, conferencenum, (uint8_t *) conference_id); - if (rename_logfile(chat->title, new_title, myid, conference_id, chat->chatwin) != 0) { + if (rename_logfile(toxic->c_config, chat->title, new_title, myid, conference_id, chat->chatwin) != 0) { fprintf(stderr, "Failed to rename conference log to `%s`\n", new_title); } } @@ -373,6 +373,7 @@ static void conference_onConferenceMessage(ToxWindow *self, Toxic *toxic, uint32 } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (self->num != conferencenum) { return; @@ -394,20 +395,21 @@ static void conference_onConferenceMessage(ToxWindow *self, Toxic *toxic, uint32 /* Only play sound if mentioned by someone else */ if (strcasestr(msg, selfnick) && strcmp(selfnick, nick)) { if (self->active_box != -1) { - box_notify2(self, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | user_settings->bell_on_message, + box_notify2(self, c_config, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_message, self->active_box, "%s %s", nick, msg); } else { - box_notify(self, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | user_settings->bell_on_message, + box_notify(self, c_config, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_message, &self->active_box, self->name, "%s %s", nick, msg); } nick_clr = RED; } else { - sound_notify(self, silent, NT_WNDALERT_1, NULL); + sound_notify(self, c_config, silent, NT_WNDALERT_1, NULL); } - line_info_add(self, true, nick, NULL, type == TOX_MESSAGE_TYPE_NORMAL ? IN_MSG : IN_ACTION, 0, nick_clr, "%s", msg); - write_to_log(msg, nick, ctx->log, false); + line_info_add(self, c_config, true, nick, NULL, type == TOX_MESSAGE_TYPE_NORMAL ? IN_MSG : IN_ACTION, 0, + nick_clr, "%s", msg); + write_to_log(ctx->log, c_config, msg, nick, false); } static void conference_onConferenceTitleChange(ToxWindow *self, Toxic *toxic, uint32_t conferencenum, uint32_t peernum, @@ -419,6 +421,7 @@ static void conference_onConferenceTitleChange(ToxWindow *self, Toxic *toxic, ui } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; @@ -432,7 +435,7 @@ static void conference_onConferenceTitleChange(ToxWindow *self, Toxic *toxic, ui return; } - conference_rename_log_path(tox, conferencenum, title); // must be called first + conference_rename_log_path(toxic, conferencenum, title); // must be called first conference_set_title(self, conferencenum, title, length); @@ -443,11 +446,11 @@ static void conference_onConferenceTitleChange(ToxWindow *self, Toxic *toxic, ui char nick[TOX_MAX_NAME_LENGTH]; get_conference_nick_truncate(tox, nick, peernum, conferencenum); - line_info_add(self, true, nick, NULL, NAME_CHANGE, 0, 0, " set the conference title to: %s", title); + line_info_add(self, c_config, true, nick, NULL, NAME_CHANGE, 0, 0, " set the conference title to: %s", title); char tmp_event[MAX_STR_SIZE]; snprintf(tmp_event, sizeof(tmp_event), "set title to %s", title); - write_to_log(tmp_event, nick, ctx->log, true); + write_to_log(ctx->log, c_config, tmp_event, nick, true); } /* Puts `(NameListEntry *)`s in `entries` for each matched peer, up to a @@ -680,6 +683,7 @@ static void update_peer_list(ToxWindow *self, Toxic *toxic, uint32_t conferencen } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; @@ -744,8 +748,8 @@ static void update_peer_list(ToxWindow *self, Toxic *toxic, uint32_t conferencen if (new_peer && peer->name_length > 0 && timed_out(chat->start_time, CONFERENCE_EVENT_WAIT)) { const char *msg = "has joined the conference"; - line_info_add(self, true, peer->name, NULL, CONNECTION, 0, GREEN, msg); - write_to_log(msg, peer->name, ctx->log, true); + line_info_add(self, c_config, true, peer->name, NULL, CONNECTION, 0, GREEN, msg); + write_to_log(ctx->log, c_config, msg, peer->name, true); } #ifdef AUDIO @@ -761,8 +765,8 @@ static void update_peer_list(ToxWindow *self, Toxic *toxic, uint32_t conferencen if (old_peer->active) { if (old_peer->name_length > 0 && !find_peer_by_pubkey(chat->peer_list, chat->num_peers, old_peer->pubkey, NULL)) { const char *msg = "has left the conference"; - line_info_add(self, true, old_peer->name, NULL, DISCONNECTION, 0, RED, msg); - write_to_log(msg, old_peer->name, ctx->log, true); + line_info_add(self, c_config, true, old_peer->name, NULL, DISCONNECTION, 0, RED, msg); + write_to_log(ctx->log, c_config, msg, old_peer->name, true); } free_peer(old_peer); @@ -820,6 +824,8 @@ static void conference_onConferencePeerNameChange(ToxWindow *self, Toxic *toxic, return; } + const Client_Config *c_config = toxic->c_config; + if (self->num != conferencenum) { return; } @@ -831,23 +837,23 @@ static void conference_onConferencePeerNameChange(ToxWindow *self, Toxic *toxic, if (peer->name_length > 0) { char log_event[TOXIC_MAX_NAME_LENGTH * 2 + 32]; - line_info_add(self, true, peer->name, (const char *) name, NAME_CHANGE, 0, 0, " is now known as "); + line_info_add(self, c_config, true, peer->name, (const char *) name, NAME_CHANGE, 0, 0, " is now known as "); snprintf(log_event, sizeof(log_event), "is now known as %s", (const char *) name); - write_to_log(log_event, peer->name, ctx->log, true); + write_to_log(ctx->log, c_config, log_event, peer->name, true); // this is kind of a hack; peers always join a group with no name set and then set it after } else if (timed_out(conferences[conferencenum].start_time, CONFERENCE_EVENT_WAIT)) { const char *msg = "has joined the conference"; - line_info_add(self, true, name, NULL, CONNECTION, 0, GREEN, msg); - write_to_log(msg, name, ctx->log, true); + line_info_add(self, c_config, true, name, NULL, CONNECTION, 0, GREEN, msg); + write_to_log(ctx->log, c_config, msg, name, true); } } conference_onConferenceNameListChange(self, toxic, conferencenum); } -static void send_conference_action(ToxWindow *self, ChatContext *ctx, Tox *tox, char *action) +static void send_conference_action(ToxWindow *self, ChatContext *ctx, Toxic *toxic, char *action) { if (action == NULL) { wprintw(ctx->history, "Invalid syntax.\n"); @@ -856,8 +862,10 @@ static void send_conference_action(ToxWindow *self, ChatContext *ctx, Tox *tox, Tox_Err_Conference_Send_Message err; - if (!tox_conference_send_message(tox, self->num, TOX_MESSAGE_TYPE_ACTION, (uint8_t *) action, strlen(action), &err)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, " * Failed to send action (error %d)", err); + if (!tox_conference_send_message(toxic->tox, self->num, TOX_MESSAGE_TYPE_ACTION, (uint8_t *) action, + strlen(action), &err)) { + line_info_add(self, toxic->c_config, false, NULL, NULL, SYS_MSG, 0, RED, + " * Failed to send action (error %d)", err); } } @@ -877,6 +885,7 @@ static bool conference_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; @@ -900,15 +909,15 @@ static bool conference_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr } if (ltr || key == L'\n') { /* char is printable */ - input_new_char(self, key, x, x2); + input_new_char(self, c_config, key, x, x2); return true; } - if (line_info_onKey(self, key)) { + if (line_info_onKey(self, c_config, key)) { return true; } - if (input_handle(self, key, x, x2)) { + if (input_handle(self, c_config, key, x, x2)) { return true; } @@ -939,7 +948,7 @@ static bool conference_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr complete_strs[i] = (const char *) chat->name_list[i].name; } - diff = complete_line(self, complete_strs, chat->num_peers); + diff = complete_line(self, toxic, complete_strs, chat->num_peers); free(complete_strs); } } else if (wcsncmp(ctx->line, L"/avatar ", wcslen(L"/avatar ")) == 0) { @@ -960,21 +969,21 @@ static bool conference_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr complete_strs[i] = (const char *) chat->name_list[i].name; } - diff = complete_line(self, complete_strs, chat->num_peers); + diff = complete_line(self, toxic, complete_strs, chat->num_peers); if (diff == -1) { for (uint32_t i = 0; i < chat->num_peers; ++i) { complete_strs[i] = (const char *) chat->name_list[i].pubkey_str; } - diff = complete_line(self, complete_strs, chat->num_peers); + diff = complete_line(self, toxic, complete_strs, chat->num_peers); } free(complete_strs); } } else { - diff = complete_line(self, conference_cmd_list, sizeof(conference_cmd_list) / sizeof(char *)); + diff = complete_line(self, toxic, conference_cmd_list, sizeof(conference_cmd_list) / sizeof(char *)); } if (diff != -1) { @@ -983,10 +992,10 @@ static bool conference_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr ctx->start = wlen < x2 ? 0 : wlen - x2 + 1; } } else { - sound_notify(self, notif_error, 0, NULL); + sound_notify(self, c_config, notif_error, 0, NULL); } } else { - sound_notify(self, notif_error, 0, NULL); + sound_notify(self, c_config, notif_error, 0, NULL); } } else if (key == T_KEY_C_DOWN) { /* Scroll peerlist up and down one position */ input_ret = true; @@ -1021,7 +1030,7 @@ static bool conference_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr delete_conference(self, toxic, self->num); return true; } else if (strncmp(line, "/me ", strlen("/me ")) == 0) { - send_conference_action(self, ctx, tox, line + strlen("/me ")); + send_conference_action(self, ctx, toxic, line + strlen("/me ")); } else { execute(ctx->history, self, toxic, line, CONFERENCE_COMMAND_MODE); } @@ -1029,10 +1038,10 @@ static bool conference_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr Tox_Err_Conference_Send_Message err; if (!tox_conference_send_message(tox, self->num, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) line, strlen(line), &err)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, " * Failed to send message (error %d)", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, " * Failed to send message (error %d)", err); } } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, " * Failed to parse message."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, " * Failed to parse message."); } } @@ -1118,7 +1127,7 @@ static void conference_onDraw(ToxWindow *self, Toxic *toxic) ChatContext *ctx = self->chatwin; pthread_mutex_lock(&Winthread.lock); - line_info_print(self); + line_info_print(self, toxic->c_config); pthread_mutex_unlock(&Winthread.lock); wclear(ctx->linewin); @@ -1305,6 +1314,12 @@ static ToxWindow *new_conference_chat(uint32_t conferencenum) void audio_conference_callback(void *tox, uint32_t conferencenum, uint32_t peernum, const int16_t *pcm, unsigned int samples, uint8_t channels, uint32_t sample_rate, void *userdata) { + const Client_Config *c_config = (Client_Config *) userdata; + + if (c_config == NULL) { + return; + } + ConferencePeer *peer = peer_in_conference(conferencenum, peernum); if (peer == NULL) { @@ -1313,7 +1328,7 @@ void audio_conference_callback(void *tox, uint32_t conferencenum, uint32_t peern if (!peer->sending_audio) { if (open_output_device(&peer->audio_out_idx, - sample_rate, CONFAV_FRAME_DURATION, channels) != de_None) { + sample_rate, CONFAV_FRAME_DURATION, channels, c_config->VAD_threshold) != de_None) { // TODO: error message? return; } @@ -1344,15 +1359,14 @@ static void conference_read_device_callback(const int16_t *captured, uint32_t si chat->last_sent_audio = get_unix_time(); - const int channels = user_settings->conference_audio_channels; - toxav_group_send_audio(audio_input_callback_data->tox, audio_input_callback_data->conferencenum, captured, CONFAV_SAMPLES_PER_FRAME, - channels, CONFAV_SAMPLE_RATE); + audio_input_callback_data->audio_channels, + CONFAV_SAMPLE_RATE); } -bool init_conference_audio_input(Tox *tox, uint32_t conferencenum) +bool init_conference_audio_input(Toxic *toxic, uint32_t conferencenum) { ConferenceChat *chat = &conferences[conferencenum]; @@ -1360,14 +1374,23 @@ bool init_conference_audio_input(Tox *tox, uint32_t conferencenum) return false; } - const AudioInputCallbackData audio_input_callback_data = { tox, conferencenum }; + const Client_Config *c_config = toxic->c_config; + + const int channels = c_config->conference_audio_channels; + + const AudioInputCallbackData audio_input_callback_data = { + toxic->tox, + conferencenum, + channels, + }; + chat->audio_input_callback_data = audio_input_callback_data; - const int channels = user_settings->conference_audio_channels; const bool success = (open_input_device(&chat->audio_in_idx, conference_read_device_callback, &chat->audio_input_callback_data, - CONFAV_SAMPLE_RATE, CONFAV_FRAME_DURATION, channels) + CONFAV_SAMPLE_RATE, CONFAV_FRAME_DURATION, channels, + c_config->VAD_threshold) == de_None); chat->audio_enabled = success; @@ -1391,7 +1414,8 @@ bool toggle_conference_push_to_talk(uint32_t conferencenum, bool enabled) bool enable_conference_audio(ToxWindow *self, Toxic *toxic, uint32_t conferencenum) { if (!toxav_groupchat_av_enabled(toxic->tox, conferencenum)) { - if (toxav_groupchat_enable_av(toxic->tox, conferencenum, audio_conference_callback, NULL) != 0) { + if (toxav_groupchat_enable_av(toxic->tox, conferencenum, audio_conference_callback, + (void *) toxic->c_config) != 0) { return false; } } @@ -1402,7 +1426,7 @@ bool enable_conference_audio(ToxWindow *self, Toxic *toxic, uint32_t conferencen return true; } - const bool success = init_conference_audio_input(toxic->tox, conferencenum); + const bool success = init_conference_audio_input(toxic, conferencenum); if (success) { self->is_call = true; diff --git a/src/conference.h b/src/conference.h index b5672415e..a802e58fd 100644 --- a/src/conference.h +++ b/src/conference.h @@ -48,6 +48,7 @@ typedef struct ConferencePeer { typedef struct AudioInputCallbackData { Tox *tox; uint32_t conferencenum; + int audio_channels; } AudioInputCallbackData; #define PUBKEY_STRING_SIZE (2 * TOX_PUBLIC_KEY_SIZE + 1) @@ -84,7 +85,7 @@ typedef struct { } ConferenceChat; /* Frees all Toxic associated data structures for a conference (does not call tox_conference_delete() ) */ -void free_conference(ToxWindow *self, uint32_t conferencenum); +void free_conference(ToxWindow *self, const Client_Config *c_config, uint32_t conferencenum); int init_conference_win(Toxic *toxic, uint32_t conferencenum, uint8_t type, const char *title, size_t length); @@ -92,7 +93,7 @@ int init_conference_win(Toxic *toxic, uint32_t conferencenum, uint8_t type, cons void redraw_conference_win(ToxWindow *self); void conference_set_title(ToxWindow *self, uint32_t conferencesnum, const char *title, size_t length); -void conference_rename_log_path(Tox *tox, uint32_t conferencenum, const char *new_title); +void conference_rename_log_path(Toxic *toxic, uint32_t conferencenum, const char *new_title); int conference_enable_logging(ToxWindow *self, Tox *tox, uint32_t conferencenum, struct chatlog *log); /* Puts `(NameListEntry *)`s in `entries` for each matched peer, up to a maximum @@ -116,7 +117,7 @@ bool enable_conference_audio(ToxWindow *self, Toxic *toxic, uint32_t conferencen */ bool disable_conference_audio(ToxWindow *self, Toxic *toxic, uint32_t conferencenum); -bool init_conference_audio_input(Tox *tox, uint32_t conferencenum); +bool init_conference_audio_input(Toxic *toxic, uint32_t conferencenum); bool toggle_conference_push_to_talk(uint32_t conferencenum, bool enabled); void audio_conference_callback(void *tox, uint32_t conferencenum, uint32_t peernum, const int16_t *pcm, unsigned int samples, uint8_t channels, uint32_t diff --git a/src/conference_commands.c b/src/conference_commands.c index 81b6ad805..b3b6043af 100644 --- a/src/conference_commands.c +++ b/src/conference_commands.c @@ -30,9 +30,9 @@ #include "toxic.h" #include "windows.h" -static void print_err(ToxWindow *self, const char *error_str) +static void print_err(ToxWindow *self, const Client_Config *c_config, const char *error_str) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", error_str); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", error_str); } void cmd_conference_set_title(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) @@ -44,6 +44,7 @@ void cmd_conference_set_title(WINDOW *window, ToxWindow *self, Toxic *toxic, int } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; Tox_Err_Conference_Title err; char title[CONFERENCE_MAX_TITLE_LENGTH + 1]; @@ -52,17 +53,17 @@ void cmd_conference_set_title(WINDOW *window, ToxWindow *self, Toxic *toxic, int size_t tlen = tox_conference_get_title_size(tox, self->num, &err); if (err != TOX_ERR_CONFERENCE_TITLE_OK || tlen >= sizeof(title)) { - print_err(self, "Title is not set"); + print_err(self, c_config, "Title is not set"); return; } if (!tox_conference_get_title(tox, self->num, (uint8_t *) title, &err)) { - print_err(self, "Title is not set"); + print_err(self, c_config, "Title is not set"); return; } title[tlen] = '\0'; - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Title is set to: %s", title); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Title is set to: %s", title); return; } @@ -70,18 +71,18 @@ void cmd_conference_set_title(WINDOW *window, ToxWindow *self, Toxic *toxic, int size_t len = strlen(argv[1]); if (len >= sizeof(title)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to set title: max length exceeded."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to set title: max length exceeded."); return; } snprintf(title, sizeof(title), "%s", argv[1]); if (!tox_conference_set_title(tox, self->num, (uint8_t *) title, len, &err)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to set title (error %d)", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to set title (error %d)", err); return; } - conference_rename_log_path(tox, self->num, title); // must be called first + conference_rename_log_path(toxic, self->num, title); // must be called first conference_set_title(self, self->num, title, len); @@ -91,11 +92,12 @@ void cmd_conference_set_title(WINDOW *window, ToxWindow *self, Toxic *toxic, int size_t sn_len = tox_self_get_name_size(tox); selfnick[sn_len] = '\0'; - line_info_add(self, true, selfnick, NULL, NAME_CHANGE, 0, 0, " set the conference title to: %s", title); + line_info_add(self, c_config, true, selfnick, NULL, NAME_CHANGE, 0, 0, " set the conference title to: %s", + title); char tmp_event[MAX_STR_SIZE + 20]; snprintf(tmp_event, sizeof(tmp_event), "set title to %s", title); - write_to_log(tmp_event, selfnick, self->chatwin->log, true); + write_to_log(self->chatwin->log, c_config, tmp_event, selfnick, true); } #ifdef AUDIO @@ -107,6 +109,7 @@ void cmd_enable_audio(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, c return; } + const Client_Config *c_config = toxic->c_config; bool enable; if (argc == 1 && !strcasecmp(argv[1], "on")) { @@ -114,15 +117,15 @@ void cmd_enable_audio(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, c } else if (argc == 1 && !strcasecmp(argv[1], "off")) { enable = false; } else { - print_err(self, "Please specify: on | off"); + print_err(self, c_config, "Please specify: on | off"); return; } if (enable ? enable_conference_audio(self, toxic, self->num) : disable_conference_audio(self, toxic, self->num)) { - print_err(self, enable ? "Enabled conference audio. Use the '/ptt' command to toggle Push-To-Talk." + print_err(self, c_config, enable ? "Enabled conference audio. Use the '/ptt' command to toggle Push-To-Talk." : "Disabled conference audio"); } else { - print_err(self, enable ? "Failed to enable audio" : "Failed to disable audio"); + print_err(self, c_config, enable ? "Failed to enable audio" : "Failed to disable audio"); } } @@ -135,36 +138,39 @@ void cmd_conference_mute(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (argc < 1) { if (conference_mute_self(self->num)) { - print_err(self, "Toggled self audio mute status"); + print_err(self, c_config, "Toggled self audio mute status"); } else { - print_err(self, "No audio input to mute"); + print_err(self, c_config, "No audio input to mute"); } } else { NameListEntry *entries[16]; uint32_t n = get_name_list_entries_by_prefix(self->num, argv[1], entries, 16); if (n == 0) { - print_err(self, "No such peer"); + print_err(self, c_config, "No such peer"); return; } if (n > 1) { - print_err(self, "Multiple matching peers (use /mute [public key] to disambiguate):"); + print_err(self, c_config, "Multiple matching peers (use /mute [public key] to disambiguate):"); for (uint32_t i = 0; i < n; ++i) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s: %s", entries[i]->pubkey_str, entries[i]->name); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s: %s", entries[i]->pubkey_str, + entries[i]->name); } return; } if (conference_mute_peer(tox, self->num, entries[0]->peernum)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Toggled audio mute status of %s", entries[0]->name); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Toggled audio mute status of %s", + entries[0]->name); } else { - print_err(self, "Peer is not on the call"); + print_err(self, c_config, "Peer is not on the call"); } } } @@ -172,20 +178,21 @@ void cmd_conference_mute(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc void cmd_conference_sense(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(toxic); - if (self == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; + if (argc == 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Current VAD threshold: %.1f", + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Current VAD threshold: %.1f", (double) conference_get_VAD_threshold(self->num)); return; } if (argc > 1) { - print_err(self, "Only one argument allowed."); + print_err(self, c_config, "Only one argument allowed."); return; } @@ -193,26 +200,27 @@ void cmd_conference_sense(WINDOW *window, ToxWindow *self, Toxic *toxic, int arg float value = strtof(argv[1], &end); if (*end) { - print_err(self, "Invalid input"); + print_err(self, c_config, "Invalid input"); return; } if (conference_set_VAD_threshold(self->num, value)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Set VAD threshold to %.1f", (double) value); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Set VAD threshold to %.1f", (double) value); } else { - print_err(self, "Failed to set conference audio input sensitivity."); + print_err(self, c_config, "Failed to set conference audio input sensitivity."); } } void cmd_conference_push_to_talk(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(toxic); - if (self == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; + bool enable; if (argc == 1 && !strcasecmp(argv[1], "on")) { @@ -220,15 +228,15 @@ void cmd_conference_push_to_talk(WINDOW *window, ToxWindow *self, Toxic *toxic, } else if (argc == 1 && !strcasecmp(argv[1], "off")) { enable = false; } else { - print_err(self, "Please specify: on | off"); + print_err(self, c_config, "Please specify: on | off"); return; } if (!toggle_conference_push_to_talk(self->num, enable)) { - print_err(self, "Failed to toggle push to talk."); + print_err(self, c_config, "Failed to toggle push to talk."); return; } - print_err(self, enable ? "Push-To-Talk is enabled. Push F2 to activate" : "Push-To-Talk is disabled"); + print_err(self, c_config, enable ? "Push-To-Talk is enabled. Push F2 to activate" : "Push-To-Talk is disabled"); } #endif /* AUDIO */ diff --git a/src/execute.c b/src/execute.c index af2aaa185..62619e652 100644 --- a/src/execute.c +++ b/src/execute.c @@ -321,5 +321,5 @@ void execute(WINDOW *w, ToxWindow *self, Toxic *toxic, const char *input, int mo #endif - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid command."); + line_info_add(self, toxic->c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid command."); } diff --git a/src/file_transfers.c b/src/file_transfers.c index 2599e112c..e887c3f4b 100644 --- a/src/file_transfers.c +++ b/src/file_transfers.c @@ -332,10 +332,12 @@ int file_send_queue_remove(uint32_t friendnumber, size_t index) * Set CTRL to -1 if we don't want to send a control signal. * Set message or self to NULL if we don't want to display a message. */ -void close_file_transfer(ToxWindow *self, Tox *tox, FileTransfer *ft, int CTRL, const char *message, +void close_file_transfer(ToxWindow *self, const Toxic *toxic, FileTransfer *ft, int CTRL, const char *message, Notification sound_type) { - if (!ft) { + const Client_Config *c_config = toxic->c_config; + + if (ft == NULL) { return; } @@ -350,50 +352,50 @@ void close_file_transfer(ToxWindow *self, Tox *tox, FileTransfer *ft, int CTRL, if (CTRL >= 0) { Tox_Err_File_Control err; - if (!tox_file_control(tox, ft->friendnumber, ft->filenumber, (Tox_File_Control) CTRL, &err)) { + if (!tox_file_control(toxic->tox, ft->friendnumber, ft->filenumber, (Tox_File_Control) CTRL, &err)) { fprintf(stderr, "Failed to cancel file transfer: %d\n", err); } } if (message && self) { if (self->active_box != -1 && sound_type != silent) { - box_notify2(self, sound_type, NT_NOFOCUS | NT_WNDALERT_2, self->active_box, "%s", message); + box_notify2(self, c_config, sound_type, NT_NOFOCUS | NT_WNDALERT_2, self->active_box, "%s", message); } else { - box_notify(self, sound_type, NT_NOFOCUS | NT_WNDALERT_2, &self->active_box, self->name, "%s", message); + box_notify(self, c_config, sound_type, NT_NOFOCUS | NT_WNDALERT_2, &self->active_box, self->name, "%s", message); } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", message); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", message); } clear_file_transfer(ft); } /* Kills active outgoing avatar file transfers for friendnumber */ -void kill_avatar_file_transfers_friend(Tox *tox, uint32_t friendnumber) +void kill_avatar_file_transfers_friend(Toxic *toxic, uint32_t friendnumber) { for (size_t i = 0; i < MAX_FILES; ++i) { FileTransfer *ft = &Friends.list[friendnumber].file_sender[i]; if (ft->file_type == TOX_FILE_KIND_AVATAR) { - close_file_transfer(NULL, tox, ft, TOX_FILE_CONTROL_CANCEL, NULL, silent); + close_file_transfer(NULL, toxic, ft, TOX_FILE_CONTROL_CANCEL, NULL, silent); } } } /* Kills all active file transfers for friendnumber */ -void kill_all_file_transfers_friend(Tox *tox, uint32_t friendnumber) +void kill_all_file_transfers_friend(Toxic *toxic, uint32_t friendnumber) { for (size_t i = 0; i < MAX_FILES; ++i) { - close_file_transfer(NULL, tox, &Friends.list[friendnumber].file_sender[i], TOX_FILE_CONTROL_CANCEL, NULL, silent); - close_file_transfer(NULL, tox, &Friends.list[friendnumber].file_receiver[i], TOX_FILE_CONTROL_CANCEL, NULL, silent); + close_file_transfer(NULL, toxic, &Friends.list[friendnumber].file_sender[i], TOX_FILE_CONTROL_CANCEL, NULL, silent); + close_file_transfer(NULL, toxic, &Friends.list[friendnumber].file_receiver[i], TOX_FILE_CONTROL_CANCEL, NULL, silent); file_send_queue_remove(friendnumber, i); } } -void kill_all_file_transfers(Tox *tox) +void kill_all_file_transfers(Toxic *toxic) { for (size_t i = 0; i < Friends.max_idx; ++i) { - kill_all_file_transfers_friend(tox, Friends.list[i].num); + kill_all_file_transfers_friend(toxic, Friends.list[i].num); } } diff --git a/src/file_transfers.h b/src/file_transfers.h index 748c33a59..b8b27e95d 100644 --- a/src/file_transfers.h +++ b/src/file_transfers.h @@ -135,16 +135,16 @@ int file_send_queue_remove(uint32_t friendnumber, size_t index); * Set CTRL to -1 if we don't want to send a control signal. * Set message or self to NULL if we don't want to display a message. */ -void close_file_transfer(ToxWindow *self, Tox *tox, struct FileTransfer *ft, int CTRL, const char *message, +void close_file_transfer(ToxWindow *self, const Toxic *toxic, struct FileTransfer *ft, int CTRL, const char *message, Notification sound_type); /* Kills active outgoing avatar file transfers for friendnumber */ -void kill_avatar_file_transfers_friend(Tox *tox, uint32_t friendnumber); +void kill_avatar_file_transfers_friend(Toxic *toxic, uint32_t friendnumber); /* Kills all active file transfers for friendnumber */ -void kill_all_file_transfers_friend(Tox *tox, uint32_t friendnumber); +void kill_all_file_transfers_friend(Toxic *toxic, uint32_t friendnumber); -void kill_all_file_transfers(Tox *tox); +void kill_all_file_transfers(Toxic *toxic); /* Return true if any pending or active file receiver has the path `path`. */ bool file_transfer_recv_path_exists(const char *path); diff --git a/src/friendlist.c b/src/friendlist.c index b145b82f7..5367f5242 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -68,7 +68,7 @@ typedef enum DefaultFriendSettings { DefaultFriendSettingsTabNameColour = WHITE_BAR_FG, } DefaultFriendSettings; -static void set_default_friend_config_settings(ToxicFriend *friend) +static void set_default_friend_config_settings(ToxicFriend *friend, int autolog) { if (friend == NULL) { return; @@ -77,7 +77,7 @@ static void set_default_friend_config_settings(ToxicFriend *friend) FriendSettings *settings = &friend->settings; settings->tab_name_colour = DefaultFriendSettingsTabNameColour; - settings->autolog = (bool) user_settings->autolog == AUTOLOG_ON; + settings->autolog = autolog == AUTOLOG_ON; } static void realloc_friends(int n) @@ -122,7 +122,7 @@ static void realloc_blocklist(int n) Blocked.index = b_idx; } -void kill_friendlist(ToxWindow *self) +void kill_friendlist(ToxWindow *self, const Client_Config *c_config) { for (size_t i = 0; i < Friends.max_idx; ++i) { if (Friends.list[i].active) { @@ -140,7 +140,7 @@ void kill_friendlist(ToxWindow *self) realloc_blocklist(0); realloc_friends(0); free(self->help); - del_window(self); + del_window(self, c_config); } static void clear_blocklist_index(size_t idx) @@ -384,14 +384,13 @@ static void sort_blocklist_index(void) qsort(Blocked.index, Blocked.num_blocked, sizeof(uint32_t), index_name_cmp_block); } -static void update_friend_last_online(uint32_t num, time_t timestamp) +static void update_friend_last_online(uint32_t num, time_t timestamp, const char *timestamp_format) { Friends.list[num].last_online.last_on = timestamp; Friends.list[num].last_online.tm = *localtime((const time_t *)×tamp); /* if the format changes make sure TIME_STR_SIZE is the correct size */ - const char *t = user_settings->timestamp_format; - strftime(Friends.list[num].last_online.hour_min_str, TIME_STR_SIZE, t, + strftime(Friends.list[num].last_online.hour_min_str, TIME_STR_SIZE, timestamp_format, &Friends.list[num].last_online.tm); } @@ -407,6 +406,7 @@ static void friendlist_onMessage(ToxWindow *self, Toxic *toxic, uint32_t num, To } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (num >= Friends.max_idx) { return; @@ -424,9 +424,11 @@ static void friendlist_onMessage(ToxWindow *self, Toxic *toxic, uint32_t num, To char nick[TOX_MAX_NAME_LENGTH]; get_nick_truncate(tox, nick, num); - line_info_add(prompt, true, nick, NULL, IN_MSG, 0, 0, "%s", str); - line_info_add(prompt, false, NULL, NULL, SYS_MSG, 0, RED, "* Warning: Too many windows are open."); - sound_notify(prompt, notif_error, NT_WNDALERT_1, NULL); + line_info_add(prompt, c_config, true, nick, NULL, IN_MSG, 0, 0, "%s", str); + line_info_add(prompt, c_config, false, NULL, NULL, SYS_MSG, 0, RED, + "* Warning: Too many windows are open."); + + sound_notify(prompt, c_config, notif_error, NT_WNDALERT_1, NULL); } static void friendlist_onConnectionChange(ToxWindow *self, Toxic *toxic, uint32_t num, Tox_Connection connection_status) @@ -452,7 +454,7 @@ static void friendlist_onConnectionChange(ToxWindow *self, Toxic *toxic, uint32_ } Friends.list[num].connection_status = connection_status; - update_friend_last_online(num, get_unix_time()); + update_friend_last_online(num, get_unix_time(), toxic->c_config->timestamp_format); store_data(toxic); sort_friendlist_index(); } @@ -481,7 +483,8 @@ static void friendlist_onNickChange(ToxWindow *self, Toxic *toxic, uint32_t num, tox_self_get_address(toxic->tox, (uint8_t *) myid); if (strcmp(oldname, newnamecpy) != 0) { - if (rename_logfile(oldname, newnamecpy, myid, Friends.list[num].pub_key, Friends.list[num].chatwin) != 0) { + if (rename_logfile(toxic->c_config, oldname, newnamecpy, myid, Friends.list[num].pub_key, + Friends.list[num].chatwin) != 0) { fprintf(stderr, "Failed to rename friend chat log from `%s` to `%s`\n", oldname, newnamecpy); } } @@ -523,13 +526,12 @@ void friendlist_onFriendAdded(ToxWindow *self, Toxic *toxic, uint32_t num, bool } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; realloc_friends(Friends.max_idx + 1); clear_friendlist_index(Friends.max_idx); - uint32_t i; - - for (i = 0; i <= Friends.max_idx; ++i) { + for (uint32_t i = 0; i <= Friends.max_idx; ++i) { if (Friends.list[i].active) { continue; } @@ -542,7 +544,7 @@ void friendlist_onFriendAdded(ToxWindow *self, Toxic *toxic, uint32_t num, bool Friends.list[i].auto_accept_files = false; // do not change Friends.list[i].connection_status = TOX_CONNECTION_NONE; Friends.list[i].status = TOX_USER_STATUS_NONE; - set_default_friend_config_settings(&Friends.list[i]); + set_default_friend_config_settings(&Friends.list[i], c_config->autolog); Tox_Err_Friend_Get_Public_Key pkerr; tox_friend_get_public_key(tox, num, (uint8_t *) Friends.list[i].pub_key, &pkerr); @@ -558,7 +560,7 @@ void friendlist_onFriendAdded(ToxWindow *self, Toxic *toxic, uint32_t num, bool t = 0; } - update_friend_last_online(i, t); + update_friend_last_online(i, t, c_config->timestamp_format); char tempname[TOX_MAX_NAME_LENGTH + 1]; int name_len = get_nick_truncate(tox, tempname, num); @@ -583,14 +585,12 @@ void friendlist_onFriendAdded(ToxWindow *self, Toxic *toxic, uint32_t num, bool } /* Puts blocked friend back in friendlist. fnum is new friend number, bnum is blocked number. */ -static void friendlist_add_blocked(uint32_t fnum, uint32_t bnum) +static void friendlist_add_blocked(const Client_Config *c_config, uint32_t fnum, uint32_t bnum) { realloc_friends(Friends.max_idx + 1); clear_friendlist_index(Friends.max_idx); - int i; - - for (i = 0; i <= Friends.max_idx; ++i) { + for (int i = 0; i <= Friends.max_idx; ++i) { if (Friends.list[i].active) { continue; } @@ -601,9 +601,9 @@ static void friendlist_add_blocked(uint32_t fnum, uint32_t bnum) Friends.list[i].active = true; Friends.list[i].chatwin = -1; Friends.list[i].status = TOX_USER_STATUS_NONE; - Friends.list[i].logging_on = (bool) user_settings->autolog == AUTOLOG_ON; + Friends.list[i].logging_on = c_config->autolog == AUTOLOG_ON; Friends.list[i].namelength = Blocked.list[bnum].namelength; - update_friend_last_online(i, Blocked.list[bnum].last_on); + update_friend_last_online(i, Blocked.list[bnum].last_on, c_config->timestamp_format); memcpy(Friends.list[i].name, Blocked.list[bnum].name, Friends.list[i].namelength + 1); memcpy(Friends.list[i].pub_key, Blocked.list[bnum].pub_key, TOX_PUBLIC_KEY_SIZE); @@ -635,6 +635,7 @@ static void friendlist_onGameInvite(ToxWindow *self, Toxic *toxic, uint32_t frie } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (friend_number >= Friends.max_idx) { return; @@ -652,10 +653,10 @@ static void friendlist_onGameInvite(ToxWindow *self, Toxic *toxic, uint32_t frie char nick[TOX_MAX_NAME_LENGTH]; get_nick_truncate(tox, nick, friend_number); - line_info_add(prompt, false, NULL, NULL, SYS_MSG, 0, RED, + line_info_add(prompt, c_config, false, NULL, NULL, SYS_MSG, 0, RED, "* Game invite from %s failed: Too many windows are open.", nick); - sound_notify(prompt, notif_error, NT_WNDALERT_1, NULL); + sound_notify(prompt, c_config, notif_error, NT_WNDALERT_1, NULL); } #endif // GAMES @@ -669,6 +670,7 @@ static void friendlist_onFileRecv(ToxWindow *self, Toxic *toxic, uint32_t num, u UNUSED_VAR(name_length); Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (num >= Friends.max_idx) { return; @@ -688,10 +690,10 @@ static void friendlist_onFileRecv(ToxWindow *self, Toxic *toxic, uint32_t num, u char nick[TOX_MAX_NAME_LENGTH]; get_nick_truncate(tox, nick, num); - line_info_add(prompt, false, NULL, NULL, SYS_MSG, 0, RED, + line_info_add(prompt, c_config, false, NULL, NULL, SYS_MSG, 0, RED, "* File transfer from %s failed: too many windows are open.", nick); - sound_notify(prompt, notif_error, NT_WNDALERT_1, NULL); + sound_notify(prompt, c_config, notif_error, NT_WNDALERT_1, NULL); } static void friendlist_onConferenceInvite(ToxWindow *self, Toxic *toxic, int32_t num, uint8_t type, @@ -708,6 +710,7 @@ static void friendlist_onConferenceInvite(ToxWindow *self, Toxic *toxic, int32_t } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (num >= Friends.max_idx) { return; @@ -725,10 +728,10 @@ static void friendlist_onConferenceInvite(ToxWindow *self, Toxic *toxic, int32_t char nick[TOX_MAX_NAME_LENGTH]; get_nick_truncate(tox, nick, num); - line_info_add(prompt, false, NULL, NULL, SYS_MSG, 0, RED, + line_info_add(prompt, c_config, false, NULL, NULL, SYS_MSG, 0, RED, "* Conference chat invite from %s failed: too many windows are open.", nick); - sound_notify(prompt, notif_error, NT_WNDALERT_1, NULL); + sound_notify(prompt, c_config, notif_error, NT_WNDALERT_1, NULL); } static void friendlist_onGroupInvite(ToxWindow *self, Toxic *toxic, uint32_t num, const char *data, size_t length, @@ -743,6 +746,7 @@ static void friendlist_onGroupInvite(ToxWindow *self, Toxic *toxic, uint32_t num } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (num >= Friends.max_idx) { return; @@ -760,10 +764,10 @@ static void friendlist_onGroupInvite(ToxWindow *self, Toxic *toxic, uint32_t num char nick[TOX_MAX_NAME_LENGTH]; get_nick_truncate(tox, nick, num); - line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, RED, + line_info_add(prompt, c_config, NULL, NULL, NULL, SYS_MSG, 0, RED, "* Group chat invite from %s failed: too many windows are open.", nick); - sound_notify(prompt, notif_error, NT_WNDALERT_1, NULL); + sound_notify(prompt, c_config, notif_error, NT_WNDALERT_1, NULL); } /* move friendlist/blocklist cursor up and down */ @@ -790,8 +794,8 @@ static void delete_friend(Toxic *toxic, uint32_t f_num) Tox *tox = toxic->tox; - kill_all_file_transfers_friend(tox, f_num); - kill_avatar_file_transfers_friend(tox, f_num); + kill_all_file_transfers_friend(toxic, f_num); + kill_avatar_file_transfers_friend(toxic, f_num); Tox_Err_Friend_Delete err; @@ -979,11 +983,12 @@ static void unblock_friend(Toxic *toxic, uint32_t bnum) 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); + line_info_add(prompt, toxic->c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Failed to unblock friend (error %d)", err); return; } - friendlist_add_blocked(friendnum, bnum); + friendlist_add_blocked(toxic->c_config, friendnum, bnum); delete_blocked_friend(toxic, bnum); sort_blocklist_index(); sort_friendlist_index(); @@ -999,6 +1004,7 @@ static bool friendlist_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (self->help->active) { help_onKey(self, key); @@ -1053,8 +1059,8 @@ static bool friendlist_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr set_active_window_index(Friends.list[f].chatwin); } else { const char *msg = "* Warning: Too many windows are open."; - line_info_add(prompt, false, NULL, NULL, SYS_MSG, 0, RED, msg); - sound_notify(prompt, notif_error, NT_WNDALERT_1, NULL); + line_info_add(prompt, c_config, false, NULL, NULL, SYS_MSG, 0, RED, msg); + sound_notify(prompt, c_config, notif_error, NT_WNDALERT_1, NULL); } break; @@ -1440,6 +1446,7 @@ static void friendlist_onAV(ToxWindow *self, Toxic *toxic, uint32_t friend_numbe } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (Friends.list[friend_number].chatwin == -1) { if (get_num_active_windows() < MAX_WINDOWS_NUM) { @@ -1450,12 +1457,12 @@ static void friendlist_onAV(ToxWindow *self, Toxic *toxic, uint32_t friend_numbe } else { char nick[TOX_MAX_NAME_LENGTH]; get_nick_truncate(tox, nick, Friends.list[friend_number].num); - line_info_add(prompt, false, NULL, NULL, SYS_MSG, 0, 0, "Audio action from: %s!", nick); + line_info_add(prompt, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Audio action from: %s!", nick); const char *errmsg = "* Warning: Too many windows are open."; - line_info_add(prompt, false, NULL, NULL, SYS_MSG, 0, RED, errmsg); + line_info_add(prompt, c_config, false, NULL, NULL, SYS_MSG, 0, RED, errmsg); - sound_notify(prompt, notif_error, NT_WNDALERT_1, NULL); + sound_notify(prompt, c_config, notif_error, NT_WNDALERT_1, NULL); } } } @@ -1649,7 +1656,6 @@ ToxWindow *new_friendlist(void) ret->onInvite = &friendlist_onAV; ret->onRinging = &friendlist_onAV; ret->onStarting = &friendlist_onAV; - ret->onEnding = &friendlist_onAV; ret->onError = &friendlist_onAV; ret->onStart = &friendlist_onAV; ret->onCancel = &friendlist_onAV; diff --git a/src/friendlist.h b/src/friendlist.h index a0d8906a7..069758ed8 100644 --- a/src/friendlist.h +++ b/src/friendlist.h @@ -123,7 +123,7 @@ ToxWindow *new_friendlist(void); void friendlist_onInit(ToxWindow *self, Toxic *toxic); void disable_chatwin(uint32_t f_num); int get_friendnum(uint8_t *name); -void kill_friendlist(ToxWindow *self); +void kill_friendlist(ToxWindow *self, const Client_Config *c_config); void friendlist_onFriendAdded(ToxWindow *self, Toxic *toxic, uint32_t num, bool sort); Tox_User_Status get_friend_status(uint32_t friendnumber); Tox_Connection get_friend_connection_status(uint32_t friendnumber); diff --git a/src/game_base.c b/src/game_base.c index 4702a8fcf..f3679b457 100644 --- a/src/game_base.c +++ b/src/game_base.c @@ -38,7 +38,6 @@ #include "windows.h" extern struct Winthread Winthread; -extern struct user_settings *user_settings; /* * Determines the base rate at which game objects should update their state. @@ -112,14 +111,14 @@ const char *game_get_name_string(GameType type) return NULL; } -void game_list_print(ToxWindow *self) +void game_list_print(ToxWindow *self, const Client_Config *c_config) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Available games:"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Available games:"); const char *name = NULL; for (size_t i = 0; (name = game_list[i].name); ++i) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%zu: %s", i + 1, name); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%zu: %s", i + 1, name); } } @@ -155,11 +154,15 @@ void game_window_notify(const GameData *game, const char *message) return; } + const Client_Config *c_config = game->toxic->c_config; + + const int bell_on_message = c_config->bell_on_message; + if (self->active_box != -1) { - box_notify2(self, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | user_settings->bell_on_message, + box_notify2(self, c_config, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | bell_on_message, self->active_box, "%s", message); } else { - box_notify(self, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | user_settings->bell_on_message, + box_notify(self, c_config, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | bell_on_message, &self->active_box, self->name, "%s", message); } } @@ -172,11 +175,11 @@ TIME_MS get_time_millis(void) return ((TIME_MS) t.tv_sec) * 1000 + ((TIME_MS) t.tv_nsec) / 1000000; } -void game_kill(ToxWindow *self) +void game_kill(ToxWindow *self, const Client_Config *c_config) { GameData *game = self->game; - if (game) { + if (game != NULL) { if (game->cb_game_kill) { game->cb_game_kill(game, game->cb_game_kill_data); } @@ -187,16 +190,16 @@ void game_kill(ToxWindow *self) } kill_notifs(self->active_box); - del_window(self); + del_window(self, c_config); if (get_num_active_windows_type(WINDOW_TYPE_GAME) == 0) { set_window_refresh_rate(NCURSES_DEFAULT_REFRESH_RATE); } } -static void game_init_abort(const ToxWindow *parent, ToxWindow *self) +static void game_init_abort(const ToxWindow *parent, ToxWindow *self, const Client_Config *c_config) { - game_kill(self); + game_kill(self, c_config); set_active_window_index(parent->index); } @@ -253,6 +256,8 @@ static int game_initialize_type(GameData *game, const uint8_t *data, size_t leng int game_initialize(const ToxWindow *parent, Toxic *toxic, GameType type, uint32_t id, const uint8_t *multiplayer_data, size_t length, bool self_host) { + const Client_Config *c_config = toxic->c_config; + int max_x; int max_y; getmaxyx(parent->window, max_y, max_x); @@ -279,12 +284,12 @@ int game_initialize(const ToxWindow *parent, Toxic *toxic, GameType type, uint32 if (game->is_multiplayer) { if (parent->type != WINDOW_TYPE_CHAT) { - game_init_abort(parent, self); + game_init_abort(parent, self, c_config); return -3; } if (get_friend_connection_status(parent->num) == TOX_CONNECTION_NONE) { - game_init_abort(parent, self); + game_init_abort(parent, self, c_config); return -2; } @@ -303,14 +308,14 @@ int game_initialize(const ToxWindow *parent, Toxic *toxic, GameType type, uint32 game->friend_number = parent->num; if (game->window == NULL) { - game_init_abort(parent, self); + game_init_abort(parent, self, c_config); return -4; } const int init_ret = game_initialize_type(game, multiplayer_data, length, self_host); if (init_ret < 0) { - game_init_abort(parent, self); + game_init_abort(parent, self, c_config); return init_ret; } @@ -785,13 +790,12 @@ void game_onDraw(ToxWindow *self, Toxic *toxic) bool game_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool is_printable) { UNUSED_VAR(is_printable); - UNUSED_VAR(toxic); GameData *game = self->game; if (key == KEY_F(9)) { pthread_mutex_lock(&Winthread.lock); - game_kill(self); + game_kill(self, toxic->c_config); pthread_mutex_unlock(&Winthread.lock); return true; } diff --git a/src/game_base.h b/src/game_base.h index afac26c89..e8a1e06d1 100644 --- a/src/game_base.h +++ b/src/game_base.h @@ -257,7 +257,7 @@ const char *game_get_name_string(GameType type); /* * Prints all available games to window associated with `self`. */ -void game_list_print(ToxWindow *self); +void game_list_print(ToxWindow *self, const Client_Config *c_config); /* * Return true if game `type` has a multiplayer mode. @@ -394,7 +394,7 @@ TIME_MS get_time_millis(void); /* * Ends game associated with `self` and cleans up. */ -void game_kill(ToxWindow *self); +void game_kill(ToxWindow *self, const Client_Config *c_config); /* * Sends a packet containing payload `data` of size `length` to the friendnumber associated with the game's diff --git a/src/global_commands.c b/src/global_commands.c index 6283d14eb..ea7d16e0a 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -53,21 +53,22 @@ void cmd_accept(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Request ID required."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Request ID required."); return; } long int req = strtol(argv[1], NULL, 10); if ((req == 0 && strcmp(argv[1], "0")) || req < 0 || req >= MAX_FRIEND_REQUESTS) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending friend request with that ID."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "No pending friend request with that ID."); return; } if (!FrndRequests.request[req].active) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending friend request with that ID."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "No pending friend request with that ID."); return; } @@ -75,10 +76,10 @@ void cmd_accept(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* uint32_t friendnum = tox_friend_add_norequest(tox, FrndRequests.request[req].key, &err); if (err != TOX_ERR_FRIEND_ADD_OK) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to add friend (error %d\n)", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to add friend (error %d\n)", err); return; } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Friend request accepted."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Friend request accepted."); on_friend_added(toxic, friendnum, true); } @@ -100,7 +101,7 @@ void cmd_accept(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* void cmd_add_helper(ToxWindow *self, Toxic *toxic, const char *id_bin, const char *msg) { - const char *errmsg; + const char *errmsg = NULL; Tox_Err_Friend_Add err; uint32_t f_num = tox_friend_add(toxic->tox, (const uint8_t *) id_bin, (const uint8_t *) msg, strlen(msg), &err); @@ -147,7 +148,7 @@ void cmd_add_helper(ToxWindow *self, Toxic *toxic, const char *id_bin, const cha break; } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, errmsg); + line_info_add(self, toxic->c_config, false, NULL, NULL, SYS_MSG, 0, 0, errmsg); } void cmd_add(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) @@ -159,9 +160,10 @@ void cmd_add(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Tox ID or address required."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Tox ID or address required."); return; } @@ -193,7 +195,7 @@ void cmd_add(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg return; } } else if (!valid_id_size) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid Tox ID."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid Tox ID."); return; } @@ -206,7 +208,7 @@ void cmd_add(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg xx[2] = 0; if (sscanf(xx, "%02x", &x) != 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid Tox ID."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid Tox ID."); return; } @@ -214,7 +216,7 @@ void cmd_add(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg } if (friend_is_blocked(id_bin)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Friend is in your block list."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Friend is in your block list."); return; } @@ -230,10 +232,11 @@ void cmd_avatar(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; 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."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Avatar has been unset."); return; } @@ -242,7 +245,7 @@ void cmd_avatar(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* int len = strlen(path); if (len <= 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid path."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid path."); return; } @@ -251,13 +254,13 @@ void cmd_avatar(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* get_file_name(filename, sizeof(filename), path); if (avatar_set(tox, path, len) == -1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to set avatar. Avatars must be in PNG format and may not exceed %d bytes.", MAX_AVATAR_FILE_SIZE); return; } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Avatar set to '%s'", filename); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Avatar set to '%s'", filename); } void cmd_clear(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) @@ -277,14 +280,15 @@ void cmd_clear(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*a void cmd_color(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(toxic); - if (self == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; + if (argc != 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Change the name of the focused window with: " "/color [black|white|gray|brown|red|green|blue|cyan|yellow|magenta|orange|pink]"); return; @@ -295,7 +299,7 @@ void cmd_color(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*a const int colour_val = colour_string_to_int(colour); if (colour_val < 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Valid colors are: black|white|gray|brown|red|green|blue|cyan|yellow|magenta|orange|pink"); return; } @@ -312,9 +316,10 @@ void cmd_connect(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char ( } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (argc != 3) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Require: "); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Require: "); return; } @@ -325,14 +330,14 @@ void cmd_connect(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char ( long int port = strtol(port_str, NULL, 10); if (port <= 0 || port > MAX_PORT_RANGE) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid port."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid port."); return; } char key_binary[TOX_PUBLIC_KEY_SIZE]; if (tox_pk_string_to_bytes(ascii_key, strlen(ascii_key), key_binary, sizeof(key_binary)) == -1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid key."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid key."); return; } @@ -342,15 +347,15 @@ void cmd_connect(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char ( switch (err) { case TOX_ERR_BOOTSTRAP_BAD_HOST: - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Bootstrap failed: Invalid IP."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Bootstrap failed: Invalid IP."); break; case TOX_ERR_BOOTSTRAP_BAD_PORT: - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Bootstrap failed: Invalid port."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Bootstrap failed: Invalid port."); break; case TOX_ERR_BOOTSTRAP_NULL: - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Bootstrap failed."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Bootstrap failed."); break; default: @@ -361,26 +366,27 @@ void cmd_connect(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char ( void cmd_decline(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(toxic); - if (self == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; + if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Request ID required."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Request ID required."); return; } long int req = strtol(argv[1], NULL, 10); if ((req == 0 && strcmp(argv[1], "0")) || req < 0 || req >= MAX_FRIEND_REQUESTS) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending friend request with that ID."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "No pending friend request with that ID."); return; } if (!FrndRequests.request[req].active) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending friend request with that ID."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "No pending friend request with that ID."); return; } @@ -410,20 +416,22 @@ void cmd_game(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar return; } + const Client_Config *c_config = toxic->c_config; + if (argc < 1) { - game_list_print(self); + game_list_print(self, c_config); return; } GameType type = game_get_type(argv[1]); if (type >= GT_Invalid) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Unknown game."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Unknown game."); return; } 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."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, " * Warning: Too many windows are open."); return; } @@ -436,24 +444,24 @@ void cmd_game(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar } case -1: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Window is too small."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Window is too small."); return; } case -2: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Game failed to initialize: Network error."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Game failed to initialize: Network error."); return; } case -3: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Game is multiplayer only. Try the command again in the chat window of the contact you wish to play with."); return; } default: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Game failed to initialize (error %d)", ret); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Game failed to initialize (error %d)", ret); return; } } @@ -470,14 +478,15 @@ void cmd_conference(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, cha } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; 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."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, " * Warning: Too many windows are open."); return; } if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Please specify conference type: text | audio"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Please specify conference type: text | audio"); return; } @@ -488,7 +497,7 @@ void cmd_conference(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, cha } else if (!strcasecmp(argv[1], "text")) { type = TOX_CONFERENCE_TYPE_TEXT; } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Valid conference types are: text | audio"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Valid conference types are: text | audio"); return; } @@ -500,7 +509,8 @@ void cmd_conference(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, cha conferencenum = tox_conference_new(tox, &err); if (err != TOX_ERR_CONFERENCE_NEW_OK) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Conference instance failed to initialize (error %d)", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Conference instance failed to initialize (error %d)", err); return; } } else if (type == TOX_CONFERENCE_TYPE_AV) { @@ -508,18 +518,20 @@ void cmd_conference(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, cha conferencenum = toxav_add_av_groupchat(tox, audio_conference_callback, NULL); if (conferencenum == (uint32_t) -1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Audio conference instance failed to initialize"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Audio conference instance failed to initialize"); return; } #else - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Audio support disabled by compile-time option."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Audio support disabled by compile-time option."); return; #endif } 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."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Conference window failed to initialize."); tox_conference_delete(tox, conferencenum, NULL); return; } @@ -527,14 +539,15 @@ void cmd_conference(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, cha #ifdef AUDIO if (type == TOX_CONFERENCE_TYPE_AV) { - if (!init_conference_audio_input(tox, conferencenum)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Audio capture failed; use \"/audio on\" to try again."); + if (!init_conference_audio_input(toxic, conferencenum)) { + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Audio capture failed; use \"/audio on\" to try again."); } } #endif - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Conference [%d] created.", conferencenum); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Conference [%d] created.", conferencenum); } void cmd_groupchat(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) @@ -545,14 +558,15 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; 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."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, " * Warning: Too many windows are open."); return; } if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Group name required"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Group name required"); return; } @@ -560,7 +574,7 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char int len = strlen(tmp_name); if (len == 0 || len > TOX_GROUP_MAX_GROUP_NAME_LENGTH) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid group name."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid group name."); return; } @@ -580,18 +594,19 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char if (err != TOX_ERR_GROUP_NEW_OK) { switch (err) { case TOX_ERR_GROUP_NEW_TOO_LONG: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Group name length cannot exceed %d.", + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Group name length cannot exceed %d.", TOX_GROUP_MAX_GROUP_NAME_LENGTH); break; } case TOX_ERR_GROUP_NEW_EMPTY: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Group name cannot be empty."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Group name cannot be empty."); break; } default: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Group chat instance failed to initialize (error %d).", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Group chat instance failed to initialize (error %d).", err); break; } } @@ -602,10 +617,10 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char 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."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Group chat window failed to initialize."); tox_group_leave(tox, groupnumber, NULL, 0, NULL); } else if (init == -2) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "You have been kicked from a group. Close the window and try again."); tox_group_leave(tox, groupnumber, NULL, 0, NULL); } @@ -618,21 +633,22 @@ void cmd_join(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; 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."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, " * Warning: Too many windows are open."); return; } if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Chat ID is required."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Chat ID is required."); return; } const char *chat_id = argv[1]; if (strlen(chat_id) != TOX_GROUP_CHAT_ID_SIZE * 2) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid chat ID"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid chat ID"); return; } @@ -648,7 +664,7 @@ void cmd_join(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar xch[2] = '\0'; if (sscanf(xch, "%02x", &x) != 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid chat ID."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid chat ID."); return; } @@ -664,7 +680,8 @@ void cmd_join(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar } if (passwd_len > TOX_GROUP_MAX_PASSWORD_SIZE) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Password length cannot exceed %d.", TOX_GROUP_MAX_PASSWORD_SIZE); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Password length cannot exceed %d.", + TOX_GROUP_MAX_PASSWORD_SIZE); return; } @@ -678,14 +695,14 @@ void cmd_join(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar (const uint8_t *) passwd, passwd_len, &err); if (err != TOX_ERR_GROUP_JOIN_OK) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to join group (error %d).", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to join group (error %d).", err); return; } 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."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Group chat window failed to initialize."); tox_group_leave(tox, groupnumber, NULL, 0, NULL); } } @@ -693,12 +710,13 @@ void cmd_join(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar 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) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; + const char *msg; struct chatlog *log = self->chatwin->log; @@ -709,7 +727,7 @@ void cmd_log(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg msg = "Logging for this window is OFF; type \"/log on\" to enable."; } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, msg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, msg); return; } @@ -717,7 +735,7 @@ void cmd_log(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg if (!strcmp(swch, "1") || !strcmp(swch, "on")) { msg = log_enable(log) == 0 ? "Logging enabled." : "Warning: Failed to enable log."; - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, msg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, msg); return; } else if (!strcmp(swch, "0") || !strcmp(swch, "off")) { if (self->type == WINDOW_TYPE_CHAT) { @@ -727,12 +745,12 @@ void cmd_log(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg log_disable(log); msg = "Logging disabled."; - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, msg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, msg); return; } msg = "Invalid option. Use \"/log on\" and \"/log off\" to toggle logging."; - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, msg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, msg); } void cmd_myid(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) @@ -745,16 +763,18 @@ void cmd_myid(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar return; } + const Client_Config *c_config = toxic->c_config; + char id_string[TOX_ADDRESS_SIZE * 2 + 1]; char bin_id[TOX_ADDRESS_SIZE]; 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."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to print ID."); return; } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", id_string); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", id_string); } #ifdef QRCODE @@ -767,13 +787,14 @@ void cmd_myqr(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; char id_string[TOX_ADDRESS_SIZE * 2 + 1]; char bin_id[TOX_ADDRESS_SIZE]; tox_self_get_address(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 create QR code."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code."); return; } @@ -786,7 +807,7 @@ void cmd_myqr(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar char *dir = malloc(data_file_len + 1); if (dir == NULL) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code: Out of memory."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code: Out of memory."); return; } @@ -795,7 +816,7 @@ void cmd_myqr(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar #ifdef QRPNG if (argc == 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Required 'txt' or 'png'"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Required 'txt' or 'png'"); free(dir); return; } else if (!strcmp(argv[1], "txt")) { @@ -805,7 +826,7 @@ void cmd_myqr(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar char *qr_path = malloc(qr_path_buf_size); if (qr_path == NULL) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code: Out of memory"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code: Out of memory"); free(dir); return; } @@ -813,13 +834,14 @@ void cmd_myqr(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar snprintf(qr_path, qr_path_buf_size, "%s%s%s", dir, nick, QRCODE_FILENAME_EXT); if (ID_to_QRcode_txt(id_string, qr_path) == -1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code."); free(dir); free(qr_path); return; } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "QR code has been printed to the file '%s'", qr_path); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "QR code has been printed to the file '%s'", + qr_path); free(qr_path); @@ -829,7 +851,7 @@ void cmd_myqr(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar char *qr_path = malloc(qr_path_buf_size); if (qr_path == NULL) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code: Out of memory"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code: Out of memory"); free(dir); return; } @@ -837,18 +859,20 @@ void cmd_myqr(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar snprintf(qr_path, qr_path_buf_size, "%s%s%s", dir, nick, QRCODE_FILENAME_EXT_PNG); if (ID_to_QRcode_png(id_string, qr_path) == -1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code."); free(dir); free(qr_path); return; } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "QR code has been printed to the file '%s'", qr_path); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "QR code has been printed to the file '%s'", + qr_path); free(qr_path); } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Unknown option '%s' -- Required 'txt' or 'png'", argv[1]); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Unknown option '%s' -- Required 'txt' or 'png'", + argv[1]); free(dir); return; } @@ -868,9 +892,10 @@ void cmd_nick(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Input required."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Input required."); return; } @@ -879,7 +904,7 @@ void cmd_nick(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar size_t len = strlen(nick); if (!valid_nick(nick)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid name."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid name."); return; } @@ -903,7 +928,7 @@ void cmd_note(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar const char *note = argc >= 1 ? argv[1] : ""; - prompt_update_statusmessage(prompt, toxic->tox, note); + prompt_update_statusmessage(prompt, toxic, note); } void cmd_nospam(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) @@ -913,6 +938,7 @@ void cmd_nospam(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; long int nospam = (long int)rand_not_secure(); // the nospam isn't cryptographically sensitive @@ -920,7 +946,7 @@ void cmd_nospam(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* nospam = strtol(argv[1], NULL, 16); if ((nospam == 0 && strcmp(argv[1], "0")) || nospam < 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid nospam value."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid nospam value."); return; } } @@ -928,12 +954,13 @@ void cmd_nospam(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* uint32_t old_nospam = tox_self_get_nospam(tox); tox_self_set_nospam(tox, (uint32_t) nospam); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Your new Tox ID is:"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Your new Tox ID is:"); 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, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, ""); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Any services that relied on your old ID will need to be updated manually."); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "If you ever want your old Tox ID back, type '/nospam %X'", + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "If you ever want your old Tox ID back, type '/nospam %X'", old_nospam); } @@ -964,20 +991,22 @@ void cmd_quit(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar void cmd_requests(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(toxic); UNUSED_VAR(argc); UNUSED_VAR(argv); - if (self == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; + if (FrndRequests.num_requests == 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending friend requests."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "No pending friend requests."); return; } - int i, j; + int i; + int j; int count = 0; for (i = 0; i < FrndRequests.max_idx; ++i) { @@ -993,11 +1022,11 @@ void cmd_requests(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char strcat(id, d); } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%d : %s", i, id); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", FrndRequests.request[i].msg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%d : %s", i, id); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", FrndRequests.request[i].msg); if (++count < FrndRequests.num_requests) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, ""); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, ""); } } } @@ -1011,14 +1040,15 @@ void cmd_status(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; - const char *errmsg; + const char *errmsg = NULL; lock_status(); if (argc < 1) { errmsg = "Require a status. Statuses are: online, busy and away."; - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, errmsg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, errmsg); goto finish; } @@ -1033,7 +1063,7 @@ void cmd_status(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* status = TOX_USER_STATUS_BUSY; } else { errmsg = "Invalid status. Valid statuses are: online, busy and away."; - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, errmsg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, errmsg); goto finish; } @@ -1041,7 +1071,8 @@ void cmd_status(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* prompt_update_status(prompt, status); set_status_all_groups(toxic, status); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Your status has been changed to %s.", status_str); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Your status has been changed to %s.", + status_str); finish: diff --git a/src/groupchat_commands.c b/src/groupchat_commands.c index edc780763..9e59db1b2 100644 --- a/src/groupchat_commands.c +++ b/src/groupchat_commands.c @@ -40,13 +40,16 @@ void cmd_chatid(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* return; } + const Client_Config *c_config = toxic->c_config; + 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(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); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve the Chat ID (error %d).", + err); return; } @@ -56,7 +59,7 @@ void cmd_chatid(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* strcat(chatid, xx); } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", chatid); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", chatid); } void cmd_disconnect(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) @@ -67,22 +70,27 @@ void cmd_disconnect(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, cha return; } + const Client_Config *c_config = toxic->c_config; + Tox_Err_Group_Disconnect err; tox_group_disconnect(toxic->tox, self->num, &err); switch (err) { case TOX_ERR_GROUP_DISCONNECT_OK: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Disconnected from group. Type '/rejoin' to reconnect."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Disconnected from group. Type '/rejoin' to reconnect."); return; } case TOX_ERR_GROUP_DISCONNECT_ALREADY_DISCONNECTED: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Already disconnected. Type '/rejoin' to connect."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Already disconnected. Type '/rejoin' to connect."); return; } default: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to disconnect from group. Error: %d", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to disconnect from group. Error: %d", + err); return; } } @@ -96,8 +104,10 @@ void cmd_group_nick(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, cha return; } + const Client_Config *c_config = toxic->c_config; + if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Input required."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Input required."); return; } @@ -106,7 +116,7 @@ void cmd_group_nick(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, cha size_t len = strlen(nick); if (!valid_nick(nick)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid name."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid name."); return; } @@ -126,15 +136,17 @@ void cmd_ignore(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* return; } + const Client_Config *c_config = toxic->c_config; + if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); return; } const char *nick = argv[1]; uint32_t peer_id; - if (group_get_peer_id_of_identifier(self, nick, &peer_id) != 0) { + if (group_get_peer_id_of_identifier(self, c_config, nick, &peer_id) != 0) { return; } @@ -147,22 +159,23 @@ void cmd_ignore(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* } case TOX_ERR_GROUP_SET_IGNORE_SELF: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You cannot ignore yourself."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "You cannot ignore yourself."); return; } case TOX_ERR_GROUP_SET_IGNORE_PEER_NOT_FOUND: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "The specified nick or public key is invalid."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "The specified nick or public key is invalid."); return; } default: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to toggle ignore on %s (error %d).", nick, err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to toggle ignore on %s (error %d).", + nick, err); return; } } - line_info_add(self, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- Ignoring %s", nick); + line_info_add(self, c_config, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- Ignoring %s", nick); group_toggle_peer_ignore(self->num, peer_id, true); } @@ -176,9 +189,10 @@ void cmd_kick(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); return; } @@ -186,7 +200,7 @@ void cmd_kick(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar uint32_t target_peer_id; - if (group_get_peer_id_of_identifier(self, nick, &target_peer_id) != 0) { + if (group_get_peer_id_of_identifier(self, c_config, nick, &target_peer_id) != 0) { return; } @@ -198,29 +212,31 @@ void cmd_kick(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar char self_nick[TOX_MAX_NAME_LENGTH + 1]; get_group_self_nick_truncate(tox, self_nick, self->num); - line_info_add(self, true, NULL, NULL, SYS_MSG, 1, RED, "-!- %s has been kicked by %s", nick, self_nick); + line_info_add(self, c_config, true, NULL, NULL, SYS_MSG, 1, RED, "-!- %s has been kicked by %s", nick, + self_nick); groupchat_onGroupPeerExit(self, toxic, self->num, target_peer_id, TOX_GROUP_EXIT_TYPE_KICK, nick, strlen(nick), NULL, 0); return; } case TOX_ERR_GROUP_MOD_KICK_PEER_PERMISSIONS: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You do not have permission to kick %s.", nick); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "You do not have permission to kick %s.", nick); return; } case TOX_ERR_GROUP_MOD_KICK_PEER_SELF: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You cannot kick yourself."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "You cannot kick yourself."); return; } case TOX_ERR_GROUP_MOD_KICK_PEER_PEER_NOT_FOUND: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Specified nick or public key is invalid."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Specified nick or public key is invalid."); return; } default: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to kick %s from the group (error %d).", nick, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to kick %s from the group (error %d).", + nick, err); return; } @@ -230,16 +246,16 @@ void cmd_kick(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar 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) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; GroupChat *chat = get_groupchat(self->num); if (chat == NULL) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch GroupChat object."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch GroupChat object."); return; } @@ -258,7 +274,7 @@ void cmd_list(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*ar strcat(pk_string, d); } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s : %s", pk_string, peer->name); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s : %s", pk_string, peer->name); } } @@ -271,16 +287,17 @@ void cmd_mod(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); return; } const char *nick = argv[1]; uint32_t target_peer_id; - if (group_get_peer_id_of_identifier(self, nick, &target_peer_id) != 0) { + if (group_get_peer_id_of_identifier(self, c_config, nick, &target_peer_id) != 0) { return; } @@ -288,7 +305,7 @@ void cmd_mod(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg uint32_t self_peer_id = tox_group_self_get_peer_id(tox, self->num, &s_err); if (s_err != TOX_ERR_GROUP_SELF_QUERY_OK) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch self peer_id."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch self peer_id."); return; } @@ -302,27 +319,29 @@ void cmd_mod(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg } case TOX_ERR_GROUP_MOD_SET_ROLE_PERMISSIONS: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You do not have permission to promote moderators."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "You do not have permission to promote moderators."); return; } case TOX_ERR_GROUP_MOD_SET_ROLE_ASSIGNMENT: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s is already a moderator.", nick); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s is already a moderator.", nick); return; } case TOX_ERR_GROUP_MOD_SET_ROLE_SELF: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You cannot make yourself a moderator."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "You cannot make yourself a moderator."); return; } case TOX_ERR_GROUP_MOD_SET_ROLE_PEER_NOT_FOUND: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "The specified nick or public key is invalid."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "The specified nick or public key is invalid."); return; } default: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to promote peer to moderator (error %d).", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Failed to promote peer to moderator (error %d).", err); return; } } @@ -337,16 +356,17 @@ void cmd_unmod(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*a } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); return; } const char *nick = argv[1]; uint32_t target_peer_id; - if (group_get_peer_id_of_identifier(self, nick, &target_peer_id) != 0) { + if (group_get_peer_id_of_identifier(self, c_config, nick, &target_peer_id) != 0) { return; } @@ -354,12 +374,12 @@ void cmd_unmod(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*a uint32_t self_peer_id = tox_group_self_get_peer_id(tox, self->num, &s_err); if (s_err != TOX_ERR_GROUP_SELF_QUERY_OK) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch self peer_id."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch self peer_id."); return; } if (tox_group_peer_get_role(tox, self->num, target_peer_id, NULL) != TOX_GROUP_ROLE_MODERATOR) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s is not a moderator.", nick); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s is not a moderator.", nick); return; } @@ -373,22 +393,24 @@ void cmd_unmod(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*a } case TOX_ERR_GROUP_MOD_SET_ROLE_PERMISSIONS: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You do not have permission to unmod %s.", nick); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "You do not have permission to unmod %s.", + nick); return; } case TOX_ERR_GROUP_MOD_SET_ROLE_SELF: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You cannot remove your own moderator status."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "You cannot remove your own moderator status."); return; } case TOX_ERR_GROUP_MOD_SET_ROLE_PEER_NOT_FOUND: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "The specified nick or public key is invalid."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "The specified nick or public key is invalid."); return; } default: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to revoke moderator powers from %s (error %d).", nick, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Failed to revoke moderator powers from %s (error %d).", nick, err); return; } @@ -403,6 +425,8 @@ void cmd_set_passwd(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, cha return; } + const Client_Config *c_config = toxic->c_config; + const char *passwd = NULL; size_t len = 0; @@ -417,27 +441,28 @@ void cmd_set_passwd(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, cha switch (err) { case TOX_ERR_GROUP_FOUNDER_SET_PASSWORD_OK: { if (len > 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Password has been set to %s.", passwd); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Password has been set to %s.", passwd); } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Password has been unset."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Password has been unset."); } return; } case TOX_ERR_GROUP_FOUNDER_SET_PASSWORD_TOO_LONG: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Password length must not exceed %d.", + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Password length must not exceed %d.", TOX_GROUP_MAX_PASSWORD_SIZE); return; } case TOX_ERR_GROUP_FOUNDER_SET_PASSWORD_PERMISSIONS: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You do not have permission to set the password."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "You do not have permission to set the password."); return; } default: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to set password (error %d).", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to set password (error %d).", err); return; } } @@ -452,6 +477,7 @@ void cmd_set_peerlimit(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; int maxpeers = 0; @@ -460,18 +486,19 @@ void cmd_set_peerlimit(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, uint32_t maxpeers = tox_group_get_peer_limit(tox, self->num, &err); if (err != TOX_ERR_GROUP_STATE_QUERIES_OK) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve peer limit (error %d).", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve peer limit (error %d).", + err); return; } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer limit is set to %d", maxpeers); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Peer limit is set to %d", maxpeers); return; } maxpeers = atoi(argv[1]); if (maxpeers <= 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer limit must be a value greater than 0."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Peer limit must be a value greater than 0."); return; } @@ -480,17 +507,18 @@ void cmd_set_peerlimit(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, switch (err) { case TOX_ERR_GROUP_FOUNDER_SET_PEER_LIMIT_OK: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer limit has been set to %d.", maxpeers); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Peer limit has been set to %d.", maxpeers); return; } case TOX_ERR_GROUP_FOUNDER_SET_PEER_LIMIT_PERMISSIONS: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You do not have permission to set the peer limit."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "You do not have permission to set the peer limit."); return; } default: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to set the peer limit (error %d).", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to set the peer limit (error %d).", err); return; } } @@ -505,6 +533,7 @@ void cmd_set_voice(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; Tox_Group_Voice_State voice_state; @@ -513,28 +542,29 @@ void cmd_set_voice(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char voice_state = tox_group_get_voice_state(tox, self->num, &err); if (err != TOX_ERR_GROUP_STATE_QUERIES_OK) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve voice state (error %d).", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve voice state (error %d).", + err); return; } switch (voice_state) { case TOX_GROUP_VOICE_STATE_ALL: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Voice state is set to ALL"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Voice state is set to ALL"); break; } case TOX_GROUP_VOICE_STATE_MODERATOR: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Voice state is set to MODERATOR"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Voice state is set to MODERATOR"); break; } case TOX_GROUP_VOICE_STATE_FOUNDER: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Voice state is set to FOUNDER"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Voice state is set to FOUNDER"); break; } default: - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Error: Unknown voice state: %d", voice_state); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Error: Unknown voice state: %d", voice_state); return; } @@ -550,7 +580,7 @@ void cmd_set_voice(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char } else if (strcasecmp(vstate_str, "all") == 0) { voice_state = TOX_GROUP_VOICE_STATE_ALL; } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "voice state must be \"all\", \"mod\", or \"founder\"."); return; } @@ -560,17 +590,18 @@ void cmd_set_voice(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char switch (err) { case TOX_ERR_GROUP_FOUNDER_SET_VOICE_STATE_OK: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Voice state has been set to %s.", vstate_str); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Voice state has been set to %s.", vstate_str); return; } case TOX_ERR_GROUP_FOUNDER_SET_VOICE_STATE_PERMISSIONS: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You do not have permission to set the voice state."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "You do not have permission to set the voice state."); return; } default: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Error setting voice state (error %d).", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Error setting voice state (error %d).", err); return; } } @@ -585,6 +616,7 @@ void cmd_set_privacy(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, ch } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; const char *pstate_str = NULL; Tox_Group_Privacy_State privacy_state; @@ -594,19 +626,21 @@ void cmd_set_privacy(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, ch privacy_state = tox_group_get_privacy_state(tox, self->num, &err); if (err != TOX_ERR_GROUP_STATE_QUERIES_OK) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve privacy state (error %d).", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve privacy state (error %d).", + err); return; } pstate_str = privacy_state == TOX_GROUP_PRIVACY_STATE_PRIVATE ? "private" : "public"; - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Privacy state is set to %s.", pstate_str); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Privacy state is set to %s.", pstate_str); return; } pstate_str = argv[1]; if (strcasecmp(pstate_str, "private") != 0 && strcasecmp(pstate_str, "public") != 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Privacy state must be \"private\" or \"public\"."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Privacy state must be \"private\" or \"public\"."); return; } @@ -618,17 +652,18 @@ void cmd_set_privacy(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, ch switch (err) { case TOX_ERR_GROUP_FOUNDER_SET_PRIVACY_STATE_OK: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Privacy state has been set to %s.", pstate_str); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Privacy state has been set to %s.", pstate_str); return; } case TOX_ERR_GROUP_FOUNDER_SET_PRIVACY_STATE_PERMISSIONS: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You do not have permission to set the privacy state."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "You do not have permission to set the privacy state."); return; } default: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Error setting privacy state (error %d).", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Error setting privacy state (error %d).", err); return; } } @@ -643,6 +678,7 @@ void cmd_set_topic_lock(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; Tox_Group_Topic_Lock topic_lock; const char *tlock_str = NULL; @@ -652,19 +688,20 @@ void cmd_set_topic_lock(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, topic_lock = tox_group_get_topic_lock(tox, self->num, &err); if (err != TOX_ERR_GROUP_STATE_QUERIES_OK) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve topic lock (error %d).", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve topic lock (error %d).", + err); return; } tlock_str = topic_lock == TOX_GROUP_TOPIC_LOCK_ENABLED ? "Enabled" : "Disabled"; - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Topic lock is %s.", tlock_str); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Topic lock is %s.", tlock_str); return; } tlock_str = argv[1]; if (strcasecmp(tlock_str, "on") != 0 && strcasecmp(tlock_str, "off") != 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Topic lock must be \"on\" or \"off\"."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Topic lock must be \"on\" or \"off\"."); return; } @@ -676,17 +713,18 @@ void cmd_set_topic_lock(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, switch (err) { case TOX_ERR_GROUP_FOUNDER_SET_TOPIC_LOCK_OK: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Topic lock has been %s.", display_str); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Topic lock has been %s.", display_str); return; } case TOX_ERR_GROUP_FOUNDER_SET_TOPIC_LOCK_PERMISSIONS: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You do not have permission to set the topic lock."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "You do not have permission to set the topic lock."); return; } default: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Error setting topic lock (%d).", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Error setting topic lock (%d).", err); return; } } @@ -701,16 +739,17 @@ void cmd_silence(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char ( } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); return; } const char *nick = argv[1]; uint32_t target_peer_id; - if (group_get_peer_id_of_identifier(self, nick, &target_peer_id) != 0) { + if (group_get_peer_id_of_identifier(self, c_config, nick, &target_peer_id) != 0) { return; } @@ -718,7 +757,7 @@ void cmd_silence(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char ( uint32_t self_peer_id = tox_group_self_get_peer_id(tox, self->num, &s_err); if (s_err != TOX_ERR_GROUP_SELF_QUERY_OK) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch self peer_id."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch self peer_id."); return; } @@ -732,27 +771,28 @@ void cmd_silence(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char ( } case TOX_ERR_GROUP_MOD_SET_ROLE_PERMISSIONS: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You do not have permission to silence %s.", nick); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "You do not have permission to silence %s.", + nick); return; } case TOX_ERR_GROUP_MOD_SET_ROLE_ASSIGNMENT: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s is already silenced.", nick); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s is already silenced.", nick); return; } case TOX_ERR_GROUP_MOD_SET_ROLE_SELF: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You cannot silence yourself."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "You cannot silence yourself."); return; } case TOX_ERR_GROUP_MOD_SET_ROLE_PEER_NOT_FOUND: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "The specified nick or public key is invalid."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "The specified nick or public key is invalid."); return; } default: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to silence %s (error %d).", nick, err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to silence %s (error %d).", nick, err); return; } } @@ -767,21 +807,22 @@ void cmd_unsilence(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); return; } const char *nick = argv[1]; uint32_t target_peer_id; - if (group_get_peer_id_of_identifier(self, nick, &target_peer_id) != 0) { + if (group_get_peer_id_of_identifier(self, c_config, nick, &target_peer_id) != 0) { return; } if (tox_group_peer_get_role(tox, self->num, target_peer_id, NULL) != TOX_GROUP_ROLE_OBSERVER) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s is not silenced.", nick); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s is not silenced.", nick); return; } @@ -789,7 +830,7 @@ void cmd_unsilence(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char uint32_t self_peer_id = tox_group_self_get_peer_id(tox, self->num, &s_err); if (s_err != TOX_ERR_GROUP_SELF_QUERY_OK) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch self peer_id."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch self peer_id."); return; } @@ -803,27 +844,28 @@ void cmd_unsilence(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char } case TOX_ERR_GROUP_MOD_SET_ROLE_PERMISSIONS: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You do not have permission to unsilence %s.", nick); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "You do not have permission to unsilence %s.", + nick); return; } case TOX_ERR_GROUP_MOD_SET_ROLE_ASSIGNMENT: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s is not silenced.", nick); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s is not silenced.", nick); return; } case TOX_ERR_GROUP_MOD_SET_ROLE_SELF: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You cannot unsilence yourself."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "You cannot unsilence yourself."); return; } case TOX_ERR_GROUP_MOD_SET_ROLE_PEER_NOT_FOUND: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "The specified nick or public key is invalid."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "The specified nick or public key is invalid."); return; } default: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to unsilence %s (error %d).", nick, err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to unsilence %s (error %d).", nick, err); return; } } @@ -837,14 +879,16 @@ void cmd_rejoin(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (* return; } + const Client_Config *c_config = toxic->c_config; + Tox_Err_Group_Reconnect err; if (!tox_group_reconnect(toxic->tox, self->num, &err)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to rejoin group (error %d).", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to rejoin group (error %d).", err); return; } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Reconnecting to group..."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Reconnecting to group..."); groupchat_rejoin(self, toxic); } @@ -858,13 +902,15 @@ void cmd_set_topic(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (argc < 1) { Tox_Err_Group_State_Queries err; size_t tlen = tox_group_get_topic_size(tox, self->num, &err); if (err != TOX_ERR_GROUP_STATE_QUERIES_OK) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve topic length (error %d).", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve topic length (error %d).", + err); return; } @@ -872,14 +918,14 @@ void cmd_set_topic(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char char cur_topic[TOX_GROUP_MAX_TOPIC_LENGTH + 1]; if (!tox_group_get_topic(tox, self->num, (uint8_t *) cur_topic, &err)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve topic (error %d).", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve topic (error %d).", err); return; } cur_topic[tlen] = 0; - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Topic is set to: %s", cur_topic); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Topic is set to: %s", cur_topic); } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Topic is not set."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Topic is not set."); } return; @@ -897,22 +943,23 @@ void cmd_set_topic(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char } case TOX_ERR_GROUP_TOPIC_SET_TOO_LONG: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Topic length must not exceed %d.", TOX_GROUP_MAX_TOPIC_LENGTH); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Topic length must not exceed %d.", + TOX_GROUP_MAX_TOPIC_LENGTH); return; } case TOX_ERR_GROUP_TOPIC_SET_PERMISSIONS: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You do not have permission to set the topic."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "You do not have permission to set the topic."); return; } case TOX_ERR_GROUP_TOPIC_SET_DISCONNECTED: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You are disconnected from the group."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "You are disconnected from the group."); return; } default: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to set the topic (error %d).", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to set the topic (error %d).", err); return; } } @@ -920,11 +967,11 @@ void cmd_set_topic(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char char self_nick[TOX_MAX_NAME_LENGTH + 1]; get_group_self_nick_truncate(tox, self_nick, self->num); - line_info_add(self, true, NULL, NULL, SYS_MSG, 1, MAGENTA, "-!- You set the topic to: %s", topic); + line_info_add(self, c_config, true, NULL, NULL, SYS_MSG, 1, MAGENTA, "-!- You set the topic to: %s", topic); char tmp_event[MAX_STR_SIZE]; snprintf(tmp_event, sizeof(tmp_event), "set topic to %s", topic); - write_to_log(tmp_event, self_nick, self->chatwin->log, true); + write_to_log(self->chatwin->log, c_config, tmp_event, self_nick, true); } void cmd_unignore(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) @@ -935,15 +982,17 @@ void cmd_unignore(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char return; } + const Client_Config *c_config = toxic->c_config; + if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); return; } const char *nick = argv[1]; uint32_t peer_id; - if (group_get_peer_id_of_identifier(self, nick, &peer_id) != 0) { + if (group_get_peer_id_of_identifier(self, c_config, nick, &peer_id) != 0) { return; } @@ -956,22 +1005,23 @@ void cmd_unignore(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char } case TOX_ERR_GROUP_SET_IGNORE_SELF: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "You cannot unignore yourself."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "You cannot unignore yourself."); return; } case TOX_ERR_GROUP_SET_IGNORE_PEER_NOT_FOUND: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "The specified nick or public key is invalid."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "The specified nick or public key is invalid."); return; } default: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to toggle ignore on %s (error %d).", nick, err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to toggle ignore on %s (error %d).", + nick, err); return; } } - line_info_add(self, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- You are no longer ignoring %s", nick); + line_info_add(self, c_config, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- You are no longer ignoring %s", nick); group_toggle_peer_ignore(self->num, peer_id, false); } @@ -985,16 +1035,17 @@ void cmd_whois(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*a } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (argc < 1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Peer name or public key must be specified."); return; } GroupChat *chat = get_groupchat(self->num); if (chat == NULL) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch GroupChat object."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch GroupChat object."); return; } @@ -1085,15 +1136,15 @@ void cmd_whois(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*a #endif // TOX_EXPERIMENTAL - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Public key: %s", pk_string); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Name: %s", peer->name); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Role: %s", role_str); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Status: %s", status_str); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Connection: %s", connection_type_str); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Public key: %s", pk_string); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Name: %s", peer->name); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Role: %s", role_str); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Status: %s", status_str); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Connection: %s", connection_type_str); #ifdef TOX_EXPERIMENTAL - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "IP Address: %s", ip_addr); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "IP Address: %s", ip_addr); #endif - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Last active: %s", last_seen_str); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Last active: %s", last_seen_str); } } diff --git a/src/groupchats.c b/src/groupchats.c index 5e0dec419..86b87795f 100644 --- a/src/groupchats.c +++ b/src/groupchats.c @@ -64,7 +64,6 @@ extern char *DATA_FILE; static int max_groupchat_index = 0; -extern struct user_settings *user_settings; extern struct Winthread Winthread; #define GROUP_SIDEBAR_OFFSET 3 /* Offset for the peer number box at the top of the statusbar */ @@ -241,10 +240,11 @@ static void clear_peer(GroupPeer *peer) void groupchat_rejoin(ToxWindow *self, Toxic *toxic) { + const Client_Config *c_config = toxic->c_config; GroupChat *chat = get_groupchat(self->num); - if (!chat) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch GroupChat object."); + if (chat == NULL) { + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch GroupChat object."); return; } @@ -252,7 +252,8 @@ void groupchat_rejoin(ToxWindow *self, Toxic *toxic) const uint32_t self_peer_id = tox_group_self_get_peer_id(toxic->tox, self->num, &s_err); if (s_err != TOX_ERR_GROUP_SELF_QUERY_OK) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch self peer_id in groupchat_rejoin()"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Failed to fetch self peer_id in groupchat_rejoin()"); return; } @@ -267,7 +268,7 @@ void groupchat_rejoin(ToxWindow *self, Toxic *toxic) groupchat_onGroupPeerJoin(self, toxic, self->num, self_peer_id); } -static void kill_groupchat_window(ToxWindow *self) +static void kill_groupchat_window(ToxWindow *self, const Client_Config *c_config) { if (self == NULL) { return; @@ -287,7 +288,7 @@ static void kill_groupchat_window(ToxWindow *self) free(self->help); kill_notifs(self->active_box); - del_window(self); + del_window(self, c_config); } /* Closes groupchat window and cleans up. */ @@ -295,7 +296,7 @@ static void close_groupchat(ToxWindow *self, Toxic *toxic, uint32_t groupnumber) { GroupChat *chat = get_groupchat(groupnumber); - if (!chat) { + if (chat == NULL) { return; } @@ -318,7 +319,7 @@ static void close_groupchat(ToxWindow *self, Toxic *toxic, uint32_t groupnumber) } max_groupchat_index = i; - kill_groupchat_window(self); + kill_groupchat_window(self, toxic->c_config); } void exit_groupchat(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, const char *partmessage, size_t length) @@ -376,10 +377,11 @@ static void init_groupchat_log(ToxWindow *self, Toxic *toxic, uint32_t groupnumb } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; GroupChat *chat = get_groupchat(groupnumber); - if (!chat) { + if (chat == NULL) { return; } @@ -393,22 +395,23 @@ static void init_groupchat_log(ToxWindow *self, Toxic *toxic, uint32_t groupnumb Tox_Err_Group_State_Queries err; if (!tox_group_get_chat_id(tox, groupnumber, (uint8_t *)chat_id, &err)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch chat id. Logging disabled. (error: %d)", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Failed to fetch chat id. Logging disabled. (error: %d)", err); return; } - if (log_init(ctx->log, chat->group_name, my_id, chat_id, LOG_TYPE_CHAT) != 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Warning: Log failed to initialize."); + if (log_init(ctx->log, c_config, chat->group_name, my_id, chat_id, LOG_TYPE_CHAT) != 0) { + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Warning: Log failed to initialize."); return; } - if (load_chat_history(self, ctx->log) != 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to load chat history."); + if (load_chat_history(ctx->log, self, c_config) != 0) { + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to load chat history."); } - if (user_settings->autolog == AUTOLOG_ON) { + if (c_config->autolog == AUTOLOG_ON) { if (!enable_groupchat_log(groupnumber)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to enable chat log."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to enable chat log."); } } } @@ -469,7 +472,7 @@ int init_groupchat_win(Toxic *toxic, uint32_t groupnumber, const char *groupname } } - kill_groupchat_window(self); + kill_groupchat_window(self, toxic->c_config); return -1; } @@ -481,6 +484,7 @@ void set_nick_this_group(ToxWindow *self, Toxic *toxic, const char *new_nick, si } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; char old_nick[TOX_MAX_NAME_LENGTH + 1]; size_t old_length = get_group_self_nick_truncate(tox, old_nick, self->num); @@ -491,7 +495,7 @@ void set_nick_this_group(ToxWindow *self, Toxic *toxic, const char *new_nick, si GroupChat *chat = get_groupchat(self->num); if (chat == NULL) { - line_info_add(self, false, NULL, 0, SYS_MSG, 0, RED, "-!- Failed to set nick: invalid groupnumber"); + line_info_add(self, c_config, false, NULL, 0, SYS_MSG, 0, RED, "-!- Failed to set nick: invalid groupnumber"); return; } @@ -503,7 +507,7 @@ void set_nick_this_group(ToxWindow *self, Toxic *toxic, const char *new_nick, si default: { if (chat->time_connected > 0) { - line_info_add(self, false, NULL, 0, SYS_MSG, 0, RED, "-!- Failed to set nick (error %d).", err); + line_info_add(self, c_config, false, NULL, 0, SYS_MSG, 0, RED, "-!- Failed to set nick (error %d).", err); } break; @@ -535,7 +539,7 @@ void set_nick_this_group(ToxWindow *self, Toxic *toxic, const char *new_nick, si /* default: { */ /* if (groupchats[i].time_connected > 0) { */ -/* line_info_add(self, false, NULL, 0, SYS_MSG, 0, RED, "-!- Failed to set nick (error %d).", err); */ +/* line_info_add(self, c_config, false, NULL, 0, SYS_MSG, 0, RED, "-!- Failed to set nick (error %d).", err); */ /* } */ /* break; */ @@ -551,7 +555,7 @@ void set_status_all_groups(Toxic *toxic, uint8_t status) if (groupchats[i].active) { ToxWindow *self = get_window_ptr(groupchats[i].chatwin); - if (!self) { + if (self == NULL) { continue; } @@ -559,7 +563,8 @@ void set_status_all_groups(Toxic *toxic, uint8_t status) const uint32_t self_peer_id = tox_group_self_get_peer_id(toxic->tox, self->num, &s_err); if (s_err != TOX_ERR_GROUP_SELF_QUERY_OK) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to fetch self peer_id."); + line_info_add(self, toxic->c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Failed to fetch self peer_id."); continue; } @@ -702,7 +707,8 @@ int group_get_public_key_peer_id(uint32_t groupnumber, const char *public_key, u * Return -1 if the identifier does not correspond with a peer in the group, or if * the identifier is a nick which is being used by multiple peers. */ -int group_get_peer_id_of_identifier(ToxWindow *self, const char *identifier, uint32_t *peer_id) +int group_get_peer_id_of_identifier(ToxWindow *self, const Client_Config *c_config, + const char *identifier, uint32_t *peer_id) { *peer_id = (uint32_t) -1; @@ -718,21 +724,21 @@ int group_get_peer_id_of_identifier(ToxWindow *self, const char *identifier, uin } case -1: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name or public key."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid peer name or public key."); return -1; } case -2: { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "More than one peer is using this name; specify the target's public key."); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Use the /whois or /list command to determine the key."); *peer_id = (uint32_t) -1; return -1; } default: - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Unspecified error."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Unspecified error."); return -1; } } @@ -977,6 +983,8 @@ void redraw_groupchat_win(ToxWindow *self) static void group_onAction(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, uint32_t peer_id, const char *action, size_t len) { + const Client_Config *c_config = toxic->c_config; + ChatContext *ctx = self->chatwin; char nick[TOX_MAX_NAME_LENGTH + 1]; @@ -987,18 +995,18 @@ static void group_onAction(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, if (strcasestr(action, self_nick)) { if (self->active_box != -1) { - box_notify2(self, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | user_settings->bell_on_message, self->active_box, - "* %s %s", nick, action); + box_notify2(self, c_config, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | + c_config->bell_on_message, self->active_box, "* %s %s", nick, action); } else { - box_notify(self, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | user_settings->bell_on_message, &self->active_box, - self->name, "* %s %s", nick, action); + box_notify(self, c_config, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | + c_config->bell_on_message, &self->active_box, self->name, "* %s %s", nick, action); } } else { - sound_notify(self, silent, NT_WNDALERT_1, NULL); + sound_notify(self, c_config, silent, NT_WNDALERT_1, NULL); } - line_info_add(self, true, nick, NULL, IN_ACTION, 0, 0, "%s", action); - write_to_log(action, nick, ctx->log, true); + line_info_add(self, c_config, true, nick, NULL, IN_ACTION, 0, 0, "%s", action); + write_to_log(ctx->log, c_config, action, nick, true); } static void groupchat_onGroupMessage(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, uint32_t peer_id, @@ -1009,6 +1017,7 @@ static void groupchat_onGroupMessage(ToxWindow *self, Toxic *toxic, uint32_t gro } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (self->num != groupnumber || !get_groupchat(groupnumber)) { return; @@ -1033,21 +1042,21 @@ static void groupchat_onGroupMessage(ToxWindow *self, Toxic *toxic, uint32_t gro /* Only play sound if mentioned by someone else */ if (strcasestr(msg, self_nick) && strcmp(self_nick, nick)) { - sound_notify(self, generic_message, NT_WNDALERT_0 | user_settings->bell_on_message, NULL); + sound_notify(self, c_config, generic_message, NT_WNDALERT_0 | c_config->bell_on_message, NULL); if (self->active_box != -1) { - box_silent_notify2(self, NT_NOFOCUS, self->active_box, "%s %s", nick, msg); + box_silent_notify2(self, c_config, NT_NOFOCUS, self->active_box, "%s %s", nick, msg); } else { - box_silent_notify(self, NT_NOFOCUS, &self->active_box, self->name, "%s %s", nick, msg); + box_silent_notify(self, c_config, NT_NOFOCUS, &self->active_box, self->name, "%s %s", nick, msg); } nick_clr = RED; } else { - sound_notify(self, silent, NT_WNDALERT_1, NULL); + sound_notify(self, c_config, silent, NT_WNDALERT_1, NULL); } - line_info_add(self, true, nick, NULL, IN_MSG, 0, nick_clr, "%s", msg); - write_to_log(msg, nick, ctx->log, false); + line_info_add(self, c_config, true, nick, NULL, IN_MSG, 0, nick_clr, "%s", msg); + write_to_log(ctx->log, c_config, msg, nick, false); } static void groupchat_onGroupPrivateMessage(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, uint32_t peer_id, @@ -1058,6 +1067,7 @@ static void groupchat_onGroupPrivateMessage(ToxWindow *self, Toxic *toxic, uint3 } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (self->num != groupnumber || !get_groupchat(groupnumber)) { return; @@ -1070,14 +1080,14 @@ static void groupchat_onGroupPrivateMessage(ToxWindow *self, Toxic *toxic, uint3 char nick[TOX_MAX_NAME_LENGTH + 1]; get_group_nick_truncate(tox, nick, peer_id, groupnumber); - line_info_add(self, true, nick, NULL, IN_PRVT_MSG, 0, MAGENTA, "%s", msg); - write_to_log(msg, nick, ctx->log, false); + line_info_add(self, c_config, true, nick, NULL, IN_PRVT_MSG, 0, MAGENTA, "%s", msg); + write_to_log(ctx->log, c_config, msg, nick, false); if (self->active_box != -1) { - box_notify2(self, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | user_settings->bell_on_message, + box_notify2(self, c_config, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_message, self->active_box, "%s %s", nick, msg); } else { - box_notify(self, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | user_settings->bell_on_message, + box_notify(self, c_config, generic_message, NT_WNDALERT_0 | NT_NOFOCUS | c_config->bell_on_message, &self->active_box, self->name, "%s %s", nick, msg); } } @@ -1090,6 +1100,7 @@ static void groupchat_onGroupTopicChange(ToxWindow *self, Toxic *toxic, uint32_t } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; @@ -1101,44 +1112,42 @@ static void groupchat_onGroupTopicChange(ToxWindow *self, Toxic *toxic, uint32_t char nick[TOX_MAX_NAME_LENGTH + 1]; get_group_nick_truncate(tox, nick, peer_id, groupnumber); - line_info_add(self, true, NULL, NULL, SYS_MSG, 1, MAGENTA, "-!- %s set the topic to: %s", nick, topic); + line_info_add(self, c_config, true, NULL, NULL, SYS_MSG, 1, MAGENTA, "-!- %s set the topic to: %s", nick, topic); char tmp_event[MAX_STR_SIZE]; snprintf(tmp_event, sizeof(tmp_event), " set the topic to %s", topic); - write_to_log(tmp_event, nick, ctx->log, true); + write_to_log(ctx->log, c_config, tmp_event, nick, true); } static void groupchat_onGroupPeerLimit(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, uint32_t peer_limit) { - UNUSED_VAR(toxic); - - if (self == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; if (self->num != groupnumber || !get_groupchat(groupnumber)) { return; } - line_info_add(self, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- The group founder has set the peer limit to %d", - peer_limit); + line_info_add(self, c_config, true, NULL, NULL, SYS_MSG, 1, BLUE, + "-!- The group founder has set the peer limit to %d", peer_limit); char tmp_event[MAX_STR_SIZE]; snprintf(tmp_event, sizeof(tmp_event), " set the peer limit to %u", peer_limit); - write_to_log(tmp_event, "The founder", ctx->log, true); + write_to_log(ctx->log, c_config, tmp_event, "The founder", true); } static void groupchat_onGroupPrivacyState(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, Tox_Group_Privacy_State state) { - UNUSED_VAR(toxic); - - if (self == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; if (self->num != groupnumber || !get_groupchat(groupnumber)) { @@ -1147,22 +1156,21 @@ static void groupchat_onGroupPrivacyState(ToxWindow *self, Toxic *toxic, uint32_ const char *state_str = state == TOX_GROUP_PRIVACY_STATE_PUBLIC ? "public" : "private"; - line_info_add(self, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- The group founder has set the group to %s.", - state_str); + line_info_add(self, c_config, true, NULL, NULL, SYS_MSG, 1, BLUE, + "-!- The group founder has set the group to %s.", state_str); char tmp_event[MAX_STR_SIZE]; snprintf(tmp_event, sizeof(tmp_event), " set the group to %s.", state_str); - write_to_log(tmp_event, "The founder", ctx->log, true); + write_to_log(ctx->log, c_config, tmp_event, "The founder", true); } void groupchat_onGroupVoiceState(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, Tox_Group_Voice_State voice_state) { - UNUSED_VAR(toxic); - - if (self == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; if (self->num != groupnumber || !get_groupchat(groupnumber)) { @@ -1173,20 +1181,20 @@ void groupchat_onGroupVoiceState(ToxWindow *self, Toxic *toxic, uint32_t groupnu switch (voice_state) { case TOX_GROUP_VOICE_STATE_ALL: { - line_info_add(self, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- Voice: Everyone may speak"); + line_info_add(self, c_config, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- Voice: Everyone may speak"); snprintf(tmp_event, sizeof(tmp_event), " set the voice state to ALL."); break; } case TOX_GROUP_VOICE_STATE_MODERATOR: { - line_info_add(self, true, NULL, NULL, SYS_MSG, 1, BLUE, + line_info_add(self, c_config, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- Voice: Only moderators and the founder may speak"); snprintf(tmp_event, sizeof(tmp_event), " set the voice state to MODERATOR."); break; } case TOX_GROUP_VOICE_STATE_FOUNDER: { - line_info_add(self, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- Voice: Only the founder may speak."); + line_info_add(self, c_config, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- Voice: Only the founder may speak."); snprintf(tmp_event, sizeof(tmp_event), " set the voice state to FOUNDER."); break; } @@ -1195,18 +1203,17 @@ void groupchat_onGroupVoiceState(ToxWindow *self, Toxic *toxic, uint32_t groupnu return; } - write_to_log(tmp_event, "The founder", ctx->log, true); + write_to_log(ctx->log, c_config, tmp_event, "The founder", true); } static void groupchat_onGroupTopicLock(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, Tox_Group_Topic_Lock topic_lock) { - UNUSED_VAR(toxic); - - if (self == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; if (self->num != groupnumber || !get_groupchat(groupnumber)) { @@ -1215,22 +1222,22 @@ static void groupchat_onGroupTopicLock(ToxWindow *self, Toxic *toxic, uint32_t g const char *tlock_str = topic_lock == TOX_GROUP_TOPIC_LOCK_ENABLED ? "locked" : "unlocked"; - line_info_add(self, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- The group founder has %s the topic.", tlock_str); + line_info_add(self, c_config, true, NULL, NULL, SYS_MSG, 1, BLUE, + "-!- The group founder has %s the topic.", tlock_str); char tmp_event[MAX_STR_SIZE]; snprintf(tmp_event, sizeof(tmp_event), " %s the topic.", tlock_str); - write_to_log(tmp_event, "The founder", ctx->log, true); + write_to_log(ctx->log, c_config, tmp_event, "The founder", true); } static void groupchat_onGroupPassword(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, const char *password, size_t length) { - UNUSED_VAR(toxic); - - if (self == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; if (self->num != groupnumber || !get_groupchat(groupnumber)) { @@ -1238,17 +1245,19 @@ static void groupchat_onGroupPassword(ToxWindow *self, Toxic *toxic, uint32_t gr } if (length > 0) { - line_info_add(self, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- The group founder has password protected the group."); + line_info_add(self, c_config, true, NULL, NULL, SYS_MSG, 1, BLUE, + "-!- The group founder has password protected the group."); char tmp_event[MAX_STR_SIZE]; snprintf(tmp_event, sizeof(tmp_event), " set a new password."); - write_to_log(tmp_event, "The founder", ctx->log, true); + write_to_log(ctx->log, c_config, tmp_event, "The founder", true); } else { - line_info_add(self, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- The group founder has removed password protection."); + line_info_add(self, c_config, true, NULL, NULL, SYS_MSG, 1, BLUE, + "-!- The group founder has removed password protection."); char tmp_event[MAX_STR_SIZE]; snprintf(tmp_event, sizeof(tmp_event), " removed password protection."); - write_to_log(tmp_event, "The founder", ctx->log, true); + write_to_log(ctx->log, c_config, tmp_event, "The founder", true); } } @@ -1261,7 +1270,7 @@ static int realloc_peer_list(uint32_t groupnumber, uint32_t n) { GroupChat *chat = get_groupchat(groupnumber); - if (!chat) { + if (chat == NULL) { return -1; } @@ -1289,6 +1298,8 @@ static void groupchat_onGroupPeerJoin(ToxWindow *self, Toxic *toxic, uint32_t gr } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; + ChatContext *ctx = self->chatwin; if (self->num != groupnumber) { return; @@ -1296,7 +1307,7 @@ static void groupchat_onGroupPeerJoin(ToxWindow *self, Toxic *toxic, uint32_t gr GroupChat *chat = get_groupchat(groupnumber); - if (!chat) { + if (chat == NULL) { return; } @@ -1336,11 +1347,11 @@ static void groupchat_onGroupPeerJoin(ToxWindow *self, Toxic *toxic, uint32_t gr /* ignore join messages when we first connect to the group */ if (timed_out(chat->time_connected, 60) - && user_settings->show_group_connection_msg == SHOW_GROUP_CONNECTION_MSG_ON) { - line_info_add(self, true, peer->name, NULL, CONNECTION, 0, GREEN, "has joined the room"); + && c_config->show_group_connection_msg == SHOW_GROUP_CONNECTION_MSG_ON) { + line_info_add(self, c_config, true, peer->name, NULL, CONNECTION, 0, GREEN, "has joined the room"); - write_to_log("has joined the room", peer->name, self->chatwin->log, true); - sound_notify(self, silent, NT_WNDALERT_2, NULL); + write_to_log(ctx->log, c_config, "has joined the room", peer->name, true); + sound_notify(self, c_config, silent, NT_WNDALERT_2, NULL); } group_update_name_list(groupnumber); @@ -1355,37 +1366,39 @@ void groupchat_onGroupPeerExit(ToxWindow *self, Toxic *toxic, uint32_t groupnumb { UNUSED_VAR(name_len); UNUSED_VAR(length); - UNUSED_VAR(toxic); - if (self == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; + ChatContext *ctx = self->chatwin; + if (self->num != groupnumber) { return; } GroupChat *chat = get_groupchat(groupnumber); - if (!chat) { + if (chat == NULL) { return; } if (exit_type != TOX_GROUP_EXIT_TYPE_SELF_DISCONNECTED - && user_settings->show_group_connection_msg == SHOW_GROUP_CONNECTION_MSG_ON) { + && c_config->show_group_connection_msg == SHOW_GROUP_CONNECTION_MSG_ON) { char log_str[TOX_MAX_NAME_LENGTH + MAX_STR_SIZE]; if (length > 0) { - line_info_add(self, true, name, NULL, DISCONNECTION, 0, RED, "[Quit]: %s", part_message); + line_info_add(self, c_config, true, name, NULL, DISCONNECTION, 0, RED, "[Quit]: %s", part_message); snprintf(log_str, sizeof(log_str), "has left the room (%s)", part_message); - write_to_log(log_str, name, self->chatwin->log, true); - sound_notify(self, silent, NT_WNDALERT_2, NULL); + write_to_log(ctx->log, c_config, log_str, name, true); + sound_notify(self, c_config, silent, NT_WNDALERT_2, NULL); } else { const char *exit_string = get_group_exit_string(exit_type); - line_info_add(self, true, name, NULL, DISCONNECTION, 0, RED, "[%s]", exit_string); + line_info_add(self, c_config, true, name, NULL, DISCONNECTION, 0, RED, "[%s]", exit_string); snprintf(log_str, sizeof(log_str), "[%s]", exit_string); - write_to_log(log_str, name, self->chatwin->log, true); - sound_notify(self, silent, NT_WNDALERT_2, NULL); + write_to_log(ctx->log, c_config, log_str, name, true); + sound_notify(self, c_config, silent, NT_WNDALERT_2, NULL); } } @@ -1423,6 +1436,7 @@ static void groupchat_set_group_name(ToxWindow *self, Toxic *toxic, uint32_t gro } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; GroupChat *chat = get_groupchat(groupnumber); @@ -1434,14 +1448,15 @@ static void groupchat_set_group_name(ToxWindow *self, Toxic *toxic, uint32_t gro size_t len = tox_group_get_name_size(tox, groupnumber, &err); if (err != TOX_ERR_GROUP_STATE_QUERIES_OK) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve group name length (error %d)", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Failed to retrieve group name length (error %d)", err); return; } char tmp_groupname[TOX_GROUP_MAX_GROUP_NAME_LENGTH + 1]; if (!tox_group_get_name(tox, groupnumber, (uint8_t *) tmp_groupname, &err)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve group name (error %d)", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve group name (error %d)", err); return; } @@ -1461,6 +1476,7 @@ static void groupchat_onGroupSelfJoin(ToxWindow *self, Toxic *toxic, uint32_t gr } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (self->num != groupnumber) { return; @@ -1480,7 +1496,8 @@ static void groupchat_onGroupSelfJoin(ToxWindow *self, Toxic *toxic, uint32_t gr const size_t topic_length = tox_group_get_topic_size(tox, groupnumber, &err); if (err != TOX_ERR_GROUP_STATE_QUERIES_OK) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve group topic length (error %d)", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Failed to retrieve group topic length (error %d)", err); return; } @@ -1488,11 +1505,12 @@ static void groupchat_onGroupSelfJoin(ToxWindow *self, Toxic *toxic, uint32_t gr topic[topic_length] = 0; if (err != TOX_ERR_GROUP_STATE_QUERIES_OK) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve group topic (error %d)", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to retrieve group topic (error %d)", + err); return; } - line_info_add(self, true, NULL, NULL, SYS_MSG, 1, MAGENTA, "-!- Topic set to: %s", topic); + line_info_add(self, c_config, true, NULL, NULL, SYS_MSG, 1, MAGENTA, "-!- Topic set to: %s", topic); if (chat->group_name_length == 0) { groupchat_set_group_name(self, toxic, groupnumber); @@ -1525,9 +1543,7 @@ static void groupchat_onGroupSelfJoin(ToxWindow *self, Toxic *toxic, uint32_t gr static void groupchat_onGroupRejected(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, Tox_Group_Join_Fail type) { - UNUSED_VAR(toxic); - - if (self == NULL) { + if (toxic == NULL || self == NULL) { return; } @@ -1551,7 +1567,7 @@ static void groupchat_onGroupRejected(ToxWindow *self, Toxic *toxic, uint32_t gr break; } - line_info_add(self, true, NULL, NULL, SYS_MSG, 0, RED, "-!- %s", msg); + line_info_add(self, toxic->c_config, true, NULL, NULL, SYS_MSG, 0, RED, "-!- %s", msg); } static void groupchat_update_roles(Tox *tox, uint32_t groupnumber) @@ -1586,6 +1602,7 @@ void groupchat_onGroupModeration(ToxWindow *self, Toxic *toxic, uint32_t groupnu } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; if (self->num != groupnumber) { return; @@ -1614,24 +1631,28 @@ void groupchat_onGroupModeration(ToxWindow *self, Toxic *toxic, uint32_t groupnu switch (type) { case TOX_GROUP_MOD_EVENT_KICK: - line_info_add(self, true, NULL, NULL, SYS_MSG, 1, RED, "-!- %s has been kicked by %s", tgt_name, src_name); + line_info_add(self, c_config, true, NULL, NULL, SYS_MSG, 1, RED, "-!- %s has been kicked by %s", tgt_name, + src_name); break; case TOX_GROUP_MOD_EVENT_OBSERVER: chat->peer_list[tgt_index].role = TOX_GROUP_ROLE_OBSERVER; - line_info_add(self, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- %s has set %s's role to observer", src_name, tgt_name); + line_info_add(self, c_config, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- %s has set %s's role to observer", + src_name, tgt_name); sort_peerlist(groupnumber); break; case TOX_GROUP_MOD_EVENT_USER: chat->peer_list[tgt_index].role = TOX_GROUP_ROLE_USER; - line_info_add(self, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- %s has set %s's role to user", src_name, tgt_name); + line_info_add(self, c_config, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- %s has set %s's role to user", src_name, + tgt_name); sort_peerlist(groupnumber); break; case TOX_GROUP_MOD_EVENT_MODERATOR: chat->peer_list[tgt_index].role = TOX_GROUP_ROLE_MODERATOR; - line_info_add(self, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- %s has set %s's role to moderator", src_name, + line_info_add(self, c_config, true, NULL, NULL, SYS_MSG, 1, BLUE, "-!- %s has set %s's role to moderator", + src_name, tgt_name); sort_peerlist(groupnumber); break; @@ -1680,7 +1701,8 @@ static void groupchat_onGroupSelfNickChange(ToxWindow *self, Toxic *toxic, uint3 chat->peer_list[peer_index].name[length] = 0; chat->peer_list[peer_index].name_length = length; - line_info_add(self, true, old_nick, chat->peer_list[peer_index].name, NAME_CHANGE, 0, MAGENTA, " is now known as "); + line_info_add(self, toxic->c_config, true, old_nick, chat->peer_list[peer_index].name, NAME_CHANGE, 0, + MAGENTA, " is now known as "); groupchat_update_last_seen(groupnumber, peer_id); group_update_name_list(groupnumber); @@ -1689,9 +1711,7 @@ static void groupchat_onGroupSelfNickChange(ToxWindow *self, Toxic *toxic, uint3 static void groupchat_onGroupNickChange(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, uint32_t peer_id, const char *new_nick, size_t length) { - UNUSED_VAR(toxic); - - if (self == NULL) { + if (toxic == NULL || self == NULL) { return; } @@ -1720,7 +1740,8 @@ static void groupchat_onGroupNickChange(ToxWindow *self, Toxic *toxic, uint32_t peer->name[length] = 0; peer->name_length = length; - line_info_add(self, true, peer->prev_name, peer->name, NAME_CHANGE, 0, MAGENTA, " is now known as "); + line_info_add(self, toxic->c_config, true, peer->prev_name, peer->name, NAME_CHANGE, 0, MAGENTA, + " is now known as "); snprintf(peer->prev_name, sizeof(peer->prev_name), "%s", peer->name); @@ -1764,6 +1785,7 @@ static void send_group_message(ToxWindow *self, Toxic *toxic, uint32_t groupnumb } Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; @@ -1780,12 +1802,12 @@ static void send_group_message(ToxWindow *self, Toxic *toxic, uint32_t groupnumb const Tox_Group_Role role = tox_group_self_get_role(tox, groupnumber, NULL); if (role == TOX_GROUP_ROLE_OBSERVER) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, " * You are silenced."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, " * You are silenced."); } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, " * You do not have voice."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, " * You do not have voice."); } } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, " * Failed to send message (Error %d).", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, " * Failed to send message (Error %d).", err); } return; @@ -1795,28 +1817,29 @@ static void send_group_message(ToxWindow *self, Toxic *toxic, uint32_t groupnumb get_group_self_nick_truncate(tox, self_nick, groupnumber); if (type == TOX_MESSAGE_TYPE_NORMAL) { - line_info_add(self, true, self_nick, NULL, OUT_MSG_READ, 0, 0, "%s", msg); - write_to_log(msg, self_nick, ctx->log, false); + line_info_add(self, c_config, true, self_nick, NULL, OUT_MSG_READ, 0, 0, "%s", msg); + write_to_log(ctx->log, c_config, msg, self_nick, false); } else if (type == TOX_MESSAGE_TYPE_ACTION) { - line_info_add(self, true, self_nick, NULL, OUT_ACTION_READ, 0, 0, "%s", msg); - write_to_log(msg, self_nick, ctx->log, true); + line_info_add(self, c_config, true, self_nick, NULL, OUT_ACTION_READ, 0, 0, "%s", msg); + write_to_log(ctx->log, c_config, msg, self_nick, true); } } static void send_group_prvt_message(ToxWindow *self, Toxic *toxic, uint32_t groupnumber, const char *data, size_t data_len) { + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; GroupChat *chat = get_groupchat(groupnumber); if (chat == NULL) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, "Failed to fetch GroupChat object."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, "Failed to fetch GroupChat object."); return; } if (data == NULL) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, "Invalid comand."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, "Invalid comand."); return; } @@ -1843,7 +1866,7 @@ static void send_group_prvt_message(ToxWindow *self, Toxic *toxic, uint32_t grou if (nick == NULL) { if (data_len < TOX_GROUP_PEER_PUBLIC_KEY_SIZE * 2) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid nick."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid nick."); return; } @@ -1853,14 +1876,14 @@ static void send_group_prvt_message(ToxWindow *self, Toxic *toxic, uint32_t grou uint32_t peer_id; - if (group_get_peer_id_of_identifier(self, nick, &peer_id) != 0) { + if (group_get_peer_id_of_identifier(self, c_config, nick, &peer_id) != 0) { return; } const int msg_len = ((int) data_len) - ((int) name_length) - 1; if (msg_len <= 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Message is empty."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Message is empty."); return; } @@ -1871,9 +1894,9 @@ static void send_group_prvt_message(ToxWindow *self, Toxic *toxic, uint32_t grou if (!tox_group_send_private_message(toxic->tox, groupnumber, peer_id, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) msg, msg_len, &err)) { if (err == TOX_ERR_GROUP_SEND_PRIVATE_MESSAGE_PERMISSIONS) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, " * You are silenced."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, " * You are silenced."); } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, " * Failed to send private message (%d)", err); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, " * Failed to send private message (%d)", err); } return; @@ -1882,8 +1905,8 @@ static void send_group_prvt_message(ToxWindow *self, Toxic *toxic, uint32_t grou char pm_nick[TOX_MAX_NAME_LENGTH + 3]; snprintf(pm_nick, sizeof(pm_nick), ">%s<", nick); - line_info_add(self, true, pm_nick, NULL, OUT_PRVT_MSG, 0, 0, "%s", msg); - write_to_log(msg, pm_nick, ctx->log, false); + line_info_add(self, c_config, true, pm_nick, NULL, OUT_PRVT_MSG, 0, 0, "%s", msg); + write_to_log(ctx->log, c_config, msg, pm_nick, false); } /* @@ -1895,6 +1918,7 @@ static bool groupchat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) return false; } + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; GroupChat *chat = get_groupchat(self->num); @@ -1923,15 +1947,15 @@ static bool groupchat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) } if (ltr || key == L'\n') { /* char is printable */ - input_new_char(self, key, x, x2); + input_new_char(self, c_config, key, x, x2); return true; } - if (line_info_onKey(self, key)) { + if (line_info_onKey(self, c_config, key)) { return true; } - if (input_handle(self, key, x, x2)) { + if (input_handle(self, c_config, key, x, x2)) { return true; } @@ -1945,11 +1969,11 @@ static bool groupchat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) /* TODO: make this not suck */ if (ctx->line[0] != L'/' || wcschr(ctx->line, L' ') != NULL) { - diff = complete_line(self, (const char **) chat->name_list, chat->num_peers); + diff = complete_line(self, toxic, (const char **) chat->name_list, chat->num_peers); } else if (wcsncmp(ctx->line, L"/avatar \"", wcslen(L"/avatar \"")) == 0) { diff = dir_match(self, toxic, ctx->line, L"/avatar"); } else { - diff = complete_line(self, group_cmd_list, sizeof(group_cmd_list) / sizeof(char *)); + diff = complete_line(self, toxic, group_cmd_list, sizeof(group_cmd_list) / sizeof(char *)); } if (diff != -1) { @@ -1958,10 +1982,10 @@ static bool groupchat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) ctx->start = wlen < x2 ? 0 : wlen - x2 + 1; } } else { - sound_notify(self, notif_error, 0, NULL); + sound_notify(self, c_config, notif_error, 0, NULL); } } else { - sound_notify(self, notif_error, 0, NULL); + sound_notify(self, c_config, notif_error, 0, NULL); } } else if (key == T_KEY_C_DOWN) { /* Scroll peerlist up and down one position */ input_ret = true; @@ -2005,7 +2029,8 @@ static bool groupchat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) if (part_length > 0) { exit_groupchat(self, toxic, self->num, part_message, part_length); } else { - exit_groupchat(self, toxic, self->num, user_settings->group_part_message, strlen(user_settings->group_part_message)); + exit_groupchat(self, toxic, self->num, c_config->group_part_message, + strlen(c_config->group_part_message)); } return true; @@ -2019,7 +2044,7 @@ static bool groupchat_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) } else if (line[0]) { send_group_message(self, toxic, self->num, line, TOX_MESSAGE_TYPE_NORMAL); } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, " * Failed to parse message."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, " * Failed to parse message."); } wclear(ctx->linewin); @@ -2056,7 +2081,7 @@ static void groupchat_onDraw(ToxWindow *self, Toxic *toxic) return; } - line_info_print(self); + line_info_print(self, toxic->c_config); pthread_mutex_unlock(&Winthread.lock); diff --git a/src/groupchats.h b/src/groupchats.h index ba23e2420..e13f61cb9 100644 --- a/src/groupchats.h +++ b/src/groupchats.h @@ -98,7 +98,8 @@ void groupchat_rejoin(ToxWindow *self, Toxic *toxic); * Return -1 if the identifier does not correspond with a peer in the group. * Return -2 if the identifier is a nick and the nick is in use by multiple peers. */ -int group_get_peer_id_of_identifier(ToxWindow *self, const char *identifier, uint32_t *peer_id); +int group_get_peer_id_of_identifier(ToxWindow *self, const Client_Config *c_config, const char *identifier, + uint32_t *peer_id); /* Gets the peer_id associated with `public_key`. * diff --git a/src/input.c b/src/input.c index 43ba75fa1..87d967be6 100644 --- a/src/input.c +++ b/src/input.c @@ -36,10 +36,8 @@ #include "toxic_strings.h" #include "windows.h" -extern struct user_settings *user_settings; - /* add a char to input field and buffer */ -void input_new_char(ToxWindow *self, wint_t key, int x, int mx_x) +void input_new_char(ToxWindow *self, const Client_Config *c_config, wint_t key, int x, int mx_x) { ChatContext *ctx = self->chatwin; @@ -51,12 +49,12 @@ void input_new_char(ToxWindow *self, wint_t key, int x, int mx_x) int cur_len = wcwidth(key); if (cur_len == -1) { - sound_notify(self, notif_error, 0, NULL); + sound_notify(self, c_config, notif_error, 0, NULL); return; } if (add_char_to_buf(ctx, key) == -1) { - sound_notify(self, notif_error, 0, NULL); + sound_notify(self, c_config, notif_error, 0, NULL); return; } @@ -67,12 +65,12 @@ void input_new_char(ToxWindow *self, wint_t key, int x, int mx_x) } /* delete a char via backspace key from input field and buffer */ -static void input_backspace(ToxWindow *self, int x, int mx_x) +static void input_backspace(ToxWindow *self, const Client_Config *c_config, int x, int mx_x) { ChatContext *ctx = self->chatwin; if (del_char_buf_bck(ctx) == -1) { - sound_notify(self, notif_error, 0, NULL); + sound_notify(self, c_config, notif_error, 0, NULL); return; } @@ -87,45 +85,45 @@ static void input_backspace(ToxWindow *self, int x, int mx_x) } /* delete a char via delete key from input field and buffer */ -static void input_delete(ToxWindow *self) +static void input_delete(ToxWindow *self, const Client_Config *c_config) { if (del_char_buf_frnt(self->chatwin) == -1) { - sound_notify(self, notif_error, 0, NULL); + sound_notify(self, c_config, notif_error, 0, NULL); } } /* delete last typed word */ -static void input_del_word(ToxWindow *self) +static void input_del_word(ToxWindow *self, const Client_Config *c_config) { ChatContext *ctx = self->chatwin; if (del_word_buf(ctx) == -1) { - sound_notify(self, notif_error, 0, NULL); + sound_notify(self, c_config, notif_error, 0, NULL); } } /* deletes entire line before cursor from input field and buffer */ -static void input_discard(ToxWindow *self) +static void input_discard(ToxWindow *self, const Client_Config *c_config) { if (discard_buf(self->chatwin) == -1) { - sound_notify(self, notif_error, 0, NULL); + sound_notify(self, c_config, notif_error, 0, NULL); } } /* deletes entire line after cursor from input field and buffer */ -static void input_kill(ChatContext *ctx) +static void input_kill(ChatContext *ctx, const Client_Config *c_config) { if (kill_buf(ctx) == -1) { - sound_notify(NULL, notif_error, NT_ALWAYS, NULL); + sound_notify(NULL, c_config, notif_error, NT_ALWAYS, NULL); } } -static void input_yank(ToxWindow *self, int x, int mx_x) +static void input_yank(ToxWindow *self, const Client_Config *c_config, int x, int mx_x) { ChatContext *ctx = self->chatwin; if (yank_buf(ctx) == -1) { - sound_notify(self, notif_error, 0, NULL); + sound_notify(self, c_config, notif_error, 0, NULL); return; } @@ -250,45 +248,45 @@ static void input_skip_right(ToxWindow *self, int x, int mx_x) } /* puts a line history item in input field and buffer */ -static void input_history(ToxWindow *self, wint_t key, int mx_x) +static void input_history(ToxWindow *self, const Client_Config *c_config, wint_t key, int mx_x) { ChatContext *ctx = self->chatwin; - fetch_hist_item(ctx, key); + fetch_hist_item(c_config, ctx, key); int wlen = MAX(0, wcswidth(ctx->line, sizeof(ctx->line) / sizeof(wchar_t))); ctx->start = wlen < mx_x ? 0 : wlen - mx_x + 1; } /* Handles non-printable input keys that behave the same for all types of chat windows. return true if key matches a function, false otherwise */ -bool input_handle(ToxWindow *self, wint_t key, int x, int mx_x) +bool input_handle(ToxWindow *self, const Client_Config *c_config, wint_t key, int x, int mx_x) { bool match = true; switch (key) { case 0x7f: case KEY_BACKSPACE: - input_backspace(self, x, mx_x); + input_backspace(self, c_config, x, mx_x); break; case KEY_DC: - input_delete(self); + input_delete(self, c_config); break; case T_KEY_DISCARD: - input_discard(self); + input_discard(self, c_config); break; case T_KEY_KILL: - input_kill(self->chatwin); + input_kill(self->chatwin, c_config); break; case T_KEY_C_Y: - input_yank(self, x, mx_x); + input_yank(self, c_config, x, mx_x); break; case T_KEY_C_W: - input_del_word(self); + input_del_word(self, c_config); break; case KEY_HOME: @@ -311,7 +309,7 @@ bool input_handle(ToxWindow *self, wint_t key, int x, int mx_x) case KEY_UP: case KEY_DOWN: - input_history(self, key, mx_x); + input_history(self, c_config, key, mx_x); break; case T_KEY_C_L: @@ -334,7 +332,7 @@ bool input_handle(ToxWindow *self, wint_t key, int x, int mx_x) /* TODO: this special case is ugly. maybe convert entire function to if/else and make them all customizable keys? */ if (!match) { - if (key == user_settings->key_toggle_peerlist) { + if (key == c_config->key_toggle_peerlist) { if (self->type == WINDOW_TYPE_CONFERENCE) { self->show_peerlist ^= 1; redraw_conference_win(self); @@ -344,7 +342,7 @@ bool input_handle(ToxWindow *self, wint_t key, int x, int mx_x) } match = true; - } else if (key == user_settings->key_toggle_pastemode) { + } else if (key == c_config->key_toggle_pastemode) { self->chatwin->pastemode ^= 1; match = true; } diff --git a/src/input.h b/src/input.h index 33f5e1b5c..3600f32d2 100644 --- a/src/input.h +++ b/src/input.h @@ -23,11 +23,13 @@ #ifndef INPUT_H #define INPUT_H +#include "settings.h" + /* add a char to input field and buffer for given chatcontext */ -void input_new_char(ToxWindow *self, wint_t key, int x, int mx_x); +void input_new_char(ToxWindow *self, const Client_Config *c_config, wint_t key, int x, int mx_x); /* Handles non-printable input keys that behave the same for all types of chat windows. return true if key matches a function, false otherwise */ -bool input_handle(ToxWindow *self, wint_t key, int x, int mx_x); +bool input_handle(ToxWindow *self, const Client_Config *c_config, wint_t key, int x, int mx_x); #endif /* INPUT_H */ diff --git a/src/line_info.c b/src/line_info.c index b40787c3a..c45c1bf9b 100644 --- a/src/line_info.c +++ b/src/line_info.c @@ -39,8 +39,6 @@ #include "toxic.h" #include "windows.h" -extern struct user_settings *user_settings; - void line_info_init(struct history *hst) { hst->line_root = calloc(1, sizeof(struct line_info)); @@ -244,10 +242,6 @@ static unsigned int newline_count(const wchar_t *s) */ static int print_wrap(WINDOW *win, struct line_info *line, int max_x, int max_y) { - int x; - int y; - UNUSED_VAR(y); - const wchar_t *msg = line->msg; uint16_t length = line->msg_width; uint16_t lines = 0; @@ -259,6 +253,10 @@ static int print_wrap(WINDOW *win, struct line_info *line, int max_x, int max_y) return -1; } + int x; + int y; + UNUSED_VAR(y); + while (msg) { getyx(win, y, x); @@ -396,10 +394,10 @@ static void line_info_init_line(ToxWindow *self, struct line_info *line) * Returns the id of the new line. * Returns -1 on failure. */ -int line_info_add(ToxWindow *self, bool show_timestamp, const char *name1, const char *name2, LINE_TYPE type, - uint8_t bold, uint8_t colour, const char *msg, ...) +int line_info_add(ToxWindow *self, const Client_Config *c_config, bool show_timestamp, const char *name1, + const char *name2, LINE_TYPE type, uint8_t bold, uint8_t colour, const char *msg, ...) { - if (!self) { + if (self == NULL) { return -1; } @@ -434,7 +432,7 @@ int line_info_add(ToxWindow *self, bool show_timestamp, const char *name1, const /* fallthrough */ case OUT_ACTION: - len += strlen(user_settings->line_normal) + 2; // two spaces + len += strlen(c_config->line_normal) + 2; // two spaces break; case IN_MSG: @@ -442,29 +440,29 @@ int line_info_add(ToxWindow *self, bool show_timestamp, const char *name1, const /* fallthrough */ case OUT_MSG: - len += strlen(user_settings->line_normal) + 3; // two spaces and a ':' char + len += strlen(c_config->line_normal) + 3; // two spaces and a ':' char break; case IN_PRVT_MSG: /* fallthrough */ case OUT_PRVT_MSG: - len += strlen(user_settings->line_special) + 3; + len += strlen(c_config->line_special) + 3; break; case CONNECTION: - len += strlen(user_settings->line_join) + 2; // two spaces + len += strlen(c_config->line_join) + 2; // two spaces break; case DISCONNECTION: - len += strlen(user_settings->line_quit) + 2; // two spaces + len += strlen(c_config->line_quit) + 2; // two spaces break; case SYS_MSG: break; case NAME_CHANGE: - len += strlen(user_settings->line_alert) + 2; // two spaces + len += strlen(c_config->line_alert) + 2; // two spaces break; case PROMPT: @@ -479,9 +477,12 @@ int line_info_add(ToxWindow *self, bool show_timestamp, const char *name1, const const uint16_t msg_width = line_info_add_msg(new_line->msg, sizeof(new_line->msg) / sizeof(wchar_t), frmt_msg); len += msg_width; - if (show_timestamp) { - get_time_str(new_line->timestr, sizeof(new_line->timestr)); - len += strlen(new_line->timestr) + 1; + if (show_timestamp) { + if (c_config->timestamps == TIMESTAMPS_ON) { + get_time_str(new_line->timestr, sizeof(new_line->timestr), c_config->timestamp_format); + } + + len += strlen(new_line->timestr) + 1; // need the +1 regardless of client setting } if (name1) { @@ -515,7 +516,7 @@ int line_info_add(ToxWindow *self, bool show_timestamp, const char *name1, const } /* adds a single queue item to hst if possible. only called once per call to line_info_print() */ -static void line_info_check_queue(ToxWindow *self) +static void line_info_check_queue(ToxWindow *self, const Client_Config *c_config) { struct history *hst = self->chatwin->hst; struct line_info *line = line_info_ret_queue(hst); @@ -524,7 +525,7 @@ static void line_info_check_queue(ToxWindow *self) return; } - if (hst->start_id > user_settings->history_size) { + if (hst->start_id > c_config->history_size) { line_info_root_fwd(hst); } @@ -538,7 +539,7 @@ static void line_info_check_queue(ToxWindow *self) } } -void line_info_print(ToxWindow *self) +void line_info_print(ToxWindow *self, const Client_Config *c_config) { ChatContext *ctx = self->chatwin; @@ -549,7 +550,7 @@ void line_info_print(ToxWindow *self) struct history *hst = ctx->hst; /* Only allow one new item to be added to chat window per call to this function */ - line_info_check_queue(self); + line_info_check_queue(self, c_config); WINDOW *win = ctx->history; @@ -573,7 +574,7 @@ void line_info_print(ToxWindow *self) struct line_info *line = hst->line_start->next; - if (!line) { + if (line == NULL) { return; } @@ -615,7 +616,7 @@ void line_info_print(ToxWindow *self) } wattron(win, COLOR_PAIR(nameclr)); - wprintw(win, "%s %s: ", user_settings->line_normal, line->name1); + wprintw(win, "%s %s: ", c_config->line_normal, line->name1); wattroff(win, COLOR_PAIR(nameclr)); if (line->msg[0] == L'\0') { @@ -653,7 +654,7 @@ void line_info_print(ToxWindow *self) const int nameclr = line->colour ? line->colour : GREEN; wattron(win, COLOR_PAIR(nameclr)); - wprintw(win, "%s %s: ", user_settings->line_special, line->name1); + wprintw(win, "%s %s: ", c_config->line_special, line->name1); wattroff(win, COLOR_PAIR(nameclr)); if (line->msg[0] == '>') { @@ -686,7 +687,7 @@ void line_info_print(ToxWindow *self) wattroff(win, COLOR_PAIR(BLUE)); wattron(win, COLOR_PAIR(YELLOW)); - wprintw(win, "%s %s ", user_settings->line_normal, line->name1); + wprintw(win, "%s %s ", c_config->line_normal, line->name1); print_wrap(win, line, max_x, max_y); wattroff(win, COLOR_PAIR(YELLOW)); @@ -742,7 +743,7 @@ void line_info_print(ToxWindow *self) wattroff(win, COLOR_PAIR(BLUE)); wattron(win, COLOR_PAIR(line->colour)); - wprintw(win, "%s ", user_settings->line_join); + wprintw(win, "%s ", c_config->line_join); wattron(win, A_BOLD); wprintw(win, "%s ", line->name1); @@ -762,7 +763,7 @@ void line_info_print(ToxWindow *self) wattroff(win, COLOR_PAIR(BLUE)); wattron(win, COLOR_PAIR(line->colour)); - wprintw(win, "%s ", user_settings->line_quit); + wprintw(win, "%s ", c_config->line_quit); wattron(win, A_BOLD); wprintw(win, "%s ", line->name1); @@ -782,7 +783,7 @@ void line_info_print(ToxWindow *self) wattroff(win, COLOR_PAIR(BLUE)); wattron(win, COLOR_PAIR(MAGENTA)); - wprintw(win, "%s ", user_settings->line_alert); + wprintw(win, "%s ", c_config->line_alert); wattron(win, A_BOLD); wprintw(win, "%s", line->name1); wattroff(win, A_BOLD); @@ -805,7 +806,7 @@ void line_info_print(ToxWindow *self) /* keep calling until queue is empty */ if (hst->queue_size > 0) { - line_info_print(self); + line_info_print(self, c_config); } } @@ -949,20 +950,20 @@ static void line_info_page_down(ToxWindow *self, struct history *hst) } } -bool line_info_onKey(ToxWindow *self, wint_t key) +bool line_info_onKey(ToxWindow *self, const Client_Config *c_config, wint_t key) { struct history *hst = self->chatwin->hst; bool match = true; - if (key == user_settings->key_half_page_up) { + if (key == c_config->key_half_page_up) { line_info_page_up(self, hst); - } else if (key == user_settings->key_half_page_down) { + } else if (key == c_config->key_half_page_down) { line_info_page_down(self, hst); - } else if (key == user_settings->key_scroll_line_up) { + } else if (key == c_config->key_scroll_line_up) { line_info_scroll_up(self, hst); - } else if (key == user_settings->key_scroll_line_down) { + } else if (key == c_config->key_scroll_line_down) { line_info_scroll_down(self, hst); - } else if (key == user_settings->key_page_bottom) { + } else if (key == c_config->key_page_bottom) { line_info_reset_start(self, hst); } else { match = false; diff --git a/src/line_info.h b/src/line_info.h index 64b5c041f..0e0e796a1 100644 --- a/src/line_info.h +++ b/src/line_info.h @@ -25,6 +25,7 @@ #include +#include "settings.h" #include "toxic.h" #include "windows.h" @@ -85,11 +86,11 @@ struct history { * Returns the id of the new line. * Returns -1 on failure. */ -int line_info_add(ToxWindow *self, bool show_timestamp, const char *name1, const char *name2, LINE_TYPE type, - uint8_t bold, uint8_t colour, const char *msg, ...); +int line_info_add(ToxWindow *self, const Client_Config *c_config, bool show_timestamp, const char *name1, + const char *name2, LINE_TYPE type, uint8_t bold, uint8_t colour, const char *msg, ...); /* Prints a section of history starting at line_start */ -void line_info_print(ToxWindow *self); +void line_info_print(ToxWindow *self, const Client_Config *c_config); /* frees all history lines */ void line_info_cleanup(struct history *hst); @@ -109,6 +110,8 @@ struct line_info *line_info_get(ToxWindow *self, uint32_t id); void line_info_reset_start(ToxWindow *self, struct history *hst); void line_info_init(struct history *hst); -bool line_info_onKey(ToxWindow *self, wint_t key); /* returns true if key is a match */ + +/* returns true if key is a match */ +bool line_info_onKey(ToxWindow *self, const Client_Config *c_config, wint_t key); #endif /* LINE_INFO_H */ diff --git a/src/log.c b/src/log.c index aac77eb90..30a6745fa 100644 --- a/src/log.c +++ b/src/log.c @@ -33,8 +33,6 @@ #include "toxic.h" #include "windows.h" -extern struct user_settings *user_settings; - /* Creates a log path and puts it in `dest. * * There are two types of logs: chat logs and prompt logs (see LOG_TYPE in log.h) @@ -47,14 +45,15 @@ extern struct user_settings *user_settings; * Return path length on success. * Return -1 if the path is too long. */ -static int get_log_path(char *dest, int destsize, const char *name, const char *selfkey, const char *otherkey) +static int get_log_path(const Client_Config *c_config, char *dest, int destsize, const char *name, + const char *selfkey, const char *otherkey) { if (!valid_nick(name)) { name = UNKNOWN_NAME; } const char *namedash = otherkey ? "-" : ""; - const char *set_path = user_settings->chatlogs_path; + const char *set_path = c_config->chatlogs_path; char *user_config_dir = get_user_config_dir(); int path_len = strlen(name) + strlen(".log") + strlen("-") + strlen(namedash); @@ -100,8 +99,8 @@ static int get_log_path(char *dest, int destsize, const char *name, const char * * Return 0 on success. * Return -1 on failure. */ -static int init_logging_session(const char *name, const char *selfkey, const char *otherkey, struct chatlog *log, - LOG_TYPE type) +static int init_logging_session(const Client_Config *c_config, const char *name, const char *selfkey, + const char *otherkey, struct chatlog *log, LOG_TYPE type) { if (log == NULL) { return -1; @@ -113,7 +112,7 @@ static int init_logging_session(const char *name, const char *selfkey, const cha char log_path[MAX_STR_SIZE]; - int path_len = get_log_path(log_path, sizeof(log_path), name, selfkey, otherkey); + const int path_len = get_log_path(c_config, log_path, sizeof(log_path), name, selfkey, otherkey); if (path_len == -1 || path_len >= sizeof(log->path)) { return -1; @@ -127,7 +126,8 @@ static int init_logging_session(const char *name, const char *selfkey, const cha #define LOG_FLUSH_LIMIT 1 /* limits calls to fflush to a max of one per LOG_FLUSH_LIMIT seconds */ -void write_to_log(const char *msg, const char *name, struct chatlog *log, bool event) +void write_to_log(struct chatlog *log, const Client_Config *c_config, const char *msg, const char *name, + bool event) { if (log == NULL) { return; @@ -150,7 +150,7 @@ void write_to_log(const char *msg, const char *name, struct chatlog *log, bool e snprintf(name_frmt, sizeof(name_frmt), "%s:", name); } - const char *t = user_settings->log_timestamp_format; + const char *t = c_config->log_timestamp_format; char s[MAX_STR_SIZE]; strftime(s, MAX_STR_SIZE, t, get_time()); fprintf(log->file, "%s %s %s\n", s, name_frmt, msg); @@ -210,7 +210,8 @@ int log_enable(struct chatlog *log) * Return 0 on success. * Return -1 on failure. */ -int log_init(struct chatlog *log, const char *name, const char *selfkey, const char *otherkey, LOG_TYPE type) +int log_init(struct chatlog *log, const Client_Config *c_config, const char *name, const char *selfkey, + const char *otherkey, LOG_TYPE type) { if (log == NULL) { return -1; @@ -221,7 +222,7 @@ int log_init(struct chatlog *log, const char *name, const char *selfkey, const c return -1; } - if (init_logging_session(name, selfkey, otherkey, log, type) == -1) { + if (init_logging_session(c_config, name, selfkey, otherkey, log, type) == -1) { return -1; } @@ -235,7 +236,7 @@ int log_init(struct chatlog *log, const char *name, const char *selfkey, const c * Return 0 on success or if log file doesn't exist. * Return -1 on failure. */ -int load_chat_history(ToxWindow *self, struct chatlog *log) +int load_chat_history(struct chatlog *log, ToxWindow *self, const Client_Config *c_config) { if (log == NULL) { return -1; @@ -245,7 +246,7 @@ int load_chat_history(ToxWindow *self, struct chatlog *log) return -1; } - off_t sz = file_size(log->path); + const off_t sz = file_size(log->path); if (sz <= 0) { return 0; @@ -281,7 +282,7 @@ int load_chat_history(ToxWindow *self, struct chatlog *log) buf[sz] = 0; /* Number of history lines to load: must not be larger than MAX_LINE_INFO_QUEUE - 2 */ - int L = MIN(MAX_LINE_INFO_QUEUE - 2, user_settings->history_size); + int L = MIN(MAX_LINE_INFO_QUEUE - 2, c_config->history_size); int start = 0; int count = 0; @@ -302,11 +303,11 @@ int load_chat_history(ToxWindow *self, struct chatlog *log) } while (line != NULL && count--) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", line); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", line); line = strtok_r(NULL, "\n", &tmp); } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, YELLOW, "---"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, YELLOW, "---"); free(buf); @@ -318,7 +319,8 @@ int load_chat_history(ToxWindow *self, struct chatlog *log) * Return 0 on success or if no log exists. * Return -1 on failure. */ -int rename_logfile(const char *src, const char *dest, const char *selfkey, const char *otherkey, int winnum) +int rename_logfile(const Client_Config *c_config, const char *src, const char *dest, const char *selfkey, + const char *otherkey, int winnum) { ToxWindow *toxwin = get_window_ptr(winnum); struct chatlog *log = NULL; @@ -342,16 +344,16 @@ int rename_logfile(const char *src, const char *dest, const char *selfkey, const char newpath[MAX_STR_SIZE]; char oldpath[MAX_STR_SIZE]; - if (get_log_path(oldpath, sizeof(oldpath), src, selfkey, otherkey) == -1) { + if (get_log_path(c_config, oldpath, sizeof(oldpath), src, selfkey, otherkey) == -1) { goto on_error; } - if (!file_exists(oldpath)) { - init_logging_session(dest, selfkey, otherkey, log, LOG_TYPE_CHAT); // still need to rename path + if (!file_exists(oldpath)) { // still need to rename path + init_logging_session(c_config, dest, selfkey, otherkey, log, LOG_TYPE_CHAT); return 0; } - int new_path_len = get_log_path(newpath, sizeof(newpath), dest, selfkey, otherkey); + const int new_path_len = get_log_path(c_config, newpath, sizeof(newpath), dest, selfkey, otherkey); if (new_path_len == -1 || new_path_len >= MAX_STR_SIZE) { goto on_error; diff --git a/src/log.h b/src/log.h index 99dcba29c..42d721d8c 100644 --- a/src/log.h +++ b/src/log.h @@ -23,6 +23,8 @@ #ifndef LOG_H #define LOG_H +#include "settings.h" + struct chatlog { FILE *file; time_t lastwrite; @@ -40,10 +42,13 @@ typedef enum LOG_TYPE { * Return 0 on success. * Return -1 on failure. */ -int log_init(struct chatlog *log, const char *name, const char *selfkey, const char *otherkey, LOG_TYPE type); +int log_init(struct chatlog *log, const Client_Config *c_config, const char *name, const char *selfkey, + const char *otherkey, + LOG_TYPE type); /* formats/writes line to log file */ -void write_to_log(const char *msg, const char *name, struct chatlog *log, bool event); +void write_to_log(struct chatlog *log, const Client_Config *c_config, const char *msg, const char *name, + bool event); /* enables logging for specified log. * @@ -60,13 +65,14 @@ void log_disable(struct chatlog *log); * Return 0 on success or if log file doesn't exist. * Return -1 on failure. */ -int load_chat_history(ToxWindow *self, struct chatlog *log); +int load_chat_history(struct chatlog *log, ToxWindow *self, const Client_Config *c_config); /* Renames chatlog file `src` to `dest`. * * Return 0 on success or if no log exists. * Return -1 on failure. */ -int rename_logfile(const char *src, const char *dest, const char *selfkey, const char *otherkey, int winnum); +int rename_logfile(const Client_Config *c_config, const char *src, const char *dest, const char *selfkey, + const char *otherkey, int winnum); #endif /* LOG_H */ diff --git a/src/message_queue.c b/src/message_queue.c index eeb633b43..460f15a39 100644 --- a/src/message_queue.c +++ b/src/message_queue.c @@ -94,12 +94,15 @@ static void cqueue_mark_read(ToxWindow *self, struct cqueue_msg *msg) } /* removes message with matching receipt from queue, writes to log and updates line to show the message was received. */ -void cqueue_remove(ToxWindow *self, Tox *tox, uint32_t receipt) +void cqueue_remove(ToxWindow *self, Toxic *toxic, uint32_t receipt) { struct chatlog *log = self->chatwin->log; struct chat_queue *q = self->chatwin->cqueue; struct cqueue_msg *msg = q->root; + Tox *tox = toxic->tox; + const Client_Config *c_config = toxic->c_config; + while (msg) { if (msg->receipt != receipt) { msg = msg->next; @@ -113,7 +116,7 @@ void cqueue_remove(ToxWindow *self, Tox *tox, uint32_t receipt) size_t len = tox_self_get_name_size(tox); selfname[len] = 0; - write_to_log(msg->message, selfname, log, msg->type == OUT_ACTION); + write_to_log(log, c_config, msg->message, selfname, msg->type == OUT_ACTION); } cqueue_mark_read(self, msg); diff --git a/src/message_queue.h b/src/message_queue.h index bf349e9ae..6420c0473 100644 --- a/src/message_queue.h +++ b/src/message_queue.h @@ -57,6 +57,6 @@ void cqueue_try_send(ToxWindow *self, Tox *tox); void cqueue_check_unread(ToxWindow *self); /* removes message with matching receipt from queue, writes to log and updates line to show the message was received. */ -void cqueue_remove(ToxWindow *self, Tox *tox, uint32_t receipt); +void cqueue_remove(ToxWindow *self, Toxic *toxic, uint32_t receipt); #endif /* MESSAGE_QUEUE_H */ diff --git a/src/misc_tools.c b/src/misc_tools.c index 1757acfff..60d1ed075 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -35,7 +35,6 @@ #include "windows.h" extern ToxWindow *prompt; -extern struct user_settings *user_settings; void clear_screen(void) { @@ -96,24 +95,21 @@ struct tm *get_time(void) return timeinfo; } -/* Puts the current time in buf in the format of specified by the config */ -void get_time_str(char *buf, size_t bufsize) +void get_time_str(char *buf, size_t bufsize, const char *format_string) { if (buf == NULL || bufsize == 0) { return; } - *buf = 0; - - if (user_settings->timestamps == TIMESTAMPS_OFF) { + if (strftime(buf, bufsize, format_string, get_time()) > 0) { return; } - const char *t = user_settings->timestamp_format; - - if (strftime(buf, bufsize, t, get_time()) == 0) { - strftime(buf, bufsize, TIMESTAMP_DEFAULT, get_time()); + if (strftime(buf, bufsize, TIMESTAMP_DEFAULT, get_time()) > 0) { + return; } + + buf[0] = '\0'; } /* Converts seconds to string in format HH:mm:ss; truncates hours and minutes when necessary */ diff --git a/src/misc_tools.h b/src/misc_tools.h index 33544f770..6afa40e7e 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -95,8 +95,12 @@ int tox_id_bytes_to_str(const char *bin_id, size_t bin_id_size, char *output, si /* get the current unix time (not thread safe) */ time_t get_unix_time(void); -/* Puts the current time in buf in the format of specified by the config */ -void get_time_str(char *buf, size_t bufsize); +/* Puts the current time in `buf` in the format of specified by `format_string`. + * + * If the passed format string is invalid, a default format will be tried. If that + * fails, `buf` will be set to nul. + */ +void get_time_str(char *buf, size_t bufsize, const char *format_string); /* Converts seconds to string in format H hours, m minutes, s seconds */ void get_elapsed_time_str_alt(char *buf, int bufsize, uint64_t secs); diff --git a/src/name_lookup.c b/src/name_lookup.c index f87f30fb5..4f41c245a 100644 --- a/src/name_lookup.c +++ b/src/name_lookup.c @@ -67,7 +67,7 @@ static void clear_thread_data(void) }; } -static int lookup_error(ToxWindow *self, const char *errmsg, ...) +static int lookup_error(ToxWindow *self, const Client_Config *c_config, const char *errmsg, ...) { char frmt_msg[MAX_STR_SIZE]; @@ -77,7 +77,7 @@ static int lookup_error(ToxWindow *self, const char *errmsg, ...) va_end(args); pthread_mutex_lock(&Winthread.lock); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "name lookup failed: %s", frmt_msg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "name lookup failed: %s", frmt_msg); pthread_mutex_unlock(&Winthread.lock); return -1; @@ -240,7 +240,11 @@ static int process_response(struct Recv_Curl_Data *recv_data) void *lookup_thread_func(void *data) { - UNUSED_VAR(data); + const Client_Config *c_config = (Client_Config *) data; + + if (c_config == NULL) { + kill_lookup_thread(); + } ToxWindow *self = t_data.self; @@ -248,7 +252,7 @@ void *lookup_thread_func(void *data) char name[MAX_STR_SIZE]; if (parse_addr(t_data.addr, name, sizeof(name), input_domain, sizeof(input_domain)) == -1) { - lookup_error(self, "Input must be a 76 character Tox ID or an address in the form: username@domain"); + lookup_error(self, c_config, "Input must be a 76 character Tox ID or an address in the form: username@domain"); kill_lookup_thread(); } @@ -257,9 +261,9 @@ void *lookup_thread_func(void *data) if (!get_domain_match(nameserver_key, real_domain, sizeof(real_domain), input_domain)) { if (!strcasecmp(input_domain, "utox.org")) { - lookup_error(self, "utox.org uses deprecated DNS-based lookups and is no longer supported by Toxic."); + lookup_error(self, c_config, "utox.org uses deprecated DNS-based lookups and is no longer supported by Toxic."); } else { - lookup_error(self, "Name server domain not found."); + lookup_error(self, c_config, "Name server domain not found."); } kill_lookup_thread(); @@ -268,14 +272,14 @@ void *lookup_thread_func(void *data) CURL *c_handle = curl_easy_init(); if (!c_handle) { - lookup_error(self, "curl handler error"); + lookup_error(self, c_config, "curl handler error"); kill_lookup_thread(); } struct Recv_Curl_Data *recv_data = calloc(1, sizeof(struct Recv_Curl_Data)); if (recv_data == NULL) { - lookup_error(self, "memory allocation error"); + lookup_error(self, c_config, "memory allocation error"); kill_lookup_thread(); } @@ -292,70 +296,70 @@ void *lookup_thread_func(void *data) int ret = curl_easy_setopt(c_handle, CURLOPT_HTTPHEADER, headers); if (ret != CURLE_OK) { - lookup_error(self, "Failed to set http headers (libcurl error %d)", ret); + lookup_error(self, c_config, "Failed to set http headers (libcurl error %d)", ret); goto on_exit; } ret = curl_easy_setopt(c_handle, CURLOPT_URL, real_domain); if (ret != CURLE_OK) { - lookup_error(self, "Failed to set url (libcurl error %d)", ret); + lookup_error(self, c_config, "Failed to set url (libcurl error %d)", ret); goto on_exit; } ret = curl_easy_setopt(c_handle, CURLOPT_WRITEFUNCTION, curl_cb_write_data); if (ret != CURLE_OK) { - lookup_error(self, "Failed to set write function callback (libcurl error %d)", ret); + lookup_error(self, c_config, "Failed to set write function callback (libcurl error %d)", ret); goto on_exit; } ret = curl_easy_setopt(c_handle, CURLOPT_WRITEDATA, recv_data); if (ret != CURLE_OK) { - lookup_error(self, "Failed to set write data (libcurl error %d)", ret); + lookup_error(self, c_config, "Failed to set write data (libcurl error %d)", ret); goto on_exit; } ret = curl_easy_setopt(c_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); if (ret != CURLE_OK) { - lookup_error(self, "Failed to set useragent (libcurl error %d)", ret); + lookup_error(self, c_config, "Failed to set useragent (libcurl error %d)", ret); goto on_exit; } ret = curl_easy_setopt(c_handle, CURLOPT_POSTFIELDS, post_data); if (ret != CURLE_OK) { - lookup_error(self, "Failed to set post data (libcurl error %d)", ret); + lookup_error(self, c_config, "Failed to set post data (libcurl error %d)", ret); goto on_exit; } int proxy_ret = set_curl_proxy(c_handle, arg_opts.proxy_address, arg_opts.proxy_port, arg_opts.proxy_type); if (proxy_ret != 0) { - lookup_error(self, "Failed to set proxy (error %d)\n", proxy_ret); + lookup_error(self, c_config, "Failed to set proxy (error %d)\n", proxy_ret); goto on_exit; } ret = curl_easy_setopt(c_handle, CURLOPT_USE_SSL, CURLUSESSL_ALL); if (ret != CURLE_OK) { - lookup_error(self, "TLS could not be enabled (libcurl error %d)", ret); + lookup_error(self, c_config, "TLS could not be enabled (libcurl error %d)", ret); goto on_exit; } ret = curl_easy_setopt(c_handle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); if (ret != CURLE_OK) { - lookup_error(self, "TLSv1.2 could not be set (libcurl error %d)", ret); + lookup_error(self, c_config, "TLSv1.2 could not be set (libcurl error %d)", ret); goto on_exit; } ret = curl_easy_setopt(c_handle, CURLOPT_SSL_CIPHER_LIST, TLS_CIPHER_SUITE_LIST); if (ret != CURLE_OK) { - lookup_error(self, "Failed to set TLS cipher list (libcurl error %d)", ret); + lookup_error(self, c_config, "Failed to set TLS cipher list (libcurl error %d)", ret); goto on_exit; } @@ -367,7 +371,7 @@ void *lookup_thread_func(void *data) ret = curl_easy_setopt(c_handle, CURLOPT_SSL_CIPHER_LIST, NULL); if (ret != CURLE_OK) { - lookup_error(self, "Failed to set TLS cipher list (libcurl error %d)", ret); + lookup_error(self, c_config, "Failed to set TLS cipher list (libcurl error %d)", ret); goto on_exit; } @@ -375,13 +379,13 @@ void *lookup_thread_func(void *data) } if (ret != CURLE_OK) { - lookup_error(self, "HTTPS lookup error (libcurl error %d)", ret); + lookup_error(self, c_config, "HTTPS lookup error (libcurl error %d)", ret); goto on_exit; } } if (process_response(recv_data) == -1) { - lookup_error(self, "Bad response."); + lookup_error(self, c_config, "Bad response."); goto on_exit; } @@ -404,13 +408,16 @@ void *lookup_thread_func(void *data) */ bool name_lookup(ToxWindow *self, Toxic *toxic, const char *id_bin, const char *addr, const char *message) { + const Client_Config *c_config = toxic->c_config; + if (t_data.disabled) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "nameservers list is empty or does not exist."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "nameservers list is empty or does not exist."); return false; } if (t_data.busy) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Please wait for previous name lookup to finish."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, + "Please wait for previous name lookup to finish."); return false; } @@ -422,20 +429,20 @@ bool name_lookup(ToxWindow *self, Toxic *toxic, const char *id_bin, const char * t_data.busy = true; if (pthread_attr_init(&lookup_thread.attr) != 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, "Error: lookup thread attr failed to init"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, "Error: lookup thread attr failed to init"); clear_thread_data(); return false; } if (pthread_attr_setdetachstate(&lookup_thread.attr, PTHREAD_CREATE_DETACHED) != 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, "Error: lookup thread attr failed to set"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, "Error: lookup thread attr failed to set"); pthread_attr_destroy(&lookup_thread.attr); clear_thread_data(); return false; } - if (pthread_create(&lookup_thread.tid, &lookup_thread.attr, lookup_thread_func, NULL) != 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, "Error: lookup thread failed to init"); + if (pthread_create(&lookup_thread.tid, &lookup_thread.attr, lookup_thread_func, (void *) c_config) != 0) { + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, "Error: lookup thread failed to init"); pthread_attr_destroy(&lookup_thread.attr); clear_thread_data(); return false; diff --git a/src/notify.c b/src/notify.c index afca5bb58..30f69c8cc 100644 --- a/src/notify.c +++ b/src/notify.c @@ -61,8 +61,6 @@ #define SOUNDS_SIZE 10 #define ACTIVE_NOTIFS_MAX 10 -extern struct user_settings *user_settings; - static struct Control { time_t cooldown; time_t notif_timeout; @@ -129,9 +127,9 @@ static void tab_notify(ToxWindow *self, uint64_t flags) ++self->pending_messages; } -static bool notifications_are_disabled(uint64_t flags) +static bool notifications_are_disabled(const Client_Config *c_config, uint64_t flags) { - if (user_settings->alerts != ALERTS_ENABLED) { + if (c_config->alerts != ALERTS_ENABLED) { return true; } @@ -172,7 +170,7 @@ static bool device_opened = false; time_t last_opened_update = 0; /* Opens primary device. Returns true on succe*/ -void m_open_device(void) +static void m_open_device(const Client_Config *c_config) { last_opened_update = get_unix_time(); @@ -180,8 +178,14 @@ void m_open_device(void) return; } - /* Blah error check */ - open_output_device(&Control.device_idx, 48000, 20, 1); +#ifdef AUDIO + const double VAD_threshold = c_config->VAD_threshold; +#else + const double VAD_threshold = 0; +#endif // AUDIO + + /* TODO: error check */ + open_output_device(&Control.device_idx, 48000, 20, 1, VAD_threshold); device_opened = true; } @@ -515,12 +519,12 @@ bool set_sound(Notification sound, const char *value) return stat(value, &buf) == 0; } -int play_sound_internal(Notification what, bool loop) +static int play_sound_internal(const Client_Config *c_config, Notification what, bool loop) { uint32_t source; uint32_t buffer; - m_open_device(); + m_open_device(c_config); alGenSources(1, &source); alGenBuffers(1, &buffer); @@ -540,7 +544,7 @@ int play_sound_internal(Notification what, bool loop) return rc; } -int play_notify_sound(Notification notif, uint64_t flags) +static int play_notify_sound(const Client_Config *c_config, Notification notif, uint64_t flags) { int rc = -1; @@ -553,7 +557,7 @@ int play_notify_sound(Notification notif, uint64_t flags) return -1; } - rc = play_sound_internal(notif, flags & NT_LOOP ? 1 : 0); + rc = play_sound_internal(c_config, notif, flags & NT_LOOP ? 1 : 0); } return rc; @@ -580,10 +584,10 @@ void stop_sound(int id) } #endif /* SOUND_NOTIFY */ -static int m_play_sound(Notification notif, uint64_t flags) +static int m_play_sound(const Client_Config *c_config, Notification notif, uint64_t flags) { #ifdef SOUND_NOTIFY - return play_notify_sound(notif, flags); + return play_notify_sound(c_config, notif, flags); #else if (notif != silent) { @@ -594,11 +598,12 @@ static int m_play_sound(Notification notif, uint64_t flags) #endif /* SOUND_NOTIFY */ } -int sound_notify(ToxWindow *self, Notification notif, uint64_t flags, int *id_indicator) +int sound_notify(ToxWindow *self, const Client_Config *c_config, Notification notif, uint64_t flags, + int *id_indicator) { tab_notify(self, flags); - if (notifications_are_disabled(flags)) { + if (notifications_are_disabled(c_config, flags)) { return -1; } @@ -606,9 +611,9 @@ int sound_notify(ToxWindow *self, Notification notif, uint64_t flags, int *id_in control_lock(); if (self && (!self->stb || self->stb->status != TOX_USER_STATUS_BUSY)) { - id = m_play_sound(notif, flags); + id = m_play_sound(c_config, notif, flags); } else if (flags & NT_ALWAYS) { - id = m_play_sound(notif, flags); + id = m_play_sound(c_config, notif, flags); } #if defined(BOX_NOTIFY) && !defined(SOUND_NOTIFY) @@ -634,11 +639,11 @@ int sound_notify(ToxWindow *self, Notification notif, uint64_t flags, int *id_in return id; } -int sound_notify2(ToxWindow *self, Notification notif, uint64_t flags, int id) +int sound_notify2(ToxWindow *self, const Client_Config *c_config, Notification notif, uint64_t flags, int id) { tab_notify(self, flags); - if (notifications_are_disabled(flags)) { + if (notifications_are_disabled(c_config, flags)) { return -1; } @@ -654,7 +659,7 @@ int sound_notify2(ToxWindow *self, Notification notif, uint64_t flags, int id) return -1; } - m_open_device(); + m_open_device(c_config); alSourceStop(actives[id].source); alDeleteSources(1, &actives[id].source); @@ -681,17 +686,17 @@ int sound_notify2(ToxWindow *self, Notification notif, uint64_t flags, int id) #endif /* SOUND_NOTIFY */ } -int box_notify(ToxWindow *self, Notification notif, uint64_t flags, int *id_indicator, const char *title, - const char *format, ...) +int box_notify(ToxWindow *self, const Client_Config *c_config, Notification notif, uint64_t flags, + int *id_indicator, const char *title, const char *format, ...) { - if (notifications_are_disabled(flags)) { + if (notifications_are_disabled(c_config, flags)) { tab_notify(self, flags); return -1; } #ifdef BOX_NOTIFY - int id = sound_notify(self, notif, flags, id_indicator); + int id = sound_notify(self, c_config, notif, flags, id_indicator); control_lock(); @@ -750,20 +755,21 @@ int box_notify(ToxWindow *self, Notification notif, uint64_t flags, int *id_indi control_unlock(); return id; #else - return sound_notify(self, notif, flags, id_indicator); + return sound_notify(self, c_config, notif, flags, id_indicator); #endif /* BOX_NOTIFY */ } -int box_notify2(ToxWindow *self, Notification notif, uint64_t flags, int id, const char *format, ...) +int box_notify2(ToxWindow *self, const Client_Config *c_config, Notification notif, uint64_t flags, + int id, const char *format, ...) { - if (notifications_are_disabled(flags)) { + if (notifications_are_disabled(c_config, flags)) { tab_notify(self, flags); return -1; } #ifdef BOX_NOTIFY - if (sound_notify2(self, notif, flags, id) == -1) { + if (sound_notify2(self, c_config, notif, flags, id) == -1) { return -1; } @@ -802,15 +808,16 @@ int box_notify2(ToxWindow *self, Notification notif, uint64_t flags, int id, con return id; #else - return sound_notify2(self, notif, flags, id); + return sound_notify2(self, c_config, notif, flags, id); #endif /* BOX_NOTIFY */ } -int box_silent_notify(ToxWindow *self, uint64_t flags, int *id_indicator, const char *title, const char *format, ...) +int box_silent_notify(ToxWindow *self, const Client_Config *c_config, uint64_t flags, int *id_indicator, + const char *title, const char *format, ...) { tab_notify(self, flags); - if (notifications_are_disabled(flags)) { + if (notifications_are_disabled(c_config, flags)) { return -1; } @@ -864,11 +871,12 @@ int box_silent_notify(ToxWindow *self, uint64_t flags, int *id_indicator, const #endif /* BOX_NOTIFY */ } -int box_silent_notify2(ToxWindow *self, uint64_t flags, int id, const char *format, ...) +int box_silent_notify2(ToxWindow *self, const Client_Config *c_config, uint64_t flags, int id, + const char *format, ...) { tab_notify(self, flags); - if (notifications_are_disabled(flags)) { + if (notifications_are_disabled(c_config, flags)) { return -1; } diff --git a/src/notify.h b/src/notify.h index ebd2cee56..27a2a41bf 100644 --- a/src/notify.h +++ b/src/notify.h @@ -65,16 +65,21 @@ void terminate_notify(void); /* Kills all notifications for `id`. This must be called before freeing a ToxWindow. */ void kill_notifs(int id); -int sound_notify(ToxWindow *self, Notification notif, uint64_t flags, int *id_indicator); -int sound_notify2(ToxWindow *self, Notification notif, uint64_t flags, int id); +int sound_notify(ToxWindow *self, const Client_Config *c_config, Notification notif, uint64_t flags, + int *id_indicator); +int sound_notify2(ToxWindow *self, const Client_Config *c_config, Notification notif, uint64_t flags, int id); void stop_sound(int id); -int box_notify(ToxWindow *self, Notification notif, uint64_t flags, int *id_indicator, const char *title, - const char *format, ...); -int box_notify2(ToxWindow *self, Notification notif, uint64_t flags, int id, const char *format, ...); -int box_silent_notify(ToxWindow *self, uint64_t flags, int *id_indicator, const char *title, const char *format, ...); -int box_silent_notify2(ToxWindow *self, uint64_t flags, int id, const char *format, ...); +int box_notify(ToxWindow *self, const Client_Config *c_config, Notification notif, uint64_t flags, + int *id_indicator, + const char *title, const char *format, ...); +int box_notify2(ToxWindow *self, const Client_Config *c_config, Notification notif, uint64_t flags, int id, + const char *format, ...); +int box_silent_notify(ToxWindow *self, const Client_Config *c_config, uint64_t flags, int *id_indicator, + const char *title, const char *format, ...); +int box_silent_notify2(ToxWindow *self, const Client_Config *c_config, uint64_t flags, int id, + const char *format, ...); #ifdef SOUND_NOTIFY bool set_sound(Notification sound, const char *value); diff --git a/src/prompt.c b/src/prompt.c index 5b999fc6a..06593f4ad 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -44,7 +44,6 @@ #include "windows.h" extern ToxWindow *prompt; -extern struct user_settings *user_settings; extern struct Winthread Winthread; extern FriendsList Friends; @@ -101,24 +100,26 @@ static const char *glob_cmd_list[] = { }; -void kill_prompt_window(ToxWindow *self) +void kill_prompt_window(ToxWindow *self, const Client_Config *c_config) { ChatContext *ctx = self->chatwin; StatusBar *statusbar = self->stb; - log_disable(ctx->log); - line_info_cleanup(ctx->hst); + if (ctx != NULL) { + log_disable(ctx->log); + line_info_cleanup(ctx->hst); - delwin(ctx->linewin); - delwin(ctx->history); - delwin(statusbar->topline); + delwin(ctx->linewin); + delwin(ctx->history); + free(ctx->log); + free(ctx); + } - free(ctx->log); - free(ctx); + delwin(statusbar->topline); free(self->help); free(statusbar); - del_window(self); + del_window(self, c_config); } /* callback: Updates own connection status in prompt statusbar */ @@ -141,7 +142,7 @@ void prompt_update_nick(ToxWindow *prompt, const char *nick) } /* Updates own statusmessage */ -void prompt_update_statusmessage(ToxWindow *prompt, Tox *tox, const char *statusmsg) +void prompt_update_statusmessage(ToxWindow *prompt, Toxic *toxic, const char *statusmsg) { StatusBar *statusbar = prompt->stb; snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); @@ -149,10 +150,10 @@ void prompt_update_statusmessage(ToxWindow *prompt, Tox *tox, const char *status statusbar->statusmsg_len = len; Tox_Err_Set_Info err; - tox_self_set_status_message(tox, (const uint8_t *) statusmsg, len, &err); + tox_self_set_status_message(toxic->tox, (const uint8_t *) statusmsg, len, &err); if (err != TOX_ERR_SET_INFO_OK) { - line_info_add(prompt, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to set note (error %d)\n", err); + line_info_add(prompt, toxic->c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to set note (error %d)\n", err); } } @@ -206,6 +207,7 @@ static bool prompt_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) return false; } + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; int x, y, y2, x2; @@ -229,15 +231,15 @@ static bool prompt_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) } if (ltr || key == '\n') { /* char is printable */ - input_new_char(self, key, x, x2); + input_new_char(self, c_config, key, x, x2); return true; } - if (line_info_onKey(self, key)) { + if (line_info_onKey(self, c_config, key)) { return true; } - if (input_handle(self, key, x, x2)) { + if (input_handle(self, c_config, key, x, x2)) { return true; } @@ -266,9 +268,9 @@ static bool prompt_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) "away", "busy", }; - diff = complete_line(self, status_cmd_list, sizeof(status_cmd_list) / sizeof(char *)); + diff = complete_line(self, toxic, status_cmd_list, sizeof(status_cmd_list) / sizeof(char *)); } else { - diff = complete_line(self, glob_cmd_list, sizeof(glob_cmd_list) / sizeof(char *)); + diff = complete_line(self, toxic, glob_cmd_list, sizeof(glob_cmd_list) / sizeof(char *)); } if (diff != -1) { @@ -277,10 +279,10 @@ static bool prompt_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) ctx->start = wlen < x2 ? 0 : wlen - x2 + 1; } } else { - sound_notify(self, notif_error, 0, NULL); + sound_notify(self, c_config, notif_error, 0, NULL); } } else { - sound_notify(self, notif_error, 0, NULL); + sound_notify(self, c_config, notif_error, 0, NULL); } } else if (key == '\r') { input_ret = true; @@ -294,10 +296,10 @@ static bool prompt_onKey(ToxWindow *self, Toxic *toxic, wint_t key, bool ltr) char line[MAX_STR_SIZE]; if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, " * Failed to parse message."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, " * Failed to parse message."); } else { if (strcmp(line, "/clear") != 0) { - line_info_add(self, false, NULL, NULL, PROMPT, 0, 0, "%s", line); + line_info_add(self, c_config, false, NULL, NULL, PROMPT, 0, 0, "%s", line); } execute(ctx->history, self, toxic, line, GLOBAL_COMMAND_MODE); @@ -330,7 +332,7 @@ static void prompt_onDraw(ToxWindow *self, Toxic *toxic) ChatContext *ctx = self->chatwin; pthread_mutex_lock(&Winthread.lock); - line_info_print(self); + line_info_print(self, toxic->c_config); pthread_mutex_unlock(&Winthread.lock); wclear(ctx->linewin); @@ -499,6 +501,7 @@ static void prompt_onConnectionChange(ToxWindow *self, Toxic *toxic, uint32_t fr return; } + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; char nick[TOX_MAX_NAME_LENGTH] = {0}; /* stop removing this initiation */ @@ -510,32 +513,32 @@ static void prompt_onConnectionChange(ToxWindow *self, Toxic *toxic, uint32_t fr const char *msg; - if (user_settings->show_connection_msg == SHOW_WELCOME_MSG_OFF) { + if (c_config->show_connection_msg == SHOW_WELCOME_MSG_OFF) { return; } if (connection_status != TOX_CONNECTION_NONE && Friends.list[friendnum].connection_status == TOX_CONNECTION_NONE) { msg = "has come online"; - line_info_add(self, true, nick, NULL, CONNECTION, 0, GREEN, msg); - write_to_log(msg, nick, ctx->log, true); + line_info_add(self, c_config, true, nick, NULL, CONNECTION, 0, GREEN, msg); + write_to_log(ctx->log, c_config, msg, nick, true); if (self->active_box != -1) { - box_notify2(self, user_log_in, NT_WNDALERT_2 | NT_NOTIFWND | NT_RESTOL, self->active_box, + box_notify2(self, c_config, user_log_in, NT_WNDALERT_2 | NT_NOTIFWND | NT_RESTOL, self->active_box, "%s has come online", nick); } else { - box_notify(self, user_log_in, NT_WNDALERT_2 | NT_NOTIFWND | NT_RESTOL, &self->active_box, + box_notify(self, c_config, user_log_in, NT_WNDALERT_2 | NT_NOTIFWND | NT_RESTOL, &self->active_box, "Toxic", "%s has come online", nick); } } else if (connection_status == TOX_CONNECTION_NONE) { msg = "has gone offline"; - line_info_add(self, true, nick, NULL, DISCONNECTION, 0, RED, msg); - write_to_log(msg, nick, ctx->log, true); + line_info_add(self, c_config, true, nick, NULL, DISCONNECTION, 0, RED, msg); + write_to_log(ctx->log, c_config, msg, nick, true); if (self->active_box != -1) { - box_notify2(self, user_log_out, NT_WNDALERT_2 | NT_NOTIFWND | NT_RESTOL, self->active_box, + box_notify2(self, c_config, user_log_out, NT_WNDALERT_2 | NT_NOTIFWND | NT_RESTOL, self->active_box, "%s has gone offline", nick); } else { - box_notify(self, user_log_out, NT_WNDALERT_2 | NT_NOTIFWND | NT_RESTOL, &self->active_box, + box_notify(self, c_config, user_log_out, NT_WNDALERT_2 | NT_NOTIFWND | NT_RESTOL, &self->active_box, "Toxic", "%s has gone offline", nick); } } @@ -563,22 +566,22 @@ static bool key_is_similar(const char *key) static void prompt_onFriendRequest(ToxWindow *self, Toxic *toxic, const char *key, const char *data, size_t length) { - UNUSED_VAR(toxic); UNUSED_VAR(length); - if (self == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; - line_info_add(self, true, NULL, NULL, SYS_MSG, 0, 0, "Friend request with the message '%s'", data); - write_to_log("Friend request with the message '%s'", "", ctx->log, true); + line_info_add(self, c_config, true, NULL, NULL, SYS_MSG, 0, 0, "Friend request with the message '%s'", data); + write_to_log(ctx->log, c_config, "Friend request with the message '%s'", "", true); if (key_is_similar(key)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, "WARNING: This contact's public key is suspiciously similar to that of another contact "); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, RED, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, RED, "in your list. This may be an impersonation attempt, or it may have occurred by chance."); } @@ -586,15 +589,15 @@ static void prompt_onFriendRequest(ToxWindow *self, Toxic *toxic, const char *ke if (n == -1) { const char *errmsg = "Friend request queue is full. Discarding request."; - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, errmsg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, errmsg); return; } - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Type \"/accept %d\" or \"/decline %d\"", n, n); - sound_notify(self, generic_message, NT_WNDALERT_1 | NT_NOTIFWND, NULL); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Type \"/accept %d\" or \"/decline %d\"", n, n); + sound_notify(self, c_config, generic_message, NT_WNDALERT_1 | NT_NOTIFWND, NULL); } -void prompt_init_statusbar(ToxWindow *self, Tox *tox, bool first_time_run) +void prompt_init_statusbar(ToxWindow *self, Toxic *toxic, bool first_time_run) { int x2, y2; getmaxyx(self->window, y2, x2); @@ -605,6 +608,8 @@ void prompt_init_statusbar(ToxWindow *self, Tox *tox, bool first_time_run) UNUSED_VAR(y2); + Tox *tox = toxic->tox; + /* Init statusbar info */ StatusBar *statusbar = self->stb; statusbar->status = TOX_USER_STATUS_NONE; @@ -630,7 +635,7 @@ void prompt_init_statusbar(ToxWindow *self, Tox *tox, bool first_time_run) statusmsg[s_len] = '\0'; } - prompt_update_statusmessage(prompt, tox, statusmsg); + prompt_update_statusmessage(prompt, toxic, statusmsg); prompt_update_status(prompt, status); prompt_update_nick(prompt, nick); @@ -638,37 +643,39 @@ void prompt_init_statusbar(ToxWindow *self, Tox *tox, bool first_time_run) statusbar->topline = subwin(self->window, TOP_BAR_HEIGHT, x2, 0, 0); } -static void print_welcome_msg(ToxWindow *self) +static void print_welcome_msg(ToxWindow *self, const Client_Config *c_config) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 1, BLUE, " _____ _____ _____ ____ "); - line_info_add(self, false, NULL, NULL, SYS_MSG, 1, BLUE, " |_ _/ _ \\ \\/ /_ _/ ___|"); - line_info_add(self, false, NULL, NULL, SYS_MSG, 1, BLUE, " | || | | \\ / | | | "); - line_info_add(self, false, NULL, NULL, SYS_MSG, 1, BLUE, " | || |_| / \\ | | |___ "); - line_info_add(self, false, NULL, NULL, SYS_MSG, 1, BLUE, " |_| \\___/_/\\_\\___\\____| v." TOXICVER); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, ""); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 1, BLUE, " _____ _____ _____ ____ "); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 1, BLUE, " |_ _/ _ \\ \\/ /_ _/ ___|"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 1, BLUE, " | || | | \\ / | | | "); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 1, BLUE, " | || |_| / \\ | | |___ "); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 1, BLUE, + " |_| \\___/_/\\_\\___\\____| v." TOXICVER); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, ""); const char *msg = "Welcome to Toxic, a free, open source Tox-based instant messaging client."; - line_info_add(self, false, NULL, NULL, SYS_MSG, 1, CYAN, msg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 1, CYAN, msg); msg = "Type \"/help\" for assistance. Further help may be found via the man page."; - line_info_add(self, false, NULL, NULL, SYS_MSG, 1, CYAN, msg); - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, ""); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 1, CYAN, msg); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, ""); } -static void prompt_init_log(ToxWindow *self, Tox *tox, const char *self_name) +static void prompt_init_log(ToxWindow *self, Toxic *toxic, const char *self_name) { + const Client_Config *c_config = toxic->c_config; ChatContext *ctx = self->chatwin; char myid[TOX_ADDRESS_SIZE]; - tox_self_get_address(tox, (uint8_t *) myid); + tox_self_get_address(toxic->tox, (uint8_t *) myid); - if (log_init(ctx->log, self->name, myid, NULL, LOG_TYPE_PROMPT) != 0) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Warning: Log failed to initialize."); + if (log_init(ctx->log, toxic->c_config, self->name, myid, NULL, LOG_TYPE_PROMPT) != 0) { + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Warning: Log failed to initialize."); return; } - if (user_settings->autolog == AUTOLOG_ON) { + if (toxic->c_config->autolog == AUTOLOG_ON) { if (log_enable(ctx->log) == -1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Warning: Failed to enable log."); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Warning: Failed to enable log."); } } } @@ -704,13 +711,13 @@ static void prompt_onInit(ToxWindow *self, Toxic *toxic) line_info_init(ctx->hst); - prompt_init_log(self, toxic->tox, self->name); + prompt_init_log(self, toxic, self->name); scrollok(ctx->history, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0); - if (user_settings->show_welcome_msg == SHOW_WELCOME_MSG_ON) { - print_welcome_msg(self); + if (toxic->c_config->show_welcome_msg == SHOW_WELCOME_MSG_ON) { + print_welcome_msg(self, toxic->c_config); } } diff --git a/src/prompt.h b/src/prompt.h index 8c8b1e9dc..99f49b502 100644 --- a/src/prompt.h +++ b/src/prompt.h @@ -46,12 +46,12 @@ extern FriendRequests FrndRequests; ToxWindow *new_prompt(void); void prep_prompt_win(void); -void prompt_init_statusbar(ToxWindow *self, Tox *tox, bool first_time_run); +void prompt_init_statusbar(ToxWindow *self, Toxic *toxic, bool first_time_run); void prompt_update_nick(ToxWindow *prompt, const char *nick); -void prompt_update_statusmessage(ToxWindow *prompt, Tox *tox, const char *statusmsg); +void prompt_update_statusmessage(ToxWindow *prompt, Toxic *toxic, const char *statusmsg); void prompt_update_status(ToxWindow *prompt, Tox_User_Status status); void prompt_update_connectionstatus(ToxWindow *prompt, bool is_connected); -void kill_prompt_window(ToxWindow *self); +void kill_prompt_window(ToxWindow *self, const Client_Config *c_config); /* callback: Updates own connection status in prompt statusbar */ void on_self_connection_status(Tox *tox, Tox_Connection connection_status, void *userdata); diff --git a/src/qr_code.c b/src/qr_code.c index 8f9d48d48..fb3ff2112 100644 --- a/src/qr_code.c +++ b/src/qr_code.c @@ -1,4 +1,4 @@ -/* qr_obj_code.c +/* qr_code.c * * * Copyright (C) 2024 Toxic All Rights Reserved. diff --git a/src/settings.c b/src/settings.c index 98f416339..a048fbbbf 100644 --- a/src/settings.c +++ b/src/settings.c @@ -121,7 +121,7 @@ static struct ui_strings { "color_bar_notify", }; -static void ui_defaults(struct user_settings *settings) +static void ui_defaults(Client_Config *settings) { settings->timestamps = TIMESTAMPS_ON; snprintf(settings->timestamp_format, sizeof(settings->timestamp_format), "%s", TIMESTAMP_DEFAULT); @@ -179,7 +179,7 @@ static const struct keys_strings { }; /* defines from toxic.h */ -static void key_defaults(struct user_settings *settings) +static void key_defaults(Client_Config *settings) { settings->key_next_tab = T_KEY_NEXT; settings->key_prev_tab = T_KEY_PREV; @@ -208,7 +208,7 @@ static const struct tox_strings { "password_eval", }; -static void tox_defaults(struct user_settings *settings) +static void tox_defaults(Client_Config *settings) { strcpy(settings->download_path, ""); strcpy(settings->chatlogs_path, ""); @@ -236,7 +236,7 @@ static const struct audio_strings { "push_to_talk", }; -static void audio_defaults(struct user_settings *settings) +static void audio_defaults(Client_Config *settings) { settings->audio_in_dev = 0; settings->audio_out_dev = 0; @@ -504,7 +504,7 @@ int settings_load_friends(const char *patharg) return 0; } -int settings_load_main(struct user_settings *s, const char *patharg) +int settings_load_main(Client_Config *s, const char *patharg) { config_t cfg[1]; config_setting_t *setting = NULL; diff --git a/src/settings.h b/src/settings.h index 6822452ba..2ddc8c579 100644 --- a/src/settings.h +++ b/src/settings.h @@ -27,13 +27,15 @@ #include +#include "toxic_constants.h" + /* Represents line_* hints max strlen */ #define LINE_HINT_MAX 3 #define PASSWORD_EVAL_MAX 512 /* Holds user setting values defined in the toxic config file. */ -struct user_settings { +typedef struct Client_Config { int autolog; /* boolean */ int alerts; /* boolean */ @@ -97,9 +99,7 @@ struct user_settings { int chat_audio_channels; int push_to_talk; /* boolean */ #endif -}; - -extern struct user_settings *user_settings; +} Client_Config; enum settings_values { AUTOLOG_OFF = 0, @@ -148,7 +148,7 @@ enum settings_values { * Return -1 if we fail to open the file path. * Return -2 if libconfig fails to read the config file. */ -int settings_load_main(struct user_settings *s, const char *patharg); +int settings_load_main(Client_Config *s, const char *patharg); /* * Loads friend config settings from the toxic config file pointed to by `patharg`. diff --git a/src/term_mplex.c b/src/term_mplex.c index 9a6e0b277..041790868 100644 --- a/src/term_mplex.c +++ b/src/term_mplex.c @@ -40,7 +40,6 @@ #include "windows.h" extern struct ToxWindow *prompt; -extern struct user_settings *user_settings; extern struct Winthread Winthread; #if defined(PATH_MAX) && PATH_MAX > 512 @@ -405,7 +404,7 @@ static void mplex_timer_handler(Toxic *toxic) 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; + new_note = toxic->c_config->mplex_away_note; } else { return; } @@ -442,7 +441,7 @@ int init_mplex_away_timer(Toxic *toxic) return 0; } - if (!user_settings->mplex_away) { + if (!toxic->c_config->mplex_away) { return 0; } diff --git a/src/toxic.c b/src/toxic.c index e0ab56a5e..e3af16f86 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -101,10 +101,6 @@ struct arg_opts arg_opts; static struct av_thread av_thread; #endif -// 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 void queue_init_message(const char *msg, ...); static time_t last_signal_time; @@ -148,20 +144,13 @@ static void init_signal_catchers(void) signal(SIGSEGV, catch_SIGSEGV); } -void free_global_data(void) -{ - if (user_settings) { - free(user_settings); - user_settings = NULL; - } -} - void kill_toxic(Toxic *toxic) { Client_Data *client_data = &toxic->client_data; free(client_data->data_path); free(client_data->block_path); + free(toxic->c_config); free(toxic); } @@ -175,7 +164,7 @@ void exit_toxic_success(Toxic *toxic) terminate_notify(); - kill_all_file_transfers(toxic->tox); + kill_all_file_transfers(toxic); kill_all_windows(toxic); #ifdef AUDIO @@ -189,7 +178,6 @@ void exit_toxic_success(Toxic *toxic) terminate_python(); #endif /* PYTHON */ - free_global_data(); tox_kill(toxic->tox); if (arg_opts.log_fp != NULL) { @@ -211,8 +199,6 @@ void exit_toxic_success(Toxic *toxic) void exit_toxic_err(const char *errmsg, int errcode) { - free_global_data(); - if (freopen("/dev/tty", "w", stderr)) { fprintf(stderr, "Toxic session aborted with error code %d (%s)\n", errcode, errmsg); } @@ -278,123 +264,123 @@ void set_window_refresh_rate(size_t refresh_rate) timeout(refresh_rate); } -static void get_custom_toxic_colours(short *bar_bg_color, short *bar_fg_color, short *bar_accent_color, - short *bar_notify_color) +static void get_custom_toxic_colours(const Client_Config *c_config, short *bar_bg_color, short *bar_fg_color, + short *bar_accent_color, short *bar_notify_color) { - if (!string_is_empty(user_settings->color_bar_bg)) { - if (strcmp(user_settings->color_bar_bg, "black") == 0) { + if (!string_is_empty(c_config->color_bar_bg)) { + if (strcmp(c_config->color_bar_bg, "black") == 0) { *bar_bg_color = COLOR_BLACK; - } else if (strcmp(user_settings->color_bar_bg, "red") == 0) { + } else if (strcmp(c_config->color_bar_bg, "red") == 0) { *bar_bg_color = COLOR_RED; - } else if (strcmp(user_settings->color_bar_bg, "blue") == 0) { + } else if (strcmp(c_config->color_bar_bg, "blue") == 0) { *bar_bg_color = COLOR_BLUE; - } else if (strcmp(user_settings->color_bar_bg, "cyan") == 0) { + } else if (strcmp(c_config->color_bar_bg, "cyan") == 0) { *bar_bg_color = COLOR_CYAN; - } else if (strcmp(user_settings->color_bar_bg, "green") == 0) { + } else if (strcmp(c_config->color_bar_bg, "green") == 0) { *bar_bg_color = COLOR_GREEN; - } else if (strcmp(user_settings->color_bar_bg, "yellow") == 0) { + } else if (strcmp(c_config->color_bar_bg, "yellow") == 0) { *bar_bg_color = COLOR_YELLOW; - } else if (strcmp(user_settings->color_bar_bg, "magenta") == 0) { + } else if (strcmp(c_config->color_bar_bg, "magenta") == 0) { *bar_bg_color = COLOR_MAGENTA; - } else if (strcmp(user_settings->color_bar_bg, "white") == 0) { + } else if (strcmp(c_config->color_bar_bg, "white") == 0) { *bar_bg_color = COLOR_WHITE; - } else if (strcmp(user_settings->color_bar_bg, "gray") == 0) { + } else if (strcmp(c_config->color_bar_bg, "gray") == 0) { *bar_bg_color = CUSTOM_COLOUR_GRAY; - } else if (strcmp(user_settings->color_bar_bg, "orange") == 0) { + } else if (strcmp(c_config->color_bar_bg, "orange") == 0) { *bar_bg_color = CUSTOM_COLOUR_ORANGE; - } else if (strcmp(user_settings->color_bar_bg, "pink") == 0) { + } else if (strcmp(c_config->color_bar_bg, "pink") == 0) { *bar_bg_color = CUSTOM_COLOUR_PINK; - } else if (strcmp(user_settings->color_bar_bg, "brown") == 0) { + } else if (strcmp(c_config->color_bar_bg, "brown") == 0) { *bar_bg_color = CUSTOM_COLOUR_BROWN; } } - if (!string_is_empty(user_settings->color_bar_fg)) { - if (strcmp(user_settings->color_bar_fg, "black") == 0) { + if (!string_is_empty(c_config->color_bar_fg)) { + if (strcmp(c_config->color_bar_fg, "black") == 0) { *bar_fg_color = COLOR_BLACK; - } else if (strcmp(user_settings->color_bar_fg, "red") == 0) { + } else if (strcmp(c_config->color_bar_fg, "red") == 0) { *bar_fg_color = COLOR_RED; - } else if (strcmp(user_settings->color_bar_fg, "blue") == 0) { + } else if (strcmp(c_config->color_bar_fg, "blue") == 0) { *bar_fg_color = COLOR_BLUE; - } else if (strcmp(user_settings->color_bar_fg, "cyan") == 0) { + } else if (strcmp(c_config->color_bar_fg, "cyan") == 0) { *bar_fg_color = COLOR_CYAN; - } else if (strcmp(user_settings->color_bar_fg, "green") == 0) { + } else if (strcmp(c_config->color_bar_fg, "green") == 0) { *bar_fg_color = COLOR_GREEN; - } else if (strcmp(user_settings->color_bar_fg, "yellow") == 0) { + } else if (strcmp(c_config->color_bar_fg, "yellow") == 0) { *bar_fg_color = COLOR_YELLOW; - } else if (strcmp(user_settings->color_bar_fg, "magenta") == 0) { + } else if (strcmp(c_config->color_bar_fg, "magenta") == 0) { *bar_fg_color = COLOR_MAGENTA; - } else if (strcmp(user_settings->color_bar_fg, "white") == 0) { + } else if (strcmp(c_config->color_bar_fg, "white") == 0) { *bar_fg_color = COLOR_WHITE; - } else if (strcmp(user_settings->color_bar_fg, "gray") == 0) { + } else if (strcmp(c_config->color_bar_fg, "gray") == 0) { *bar_fg_color = CUSTOM_COLOUR_GRAY; - } else if (strcmp(user_settings->color_bar_fg, "orange") == 0) { + } else if (strcmp(c_config->color_bar_fg, "orange") == 0) { *bar_fg_color = CUSTOM_COLOUR_ORANGE; - } else if (strcmp(user_settings->color_bar_fg, "pink") == 0) { + } else if (strcmp(c_config->color_bar_fg, "pink") == 0) { *bar_fg_color = CUSTOM_COLOUR_PINK; - } else if (strcmp(user_settings->color_bar_fg, "brown") == 0) { + } else if (strcmp(c_config->color_bar_fg, "brown") == 0) { *bar_fg_color = CUSTOM_COLOUR_BROWN; } } - if (!string_is_empty(user_settings->color_bar_accent)) { - if (strcmp(user_settings->color_bar_accent, "black") == 0) { + if (!string_is_empty(c_config->color_bar_accent)) { + if (strcmp(c_config->color_bar_accent, "black") == 0) { *bar_accent_color = COLOR_BLACK; - } else if (strcmp(user_settings->color_bar_accent, "red") == 0) { + } else if (strcmp(c_config->color_bar_accent, "red") == 0) { *bar_accent_color = COLOR_RED; - } else if (strcmp(user_settings->color_bar_accent, "blue") == 0) { + } else if (strcmp(c_config->color_bar_accent, "blue") == 0) { *bar_accent_color = COLOR_BLUE; - } else if (strcmp(user_settings->color_bar_accent, "cyan") == 0) { + } else if (strcmp(c_config->color_bar_accent, "cyan") == 0) { *bar_accent_color = COLOR_CYAN; - } else if (strcmp(user_settings->color_bar_accent, "green") == 0) { + } else if (strcmp(c_config->color_bar_accent, "green") == 0) { *bar_accent_color = COLOR_GREEN; - } else if (strcmp(user_settings->color_bar_accent, "yellow") == 0) { + } else if (strcmp(c_config->color_bar_accent, "yellow") == 0) { *bar_accent_color = COLOR_YELLOW; - } else if (strcmp(user_settings->color_bar_accent, "magenta") == 0) { + } else if (strcmp(c_config->color_bar_accent, "magenta") == 0) { *bar_accent_color = COLOR_MAGENTA; - } else if (strcmp(user_settings->color_bar_accent, "white") == 0) { + } else if (strcmp(c_config->color_bar_accent, "white") == 0) { *bar_accent_color = COLOR_WHITE; - } else if (strcmp(user_settings->color_bar_accent, "gray") == 0) { + } else if (strcmp(c_config->color_bar_accent, "gray") == 0) { *bar_accent_color = CUSTOM_COLOUR_GRAY; - } else if (strcmp(user_settings->color_bar_accent, "orange") == 0) { + } else if (strcmp(c_config->color_bar_accent, "orange") == 0) { *bar_accent_color = CUSTOM_COLOUR_ORANGE; - } else if (strcmp(user_settings->color_bar_accent, "pink") == 0) { + } else if (strcmp(c_config->color_bar_accent, "pink") == 0) { *bar_accent_color = CUSTOM_COLOUR_PINK; - } else if (strcmp(user_settings->color_bar_accent, "brown") == 0) { + } else if (strcmp(c_config->color_bar_accent, "brown") == 0) { *bar_accent_color = CUSTOM_COLOUR_BROWN; } } - if (!string_is_empty(user_settings->color_bar_notify)) { - if (strcmp(user_settings->color_bar_notify, "black") == 0) { + if (!string_is_empty(c_config->color_bar_notify)) { + if (strcmp(c_config->color_bar_notify, "black") == 0) { *bar_notify_color = COLOR_BLACK; - } else if (strcmp(user_settings->color_bar_notify, "red") == 0) { + } else if (strcmp(c_config->color_bar_notify, "red") == 0) { *bar_notify_color = COLOR_RED; - } else if (strcmp(user_settings->color_bar_notify, "blue") == 0) { + } else if (strcmp(c_config->color_bar_notify, "blue") == 0) { *bar_notify_color = COLOR_BLUE; - } else if (strcmp(user_settings->color_bar_notify, "cyan") == 0) { + } else if (strcmp(c_config->color_bar_notify, "cyan") == 0) { *bar_notify_color = COLOR_CYAN; - } else if (strcmp(user_settings->color_bar_notify, "green") == 0) { + } else if (strcmp(c_config->color_bar_notify, "green") == 0) { *bar_notify_color = COLOR_GREEN; - } else if (strcmp(user_settings->color_bar_notify, "yellow") == 0) { + } else if (strcmp(c_config->color_bar_notify, "yellow") == 0) { *bar_notify_color = COLOR_YELLOW; - } else if (strcmp(user_settings->color_bar_notify, "magenta") == 0) { + } else if (strcmp(c_config->color_bar_notify, "magenta") == 0) { *bar_notify_color = COLOR_MAGENTA; - } else if (strcmp(user_settings->color_bar_notify, "white") == 0) { + } else if (strcmp(c_config->color_bar_notify, "white") == 0) { *bar_notify_color = COLOR_WHITE; - } else if (strcmp(user_settings->color_bar_notify, "gray") == 0) { + } else if (strcmp(c_config->color_bar_notify, "gray") == 0) { *bar_notify_color = CUSTOM_COLOUR_GRAY; - } else if (strcmp(user_settings->color_bar_notify, "orange") == 0) { + } else if (strcmp(c_config->color_bar_notify, "orange") == 0) { *bar_notify_color = CUSTOM_COLOUR_ORANGE; - } else if (strcmp(user_settings->color_bar_notify, "pink") == 0) { + } else if (strcmp(c_config->color_bar_notify, "pink") == 0) { *bar_notify_color = CUSTOM_COLOUR_PINK; - } else if (strcmp(user_settings->color_bar_notify, "brown") == 0) { + } else if (strcmp(c_config->color_bar_notify, "brown") == 0) { *bar_notify_color = CUSTOM_COLOUR_BROWN; } } } -static void init_term(void) +static void init_term(const Client_Config *c_config) { #if HAVE_WIDECHAR @@ -430,13 +416,13 @@ static void init_term(void) // let's try anyways } - if (user_settings->colour_theme == NATIVE_COLS) { + if (c_config->colour_theme == NATIVE_COLS) { if (assume_default_colors(-1, -1) == OK) { bg_color = -1; } } - get_custom_toxic_colours(&bar_bg_color, &bar_fg_color, &bar_accent_color, &bar_notify_color); + get_custom_toxic_colours(c_config, &bar_bg_color, &bar_fg_color, &bar_accent_color, &bar_notify_color); init_pair(WHITE, COLOR_WHITE, COLOR_BLACK); init_pair(GREEN, COLOR_GREEN, bg_color); @@ -535,10 +521,10 @@ static void cleanup_init_messages(void) free(init_messages.msgs); } -static void print_init_messages(ToxWindow *toxwin) +static void print_init_messages(ToxWindow *toxwin, const Client_Config *c_config) { for (int i = 0; i < init_messages.num; ++i) { - line_info_add(toxwin, NULL, NULL, NULL, SYS_MSG, 0, 0, init_messages.msgs[i]); + line_info_add(toxwin, c_config, NULL, NULL, NULL, SYS_MSG, 0, 0, init_messages.msgs[i]); } } @@ -633,7 +619,8 @@ static void load_conferences(Toxic *toxic) } if (type == TOX_CONFERENCE_TYPE_AV) { - line_info_add(get_window_ptr(win_idx), NULL, NULL, NULL, SYS_MSG, 0, 0, + ToxWindow *win = get_window_ptr(win_idx); + line_info_add(win, toxic->c_config, NULL, NULL, NULL, SYS_MSG, 0, 0, #ifdef AUDIO "Use \"/audio on\" to enable audio in this conference." #else @@ -694,12 +681,12 @@ static int password_prompt(char *buf, int size) /* Get the password from the eval command. * return length of password on success, 0 on failure */ -static int password_eval(char *buf, int size) +static int password_eval(const Client_Config *c_config, char *buf, int size) { buf[0] = '\0'; /* Run password_eval command */ - FILE *f = popen(user_settings->password_eval, "r"); + FILE *f = popen(c_config->password_eval, "r"); if (f == NULL) { fprintf(stderr, "Executing password_eval failed\n"); @@ -1041,7 +1028,7 @@ static bool load_tox(Toxic *toxic, struct Tox_Options *tox_opts, Tox_Err_New *ne } size_t pwlen = 0; - int pweval = user_settings->password_eval[0]; + int pweval = toxic->c_config->password_eval[0]; if (!pweval) { clear_screen(); @@ -1061,7 +1048,7 @@ static bool load_tox(Toxic *toxic, struct Tox_Options *tox_opts, Tox_Err_New *ne fflush(stdout); // Flush before prompts so the user sees the question/message if (pweval) { - pwlen = password_eval(client_data->pass, sizeof(client_data->pass)); + pwlen = password_eval(toxic->c_config, client_data->pass, sizeof(client_data->pass)); } else { pwlen = password_prompt(client_data->pass, sizeof(client_data->pass)); } @@ -1265,7 +1252,7 @@ void *thread_winref(void *data) on_window_resize(); Winthread.flag_resize = 0; } else if (draw_count >= INACTIVE_WIN_REFRESH_RATE) { - refresh_inactive_windows(); + refresh_inactive_windows(toxic->c_config); draw_count = 0; } @@ -1650,12 +1637,21 @@ static void init_default_data_files(Client_Data *client_data) static Toxic *toxic_init(void) { - Toxic *toxic = calloc(1, sizeof(Toxic)); + Toxic *toxic = (Toxic *) calloc(1, sizeof(Toxic)); if (toxic == NULL) { return NULL; } + Client_Config *tmp_settings = (Client_Config *) calloc(1, sizeof(Client_Config)); + + if (tmp_settings == NULL) { + free(toxic); + return NULL; + } + + toxic->c_config = tmp_settings; + return toxic; } @@ -1672,6 +1668,8 @@ int main(int argc, char **argv) exit_toxic_err("failed in main", FATALERR_TOXIC_INIT); } + const Client_Config *c_config = toxic->c_config; + parse_args(toxic, argc, argv); /* Use the -b flag to enable stderr */ @@ -1697,16 +1695,9 @@ int main(int argc, char **argv) first_time_encrypt(&toxic->client_data, "Encrypt existing data file? Y/n (q to quit)"); } - /* init user_settings struct and load settings from conf file */ - user_settings = calloc(1, sizeof(struct user_settings)); - - if (user_settings == NULL) { - exit_toxic_err("failed in main", FATALERR_MEMORY); - } - const char *config_path = arg_opts.config_path[0] ? arg_opts.config_path : NULL; - const int ms_ret = settings_load_main(user_settings, config_path); + const int ms_ret = settings_load_main(toxic->c_config, config_path); if (ms_ret < 0) { queue_init_message("Failed to load user settings: error %d", ms_ret); @@ -1739,10 +1730,10 @@ int main(int argc, char **argv) arg_opts.encrypt_data = 0; } - init_term(); + init_term(c_config); prompt = init_windows(toxic); - prompt_init_statusbar(prompt, toxic->tox, !datafile_exists); + prompt_init_statusbar(prompt, toxic, !datafile_exists); load_groups(toxic); load_conferences(toxic); @@ -1786,8 +1777,8 @@ int main(int argc, char **argv) exit_toxic_err("failed in main", FATALERR_THREAD_CREATE); } - set_al_device(input, user_settings->audio_in_dev); - set_al_device(output, user_settings->audio_out_dev); + set_al_device(input, c_config->audio_in_dev); + set_al_device(output, c_config->audio_out_dev); #elif SOUND_NOTIFY @@ -1810,25 +1801,25 @@ int main(int argc, char **argv) #ifdef PYTHON init_python(toxic->tox); - invoke_autoruns(prompt->chatwin->history, prompt); + invoke_autoruns(prompt->chatwin->history, prompt, c_config->autorun_path); #endif /* PYTHON */ - init_notify(60, user_settings->notification_timeout); + init_notify(60, c_config->notification_timeout); /* screen/tmux auto-away timer */ if (init_mplex_away_timer(toxic) == -1) { queue_init_message("Failed to init mplex auto-away."); } - const int nodeslist_ret = load_DHT_nodeslist(); + const int nodeslist_ret = load_DHT_nodeslist(c_config); if (nodeslist_ret != 0) { queue_init_message("DHT nodeslist failed to load (error %d)", nodeslist_ret); } pthread_mutex_lock(&Winthread.lock); - print_init_messages(prompt); + print_init_messages(prompt, c_config); flag_interface_refresh(); pthread_mutex_unlock(&Winthread.lock); @@ -1836,7 +1827,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); + snprintf(avatarstr, sizeof(avatarstr), "/avatar %s", c_config->avatar_path); execute(prompt->chatwin->history, prompt, toxic, avatarstr, GLOBAL_COMMAND_MODE); time_t last_save = get_unix_time(); @@ -1846,11 +1837,12 @@ int main(int argc, char **argv) const time_t cur_time = get_unix_time(); - if (user_settings->autosave_freq > 0 && timed_out(last_save, user_settings->autosave_freq)) { + if (c_config->autosave_freq > 0 && timed_out(last_save, c_config->autosave_freq)) { pthread_mutex_lock(&Winthread.lock); if (store_data(toxic) != 0) { - line_info_add(prompt, false, NULL, NULL, SYS_MSG, 0, RED, "WARNING: Failed to save to data file"); + line_info_add(prompt, c_config, false, NULL, NULL, SYS_MSG, 0, RED, + "WARNING: Failed to save to data file"); } pthread_mutex_unlock(&Winthread.lock); diff --git a/src/toxic.h b/src/toxic.h index 4a481c1f9..51313f855 100644 --- a/src/toxic.h +++ b/src/toxic.h @@ -48,71 +48,8 @@ #include #endif -#define UNKNOWN_NAME "Anonymous" -#define DEFAULT_TOX_NAME "Tox User" /* should always be the same as toxcore's default name */ - -#define MAX_STR_SIZE TOX_MAX_MESSAGE_LENGTH /* must be >= TOX_MAX_MESSAGE_LENGTH */ -#define MAX_CMDNAME_SIZE 64 -#define TOXIC_MAX_NAME_LENGTH 32 /* Must be <= TOX_MAX_NAME_LENGTH */ -#define KEY_IDENT_BYTES 6 /* number of hex digits to display for the public key identifier */ -#define TIME_STR_SIZE 32 -#define COLOR_STR_SIZE 10 /* should fit every color option */ - -#define NCURSES_DEFAULT_REFRESH_RATE 100 -#define NCURSES_GAME_REFRESH_RATE 25 - -#ifndef MAX_PORT_RANGE -#define MAX_PORT_RANGE 65535 -#endif - -/* ASCII key codes */ -#define T_KEY_ESC 0x1B /* ESC key */ -#define T_KEY_KILL 0x0B /* ctrl-k */ -#define T_KEY_DISCARD 0x15 /* ctrl-u */ -#define T_KEY_NEXT 0x10 /* ctrl-p */ -#define T_KEY_PREV 0x0F /* ctrl-o */ -#define T_KEY_C_E 0x05 /* ctrl-e */ -#define T_KEY_C_A 0x01 /* ctrl-a */ -#define T_KEY_C_V 0x16 /* ctrl-v */ -#define T_KEY_C_F 0x06 /* ctrl-f */ -#define T_KEY_C_H 0x08 /* ctrl-h */ -#define T_KEY_C_Y 0x19 /* ctrl-y */ -#define T_KEY_C_L 0x0C /* ctrl-l */ -#define T_KEY_C_W 0x17 /* ctrl-w */ -#define T_KEY_C_B 0x02 /* ctrl-b */ -#define T_KEY_C_T 0x14 /* ctrl-t */ -#define T_KEY_C_LEFT 0x221 /* ctrl-left arrow */ -#define T_KEY_C_RIGHT 0x230 /* ctrl-right arrow */ -#define T_KEY_C_UP 0x236 /* ctrl-up arrow */ -#define T_KEY_C_DOWN 0x20D /* ctrl-down arrow */ -#define T_KEY_TAB 0x09 /* TAB key */ - -#define ONLINE_CHAR "o" -#define OFFLINE_CHAR "o" - -typedef enum _FATAL_ERRS { - FATALERR_MEMORY = -1, /* heap memory allocation failed */ - FATALERR_FILEOP = -2, /* critical file operation failed */ - FATALERR_THREAD_CREATE = -3, /* thread creation failed for critical thread */ - FATALERR_MUTEX_INIT = -4, /* mutex init for critical thread failed */ - FATALERR_THREAD_ATTR = -5, /* thread attr object init failed */ - FATALERR_LOCALE_NOT_SET = -6, /* system locale not set */ - FATALERR_STORE_DATA = -7, /* store_data failed in critical section */ - FATALERR_INFLOOP = -8, /* infinite loop detected */ - FATALERR_WININIT = -9, /* window init failed */ - FATALERR_PROXY = -10, /* Tox network failed to init using a proxy */ - FATALERR_ENCRYPT = -11, /* Data file encryption failure */ - FATALERR_TOX_INIT = -12, /* Tox instance failed to initialize */ - FATALERR_TOXIC_INIT = -13, /* Toxic instance failed to initialize */ - FATALERR_CURSES = -14, /* Unrecoverable Ncurses error */ -} FATAL_ERRS; - -/* Fixes text color problem on some terminals. - Uncomment if necessary */ -/* #define URXVT_FIX */ - -#define MIN_PASSWORD_LEN 6 -#define MAX_PASSWORD_LEN 64 +#include "settings.h" +#include "toxic_constants.h" typedef struct Client_Data { bool is_encrypted; @@ -129,7 +66,8 @@ typedef struct Toxic { #ifdef AUDIO ToxAV *av; #endif - Client_Data client_data; + Client_Data client_data; + Client_Config *c_config; } Toxic; void lock_status(void); diff --git a/src/toxic_constants.h b/src/toxic_constants.h new file mode 100644 index 000000000..16deb8b89 --- /dev/null +++ b/src/toxic_constants.h @@ -0,0 +1,92 @@ +/* toxic_constants.h + * + * + * Copyright (C) 2024 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * + */ + +#ifndef TOXIC_CONSTANTS_H +#define TOXIC_CONSTANTS_H + +#define UNKNOWN_NAME "Anonymous" +#define DEFAULT_TOX_NAME "Tox User" /* should always be the same as toxcore's default name */ + +#define MAX_STR_SIZE TOX_MAX_MESSAGE_LENGTH /* must be >= TOX_MAX_MESSAGE_LENGTH */ +#define MAX_CMDNAME_SIZE 64 +#define TOXIC_MAX_NAME_LENGTH 32 /* Must be <= TOX_MAX_NAME_LENGTH */ +#define KEY_IDENT_BYTES 6 /* number of hex digits to display for the public key identifier */ +#define TIME_STR_SIZE 32 +#define COLOR_STR_SIZE 10 /* should fit every color option */ + +#define NCURSES_DEFAULT_REFRESH_RATE 100 +#define NCURSES_GAME_REFRESH_RATE 25 + +#ifndef MAX_PORT_RANGE +#define MAX_PORT_RANGE 65535 +#endif + +#define MIN_PASSWORD_LEN 6 +#define MAX_PASSWORD_LEN 64 + +/* Fixes text color problem on some terminals. + Uncomment if necessary */ +/* #define URXVT_FIX */ + +/* ASCII key codes */ +#define T_KEY_ESC 0x1B /* ESC key */ +#define T_KEY_KILL 0x0B /* ctrl-k */ +#define T_KEY_DISCARD 0x15 /* ctrl-u */ +#define T_KEY_NEXT 0x10 /* ctrl-p */ +#define T_KEY_PREV 0x0F /* ctrl-o */ +#define T_KEY_C_E 0x05 /* ctrl-e */ +#define T_KEY_C_A 0x01 /* ctrl-a */ +#define T_KEY_C_V 0x16 /* ctrl-v */ +#define T_KEY_C_F 0x06 /* ctrl-f */ +#define T_KEY_C_H 0x08 /* ctrl-h */ +#define T_KEY_C_Y 0x19 /* ctrl-y */ +#define T_KEY_C_L 0x0C /* ctrl-l */ +#define T_KEY_C_W 0x17 /* ctrl-w */ +#define T_KEY_C_B 0x02 /* ctrl-b */ +#define T_KEY_C_T 0x14 /* ctrl-t */ +#define T_KEY_C_LEFT 0x221 /* ctrl-left arrow */ +#define T_KEY_C_RIGHT 0x230 /* ctrl-right arrow */ +#define T_KEY_C_UP 0x236 /* ctrl-up arrow */ +#define T_KEY_C_DOWN 0x20D /* ctrl-down arrow */ +#define T_KEY_TAB 0x09 /* TAB key */ + +#define ONLINE_CHAR "o" +#define OFFLINE_CHAR "o" + +typedef enum _FATAL_ERRS { + FATALERR_MEMORY = -1, /* heap memory allocation failed */ + FATALERR_FILEOP = -2, /* critical file operation failed */ + FATALERR_THREAD_CREATE = -3, /* thread creation failed for critical thread */ + FATALERR_MUTEX_INIT = -4, /* mutex init for critical thread failed */ + FATALERR_THREAD_ATTR = -5, /* thread attr object init failed */ + FATALERR_LOCALE_NOT_SET = -6, /* system locale not set */ + FATALERR_STORE_DATA = -7, /* store_data failed in critical section */ + FATALERR_INFLOOP = -8, /* infinite loop detected */ + FATALERR_WININIT = -9, /* window init failed */ + FATALERR_PROXY = -10, /* Tox network failed to init using a proxy */ + FATALERR_ENCRYPT = -11, /* Data file encryption failure */ + FATALERR_TOX_INIT = -12, /* Tox instance failed to initialize */ + FATALERR_TOXIC_INIT = -13, /* Toxic instance failed to initialize */ + FATALERR_CURSES = -14, /* Unrecoverable Ncurses error */ +} FATAL_ERRS; + +#endif // TOXIC_CONSTANTS_H diff --git a/src/toxic_strings.c b/src/toxic_strings.c index 00e627100..3c91268b6 100644 --- a/src/toxic_strings.c +++ b/src/toxic_strings.c @@ -234,7 +234,7 @@ void add_line_to_hist(ChatContext *ctx) hst_pos is decremented or incremented depending on key_dir. resets line if at end of history */ -void fetch_hist_item(ChatContext *ctx, int key_dir) +void fetch_hist_item(const Client_Config *c_config, ChatContext *ctx, int key_dir) { if (wcscmp(ctx->line, L"\0") != 0 && ctx->hst_pos == ctx->hst_tot) { @@ -245,7 +245,7 @@ void fetch_hist_item(ChatContext *ctx, int key_dir) if (key_dir == KEY_UP) { if (--ctx->hst_pos < 0) { ctx->hst_pos = 0; - sound_notify(NULL, notif_error, NT_ALWAYS, NULL); + sound_notify(NULL, c_config, notif_error, NT_ALWAYS, NULL); } } else { if (++ctx->hst_pos >= ctx->hst_tot) { diff --git a/src/toxic_strings.h b/src/toxic_strings.h index a8f7a8f6a..7b217f57e 100644 --- a/src/toxic_strings.h +++ b/src/toxic_strings.h @@ -64,7 +64,7 @@ void add_line_to_hist(ChatContext *ctx); hst_pos is decremented or incremented depending on key_dir. resets line if at end of history */ -void fetch_hist_item(ChatContext *ctx, int key_dir); +void fetch_hist_item(const Client_Config *c_config, ChatContext *ctx, int key_dir); /* Substitutes all occurrences of old with new. */ void strsubst(char *str, char old, char new); diff --git a/src/video_call.c b/src/video_call.c index d76f30775..283a2a6dc 100644 --- a/src/video_call.c +++ b/src/video_call.c @@ -49,13 +49,15 @@ void on_video_receive_frame(ToxAV *av, uint32_t friend_number, void on_video_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t video_bit_rate, void *user_data); -static void print_err(ToxWindow *self, const char *error_str) +static void print_err(ToxWindow *self, const Client_Config *c_config, const char *error_str) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", error_str); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", error_str); } -ToxAV *init_video(ToxWindow *self, const Toxic *toxic) +ToxAV *init_video(ToxWindow *self, Toxic *toxic) { + const Client_Config *c_config = toxic->c_config; + CallControl.video_errors = ve_None; CallControl.video_enabled = true; @@ -63,12 +65,12 @@ ToxAV *init_video(ToxWindow *self, const Toxic *toxic) CallControl.video_frame_duration = 10; if (toxic->av == NULL) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Video failed to init with ToxAV instance"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Video failed to init with ToxAV instance"); return NULL; } - if (init_video_devices(toxic->av) == vde_InternalError) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to init video devices"); + if (init_video_devices(toxic->av, c_config) == vde_InternalError) { + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to init video devices"); return NULL; } @@ -94,8 +96,8 @@ void terminate_video(void) terminate_video_devices(); } -void read_video_device_callback(int16_t width, int16_t height, const uint8_t *y, const uint8_t *u, const uint8_t *v, - void *data) +void read_video_device_callback(const Client_Config *c_config, int16_t width, int16_t height, const uint8_t *y, + const uint8_t *u, const uint8_t *v, void *data) { uint32_t friend_number = *((uint32_t *)data); /* TODO: Or pass an array of call_idx's */ Call *this_call = &CallControl.calls[friend_number]; @@ -103,17 +105,17 @@ void read_video_device_callback(int16_t width, int16_t height, const uint8_t *y, /* Drop frame if video sending is disabled */ if (this_call->video_bit_rate == 0 || this_call->status != cs_Active || this_call->vin_idx == -1) { - line_info_add(CallControl.prompt, false, NULL, NULL, SYS_MSG, 0, 0, "Video frame dropped."); + line_info_add(CallControl.prompt, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Video frame dropped."); return; } if (toxav_video_send_frame(CallControl.av, friend_number, width, height, y, u, v, &error) == false) { - line_info_add(CallControl.prompt, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to send video frame"); + line_info_add(CallControl.prompt, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to send video frame"); if (error == TOXAV_ERR_SEND_FRAME_NULL) { - line_info_add(CallControl.prompt, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to capture video frame"); + line_info_add(CallControl.prompt, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to capture video frame"); } else if (error == TOXAV_ERR_SEND_FRAME_INVALID) { - line_info_add(CallControl.prompt, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to prepare video frame"); + line_info_add(CallControl.prompt, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to prepare video frame"); } } } @@ -128,25 +130,31 @@ void write_video_device_callback(uint32_t friend_number, uint16_t width, uint16_ write_video_out(width, height, y, u, v, ystride, ustride, vstride, user_data); } -int start_video_transmission(ToxWindow *self, ToxAV *av, Call *call) +int start_video_transmission(ToxWindow *self, Toxic *toxic, Call *call) { - if (!self || !av) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to prepare video transmission"); + if (self == NULL || toxic == NULL) { + return -1; + } + + const Client_Config *c_config = toxic->c_config; + + if (toxic->av == NULL) { + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to prepare video transmission"); return -1; } if (open_primary_video_device(vdt_input, &call->vin_idx, &call->video_width, &call->video_height) != vde_None) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to open input video device!"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to open input video device!"); return -1; } if (register_video_device_callback(self->num, call->vin_idx, read_video_device_callback, &self->num) != vde_None) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to register input video handler!"); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to register input video handler!"); return -1; } - if (!toxav_video_set_bit_rate(CallControl.av, self->num, call->video_bit_rate, NULL)) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to set video bit rate"); + if (!toxav_video_set_bit_rate(toxic->av, self->num, call->video_bit_rate, NULL)) { + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to set video bit rate"); return -1; } @@ -226,22 +234,20 @@ void callback_recv_video_end(uint32_t friend_number) close_video_device(vdt_output, this_call->vout_idx); this_call->vout_idx = -1; } -void callback_video_starting(uint32_t friend_number) +void callback_video_starting(Toxic *toxic, uint32_t friend_number) { Call *this_call = &CallControl.calls[friend_number]; Toxav_Err_Call_Control error = TOXAV_ERR_CALL_CONTROL_OK; - toxav_call_control(CallControl.av, friend_number, TOXAV_CALL_CONTROL_SHOW_VIDEO, &error); + toxav_call_control(toxic->av, friend_number, TOXAV_CALL_CONTROL_SHOW_VIDEO, &error); if (error == TOXAV_ERR_CALL_CONTROL_OK) { - size_t i; - - for (i = 0; i < MAX_WINDOWS_NUM; ++i) { + for (size_t i = 0; i < MAX_WINDOWS_NUM; ++i) { ToxWindow *window = get_window_ptr(i); if (window != NULL && window->is_call && window->num == friend_number) { - if (start_video_transmission(window, CallControl.av, this_call) == 0) { - line_info_add(window, NULL, NULL, NULL, SYS_MSG, 0, 0, "Video capture starting."); + if (start_video_transmission(window, toxic, this_call) == 0) { + line_info_add(window, toxic->c_config, NULL, NULL, NULL, SYS_MSG, 0, 0, "Video capture starting."); } } } @@ -265,29 +271,31 @@ void cmd_vcall(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*a UNUSED_VAR(window); UNUSED_VAR(argv); - if (toxic == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; + if (argc != 0) { - print_err(self, "Unknown arguments."); + print_err(self, c_config, "Unknown arguments."); return; } if (toxic->av == NULL) { - print_err(self, "ToxAV not supported!"); + print_err(self, c_config, "ToxAV not supported!"); return; } if (!self->stb->connection) { - print_err(self, "Friend is offline."); + print_err(self, c_config, "Friend is offline."); return; } Call *call = &CallControl.calls[self->num]; if (call->status != cs_None) { - print_err(self, "Already calling."); + print_err(self, c_config, "Already calling."); return; } @@ -295,7 +303,7 @@ void cmd_vcall(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*a call->video_bit_rate = DEFAULT_VIDEO_BIT_RATE; - place_call(self); + place_call(self, toxic); } void cmd_video(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) @@ -303,35 +311,37 @@ void cmd_video(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*a UNUSED_VAR(window); UNUSED_VAR(argv); - if (toxic == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; + Call *this_call = &CallControl.calls[self->num]; if (argc != 0) { - print_err(self, "Unknown arguments."); + print_err(self, c_config, "Unknown arguments."); return; } if (toxic->av == NULL) { - print_err(self, "ToxAV not supported!"); + print_err(self, c_config, "ToxAV not supported!"); return; } if (!self->stb->connection) { - print_err(self, "Friend is offline."); + print_err(self, c_config, "Friend is offline."); return; } if (this_call->status != cs_Active) { - print_err(self, "Not in call!"); + print_err(self, c_config, "Not in call!"); return; } if (this_call->vin_idx == -1) { this_call->video_bit_rate = DEFAULT_VIDEO_BIT_RATE; - callback_video_starting(self->num); + callback_video_starting(toxic, self->num); } else { callback_video_end(self->num); } @@ -341,19 +351,21 @@ void cmd_res(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg { UNUSED_VAR(window); - if (toxic == NULL) { + if (toxic == NULL || self == NULL) { return; } + const Client_Config *c_config = toxic->c_config; + Call *call = &CallControl.calls[self->num]; if (argc == 0) { if (call->status == cs_Active && call->vin_idx != -1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Resolution of current call: %u x %u", call->video_width, call->video_height); } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Initial resolution for video calls: %u x %u", CallControl.default_video_width, CallControl.default_video_height); } @@ -362,7 +374,7 @@ void cmd_res(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg } if (argc != 2) { - print_err(self, "Require 0 or 2 arguments."); + print_err(self, c_config, "Require 0 or 2 arguments."); return; } @@ -371,7 +383,7 @@ void cmd_res(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg const long int height = strtol(argv[2], &endh, 10); if (*endw || *endh || width < 0 || height < 0) { - print_err(self, "Invalid input"); + print_err(self, c_config, "Invalid input"); return; } @@ -380,7 +392,7 @@ void cmd_res(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg call->video_width = width; call->video_height = height; call->video_bit_rate = DEFAULT_VIDEO_BIT_RATE; - start_video_transmission(self, toxic->av, call); + start_video_transmission(self, toxic, call); } else { CallControl.default_video_width = width; CallControl.default_video_height = height; @@ -390,13 +402,18 @@ void cmd_res(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*arg void cmd_list_video_devices(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(toxic); + + if (toxic == NULL || self == NULL) { + return; + } + + const Client_Config *c_config = toxic->c_config; if (argc != 1) { if (argc < 1) { - print_err(self, "Type must be specified!"); + print_err(self, c_config, "Type must be specified!"); } else { - print_err(self, "Only one argument allowed!"); + print_err(self, c_config, "Only one argument allowed!"); } return; @@ -413,26 +430,31 @@ void cmd_list_video_devices(WINDOW *window, ToxWindow *self, Toxic *toxic, int a } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid type: %s", argv[1]); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid type: %s", argv[1]); return; } - print_video_devices(self, type); + print_video_devices(self, c_config, type); } /* This changes primary video device only */ void cmd_change_video_device(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); - UNUSED_VAR(toxic); + + if (toxic == NULL || self == NULL) { + return; + } + + const Client_Config *c_config = toxic->c_config; if (argc != 2) { if (argc < 1) { - print_err(self, "Type must be specified!"); + print_err(self, c_config, "Type must be specified!"); } else if (argc < 2) { - print_err(self, "Must have id!"); + print_err(self, c_config, "Must have id!"); } else { - print_err(self, "Only two arguments allowed!"); + print_err(self, c_config, "Only two arguments allowed!"); } return; @@ -449,7 +471,7 @@ void cmd_change_video_device(WINDOW *window, ToxWindow *self, Toxic *toxic, int } else { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid type: %s", argv[1]); + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid type: %s", argv[1]); return; } @@ -458,12 +480,12 @@ void cmd_change_video_device(WINDOW *window, ToxWindow *self, Toxic *toxic, int long int selection = strtol(argv[2], &end, 10); if (*end) { - print_err(self, "Invalid input"); + print_err(self, c_config, "Invalid input"); return; } if (set_primary_video_device(type, selection) == vde_InvalidSelection) { - print_err(self, "Invalid selection!"); + print_err(self, c_config, "Invalid selection!"); return; } } diff --git a/src/video_call.h b/src/video_call.h index 0db58b4df..5e3f73695 100644 --- a/src/video_call.h +++ b/src/video_call.h @@ -26,18 +26,17 @@ #include #include "audio_call.h" - +#include "toxic.h" #include "video_device.h" /* You will have to pass pointer to first member of 'windows' declared in windows.c */ -ToxAV *init_video(ToxWindow *self, const Toxic *toxic); +ToxAV *init_video(ToxWindow *self, Toxic *toxic); void terminate_video(void); -int start_video_transmission(ToxWindow *self, ToxAV *av, Call *call); +int start_video_transmission(ToxWindow *self, Toxic *toxic, Call *call); int stop_video_transmission(Call *call, int friend_number); void callback_recv_video_starting(uint32_t friend_number); void callback_recv_video_end(uint32_t friend_number); -void callback_video_starting(uint32_t friend_number); void callback_video_end(uint32_t friend_number); #endif /* VIDEO_CALL_H */ diff --git a/src/video_device.c b/src/video_device.c index 5cb9a7934..c213afbac 100644 --- a/src/video_device.c +++ b/src/video_device.c @@ -59,8 +59,6 @@ #define inline__ inline __attribute__((always_inline)) -extern struct user_settings *user_settings; - struct VideoBuffer { void *start; size_t length; @@ -108,7 +106,7 @@ static pthread_mutex_t video_mutex; static bool video_thread_running = true; static bool video_thread_paused = true; /* Thread control */ -void *video_thread_poll(void *); +void *video_thread_poll(void *userdata); static void yuv420tobgr(uint16_t width, uint16_t height, const uint8_t *y, const uint8_t *u, const uint8_t *v, unsigned int ystride, @@ -178,9 +176,9 @@ static int xioctl(int fh, unsigned long request, void *arg) /* Meet devices */ #ifdef VIDEO -VideoDeviceError init_video_devices(ToxAV *av_) +VideoDeviceError init_video_devices(ToxAV *av_, const Client_Config *c_config) #else -VideoDeviceError init_video_devices(void) +VideoDeviceError init_video_devices(const Client_Config *c_config) #endif /* VIDEO */ { size[vdt_input] = 0; @@ -254,7 +252,8 @@ VideoDeviceError init_video_devices(void) pthread_t thread_id; - if (pthread_create(&thread_id, NULL, video_thread_poll, NULL) != 0 || pthread_detach(thread_id) != 0) { + if (pthread_create(&thread_id, NULL, video_thread_poll, (void *) c_config) != 0 + || pthread_detach(thread_id) != 0) { return vde_InternalError; } @@ -681,12 +680,17 @@ VideoDeviceError write_video_out(uint16_t width, uint16_t height, return vde_None; } -void *video_thread_poll(void *arg) // TODO: maybe use thread for every input source +void *video_thread_poll(void *userdata) // TODO: maybe use thread for every input source { /* * NOTE: We only need to poll input devices for data. */ - UNUSED_VAR(arg); + const Client_Config *c_config = (Client_Config *) userdata; + + if (c_config == NULL) { + pthread_exit(NULL); + } + uint32_t i; while (1) { @@ -741,7 +745,7 @@ void *video_thread_poll(void *arg) // TODO: maybe use thread for every input so /* Send frame data to friend through ToxAV */ if (device->cb) { - device->cb(video_width, video_height, y, u, v, device->cb_data); + device->cb(c_config, video_width, video_height, y, u, v, device->cb_data); } /* Convert YUV420 data to BGR */ @@ -862,12 +866,10 @@ VideoDeviceError close_video_device(VideoDeviceType type, uint32_t device_idx) return rc; } -void print_video_devices(ToxWindow *self, VideoDeviceType type) +void print_video_devices(ToxWindow *self, const Client_Config *c_config, VideoDeviceType type) { - int i; - - for (i = 0; i < size[type]; ++i) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%d: %s", i, video_devices_names[type][i]); + for (int i = 0; i < size[type]; ++i) { + line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%d: %s", i, video_devices_names[type][i]); } return; diff --git a/src/video_device.h b/src/video_device.h index 4a18d2e16..361cecc5b 100644 --- a/src/video_device.h +++ b/src/video_device.h @@ -24,7 +24,10 @@ #define VIDEO_DEVICE_H #define MAX_DEVICES 32 + #include + +#include "settings.h" #include "windows.h" typedef enum VideoDeviceType { @@ -45,13 +48,13 @@ typedef enum VideoDeviceError { vde_CaptureError = -9, } VideoDeviceError; -typedef void (*VideoDataHandleCallback)(int16_t width, int16_t height, const uint8_t *y, const uint8_t *u, - const uint8_t *v, void *data); +typedef void (*VideoDataHandleCallback)(const Client_Config *c_config, int16_t width, int16_t height, + const uint8_t *y, const uint8_t *u, const uint8_t *v, void *data); #ifdef VIDEO -VideoDeviceError init_video_devices(ToxAV *av); +VideoDeviceError init_video_devices(ToxAV *av_, const Client_Config *c_config); #else -VideoDeviceError init_video_devices(void); +VideoDeviceError init_video_devices(const Client_Config *c_config); #endif /* VIDEO */ VideoDeviceError terminate_video_devices(void); @@ -74,7 +77,7 @@ VideoDeviceError close_video_device(VideoDeviceType type, uint32_t device_idx); VideoDeviceError write_video_out(uint16_t width, uint16_t height, uint8_t const *y, uint8_t const *u, uint8_t const *v, int32_t ystride, int32_t ustride, int32_t vstride, void *user_data); -void print_video_devices(ToxWindow *self, VideoDeviceType type); +void print_video_devices(ToxWindow *self, const Client_Config *c_config, VideoDeviceType type); void get_primary_video_device_name(VideoDeviceType type, char *buf, int size); VideoDeviceError video_selection_valid(VideoDeviceType type, int32_t selection); diff --git a/src/windows.c b/src/windows.c index de837d1ff..0236e10cc 100644 --- a/src/windows.c +++ b/src/windows.c @@ -67,7 +67,7 @@ void on_friend_connection_status(Tox *tox, uint32_t friendnumber, Tox_Connection UNUSED_VAR(tox); Toxic *toxic = (Toxic *) userdata; - on_avatar_friend_connection_status(toxic->tox, friendnumber, connection_status); + on_avatar_friend_connection_status(toxic, friendnumber, connection_status); for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i] != NULL && windows[i]->onConnectionChange != NULL) { @@ -83,7 +83,7 @@ void on_friend_typing(Tox *tox, uint32_t friendnumber, bool is_typing, void *use UNUSED_VAR(tox); Toxic *toxic = (Toxic *) userdata; - if (user_settings->show_typing_other == SHOW_TYPING_OFF) { + if (toxic->c_config->show_typing_other == SHOW_TYPING_OFF) { return; } @@ -265,7 +265,7 @@ void on_file_chunk_request(Tox *tox, uint32_t friendnumber, uint32_t filenumber, } if (ft->file_type == TOX_FILE_KIND_AVATAR) { - on_avatar_chunk_request(tox, ft, position, length); + on_avatar_chunk_request(toxic, ft, position, length); return; } @@ -307,7 +307,7 @@ void on_file_recv_control(Tox *tox, uint32_t friendnumber, uint32_t filenumber, } if (ft->file_type == TOX_FILE_KIND_AVATAR) { - on_avatar_file_control(tox, ft, control); + on_avatar_file_control(toxic, ft, control); return; } @@ -386,6 +386,8 @@ void on_lossless_custom_packet(Tox *tox, uint32_t friendnumber, const uint8_t *d break; } +#else + UNUSED_VAR(toxic); #endif // GAMES default: { @@ -691,11 +693,11 @@ void set_active_window_index(uint8_t index) /* Displays the next window if `ch` is equal to the next window key binding. * Otherwise displays the previous window. */ -static void set_next_window(int ch) +static void set_next_window(const Client_Config *c_config, int ch) { uint8_t index = 0; - if (ch == user_settings->key_next_tab) { + if (ch == c_config->key_next_tab) { for (uint8_t i = active_window_index + 1; i < MAX_WINDOWS_NUM; ++i) { if (windows[i] != NULL) { index = i; @@ -717,9 +719,9 @@ static void set_next_window(int ch) } /* Deletes window w and cleans up */ -void del_window(ToxWindow *w) +void del_window(ToxWindow *w, const Client_Config *c_config) { - uint8_t idx = w->index; + const uint8_t idx = w->index; delwin(w->window_bar); delwin(w->window); free(w); @@ -729,11 +731,11 @@ void del_window(ToxWindow *w) refresh(); if (num_active_windows > 0) { - if (active_window_index == 2) { // if closing current window would bring us to friend list - set_next_window(-1); // skip back to the home window instead. FIXME: magic numbers + if (active_window_index == 2) { // if closing current window would bring us to friend list + set_next_window(c_config, -1); // skip back to the home window instead. FIXME: magic numbers } - set_next_window(-1); + set_next_window(c_config, -1); --num_active_windows; } @@ -1061,6 +1063,7 @@ void draw_active_window(Toxic *toxic) return; } + const Client_Config *c_config = toxic->c_config; ToxWindow *a = windows[active_window_index]; if (a == NULL) { @@ -1107,8 +1110,8 @@ void draw_active_window(Toxic *toxic) flag_interface_refresh(); pthread_mutex_unlock(&Winthread.lock); - if (ch == user_settings->key_next_tab || ch == user_settings->key_prev_tab) { - set_next_window(ch); + if (ch == c_config->key_next_tab || ch == c_config->key_prev_tab) { + set_next_window(c_config, ch); } a->onKey(a, toxic, ch, false); // we lock only when necessary in the onKey callback @@ -1129,8 +1132,8 @@ void draw_active_window(Toxic *toxic) flag_interface_refresh(); pthread_mutex_unlock(&Winthread.lock); - if (printable == 0 && (ch == user_settings->key_next_tab || ch == user_settings->key_prev_tab)) { - set_next_window((int) ch); + if (printable == 0 && (ch == c_config->key_next_tab || ch == c_config->key_prev_tab)) { + set_next_window(c_config, (int) ch); return; } else if ((printable == 0) && (a->type != WINDOW_TYPE_FRIEND_LIST)) { pthread_mutex_lock(&Winthread.lock); @@ -1157,7 +1160,7 @@ void draw_active_window(Toxic *toxic) /* Refresh inactive windows to prevent scrolling bugs. * Call at least once per second. */ -void refresh_inactive_windows(void) +void refresh_inactive_windows(const Client_Config *c_config) { for (uint8_t i = 0; i < MAX_WINDOWS_NUM; ++i) { ToxWindow *toxwin = windows[i]; @@ -1168,7 +1171,7 @@ void refresh_inactive_windows(void) if ((i != active_window_index) && (toxwin->type != WINDOW_TYPE_FRIEND_LIST)) { pthread_mutex_lock(&Winthread.lock); - line_info_print(toxwin); + line_info_print(toxwin, c_config); pthread_mutex_unlock(&Winthread.lock); } } @@ -1227,6 +1230,8 @@ size_t get_num_active_windows_type(WINDOW_TYPE type) /* destroys all chat and conference windows (should only be called on shutdown) */ void kill_all_windows(Toxic *toxic) { + const Client_Config *c_config = toxic->c_config; + for (size_t i = 2; i < MAX_WINDOWS_NUM; ++i) { ToxWindow *w = windows[i]; @@ -1241,22 +1246,22 @@ void kill_all_windows(Toxic *toxic) } case WINDOW_TYPE_CONFERENCE: { - free_conference(w, w->num); + free_conference(w, c_config, w->num); break; } #ifdef GAMES case WINDOW_TYPE_GAME: { - game_kill(w); + game_kill(w, c_config); break; } #endif // GAMES case WINDOW_TYPE_GROUPCHAT: { - exit_groupchat(w, toxic, w->num, user_settings->group_part_message, - strlen(user_settings->group_part_message)); + exit_groupchat(w, toxic, w->num, c_config->group_part_message, + strlen(c_config->group_part_message)); break; } @@ -1267,6 +1272,6 @@ void kill_all_windows(Toxic *toxic) } /* TODO: use enum instead of magic indices */ - kill_friendlist(windows[1]); - kill_prompt_window(windows[0]); + kill_friendlist(windows[1], c_config); + kill_prompt_window(windows[0], c_config); } diff --git a/src/windows.h b/src/windows.h index 11257aaac..995476d80 100644 --- a/src/windows.h +++ b/src/windows.h @@ -34,6 +34,7 @@ #include #endif /* AUDIO */ +#include "settings.h" #include "toxic.h" #define MAX_WINDOWS_NUM 20 @@ -237,7 +238,6 @@ struct ToxWindow { 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); @@ -353,7 +353,7 @@ struct Help { ToxWindow *init_windows(Toxic *toxic); void draw_active_window(Toxic *toxic); int add_window(Toxic *toxic, ToxWindow *w); -void del_window(ToxWindow *w); +void del_window(ToxWindow *w, const Client_Config *c_config); void set_active_window_index(uint8_t index); int get_num_active_windows(void); void kill_all_windows(Toxic *toxic); /* should only be called on shutdown */ @@ -368,7 +368,6 @@ size_t get_num_active_windows_type(WINDOW_TYPE type); /* refresh inactive windows to prevent scrolling bugs. call at least once per second */ -void refresh_inactive_windows(void); +void refresh_inactive_windows(const Client_Config *c_config); #endif // WINDOWS_H -