add some test code for uri_post
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / gamecommand.qc
1 void GameCommand_Init()
2 {
3         // make gg call menu QC theCommands
4         localcmd("alias qc_cmd \"menu_cmd $*\"\n");
5 }
6
7 string _dumptree_space;
8 void _dumptree_open(entity pass, entity me)
9 {
10         string s;
11         s = me.toString(me);
12         if(s == "")
13                 s = me.classname;
14         else
15                 s = strcat(me.classname, ": ", s);
16         print(_dumptree_space, etos(me), " (", s, ")");
17         if(me.firstChild)
18         {
19                 print(" {\n");
20                 _dumptree_space = strcat(_dumptree_space, "  ");
21         }
22         else
23                 print("\n");
24 }
25 void _dumptree_close(entity pass, entity me)
26 {
27         if(me.firstChild)
28         {
29                 _dumptree_space = substring(_dumptree_space, 0, strlen(_dumptree_space) - 2);
30                 print(_dumptree_space, "}\n");
31         }
32 }
33
34 float curl_uri_get_pos;
35 float curl_uri_get_exec[URI_GET_CURL_END - URI_GET_CURL + 1];
36 string curl_uri_get_cvar[URI_GET_CURL_END - URI_GET_CURL + 1];
37 void Curl_URI_Get_Callback(float id, float status, string data)
38 {
39         float i;
40         float do_exec;
41         string do_cvar;
42         i = id - URI_GET_CURL;
43         do_exec = curl_uri_get_exec[i];
44         do_cvar = curl_uri_get_cvar[i];
45         if(status != 0)
46         {
47                 print("error: status is ", ftos(status), "\n");
48                 return;
49         }
50         if(do_exec)
51                 localcmd(data);
52         if(do_cvar)
53                 cvar_set(do_cvar, data);
54         if(!do_exec && !do_cvar)
55                 print(data);
56 }
57
58 void GameCommand(string theCommand)
59 {
60         float argc;
61         argc = tokenize_console(theCommand);
62
63         if(argv(0) == "help" || argc == 0)
64         {
65                 print("Usage: menu_cmd theCommand..., where possible theCommands are:\n");
66                 print("  sync - reloads all cvars on the current menu page\n");
67                 print("  directmenu ITEM - select a menu item as main item\n");
68                 GameCommand_Generic("help");
69                 return;
70         }
71
72         if(GameCommand_Generic(theCommand))
73                 return;
74
75         if(argv(0) == "sync")
76         {
77                 loadAllCvars(main);
78                 return;
79         }
80
81         if(argv(0) == "directmenu") if(argc == 2)
82         {
83                 // switch to a menu item
84                 if(!isdemo()) // don't allow this command in demos
85                         m_goto(argv(1));
86                 return;
87         }
88
89         if(argv(0) == "directpanelhudmenu")
90         {
91                 // switch to a menu item
92                 m_goto(strcat("HUD", argv(1)));
93                 return;
94         }
95
96         if(argv(0) == "skinselect")
97         {
98                 m_goto_skin_selector();
99                 return;
100         }
101
102         if(argv(0) == "videosettings")
103         {
104                 m_goto_video_settings();
105                 return;
106         }
107
108         if(argv(0) == "dumptree")
109         {
110                 _dumptree_space = "";
111                 depthfirst(main, parent, firstChild, nextSibling, _dumptree_open, _dumptree_close, NULL);
112                 return;
113         }
114
115         if(argv(0) == "setresolution")
116         {
117                 updateConwidths();
118                 return;
119         }
120
121         if(argv(0) == "setcompression")
122         {
123                 updateCompression();
124                 return;
125         }
126
127         if(argv(0) == "curl")
128         {
129                 float do_exec;
130                 string do_cvar;
131                 float key;
132                 float i, j;
133                 string url;
134                 float buf;
135
136                 do_exec = FALSE;
137                 do_cvar = string_null;
138                 key = -1;
139
140                 for(i = 1; i+1 < argc; ++i)
141                 {
142                         if(argv(i) == "--cvar" && i+2 < argc)
143                         {
144                                 ++i;
145                                 do_cvar = argv(i);
146                                 ++i;
147                                 continue;
148                         }
149                         if(argv(i) == "--exec")
150                         {
151                                 do_exec = TRUE;
152                                 ++i;
153                                 continue;
154                         }
155                         if(argv(i) == "--key" && i+2 < argc)
156                         {
157                                 ++i;
158                                 key = stof(argv(i));
159                                 ++i;
160                                 continue;
161                         }
162                         break;
163                 }
164
165                 // now, argv(i) is the URL
166                 // following args may be POST parameters
167                 url = argv(i);
168                 ++i;
169                 buf = buf_create();
170                 j = 0;
171                 for(; i+1 < argc; i += 2)
172                         bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
173                 if(i < argc)
174                         bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
175
176                 if(j == 0) // no args: GET
177                         crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
178                 else // with args: POST
179                         crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
180
181                 curl_uri_get_pos = mod(curl_uri_get_pos + 1, URI_GET_CURL_END - URI_GET_CURL + 1);
182
183                 buf_del(buf);
184
185                 return;
186         }
187
188 #if 0
189         if(argv(0) == "tokentest")
190         {
191                 string s;
192                 float i, n;
193
194                 print("SANE tokenizer:\n");
195                 s = cvar_string("tokentest");
196                 n = tokenize_console_force_builtin(s);
197                 for(i = -n; i < n; ++i)
198                 {
199                         print("token ", ftos(i), ": '", argv(i), "' = ");
200                         print(ftos(argv_start_index(i)), " to ", ftos(argv_end_index(i)), "\n");
201                 }
202                 print(".\n");
203
204                 print("INSANE tokenizer:\n");
205                 s = cvar_string("tokentest");
206                 n = tokenize(s);
207                 for(i = -n; i < n; ++i)
208                 {
209                         print("token ", ftos(i), ": '", argv(i), "' = ");
210                         print(ftos(argv_start_index(i)), " to ", ftos(argv_end_index(i)), "\n");
211                 }
212                 print(".\n");
213
214                 print("EMULATED tokenizer:\n");
215                 s = cvar_string("tokentest");
216                 n = tokenize_console_force_emulation(s);
217                 for(i = -n; i < n; ++i)
218                 {
219                         print("token ", ftos(i), ": '", argv(i), "' = ");
220                         print(ftos(argv_start_index(i)), " to ", ftos(argv_end_index(i)), "\n");
221                 }
222                 print(".\n");
223                 return;
224         }
225 #endif
226
227         print("Invalid theCommand. For a list of supported theCommands, try menu_cmd help.\n");
228 }