From: jxnshi Date: Mon, 3 Mar 2025 12:52:32 +0000 (+0100) Subject: Add profile config value X-Git-Url: https://jxnshi.xyz/repos?a=commitdiff_plain;h=08f22c7feb52c62f2481c8b196c2acf812451be7;p=mesange.git Add profile config value --- diff --git a/client-cli/client-cli b/client-cli/client-cli index 1d90f42..0fa5051 100755 Binary files a/client-cli/client-cli and b/client-cli/client-cli differ diff --git a/client-cli/command.odin b/client-cli/command.odin index 3f7b51c..113a19d 100644 --- a/client-cli/command.odin +++ b/client-cli/command.odin @@ -49,7 +49,7 @@ handle_command :: proc(app: ^App, command: string) -> Maybe(Handle_Command_Error return nil case ":del", ":delete": - profile_filename := strings.concatenate({ app.config.selected_profile.?, ".json" }, context.temp_allocator) + profile_filename := strings.concatenate({ app.config.?.selected_profile.?, ".json" }, context.temp_allocator) profile_path := fpath.join({ app.profiles_path, profile_filename }, context.temp_allocator) os.remove(profile_path) @@ -114,7 +114,7 @@ handle_command :: proc(app: ^App, command: string) -> Maybe(Handle_Command_Error room_name = room_name_bytes, } - err := request_host(app, request, &app.profile.private_key, nil) + err := request_host(app, request, &(&app.profile.?).private_key, nil) if err != nil { app_set_info_bar(app, "Could not subscribe to room.") @@ -161,7 +161,7 @@ handle_command :: proc(app: ^App, command: string) -> Maybe(Handle_Command_Error } } - err := request_host(app, request, &app.profile.private_key, callback) + err := request_host(app, request, &(&app.profile.?).private_key, callback) if err != nil { app_set_info_bar(app, "Could not subscribe to room.") @@ -174,7 +174,7 @@ handle_command :: proc(app: ^App, command: string) -> Maybe(Handle_Command_Error return nil case ":k", ":key": - selected_room := &app.profile.rooms[app.selected_room] + selected_room := &(&app.profile.?).rooms[app.selected_room] key_string_buffer: [44]u8 key_string_builder := strings.builder_from_bytes(key_string_buffer[:]) @@ -203,13 +203,13 @@ handle_command :: proc(app: ^App, command: string) -> Maybe(Handle_Command_Error return nil } - room := &app.profile.rooms[app.selected_room] + room := &(&app.profile.?).rooms[app.selected_room] request := common.Unsubscribe_Request{ room_id = room_get_id(room), } - err := request_host(app, request, &app.profile.private_key, nil) + err := request_host(app, request, &(&app.profile.?).private_key, nil) if err != nil { app_set_info_bar(app, "Could not subscribe to room.") @@ -240,16 +240,44 @@ handle_command :: proc(app: ^App, command: string) -> Maybe(Handle_Command_Error return nil case "hosts": - app_set_info_bar(app, "%v", app.profile.hosts) + app_set_info_bar(app, "%v", app.profile.?.hosts) return nil - case: - app_set_info_bar(app, "Invalid field name.") - return .Invalid_Arguments + case "profile": + app_set_info_bar(app, "%v", app.config.?.selected_profile.?) + return nil + } + } else if len(args) == 2 { + value := args[1] + + switch field_name { + case "seed_phrqse": + app_set_info_bar(app, "TODO") + return nil + + case "hosts": + sync.mutex_lock(&app.data_mutex) + defer sync.mutex_unlock(&app.data_mutex) + + profile_set_hosts(&app.profile.?, value) + + return nil + + case "profile": + sync.mutex_lock(&app.data_mutex) + defer sync.mutex_unlock(&app.data_mutex) + + config_set_selected_profile(&app.config.?, value) + config_update(app.config.?, app) + + // app_set_state(app, .Load_Config) + + return nil } } - return nil + app_set_info_bar(app, "Invalid field name.") + return .Invalid_Arguments case ":q", ":quit": app.running = false diff --git a/client-cli/config.odin b/client-cli/config.odin index 0552629..41b20a2 100644 --- a/client-cli/config.odin +++ b/client-cli/config.odin @@ -84,3 +84,8 @@ config_update :: proc(config: Config, app: ^App) { return } } + +config_set_selected_profile :: proc(config: ^Config, selected_profile: string) { + delete(config.selected_profile.?) + config.selected_profile = strings.clone(selected_profile) +} diff --git a/client-cli/main.odin b/client-cli/main.odin index f33fd6b..ecc845d 100644 --- a/client-cli/main.odin +++ b/client-cli/main.odin @@ -55,9 +55,9 @@ App :: struct { info_bar_content: string, - config: Config, + config: Maybe(Config), profile_password: string, - profile: Profile, + profile: Maybe(Profile), seed_phrase_entropy: u128, seed_phrase_checksum: u8, @@ -134,13 +134,17 @@ app_deinit :: proc(app: ^App) { net.close(host) } - profile_deinit(app.profile) + if profile, ok := app.profile.?; ok { + profile_deinit(profile) + } if app.profile_password != "" { delete(app.profile_password) } - config_deinit(app.config) + if config, ok := app.config.?; ok { + config_deinit(config) + } if app.info_bar_content != "" { delete(app.info_bar_content) @@ -183,7 +187,7 @@ app_update_room_list_window :: proc(app: ^App) { c_room_name_buffer: [common.ROOM_NAME_SIZE + 1]u8 - for &room, i in app.profile.rooms { + for &room, i in app.profile.?.rooms { room_color := nc.COLOR_PAIR(3) if i == app.selected_room { @@ -718,7 +722,7 @@ app_reset_seed_phrase :: proc(app: ^App) { app_room_key_exists :: proc(app: ^App, key: [common.ROOM_KEY_SIZE]u8) -> bool { key := key - for &room in app.profile.rooms { + for &room in app.profile.?.rooms { if slice.equal(room.key[:], key[:]) { return true } @@ -734,8 +738,8 @@ app_add_room :: proc(app: ^App, room: Room) { sync.mutex_lock(&app.data_mutex) defer sync.mutex_unlock(&app.data_mutex) - append(&app.profile.rooms, room) - profile_update(app.profile, app) + append(&(&app.profile.?).rooms, room) + profile_update(app.profile.?, app) } app_update_room_list_window(app) @@ -746,13 +750,13 @@ app_remove_room :: proc(app: ^App, index: int) { sync.mutex_lock(&app.data_mutex) defer sync.mutex_unlock(&app.data_mutex) - ordered_remove(&app.profile.rooms, index) + ordered_remove(&(&app.profile.?).rooms, index) - if app.selected_room != 0 && app.selected_room >= len(app.profile.rooms) { - app.selected_room = len(app.profile.rooms) - 1 + if app.selected_room != 0 && app.selected_room >= len(app.profile.?.rooms) { + app.selected_room = len(app.profile.?.rooms) - 1 } - profile_update(app.profile, app) + profile_update(app.profile.?, app) } app_update_room_list_window(app) diff --git a/client-cli/profile.odin b/client-cli/profile.odin index 2171c80..07651ca 100644 --- a/client-cli/profile.odin +++ b/client-cli/profile.odin @@ -179,7 +179,7 @@ profile_load_from_name :: proc(name: string, app: ^App) -> (Profile, Maybe(Profi } profile_update :: proc(profile: Profile, app: ^App) { - profile_filename := strings.concatenate({ app.config.selected_profile.?, ".json" }, context.temp_allocator) + profile_filename := strings.concatenate({ app.config.?.selected_profile.?, ".json" }, context.temp_allocator) profile_path := fpath.join({ app.profiles_path, profile_filename }, context.temp_allocator) profile_file, err1 := os.open(profile_path, os.O_WRONLY | os.O_CREATE | os.O_TRUNC, 0o664) @@ -305,3 +305,11 @@ profile_set_private_key_from_seed_phrase :: proc(profile: ^Profile, app: ^App, s return nil } + +profile_set_hosts :: proc(profile: ^Profile, hosts: string) { + if len(profile.hosts) != 0 { + delete(profile.hosts) + } + + profile.hosts, _ = strings.clone(hosts) +} diff --git a/client-cli/state.odin b/client-cli/state.odin index a4d4a7f..03d9382 100644 --- a/client-cli/state.odin +++ b/client-cli/state.odin @@ -54,6 +54,10 @@ state_load_config :: proc(app: ^App) { sync.mutex_lock(&app.data_mutex) defer sync.mutex_unlock(&app.data_mutex) + if config_, ok := app.config.?; ok { + config_deinit(config_) + } + app.config = config } @@ -84,7 +88,7 @@ state_invalid_config :: proc(app: ^App) { } state_load_profile :: proc(app: ^App) { - selected_profile, ok := app.config.selected_profile.? + selected_profile, ok := app.config.?.selected_profile.? if !ok { app_set_state(app, .Ask_Profile) @@ -121,6 +125,10 @@ state_load_profile :: proc(app: ^App) { sync.mutex_lock(&app.data_mutex) defer sync.mutex_unlock(&app.data_mutex) + if profile, ok := app.profile.?; ok { + profile_deinit(profile) + } + app.profile = profile } @@ -178,10 +186,10 @@ state_ask_profile :: proc(app: ^App) { sync.mutex_lock(&app.data_mutex) defer sync.mutex_unlock(&app.data_mutex) - app.config.selected_profile = input + (&app.config.?).selected_profile = input } - config_update(app.config, app) + config_update(app.config.?, app) app_set_state(app, .Load_Profile) } @@ -291,7 +299,7 @@ state_ask_profile_seed_phrase :: proc(app: ^App) { return } - err2 := profile_set_private_key_from_seed_phrase(&app.profile, app, input) + err2 := profile_set_private_key_from_seed_phrase(&app.profile.?, app, input) if err2 != nil { log.errorf("Invalid seed phrase with error %v", err2) @@ -325,7 +333,7 @@ state_ask_profile_hosts :: proc(app: ^App) { sync.mutex_lock(&app.data_mutex) defer sync.mutex_unlock(&app.data_mutex) - app.profile.hosts = input + profile_set_hosts(&app.profile.?, input) } app_set_state(app, .Ask_Profile_Set_Password) @@ -378,7 +386,7 @@ state_ask_profile_confirm_password :: proc(app: ^App) { return } - profile_update(app.profile, app) + profile_update(app.profile.?, app) app_set_state(app, .Connect_To_Host) } @@ -387,7 +395,7 @@ state_connect_to_host :: proc(app: ^App) { app_set_info_bar(app, "Connecting to host...") maybe_host: Maybe(net.TCP_Socket) - hosts := strings.clone(app.profile.hosts, context.temp_allocator) + hosts := strings.clone(app.profile.?.hosts, context.temp_allocator) for host_address in strings.split_iterator(&hosts, " ") { host_endpoint: net.Endpoint @@ -460,10 +468,8 @@ state_invalid_hosts :: proc(app: ^App) { return } - delete(app.profile.hosts) - app.profile.hosts = input - - profile_update(app.profile, app) + profile_set_hosts(&app.profile.?, input) + profile_update(app.profile.?, app) app_set_state(app, .Connect_To_Host) } @@ -499,7 +505,7 @@ state_room_list :: proc(app: ^App) { } case Event.down: - if app.selected_room + 1 != len(app.profile.rooms) { + if app.selected_room + 1 != len(app.profile.?.rooms) { app.selected_room += 1 app_update_room_list_window(app) } @@ -530,7 +536,7 @@ state_room :: proc(app: ^App) { return } - selected_room := &app.profile.rooms[app.selected_room] + selected_room := &(&app.profile.?).rooms[app.selected_room] iv: [chacha.IV_SIZE]u8 _ = rand.read(iv[:]) @@ -549,7 +555,7 @@ state_room :: proc(app: ^App) { message = encrypted_message, } - err := request_host(app, request, &app.profile.private_key, nil) + err := request_host(app, request, &(&app.profile.?).private_key, nil) if err != nil { app_set_info_bar(app, "Failed to send message.") diff --git a/server/server b/server/server index f857406..8863221 100755 Binary files a/server/server and b/server/server differ