27 #include "../cl_shared.h"
28 #include "../cgame/cl_game.h"
29 #include "../ui/ui_main.h"
30 #include "../../shared/parse.h"
50 static const char*
WEB_CGameGetURL (
char* out,
size_t outSize,
const char* url,
const char* cgameId,
int category,
const char*
filename,
int userId = -1)
53 Com_sprintf(categoryStr,
sizeof(categoryStr),
"%i", category);
55 Com_sprintf(userIdStr,
sizeof(userIdStr),
"%i", userId);
57 const struct urlIds_s {
62 {
"$cgame$", cgameId,
true},
63 {
"$category$", categoryStr, category >= 0},
64 {
"$file$",
filename, filename !=
nullptr},
65 {
"$userid$", userIdStr, userId > 0}
70 const size_t entries =
lengthof(urlIds);
71 for (
int i = 0;
i < entries;
i++) {
72 const urlIds_s&
id = urlIds[
i];
73 if (strstr(out,
id.
id) !=
nullptr) {
74 char encoded[256] = {
'\0' };
75 const char* replace = encoded;
76 if (
id.replace !=
nullptr) {
77 if (!
HTTP_Encode(
id.replace, encoded,
sizeof(encoded))) {
78 Com_Printf(
"failed to encode '%s'\n",
id.replace);
83 if (!
Q_strreplace(out,
id.
id, replace, buf1,
sizeof(buf1))) {
88 }
else if (
id.mandatory) {
89 Com_Printf(
"'%s' is missing in the url\n",
id.
id);
117 const char* fullPath =
va(
"%s/%s",
FS_Gamedir(), filename);
127 const char* encodedURL =
WEB_CGameGetURL(url,
sizeof(url), web_cgameuploadurl->
string, cgameId, category,
nullptr);
128 if (encodedURL ==
nullptr) {
134 Com_Printf(
"failed to upload the team from file '%s'\n", filename);
139 Com_Printf(
"uploaded the team '%s'\n", filename);
158 const char* encodedURL =
WEB_CGameGetURL(url,
sizeof(url), web_cgamedeleteurl->
string, cgameId, category, filename);
159 if (encodedURL ==
nullptr)
163 Com_Printf(
"failed to delete the cgame file '%s' from the server\n", filename);
168 Com_Printf(
"deleted the cgame file '%s'\n", filename);
190 const char* encodedURL =
WEB_CGameGetURL(url,
sizeof(url), web_cgamedownloadurl->
string, cgameId, category, filename, userId);
191 if (encodedURL ==
nullptr)
196 Q_strcat(buf,
sizeof(buf),
"%s", filename);
201 Com_Printf(
"Could not open the target file\n");
207 Com_Printf(
"cgame file download failed from '%s'\n", url);
220 int*
count = (
int*)userdata;
222 if (count !=
nullptr)
226 Com_Printf(
"Could not load the cgame list\n");
240 {
"file",
V_STRING, offsetof(entry_s, file), 0},
247 const char* token =
Com_Parse(&responseBuf);
248 if (token[0] !=
'{') {
249 Com_Printf(
"invalid token: '%s' - expected {\n", token);
258 if (token[0] ==
'{') {
263 if (token[0] ==
'}') {
272 UI_ExecuteConfunc(
"cgamefiles_add \"%s\" %i %i \"%s\" \"%s\" %i", idBuf, entry.category, entry.userId,
id, entry.name, ownEntry ? 1 : 0);
278 for (value = values; value->
string; value++) {
288 if (value->
string ==
nullptr) {
297 Com_Printf(
"found %i cgame file entries\n", num);
298 if (count !=
nullptr)
320 const char* encodedURL =
WEB_CGameGetURL(url,
sizeof(url), web_cgamelisturl->
string, cgameId, category,
nullptr, userId);
321 if (encodedURL ==
nullptr)
327 Com_Printf(
"failed to query the cgame list for '%s' in category %i and for user %i\n", cgameId, category, userId);
341 if (name ==
nullptr) {
345 const int category = atoi(
Cmd_Argv(1));
359 if (name ==
nullptr) {
363 const int category = atoi(
Cmd_Argv(1));
375 if (name ==
nullptr) {
379 const int category = atoi(
Cmd_Argv(1));
392 if (name ==
nullptr) {
396 const int category = atoi(
Cmd_Argv(1));
399 Com_Printf(
"failed to list the cgame files for '%s' in category %i for userid %i\n", name, category, userId);
405 web_cgamedownloadurl =
Cvar_Get(
"web_cgamedownloadurl",
WEB_API_SERVER "cgame/$cgame$/$userid$/$category$/$file$", 0,
"The url to download a shared cgame file from. Use $userid$, $category$, $cgame$ and $file$ as placeholders.");
406 web_cgamelisturl =
Cvar_Get(
"web_cgamelisturl",
WEB_API_SERVER "api/cgamelist.php?cgame=$cgame$&category=$category$&userid=$userid$", 0,
"The url to get the cgame file list from.");
407 web_cgamedeleteurl =
Cvar_Get(
"web_cgamedeleteurl",
WEB_API_SERVER "api/cgamedelete.php?cgame=$cgame$&category=$category$&file=$file$", 0,
"The url to call if you want to delete one of your own cgame files again.");
408 web_cgameuploadurl =
Cvar_Get(
"web_cgameuploadurl",
WEB_API_SERVER "api/cgameupload.php?cgame=$cgame$&category=$category$", 0,
"The url to upload a cgame file to.");
bool Q_strnull(const char *string)
const char * Cmd_Argv(int arg)
Returns a given argument.
void Cmd_AddCommand(const char *cmdName, xcommand_t function, const char *desc)
Add a new command to the script interface.
static cvar_t * web_cgameuploadurl
const char * Com_SkipPath(const char *pathname)
Returns just the filename from a given path.
int FS_OpenFile(const char *filename, qFILE *file, filemode_t mode)
Finds and opens the file in the search path.
static void WEB_ListCGameFilesCallback(const char *responseBuf, void *userdata)
The http callback for the cgame list command.
const char * va(const char *format,...)
does a varargs printf into a temp buffer, so I don't need to have varargs versions of all text functi...
This is a cvar definition. Cvars can be user modified and used in our menus e.g.
bool Com_sprintf(char *dest, size_t size, const char *fmt,...)
copies formatted string with buffer-size checking
void Com_StripExtension(const char *in, char *out, const size_t size)
Removes the file extension from a filename.
bool FS_FileExists(const char *filename,...)
Checks whether a file exists (not in virtual filesystem)
static cvar_t * web_cgamelisturl
void Com_Printf(const char *const fmt,...)
static void WEB_ListCGame_f(void)
void WEB_CGameCommands(void)
static const char * WEB_CGameGetURL(char *out, size_t outSize, const char *url, const char *cgameId, int category, const char *filename, int userId=-1)
Replaces placeholders in the urls with given values.
const char * FS_Gamedir(void)
Called to find where to write a file (savegames, etc)
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
cvar_t * Cvar_Get(const char *var_name, const char *var_value, int flags, const char *desc)
Init or return a cvar.
bool WEB_PutFile(const char *formName, const char *fileName, const char *url, upparam_t *params)
Uploads a file to the server with the login credentials.
int Cmd_Argc(void)
Return the number of arguments of the current command. "command parameter" will result in a argc of 2...
static cvar_t * web_cgamedownloadurl
void Com_DPrintf(int level, const char *fmt,...)
A Com_Printf that only shows up if the "developer" cvar is set.
bool HTTP_GetToFile(const char *url, FILE *file, const char *postfields)
Downloads the given url into the given file.
static void WEB_DownloadCGame_f(void)
static void WEB_DeleteCGame_f(void)
bool WEB_CheckAuth(void)
Pushes the webauth window if the password is not yet set.
int WEB_CGameListForUser(const char *cgameId, int category, int userId)
Shows the uploaded files for the particular cgame category and the given userid.
static cvar_t * web_cgamedeleteurl
const char * GAME_GetCurrentName(void)
int Com_EParseValue(void *base, const char *token, valueTypes_t type, int ofs, size_t size)
const char * Com_Parse(const char *data_p[], char *target, size_t size, bool replaceWhitespaces)
Parse a token out of a string.
bool HTTP_Encode(const char *url, char *out, size_t outLength)
This function converts the given url to an URL encoded string. All input characters that are not a-z...
static void WEB_UploadCGame_f(void)
bool WEB_CGameUpload(const char *cgameId, int category, const char *filename)
Uploads a file to the server.
UFOAI web interface management. Authentication as well as uploading/downloading stuff to and from you...
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
void Q_strcat(char *dest, size_t destsize, const char *format,...)
Safely (without overflowing the destination buffer) concatenates two strings.
#define MEMBER_SIZEOF(TYPE, MEMBER)
bool Q_strreplace(const char *source, const char *pattern, const char *replace, char *dest, size_t destsize)
Replaces the first occurence of the given pattern in the source string with the given replace string...
bool WEB_CGameDelete(const char *cgameId, int category, const char *filename)
Deletes a user owned file on the server.
void WEB_CGameCvars(void)
UFOAI web interface management. c(lient)game related stuff.
const char * GAME_GetRelativeSavePath(char *buf, size_t bufSize)
bool WEB_CGameDownloadFromUser(const char *cgameId, int category, const char *filename, int userId)
Downloads a file from the server and store it in the user directory.
void UI_ExecuteConfunc(const char *fmt,...)
Executes confunc - just to identify those confuncs in the code - in this frame.
bool WEB_GetURL(const char *url, http_callback_t callback, void *userdata)
Downloads the given url and notify the callback. The login credentials are automatically added as GET...