]> jxnshi.xyz Git - mesange.git/commitdiff
Add profile config value
authorjxnshi <jxnshi@cock.li>
Mon, 3 Mar 2025 12:52:32 +0000 (13:52 +0100)
committerjxnshi <jxnshi@cock.li>
Mon, 3 Mar 2025 12:52:32 +0000 (13:52 +0100)
client-cli/client-cli
client-cli/command.odin
client-cli/config.odin
client-cli/main.odin
client-cli/profile.odin
client-cli/state.odin
server/server

index 1d90f4273c3a29a296b84de020f2692faf735255..0fa5051ce0226045fdaa0a3ca415911d1df0bad6 100755 (executable)
Binary files a/client-cli/client-cli and b/client-cli/client-cli differ
index 3f7b51cdf015dda6eae8799b693720f2b2db4516..113a19d7a4c7870baf648f65cc85ec259732ffab 100644 (file)
@@ -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
index 0552629e2478c6eb4fb1324da81d3a226837103c..41b20a2fc628fd502f7a2c6005ae1e77507e45ab 100644 (file)
@@ -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)
+}
index f33fd6b3a2adeccbe62bdb67618f08b2fe0e096e..ecc845dd94920d6dc23eb094190d37fc3e4fc074 100644 (file)
@@ -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)
index 2171c80e07e08dbc636f2eb6ff35cc57882a4e1d..07651cadebd88e0ffa610dc3cb476b96c5e86718 100644 (file)
@@ -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)
+}
index a4d4a7f3a6a537a2a89d52879a5d81fc8f754d06..03d9382e19011bf5c26f8b4174c315cd9ca4ecab 100644 (file)
@@ -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.")
index f85740603d4ca98e812c6bd5c15fcb15c1dca19e..886322106591c3ddaa22594d33c908e2bf34ee09 100755 (executable)
Binary files a/server/server and b/server/server differ