]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
provide the "curl" command in all QC VMs
authorRudolf Polzer <divverent@alientrap.org>
Tue, 7 Feb 2012 22:39:45 +0000 (23:39 +0100)
committerRudolf Polzer <divverent@alientrap.org>
Tue, 7 Feb 2012 22:40:53 +0000 (23:40 +0100)
14 files changed:
commands.cfg
qcsrc/client/miscfunctions.qc
qcsrc/client/progs.src
qcsrc/common/command/generic.qc
qcsrc/common/command/generic.qh
qcsrc/common/constants.qh
qcsrc/common/urllib.qc
qcsrc/common/urllib.qh
qcsrc/menu/command/menu_cmd.qc
qcsrc/menu/progs.src
qcsrc/menu/xonotic/util.qc
qcsrc/menu/xonotic/util.qh
qcsrc/server/ipban.qc
qcsrc/server/miscfunctions.qc

index b3a9062d29a3b3b0da16f48d0b1361cdcfdbc96b..174a458347b8b76e827f4ca670412c5e29f51059 100644 (file)
@@ -52,6 +52,7 @@ alias who                  "qc_cmd_svcmd  who                  ${* ?}" // Displa
 
 // generic commands (across all programs)
 alias addtolist            "qc_cmd_svmenu addtolist            ${* ?}" // Add a string to a cvar
+alias qc_curl              "qc_cmd_svmenu curl                 ${* ?}" // curl requests
 alias dumpcommands         "qc_cmd_svmenu dumpcommands         ${* ?}" // Dump all commands on the program to *_cmd_dump.txt
 alias maplist              "qc_cmd_svmenu maplist              ${* ?}" // Automatic control of maplist
 alias nextframe            "qc_cmd_svmenu nextframe            ${* ?}" // do something next frame
index 6e4e2dd87c37789c78f045aa9fb7836d94ee04b1..070096fbf4d6db912bb50d7f9c88f0eaa4e8de9c 100644 (file)
@@ -606,3 +606,24 @@ float getplayerisdead(float pl)
        
        return FALSE;
 }
+
+void URI_Get_Callback(float id, float status, string data)
+{
+       if(url_URI_Get_Callback(id, status, data))
+       {
+               // handled
+       }
+       else if (id == URI_GET_DISCARD)
+       {
+               // discard
+       }
+       else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
+       {
+               // sv_cmd curl
+               Curl_URI_Get_Callback(id, status, data);
+       }
+       else
+       {
+               print(sprintf(_("Received HTTP request data for an invalid id %d.\n"), id));
+       }
+}
index 972efb6a0ae5df9cbf5d777507a3bd6af53a980e..8756fe9447070c5999b5ac2d91c6b61d62e8cb1c 100644 (file)
@@ -22,6 +22,7 @@ Defs.qc
 ../common/command/rpn.qh
 ../common/command/generic.qh
 ../common/command/shared_defs.qh
+../common/urllib.qh
 
 command/cl_cmd.qh
 
@@ -99,6 +100,7 @@ noise.qc
 ../common/items.qc
 ../server/w_all.qc
 ../common/explosion_equation.qc
+../common/urllib.qc
 
 command/cl_cmd.qc
 
index d0cd7a3042cd1e993522c1377ff52864bdbbeaeb..68ee8c7129c89c90be0cd2fa807e422f80b77539 100644 (file)
@@ -65,6 +65,114 @@ void GenericCommand_addtolist(float request, float argc)
        }
 }
 
