Debian Codegraph @

src/bnetd/luafunctions.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 #ifdef WITH_LUA
19 #include "common/setup_before.h"
20 #define GAME_INTERNAL_ACCESS
21
22 #include <cctype>
23 #include <cerrno>
24 #include <cstring>
25 #include <cstdlib>
26 #include <sstream>
27
28 #include "compat/strcasecmp.h"
29 #include "common/tag.h"
30 #include "common/util.h"
31 #include "common/eventlog.h"
32 #include "common/list.h"
33 #include "common/hashtable.h"
34 #include "common/xstring.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 "attrlayer.h"
48 #include "icons.h"
49 #include "helpfile.h"
50 #include "i18n.h"
51
52 #include "luawrapper.h"
53 #include "luaobjects.h"
54
55 #include "common/setup_after.h"
56
57
58 namespace pvpgn
59 {
60
61 namespace bnetd
62 {
63
64 typedef enum {
65 attr_type_str,
66 attr_type_num,
67 attr_type_bool,
68 attr_type_raw
69 } t_attr_type;
70
71
72 /* Send message text to user */
73 extern int __message_send_text(lua_State* L)
74 {
75 const char *text;
76 const char *username_src, *username_dst;
77 int message_type;
78 t_connection *c_src = NULL, *c_dst = NULL;
79
80 try
81 {
82 lua::stack st(L);
83 // get args
84 st.at(1, username_dst);
85 st.at(2, message_type);
86 st.at(3, username_src);
87 st.at(4, text);
88 }
89 catch (const std::exception& e)
90 {
91 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
92 }
93 catch (...)
94 {
95 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
96 }
97
98 // get user connections
99 if (t_account * account = accountlist_find_account(username_dst))
100 c_dst = account_get_conn(account);
101
102 if (username_src)
103 if (t_account * account = accountlist_find_account(username_src))
104 c_src = account_get_conn(account);
105
106 // send message
107 if (c_dst)
108 {
109 // src connection can be NULL if message_type == message_type_whisper (message will be sent from the server name)
110 message_send_text(c_dst, (t_message_type)message_type, c_src, text);
111 }
112
113 return 0;
114 }
115
116 /* Log text into logfile */
117 extern int __eventlog(lua_State* L)
118 {
119 int loglevel;
120 const char *text, *function;
121
122 try
123 {
124 lua::stack st(L);
125 // get args
126 st.at(1, loglevel);
127 st.at(2, function);
128 st.at(3, text);
129 eventlog(t_eventlog_level(loglevel), function, "{}", text);
130 }
131 catch (const std::exception& e)
132 {
133 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
134 }
135 catch (...)
136 {
137 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
138 }
139
140 return 0;
141 }
142
143 /* Get one account table object */
144 extern int __account_get_by_name(lua_State* L)
145 {
146 const char *username;
147 std::map<std::string, std::string> o_account;
148
149 try
150 {
151 lua::stack st(L);
152 // get args
153 st.at(1, username);
154 o_account = get_account_object(username);
155
156 st.push(o_account);
157 }
158 catch (const std::exception& e)
159 {
160 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
161 }
162 catch (...)
163 {
164 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
165 }
166
167 return 1;
168 }
169
170
171 /* Get one account table object */
172 extern int __account_get_by_id(lua_State* L)
173 {
174 unsigned int userid;
175 std::map<std::string, std::string> o_account;
176
177 try
178 {
179 lua::stack st(L);
180 // get args
181 st.at(1, userid);
182 o_account = get_account_object(userid);
183
184 st.push(o_account);
185 }
186 catch (const std::exception& e)
187 {
188 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
189 }
190 catch (...)
191 {
192 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
193 }
194
195 return 1;
196 }
197
198 /* Get account attribute value */
199 extern int __account_get_attr(lua_State* L)
200 {
201 const char *username, *attrkey;
202 int attrtype;
203
204 try
205 {
206 lua::stack st(L);
207 // get args
208 st.at(1, username);
209 st.at(2, attrkey);
210 st.at(3, attrtype);
211
212 if (t_account *account = accountlist_find_account(username))
213 {
214 switch ((t_attr_type)attrtype)
215 {
216 case attr_type_str:
217 if (const char * val = account_get_strattr(account, attrkey))
218 st.push(val);
219 break;
220 case attr_type_num:
221 st.push(account_get_numattr(account, attrkey));
222 break;
223 case attr_type_bool:
224 st.push(account_get_boolattr(account, attrkey));
225 break;
226 case attr_type_raw:
227 if (const char * val = account_get_rawattr(account, attrkey))
228 st.push(val);
229 break;
230 }
231 }
232 }
233 catch (const std::exception& e)
234 {
235 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
236 }
237 catch (...)
238 {
239 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
240 }
241 return 1;
242 }
243
244 /* Set account attribute value
245 * (Return 0 if attribute is not set, and 1 if set)
246 */
247 extern int __account_set_attr(lua_State* L)
248 {
249 const char *username, *attrkey;
250 int attrtype;
251 std::map<std::string, std::string> o_account;
252 int result = 0;
253
254 try
255 {
256 lua::stack st(L);
257 // get args
258 st.at(1, username);
259 st.at(2, attrkey);
260 st.at(3, attrtype);
261
262 if (t_account *account = accountlist_find_account(username))
263 {
264 switch ((t_attr_type)attrtype)
265 {
266 case attr_type_str:
267 const char * strvalue;
268 st.at(4, strvalue);
269
270 if (account_set_strattr(account, attrkey, strvalue) >= 0)
271 result = 1;
272 break;
273 case attr_type_num:
274 int numvalue;
275 st.at(4, numvalue);
276
277 if (account_set_numattr(account, attrkey, numvalue) >= 0)
278 result = 1;
279 break;
280 case attr_type_bool:
281 bool boolvalue;
282 st.at(4, boolvalue);
283
284 if (account_set_boolattr(account, attrkey, boolvalue ? 1 : 0) >= 0)
285 result = 1;
286 break;
287 case attr_type_raw:
288 const char * rawvalue;
289 int length;
290 st.at(4, rawvalue);
291 st.at(5, length);
292
293 if (account_set_rawattr(account, attrkey, rawvalue, length) >= 0)
294 result = 1;
295 break;
296 }
297 }
298 st.push(result);
299 }
300 catch (const std::exception& e)
301 {
302 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
303 }
304 catch (...)
305 {
306 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
307 }
308 return 1;
309 }
310
311
312
313 /* Get friend list of account */
314 extern int __account_get_friends(lua_State* L)
315 {
316 const char *username;
317 std::vector<std::map<std::string, std::string> > friends;
318
319 try
320 {
321 lua::stack st(L);
322 // get args
323 st.at(1, username);
324
325 if (t_account * account = accountlist_find_account(username))
326 if (t_list *friendlist = account_get_friends(account))
327 {
328 t_elem const * curr;
329 t_friend * f;
330 LIST_TRAVERSE_CONST(friendlist, curr)
331 {
332 if (!(f = (t_friend*)elem_get_data(curr)))
333 {
334 eventlog(eventlog_level_error, __FUNCTION__, "found NULL entry in list");
335 continue;
336 }
337 friends.push_back(get_friend_object(f));
338 }
339 }
340 st.push(friends);
341 }
342 catch (const std::exception& e)
343 {
344 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
345 }
346 catch (...)
347 {
348 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
349 }
350 return 1;
351 }
352
353
354 /* Get team list of account */
355 extern int __account_get_teams(lua_State* L)
356 {
357 const char *username;
358 std::vector<std::map<std::string, std::string> > teams;
359
360 try
361 {
362 lua::stack st(L);
363 // get args
364 st.at(1, username);
365
366 if (t_account * account = accountlist_find_account(username))
367 if (t_list *teamlist = account_get_teams(account))
368 {
369 t_elem const * curr;
370 t_team * t;
371 LIST_TRAVERSE_CONST(teamlist, curr)
372 {
373 if (!(t = (t_team*)elem_get_data(curr)))
374 {
375 eventlog(eventlog_level_error, __FUNCTION__, "found NULL entry in list");
376 continue;
377 }
378 teams.push_back(get_team_object(t));
379 }
380 }
381 st.push(teams);
382 }
383 catch (const std::exception& e)
384 {
385 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
386 }
387 catch (...)
388 {
389 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
390 }
391 return 1;
392 }
393
394 /* Get clan member list */
395 extern int __clan_get_members(lua_State* L)
396 {
397 unsigned int clanid;
398 std::vector<std::map<std::string, std::string> > members;
399
400 try
401 {
402 lua::stack st(L);
403 // get args
404 st.at(1, clanid);
405
406 if (t_clan * clan = clanlist_find_clan_by_clanid(clanid))
407 if (t_list * clanmembers = clan_get_members(clan))
408 {
409 t_elem const * curr;
410 LIST_TRAVERSE_CONST(clanmembers, curr)
411 {
412 t_clanmember * m;
413 if (!(m = (t_clanmember*)elem_get_data(curr)))
414 {
415 eventlog(eventlog_level_error, __FUNCTION__, "got NULL elem in list");
416 continue;
417 }
418 members.push_back(get_clanmember_object(m));
419 }
420 }
421 st.push(members);
422 }
423 catch (const std::exception& e)
424 {
425 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
426 }
427 catch (...)
428 {
429 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
430 }
431 return 1;
432 }
433
434 /* Get one game table object */
435 extern int __game_get_by_id(lua_State* L)
436 {
437 unsigned int gameid;
438 std::map<std::string, std::string> o_game;
439 try
440 {
441 lua::stack st(L);
442 // get args
443 st.at(1, gameid);
444 o_game = get_game_object(gameid);
445
446 st.push(o_game);
447 }
448 catch (const std::exception& e)
449 {
450 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
451 }
452 catch (...)
453 {
454 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
455 }
456 return 1;
457 }
458 extern int __game_get_by_name(lua_State* L)
459 {
460 const char* gamename;
461 const char* clienttag_str;
462 int gametype;
463 std::map<std::string, std::string> o_game;
464 try
465 {
466 lua::stack st(L);
467 // get args
468 st.at(1, gamename);
469 st.at(2, clienttag_str);
470 st.at(3, gametype);
471
472 t_clienttag clienttag = tag_str_to_uint(clienttag_str);
473 o_game = get_game_object(gamename, clienttag, (t_game_type)gametype);
474
475 st.push(o_game);
476 }
477 catch (const std::exception& e)
478 {
479 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
480 }
481 catch (...)
482 {
483 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
484 }
485 return 1;
486 }
487
488
489
490 /* Get one channel table object */
491 extern int __channel_get_by_id(lua_State* L)
492 {
493 unsigned int channelid;
494 std::map<std::string, std::string> o_channel;
495 try
496 {
497 lua::stack st(L);
498 // get args
499 st.at(1, channelid);
500 o_channel = get_channel_object(channelid);
501
502 st.push(o_channel);
503 }
504 catch (const std::exception& e)
505 {
506 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
507 }
508 catch (...)
509 {
510 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
511 }
512
513 return 1;
514 }
515
516 /* Get usernames online. If allaccounts = true then return all accounts that were used by the server since start */
517 extern int __server_get_users(lua_State* L)
518 {
519 bool allaccounts = false;
520 std::vector<std::map<std::string, std::string> > users;
521 t_connection * conn;
522 t_account * account;
523
524 try
525 {
526 lua::stack st(L);
527 // get args
528 st.at(1, allaccounts);
529
530 if (allaccounts)
531 {
532 t_entry * curr;
533 HASHTABLE_TRAVERSE(accountlist(), curr)
534 {
535 if (account = (t_account*)entry_get_data(curr))
536 users.push_back(get_account_object(account));
537 }
538 }
539 else
540 {
541 t_elem const * curr;
542 LIST_TRAVERSE_CONST(connlist(), curr)
543 {
544 if (conn = (t_connection*)elem_get_data(curr))
545 if (account = conn_get_account(conn))
546 users.push_back(get_account_object(account));
547 }
548 }
549 st.push(users);
550 }
551 catch (const std::exception& e)
552 {
553 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
554 }
555 catch (...)
556 {
557 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
558 }
559 return 1;
560 }
561 /* Get game list table (id => name) */
562 extern int __server_get_games(lua_State* L)
563 {
564 std::vector<std::map<std::string, std::string> > games;
565 try
566 {
567 lua::stack st(L);
568
569 t_elist * curr;
570 t_game * game;
571 elist_for_each(curr, gamelist())
572 {
573 if (game = elist_entry(curr, t_game, glist_link))
574 games.push_back(get_game_object(game));
575 }
576 st.push(games);
577 }
578 catch (const std::exception& e)
579 {
580 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
581 }
582 catch (...)
583 {
584 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
585 }
586 return 1;
587 }
588
589 /* Get channel list (id => name) */
590 extern int __server_get_channels(lua_State* L)
591 {
592 std::vector<std::map<std::string, std::string> > channels;
593 t_channel * channel;
594
595 try
596 {
597 lua::stack st(L);
598
599 t_elem * curr;
600 LIST_TRAVERSE(channellist(), curr)
601 {
602 if (channel = (t_channel*)elem_get_data(curr))
603 channels.push_back(get_channel_object(channel));
604 }
605 st.push(channels);
606 }
607 catch (const std::exception& e)
608 {
609 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
610 }
611 catch (...)
612 {
613 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
614 }
615 return 1;
616 }
617
618
619
620
621 /* Get command groups for a command string */
622 extern int __command_get_group(lua_State* L)
623 {
624 char const * command;
625 try
626 {
627 lua::stack st(L);
628 // get args
629 st.at(1, command);
630 int group = command_get_group(command);
631
632 st.push(group);
633 }
634 catch (const std::exception& e)
635 {
636 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
637 }
638 catch (...)
639 {
640 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
641 }
642 return 1;
643 }
644
645 /* Get customicon rank by rating */
646 extern int __icon_get_rank(lua_State* L)
647 {
648 int rating;
649 char const * clienttag;
650 try
651 {
652 lua::stack st(L);
653 // get args
654 st.at(1, rating);
655 st.at(2, clienttag);
656 if (t_icon_info * icon = customicons_get_icon_by_rating(rating, (char*)clienttag))
657 st.push(icon->rank);
658 }
659 catch (const std::exception& e)
660 {
661 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
662 }
663 catch (...)
664 {
665 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
666 }
667 return 1;
668 }
669
670 /* Display help for a command */
671 extern int __describe_command(lua_State* L)
672 {
673 char const *username, *cmdname;
674 try
675 {
676 lua::stack st(L);
677 // get args
678 st.at(1, username);
679 st.at(2, cmdname);
680
681 if (t_account * account = accountlist_find_account(username))
682 {
683 if (t_connection * c = account_get_conn(account))
684 describe_command(c, cmdname);
685 }
686 }
687 catch (const std::exception& e)
688 {
689 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
690 }
691 catch (...)
692 {
693 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
694 }
695 return 0;
696 }
697
698 /* Show messagebox */
699 extern int __messagebox_show(lua_State* L)
700 {
701 char const *username, *text, *caption;
702 int messagebox_type;
703 try
704 {
705 lua::stack st(L);
706 // get args
707 st.at(1, username);
708 st.at(2, text);
709 st.at(3, caption);
710 st.at(4, messagebox_type);
711
712 if (t_account * account = accountlist_find_account(username))
713 {
714 if (t_connection * c = account_get_conn(account))
715 messagebox_show(c, text, caption, messagebox_type);
716 }
717 }
718 catch (const std::exception& e)
719 {
720 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
721 }
722 catch (...)
723 {
724 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
725 }
726 return 0;
727 }
728
729 /* Read memory of game client */
730 extern int __client_readmemory(lua_State* L)
731 {
732 const char * username;
733 unsigned int request_id, offset, length;
734 try
735 {
736 lua::stack st(L);
737 // get args
738 st.at(1, username);
739 st.at(2, request_id);
740 st.at(3, offset);
741 st.at(4, length);
742
743 if (t_account * account = accountlist_find_account(username))
744 {
745 if (t_connection * c = account_get_conn(account))
746 conn_client_readmemory(c, request_id, offset, length);
747 }
748 }
749 catch (const std::exception& e)
750 {
751 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
752 }
753 catch (...)
754 {
755 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
756 }
757 return 0;
758 }
759
760 /* Send SID_REQUIREDWORK packet to a client */
761 extern int __client_requiredwork(lua_State* L)
762 {
763 const char * username;
764 const char * filename;
765 try
766 {
767 lua::stack st(L);
768 // get args
769 st.at(1, username);
770 st.at(2, filename);
771
772 if (t_account * account = accountlist_find_account(username))
773 {
774 if (t_connection * c = account_get_conn(account))
775 conn_client_requiredwork(c, filename);
776 }
777 }
778 catch (const std::exception& e)
779 {
780 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
781 }
782 catch (...)
783 {
784 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
785 }
786 return 0;
787 }
788
789 /* Destroy client connection */
790 extern int __client_kill(lua_State* L)
791 {
792 const char * username;
793 try
794 {
795 lua::stack st(L);
796 // get args
797 st.at(1, username);
798
799 if (t_account * account = accountlist_find_account(username))
800 {
801 if (t_connection * c = account_get_conn(account))
802 conn_set_state(c, conn_state_destroy);
803 }
804 }
805 catch (const std::exception& e)
806 {
807 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
808 }
809 catch (...)
810 {
811 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
812 }
813 return 0;
814 }
815
816 /* Localize text */
817 extern int __localize(lua_State* L)
818 {
819 const char *username, *text;
820 const char *arg1, *arg2, *arg3, *arg4, *arg5;
821 try
822 {
823 lua::stack st(L);
824 // get args
825 st.at(1, username);
826 st.at(2, text);
827 st.at(3, arg1);
828 st.at(4, arg2);
829 st.at(5, arg3);
830 st.at(6, arg4);
831 st.at(7, arg5);
832
833 if (t_account * account = accountlist_find_account(username))
834 {
835 if (t_connection * c = account_get_conn(account))
836 {
837 st.push(localize(c, text, arg1, arg2, arg3, arg4, arg5));
838 }
839 }
840 }
841 catch (const std::exception& e)
842 {
843 eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what());
844 }
845 catch (...)
846 {
847 eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
848 }
849 return 1;
850 }
851 }
852 }
853 #endif