aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Plugin.cpp
blob: ac601872e4990b547d5843cbb4538f23b67d2c86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "../../plugins/Plugin.hpp"
#include <sstream>
#include <iomanip>
#include <array>
#include <json/reader.h>

namespace QuickMedia {
    SearchResult Plugin::search(const std::string &text, BodyItems &result_items) {
        (void)text;
        (void)result_items;
        return SearchResult::OK;
    }

    SuggestionResult Plugin::update_search_suggestions(const std::string &text, BodyItems &result_items) {
        (void)text;
        (void)result_items;
        return SuggestionResult::OK;
    }

    SearchResult Plugin::content_list_search(const std::string &list_url, const std::string &text, BodyItems &result_items) {
        (void)list_url;
        (void)text;
        (void)result_items;
        return SearchResult::OK;
    }

    BodyItems Plugin::get_related_media(const std::string &url) {
        (void)url;
        return {};
    }

    struct HtmlEscapeSequence {
        char unescape_char;
        std::string escape_sequence;
    };

    void html_escape_sequences(std::string &str) {
        const std::array<HtmlEscapeSequence, 6> escape_sequences = {
            HtmlEscapeSequence { '&', "&amp;" }, // This should be first, to not accidentally replace a new sequence caused by replacing this
            HtmlEscapeSequence { '"', "&quot;" },
            HtmlEscapeSequence { '\'', "&#39;" },
            HtmlEscapeSequence { '<', "&lt;" },
            HtmlEscapeSequence { '>', "&gt;" },
            HtmlEscapeSequence { '\n', "<br>" }
        };

        for(const HtmlEscapeSequence &escape_sequence : escape_sequences) {
            string_replace_all(str, escape_sequence.unescape_char, escape_sequence.escape_sequence);
        }
    }

    struct HtmlUnescapeSequence {
        std::string escape_sequence;
        std::string unescaped_str;
    };

    void html_unescape_sequences(std::string &str) {
        const std::array<HtmlUnescapeSequence, 6> unescape_sequences = {
            HtmlUnescapeSequence { "&quot;", "\"" },
            HtmlUnescapeSequence { "&#039;", "'" },
            HtmlUnescapeSequence { "&#39;", "'" },
            HtmlUnescapeSequence { "&lt;", "<" },
            HtmlUnescapeSequence { "&gt;", ">" },
            HtmlUnescapeSequence { "&amp;", "&" } // This should be last, to not accidentally replace a new sequence caused by replacing this
        };

        for(const HtmlUnescapeSequence &unescape_sequence : unescape_sequences) {
            string_replace_all(str, unescape_sequence.escape_sequence, unescape_sequence.unescaped_str);
        }
    }

    std::string Plugin::url_param_encode(const std::string &param) const {
        std::ostringstream result;
        result.fill('0');
        result << std::hex;

        for(char c : param) {
            if(isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
                result << c;
            } else {
                result << std::uppercase;
                result << "%" << std::setw(2) << (int)(unsigned char)(c);
            }
        }

        return result.str();
    }

    DownloadResult Plugin::download_json(Json::Value &result, const std::string &url, std::vector<CommandArg> additional_args, bool use_browser_useragent, std::string *err_msg) const {
        std::string server_response;
        if(download_to_string(url, server_response, std::move(additional_args), use_tor, use_browser_useragent, err_msg == nullptr) != DownloadResult::OK) {
            if(err_msg)
                *err_msg = server_response;
            return DownloadResult::NET_ERR;
        }

        Json::CharReaderBuilder json_builder;
        std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader());
        std::string json_errors;
        if(!json_reader->parse(&server_response[0], &server_response[server_response.size()], &result, &json_errors)) {
            fprintf(stderr, "download_json error: %s\n", json_errors.c_str());
            if(err_msg)
                *err_msg = std::move(json_errors);
            return DownloadResult::ERR;
        }

        return DownloadResult::OK;
    }
}