+float curl_uri_get_pos;
+float curl_uri_get_exec[URI_GET_CURL_END - URI_GET_CURL + 1];
+string curl_uri_get_cvar[URI_GET_CURL_END - URI_GET_CURL + 1];
+void Curl_URI_Get_Callback(float id, float status, string data)
+{
+       float i;
+       float do_exec;
+       string do_cvar;
+       i = id - URI_GET_CURL;
+       do_exec = curl_uri_get_exec[i];
+       do_cvar = curl_uri_get_cvar[i];
+       if(status != 0)
+       {
+               print(sprintf(_("error: status is %d\n"), status));
+               if(do_cvar)
+                       strunzone(do_cvar);
+               return;
+       }
+       if(do_exec)
+               localcmd(data);
+       if(do_cvar)
+       {
+               cvar_set(do_cvar, data);
+               strunzone(do_cvar);
+       }
+       if(!do_exec && !do_cvar)
+               print(data);
+}
+
+void GenericCommand_curl(float request, float argc)
+{
+       switch(request)
+       {
+               case CMD_REQUEST_COMMAND:
+               {
+                       float do_exec;
+                       string do_cvar;
+                       float key;
+                       float i, j;
+                       string url;
+                       float buf;
+                       float r;
+
+                       do_exec = FALSE;
+                       do_cvar = string_null;
+                       key = -1;
+
+                       for(i = 1; i+1 < argc; ++i)
+                       {
+                               if(argv(i) == "--cvar" && i+2 < argc)
+                               {
+                                       ++i;
+                                       do_cvar = strzone(argv(i));
+                                       continue;
+                               }
+                               if(argv(i) == "--exec")
+                               {
+                                       do_exec = TRUE;
+                                       continue;
+                               }
+                               if(argv(i) == "--key" && i+2 < argc)
+                               {
+                                       ++i;
+                                       key = stof(argv(i));
+                                       continue;
+                               }
+                               break;
+                       }
+
+                       // now, argv(i) is the URL
+                       // following args may be POST parameters
+                       url = argv(i);
+                       ++i;
+                       buf = buf_create();
+                       j = 0;
+                       for(; i+1 < argc; i += 2)
+                               bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
+                       if(i < argc)
+                               bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
+
+                       if(j == 0) // no args: GET
+                               r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
+                       else // with args: POST
+                               r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
+
+                       if(r)
+                       {
+                               curl_uri_get_exec[curl_uri_get_pos] = do_exec;
+                               curl_uri_get_cvar[curl_uri_get_pos] = do_cvar;
+                               curl_uri_get_pos = mod(curl_uri_get_pos + 1, URI_GET_CURL_END - URI_GET_CURL + 1);
+                       }
+                       else
+                               print(_("error creating curl handle\n"));
+
+                       buf_del(buf);
+
+                       return;
+               }
+                       
+               default:
+               case CMD_REQUEST_USAGE:
+               {
+                       print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " curl [--key N] [--cvar] [--exec] URL [postargs...]"));
+                       return;
+               }
+       }
+}
+
 void GenericCommand_dumpcommands(float request)
 {
        switch(request)
@@ -352,6 +460,7 @@ void GenericCommand_(float request)
 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
 #define GENERIC_COMMANDS(request,arguments,command) \
        GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
+       GENERIC_COMMAND("curl", GenericCommand_curl(request, arguments), "Queries an URL") \
        GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
        GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
        GENERIC_COMMAND("nextframe", GenericCommand_nextframe(request, arguments, command), "Execute the given command next frame of this VM") \
index 6fa7d382209bd66f10c5e12e667fb59d941ac80e..4391555c6cabe2c3828bed9d4e9d9b85c15bdf8a 100644 (file)
@@ -15,3 +15,5 @@ string GetProgramCommandPrefix(void);
 #define CMD_Write(s) fputs(fh, s)
 #define CMD_Write_Alias(execute,command,description) CMD_Write(sprintf("alias %-20s \"%-13s %-20s ${* ?}\" // %s\n", command, execute, command, description))
 void GenericCommand_macro_write_aliases(float fh);
+
+void Curl_URI_Get_Callback(float id, float status, string data);
index e413ad94ae65b0b817f29ce4c6f4bc3d9987767e..3e9795690165b3859408d6bc427c888e809971d9 100644 (file)
@@ -586,3 +586,13 @@ noref var vector autocvar_sv_player_headsize = '24 24 12';
 #define SPAWN_PRIO_NEAR_TEAMMATE_SAMETEAM 100
 #define SPAWN_PRIO_RACE_PREVIOUS_SPAWN     50
 #define SPAWN_PRIO_GOOD_DISTANCE           10
+
+// URI handles
+#define URI_GET_DISCARD              0
+#define URI_GET_IPBAN                1
+#define URI_GET_IPBAN_END           16
+#define URI_GET_CURL                17
+#define URI_GET_CURL_END            32
+#define URI_GET_UPDATENOTIFICATION  33
+#define URI_GET_URLLIB             128
+#define URI_GET_URLLIB_END         191
index 1bc06502734f66719fd81fabb14b17bb7bd14b3e..9a5e0e7da274c8b1043f3f52ec838b03590919ad 100644 (file)
 .url_ready_func url_ready;
 .entity url_ready_pass;
 
+// for multi handles
+.float url_attempt;
+.float url_mode;
+
 entity url_fromid[NUM_URL_ID];
 float autocvar__urllib_nextslot;
 
@@ -345,16 +349,16 @@ void url_multi_ready(entity fh, entity me, float status)
                        remove(me);
                        return;
                }
-               me.cnt += 1;
+               me.url_attempt += 1;
                n = tokenize_console(me.url_url);
-               if(n <= me.cnt)
+               if(n <= me.url_attempt)
                {
                        me.url_ready(fh, me.url_ready_pass, status);
                        strunzone(me.url_url);
                        remove(me);
                        return;
                }
-               url_single_fopen(argv(me.cnt), me.lip, url_multi_ready, me);
+               url_single_fopen(argv(me.url_attempt), me.url_mode, url_multi_ready, me);
                return;
        }
        me.url_ready(fh, me.url_ready_pass, status);
