]>
Commit | Line | Data |
---|---|---|
85ce9d3e | 1 | /* |
2 | * This file is provided for use with the unix-socket-faq. It is public | |
3 | * domain, and may be copied freely. There is no copyright on it. The | |
4 | * original work was by Vic Metcalfe (vic@brutus.tlug.org), and any | |
5 | * modifications made to that work were made with the understanding that | |
6 | * the finished work would be in the public domain. | |
7 | * | |
8 | * If you have found a bug, please pass it on to me at the above address | |
9 | * acknowledging that there will be no copyright on your work. | |
10 | * | |
11 | * The most recent version of this file, and the unix-socket-faq can be | |
12 | * found at http://www.interlog.com/~vic/sock-faq/. | |
13 | */ | |
14 | ||
15 | #include "sockhelp.h" | |
c7340cbd | 16 | #include "options.h" |
85ce9d3e | 17 | #include "aClient.h" |
18 | #include "extern.h" | |
448a1531 | 19 | #include "flags.h" |
85ce9d3e | 20 | #include <stdio.h> |
21 | #include <unistd.h> | |
22 | #include <string.h> | |
fb37ecc7 | 23 | #include <fstream> |
85ce9d3e | 24 | #include <stdlib.h> |
ce61cdfa | 25 | #include <fcntl.h> |
26 | #include <signal.h> | |
c10b78ac | 27 | #include <algorithm> |
a46254e3 | 28 | |
ce61cdfa | 29 | //#include <sys/types.h> |
30 | //#include <sys/wait.h> | |
31 | //#include <errno.h> | |
32 | ||
a46254e3 | 33 | using namespace std; |
fb37ecc7 | 34 | |
91c0b563 | 35 | char *PACKAGE = "GameServ"; |
8fb0b83e | 36 | char *VERSION = "1.3.4 +devel"; |
173302fe | 37 | |
85ce9d3e | 38 | int sock; |
2edcd222 | 39 | long lastrefresh; |
1fe6fccd | 40 | long lastrollover; |
44ea29f7 | 41 | |
c10b78ac | 42 | list<aClient*> clients[U_TABLE_SIZE]; |
85ce9d3e | 43 | |
2edcd222 | 44 | void save_lastrefresh(); |
45 | void load_lastrefresh(); | |
1fe6fccd | 46 | void load_lastrollover(); |
47 | void save_lastrollover(); | |
15838737 | 48 | void prettyIntro(); |
40251952 | 49 | void check_idles(); |
f9db99e0 | 50 | void clearClients(); |
51 | void clearPlayers(); | |
5de7a0b0 | 52 | void clearItems(); |
44ea29f7 | 53 | |
ce61cdfa | 54 | // Make this a daemon |
55 | int daemon(int nochdir, int noclose); | |
56 | ||
57 | // Close all file descriptors from >= fd | |
58 | void closeall(int fd); | |
59 | ||
624c0352 | 60 | int main(int argc, char *argv[]) |
85ce9d3e | 61 | { |
0faabda5 | 62 | char buffer[1024], buf[1024]; |
63 | memset(buffer, 0, 1024); | |
64 | memset(buf, 0, 1024); | |
65 | int connected; | |
66 | long lastidlecheck; | |
67 | char *cmd, *source = NULL, *conf; | |
68 | srand(time(NULL)); | |
69 | conf = new char[16]; | |
70 | strcpy(conf, "gameserv.conf"); | |
71 | ||
1579dfa2 | 72 | /* |
73 | * This needs to be fixed to work for any number of arguments in any | |
74 | * order | |
75 | * | |
76 | */ | |
624c0352 | 77 | if (argc > 1) |
624c0352 | 78 | { |
0faabda5 | 79 | if ( argc > 2 || stricmp(argv[1], "--help") == 0) |
80 | { | |
81 | cout << "Usage: gameserv [options] [configfile]" << endl; | |
82 | cout << "Options:" << endl; | |
83 | cout << "--help Displays this help dialogue" << endl; | |
5de7a0b0 | 84 | delete []conf; |
0faabda5 | 85 | return 1; |
86 | } | |
87 | delete []conf; | |
88 | conf = argv[1]; | |
624c0352 | 89 | } |
0faabda5 | 90 | |
15838737 | 91 | prettyIntro(); |
0faabda5 | 92 | |
624c0352 | 93 | if (load_config_file(conf)) |
0faabda5 | 94 | { |
95 | cout << "Config file loaded ok...\n" | |
96 | << "Turning into a daemon" << endl; | |
97 | } | |
624c0352 | 98 | else |
99 | exit(2); | |
0faabda5 | 100 | |
101 | if (argc <= 1) | |
e3ede4a7 | 102 | delete []conf; |
0faabda5 | 103 | |
104 | // Turn into a daemon | |
105 | if (daemon(1,0) < 0) | |
ce61cdfa | 106 | { |
0faabda5 | 107 | perror("Could not turn into a daemon"); |
108 | exit(3); | |
ce61cdfa | 109 | } |
e0056fa6 | 110 | if (load_items() == 0) |
cd973e97 | 111 | { |
112 | log("Error loading items"); | |
113 | goto end; | |
114 | } | |
0faabda5 | 115 | |
3f107f27 | 116 | if (load_store() == 0) |
117 | { | |
118 | log("Error loading store"); | |
119 | goto end; | |
120 | } | |
d1927afc | 121 | if (load_tavern() == 0) |
122 | { | |
123 | log("Error loading tavern"); | |
124 | goto end; | |
125 | } | |
0faabda5 | 126 | |
e0056fa6 | 127 | load_gs_dbase(); |
128 | loadNews(newsdata, todaysnews); | |
0faabda5 | 129 | |
130 | ||
cd973e97 | 131 | if (load_masters() == false) |
132 | { | |
133 | log("Error loading masters"); | |
134 | goto end; | |
135 | } | |
136 | ||
05c527e6 | 137 | if (load_monsters() == false) |
cd973e97 | 138 | { |
139 | log("Error loading monsters"); | |
140 | goto end; | |
141 | } | |
142 | ||
8e800549 | 143 | if (!load_dragon()) |
cd973e97 | 144 | { |
145 | log("Error loading dragon"); | |
146 | goto end; | |
147 | } | |
148 | ||
ea93c39a | 149 | if (load_levels() == false) |
cd973e97 | 150 | { |
151 | log("Error loading levels"); | |
152 | goto end; | |
153 | } | |
0faabda5 | 154 | |
155 | shuttingdown = false; | |
156 | ||
157 | char ignoreservers[32][256]; | |
158 | char *currentserver; | |
159 | currentserver = strtok(ignoreserverslist, " "); | |
160 | for (int server = 0; server < 32 && currentserver != NULL; server++) | |
448a1531 | 161 | { |
0faabda5 | 162 | strncpy(ignoreservers[server], currentserver, 255); |
163 | log("Placing %s on the server ignore list", currentserver); | |
164 | currentserver = strtok(NULL, " "); | |
448a1531 | 165 | } |
0faabda5 | 166 | |
167 | // This loop will retry the connection 3 times | |
168 | for (int retry = 0; retry < 3 && !shuttingdown; retry++) | |
169 | { | |
170 | connected = 1; | |
171 | load_lastrefresh(); | |
172 | load_lastrollover(); | |
173 | ||
174 | long int loadtime = time(NULL); | |
175 | long int currentTime; | |
176 | long int oldTime = loadtime; | |
177 | ||
178 | lastidlecheck = loadtime; | |
179 | ||
180 | #ifdef DEBUGMODE | |
181 | log("Setting primary Idle Check timestamp: %ld", lastidlecheck); | |
182 | #endif | |
183 | bool loaded = false; | |
184 | ||
185 | ignore_pipe(); | |
186 | sock = conn(remoteserver, remoteport, localhost, 0); | |
187 | // sock = make_connection(remoteport, SOCK_STREAM, remoteserver); | |
000f8409 | 188 | if (sock == -1) |
189 | { | |
190 | fprintf(stderr,"make_connection failed.\n"); | |
191 | unload_config_file(); | |
192 | return -1; | |
193 | } | |
0faabda5 | 194 | log("%S socket connected."); |
195 | ||
c7340cbd | 196 | #ifdef UNREAL |
0faabda5 | 197 | raw("PASS :%s", remotepass); |
198 | raw("SERVER %s 1 :%s", servername, servername); | |
199 | raw("NICK %S 1 %d %s %s %s %d :%s v%s", time(NULL), gsident, gshost, | |
200 | servername, time(NULL), PACKAGE, VERSION); | |
201 | raw(":%S JOIN %s", c_Forest); | |
202 | raw(":%S MODE %s +tn", c_Forest); | |
c7340cbd | 203 | #elif defined(BAHAMUT) |
0faabda5 | 204 | raw("PASS %s :TS", remotepass); |
205 | raw("SERVER %s 1 :%s", servername, servername); | |
206 | raw("NICK %S 1 %d +w%s %s %s %s 0 :GameServ", time(NULL), (isBOper() ? "o" : ""), | |
207 | gsident, gshost, servername); | |
208 | raw(":%s SJOIN %d %d %s +nt :@%S", servername, time(NULL), time(NULL), c_Forest); | |
581ec09e | 209 | #elif defined(HYBRID) |
0faabda5 | 210 | raw("PASS %s :TS", remotepass); |
211 | raw("SERVER %s 1 :%s", servername, servername); | |
212 | raw("NICK %S 1 %d +w%s %s %s %s :GameServ", time(NULL), (isBOper() ? "o" : ""), | |
213 | gsident, gshost, servername); | |
214 | raw(":%s SJOIN %ld %s +nt :@%S", servername, time(NULL), c_Forest); | |
7cc338f6 | 215 | #elif defined(ULTIMATE2) |
0faabda5 | 216 | raw("PASS %s :TS", remotepass); |
217 | raw("SERVER %s 1 :%s", servername, servername); | |
218 | raw("NICK %S 1 %d %s %s %s 0 :GameServ", | |
219 | time(NULL), gsident, gshost, servername); | |
220 | if (isBOper()) | |
7cc338f6 | 221 | raw(":%S mode %S +o"); |
0faabda5 | 222 | raw(":%S JOIN %s", c_Forest); |
629d0d86 | 223 | #elif defined(PTLINK) |
224 | raw("PASS %s :TS", remotepass); | |
225 | raw("SERVER %s 1 :%s", servername, servername); | |
226 | raw("NICK %S 1 %d %s%s %s %s %s :GameServ", time(NULL), (isBOper() ? "+iow " : ""), gsident, gshost, gshost, servername); | |
227 | raw(":%s SJOIN %d %s +nt :@%S", servername, time(NULL), c_Forest); | |
65326d0c | 228 | #elif defined(VLIFE) |
229 | raw("PASS %s :TS", remotepass); | |
230 | raw("SERVER %s 1 %d :%s", servername, time(NULL), servername); | |
65326d0c | 231 | raw("NNICK %S 1 %d +w%s %s %s %s %s :GameServ", time(NULL),(isBOper() ? "o" : ""), gsident, gshost, gshost, servername); |
232 | raw(":%s SJOIN %d %s +nt :@%S", servername, time(NULL), c_Forest); | |
d007a271 | 233 | #elif defined(BAHAMUT8) |
234 | raw("PASS %s :TS", remotepass); | |
235 | raw("SERVER %s %d :%s", servername, time(NULL), servername); | |
236 | raw("NICK %S 1 %d +w%s %s %s %s 0 %d :GameServ", time(NULL), (isBOper() ? "o" : ""), gsident, gshost, servername, time(NULL)); | |
237 | raw(":%s SJOIN %d %d %s +nt :@%S", servername, time(NULL), time(NULL), c_Forest); | |
e1c41a84 | 238 | #elif defined(P10) |
0faabda5 | 239 | // Server numeric is: [] <-- must be unique |
240 | raw("PASS :%s", remotepass); | |
241 | raw("SERVER %s 1 %d %d P10 []AAF :%s", servername, time(NULL), time(NULL), servername); | |
242 | raw("[] N %S 1 %d %s %s %s DAqAoB %s :%S", time(NULL), gsident, gshost, | |
243 | (isBOper() ? "+o" : ""), gsnum); | |
244 | raw("[] B %s %d +tn %s:o", c_Forest, time(NULL) - 864000, gsnum); | |
c7340cbd | 245 | #endif |
0faabda5 | 246 | |
e1c41a84 | 247 | #if defined(P10) |
0faabda5 | 248 | raw("%s T %s :%s", gsnum, c_Forest, c_ForestTopic); |
249 | raw("[] EB"); // End burst | |
581ec09e | 250 | #else |
0faabda5 | 251 | #ifndef HYBRID |
252 | #if defined(ULTIMATE2) | |
253 | raw(":%s MODE %s +o %S %ld", servername, c_Forest, | |
254 | time(NULL)); | |
255 | #else | |
256 | raw(":%S MODE %s +o %S", c_Forest); | |
e1c41a84 | 257 | #endif |
0faabda5 | 258 | #endif |
259 | raw(":%S TOPIC %s :%s", c_Forest, c_ForestTopic); | |
260 | #endif | |
261 | ||
262 | #ifndef P10 | |
263 | if (isUseNickServ()) | |
264 | { | |
265 | raw(":%S PRIVMSG %s :IDENTIFY %s", nsname, nspass); | |
266 | } | |
267 | #endif | |
268 | sock_gets(sock,buffer,sizeof(buffer)-1); /* -1 added thanks to | |
269 | David Duchene <dave@ltd.com> for pointing out the possible | |
270 | buffer overflow resulting from the linefeed added below. */ | |
271 | ||
272 | ||
273 | #ifdef DEBUGMODE | |
9f8c2acc | 274 | log("Server: %s",buffer); |
0faabda5 | 275 | #endif |
276 | ||
277 | while (connected) | |
278 | { | |
279 | if (sock_gets(sock,buffer,sizeof(buffer)) == -1) | |
280 | { | |
281 | connected = 0; | |
282 | } | |
283 | strcpy(buf, buffer); | |
284 | ||
285 | #if !defined(P10) | |
286 | if (buffer[0] == ':') | |
287 | { | |
288 | source = strtok(buf, " "); | |
289 | cmd = strtok(NULL, " "); | |
290 | } | |
291 | else | |
000f8409 | 292 | { |
293 | cmd = strtok(buf, " "); | |
294 | } | |
0faabda5 | 295 | #else |
296 | source = strtok(buf, " "); | |
297 | cmd = strtok(NULL, " "); | |
298 | #endif | |
299 | ||
300 | #ifdef DEBUGMODE | |
301 | log("Server: %s", buffer); | |
302 | #endif | |
303 | ||
304 | // Wait N seconds then we're loaded. | |
305 | if (!loaded) | |
306 | { | |
307 | if (time(NULL) >= welcomedelay + loadtime) | |
308 | { | |
309 | loaded = true; | |
310 | retry = 0; // Start the reconnection cycle over | |
311 | } | |
312 | } | |
313 | else | |
314 | { | |
315 | long TIME = time(NULL); | |
316 | if (TIME - lastidlecheck >= idlecheckperiod) | |
317 | { | |
318 | check_idles(); | |
319 | lastidlecheck = TIME; | |
320 | } | |
321 | } | |
322 | ||
323 | // Refresh players and clear news if the time is up | |
324 | currentTime = time(NULL); | |
000f8409 | 325 | |
0faabda5 | 326 | if (isRolloverForestFights()) |
327 | { | |
328 | if (currentTime - lastrollover >= rolloverperiod) | |
329 | { | |
330 | rolloverall(); | |
331 | lastrollover = currentTime; | |
332 | save_lastrollover(); | |
333 | notice(s_GameServ, c_Forest, "Adding %d forest fights to all players!", numrolloverfights); | |
334 | } | |
335 | } | |
336 | ||
337 | if (currentTime - lastrefresh >= refreshperiod) | |
338 | { | |
339 | refreshall(); | |
340 | clearNews(todaysnews); | |
341 | saveNews(newsdata, todaysnews); | |
342 | lastrefresh = currentTime; | |
343 | save_lastrefresh(); | |
344 | notice(s_GameServ, c_Forest, "Refreshing all players "\ | |
345 | "and resetting news!"); | |
346 | } | |
347 | ||
348 | // Save the player data every updateperiod seconds | |
349 | if (currentTime - oldTime >= updateperiod) | |
350 | { | |
351 | oldTime = currentTime; | |
352 | log("Saving to %s", playerdata); | |
353 | ||
354 | save_gs_dbase(); | |
355 | saveNews(newsdata, todaysnews); | |
356 | if (isSavedNotice()) | |
357 | { | |
358 | // Send notice to the channel of the update | |
359 | notice(s_GameServ, c_Forest, "%S player data saved"); | |
360 | } | |
361 | } | |
362 | ||
363 | ||
364 | #if !defined(P10) | |
000f8409 | 365 | if (stricmp(cmd, "PING") == 0) |
629d0d86 | 366 | { |
000f8409 | 367 | char *timestamp; |
368 | timestamp = strtok(NULL, ""); | |
369 | raw("PONG %s", timestamp); | |
629d0d86 | 370 | } |
000f8409 | 371 | #else |
372 | // P10 Ping | |
373 | if (stricmp(cmd, "G") == 0) | |
374 | { | |
375 | char *timestamp; | |
376 | timestamp = strtok(NULL, " "); | |
377 | raw("[] Z [] %s 0 %s", timestamp + 1, timestamp); | |
378 | } | |
379 | #endif | |
380 | #ifdef P10 | |
381 | else if (stricmp(cmd, "EB") == 0) | |
382 | { | |
383 | raw("[] EA"); | |
384 | } | |
385 | #endif | |
386 | else if (stricmp(cmd, "VERSION") == 0) | |
387 | { | |
388 | char *server; | |
389 | server = strtok(NULL, " "); | |
390 | server++; | |
391 | raw(":%s 351 %s %s_%s. %s", servername, source+1, PACKAGE, VERSION, servername); | |
392 | } | |
393 | // Code indenting is clean up until here! | |
394 | #if !defined(P10) | |
65326d0c | 395 | else if (strncmp(cmd, "NICK", 4) == 0 |
396 | #ifdef VLIFE | |
397 | || strncmp(cmd, "NNICK", 5) == 0 | |
398 | #endif | |
399 | ) | |
000f8409 | 400 | { |
401 | if (buffer[0] == ':') | |
402 | { | |
403 | aClient *tempPtr; | |
404 | if ((tempPtr = find((source + 1)))) | |
405 | { | |
406 | char *nick; | |
407 | unsigned long oldhv, newhv; | |
408 | nick = strtok(NULL, " "); | |
409 | oldhv = iHASH((unsigned char *) tempPtr->getNick()); | |
410 | newhv = iHASH((unsigned char *) nick); | |
411 | tempPtr->setNick(nick); | |
412 | clients[oldhv].remove(tempPtr); | |
413 | clients[newhv].push_back(tempPtr); | |
414 | } | |
415 | } | |
416 | else | |
417 | { | |
418 | char *nick; | |
419 | #else | |
ba2a880f | 420 | } else if (stricmp(cmd, "N") == 0 && strlen(source) == 2) { |
421 | { | |
ce61cdfa | 422 | char *nick, *realnick; |
423 | realnick = strtok(NULL, " "); | |
ba2a880f | 424 | |
ce61cdfa | 425 | for (int x = 0; x < 5; x++) |
ba2a880f | 426 | nick = strtok(NULL, " "); |
fc9d2643 | 427 | |
ba2a880f | 428 | if (nick[0] == '+') |
fc9d2643 | 429 | { |
000f8409 | 430 | #ifdef DEBUGMODE |
fc9d2643 | 431 | log ("aClient has modes"); |
000f8409 | 432 | #endif |
fc9d2643 | 433 | // Searching for the +r mode (extra parameter) |
434 | for (unsigned int count = 1; count < strlen(nick); count++) | |
435 | { | |
436 | if (nick[count] == 'r') | |
437 | { | |
438 | nick = strtok(NULL, " "); | |
439 | break; | |
440 | } | |
441 | } | |
442 | nick = strtok(NULL, " "); | |
443 | } | |
000f8409 | 444 | #endif |
c10b78ac | 445 | aClient *newuser; |
ba2a880f | 446 | |
85ce9d3e | 447 | nick = strtok(NULL, " "); |
ba2a880f | 448 | |
000f8409 | 449 | #ifdef P10 |
ce61cdfa | 450 | newuser = new aClient(nick, realnick); |
000f8409 | 451 | #else |
ce61cdfa | 452 | newuser = new aClient(nick); |
000f8409 | 453 | #endif |
ce61cdfa | 454 | |
455 | ||
c10b78ac | 456 | if (loaded) |
457 | if (isWelcome()) | |
458 | { | |
459 | #ifdef P10 | |
460 | notice(s_GameServ, nick, welcomemsg, realnick); | |
461 | #else | |
462 | notice(s_GameServ, nick, welcomemsg, nick); | |
463 | #endif | |
464 | } | |
465 | #ifdef P10 | |
c8117c0f | 466 | unsigned long hv = sHASH((unsigned char *) nick); |
c10b78ac | 467 | #else |
448a1531 | 468 | unsigned long hv = iHASH((unsigned char *) nick); |
c10b78ac | 469 | #endif |
470 | ||
471 | #if defined(HYBRID) || defined(BAHAMUT) || defined(ULTIMATE2) || defined(PTLINK) | |
472 | char *nickserver; | |
473 | strtok(NULL, " "); | |
474 | strtok(NULL, " "); | |
475 | nickserver = strtok(NULL, " "); | |
476 | if (nickserver[0] == '+') | |
477 | strtok(NULL, " "); | |
478 | strtok(NULL, " "); | |
479 | nickserver = strtok(NULL, " "); | |
480 | for (int x = 0; x < 32; x++) | |
481 | { | |
903cd861 | 482 | if (stricmp(ignoreservers[x], nickserver) == 0) |
c10b78ac | 483 | { |
484 | setIgnore(newuser); | |
903cd861 | 485 | break; |
c10b78ac | 486 | } |
487 | } | |
488 | #elif defined(UNREAL) | |
489 | char *nickserver; | |
490 | strtok(NULL, " "); | |
491 | strtok(NULL, " "); | |
492 | strtok(NULL, " "); | |
493 | strtok(NULL, " "); | |
494 | nickserver = strtok(NULL, " "); | |
495 | for (int x = 0; x < 32; x++) | |
496 | { | |
448a1531 | 497 | if (stricmp(ignoreservers[x], nickserver) == 0) |
c10b78ac | 498 | { |
499 | setIgnore(newuser); | |
448a1531 | 500 | break; |
c10b78ac | 501 | } |
502 | } | |
503 | #endif | |
504 | clients[hv].push_back(newuser); | |
85ce9d3e | 505 | } |
000f8409 | 506 | #if defined(P10) |
ba2a880f | 507 | } else if (stricmp(cmd, "Q") == 0) { |
0b259dff | 508 | // unsigned long hv = sHASH((unsigned char *) source); |
000f8409 | 509 | #else |
629d0d86 | 510 | } else if (stricmp(cmd, "QUIT") == 0) |
511 | { | |
c10b78ac | 512 | |
629d0d86 | 513 | #endif |
514 | aClient *quitter; | |
515 | char z = source[0]; | |
fc9d2643 | 516 | |
517 | if (z == ':') | |
629d0d86 | 518 | source++; |
c10b78ac | 519 | |
520 | unsigned long hv = iHASH((unsigned char *) source); | |
3f243b0b | 521 | if (!(quitter = find(source))) |
629d0d86 | 522 | { |
523 | log("Fatal Error: could not find %s in the "\ | |
524 | "clients list", source); | |
525 | goto end; | |
526 | } | |
3f243b0b | 527 | |
c10b78ac | 528 | clients[hv].remove(quitter); |
aa80398e | 529 | logout(quitter); |
c10b78ac | 530 | |
aa80398e | 531 | delete quitter; |
3eba370f | 532 | |
3f243b0b | 533 | if (z == ':') |
629d0d86 | 534 | source--; |
3f243b0b | 535 | |
000f8409 | 536 | #if defined(P10) |
5de7a0b0 | 537 | } |
538 | else if (stricmp(cmd, "P") == 0) | |
539 | { | |
540 | char *rest, *dest; | |
541 | char *longname; | |
542 | longname = new char[strlen(s_GameServ) + strlen(servername) + 2]; | |
543 | ||
544 | sprintf(longname, "%S@%s", servername); | |
545 | ||
546 | dest = strtok(NULL, " "); | |
547 | rest = strtok(NULL, ""); | |
548 | if (stricmp(dest, gsnum) == 0 || stricmp(dest, longname) == 0) | |
549 | { | |
550 | delete [] longname; | |
551 | gameserv(source, rest); | |
552 | } | |
553 | else if (stricmp(dest, c_Forest) == 0 && isListenOnCF()) | |
554 | { | |
555 | delete [] longname; | |
556 | forest(source, rest); | |
557 | } | |
000f8409 | 558 | #else |
5de7a0b0 | 559 | } |
560 | else if (stricmp(cmd, "PRIVMSG") == 0) | |
561 | { | |
85ce9d3e | 562 | char *rest, *dest; |
563 | dest = strtok(NULL, " "); | |
564 | rest = strtok(NULL, ""); | |
ad7dfaa0 | 565 | if (strnicmp(dest, s_GameServ, strlen(s_GameServ)) == 0) |
5de7a0b0 | 566 | gameserv(source, rest); |
1546c3da | 567 | else if (stricmp(dest, c_Forest) == 0 && isListenOnCF()) |
5de7a0b0 | 568 | forest(source, rest); |
000f8409 | 569 | #endif |
570 | #if defined(P10) | |
f2072f1a | 571 | } else if (stricmp(cmd, "J") == 0) { |
000f8409 | 572 | #else |
85ce9d3e | 573 | } else if (stricmp(cmd, "JOIN") == 0) { |
000f8409 | 574 | #endif |
85ce9d3e | 575 | char *channel; |
f2072f1a | 576 | aClient *joiner; |
85ce9d3e | 577 | channel = strtok(NULL, " "); |
f2072f1a | 578 | |
579 | char z = source[0]; | |
580 | ||
581 | if (z == ':') | |
582 | source++; | |
583 | ||
c10b78ac | 584 | joiner = find(source); |
f2072f1a | 585 | |
586 | if (stricmp(channel, c_Forest) == 0 && is_playing(joiner)) | |
587 | { | |
000f8409 | 588 | #ifdef DEBUGMODE |
589 | log("Player %s (IRC: %s) joined %s", | |
590 | joiner->stats->getName().c_str(), | |
591 | #ifdef P10 | |
592 | joiner->getRealNick(), | |
593 | #else | |
594 | joiner->getNick(), | |
595 | #endif | |
596 | c_Forest); | |
597 | #endif | |
598 | raw(":%S MODE %s +v %s", c_Forest, (source)); | |
f2072f1a | 599 | } |
600 | ||
601 | if (z == ':') | |
602 | source--; | |
c7340cbd | 603 | |
000f8409 | 604 | #if defined(BAHAMUT) |
c7340cbd | 605 | } else if (stricmp(cmd, "SJOIN") == 0) { |
581ec09e | 606 | char *channel, *nick, *tmp, *rest; |
c7340cbd | 607 | strtok(NULL, " "); // Ignore the TS |
581ec09e | 608 | #ifndef HYBRID |
c7340cbd | 609 | strtok(NULL, " "); // Ignore the TS |
581ec09e | 610 | #endif |
c7340cbd | 611 | channel = strtok(NULL, " "); |
581ec09e | 612 | rest = strtok(NULL, ""); |
613 | tmp = strchr(rest, ':'); | |
614 | tmp++; | |
615 | nick = strtok(tmp, " "); | |
616 | while (nick != NULL) | |
617 | { | |
618 | if (*nick == '@') | |
619 | nick++; | |
620 | if (*nick == '+') | |
621 | nick++; // Assume for users set op and voice, they | |
622 | // are never passed as +@nick | |
623 | if (stricmp(channel, c_Forest) == 0 && is_playing(nick)) | |
624 | raw(":%S MODE %s +v %s", channel, nick); | |
625 | ||
626 | nick = strtok(NULL, " "); | |
627 | } | |
628 | #endif | |
85ce9d3e | 629 | } else { |
000f8409 | 630 | #ifdef DEBUGMODE |
9f8c2acc | 631 | log("Unrecognized Message: cmd = %s source = %s", cmd, source); |
000f8409 | 632 | #endif |
85ce9d3e | 633 | } |
634 | } | |
4dde2ed9 | 635 | |
05c527e6 | 636 | } // for loop for connection retry |
637 | ||
4dde2ed9 | 638 | end: |
639 | ||
c8ada07e | 640 | save_gs_dbase(); |
8e800549 | 641 | save_dragon(); |
5aa1d28d | 642 | saveNews(newsdata, todaysnews); |
f9db99e0 | 643 | clearClients(); |
644 | clearPlayers(); | |
5de7a0b0 | 645 | clearItems(); |
c8ada07e | 646 | delete_monsters(); |
c8ada07e | 647 | |
000f8409 | 648 | #ifdef DEBUGMODE |
9f8c2acc | 649 | log("<CLOSED>"); |
000f8409 | 650 | #endif |
9f8c2acc | 651 | |
85ce9d3e | 652 | close(sock); |
653 | unload_config_file(); | |
654 | return 0; | |
655 | } | |
656 | ||
657 | aClient *find(char *nick) | |
658 | { | |
659 | return findbynick(nick); | |
660 | } | |
661 | ||
662 | aClient *find(const char *nick) | |
663 | { | |
664 | return findbynick(nick); | |
665 | } | |
666 | ||
ce61cdfa | 667 | #ifdef P10 |
668 | ||
669 | aClient *findbyrealnick(char *realnick) | |
670 | { | |
671 | ListNode <aClient> *newPtr; | |
448a1531 | 672 | unsigned long hv = sHASH((unsigned char *) realnick); |
7996e5fd | 673 | newPtr = clients[hv].First(); |
ce61cdfa | 674 | |
675 | aClient *client = NULL; | |
676 | ||
677 | while (newPtr) | |
678 | { | |
679 | client = newPtr->getData(); | |
680 | if (stricmp(client->getRealNick(), realnick) == 0) | |
681 | return client; | |
682 | client = NULL; | |
683 | newPtr = newPtr->Next(); | |
684 | } | |
685 | return client; | |
686 | } | |
4e5760fd | 687 | |
688 | #else | |
689 | ||
690 | aClient *findbyrealnick(char *realnick) | |
691 | { | |
692 | return findbynick(realnick); | |
693 | } | |
694 | ||
ce61cdfa | 695 | #endif |
85ce9d3e | 696 | |
697 | aClient *findbynick(char *nick) | |
698 | { | |
c10b78ac | 699 | list<aClient*>::iterator iter; |
629d0d86 | 700 | #ifdef P10 |
701 | unsigned long hv = sHASH((unsigned char *) nick); | |
702 | #else | |
703 | unsigned long hv = iHASH((unsigned char *) nick); | |
704 | #endif | |
c10b78ac | 705 | |
629d0d86 | 706 | aClient *client = NULL; |
707 | ||
c10b78ac | 708 | for(iter = clients[hv].begin(); iter != clients[hv].end(); iter++) |
85ce9d3e | 709 | { |
c10b78ac | 710 | client = (*iter); |
629d0d86 | 711 | #ifdef P10 |
712 | if (strcmp(client->getNick(), nick) == 0) | |
713 | { | |
714 | #else | |
715 | if (stricmp(client->getNick(), nick) == 0) | |
716 | { | |
717 | #endif | |
718 | return client; | |
719 | } | |
720 | client = NULL; | |
85ce9d3e | 721 | } |
c10b78ac | 722 | return client; |
85ce9d3e | 723 | } |
724 | ||
d61f68e3 | 725 | Player *findplayer(const char *name) |
0a1518fa | 726 | { |
d61f68e3 | 727 | list<Player*>::iterator iter; |
728 | Player *p; | |
53d5585b | 729 | unsigned long hv = iHASH((unsigned char *) name); |
c10b78ac | 730 | for (iter = players[hv].begin(); iter != players[hv].end(); iter++) |
0a1518fa | 731 | { |
d61f68e3 | 732 | p = (*iter); |
53d5585b | 733 | if (stricmp(p->getName().c_str(), name) == 0) |
d61f68e3 | 734 | return p; |
53d5585b | 735 | p = NULL; |
0a1518fa | 736 | } |
53d5585b | 737 | return NULL; |
0a1518fa | 738 | } |
739 | ||
40251952 | 740 | void check_idles() |
741 | { | |
d61f68e3 | 742 | list<Player*>::iterator iter; |
743 | Player *p; | |
c10b78ac | 744 | |
c10b78ac | 745 | for (int x = 0; x < U_TABLE_SIZE; x++) |
40251952 | 746 | { |
c10b78ac | 747 | for (iter = players[x].begin(); iter != players[x].end(); iter++) |
14e24ba1 | 748 | { |
d61f68e3 | 749 | p = (*iter); |
14e24ba1 | 750 | |
c10b78ac | 751 | switch(p->getLevel()) |
752 | { | |
753 | case 1: | |
754 | if ((time(NULL) - p->lastlogin) / 86400 >= level1expire) | |
755 | { | |
d61f68e3 | 756 | logout(p->getClient()); |
c10b78ac | 757 | return; |
758 | } | |
759 | break; | |
760 | ||
761 | default: | |
762 | if ((time(NULL) - p->lastlogin) / 86400 >= defaultexpire) | |
763 | { | |
d61f68e3 | 764 | logout(p->getClient()); |
c10b78ac | 765 | return; |
766 | } | |
767 | break; | |
768 | } | |
769 | if (timedOut(p)) | |
770 | { | |
771 | timeOutEvent(p); | |
772 | } | |
14e24ba1 | 773 | } |
40251952 | 774 | } |
775 | } | |
776 | ||
85ce9d3e | 777 | aClient *findbynick(const char *nick) |
778 | { | |
c10b78ac | 779 | list<aClient*>::iterator iter; |
780 | #ifdef P10 | |
781 | unsigned long hv = sHASH((unsigned char *) nick); | |
782 | #else | |
783 | unsigned long hv = iHASH((unsigned char *) nick); | |
784 | #endif | |
785 | ||
786 | aClient *client = NULL; | |
787 | ||
788 | for (iter = clients[hv].begin(); iter != clients[hv].end(); iter++) | |
85ce9d3e | 789 | { |
c10b78ac | 790 | client = (*iter); |
000f8409 | 791 | #ifdef P10 |
c10b78ac | 792 | if (strcmp(client->getNick(), nick) == 0) |
793 | #else | |
23ec3ff4 | 794 | if (stricmp(client->getNick(), nick) == 0) |
c10b78ac | 795 | #endif |
796 | return client; | |
797 | client = NULL; | |
85ce9d3e | 798 | } |
c10b78ac | 799 | return client; |
85ce9d3e | 800 | } |
801 | ||
ce61cdfa | 802 | /* daemon() - detach process from user and disappear into the background |
803 | * returns -1 on failure, but you can't do much except exit in that case | |
804 | * since we may already have forked. This is based on the BSD version, | |
805 | * so the caller is responsible for things like the umask, etc. | |
806 | */ | |
807 | ||
808 | /* believed to work on all Posix systems */ | |
809 | ||
810 | int daemon(int nochdir, int noclose) | |
811 | { | |
812 | pid_t pid; | |
813 | switch (pid = fork()) | |
814 | { | |
815 | case 0: break; | |
816 | case -1: return -1; | |
817 | default: _exit(0); /* exit the original process */ | |
818 | } | |
819 | ||
820 | if (setsid() < 0) /* shoudn't fail */ | |
821 | return -1; | |
822 | ||
823 | /* dyke out this switch if you want to acquire a control tty in */ | |
824 | /* the future -- not normally advisable for daemons */ | |
825 | ||
826 | switch (pid = fork()) | |
827 | { | |
828 | case 0: break; | |
829 | case -1: return -1; | |
830 | default: | |
831 | ofstream outfile; | |
69ae096c | 832 | outfile.open(pidfile); |
ce61cdfa | 833 | if (outfile.fail()) |
69ae096c | 834 | cerr << "Unable to open " << pidfile << endl; |
ce61cdfa | 835 | outfile << pid << endl; |
836 | outfile.close(); | |
837 | ||
838 | _exit(0); | |
839 | } | |
840 | ||
841 | if (!nochdir) | |
842 | chdir("/"); | |
843 | ||
844 | if (!noclose) | |
845 | { | |
846 | closeall(0); | |
847 | open("/dev/null",O_RDWR); | |
848 | dup(0); dup(0); | |
849 | } | |
850 | ||
851 | return 0; | |
852 | } | |
853 | ||
854 | ||
855 | /* closeall() -- close all FDs >= a specified value */ | |
856 | ||
857 | void closeall(int fd) | |
858 | { | |
859 | int fdlimit = sysconf(_SC_OPEN_MAX); | |
860 | ||
861 | while (fd < fdlimit) | |
862 | close(fd++); | |
863 | } | |
864 | ||
15838737 | 865 | void prettyIntro() |
866 | { | |
db6cc963 | 867 | cout << endl; |
868 | cout << " GGGG AAA MM MM EEEEEEE SSSSS EEEEEEE RRRRRR VV VV " << endl; | |
869 | cout << " GG GG AAAAA MMM MMM EE SS EE RR RR VV VV " << endl; | |
870 | cout << "GG AA AA MM MM MM EEEEE SSSSS EEEEE RRRRRR VV VV " << endl; | |
871 | cout << "GG GGG AAAAAAA MM MM EE SS EE RR RR VV VV " << endl; | |
872 | cout << "G G AA AA MM MM EEEEEEE SSSSS EEEEEEE RR RR VVV" << endl; | |
873 | cout << " GGGGG V\n\n" << endl; | |
874 | cout << "Version: " << VERSION << endl; | |
875 | cout << "http://www.gameserv.us - http://www.sourceforge.net/projects/gameservirc" << endl; | |
15838737 | 876 | } |
2edcd222 | 877 | |
878 | void load_lastrefresh() | |
879 | { | |
880 | ifstream infile; | |
881 | infile.open(".gsrefresh"); | |
882 | if (infile.fail()) | |
883 | { | |
000f8409 | 884 | #ifdef DEBUGMODE |
2edcd222 | 885 | log("Error opening .gsrefresh"); |
000f8409 | 886 | #endif |
2edcd222 | 887 | |
888 | generate: | |
14e24ba1 | 889 | long mytime = time(NULL); |
000f8409 | 890 | #ifdef DEBUGMODE |
2edcd222 | 891 | log("Generating new refresh time"); |
000f8409 | 892 | #endif |
14e24ba1 | 893 | |
894 | // Just a safety measure... tho no one should | |
1fe6fccd | 895 | // get anywhere near the actual time as their refreshperiod |
14e24ba1 | 896 | if (refreshperiod >= mytime) |
1fe6fccd | 897 | { |
898 | log("Refresh period is greater than or equal to the actual time... setting it to 86400"); | |
14e24ba1 | 899 | refreshperiod = 86400; |
1fe6fccd | 900 | } |
901 | ||
14e24ba1 | 902 | lastrefresh = mytime - (mytime % refreshperiod); |
1fe6fccd | 903 | |
2edcd222 | 904 | refreshall(); |
905 | save_lastrefresh(); | |
906 | return; | |
907 | } | |
908 | infile >> lastrefresh; | |
1fe6fccd | 909 | |
2edcd222 | 910 | infile.close(); |
911 | if (lastrefresh < 0) | |
912 | goto generate; | |
913 | } | |
914 | ||
1fe6fccd | 915 | void load_lastrollover() |
916 | { | |
917 | ifstream infile; | |
918 | infile.open(".gsrollover"); | |
919 | if (infile.fail()) | |
920 | { | |
921 | #ifdef DEBUGMODE | |
922 | log("Error opening .gsrollover"); | |
923 | #endif | |
924 | ||
925 | generate: | |
926 | long mytime = time(NULL); | |
927 | #ifdef DEBUGMODE | |
928 | log("Generating new rollover time"); | |
929 | #endif | |
930 | lastrollover = mytime; | |
931 | return; | |
932 | } | |
933 | infile >> lastrollover; | |
934 | ||
935 | infile.close(); | |
936 | if (lastrollover < 0) | |
937 | goto generate; | |
938 | } | |
939 | ||
2edcd222 | 940 | void save_lastrefresh() |
941 | { | |
942 | ofstream outfile; | |
943 | ||
944 | outfile.open(".gsrefresh"); | |
945 | ||
946 | if (outfile.fail()) | |
947 | { | |
948 | log("Error creating new file .gsrefresh"); | |
949 | return; | |
950 | } | |
1fe6fccd | 951 | outfile << lastrefresh << endl << lastrollover; |
2edcd222 | 952 | |
953 | outfile.close(); | |
954 | } | |
1fe6fccd | 955 | |
956 | void save_lastrollover() | |
957 | { | |
958 | ofstream outfile; | |
959 | ||
960 | outfile.open(".gsrollover"); | |
961 | ||
962 | if (outfile.fail()) | |
963 | { | |
964 | log("Error creating new file .gsrollover"); | |
965 | return; | |
966 | } | |
967 | outfile << lastrollover << endl; | |
968 | outfile.close(); | |
969 | } | |
5de7a0b0 | 970 | void clearItems() |
971 | { | |
972 | list<item*>::iterator iter; | |
973 | for (iter = Items.begin(); iter != Items.end(); iter++) | |
974 | { | |
975 | delete (*iter); | |
976 | Items.erase(iter); | |
977 | } | |
978 | } | |
f9db99e0 | 979 | void clearClients() |
980 | { | |
981 | list<aClient*>::iterator iter; | |
982 | for (unsigned long x = 0; x < U_TABLE_SIZE; x++) | |
983 | { | |
984 | for (iter = clients[x].begin(); iter != clients[x].end(); iter++) | |
985 | { | |
986 | delete (*iter); | |
987 | clients[x].erase(iter); | |
988 | } | |
989 | } | |
990 | } | |
5de7a0b0 | 991 | |
f9db99e0 | 992 | void clearPlayers() |
993 | { | |
994 | list<Player*>::iterator iter; | |
995 | for (unsigned long x = 0; x < U_TABLE_SIZE; x++) | |
996 | { | |
997 | for (iter = players[x].begin(); iter != players[x].end(); iter++) | |
998 | { | |
999 | delete (*iter); | |
1000 | players[x].erase(iter); | |
1001 | } | |
1002 | } | |
1003 | } |