The Battle for Wesnoth  1.19.7+dev
client.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
3  by Iris Morelle <shadowm2006@gmail.com>
4  Copyright (C) 2003 - 2008 by David White <dave@whitevine.net>
5  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY.
13 
14  See the COPYING file for more details.
15 */
16 
17 /**
18  * @file
19  * Networked add-ons (campaignd) client interface.
20  *
21  * This API revolves around the campaignd client functionality. It depends on
22  * the add-ons management and Asio network APIs.
23  */
24 
25 #pragma once
26 
27 #include "addon/info.hpp"
29 #include "network_asio.hpp"
30 
31 #include <set>
32 
33 /**
34  * Add-ons (campaignd) client class.
35  *
36  * This class encapsulates much of the logic behind the campaignd
37  * add-ons server interaction for the client-side. Most networking
38  * operations with it are implemented here.
39  */
41 {
42 public:
43  struct invalid_server_address : public std::exception {};
44  struct not_connected_to_server : public std::exception {};
45  struct user_exit : public std::exception {};
46  struct user_disconnect : public std::exception {};
47 
48  addons_client(const addons_client&) = delete;
50 
51  /**
52  * Constructor.
53  *
54  * @param address Server address (e.g. "localhost:15999").
55  */
56  explicit addons_client(const std::string& address);
57 
58  /**
59  * Tries to establish a connection to the add-ons server.
60  */
61  void connect();
62 
63  /**
64  * Disconnects from the add-on server.
65  */
66  void disconnect()
67  {
68  conn_.reset();
71  }
72 
73  /** Returns the current hostname:port used for this connection. */
74  const std::string& addr() const { return addr_; }
75 
76  /** Returns the last error message sent by the server, or an empty string. */
77  const std::string& get_last_server_error() const { return last_error_; }
78 
79  /** Returns the last error message extra data sent by the server, or an empty string. */
80  const std::string& get_last_server_error_data() const { return last_error_data_; }
81 
82  /** Returns true if the client is connected to the server. */
83  bool is_connected() { return conn_ != nullptr; }
84 
85  /**
86  * Request the add-ons list from the server.
87  *
88  * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
89  *
90  * @param cfg A config object whose contents are replaced with
91  * the server's list if available, cleared otherwise.
92  * @param icons Whether to have the add-ons server populate the icon
93  */
94  bool request_addons_list(config& cfg, bool icons);
95 
96  /**
97  * Retrieves the add-ons server web URL if available.
98  */
99  const std::string& server_url() const
100  {
101  return server_url_;
102  }
103 
104  /**
105  * Request the add-ons server distribution terms message.
106  */
107  bool request_distribution_terms(std::string& terms);
108 
109  /**
110  * Installation outcome values.
111  */
112  enum class install_outcome
113  {
114  /** The add-on was correctly installed. */
115  success,
116  /** The add-on could not be downloaded from the server. */
117  failure,
118  /** User aborted the operation because of an issue with dependencies or chose not to overwrite the add-on. */
119  abort,
120  };
121 
122  /**
123  * Contains the outcome of an add-on install operation.
124  */
126  {
127  /**
128  * Overall outcome of the operation.
129  */
131 
132  /**
133  * Specifies if WML on disk was altered and needs to be reloaded.
134  *
135  * @note Failure to install an add-on properly may not necessarily mean
136  * that WML on disk was left unchanged (e.g. if any dependencies were
137  * succesfully installed first.)
138  */
140  };
141 
142  /**
143  * Performs an add-on download and install cycle.
144  *
145  * This checks and prompts the user through the UI before overwriting an
146  * existing add-on with a .pbl file or version control system files (.git/,
147  * .svn/, etc.). It also resolves add-on dependencies and downloads them
148  * using the same system before downloading the original target add-on.
149  *
150  * @param addons Add-ons list used for resolving dependencies.
151  * @param addon Identity of the singular add-on that will be
152  * downloaded.
153  *
154  * @return An install_result with the outcome of the operation.
155  */
156  install_result install_addon_with_checks(const addons_list& addons, const addon_info& addon);
157 
158  /**
159  * Uploads an add-on to the server.
160  *
161  * This method reads the add-on upload passphrase and other data
162  * from the associated .pbl file. If the .pbl file doesn't have a
163  * passphrase, a new, random one will be automatically generated
164  * and written to the file for the user.
165  *
166  * @todo Notify the user about the automatic passphrase.
167  *
168  * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
169  *
170  * @param id Id. of the add-on to upload.
171  * @param response_message The server response message on success, such as "add-on accepted".
172  * @param cfg The pbl config of the add-on with the specified id.
173  * @param local_only Whether the addon is not present on the server.
174  */
175  bool upload_addon(const std::string& id, std::string& response_message, config& cfg, bool local_only);
176 
177  /**
178  * Requests the specified add-on to be removed from the server.
179  *
180  * This method reads the add-on upload passphrase from the associated
181  * .pbl file.
182  *
183  * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
184  *
185  * @param id Id. of the add-on to take down.
186  * @param response_message The server response message on success, such as "add-on accepted".
187  */
188  bool delete_remote_addon(const std::string& id, std::string& response_message);
189 
190  /**
191  * Returns whether the server supports the given named capability.
192  */
193  bool server_supports(const std::string& cap_id) const
194  {
195  return server_capabilities_.find(cap_id) != server_capabilities_.end();
196  }
197 
198  /**
199  * Returns whether the server supports incremental (delta) downloads and uploads.
200  */
202  {
203  return server_supports("delta");
204  }
205 
206  /**
207  * Returns whether the server supports passphrase authentication on an add-on basis.
208  */
210  {
211  return server_supports("auth:legacy");
212  }
213 
214  /**
215  * Returns whether the current connection uses TLS.
216  */
217  bool using_tls() const
218  {
219  return conn_ && conn_->using_tls();
220  }
221 
222  const std::string& server_id() const
223  {
224  return server_id_;
225  }
226 
227  const std::string& server_version() const
228  {
229  return server_version_;
230  }
231 
232 private:
234 
235  std::string addr_;
236  std::string host_;
237  std::string port_;
238  std::unique_ptr<network_asio::connection> conn_;
239  std::string last_error_;
240  std::string last_error_data_;
241 
242  std::string server_id_;
243  std::string server_version_;
244  std::set<std::string> server_capabilities_;
245  std::string server_url_;
246  std::string license_notice_;
247 
248  /**
249  * Downloads the specified add-on from the server.
250  *
251  * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
252  *
253  * @param archive_cfg Config object to hold the downloaded add-on archive data.
254  * @param id Add-on id.
255  * @param title Add-on title, used for status display.
256  * @param version Specifies an add-on version to download.
257  * @param increase_downloads Whether to request the server to increase the add-on's
258  * download count or not (e.g. when upgrading).
259  */
260  bool download_addon(config& archive_cfg, const std::string& id, const std::string& title, const version_info& version, bool increase_downloads = true);
261 
262  /**
263  * Installs the specified add-on using an archive received from the server.
264  *
265  * An _info.cfg file will be added to the local directory for the add-on
266  * to keep track of version and dependency information.
267  */
268  bool install_addon(config& archive_cfg, const addon_info& info);
269 
270  // Asks the client to download and install an addon, reporting errors in a gui dialog. Returns true if new content was installed, false otherwise.
271  bool try_fetch_addon(const addon_info& addon);
272 
273  /**
274  * Warns the user about unresolved dependencies and installs them if they choose to do so.
275  * Returns: outcome: abort in case the user chose to abort because of an issue
276  * success otherwise
277  * wml_change: indicates if new wml content was installed
278  */
280 
281  /** Checks whether the given add-on has local .pbl or VCS information and asks before overwriting it. */
283 
284  /** Makes sure the add-ons server connection is working. */
285  void check_connected() const;
286 
287  /**
288  * Sends a request to the add-ons server.
289  *
290  * @note This is an asynchronous operation. @a display_status_window
291  * should be called afterwards to wait for it to finish.
292  *
293  * @param request The client request WML.
294  * @param response A config object whose contents are replaced
295  * with the server response WML.
296  */
297  void send_request(const config& request, config& response);
298 
299  /**
300  * Sends a simple request message to the add-ons server.
301  *
302  * The real request sent consists of a WML object with an empty
303  * child node whose name corresponds to @a request_string
304  *
305  * @note This is an asynchronous operation. @a display_status_window
306  * should be called afterwards to wait for it to finish.
307  *
308  * @param request_string The client request string.
309  * @param response A config object whose contents are replaced
310  * with the server response WML.
311  */
312  void send_simple_request(const std::string& request_string, config& response);
313 
314  /**
315  * Waits for a network transfer, displaying a status window.
316  *
317  * The window is displayed with the specified contents. This
318  * method doesn't return until the network transfer is complete. It
319  * will throw a @a user_exit exception if the user cancels the
320  * transfer by canceling the status window.
321  */
322  void wait_for_transfer_done(const std::string& status_message, transfer_mode mode = transfer_mode::download);
323 
324  bool update_last_error(config& response_cfg);
325 
326  void clear_last_error();
327 
328  void clear_server_info();
329 };
Add-ons (campaignd) client class.
Definition: client.hpp:41
bool update_last_error(config &response_cfg)
Definition: client.cpp:601
std::string addr_
Definition: client.hpp:235
void disconnect()
Disconnects from the add-on server.
Definition: client.hpp:66
install_result install_addon_with_checks(const addons_list &addons, const addon_info &addon)
Performs an add-on download and install cycle.
Definition: client.cpp:575
bool is_connected()
Returns true if the client is connected to the server.
Definition: client.hpp:83
bool delete_remote_addon(const std::string &id, std::string &response_message)
Requests the specified add-on to be removed from the server.
Definition: client.cpp:274
bool do_check_before_overwriting_addon(const addon_info &addon)
Checks whether the given add-on has local .pbl or VCS information and asks before overwriting it.
Definition: client.cpp:542
void wait_for_transfer_done(const std::string &status_message, transfer_mode mode=transfer_mode::download)
Waits for a network transfer, displaying a status window.
Definition: client.cpp:694
const std::string & get_last_server_error_data() const
Returns the last error message extra data sent by the server, or an empty string.
Definition: client.hpp:80
std::unique_ptr< network_asio::connection > conn_
Definition: client.hpp:238
install_outcome
Installation outcome values.
Definition: client.hpp:113
@ success
The add-on was correctly installed.
@ failure
The add-on could not be downloaded from the server.
@ abort
User aborted the operation because of an issue with dependencies or chose not to overwrite the add-on...
const std::string & server_id() const
Definition: client.hpp:222
std::string port_
Definition: client.hpp:237
bool download_addon(config &archive_cfg, const std::string &id, const std::string &title, const version_info &version, bool increase_downloads=true)
Downloads the specified add-on from the server.
Definition: client.cpp:321
void send_request(const config &request, config &response)
Sends a request to the add-ons server.
Definition: client.cpp:644
bool try_fetch_addon(const addon_info &addon)
Definition: client.cpp:416
std::string server_url_
Definition: client.hpp:245
const std::string & server_url() const
Retrieves the add-ons server web URL if available.
Definition: client.hpp:99
std::string host_
Definition: client.hpp:236
void check_connected() const
Makes sure the add-ons server connection is working.
Definition: client.cpp:635
std::set< std::string > server_capabilities_
Definition: client.hpp:244
std::string server_id_
Definition: client.hpp:242
bool install_addon(config &archive_cfg, const addon_info &info)
Installs the specified add-on using an archive received from the server.
Definition: client.cpp:344
std::string license_notice_
Definition: client.hpp:246
bool using_tls() const
Returns whether the current connection uses TLS.
Definition: client.hpp:217
void clear_last_error()
Definition: client.cpp:620
std::string last_error_
Definition: client.hpp:239
bool server_supports_delta() const
Returns whether the server supports incremental (delta) downloads and uploads.
Definition: client.hpp:201
addons_client(const addons_client &)=delete
bool server_supports_legacy_auth() const
Returns whether the server supports passphrase authentication on an add-on basis.
Definition: client.hpp:209
std::string last_error_data_
Definition: client.hpp:240
std::string server_version_
Definition: client.hpp:243
void connect()
Tries to establish a connection to the add-ons server.
Definition: client.cpp:69
void send_simple_request(const std::string &request_string, config &response)
Sends a simple request message to the add-ons server.
Definition: client.cpp:652
bool request_distribution_terms(std::string &terms)
Request the add-ons server distribution terms message.
Definition: client.cpp:136
install_result do_resolve_addon_dependencies(const addons_list &addons, const addon_info &addon)
Warns the user about unresolved dependencies and installs them if they choose to do so.
Definition: client.cpp:435
const std::string & get_last_server_error() const
Returns the last error message sent by the server, or an empty string.
Definition: client.hpp:77
void clear_server_info()
Definition: client.cpp:626
const std::string & addr() const
Returns the current hostname:port used for this connection.
Definition: client.hpp:74
bool request_addons_list(config &cfg, bool icons)
Request the add-ons list from the server.
Definition: client.cpp:115
const std::string & server_version() const
Definition: client.hpp:227
bool upload_addon(const std::string &id, std::string &response_message, config &cfg, bool local_only)
Uploads an add-on to the server.
Definition: client.cpp:159
addons_client & operator=(const addons_client &)=delete
bool server_supports(const std::string &cap_id) const
Returns whether the server supports the given named capability.
Definition: client.hpp:193
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
Represents version numbers.
std::map< std::string, addon_info > addons_list
Definition: info.hpp:27
logger & info()
Definition: log.cpp:319
Contains the outcome of an add-on install operation.
Definition: client.hpp:126
install_outcome outcome
Overall outcome of the operation.
Definition: client.hpp:130
bool wml_changed
Specifies if WML on disk was altered and needs to be reloaded.
Definition: client.hpp:139