]> git.xonotic.org Git - xonotic/darkplaces.git/blob - host_cmd.c
c7f36efd7274d386e98f97e9170649495572525e
[xonotic/darkplaces.git] / host_cmd.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "quakedef.h"
22 #include "sv_demo.h"
23 #include "image.h"
24
25 #include "prvm_cmds.h"
26 #include "utf8lib.h"
27
28 // for secure rcon authentication
29 #include "hmac.h"
30 #include "mdfour.h"
31 #include <time.h>
32
33 int current_skill;
34 extern cvar_t sv_adminnick;
35 extern cvar_t sv_status_privacy;
36 extern cvar_t sv_status_show_qcstatus;
37 extern cvar_t sv_namechangetimer;
38 cvar_t rcon_password = {CVAR_CLIENT | CVAR_SERVER | CVAR_PRIVATE, "rcon_password", "", "password to authenticate rcon commands; NOTE: changing rcon_secure clears rcon_password, so set rcon_secure always before rcon_password; may be set to a string of the form user1:pass1 user2:pass2 user3:pass3 to allow multiple user accounts - the client then has to specify ONE of these combinations"};
39 cvar_t rcon_secure = {CVAR_CLIENT | CVAR_SERVER | CVAR_NQUSERINFOHACK, "rcon_secure", "0", "force secure rcon authentication (1 = time based, 2 = challenge based); NOTE: changing rcon_secure clears rcon_password, so set rcon_secure always before rcon_password"};
40 cvar_t rcon_secure_challengetimeout = {CVAR_CLIENT, "rcon_secure_challengetimeout", "5", "challenge-based secure rcon: time out requests if no challenge came within this time interval"};
41 cvar_t rcon_address = {CVAR_CLIENT, "rcon_address", "", "server address to send rcon commands to (when not connected to a server)"};
42 cvar_t team = {CVAR_CLIENT | CVAR_USERINFO | CVAR_SAVE, "team", "none", "QW team (4 character limit, example: blue)"};
43 cvar_t skin = {CVAR_CLIENT | CVAR_USERINFO | CVAR_SAVE, "skin", "", "QW player skin name (example: base)"};
44 cvar_t noaim = {CVAR_CLIENT | CVAR_USERINFO | CVAR_SAVE, "noaim", "1", "QW option to disable vertical autoaim"};
45 cvar_t r_fixtrans_auto = {CVAR_CLIENT, "r_fixtrans_auto", "0", "automatically fixtrans textures (when set to 2, it also saves the fixed versions to a fixtrans directory)"};
46
47 //============================================================================
48
49 /*
50 ======================
51 CL_Name_f
52 ======================
53 */
54 cvar_t cl_name = {CVAR_CLIENT | CVAR_SAVE | CVAR_NQUSERINFOHACK, "_cl_name", "player", "internal storage cvar for current player name (changed by name command)"};
55 static void CL_Name_f(cmd_state_t *cmd)
56 {
57         prvm_prog_t *prog = SVVM_prog;
58         int i, j;
59         qboolean valid_colors;
60         const char *newNameSource;
61         char newName[sizeof(host_client->name)];
62
63         if (Cmd_Argc (cmd) == 1)
64         {
65                 if (cmd->source == src_command)
66                 {
67                         Con_Printf("name: %s\n", cl_name.string);
68                 }
69                 return;
70         }
71
72         if (Cmd_Argc (cmd) == 2)
73                 newNameSource = Cmd_Argv(cmd, 1);
74         else
75                 newNameSource = Cmd_Args(cmd);
76
77         strlcpy(newName, newNameSource, sizeof(newName));
78
79         if (cmd->source == src_command)
80         {
81                 Cvar_Set (&cvars_all, "_cl_name", newName);
82                 if (strlen(newNameSource) >= sizeof(newName)) // overflowed
83                 {
84                         Con_Printf("Your name is longer than %i chars! It has been truncated.\n", (int) (sizeof(newName) - 1));
85                         Con_Printf("name: %s\n", cl_name.string);
86                 }
87                 return;
88         }
89
90         if (host.realtime < host_client->nametime)
91         {
92                 SV_ClientPrintf("You can't change name more than once every %.1f seconds!\n", max(0.0f, sv_namechangetimer.value));
93                 return;
94         }
95
96         host_client->nametime = host.realtime + max(0.0f, sv_namechangetimer.value);
97
98         // point the string back at updateclient->name to keep it safe
99         strlcpy (host_client->name, newName, sizeof (host_client->name));
100
101         for (i = 0, j = 0;host_client->name[i];i++)
102                 if (host_client->name[i] != '\r' && host_client->name[i] != '\n')
103                         host_client->name[j++] = host_client->name[i];
104         host_client->name[j] = 0;
105
106         if(host_client->name[0] == 1 || host_client->name[0] == 2)
107         // may interfere with chat area, and will needlessly beep; so let's add a ^7
108         {
109                 memmove(host_client->name + 2, host_client->name, sizeof(host_client->name) - 2);
110                 host_client->name[sizeof(host_client->name) - 1] = 0;
111                 host_client->name[0] = STRING_COLOR_TAG;
112                 host_client->name[1] = '0' + STRING_COLOR_DEFAULT;
113         }
114
115         u8_COM_StringLengthNoColors(host_client->name, 0, &valid_colors);
116         if(!valid_colors) // NOTE: this also proves the string is not empty, as "" is a valid colored string
117         {
118                 size_t l;
119                 l = strlen(host_client->name);
120                 if(l < sizeof(host_client->name) - 1)
121                 {
122                         // duplicate the color tag to escape it
123                         host_client->name[i] = STRING_COLOR_TAG;
124                         host_client->name[i+1] = 0;
125                         //Con_DPrintf("abuse detected, adding another trailing color tag\n");
126                 }
127                 else
128                 {
129                         // remove the last character to fix the color code
130                         host_client->name[l-1] = 0;
131                         //Con_DPrintf("abuse detected, removing a trailing color tag\n");
132                 }
133         }
134
135         // find the last color tag offset and decide if we need to add a reset tag
136         for (i = 0, j = -1;host_client->name[i];i++)
137         {
138                 if (host_client->name[i] == STRING_COLOR_TAG)
139                 {
140                         if (host_client->name[i+1] >= '0' && host_client->name[i+1] <= '9')
141                         {
142                                 j = i;
143                                 // if this happens to be a reset  tag then we don't need one
144                                 if (host_client->name[i+1] == '0' + STRING_COLOR_DEFAULT)
145                                         j = -1;
146                                 i++;
147                                 continue;
148                         }
149                         if (host_client->name[i+1] == STRING_COLOR_RGB_TAG_CHAR && isxdigit(host_client->name[i+2]) && isxdigit(host_client->name[i+3]) && isxdigit(host_client->name[i+4]))
150                         {
151                                 j = i;
152                                 i += 4;
153                                 continue;
154                         }
155                         if (host_client->name[i+1] == STRING_COLOR_TAG)
156                         {
157                                 i++;
158                                 continue;
159                         }
160                 }
161         }
162         // does not end in the default color string, so add it
163         if (j >= 0 && strlen(host_client->name) < sizeof(host_client->name) - 2)
164                 memcpy(host_client->name + strlen(host_client->name), STRING_COLOR_DEFAULT_STR, strlen(STRING_COLOR_DEFAULT_STR) + 1);
165
166         PRVM_serveredictstring(host_client->edict, netname) = PRVM_SetEngineString(prog, host_client->name);
167         if (strcmp(host_client->old_name, host_client->name))
168         {
169                 if (host_client->begun)
170                         SV_BroadcastPrintf("%s ^7changed name to %s\n", host_client->old_name, host_client->name);
171                 strlcpy(host_client->old_name, host_client->name, sizeof(host_client->old_name));
172                 // send notification to all clients
173                 MSG_WriteByte (&sv.reliable_datagram, svc_updatename);
174                 MSG_WriteByte (&sv.reliable_datagram, host_client - svs.clients);
175                 MSG_WriteString (&sv.reliable_datagram, host_client->name);
176                 SV_WriteNetnameIntoDemo(host_client);
177         }
178 }
179
180 /*
181 ======================
182 CL_Playermodel_f
183 ======================
184 */
185 cvar_t cl_playermodel = {CVAR_CLIENT | CVAR_SAVE | CVAR_NQUSERINFOHACK, "_cl_playermodel", "", "internal storage cvar for current player model in Nexuiz/Xonotic (changed by playermodel command)"};
186 // the old cl_playermodel in cl_main has been renamed to __cl_playermodel
187 static void CL_Playermodel_f(cmd_state_t *cmd)
188 {
189         prvm_prog_t *prog = SVVM_prog;
190         int i, j;
191         char newPath[sizeof(host_client->playermodel)];
192
193         if (Cmd_Argc (cmd) == 1)
194         {
195                 if (cmd->source == src_command)
196                 {
197                         Con_Printf("\"playermodel\" is \"%s\"\n", cl_playermodel.string);
198                 }
199                 return;
200         }
201
202         if (Cmd_Argc (cmd) == 2)
203                 strlcpy (newPath, Cmd_Argv(cmd, 1), sizeof (newPath));
204         else
205                 strlcpy (newPath, Cmd_Args(cmd), sizeof (newPath));
206
207         for (i = 0, j = 0;newPath[i];i++)
208                 if (newPath[i] != '\r' && newPath[i] != '\n')
209                         newPath[j++] = newPath[i];
210         newPath[j] = 0;
211
212         if (cmd->source == src_command)
213         {
214                 Cvar_Set (&cvars_all, "_cl_playermodel", newPath);
215                 return;
216         }
217
218         /*
219         if (host.realtime < host_client->nametime)
220         {
221                 SV_ClientPrintf("You can't change playermodel more than once every 5 seconds!\n");
222                 return;
223         }
224
225         host_client->nametime = host.realtime + 5;
226         */
227
228         // point the string back at updateclient->name to keep it safe
229         strlcpy (host_client->playermodel, newPath, sizeof (host_client->playermodel));
230         PRVM_serveredictstring(host_client->edict, playermodel) = PRVM_SetEngineString(prog, host_client->playermodel);
231         if (strcmp(host_client->old_model, host_client->playermodel))
232         {
233                 strlcpy(host_client->old_model, host_client->playermodel, sizeof(host_client->old_model));
234                 /*// send notification to all clients
235                 MSG_WriteByte (&sv.reliable_datagram, svc_updatepmodel);
236                 MSG_WriteByte (&sv.reliable_datagram, host_client - svs.clients);
237                 MSG_WriteString (&sv.reliable_datagram, host_client->playermodel);*/
238         }
239 }
240
241 /*
242 ======================
243 CL_Playerskin_f
244 ======================
245 */
246 cvar_t cl_playerskin = {CVAR_CLIENT | CVAR_SAVE | CVAR_NQUSERINFOHACK, "_cl_playerskin", "", "internal storage cvar for current player skin in Nexuiz/Xonotic (changed by playerskin command)"};
247 static void CL_Playerskin_f(cmd_state_t *cmd)
248 {
249         prvm_prog_t *prog = SVVM_prog;
250         int i, j;
251         char newPath[sizeof(host_client->playerskin)];
252
253         if (Cmd_Argc (cmd) == 1)
254         {
255                 if (cmd->source == src_command)
256                 {
257                         Con_Printf("\"playerskin\" is \"%s\"\n", cl_playerskin.string);
258                 }
259                 return;
260         }
261
262         if (Cmd_Argc (cmd) == 2)
263                 strlcpy (newPath, Cmd_Argv(cmd, 1), sizeof (newPath));
264         else
265                 strlcpy (newPath, Cmd_Args(cmd), sizeof (newPath));
266
267         for (i = 0, j = 0;newPath[i];i++)
268                 if (newPath[i] != '\r' && newPath[i] != '\n')
269                         newPath[j++] = newPath[i];
270         newPath[j] = 0;
271
272         if (cmd->source == src_command)
273         {
274                 Cvar_Set (&cvars_all, "_cl_playerskin", newPath);
275                 return;
276         }
277
278         /*
279         if (host.realtime < host_client->nametime)
280         {
281                 SV_ClientPrintf("You can't change playermodel more than once every 5 seconds!\n");
282                 return;
283         }
284
285         host_client->nametime = host.realtime + 5;
286         */
287
288         // point the string back at updateclient->name to keep it safe
289         strlcpy (host_client->playerskin, newPath, sizeof (host_client->playerskin));
290         PRVM_serveredictstring(host_client->edict, playerskin) = PRVM_SetEngineString(prog, host_client->playerskin);
291         if (strcmp(host_client->old_skin, host_client->playerskin))
292         {
293                 //if (host_client->begun)
294                 //      SV_BroadcastPrintf("%s changed skin to %s\n", host_client->name, host_client->playerskin);
295                 strlcpy(host_client->old_skin, host_client->playerskin, sizeof(host_client->old_skin));
296                 /*// send notification to all clients
297                 MSG_WriteByte (&sv.reliable_datagram, svc_updatepskin);
298                 MSG_WriteByte (&sv.reliable_datagram, host_client - svs.clients);
299                 MSG_WriteString (&sv.reliable_datagram, host_client->playerskin);*/
300         }
301 }
302
303 /*
304 ==================
305 CL_Color_f
306 ==================
307 */
308 cvar_t cl_color = {CVAR_CLIENT | CVAR_SAVE | CVAR_NQUSERINFOHACK, "_cl_color", "0", "internal storage cvar for current player colors (changed by color command)"};
309 static void CL_Color(cmd_state_t *cmd, int changetop, int changebottom)
310 {
311         prvm_prog_t *prog = SVVM_prog;
312         int top, bottom, playercolor;
313
314         // get top and bottom either from the provided values or the current values
315         // (allows changing only top or bottom, or both at once)
316         top = changetop >= 0 ? changetop : (cl_color.integer >> 4);
317         bottom = changebottom >= 0 ? changebottom : cl_color.integer;
318
319         top &= 15;
320         bottom &= 15;
321         // LadyHavoc: allowing skin colormaps 14 and 15 by commenting this out
322         //if (top > 13)
323         //      top = 13;
324         //if (bottom > 13)
325         //      bottom = 13;
326
327         playercolor = top*16 + bottom;
328
329         if (cmd->source == src_command)
330         {
331                 Cvar_SetValueQuick(&cl_color, playercolor);
332                 return;
333         }
334
335         if (cls.protocol == PROTOCOL_QUAKEWORLD)
336                 return;
337
338         if (host_client->edict && PRVM_serverfunction(SV_ChangeTeam))
339         {
340                 Con_DPrint("Calling SV_ChangeTeam\n");
341                 prog->globals.fp[OFS_PARM0] = playercolor;
342                 PRVM_serverglobalfloat(time) = sv.time;
343                 PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(host_client->edict);
344                 prog->ExecuteProgram(prog, PRVM_serverfunction(SV_ChangeTeam), "QC function SV_ChangeTeam is missing");
345         }
346         else
347         {
348                 if (host_client->edict)
349                 {
350                         PRVM_serveredictfloat(host_client->edict, clientcolors) = playercolor;
351                         PRVM_serveredictfloat(host_client->edict, team) = bottom + 1;
352                 }
353                 host_client->colors = playercolor;
354                 if (host_client->old_colors != host_client->colors)
355                 {
356                         host_client->old_colors = host_client->colors;
357                         // send notification to all clients
358                         MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
359                         MSG_WriteByte (&sv.reliable_datagram, host_client - svs.clients);
360                         MSG_WriteByte (&sv.reliable_datagram, host_client->colors);
361                 }
362         }
363 }
364
365 static void CL_Color_f(cmd_state_t *cmd)
366 {
367         int             top, bottom;
368
369         if (Cmd_Argc(cmd) == 1)
370         {
371                 if (cmd->source == src_command)
372                 {
373                         Con_Printf("\"color\" is \"%i %i\"\n", cl_color.integer >> 4, cl_color.integer & 15);
374                         Con_Print("color <0-15> [0-15]\n");
375                 }
376                 return;
377         }
378
379         if (Cmd_Argc(cmd) == 2)
380                 top = bottom = atoi(Cmd_Argv(cmd, 1));
381         else
382         {
383                 top = atoi(Cmd_Argv(cmd, 1));
384                 bottom = atoi(Cmd_Argv(cmd, 2));
385         }
386         CL_Color(cmd, top, bottom);
387 }
388
389 static void CL_TopColor_f(cmd_state_t *cmd)
390 {
391         if (Cmd_Argc(cmd) == 1)
392         {
393                 if (cmd->source == src_command)
394                 {
395                         Con_Printf("\"topcolor\" is \"%i\"\n", (cl_color.integer >> 4) & 15);
396                         Con_Print("topcolor <0-15>\n");
397                 }
398                 return;
399         }
400
401         CL_Color(cmd, atoi(Cmd_Argv(cmd, 1)), -1);
402 }
403
404 static void CL_BottomColor_f(cmd_state_t *cmd)
405 {
406         if (Cmd_Argc(cmd) == 1)
407         {
408                 if (cmd->source == src_command)
409                 {
410                         Con_Printf("\"bottomcolor\" is \"%i\"\n", cl_color.integer & 15);
411                         Con_Print("bottomcolor <0-15>\n");
412                 }
413                 return;
414         }
415
416         CL_Color(cmd, -1, atoi(Cmd_Argv(cmd, 1)));
417 }
418
419 cvar_t cl_rate = {CVAR_CLIENT | CVAR_SAVE | CVAR_NQUSERINFOHACK, "_cl_rate", "20000", "internal storage cvar for current rate (changed by rate command)"};
420 cvar_t cl_rate_burstsize = {CVAR_CLIENT | CVAR_SAVE | CVAR_NQUSERINFOHACK, "_cl_rate_burstsize", "1024", "internal storage cvar for current rate control burst size (changed by rate_burstsize command)"};
421 static void CL_Rate_f(cmd_state_t *cmd)
422 {
423         int rate;
424
425         if (Cmd_Argc(cmd) != 2)
426         {
427                 if (cmd->source == src_command)
428                 {
429                         Con_Printf("\"rate\" is \"%i\"\n", cl_rate.integer);
430                         Con_Print("rate <bytespersecond>\n");
431                 }
432                 return;
433         }
434
435         rate = atoi(Cmd_Argv(cmd, 1));
436
437         if (cmd->source == src_command)
438         {
439                 Cvar_SetValue (&cvars_all, "_cl_rate", max(NET_MINRATE, rate));
440                 return;
441         }
442
443         host_client->rate = rate;
444 }
445
446 static void CL_Rate_BurstSize_f(cmd_state_t *cmd)
447 {
448         int rate_burstsize;
449
450         if (Cmd_Argc(cmd) != 2)
451         {
452                 Con_Printf("\"rate_burstsize\" is \"%i\"\n", cl_rate_burstsize.integer);
453                 Con_Print("rate_burstsize <bytes>\n");
454                 return;
455         }
456
457         rate_burstsize = atoi(Cmd_Argv(cmd, 1));
458
459         if (cmd->source == src_command)
460         {
461                 Cvar_SetValue (&cvars_all, "_cl_rate_burstsize", rate_burstsize);
462                 return;
463         }
464
465         host_client->rate_burstsize = rate_burstsize;
466 }
467
468 /*
469 ======================
470 CL_PModel_f
471 LadyHavoc: only supported for Nehahra, I personally think this is dumb, but Mindcrime won't listen.
472 LadyHavoc: correction, Mindcrime will be removing pmodel in the future, but it's still stuck here for compatibility.
473 ======================
474 */
475 cvar_t cl_pmodel = {CVAR_CLIENT | CVAR_SAVE | CVAR_NQUSERINFOHACK, "_cl_pmodel", "0", "internal storage cvar for current player model number in nehahra (changed by pmodel command)"};
476 static void CL_PModel_f(cmd_state_t *cmd)
477 {
478         prvm_prog_t *prog = SVVM_prog;
479         int i;
480
481         if (Cmd_Argc (cmd) == 1)
482         {
483                 if (cmd->source == src_command)
484                 {
485                         Con_Printf("\"pmodel\" is \"%s\"\n", cl_pmodel.string);
486                 }
487                 return;
488         }
489         i = atoi(Cmd_Argv(cmd, 1));
490
491         if (cmd->source == src_command)
492         {
493                 if (cl_pmodel.integer == i)
494                         return;
495                 Cvar_SetValue (&cvars_all, "_cl_pmodel", i);
496                 if (cls.state == ca_connected)
497                         Cmd_ForwardToServer_f(cmd);
498                 return;
499         }
500
501         PRVM_serveredictfloat(host_client->edict, pmodel) = i;
502 }
503
504 //===========================================================================
505
506 //===========================================================================
507
508 static void CL_SendCvar_f(cmd_state_t *cmd)
509 {
510         int             i;
511         cvar_t  *c;
512         const char *cvarname;
513         client_t *old;
514         char vabuf[1024];
515
516         if(Cmd_Argc(cmd) != 2)
517                 return;
518         cvarname = Cmd_Argv(cmd, 1);
519         if (cls.state == ca_connected)
520         {
521                 c = Cvar_FindVar(&cvars_all, cvarname, CVAR_CLIENT | CVAR_SERVER);
522                 // LadyHavoc: if there is no such cvar or if it is private, send a
523                 // reply indicating that it has no value
524                 if(!c || (c->flags & CVAR_PRIVATE))
525                         Cmd_ForwardStringToServer(va(vabuf, sizeof(vabuf), "sentcvar %s", cvarname));
526                 else
527                         Cmd_ForwardStringToServer(va(vabuf, sizeof(vabuf), "sentcvar %s \"%s\"", c->name, c->string));
528                 return;
529         }
530         if(!sv.active)// || !PRVM_serverfunction(SV_ParseClientCommand))
531                 return;
532
533         old = host_client;
534         if (cls.state != ca_dedicated)
535                 i = 1;
536         else
537                 i = 0;
538         for(;i<svs.maxclients;i++)
539                 if(svs.clients[i].active && svs.clients[i].netconnection)
540                 {
541                         host_client = &svs.clients[i];
542                         SV_ClientCommands("sendcvar %s\n", cvarname);
543                 }
544         host_client = old;
545 }
546
547 /*
548 =====================
549 CL_PQRcon_f
550
551 ProQuake rcon support
552 =====================
553 */
554 static void CL_PQRcon_f(cmd_state_t *cmd)
555 {
556         int n;
557         const char *e;
558         lhnetsocket_t *mysocket;
559
560         if (Cmd_Argc(cmd) == 1)
561         {
562                 Con_Printf("%s: Usage: %s command\n", Cmd_Argv(cmd, 0), Cmd_Argv(cmd, 0));
563                 return;
564         }
565
566         if (!rcon_password.string || !rcon_password.string[0] || rcon_secure.integer > 0)
567         {
568                 Con_Printf ("You must set rcon_password before issuing an pqrcon command, and rcon_secure must be 0.\n");
569                 return;
570         }
571
572         e = strchr(rcon_password.string, ' ');
573         n = e ? e-rcon_password.string : (int)strlen(rcon_password.string);
574
575         if (cls.netcon)
576                 cls.rcon_address = cls.netcon->peeraddress;
577         else
578         {
579                 if (!rcon_address.string[0])
580                 {
581                         Con_Printf ("You must either be connected, or set the rcon_address cvar to issue rcon commands\n");
582                         return;
583                 }
584                 LHNETADDRESS_FromString(&cls.rcon_address, rcon_address.string, sv_netport.integer);
585         }
586         mysocket = NetConn_ChooseClientSocketForAddress(&cls.rcon_address);
587         if (mysocket)
588         {
589                 sizebuf_t buf;
590                 unsigned char bufdata[64];
591                 buf.data = bufdata;
592                 SZ_Clear(&buf);
593                 MSG_WriteLong(&buf, 0);
594                 MSG_WriteByte(&buf, CCREQ_RCON);
595                 SZ_Write(&buf, (const unsigned char*)rcon_password.string, n);
596                 MSG_WriteByte(&buf, 0); // terminate the (possibly partial) string
597                 MSG_WriteString(&buf, Cmd_Args(cmd));
598                 StoreBigLong(buf.data, NETFLAG_CTL | (buf.cursize & NETFLAG_LENGTH_MASK));
599                 NetConn_Write(mysocket, buf.data, buf.cursize, &cls.rcon_address);
600                 SZ_Clear(&buf);
601         }
602 }
603
604 //=============================================================================
605
606 // QuakeWorld commands
607
608 /*
609 =====================
610 CL_Rcon_f
611
612   Send the rest of the command line over as
613   an unconnected command.
614 =====================
615 */
616 static void CL_Rcon_f(cmd_state_t *cmd) // credit: taken from QuakeWorld
617 {
618         int i, n;
619         const char *e;
620         lhnetsocket_t *mysocket;
621
622         if (Cmd_Argc(cmd) == 1)
623         {
624                 Con_Printf("%s: Usage: %s command\n", Cmd_Argv(cmd, 0), Cmd_Argv(cmd, 0));
625                 return;
626         }
627
628         if (!rcon_password.string || !rcon_password.string[0])
629         {
630                 Con_Printf ("You must set rcon_password before issuing an rcon command.\n");
631                 return;
632         }
633
634         e = strchr(rcon_password.string, ' ');
635         n = e ? e-rcon_password.string : (int)strlen(rcon_password.string);
636
637         if (cls.netcon)
638                 cls.rcon_address = cls.netcon->peeraddress;
639         else
640         {
641                 if (!rcon_address.string[0])
642                 {
643                         Con_Printf ("You must either be connected, or set the rcon_address cvar to issue rcon commands\n");
644                         return;
645                 }
646                 LHNETADDRESS_FromString(&cls.rcon_address, rcon_address.string, sv_netport.integer);
647         }
648         mysocket = NetConn_ChooseClientSocketForAddress(&cls.rcon_address);
649         if (mysocket && Cmd_Args(cmd)[0])
650         {
651                 // simply put together the rcon packet and send it
652                 if(Cmd_Argv(cmd, 0)[0] == 's' || rcon_secure.integer > 1)
653                 {
654                         if(cls.rcon_commands[cls.rcon_ringpos][0])
655                         {
656                                 char s[128];
657                                 LHNETADDRESS_ToString(&cls.rcon_addresses[cls.rcon_ringpos], s, sizeof(s), true);
658                                 Con_Printf("rcon to %s (for command %s) failed: too many buffered commands (possibly increase MAX_RCONS)\n", s, cls.rcon_commands[cls.rcon_ringpos]);
659                                 cls.rcon_commands[cls.rcon_ringpos][0] = 0;
660                                 --cls.rcon_trying;
661                         }
662                         for (i = 0;i < MAX_RCONS;i++)
663                                 if(cls.rcon_commands[i][0])
664                                         if (!LHNETADDRESS_Compare(&cls.rcon_address, &cls.rcon_addresses[i]))
665                                                 break;
666                         ++cls.rcon_trying;
667                         if(i >= MAX_RCONS)
668                                 NetConn_WriteString(mysocket, "\377\377\377\377getchallenge", &cls.rcon_address); // otherwise we'll request the challenge later
669                         strlcpy(cls.rcon_commands[cls.rcon_ringpos], Cmd_Args(cmd), sizeof(cls.rcon_commands[cls.rcon_ringpos]));
670                         cls.rcon_addresses[cls.rcon_ringpos] = cls.rcon_address;
671                         cls.rcon_timeout[cls.rcon_ringpos] = host.realtime + rcon_secure_challengetimeout.value;
672                         cls.rcon_ringpos = (cls.rcon_ringpos + 1) % MAX_RCONS;
673                 }
674                 else if(rcon_secure.integer > 0)
675                 {
676                         char buf[1500];
677                         char argbuf[1500];
678                         dpsnprintf(argbuf, sizeof(argbuf), "%ld.%06d %s", (long) time(NULL), (int) (rand() % 1000000), Cmd_Args(cmd));
679                         memcpy(buf, "\377\377\377\377srcon HMAC-MD4 TIME ", 24);
680                         if(HMAC_MDFOUR_16BYTES((unsigned char *) (buf + 24), (unsigned char *) argbuf, (int)strlen(argbuf), (unsigned char *) rcon_password.string, n))
681                         {
682                                 buf[40] = ' ';
683                                 strlcpy(buf + 41, argbuf, sizeof(buf) - 41);
684                                 NetConn_Write(mysocket, buf, 41 + (int)strlen(buf + 41), &cls.rcon_address);
685                         }
686                 }
687                 else
688                 {
689                         char buf[1500];
690                         memcpy(buf, "\377\377\377\377", 4);
691                         dpsnprintf(buf+4, sizeof(buf)-4, "rcon %.*s %s",  n, rcon_password.string, Cmd_Args(cmd));
692                         NetConn_WriteString(mysocket, buf, &cls.rcon_address);
693                 }
694         }
695 }
696
697 /*
698 ==================
699 CL_FullServerinfo_f
700
701 Sent by server when serverinfo changes
702 ==================
703 */
704 // TODO: shouldn't this be a cvar instead?
705 static void CL_FullServerinfo_f(cmd_state_t *cmd) // credit: taken from QuakeWorld
706 {
707         char temp[512];
708         if (Cmd_Argc(cmd) != 2)
709         {
710                 Con_Printf ("usage: fullserverinfo <complete info string>\n");
711                 return;
712         }
713
714         strlcpy (cl.qw_serverinfo, Cmd_Argv(cmd, 1), sizeof(cl.qw_serverinfo));
715         InfoString_GetValue(cl.qw_serverinfo, "teamplay", temp, sizeof(temp));
716         cl.qw_teamplay = atoi(temp);
717 }
718
719 /*
720 ==================
721 CL_FullInfo_f
722
723 Allow clients to change userinfo
724 ==================
725 Casey was here :)
726 */
727 static void CL_FullInfo_f(cmd_state_t *cmd) // credit: taken from QuakeWorld
728 {
729         char key[512];
730         char value[512];
731         const char *s;
732
733         if (Cmd_Argc(cmd) != 2)
734         {
735                 Con_Printf ("fullinfo <complete info string>\n");
736                 return;
737         }
738
739         s = Cmd_Argv(cmd, 1);
740         if (*s == '\\')
741                 s++;
742         while (*s)
743         {
744                 size_t len = strcspn(s, "\\");
745                 if (len >= sizeof(key)) {
746                         len = sizeof(key) - 1;
747                 }
748                 strlcpy(key, s, len + 1);
749                 s += len;
750                 if (!*s)
751                 {
752                         Con_Printf ("MISSING VALUE\n");
753                         return;
754                 }
755                 ++s; // Skip over backslash.
756
757                 len = strcspn(s, "\\");
758                 if (len >= sizeof(value)) {
759                         len = sizeof(value) - 1;
760                 }
761                 strlcpy(value, s, len + 1);
762
763                 CL_SetInfo(key, value, false, false, false, false);
764
765                 s += len;
766                 if (!*s)
767                 {
768                         break;
769                 }
770                 ++s; // Skip over backslash.
771         }
772 }
773
774 /*
775 ==================
776 CL_SetInfo_f
777
778 Allow clients to change userinfo
779 ==================
780 */
781 static void CL_SetInfo_f(cmd_state_t *cmd) // credit: taken from QuakeWorld
782 {
783         if (Cmd_Argc(cmd) == 1)
784         {
785                 InfoString_Print(cls.userinfo);
786                 return;
787         }
788         if (Cmd_Argc(cmd) != 3)
789         {
790                 Con_Printf ("usage: setinfo [ <key> <value> ]\n");
791                 return;
792         }
793         CL_SetInfo(Cmd_Argv(cmd, 1), Cmd_Argv(cmd, 2), true, false, false, false);
794 }
795
796 /*
797 ====================
798 CL_Packet_f
799
800 packet <destination> <contents>
801
802 Contents allows \n escape character
803 ====================
804 */
805 static void CL_Packet_f(cmd_state_t *cmd) // credit: taken from QuakeWorld
806 {
807         char send[2048];
808         int i, l;
809         const char *in;
810         char *out;
811         lhnetaddress_t address;
812         lhnetsocket_t *mysocket;
813
814         if (Cmd_Argc(cmd) != 3)
815         {
816                 Con_Printf ("packet <destination> <contents>\n");
817                 return;
818         }
819
820         if (!LHNETADDRESS_FromString (&address, Cmd_Argv(cmd, 1), sv_netport.integer))
821         {
822                 Con_Printf ("Bad address\n");
823                 return;
824         }
825
826         in = Cmd_Argv(cmd, 2);
827         out = send+4;
828         send[0] = send[1] = send[2] = send[3] = -1;
829
830         l = (int)strlen (in);
831         for (i=0 ; i<l ; i++)
832         {
833                 if (out >= send + sizeof(send) - 1)
834                         break;
835                 if (in[i] == '\\' && in[i+1] == 'n')
836                 {
837                         *out++ = '\n';
838                         i++;
839                 }
840                 else if (in[i] == '\\' && in[i+1] == '0')
841                 {
842                         *out++ = '\0';
843                         i++;
844                 }
845                 else if (in[i] == '\\' && in[i+1] == 't')
846                 {
847                         *out++ = '\t';
848                         i++;
849                 }
850                 else if (in[i] == '\\' && in[i+1] == 'r')
851                 {
852                         *out++ = '\r';
853                         i++;
854                 }
855                 else if (in[i] == '\\' && in[i+1] == '"')
856                 {
857                         *out++ = '\"';
858                         i++;
859                 }
860                 else
861                         *out++ = in[i];
862         }
863
864         mysocket = NetConn_ChooseClientSocketForAddress(&address);
865         if (!mysocket)
866                 mysocket = NetConn_ChooseServerSocketForAddress(&address);
867         if (mysocket)
868                 NetConn_Write(mysocket, send, out - send, &address);
869 }
870
871 static void CL_PingPLReport_f(cmd_state_t *cmd)
872 {
873         char *errbyte;
874         int i;
875         int l = Cmd_Argc(cmd);
876         if (l > cl.maxclients)
877                 l = cl.maxclients;
878         for (i = 0;i < l;i++)
879         {
880                 cl.scores[i].qw_ping = atoi(Cmd_Argv(cmd, 1+i*2));
881                 cl.scores[i].qw_packetloss = strtol(Cmd_Argv(cmd, 1+i*2+1), &errbyte, 0);
882                 if(errbyte && *errbyte == ',')
883                         cl.scores[i].qw_movementloss = atoi(errbyte + 1);
884                 else
885                         cl.scores[i].qw_movementloss = 0;
886         }
887 }
888
889 //=============================================================================
890
891 /*
892 ==================
893 Host_InitCommands
894 ==================
895 */
896 void Host_InitCommands (void)
897 {
898         dpsnprintf(cls.userinfo, sizeof(cls.userinfo), "\\name\\player\\team\\none\\topcolor\\0\\bottomcolor\\0\\rate\\10000\\msg\\1\\noaim\\1\\*ver\\dp");
899
900         Cvar_RegisterVariable(&cl_name);
901         Cvar_RegisterVariable(&cl_color);
902         Cvar_RegisterVariable(&cl_rate);
903         Cvar_RegisterVariable(&cl_rate_burstsize);
904         Cvar_RegisterVariable(&cl_pmodel);
905         Cvar_RegisterVariable(&cl_playermodel);
906         Cvar_RegisterVariable(&cl_playerskin);
907         Cvar_RegisterVariable(&rcon_password);
908         Cvar_RegisterVariable(&rcon_address);
909         Cvar_RegisterVariable(&rcon_secure);
910         Cvar_RegisterVariable(&rcon_secure_challengetimeout);
911         Cvar_RegisterVariable(&r_fixtrans_auto);
912         Cvar_RegisterVariable(&team);
913         Cvar_RegisterVariable(&skin);
914         Cvar_RegisterVariable(&noaim);
915
916         Cmd_AddCommand(CMD_CLIENT | CMD_SERVER_FROM_CLIENT, "name", CL_Name_f, "change your player name");
917         Cmd_AddCommand(CMD_CLIENT | CMD_SERVER_FROM_CLIENT, "color", CL_Color_f, "change your player shirt and pants colors");
918         Cmd_AddCommand(CMD_CLIENT | CMD_SERVER_FROM_CLIENT, "rate", CL_Rate_f, "change your network connection speed");
919         Cmd_AddCommand(CMD_CLIENT | CMD_SERVER_FROM_CLIENT, "rate_burstsize", CL_Rate_BurstSize_f, "change your network connection speed");
920         Cmd_AddCommand(CMD_CLIENT | CMD_SERVER_FROM_CLIENT, "pmodel", CL_PModel_f, "(Nehahra-only) change your player model choice");
921         Cmd_AddCommand(CMD_CLIENT | CMD_SERVER_FROM_CLIENT, "playermodel", CL_Playermodel_f, "change your player model");
922         Cmd_AddCommand(CMD_CLIENT | CMD_SERVER_FROM_CLIENT, "playerskin", CL_Playerskin_f, "change your player skin number");
923
924         Cmd_AddCommand(CMD_CLIENT, "sendcvar", CL_SendCvar_f, "sends the value of a cvar to the server as a sentcvar command, for use by QuakeC");
925         Cmd_AddCommand(CMD_CLIENT, "rcon", CL_Rcon_f, "sends a command to the server console (if your rcon_password matches the server's rcon_password), or to the address specified by rcon_address when not connected (again rcon_password must match the server's); note: if rcon_secure is set, client and server clocks must be synced e.g. via NTP");
926         Cmd_AddCommand(CMD_CLIENT, "srcon", CL_Rcon_f, "sends a command to the server console (if your rcon_password matches the server's rcon_password), or to the address specified by rcon_address when not connected (again rcon_password must match the server's); this always works as if rcon_secure is set; note: client and server clocks must be synced e.g. via NTP");
927         Cmd_AddCommand(CMD_CLIENT, "pqrcon", CL_PQRcon_f, "sends a command to a proquake server console (if your rcon_password matches the server's rcon_password), or to the address specified by rcon_address when not connected (again rcon_password must match the server's)");
928         Cmd_AddCommand(CMD_CLIENT, "fullinfo", CL_FullInfo_f, "allows client to modify their userinfo");
929         Cmd_AddCommand(CMD_CLIENT, "setinfo", CL_SetInfo_f, "modifies your userinfo");
930         Cmd_AddCommand(CMD_CLIENT | CMD_CLIENT_FROM_SERVER, "packet", CL_Packet_f, "send a packet to the specified address:port containing a text string");
931         Cmd_AddCommand(CMD_CLIENT | CMD_CLIENT_FROM_SERVER, "topcolor", CL_TopColor_f, "QW command to set top color without changing bottom color");
932         Cmd_AddCommand(CMD_CLIENT, "bottomcolor", CL_BottomColor_f, "QW command to set bottom color without changing top color");
933         Cmd_AddCommand(CMD_CLIENT, "fixtrans", Image_FixTransparentPixels_f, "change alpha-zero pixels in an image file to sensible values, and write out a new TGA (warning: SLOW)");
934
935         // commands that are only sent by server to client for execution
936         Cmd_AddCommand(CMD_CLIENT_FROM_SERVER, "pingplreport", CL_PingPLReport_f, "command sent by server containing client ping and packet loss values for scoreboard, triggered by pings command from client (not used by QW servers)");
937         Cmd_AddCommand(CMD_CLIENT_FROM_SERVER, "fullserverinfo", CL_FullServerinfo_f, "internal use only, sent by server to client to update client's local copy of serverinfo string");
938 }
939
940 void Host_NoOperation_f(cmd_state_t *cmd)
941 {
942 }