Debian Codegraph @

modules/cloudpinyin/cloudpinyin.cpp

raw
1 /*
2 * SPDX-FileCopyrightText: 2017-2017 CSSlayer <[email protected]>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 */
7
8 #include "cloudpinyin.h"
9 #include "cloudpinyin_public.h"
10 #include "fetch.h"
11 #include <cstdint>
12 #include <cstring>
13 #include <curl/curl.h>
14 #include <fcitx-config/iniparser.h>
15 #include <fcitx-utils/eventloopinterface.h>
16 #include <fcitx-utils/fs.h>
17 #include <fcitx-utils/log.h>
18 #include <fcitx-utils/misc.h>
19 #include <fcitx-utils/standardpaths.h>
20 #include <fcitx-utils/stringutils.h>
21 #include <fcitx-utils/unixfd.h>
22 #include <fcitx-utils/utf8.h>
23 #include <fcitx/addoninstance.h>
24 #include <fcitx/addonmanager.h>
25 #include <fcntl.h>
26 #include <memory>
27 #include <string>
28 #include <string_view>
29 #include <unistd.h>
30 #include <utility>
31
32 using namespace fcitx;
33
34 namespace {
35
36 #define CLOUDPINYIN_DEBUG() FCITX_LOGC(cloudpinyin, Debug)
37 FCITX_DEFINE_LOG_CATEGORY(cloudpinyin, "cloudpinyin");
38
39 class GoogleBackend : public Backend {
40 public:
41 GoogleBackend(std::string url) : url_(std::move(url)) {}
42
43 bool prepareRequest(CurlQueue *queue, const std::string &pinyin) override {
44 const UniqueCPtr<char, curl_free> escaped(
45 curl_escape(pinyin.c_str(), static_cast<int>(pinyin.size())));
46 if (!escaped) {
47 return false;
48 }
49 const std::string url = stringutils::concat(url_, escaped.get());
50 CLOUDPINYIN_DEBUG() << "Request URL: " << url;
51 return (curl_easy_setopt(queue->curl(), CURLOPT_URL, url.c_str()) ==
52 CURLE_OK);
53 }
54 std::string parseResult(CurlQueue *queue) override {
55 const std::string_view result(queue->result().data(),
56 queue->result().size());
57 CLOUDPINYIN_DEBUG() << "Request result: " << result;
58 auto start = result.find("\",[\"");
59 std::string hanzi;
60 if (start != std::string_view::npos) {
61 start += strlen("\",[\"");
62 auto end = result.find('\"', start);
63 if (end != std::string_view::npos && end > start) {
64 hanzi = result.substr(start, end - start);
65 }
66 }
67 return hanzi;
68 }
69
70 private:
71 const std::string url_;
72 };
73
74 class BaiduBackend : public Backend {
75 public:
76 bool prepareRequest(CurlQueue *queue, const std::string &pinyin) override {
77 const UniqueCPtr<char, &curl_free> escaped(
78 curl_escape(pinyin.c_str(), static_cast<int>(pinyin.size())));
79 if (!escaped) {
80 return false;
81 }
82 const std::string url =
83 std::format("https://olimenew.baidu.com/"
84 "py?input={}&inputtype=py&resultcoding=utf-8",
85 escaped.get());
86 CLOUDPINYIN_DEBUG() << "Request URL: " << url;
87 return (curl_easy_setopt(queue->curl(), CURLOPT_URL, url.c_str()) ==
88 CURLE_OK);
89 }
90
91 std::string parseResult(CurlQueue *queue) override {
92 const std::string_view result(queue->result().data(),
93 queue->result().size());
94 CLOUDPINYIN_DEBUG() << "Request result: " << result;
95 auto start = result.find("[[\"");
96 std::string hanzi;
97 if (start != std::string::npos) {
98 start += strlen("[[\"");
99 auto end = result.find("\",", start);
100 if (end != std::string::npos && end > start) {
101 hanzi = result.substr(start, end - start);
102 }
103 }
104 return hanzi;
105 }
106 };
107
108 constexpr int MAX_ERROR = 10;
109 constexpr uint64_t minInUs = 60000000;
110
111 } // namespace
112
113 CloudPinyin::CloudPinyin(fcitx::AddonManager *manager)
114 : eventLoop_(manager->eventLoop()),
115 dispatcher_(manager->instance()->eventDispatcher()) {
116 curl_global_init(CURL_GLOBAL_ALL);
117
118 backends_.emplace(
119 CloudPinyinBackend::Google,
120 std::make_unique<GoogleBackend>(
121 "https://www.google.com/inputtools/request?ime=pinyin&text="));
122 backends_.emplace(
123 CloudPinyinBackend::GoogleCN,
124 std::make_unique<GoogleBackend>(
125 "https://www.google.cn/inputtools/request?ime=pinyin&text="));
126 backends_.emplace(CloudPinyinBackend::Baidu,
127 std::make_unique<BaiduBackend>());
128
129 resetError_ =
130 eventLoop_->addTimeEvent(CLOCK_MONOTONIC, now(CLOCK_MONOTONIC), minInUs,
131 [this](EventSourceTime *, uint64_t) {
132 resetError();
133 return true;
134 });
135 if (resetError_) {
136 resetError_->setEnabled(false);
137 }
138 thread_ = std::make_unique<FetchThread>(this);
139
140 reloadConfig();
141 }
142
143 CloudPinyin::~CloudPinyin() {}
144
145 void CloudPinyin::reloadConfig() {
146 readAsIni(config_, "conf/cloudpinyin.conf");
147 }
148
149 void CloudPinyin::request(const std::string &pinyin,
150 CloudPinyinCallback callback) {
151 if (static_cast<int>(pinyin.size()) < config_.minimumLength.value()) {
152 callback(pinyin, "");
153 return;
154 }
155 if (auto *value = cache_.find(pinyin)) {
156 callback(pinyin, *value);
157 } else {
158 auto backend = config_.backend.value();
159 auto iter = backends_.find(backend);
160 if (iter == backends_.end() || errorCount_ >= MAX_ERROR) {
161 callback(pinyin, "");
162 return;
163 }
164 auto *b = iter->second.get();
165 if (!thread_->addRequest([proxy = *config_.proxy, b, &pinyin,
166 &callback](CurlQueue *queue) {
167 if (!b->prepareRequest(queue, pinyin)) {
168 return false;
169 }
170 if (curl_easy_setopt(
171 queue->curl(), CURLOPT_PROXY,
172 (proxy.empty() ? nullptr : proxy.data())) != CURLE_OK) {
173 return false;
174 }
175 queue->setPinyin(pinyin);
176 queue->setBusy();
177 queue->setCallback(callback);
178 return true;
179 })) {
180 callback(pinyin, "");
181 }
182 }
183 }
184
185 void CloudPinyin::notifyFinished() {
186 dispatcher_.scheduleWithContext(this->watch(), [this]() {
187 CurlQueue *item;
188 auto backend = config_.backend.value();
189 auto iter = backends_.find(backend);
190 Backend *b = nullptr;
191 if (iter != backends_.end()) {
192 b = iter->second.get();
193 }
194
195 while ((item = thread_->popFinished())) {
196 if (item->httpCode() != 200) {
197 errorCount_ += 1;
198
199 if (errorCount_ == MAX_ERROR && resetError_) {
200 FCITX_ERROR() << "Cloud pinyin reaches max error. "
201 "Retry in 5 minutes.";
202 resetError_->setNextInterval(minInUs * 5);
203 resetError_->setOneShot();
204 }
205 }
206
207 std::string hanzi;
208 if (b) {
209 hanzi = b->parseResult(item);
210 } else {
211 hanzi = "";
212 }
213 item->callback()(item->pinyin(), hanzi);
214 if (!hanzi.empty()) {
215 cache_.insert(item->pinyin(), hanzi);
216 }
217 item->release();
218 }
219 return true;
220 });
221 }
222
223 FCITX_ADDON_FACTORY_V2(cloudpinyin, CloudPinyinFactory);