Hexaoxide / Carbon

Carbon is a modern chat plugin built on channels, with just about every single setting and format configurable.
https://modrinth.com/mod/carbon
GNU General Public License v3.0
126 stars 40 forks source link

[Bug] Username cache not refreshed #198

Closed Prof-Bloodstone closed 2 years ago

Prof-Bloodstone commented 2 years ago

Bug Description:

With the default JSON config storage, CarbonChat stores user settings in plugins/CarbonChat/users/<UUID>.json. One of the things stored there is username. Unfortunately it looks like it is never invalidated - thus changing your name will make Carbon still use your old name for months.

What is not working as it should?

New user name should be used after changing account names (preferably with configurable cache time, or just falling back to use Paper's usercache.json). Since this name is used, I believe there might be another bug - that display_name is not respected so if other plugins modify it, it won't be used.

Steps to reproduce:

  1. Create default installation (Carbon, Vault, LP)
  2. Log in to server and log out
  3. Either change your name for your account, or simply modify your user entry
  4. Always the value from users/ dir is used, no matter how long ago it was changed

System Details:

  1. Server Type: Bukkit
  2. Server Software: Paper 259
  3. MC Version: 1.18.2
  4. Carbon Version: 2.0.9 (jar checksum 95c58a7b0696e226db547d3eb9e811d4a62260ce, downloaded March 16th)
Prof-Bloodstone commented 2 years ago

Unfortunately it looks like /carbon reload doesn't reload these files. My workaround for now is to hook the following script into the server restart routine:

fix_carbon_usernames.sh ```sh #!/usr/bin/env bash # Workaround https://github.com/Hexaoxide/Carbon/issues/198 set -euo pipefail readonly scriptpath="$(realpath "${0}")" readonly scriptdir="$(dirname "${scriptpath}")" readonly gitroot="$(git -C "${scriptdir}" rev-parse --show-toplevel)" cd "${gitroot}" readonly server_dir="${gitroot}/server-config/survival" readonly usercache_file="${server_dir}/usercache.json" readonly users_dir="${server_dir}/plugins/CarbonChat/users" get_usercache_username() { local -r uuid="${1?}" local jq_args=( --raw-output # Print raw string instead of quoted JSON string --exit-status # If no match found, exit with error --arg uuid "${uuid}" # Assing UUID to $uuid 'map(select(.uuid == $uuid)) | first | .name' # Select first entry with matching uuid ) jq "${jq_args[@]}" "${usercache_file}" } get_carbon_username() { local -r file="${1?}" jq --raw-output '.username' "${file}" } set_carbon_username() { local -r file="${1?}" local -r username="${2?}" local -r tmp_file="${file}.tmp" jq --arg username "${username}" '. + {username: $username}' "${file}" >| "${tmp_file}" mv "${tmp_file}" "${file}" } changed=() for user_file in "${users_dir}"/*.json; do file_name="$(basename "${user_file}")" uuid="${file_name%.json}" username="$(get_usercache_username "${uuid}")" carbon_username=$(get_carbon_username "${user_file}") if [ "${username}" != "${carbon_username}" ]; then change="${carbon_username}->${username}" changed+=( "${change}" ) echo "Changing names: ${change}" set_carbon_username "${user_file}" "${username}" fi done no_changes="${#changed[@]}" if [ "${no_changes}" -gt 0 ]; then echo "Changed ${no_changes} usernames, you should restart the server :)" fi ```
Draycia commented 2 years ago

Fixed in 6f157ce07dbe4ac811f43ebde010c04d2bc2d80e

Regarding the /carbon reload, is a flag for that command or other means to force-refresh the user cache something that's wanted?

Prof-Bloodstone commented 2 years ago

If I understand correctly, https://github.com/Hexaoxide/Carbon/commit/6f157ce07dbe4ac811f43ebde010c04d2bc2d80e doesn't actually provide a fix, just a command to force the username update, right?

is a flag for that command or other means to force-refresh the user cache something that's wanted?

It'd be nice for when there's an issue to implement a workaround like this, but I don't think it's high priority :) As it's now, I'll still use my workaround. Being able to run it more often than my restart cycle would be nice, but it's good enough.

Draycia commented 2 years ago

The fix is to make the username transient. It's in the commit I linked. Other users have confirmed it fixing the issue.