@@ -374,8 +378,8 @@ void url_multi_fopen(string url, float mode, url_ready_func rdy, entity pass)
        me = spawn();
        me.classname = "url_multi";
        me.url_url = strzone(url);
-       me.cnt = 0;
-       me.lip = mode;
+       me.url_attempt = 0;
+       me.url_mode = mode;
        me.url_ready = rdy;
        me.url_ready_pass = pass;
        url_single_fopen(argv(0), mode, url_multi_ready, me);
index f92178b2f6f6a6569b72af048fbba23811c0e69f..8a871c6b3fdebbfefc9945aca37c2c8dccdd7ab3 100644 (file)
@@ -12,7 +12,7 @@ void url_fputs(entity e, string s);
 
 // returns true if handled
 float url_URI_Get_Callback(float id, float status, string data);
-#define MIN_URL_ID 128
-#define NUM_URL_ID 64
+#define MIN_URL_ID URI_GET_URLLIB
+#define NUM_URL_ID (URI_GET_URLLIB_END - URI_GET_URLLIB + 1)
 
 void url_multi_fopen(string url, float mode, url_ready_func rdy, entity pass);
index 3be6edc6629612eda6aca51ce768e850bd48c7bf..0e0df058942eb2f7e2e011490f1a2193c1a9dc0e 100644 (file)
@@ -25,35 +25,6 @@ void _dumptree_close(entity pass, entity me)
        }
 }
 
