aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2022-12-02 20:02:45 +0100
committerdec05eba <dec05eba@protonmail.com>2022-12-02 20:02:45 +0100
commit3aacdd395f20fa182413be92a49cbd5c5a780ab9 (patch)
tree228a44c0b51040c39459f09d011a0a85b0c26ed9
parentc15aa4decf523b81d87925942642507fe7d02848 (diff)
Matrix: extract username if login is email or user id
-rw-r--r--plugins/Matrix.hpp4
-rw-r--r--src/QuickMedia.cpp14
-rw-r--r--src/plugins/Matrix.cpp12
-rw-r--r--tests/main.cpp9
4 files changed, 31 insertions, 8 deletions
diff --git a/plugins/Matrix.hpp b/plugins/Matrix.hpp
index 0801418..c1ee3bd 100644
--- a/plugins/Matrix.hpp
+++ b/plugins/Matrix.hpp
@@ -31,6 +31,10 @@ namespace QuickMedia {
std::string pantalaimon_url_to_homeserver_url(Matrix *matrix, const std::string &url);
Message* get_latest_message_in_edit_chain(Message *message);
bool matrix_gpg_encrypt_for_each_user_in_room(Matrix *matrix, RoomData *room, const std::string &my_gpg_user_id, const std::string &str, std::string &encrypted_str);
+ // Returns empty string on error
+ std::string extract_user_name_from_user_id(const std::string &user_id);
+ // Returns empty string on error
+ std::string extract_user_name_from_email(const std::string &email);
struct MatrixChatBodyDecryptJob {
enum class DecryptState {
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp
index 81b9296..2c40580 100644
--- a/src/QuickMedia.cpp
+++ b/src/QuickMedia.cpp
@@ -5302,11 +5302,15 @@ namespace QuickMedia {
homeserver = "https://" + homeserver;
std::string err_msg;
- std::string username = login_inputs->inputs[0]->get_text();
- size_t at_index = username.find('@');
- if(at_index != std::string::npos)
- username.erase(username.begin() + at_index, username.end());
- if(matrix->login(username, login_inputs->inputs[1]->get_text(), homeserver, err_msg) == PluginResult::OK) {
+ std::string username = strip(login_inputs->inputs[0]->get_text());
+ std::string username_matrix_id = extract_user_name_from_user_id(username);
+ if(username_matrix_id.empty()) {
+ username_matrix_id = extract_user_name_from_email(username);
+ if(username_matrix_id.empty())
+ username_matrix_id = username;
+ }
+
+ if(matrix->login(username_matrix_id, login_inputs->inputs[1]->get_text(), homeserver, err_msg) == PluginResult::OK) {
login_finish();
return PluginResult::OK;
} else {
diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp
index 688e16e..0d93bd0 100644
--- a/src/plugins/Matrix.cpp
+++ b/src/plugins/Matrix.cpp
@@ -3476,14 +3476,20 @@ namespace QuickMedia {
return message;
}
- // Returns empty string on error
- static std::string extract_user_name_from_user_id(const std::string &user_id) {
+ std::string extract_user_name_from_user_id(const std::string &user_id) {
size_t index = user_id.find(':');
- if(index == std::string::npos || index == 0 || user_id.empty() || user_id[0] != '@')
+ if(index == std::string::npos || index == 0 || user_id[0] != '@')
return "";
return user_id.substr(1, index - 1);
}
+ std::string extract_user_name_from_email(const std::string &email) {
+ size_t index = email.find('@');
+ if(index == std::string::npos || index == 0)
+ return "";
+ return email.substr(0, index);
+ }
+
static std::string combine_user_display_names_for_room_name(std::vector<std::shared_ptr<UserInfo>> &user_info, const std::string &fallback_user_id) {
std::string result;
if(user_info.size() == 0)
diff --git a/tests/main.cpp b/tests/main.cpp
index 41ef45d..0eae9ba 100644
--- a/tests/main.cpp
+++ b/tests/main.cpp
@@ -2,6 +2,7 @@
#include <string.h>
#include "../include/NetUtils.hpp"
#include "../plugins/utils/EpisodeNameParser.hpp"
+#include "../plugins/Matrix.hpp"
#include "../generated/Emoji.hpp"
#define assert_fail(str) do { fprintf(stderr, "Assert failed on line %d, reason: %s\n", __LINE__, (str)); exit(1); } while(0)
@@ -112,5 +113,13 @@ int main() {
assert_equals(emoji_sequence_length, 4);
assert_equals(emoji_sequence_byte_length, 13);
+ assert_equals(extract_user_name_from_user_id("@dec05eba:domail.com"), "dec05eba");
+ assert_equals(extract_user_name_from_user_id("dec05eba@domain.com"), "");
+ assert_equals(extract_user_name_from_user_id("dec05eba"), "");
+
+ assert_equals(extract_user_name_from_email("dec05eba@domain.com"), "dec05eba");
+ assert_equals(extract_user_name_from_email("@dec05eba:domain.com"), "");
+ assert_equals(extract_user_name_from_email("dec05eba"), "");
+
return 0;
}