aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Matrix.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/Matrix.cpp')
-rw-r--r--src/plugins/Matrix.cpp51
1 files changed, 17 insertions, 34 deletions
diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp
index 653569d..1d28ed2 100644
--- a/src/plugins/Matrix.cpp
+++ b/src/plugins/Matrix.cpp
@@ -5,6 +5,7 @@
#include "../../include/Notification.hpp"
#include "../../include/Program.hpp"
#include "../../include/base64_url.hpp"
+#include "../../include/Json.hpp"
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
@@ -26,14 +27,6 @@ static const char* INITIAL_FILTER = "{\"presence\":{\"limit\":0,\"types\":[\"\"]
static const char* ADDITIONAL_MESSAGES_FILTER = "{\"presence\":{\"limit\":0,\"types\":[\"\"]},\"account_data\":{\"limit\":0,\"types\":[\"\"]},\"room\":{\"state\":{\"not_types\":[\"m.room.related_groups\",\"m.room.power_levels\",\"m.room.join_rules\",\"m.room.history_visibility\"],\"lazy_load_members\":true},\"timeline\":{\"limit\":20,\"lazy_load_members\":true},\"ephemeral\":{\"limit\":0,\"types\":[\"\"],\"lazy_load_members\":true},\"account_data\":{\"limit\":0,\"types\":[\"\"],\"lazy_load_members\":true}}}";
static const char* CONTINUE_FILTER = "{\"presence\":{\"limit\":0,\"types\":[\"\"]},\"account_data\":{\"limit\":0,\"types\":[\"\"]},\"room\":{\"state\":{\"not_types\":[\"m.room.related_groups\",\"m.room.power_levels\",\"m.room.join_rules\",\"m.room.history_visibility\"],\"lazy_load_members\":true},\"timeline\":{\"lazy_load_members\":true},\"ephemeral\":{\"limit\":0,\"types\":[\"\"],\"lazy_load_members\":true},\"account_data\":{\"types\":[\"m.fully_read\",\"m.tag\"],\"lazy_load_members\":true}}}";
-static rapidjson::Value nullValue(rapidjson::kNullType);
-static const rapidjson::Value& GetMember(const rapidjson::Value &obj, const char *key) {
- auto it = obj.FindMember(key);
- if(it != obj.MemberEnd())
- return it->value;
- return nullValue;
-}
-
static std::string capitalize(const std::string &str) {
if(str.size() >= 1)
return (char)std::toupper(str[0]) + str.substr(1);
@@ -1036,7 +1029,7 @@ namespace QuickMedia {
rapidjson::Document json_root;
std::string err_msg;
- DownloadResult download_result = download_json(json_root, url, additional_args, true, &err_msg);
+ DownloadResult download_result = download_json(json_root, url, additional_args, use_tor, true, &err_msg);
if(download_result != DownloadResult::OK) {
fprintf(stderr, "/sync for additional messages failed\n");
return;
@@ -1062,7 +1055,7 @@ namespace QuickMedia {
rapidjson::Document json_root;
std::string err_msg;
- DownloadResult download_result = download_json(json_root, url, additional_args, true, &err_msg);
+ DownloadResult download_result = download_json(json_root, url, additional_args, use_tor, true, &err_msg);
if(download_result != DownloadResult::OK) {
fprintf(stderr, "/sync failed\n");
goto sync_end;
@@ -1118,7 +1111,7 @@ namespace QuickMedia {
snprintf(url, sizeof(url), "%s/_matrix/client/r0/notifications?limit=100&only=highlight", homeserver.c_str());
rapidjson::Document json_root;
- DownloadResult download_result = download_json(json_root, url, std::move(additional_args), true);
+ DownloadResult download_result = download_json(json_root, url, std::move(additional_args), use_tor, true);
if(download_result != DownloadResult::OK || !json_root.IsObject()) {
fprintf(stderr, "Fetching notifications failed!\n");
return;
@@ -2382,7 +2375,7 @@ namespace QuickMedia {
snprintf(url, sizeof(url), "%s/_matrix/client/r0/rooms/%s/messages?from=%s&limit=20&dir=b&filter=%s", homeserver.c_str(), room_data->id.c_str(), from.c_str(), filter.c_str());
rapidjson::Document json_root;
- DownloadResult download_result = download_json(json_root, url, std::move(additional_args), true);
+ DownloadResult download_result = download_json(json_root, url, std::move(additional_args), use_tor, true);
if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result);
if(!json_root.IsObject())
@@ -2536,7 +2529,7 @@ namespace QuickMedia {
snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/%s", homeserver.c_str(), room->id.c_str(), transaction_id.c_str());
rapidjson::Document json_root;
- DownloadResult download_result = download_json(json_root, request_url, std::move(additional_args), true);
+ DownloadResult download_result = download_json(json_root, request_url, std::move(additional_args), use_tor, true);
if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result);
if(!json_root.IsObject())
@@ -2679,7 +2672,7 @@ namespace QuickMedia {
snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/%s", homeserver.c_str(), room->id.c_str(), transaction_id.c_str());
rapidjson::Document json_root;
- DownloadResult download_result = download_json(json_root, request_url, std::move(additional_args), true);
+ DownloadResult download_result = download_json(json_root, request_url, std::move(additional_args), use_tor, true);
if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result);
if(!json_root.IsObject())
@@ -2763,7 +2756,7 @@ namespace QuickMedia {
snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.room.message/%s", homeserver.c_str(), room->id.c_str(), transaction_id.c_str());
rapidjson::Document json_root;
- DownloadResult download_result = download_json(json_root, request_url, std::move(additional_args), true);
+ DownloadResult download_result = download_json(json_root, request_url, std::move(additional_args), use_tor, true);
if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result);
if(!json_root.IsObject())
@@ -2809,7 +2802,7 @@ namespace QuickMedia {
snprintf(request_url, sizeof(request_url), "%s/_matrix/client/r0/rooms/%s/send/m.reaction/%s", homeserver.c_str(), room->id.c_str(), transaction_id.c_str());
rapidjson::Document json_root;
- DownloadResult download_result = download_json(json_root, request_url, std::move(additional_args), true);
+ DownloadResult download_result = download_json(json_root, request_url, std::move(additional_args), use_tor, true);
if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result);
if(!json_root.IsObject())
@@ -3011,7 +3004,7 @@ namespace QuickMedia {
snprintf(url, sizeof(url), "%s/_matrix/media/r0/upload?filename=%s", homeserver.c_str(), filename_escaped.c_str());
rapidjson::Document json_root;
- DownloadResult download_result = download_json(json_root, url, std::move(additional_args), true, &err_msg);
+ DownloadResult download_result = download_json(json_root, url, std::move(additional_args), use_tor, true, &err_msg);
if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result);
if(!json_root.IsObject()) {
@@ -3059,7 +3052,7 @@ namespace QuickMedia {
};
rapidjson::Document json_root;
- DownloadResult download_result = download_json(json_root, homeserver + "/_matrix/client/r0/login", std::move(additional_args), true, &err_msg);
+ DownloadResult download_result = download_json(json_root, homeserver + "/_matrix/client/r0/login", std::move(additional_args), use_tor, true, &err_msg);
if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result);
Path matrix_sync_data_path = get_cache_dir().join("matrix").join("sync_data.json");
@@ -3170,7 +3163,7 @@ namespace QuickMedia {
snprintf(url, sizeof(url), "%s/_matrix/client/r0/rooms/%s/redact/%s/%s", homeserver.c_str(), room->id.c_str(), message_typed->event_id.c_str(), transaction_id.c_str());
rapidjson::Document json_root;
- DownloadResult download_result = download_json(json_root, url, std::move(additional_args), true, &err_msg);
+ DownloadResult download_result = download_json(json_root, url, std::move(additional_args), use_tor, true, &err_msg);
if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result);
if(!json_root.IsObject()) {
@@ -3392,7 +3385,7 @@ namespace QuickMedia {
snprintf(url, sizeof(url), "%s/_matrix/media/r0/config", homeserver.c_str());
rapidjson::Document json_root;
- DownloadResult download_result = download_json(json_root, url, std::move(additional_args), true);
+ DownloadResult download_result = download_json(json_root, url, std::move(additional_args), use_tor, true);
if(download_result != DownloadResult::OK) return download_result_to_plugin_result(download_result);
if(!json_root.IsObject())
@@ -3492,7 +3485,7 @@ namespace QuickMedia {
snprintf(url, sizeof(url), "%s/_matrix/client/r0/profile/%s", homeserver.c_str(), user_id.c_str());
rapidjson::Document json_root;
- DownloadResult download_result = download_json(json_root, url, {}, true);
+ DownloadResult download_result = download_json(json_root, url, {}, use_tor, true);
if(download_result != DownloadResult::OK || !json_root.IsObject()) {
fprintf(stderr, "Fetching profile for user %s failed!\n", user_id.c_str());
auto user = get_user_by_id(room, user_id);
@@ -3514,7 +3507,7 @@ namespace QuickMedia {
snprintf(url, sizeof(url), "%s/_matrix/client/r0/rooms/%s/joined_members", homeserver.c_str(), room->id.c_str());
rapidjson::Document json_root;
- DownloadResult download_result = download_json(json_root, url, std::move(additional_args), true);
+ DownloadResult download_result = download_json(json_root, url, std::move(additional_args), use_tor, true);
if(download_result != DownloadResult::OK || !json_root.IsObject()) {
fprintf(stderr, "Fetching users for room %s failed!\n", room->id.c_str());
return;
@@ -3561,7 +3554,7 @@ namespace QuickMedia {
snprintf(url, sizeof(url), "%s/_matrix/client/r0/rooms/%s/members?membership=join", homeserver.c_str(), room->id.c_str());
rapidjson::Document json_root;
- DownloadResult download_result = download_json(json_root, url, std::move(additional_args), true);
+ DownloadResult download_result = download_json(json_root, url, std::move(additional_args), use_tor, true);
if(download_result != DownloadResult::OK || !json_root.IsObject()) {
fprintf(stderr, "Fetching users for room %s failed!\n", room->id.c_str());
return;
@@ -3601,7 +3594,7 @@ namespace QuickMedia {
snprintf(url, sizeof(url), "%s/_matrix/client/r0/user/%s/filter", homeserver.c_str(), user_id.c_str());
rapidjson::Document json_root;
- DownloadResult download_result = download_json(json_root, url, std::move(additional_args), true);
+ DownloadResult download_result = download_json(json_root, url, std::move(additional_args), use_tor, true);
if(download_result != DownloadResult::OK || !json_root.IsObject()) return FILTER;
const rapidjson::Value &filter_id_json = GetMember(json_root, "filter_id");
@@ -3619,14 +3612,4 @@ namespace QuickMedia {
return INITIAL_FILTER;
#endif
}
-
- DownloadResult Matrix::download_json(rapidjson::Document &result, const std::string &url, std::vector<CommandArg> additional_args, bool use_browser_useragent, std::string *err_msg) const {
- if(download_to_json(url, result, std::move(additional_args), use_tor, use_browser_useragent, err_msg == nullptr) != DownloadResult::OK) {
- // Cant get error since we parse directly to json. TODO: Make this work somehow?
- if(err_msg)
- *err_msg = "Failed to download/parse json";
- return DownloadResult::NET_ERR;
- }
- return DownloadResult::OK;
- }
} \ No newline at end of file