Debian Codegraph @

src/bnetd/luainterface.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2014 HarpyWar ([email protected])
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. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #define GAME_INTERNAL_ACCESScxx . . $ `src/bnetd/luainterface.cpp:18:9`!
19 #ifdef WITH_LUA
20 #include "common/setup_before.h"
21
22 #include "team.h"
23
24 #include <cctype>
25 #include <cerrno>
26 #include <cstring>
27 #include <cstdlib>
28 #include <cmath>
29
30 #include "compat/strcasecmp.h"
31 #include "compat/pdir.h"
32 #include "common/tag.h"
33 #include "common/util.h"
34 #include "common/eventlog.h"
35
36 #include "connection.h"
37 #include "message.h"
38 #include "channel.h"
39 #include "game.h"
40 #include "account.h"
41 #include "account_wrap.h"
42 #include "timer.h"
43 #include "ipban.h"
44 #include "command_groups.h"
45 #include "friends.h"
46 #include "clan.h"
47 #include "prefs.h"
48
49
50 #include "luawrapper.h"
51 #include "luainterface.h"
52 #include "luafunctions.h"
53 #include "luaobjects.h"
54
55
56 #include "common/setup_after.h"
57
58
59 namespace pvpgn
60 {
61
62 namespace bnetd
63 {
64 lua::vm vm;
65
66 char _msgtemp[MAX_MESSAGE_LEN];
67 char _msgtemp2[MAX_MESSAGE_LEN];
68
69
70 void _register_functions();
71
72
73 /* Unload all the lua scripts */
74 extern void lua_unload()
75 {
76 // nothing to do, "vm.initialize()" already destroys lua vm before initialize
77 }
78
79 /* Initialize lua, register functions and load scripts */
80 extern void lua_load(char const * scriptdir)
81 {
82 eventlog(eventlog_level_info, __FUNCTION__, "Loading Lua interface...");
83
84 try
85 {
86 // init lua virtual machine
87 vm.initialize();
88
89 std::vector<std::string> files = dir_getfiles(scriptdir, ".lua", true);
90
91 // load all files from the script directory
92 for (int i = 0; i < files.size(); ++i)
93 {
94 vm.load_file(files[i].c_str());
95
96 std::snprintf(_msgtemp, sizeof(_msgtemp), "%s", files[i].c_str());
97 eventlog(eventlog_level_info, __FUNCTION__, "{}", _msgtemp);
98 }
99
100 _register_functions();
101
102 std::snprintf(_msgtemp, sizeof(_msgtemp), "Lua sripts were successfully loaded (%zu files)", files.size());
103 eventlog(eventlog_level_info, __FUNCTION__, "{}", _msgtemp);
104 }
105 catch (const std::exception& e)
106 {
107 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
108 }
109 catch (...)
110 {
111 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
112 }
113
114 // handle start event
115 lua_handle_server(luaevent_server_start);
116 }
117
118
119 /* Register C++ functions to be able use them from lua scripts */
120 void _register_functions()
121 {
122 // register package 'api' with functions
123 static const luaL_Reg api[] =
124 {
125 { "message_send_text", __message_send_text },
126 { "eventlog", __eventlog },
127 { "account_get_by_id", __account_get_by_id },
128 { "account_get_by_name", __account_get_by_name },
129 { "account_get_attr", __account_get_attr },
130 { "account_set_attr", __account_set_attr },
131 { "account_get_friends", __account_get_friends },
132 { "account_get_teams", __account_get_teams },
133 { "clan_get_members", __clan_get_members },
134
135 { "game_get_by_id", __game_get_by_id },
136 { "game_get_by_name", __game_get_by_name },
137
138 { "channel_get_by_id", __channel_get_by_id },
139
140 { "server_get_users", __server_get_users },
141 { "server_get_games", __server_get_games },
142 { "server_get_channels", __server_get_channels },
143
144 { "client_kill", __client_kill },
145 { "client_readmemory", __client_readmemory },
146 { "client_requiredwork", __client_requiredwork },
147
148 { "command_get_group", __command_get_group },
149 { "icon_get_rank", __icon_get_rank },
150 { "describe_command", __describe_command },
151 { "messagebox_show", __messagebox_show },
152
153 { "localize", __localize },
154
155 { 0, 0 }
156 };
157 vm.reg("api", api);
158
159 // register standalone functions
160 //vm.reg("sum", _sum); // (test function)
161
162
163
164 // global variables
165 lua::table g(vm);
166 g.update("PVPGN_SOFTWARE", PVPGN_SOFTWARE);
167 g.update("PVPGN_VERSION", PVPGN_VERSION);
168
169 // config variables from bnetd.conf
170 lua::transaction bind(vm);
171 bind.lookup("config");
172 {
173 lua::table config = bind.table();
174 config.update("filedir", prefs_get_filedir());
175 config.update("i18ndir", prefs_get_i18ndir());
176 config.update("scriptdir", prefs_get_scriptdir());
177 config.update("reportdir", prefs_get_reportdir());
178 config.update("chanlogdir", prefs_get_chanlogdir());
179 config.update("userlogdir", prefs_get_userlogdir());
180 config.update("localizefile", prefs_get_localizefile());
181 config.update("motdfile", prefs_get_motdfile());
182 config.update("motdw3file", prefs_get_motdw3file());
183 config.update("issuefile", prefs_get_issuefile());
184 config.update("channelfile", prefs_get_channelfile());
185 config.update("newsfile", prefs_get_newsfile());
186 config.update("adfile", prefs_get_adfile());
187 config.update("topicfile", prefs_get_topicfile());
188 config.update("ipbanfile", prefs_get_ipbanfile());
189 config.update("helpfile", prefs_get_helpfile());
190 config.update("mpqfile", prefs_get_mpqfile());
191 config.update("logfile", prefs_get_logfile());
192 config.update("realmfile", prefs_get_realmfile());
193 config.update("maildir", prefs_get_maildir());
194 config.update("versioncheck_file", prefs_get_versioncheck_file());
195 config.update("mapsfile", prefs_get_mapsfile());
196 config.update("xplevelfile", prefs_get_xplevel_file());
197 config.update("xpcalcfile", prefs_get_xpcalc_file());
198 config.update("ladderdir", prefs_get_ladderdir());
199 config.update("command_groups_file", prefs_get_command_groups_file());
200 config.update("tournament_file", prefs_get_tournament_file());
201 config.update("statusdir", prefs_get_outputdir());
202 config.update("aliasfile", prefs_get_aliasfile());
203 config.update("anongame_infos_file", prefs_get_anongame_infos_file());
204 config.update("DBlayoutfile", prefs_get_DBlayoutfile());
205 config.update("supportfile", prefs_get_supportfile());
206 config.update("transfile", prefs_get_transfile());
207 config.update("customicons_file", prefs_get_customicons_file());
208 config.update("loglevels", prefs_get_loglevels());
209 config.update("d2cs_version", prefs_get_d2cs_version());
210 config.update("allow_d2cs_setname", prefs_allow_d2cs_setname());
211 config.update("iconfile", prefs_get_iconfile());
212 config.update("war3_iconfile", prefs_get_war3_iconfile());
213 config.update("star_iconfile", prefs_get_star_iconfile());
214 config.update("tosfile", prefs_get_tosfile());
215 config.update("allowed_clients", prefs_get_allowed_clients());
216 config.update("allow_bad_version", prefs_get_allow_bad_version());
217 config.update("allow_unknown_version", prefs_get_allow_unknown_version());
218 config.update("usersync", prefs_get_user_sync_timer());
219 config.update("userflush", prefs_get_user_flush_timer());
220 config.update("userflush_connected", prefs_get_user_flush_connected());
221 config.update("userstep", prefs_get_user_step());
222 config.update("latency", prefs_get_latency());
223 config.update("nullmsg", prefs_get_nullmsg());
224 config.update("shutdown_delay", prefs_get_shutdown_delay());
225 config.update("shutdown_decr", prefs_get_shutdown_decr());
226 config.update("ipban_check_int", prefs_get_ipban_check_int());
227 config.update("new_accounts", prefs_get_allow_new_accounts());
228 config.update("max_accounts", prefs_get_max_accounts());
229 config.update("kick_old_login", prefs_get_kick_old_login());
230 config.update("ask_new_channel", prefs_get_ask_new_channel());
231 config.update("report_all_games", prefs_get_report_all_games());
232 config.update("report_diablo_games", prefs_get_report_diablo_games());
233 config.update("hide_pass_games", prefs_get_hide_pass_games());
234 config.update("hide_started_games", prefs_get_hide_started_games());
235 config.update("hide_temp_channels", prefs_get_hide_temp_channels());
236 config.update("disc_is_loss", prefs_get_discisloss());
237 config.update("ladder_games", prefs_get_ladder_games());
238 config.update("ladder_prefix", prefs_get_ladder_prefix());
239 config.update("enable_conn_all", prefs_get_enable_conn_all());
240 config.update("hide_addr", prefs_get_hide_addr());
241 config.update("chanlog", prefs_get_chanlog());
242 config.update("quota", prefs_get_quota());
243 config.update("quota_lines", prefs_get_quota_lines());
244 config.update("quota_time", prefs_get_quota_time());
245 config.update("quota_wrapline", prefs_get_quota_wrapline());
246 config.update("quota_maxline", prefs_get_quota_maxline());
247 config.update("quota_dobae", prefs_get_quota_dobae());
248 config.update("mail_support", prefs_get_mail_support());
249 config.update("mail_quota", prefs_get_mail_quota());
250 config.update("log_notice", prefs_get_log_notice());
251 config.update("passfail_count", prefs_get_passfail_count());
252 config.update("passfail_bantime", prefs_get_passfail_bantime());
253 config.update("maxusers_per_channel", prefs_get_maxusers_per_channel());
254 config.update("savebyname", prefs_get_savebyname());
255 config.update("sync_on_logoff", prefs_get_sync_on_logoff());
256 config.update("hashtable_size", prefs_get_hashtable_size());
257 config.update("account_allowed_symbols", prefs_get_account_allowed_symbols());
258 config.update("account_force_username", prefs_get_account_force_username());
259 config.update("max_friends", prefs_get_max_friends());
260 config.update("track", prefs_get_track());
261 config.update("trackaddrs", prefs_get_trackserv_addrs());
262 config.update("location", prefs_get_location());
263 config.update("description", prefs_get_description());
264 config.update("url", prefs_get_url());
265 config.update("contact_name", prefs_get_contact_name());
266 config.update("contact_email", prefs_get_contact_email());
267 config.update("servername", prefs_get_servername());
268 config.update("max_connections", prefs_get_max_connections());
269 config.update("packet_limit", prefs_get_packet_limit());
270 config.update("max_concurrent_logins", prefs_get_max_concurrent_logins());
271 config.update("use_keepalive", prefs_get_use_keepalive());
272 config.update("max_conns_per_IP", prefs_get_max_conns_per_IP());
273 config.update("servaddrs", prefs_get_bnetdserv_addrs());
274 config.update("udptest_port", prefs_get_udptest_port());
275 config.update("w3routeaddr", prefs_get_w3route_addr());
276 config.update("initkill_timer", prefs_get_initkill_timer());
277 config.update("wolv1addrs", prefs_get_wolv1_addrs());
278 config.update("wolv2addrs", prefs_get_wolv2_addrs());
279 config.update("wgameresaddrs", prefs_get_wgameres_addrs());
280 config.update("apiregaddrs", prefs_get_apireg_addrs());
281 config.update("woltimezone", prefs_get_wol_timezone());
282 config.update("wollongitude", prefs_get_wol_longitude());
283 config.update("wollatitude", prefs_get_wol_latitude());
284 config.update("wol_autoupdate_serverhost", prefs_get_wol_autoupdate_serverhost());
285 config.update("wol_autoupdate_username", prefs_get_wol_autoupdate_username());
286 config.update("wol_autoupdate_password", prefs_get_wol_autoupdate_password());
287 config.update("ircaddrs", prefs_get_irc_addrs());
288 config.update("irc_network_name", prefs_get_irc_network_name());
289 config.update("hostname", prefs_get_hostname());
290 config.update("irc_latency", prefs_get_irc_latency());
291 config.update("telnetaddrs", prefs_get_telnet_addrs());
292 config.update("war3_ladder_update_secs", prefs_get_war3_ladder_update_secs());
293 config.update("XML_output_ladder", prefs_get_XML_output_ladder());
294 config.update("output_update_secs", prefs_get_output_update_secs());
295 config.update("XML_status_output", prefs_get_XML_status_output());
296 config.update("clan_newer_time", prefs_get_clan_newer_time());
297 config.update("clan_max_members", prefs_get_clan_max_members());
298 config.update("clan_channel_default_private", prefs_get_clan_channel_default_private());
299 config.update("clan_min_invites", prefs_get_clan_min_invites());
300 config.update("log_commands", prefs_get_log_commands());
301 config.update("log_command_groups", prefs_get_log_command_groups());
302 config.update("log_command_list", prefs_get_log_command_list());
303
304 }
305
306 }
307
308
309
310
311
312 /* Lua Events (called from scripts) */
313 #ifndef _LUA_EVENTS_
314
315 extern int lua_handle_command(t_connection * c, char const * text, t_luaevent_type luaevent)
316 {
317 t_account * account;
318 const char * func_name;
319 int result = -2;
320 switch (luaevent)
321 {
322 case luaevent_command:
323 func_name = "handle_command";
324 break;
325 case luaevent_command_before:
326 func_name = "handle_command_before";
327 break;
328 default:
329 return result;
330 }
331 try
332 {
333 if (!(account = conn_get_account(c)))
334 return -2;
335
336 std::map<std::string, std::string> o_account = get_account_object(account);
337 lua::transaction(vm) << lua::lookup(func_name) << o_account << text << lua::invoke >> result << lua::end; // invoke lua function
338
339 }
340 catch (const std::exception& e)
341 {
342 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
343 }
344 catch (...)
345 {
346 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
347 }
348 return result;
349 }
350
351 extern void lua_handle_game(t_game * game, t_connection * c, t_luaevent_type luaevent)
352 {
353 t_account * account;
354 const char * func_name;
355 switch (luaevent)
356 {
357 case luaevent_game_create:
358 func_name = "handle_game_create";
359 break;
360 case luaevent_game_report:
361 func_name = "handle_game_report";
362 break;
363 case luaevent_game_end:
364 func_name = "handle_game_end";
365 break;
366 case luaevent_game_destroy:
367 func_name = "handle_game_destroy";
368 break;
369 case luaevent_game_changestatus:
370 func_name = "handle_game_changestatus";
371 break;
372 case luaevent_game_userjoin:
373 func_name = "handle_game_userjoin";
374 break;
375 case luaevent_game_userleft:
376 func_name = "handle_game_userleft";
377 break;
378 default:
379 return;
380 }
381 try
382 {
383 std::map<std::string, std::string> o_game = get_game_object(game);
384
385 // handle_game_userjoin & handle_game_userleft
386 if (c)
387 {
388 if (!(account = conn_get_account(c)))
389 return;
390
391 std::map<std::string, std::string> o_account = get_account_object(account);
392 lua::transaction(vm) << lua::lookup(func_name) << o_game << o_account << lua::invoke << lua::end; // invoke lua function
393 }
394 // other functions
395 else
396 {
397 lua::transaction(vm) << lua::lookup(func_name) << o_game << lua::invoke << lua::end; // invoke lua function
398 }
399
400 }
401 catch (const std::exception& e)
402 {
403 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
404 }
405 catch (...)
406 {
407 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
408 }
409 }
410
411 std::vector<t_game*> lua_handle_game_list(t_connection * c)
412 {
413 t_account * account;
414 std::vector<std::string> columns, data;
415 std::vector<t_game*> result;
416 try
417 {
418 if (!(account = conn_get_account(c)))
419 return result;
420
421 std::map<std::string, std::string> o_account = get_account_object(account);
422 lua::transaction(vm) << lua::lookup("handle_game_list") << o_account << lua::invoke >> columns >> data << lua::end; // invoke lua function
423
424 // check consistency of data and columns
425 if (columns.size() == 0 || columns.size() != data.size() || std::floor((float)(data.size() / columns.size())) != (data.size() / columns.size()))
426 return result;
427
428 // fill map result
429 for (std::vector<std::string>::size_type i = 1; i < data.size(); i += columns.size())
430 {
431 // init empty game struct
432 t_game * game = (t_game*)xmalloc(sizeof(t_game));
433 game->id = 0;
434 game->name = NULL;
435
436 // next columns
437 for (int j = 1; j < columns.size(); j++)
438 {
439 if (columns[j] == "id")
440 game->id = atoi(data[i+j-1].c_str());
441 else if (columns[j] == "name")
442 game->name = xstrdup(data[i + j - 1].c_str());
443 }
444 result.push_back(game);
445 }
446 }
447 catch (const std::exception& e)
448 {
449 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
450 }
451 catch (...)
452 {
453 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
454 }
455 return result;
456 }
457
458
459 extern int lua_handle_channel(t_channel * channel, t_connection * c, char const * message_text, t_message_type message_type, t_luaevent_type luaevent)
460 {
461 int result = 0;
462 t_account * account;
463 const char * func_name;
464 switch (luaevent)
465 {
466 case luaevent_channel_message:
467 func_name = "handle_channel_message";
468 break;
469 case luaevent_channel_userjoin:
470 func_name = "handle_channel_userjoin";
471 break;
472 case luaevent_channel_userleft:
473 func_name = "handle_channel_userleft";
474 break;
475 default:
476 return 0;
477 }
478 try
479 {
480 if (!(account = conn_get_account(c)))
481 return 0;
482
483 std::map<std::string, std::string> o_account = get_account_object(account);
484 std::map<std::string, std::string> o_channel = get_channel_object(channel);
485
486 // handle_channel_userleft & handle_channel_message
487 if (message_text)
488 lua::transaction(vm) << lua::lookup(func_name) << o_channel << o_account << message_text << message_type << lua::invoke >> result << lua::end; // invoke lua function
489 // other functions
490 else
491 lua::transaction(vm) << lua::lookup(func_name) << o_channel << o_account << lua::invoke << lua::end; // invoke lua function
492 }
493 catch (const std::exception& e)
494 {
495 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
496 }
497 catch (...)
498 {
499 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
500 }
501 return result;
502 }
503
504
505 extern int lua_handle_user(t_connection * c, t_connection * c_dst, char const * message_text, t_luaevent_type luaevent)
506 {
507 t_account * account, *account_dst;
508 const char * func_name;
509 int result = 0;
510 switch (luaevent)
511 {
512 case luaevent_user_whisper:
513 func_name = "handle_user_whisper";
514 break;
515 case luaevent_user_login:
516 func_name = "handle_user_login";
517 break;
518 case luaevent_user_disconnect:
519 func_name = "handle_user_disconnect";
520 break;
521 default:
522 return 0;
523 }
524 try
525 {
526 if (!(account = conn_get_account(c)))
527 return 0;
528
529 std::map<std::string, std::string> o_account = get_account_object(account);
530
531 // handle_server_whisper
532 if (c_dst && message_text)
533 {
534 if (!(account_dst = conn_get_account(c_dst)))
535 return 0;
536 std::map<std::string, std::string> o_account_dst = get_account_object(account_dst);
537
538 lua::transaction(vm) << lua::lookup(func_name) << o_account << o_account_dst << message_text << lua::invoke >> result << lua::end; // invoke lua function
539 }
540 // other functions
541 else
542 lua::transaction(vm) << lua::lookup(func_name) << o_account << lua::invoke << lua::end; // invoke lua function
543 }
544 catch (const std::exception& e)
545 {
546 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
547 }
548 catch (...)
549 {
550 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
551 }
552 return result;
553 }
554
555 extern const char * lua_handle_user_icon(t_connection * c, const char * iconinfo)
556 {
557 t_account * account;
558 const char * result = NULL;
559 try
560 {
561 if (!(account = conn_get_account(c)))
562 return 0;
563 std::map<std::string, std::string> o_account = get_account_object(account);
564
565 lua::transaction(vm) << lua::lookup("handle_user_icon") << o_account << iconinfo << lua::invoke >> result << lua::end; // invoke lua function
566 }
567 catch (const std::exception& e)
568 {
569 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
570 }
571 catch (...)
572 {
573 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
574 }
575 return result;
576 }
577
578
579
580 extern void lua_handle_server(t_luaevent_type luaevent)
581 {
582 const char * func_name;
583 switch (luaevent)
584 {
585 case luaevent_server_start:
586 func_name = "main"; // when all lua scripts are loaded
587 break;
588 case luaevent_server_mainloop:
589 func_name = "handle_server_mainloop"; // one time per second
590 break;
591 case luaevent_server_rehash:
592 func_name = "handle_server_rehash"; // when restart Lua VM
593 break;
594 default:
595 return;
596 }
597 try
598 {
599 lua::transaction(vm) << lua::lookup(func_name) << lua::invoke << lua::end; // invoke lua function
600 }
601 catch (const std::exception& e)
602 {
603 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
604 }
605 catch (...)
606 {
607 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
608 }
609 }
610
611 extern void lua_handle_client_readmemory(t_connection * c, int request_id, std::vector<int> data)
612 {
613 t_account * account;
614 try
615 {
616 if (!(account = conn_get_account(c)))
617 return;
618
619 std::map<std::string, std::string> o_account = get_account_object(account);
620
621 lua::transaction(vm) << lua::lookup("handle_client_readmemory") << o_account << request_id << data << lua::invoke << lua::end; // invoke lua function
622 }
623 catch (const std::exception& e)
624 {
625 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
626 }
627 catch (...)
628 {
629 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
630 }
631 }
632
633 extern void lua_handle_client_extrawork(t_connection * c, int gametype, int length, const char * data)
634 {
635 t_account * account;
636 try
637 {
638 if (!(account = conn_get_account(c)))
639 return;
640
641 std::map<std::string, std::string> o_account = get_account_object(account);
642
643 lua::transaction(vm) << lua::lookup("handle_client_extrawork") << o_account << gametype << length << data << lua::invoke << lua::end; // invoke lua function
644 }
645 catch (const std::exception& e)
646 {
647 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
648 }
649 catch (...)
650 {
651 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
652 }
653 }
654 #endif
655
656
657 }
658 }
659 #endif