Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
BitchX
GitHub Repository: BitchX/BitchX1.3
Path: blob/master/dll/qbx/qbx.c
1072 views
1
/*
2
* Qbx by pod ([email protected])
3
* Some of this code is from Qir by Ben Turner ([email protected])
4
*/
5
6
#include "irc.h"
7
#include "struct.h"
8
#include "hook.h"
9
#include "module.h"
10
#define INIT_MODULE
11
#include "modval.h"
12
13
#include "qbx.h"
14
15
#define cparse convert_output_format
16
17
int qfd, querying = 0, qbx_on = 1, q_type = 0;
18
char q_server[256], q_chan[256];
19
struct timeval q_tv;
20
21
void privmsg(char *to, const char *format, ...) {
22
va_list ap;
23
char buffer[1024];
24
va_start(ap, format);
25
vsnprintf(buffer, sizeof(buffer), format, ap);
26
va_end(ap);
27
queue_send_to_server(from_server, "PRIVMSG %s :%s", to, buffer);
28
}
29
30
int time_delta(struct timeval *later, struct timeval *past) {
31
if(later->tv_usec < past->tv_usec) {
32
later->tv_sec--;
33
later->tv_usec+= 1000000;
34
}
35
return (later->tv_sec - past->tv_sec) * 1000 +
36
(later->tv_usec - past->tv_usec) / 1000;
37
}
38
39
40
void q_timeout(int fd) {
41
put_it("No response from %s", q_server);
42
privmsg(q_chan, "No response from %s", q_server);
43
close_socketread(qfd);
44
querying = 0;
45
}
46
47
void q_timer(int fd) {
48
int i = 0, j = 0, k = 0, type = q_type;
49
int nextpart = 0, num_players = 0, cheats = 0;
50
struct quake_variable variable[50];
51
struct timeval tv;
52
char status[65507], temp[1024];
53
char server_hostname[1024], server_maxclients[1024], server_mapname[1024];
54
char server_fraglimit[1024], server_timelimit[1024], server_game[1024];
55
56
/* ick. */
57
memset(&temp, 0, sizeof(temp));
58
memset(&server_hostname, 0, sizeof(server_hostname));
59
memset(&server_maxclients, 0, sizeof(server_maxclients));
60
memset(&server_mapname, 0, sizeof(server_mapname));
61
memset(&server_fraglimit, 0, sizeof(server_fraglimit));
62
memset(&server_timelimit, 0, sizeof(server_timelimit));
63
memset(&server_game, 0, sizeof(server_game));
64
65
/* now we should be expecting something back */
66
memset(&status, 0, sizeof(status));
67
if(recv(fd, status, 65507, 0) < 0) {
68
put_it("Error receiving from %s: %s", q_server, strerror(errno));
69
privmsg(q_chan, "Error receiving from %s: %s", q_server, strerror(errno));
70
close_socketread(fd);
71
querying = 0;
72
return;
73
}
74
gettimeofday(&tv, NULL);
75
close_socketread(fd);
76
77
memset(&variable, 0, sizeof(variable));
78
79
/* now we have our info, but we still need to decode it! */
80
if(type == 1)
81
i = 7;
82
else if(type == 2)
83
i = 11;
84
else if(type == 3)
85
i = 20;
86
87
/* the following while loop copies all the variables from the status message
88
into an array together with their value */
89
while (status[i] != '\n') {
90
/* check whether it's time for a new variable */
91
if (status[i] == '\\') {
92
if (nextpart) {
93
nextpart = 0;
94
variable[j].value[k] = '\0';
95
j++;
96
} else {
97
nextpart = 1;
98
variable[j].name[k] = '\0';
99
}
100
k = 0;
101
} else {
102
/* put our character in the correct position */
103
if (nextpart)
104
variable[j].value[k] = status[i];
105
else
106
variable[j].name[k] = status[i];
107
k++;
108
}
109
i++;
110
}
111
variable[j].value[k] = '\0';
112
113
/* now we can count how many players there are on the server */
114
i++;
115
put_it(&status[i]);
116
while (i < strlen(status)) {
117
if (status[i] == '\n') {
118
num_players++;
119
}
120
i++;
121
}
122
123
for (i = 0; i < 50; i++) {
124
if(type != 3) {
125
if(!strcmp("hostname", variable[i].name))
126
strcpy(server_hostname, variable[i].value);
127
if(!strcmp("maxclients", variable[i].name))
128
strcpy(server_maxclients, variable[i].value);
129
} else {
130
if(!strcmp("sv_hostname", variable[i].name))
131
strcpy(server_hostname, variable[i].value);
132
if(!strcmp("sv_maxclients", variable[i].name))
133
strcpy(server_maxclients, variable[i].value);
134
if(!strcmp("g_gametype", variable[i].name)) {
135
switch(atoi(variable[i].name)) {
136
case 0: strcpy(server_game, "FFA"); break;
137
case 1: strcpy(server_game, "DUEL"); break;
138
case 3: strcpy(server_game, "TEAM DM"); break;
139
case 4: strcpy(server_game, "CTF"); break;
140
default: strcpy(server_game, "UNKNOWN"); break;
141
}
142
}
143
}
144
if(type == 1) {
145
if(!strcmp("map", variable[i].name))
146
strcpy(server_mapname, variable[i].value);
147
if(!strcmp("*gamedir", variable[i].name))
148
strcpy(server_game, variable[i].value);
149
if(!strcmp("cheats", variable[i].name))
150
cheats = 1;
151
}
152
else {
153
if(!strcmp("mapname", variable[i].name))
154
strcpy(server_mapname, variable[i].value);
155
}
156
if(type == 2) {
157
if(!strcmp("gamename", variable[i].name))
158
strcpy(server_game, variable[i].value);
159
}
160
if(!strcmp("timelimit", variable[i].name))
161
strcpy(server_timelimit, variable[i].value);
162
if(!strcmp("fraglimit", variable[i].name))
163
strcpy(server_fraglimit, variable[i].value);
164
}
165
if(type == 1) {
166
snprintf(temp, 1024, "%s : players: %d/%s, ping: %d, map: %s, timelimit: %s, fraglimit: %s", server_hostname, num_players, server_maxclients, time_delta(&tv, &q_tv), server_mapname, server_timelimit, server_fraglimit);
167
if(server_game[0] != '\0') {
168
char temp2[1024];
169
snprintf(temp2, 1024, ", game: %s", server_game);
170
strcat(temp, temp2);
171
}
172
if(cheats)
173
strcat(temp, ", cheats enabled");
174
}
175
if(type == 2)
176
snprintf(temp, 1024, "%s : players: %d/%s, ping: %d, map: %s, timelimit: %s, fraglimit: %s, game: %s", server_hostname, num_players, server_maxclients, time_delta(&tv, &q_tv), server_mapname, server_timelimit, server_fraglimit, server_game);
177
if(type == 3)
178
snprintf(temp, 1024, "%s : players: %d/%s, ping: %d, map: %s, gametype: %s, timelimit: %s, fraglimit: %s", server_hostname, num_players, server_maxclients, time_delta(&tv, &q_tv), server_mapname, server_game, server_timelimit, server_fraglimit);
179
put_it(temp);
180
privmsg(q_chan, temp);
181
querying = 0;
182
}
183
184
void query_q_server(char *server, int port, int game) {
185
char buffer[16];
186
187
struct sockaddr_in qsock;
188
struct hostent *he;
189
190
querying = 1;
191
192
/* look up our quakeserver address */
193
if ((he = gethostbyname(server)) == NULL) {
194
put_it("unknown host: %s", server);
195
close(qfd);
196
querying = 0;
197
return;
198
}
199
200
qfd = connect_by_number(server, &port, SERVICE_CLIENT, PROTOCOL_UDP, 1);
201
memset(buffer, 0, sizeof(buffer));
202
memset(&qsock, 0, sizeof(struct sockaddr_in));
203
if(game == 3)
204
strcpy(buffer, "\xFF\xFF\xFF\xFFgetstatus");
205
else
206
strcpy(buffer, "\xFF\xFF\xFF\xFFstatus");
207
208
qsock.sin_family = AF_INET;
209
qsock.sin_port = htons(port);
210
qsock.sin_addr = *((struct in_addr *)he->h_addr);
211
212
put_it("Sending status request to %d.%d.%d.%d...",
213
(unsigned char) he->h_addr_list[0][0],
214
(unsigned char) he->h_addr_list[0][1],
215
(unsigned char) he->h_addr_list[0][2],
216
(unsigned char) he->h_addr_list[0][3]);
217
218
/* send our information to the quake server */
219
sendto(qfd, buffer, strlen(buffer), 0, (struct sockaddr *)&qsock, sizeof(struct sockaddr));
220
gettimeofday(&q_tv, NULL);
221
222
strncpy(q_server, server, 256);
223
q_type = game;
224
add_socketread(qfd, port, 0, server, q_timer, NULL);
225
add_sockettimeout(qfd, 5, q_timeout);
226
}
227
228
int pub_proc(char *which, char *str, char **unused) {
229
char *loc, *nick, *chan, *cmd, *serv;
230
int port = 0;
231
232
if(qbx_on == 0) return 1;
233
loc = LOCAL_COPY(str);
234
nick = next_arg(loc, &loc);
235
chan = next_arg(loc, &loc);
236
cmd = next_arg(loc, &loc);
237
if(cmd && *cmd != '!') return 1;
238
if(my_stricmp(cmd, Q3A_COMMAND) && my_stricmp(cmd, Q2_COMMAND) && my_stricmp(cmd, QW_COMMAND))
239
return 1;
240
serv = next_arg(loc, &loc);
241
if(serv == NULL) {
242
privmsg(chan, "%s: Give me a server to query", nick);
243
return 1;
244
}
245
if(querying == 1) {
246
privmsg(chan, "%s: A query is already in progress", nick);
247
return 1;
248
}
249
if(strchr(serv, ':')) {
250
serv = strtok(serv, ":");
251
port = atoi(strtok(NULL, ""));
252
}
253
else
254
port = 0;
255
strncpy(q_chan, chan, 256);
256
if(!my_stricmp(cmd, Q3A_COMMAND)) { /* quake3 server (my fav =) */
257
if(port == 0) port = Q3A_GAME_PORT;
258
query_q_server(serv, port, 3);
259
return 1;
260
}
261
else if(!my_stricmp(cmd, Q2_COMMAND)) { /* quake2 server */
262
if(port == 0) port = Q2_GAME_PORT;
263
query_q_server(serv, port, 2);
264
return 1;
265
}
266
else if(!my_stricmp(cmd, QW_COMMAND)) { /* quakeworld server */
267
if(port == 0) port = QW_GAME_PORT;
268
query_q_server(serv, port, 1);
269
return 1;
270
}
271
return 1;
272
}
273
274
BUILT_IN_DLL(qbx_cmd)
275
{
276
if(!my_stricmp(args, "on")) {
277
qbx_on = 1;
278
put_it("Qbx turned on");
279
return;
280
}
281
if(!my_stricmp(args, "off")) {
282
qbx_on = 0;
283
put_it("Qbx turned off");
284
return;
285
}
286
userage("QBX", helparg);
287
}
288
289
int Qbx_Init(IrcCommandDll **intp, Function_ptr *global_table) {
290
initialize_module("qbx");
291
add_module_proc(HOOK_PROC, "qbx", NULL, "* % !q*", PUBLIC_LIST, 1, NULL, pub_proc);
292
add_module_proc(HOOK_PROC, "qbx", NULL, "* % !q*", PUBLIC_OTHER_LIST, 1, NULL, pub_proc);
293
add_module_proc(COMMAND_PROC, "qbx", "qbx", NULL, 0, 0, qbx_cmd, "<on|off>\n- Turns Qbx on or off");
294
put_it("Qbx %s loaded", QBX_VERSION);
295
return 0;
296
}
297
298
char *Qbx_Version(IrcCommandDll **intp) {
299
return QBX_VERSION;
300
}
301
302