-float curl_uri_get_pos;
-float curl_uri_get_exec[URI_GET_CURL_END - URI_GET_CURL + 1];
-string curl_uri_get_cvar[URI_GET_CURL_END - URI_GET_CURL + 1];
-void Curl_URI_Get_Callback(float id, float status, string data)
-{
-       float i;
-       float do_exec;
-       string do_cvar;
-       i = id - URI_GET_CURL;
-       do_exec = curl_uri_get_exec[i];
-       do_cvar = curl_uri_get_cvar[i];
-       if(status != 0)
-       {
-               print(sprintf(_("error: status is %d\n"), status));
-               if(do_cvar)
-                       strunzone(do_cvar);
-               return;
-       }
-       if(do_exec)
-               localcmd(data);
-       if(do_cvar)
-       {
-               cvar_set(do_cvar, data);
-               strunzone(do_cvar);
-       }
-       if(!do_exec && !do_cvar)
-               print(data);
-}
-
 void GameCommand(string theCommand)
 {
        float argc;
@@ -119,68 +90,6 @@ void GameCommand(string theCommand)
 
        if(argv(0) == "curl")
        {
-               float do_exec;
-               string do_cvar;
-               float key;
-               float i, j;
-               string url;
-               float buf;
-               float r;
-
-               do_exec = FALSE;
-               do_cvar = string_null;
-               key = -1;
-
-               for(i = 1; i+1 < argc; ++i)
-               {
-                       if(argv(i) == "--cvar" && i+2 < argc)
-                       {
-                               ++i;
-                               do_cvar = strzone(argv(i));
-                               continue;
-                       }
-                       if(argv(i) == "--exec")
-                       {
-                               do_exec = TRUE;
-                               continue;
-                       }
-                       if(argv(i) == "--key" && i+2 < argc)
-                       {
-                               ++i;
-                               key = stof(argv(i));
-                               continue;
-                       }
-                       break;
-               }
-
-               // now, argv(i) is the URL
-               // following args may be POST parameters
-               url = argv(i);
-               ++i;
-               buf = buf_create();
-               j = 0;
-               for(; i+1 < argc; i += 2)
-                       bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
-               if(i < argc)
-                       bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
-
-               if(j == 0) // no args: GET
-                       r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
-               else // with args: POST
-                       r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
-
-               if(r)
-               {
-                       curl_uri_get_exec[curl_uri_get_pos] = do_exec;
-                       curl_uri_get_cvar[curl_uri_get_pos] = do_cvar;
-                       curl_uri_get_pos = mod(curl_uri_get_pos + 1, URI_GET_CURL_END - URI_GET_CURL + 1);
-               }
-               else
-                       print(_("error creating curl handle\n"));
-
-               buf_del(buf);
-
-               return;
        }
 
        print(_("Invalid command. For a list of supported commands, try menu_cmd help.\n"));
index 1f70e0e7be2f0ce5aef5c87d036d36c696e3122c..0f545e60cfa368967ca4cb6f694132fff6558fd1 100644 (file)
@@ -19,6 +19,7 @@ oo/base.h
 ../common/command/rpn.qh
 ../common/command/generic.qh
 ../common/command/shared_defs.qh
+../common/urllib.qh
 
 command/menu_cmd.qh
 menu.qh
@@ -44,6 +45,7 @@ xonotic/util.qc
 ../common/campaign_setup.qc
 ../common/mapinfo.qc
 ../common/items.qc
+../common/urllib.qc
 
 ../warpzonelib/mathlib.qc
 
index 486bcce8dd0ac10095c3557131fada9592a32a82..ab86bebae013665cceb5dc20994d1bb858c03540 100644 (file)
@@ -259,18 +259,23 @@ string _Nex_ExtResponseSystem_UpdateToURL;
 
 void URI_Get_Callback(float id, float status, string data)
 {
-       if (id == URI_GET_DISCARD)
+       if(url_URI_Get_Callback(id, status, data))
        {
-               // discard
+               // handled
        }
-       else if(id == URI_GET_UPDATENOTIFICATION)
+       else if (id == URI_GET_DISCARD)
        {
-               UpdateNotification_URI_Get_Callback(id, status, data);
+               // discard
        }
-       else if(id >= URI_GET_CURL && id <= URI_GET_CURL_END)
+       else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
        {
+               // sv_cmd curl
                Curl_URI_Get_Callback(id, status, data);
        }
+       else if (id == URI_GET_UPDATENOTIFICATION)
+       {
+               UpdateNotification_URI_Get_Callback(id, status, data);
+       }
        else
        {
                print(sprintf(_("Received HTTP request data for an invalid id %d.\n"), id));
index f682fd30fd39116221344e16fea723a4424fd1bf..be13ee289b25607149d3e803621b1234eae0b6cc 100644 (file)
@@ -23,15 +23,8 @@ string getZonedTooltipForIdentifier(string s);
 
 string resolvemod(string m);
 
-float URI_GET_DISCARD = 0;
-
-float URI_GET_UPDATENOTIFICATION = 1;
 void UpdateNotification_URI_Get_Callback(float id, float status, string data);
 
-float URI_GET_CURL = 2;
-float URI_GET_CURL_END = 9;
-void Curl_URI_Get_Callback(float id, float status, string data);
-
 void URI_Get_Callback(float id, float status, string data);
 
 // game type list box stuff (does not NEED to contain all game types, other
index b40a1e3a289ad682f95cfe8a96b35e83d84a9aa3..2034b27dca1e285ce8f06db62f7e62e3c4aad5ac 100644 (file)
@@ -19,6 +19,8 @@
  *     server IP that registered the ban
  */
 
+#define MAX_IPBAN_URIS (URI_GET_IPBAN_END - URI_GET_IPBAN + 1)
+
 float Ban_Insert(string ip, float bantime, string reason, float dosync);
 
 void OnlineBanList_SendBan(string ip, float bantime, string reason)
index b780a8511a882f96ace60faa8e8a82a9223002c3..8afee84d0d16da20b40e33540e47f73cf8084af0 100644 (file)
@@ -1984,35 +1984,30 @@ float WarpZone_Projectile_Touch_ImpactFilter_Callback()
 }
 #define PROJECTILE_TOUCH if(WarpZone_Projectile_Touch()) return
 
-float MAX_IPBAN_URIS           = 16;
-                              
-float URI_GET_DISCARD          = 0;
-float URI_GET_IPBAN            = 1;
-float URI_GET_IPBAN_END        = 16;
-
 void URI_Get_Callback(float id, float status, string data)
 {
-    dprint("Received HTTP request data for id ", ftos(id), "; status is ", ftos(status), "\nData is:\n");
-    dprint(data);
-    dprint("\nEnd of data.\n");
-
-    if(url_URI_Get_Callback(id, status, data))
-    {
-        // handled
-    }
-    else if (id == URI_GET_DISCARD)
-    {
-        // discard
-    }
-    else if (id >= URI_GET_IPBAN && id <= URI_GET_IPBAN_END)
-    {
-        // online ban list
-        OnlineBanList_URI_Get_Callback(id, status, data);
-    }
-    else
-    {
-        print("Received HTTP request data for an invalid id ", ftos(id), ".\n");
-    }
+       if(url_URI_Get_Callback(id, status, data))
+       {
+               // handled
+       }
+       else if (id == URI_GET_DISCARD)
+       {
+               // discard
+       }
+       else if (id >= URI_GET_CURL && id <= URI_GET_CURL_END)
+       {
+               // sv_cmd curl
+               Curl_URI_Get_Callback(id, status, data);
+       }
+       else if (id >= URI_GET_IPBAN && id <= URI_GET_IPBAN_END)
+       {
+               // online ban list
+               OnlineBanList_URI_Get_Callback(id, status, data);
+       }
+       else
+       {
+               print("Received HTTP request data for an invalid id ", ftos(id), ".\n");
+       }
 }
 
 string uid2name(string myuid) {