1 /*
2 sv_gib.c
3
4 GIB interface to qw-server
5
6 Copyright (C) 2003 Brian Koropoff
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
17 See the GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to:
21
22 Free Software Foundation, Inc.
23 59 Temple Place - Suite 330
24 Boston, MA 02111-1307, USA
25
26 */
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_STRING_H
32 # include <string.h>
33 #endif
34 #ifdef HAVE_STRINGS_H
35 # include <strings.h>
36 #endif
37
38 #include "QF/dstring.h"
39 #include "QF/info.h"
40 #include "QF/gib.h"
41
42 #include "server.h"
43 #include "client.h"
44
45 gib_event_t *sv_chat_e;
46 gib_event_t *sv_client_connect_e;
47 gib_event_t *sv_client_disconnect_e;
48 gib_event_t *sv_client_spawn_e;
49 gib_event_t *sv_map_e;
50 gib_event_t *sv_frag_e;
51 gib_event_t *sv_setinfo_e;
52
53 static client_t *
SV_GIB_GetClient(int uid)54 SV_GIB_GetClient (int uid)
55 {
56 client_t *cl;
57 int i;
58
59 for (i = 0, cl = svs.clients; i < MAX_CLIENTS; i++, cl++)
60 if (cl->state >= cs_connected && cl->userid == uid)
61 return cl;
62 return 0;
63 }
64
65 static void
SV_GIB_Client_GetList_f(void)66 SV_GIB_Client_GetList_f (void)
67 {
68 client_t *cl;
69 int i;
70
71 if (GIB_Argc () != 1)
72 GIB_USAGE ("");
73 else if (GIB_CanReturn ())
74 for (i = 0, cl = svs.clients; i < MAX_CLIENTS; i++, cl++)
75 if (cl->state >= cs_connected)
76 dsprintf (GIB_Return (0), "%i", cl->userid);
77 }
78
79 static void
SV_GIB_Client_GetKeys_f(void)80 SV_GIB_Client_GetKeys_f (void)
81 {
82 client_t *cl;
83
84 if (GIB_Argc () != 2)
85 GIB_USAGE ("");
86 else if (!(cl = SV_GIB_GetClient (atoi (GIB_Argv (1)))))
87 GIB_Error ("uid", "No user with id '%s' was found on the server.",
88 GIB_Argv (1));
89 else if (GIB_CanReturn ()) {
90 info_key_t **key, **keys = Info_KeyList (cl->userinfo);
91 for (key = keys; *key; key++)
92 dstring_appendstr (GIB_Return (0), (*key)->key);
93 free (keys);
94 }
95 }
96
97 static void
SV_GIB_Client_GetInfo_f(void)98 SV_GIB_Client_GetInfo_f (void)
99 {
100 client_t *cl;
101 const char *str;
102
103 if (GIB_Argc () != 3)
104 GIB_USAGE ("");
105 else if (!(cl = SV_GIB_GetClient (atoi (GIB_Argv (1)))))
106 GIB_Error ("uid", "No user with id '%s' was found on the server.",
107 GIB_Argv (1));
108 else if ((str = Info_ValueForKey (cl->userinfo, GIB_Argv (2))))
109 GIB_Return (str);
110 }
111
112 static void
SV_GIB_Client_Print_f(void)113 SV_GIB_Client_Print_f (void)
114 {
115 client_t *cl;
116
117 if (GIB_Argc () != 3)
118 GIB_USAGE ("uid message");
119 else if (!(cl = SV_GIB_GetClient (atoi (GIB_Argv (1)))))
120 GIB_Error ("uid", "No user with id '%s' was found on the server.",
121 GIB_Argv (1));
122 else
123 SV_ClientPrintf (0, cl, GIB_Argv (0)[13] ? PRINT_CHAT : PRINT_HIGH,
124 "%s", GIB_Argv (2));
125 }
126
127 static void
SV_GIB_Client_Print_All_f(void)128 SV_GIB_Client_Print_All_f (void)
129 {
130 client_t *cl;
131 int i, level = GIB_Argv (0)[16] ? PRINT_CHAT : PRINT_HIGH;
132
133 if (GIB_Argc () != 2)
134 GIB_USAGE ("message");
135 else for (i = 0, cl = svs.clients; i < MAX_CLIENTS; i++, cl++)
136 if (cl->state >= cs_connected)
137 SV_ClientPrintf (0, cl, level, "%s", GIB_Argv (1));
138 }
139
140 static void
SV_GIB_Map_Get_Current_f(void)141 SV_GIB_Map_Get_Current_f (void)
142 {
143 GIB_Return (SV_Current_Map());
144 }
145
146 static void
SV_GIB_Map_Time_Elapsed_f(void)147 SV_GIB_Map_Time_Elapsed_f (void)
148 {
149 if (GIB_CanReturn ())
150 dsprintf (GIB_Return(0), "%f", sv.time);
151 }
152
153 void
SV_GIB_Init(void)154 SV_GIB_Init (void)
155 {
156 // Builtins
157 GIB_Builtin_Add ("client::getList", SV_GIB_Client_GetList_f);
158 GIB_Builtin_Add ("client::getKeys", SV_GIB_Client_GetKeys_f);
159 GIB_Builtin_Add ("client::getInfo", SV_GIB_Client_GetInfo_f);
160 GIB_Builtin_Add ("client::print", SV_GIB_Client_Print_f);
161 GIB_Builtin_Add ("client::printChat", SV_GIB_Client_Print_f);
162 GIB_Builtin_Add ("client::printAll", SV_GIB_Client_Print_All_f);
163 GIB_Builtin_Add ("client::printAllChat", SV_GIB_Client_Print_All_f);
164
165 GIB_Builtin_Add ("map::getCurrent", SV_GIB_Map_Get_Current_f);
166 GIB_Builtin_Add ("map::timeElapsed", SV_GIB_Map_Time_Elapsed_f);
167
168 // Events
169 sv_chat_e = GIB_Event_New ("chat");
170 sv_client_connect_e = GIB_Event_New ("client.connect");
171 sv_client_disconnect_e = GIB_Event_New ("client.disconnect");
172 sv_client_spawn_e = GIB_Event_New ("client.spawn");
173 sv_map_e = GIB_Event_New ("map");
174 sv_frag_e = GIB_Event_New ("frag");
175 sv_setinfo_e = GIB_Event_New ("setinfo");
176 }
177