| 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 ACCOUNT_INTERNAL_ACCESS |
| 21 | #define GAME_INTERNAL_ACCESS |
| 22 | #define CHANNEL_INTERNAL_ACCESS |
| 23 | #define CLAN_INTERNAL_ACCESS |
| 24 | #define TEAM_INTERNAL_ACCESS |
| 25 | |
| 26 | #include "team.h" |
| 27 | |
| 28 | #include <cctype> |
| 29 | #include <cerrno> |
| 30 | #include <cstring> |
| 31 | #include <cstdlib> |
| 32 | #include <sstream> |
| 33 | |
| 34 | #include "compat/strcasecmp.h" |
| 35 | #include "common/tag.h" |
| 36 | #include "common/util.h" |
| 37 | #include "common/eventlog.h" |
| 38 | #include "common/list.h" |
| 39 | #include "common/flags.h" |
| 40 | #include "common/proginfo.h" |
| 41 | #include "common/addr.h" |
| 42 | #include "common/xstring.h" |
| 43 | |
| 44 | #include "connection.h" |
| 45 | #include "message.h" |
| 46 | #include "channel.h" |
| 47 | #include "game.h" |
| 48 | #include "account.h" |
| 49 | #include "account_wrap.h" |
| 50 | #include "timer.h" |
| 51 | #include "ipban.h" |
| 52 | #include "command_groups.h" |
| 53 | #include "friends.h" |
| 54 | #include "clan.h" |
| 55 | |
| 56 | #include "luaobjects.h" |
| 57 | |
| 58 | #include "common/setup_after.h" |
| 59 | |
| 60 | |
| 61 | namespace pvpgn |
| 62 | { |
| 63 | namespace bnetd |
| 64 | { |
| 65 | template <class T, class A> |
| 66 | T join(const A &begin, const A &end, const T &t); |
| 67 | |
| 68 | extern std::map<std::string, std::string> get_account_object(unsigned int userid) |
| 69 | { |
| 70 | std::map<std::string, std::string> o_account; |
| 71 | |
| 72 | if (userid == 0) |
| 73 | return o_account; |
| 74 | |
| 75 | if (t_account * account = accountlist_find_account_by_uid(userid)) |
| 76 | o_account = get_account_object(account); |
| 77 | return o_account; |
| 78 | } |
| 79 | extern std::map<std::string, std::string> get_account_object(const char * username) |
| 80 | { |
| 81 | std::map<std::string, std::string> o_account; |
| 82 | |
| 83 | if (username[0] == '\0') |
| 84 | return o_account; |
| 85 | |
| 86 | if (t_account * account = accountlist_find_account(username)) |
| 87 | o_account = get_account_object(account); |
| 88 | return o_account; |
| 89 | } |
| 90 | extern std::map<std::string, std::string> get_account_object(t_account * account) |
| 91 | { |
| 92 | std::map<std::string, std::string> o_account; |
| 93 | |
| 94 | if (!account) |
| 95 | return o_account; |
| 96 | |
| 97 | // DO NOT ADD FIELDS FROM A DATABASE HERE - IT WILL CAUSE WASTE SQL QUERIES WHEN ITERATE ALL CONNECTIONS |
| 98 | |
| 99 | o_account["id"] = std_to_string(account_get_uid(account)); |
| 100 | o_account["name"] = account_get_name(account); |
| 101 | |
| 102 | o_account["online"] = "false"; // set init as offline |
| 103 | |
| 104 | // if user online |
| 105 | if (t_connection * c = account_get_conn(account)) |
| 106 | { |
| 107 | o_account["online"] = "true"; |
| 108 | |
| 109 | o_account["latency"] = std_to_string(conn_get_latency(c)); |
| 110 | |
| 111 | // non-game client doesn't provide a country |
| 112 | if (const char * country = conn_get_country(c)) |
| 113 | o_account["country"] = country; |
| 114 | |
| 115 | if (t_game *game = conn_get_game(c)) |
| 116 | o_account["game_id"] = std_to_string(game_get_id(game)); |
| 117 | if (t_channel *channel = conn_get_channel(c)) |
| 118 | o_account["channel_id"] = std_to_string(channel_get_channelid(channel)); |
| 119 | |
| 120 | if (t_clienttag clienttag = conn_get_clienttag(c)) |
| 121 | o_account["clienttag"] = clienttag_uint_to_str(clienttag); |
| 122 | if (t_archtag archtag = conn_get_archtag(c)) |
| 123 | o_account["archtag"] = clienttag_uint_to_str(archtag); |
| 124 | if (const char * clientexe = conn_get_clientexe(c)) |
| 125 | o_account["clientexe"] = clientexe; |
| 126 | if (const char * clientver = conn_get_clientver(c)) |
| 127 | o_account["clientver"] = clientver; |
| 128 | |
| 129 | o_account["gamelang"] = std_to_string(conn_get_gamelang(c)); |
| 130 | |
| 131 | if (int addr = conn_get_addr(c)) |
| 132 | o_account["ip"] = addr_num_to_ip_str(addr); |
| 133 | if (const char * away = conn_get_awaystr(c)) |
| 134 | o_account["away"] = away; |
| 135 | if (const char * dnd = conn_get_dndstr(c)) |
| 136 | o_account["dnd"] = dnd; |
| 137 | |
| 138 | o_account["flags"] = std_to_string(conn_get_flags(c)); |
| 139 | |
| 140 | if (const char * charname = conn_get_charname(c)) |
| 141 | o_account["charname"] = charname; |
| 142 | |
| 143 | o_account["idletime"] = std_to_string(conn_get_idletime(c)); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | if (account->clanmember) |
| 148 | o_account["clan_id"] = account->clanmember->clan->clanid; |
| 149 | |
| 150 | // - friends can be get from lua_account_get_friends |
| 151 | // - teams can be get from lua_account_get_teams |
| 152 | |
| 153 | return o_account; |
| 154 | } |
| 155 | |
| 156 | extern std::map<std::string, std::string> get_game_object(unsigned int gameid) |
| 157 | { |
| 158 | std::map<std::string, std::string> o_game; |
| 159 | |
| 160 | if (t_game * game = gamelist_find_game_byid(gameid)) |
| 161 | o_game = get_game_object(game); |
| 162 | return o_game; |
| 163 | } |
| 164 | extern std::map<std::string, std::string> get_game_object(const char * gamename, t_clienttag clienttag, t_game_type gametype) |
| 165 | { |
| 166 | std::map<std::string, std::string> o_game; |
| 167 | |
| 168 | if (t_game * game = gamelist_find_game(gamename, clienttag, gametype)) |
| 169 | o_game = get_game_object(game); |
| 170 | return o_game; |
| 171 | } |
| 172 | extern std::map<std::string, std::string> get_game_object(t_game * game) |
| 173 | { |
| 174 | std::map<std::string, std::string> o_game; |
| 175 | |
| 176 | if (!game) |
| 177 | return o_game; |
| 178 | |
| 179 | o_game["id"] = std_to_string(game->id); |
| 180 | o_game["name"] = game->name; |
| 181 | o_game["pass"] = game->pass; |
| 182 | o_game["info"] = game->info; |
| 183 | o_game["type"] = std_to_string(game->type); |
| 184 | o_game["flag"] = std_to_string(game->flag); |
| 185 | |
| 186 | o_game["address"] = addr_num_to_ip_str(game->addr); |
| 187 | o_game["port"] = std_to_string(game->port); |
| 188 | o_game["status"] = std_to_string(game->status); |
| 189 | o_game["currentplayers"] = std_to_string(game->ref); |
| 190 | o_game["totalplayers"] = std_to_string(game->count); |
| 191 | o_game["maxplayers"] = std_to_string(game->maxplayers); |
| 192 | if (game->mapname) |
| 193 | o_game["mapname"] = game->mapname; |
| 194 | o_game["option"] = std_to_string(game->option); |
| 195 | o_game["maptype"] = std_to_string(game->maptype); |
| 196 | o_game["tileset"] = std_to_string(game->tileset); |
| 197 | o_game["speed"] = std_to_string(game->speed); |
| 198 | o_game["mapsize_x"] = std_to_string(game->mapsize_x); |
| 199 | o_game["mapsize_y"] = std_to_string(game->mapsize_y); |
| 200 | if (t_connection *c = game->owner) |
| 201 | { |
| 202 | if (t_account *account = conn_get_account(c)) |
| 203 | o_game["owner"] = account_get_name(account); |
| 204 | } |
| 205 | |
| 206 | std::vector<std::string> players; |
| 207 | for (int i = 0; i < game->ref; i++) |
| 208 | { |
| 209 | if (t_account *account = game->players[i]) |
| 210 | players.push_back(account_get_name(account)); |
| 211 | } |
| 212 | o_game["players"] = join(players.begin(), players.end(), std::string(",")); |
| 213 | |
| 214 | |
| 215 | o_game["bad"] = std_to_string(game->bad); // if 1, then the results will be ignored |
| 216 | |
| 217 | std::vector<std::string> results; |
| 218 | if (game->results) |
| 219 | { |
| 220 | for (int i = 0; i < game->count; i++) |
| 221 | results.push_back(std_to_string(game->results[i])); |
| 222 | } |
| 223 | o_game["results"] = join(results.begin(), results.end(), std::string(",")); |
| 224 | // UNDONE: add report_heads and report_bodies: they are XML strings |
| 225 | |
| 226 | o_game["create_time"] = std_to_string(game->create_time); |
| 227 | o_game["start_time"] = std_to_string(game->start_time); |
| 228 | o_game["lastaccess_time"] = std_to_string(game->lastaccess_time); |
| 229 | |
| 230 | o_game["difficulty"] = std_to_string(game->difficulty); |
| 231 | o_game["version"] = vernum_to_verstr(game->version); |
| 232 | o_game["startver"] = std_to_string(game->startver); |
| 233 | |
| 234 | if (t_clienttag clienttag = game->clienttag) |
| 235 | o_game["clienttag"] = clienttag_uint_to_str(clienttag); |
| 236 | |
| 237 | |
| 238 | if (game->description) |
| 239 | o_game["description"] = game->description; |
| 240 | if (game->realmname) |
| 241 | o_game["realmname"] = game->realmname; |
| 242 | |
| 243 | return o_game; |
| 244 | } |
| 245 | |
| 246 | extern std::map<std::string, std::string> get_channel_object(unsigned int channelid) |
| 247 | { |
| 248 | std::map<std::string, std::string> o_channel; |
| 249 | |
| 250 | if (t_channel * channel = channellist_find_channel_bychannelid(channelid)) |
| 251 | o_channel = get_channel_object(channel); |
| 252 | return o_channel; |
| 253 | } |
| 254 | extern std::map<std::string, std::string> get_channel_object(t_channel * channel) |
| 255 | { |
| 256 | std::map<std::string, std::string> o_channel; |
| 257 | |
| 258 | if (!channel) |
| 259 | return o_channel; |
| 260 | |
| 261 | o_channel["id"] = std_to_string(channel->id); |
| 262 | o_channel["name"] = channel->name; |
| 263 | if (channel->shortname) |
| 264 | o_channel["shortname"] = channel->shortname; |
| 265 | if (channel->country) |
| 266 | o_channel["country"] = channel->country; |
| 267 | o_channel["flags"] = std_to_string(channel->flags); |
| 268 | o_channel["maxmembers"] = std_to_string(channel->maxmembers); |
| 269 | o_channel["currmembers"] = std_to_string(channel->currmembers); |
| 270 | |
| 271 | o_channel["clienttag"] = clienttag_uint_to_str(channel->clienttag); |
| 272 | if (channel->realmname) |
| 273 | o_channel["realmname"] = channel->realmname; |
| 274 | if (channel->logname) |
| 275 | o_channel["logname"] = channel->logname; |
| 276 | |
| 277 | // Westwood Online Extensions |
| 278 | o_channel["minmembers"] = std_to_string(channel->minmembers); |
| 279 | o_channel["gameType"] = std_to_string(channel->gameType); |
| 280 | if (channel->gameExtension) |
| 281 | o_channel["gameExtension"] = channel->gameExtension; |
| 282 | |
| 283 | |
| 284 | std::vector<std::string> members; |
| 285 | if (channel->memberlist) |
| 286 | { |
| 287 | t_channelmember *m = channel->memberlist; |
| 288 | while (m) |
| 289 | { |
| 290 | if (t_account *account = conn_get_account(m->connection)) |
| 291 | members.push_back(account_get_name(account)); |
| 292 | m = m->next; |
| 293 | } |
| 294 | } |
| 295 | o_channel["memberlist"] = join(members.begin(), members.end(), std::string(",")); |
| 296 | |
| 297 | std::vector<std::string> bans; |
| 298 | if (channel->banlist) |
| 299 | { |
| 300 | t_elem const * curr; |
| 301 | LIST_TRAVERSE_CONST(channel->banlist, curr) |
| 302 | { |
| 303 | char * b; |
| 304 | if (!(b = (char*)elem_get_data(curr))) |
| 305 | { |
| 306 | eventlog(eventlog_level_error, __FUNCTION__, "found NULL name in banlist"); |
| 307 | continue; |
| 308 | } |
| 309 | bans.push_back(b); |
| 310 | } |
| 311 | } |
| 312 | o_channel["banlist"] = join(bans.begin(), bans.end(), std::string(",")); |
| 313 | |
| 314 | return o_channel; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | extern std::map<std::string, std::string> get_clan_object(t_clan * clan) |
| 319 | { |
| 320 | std::map<std::string, std::string> o_clan; |
| 321 | |
| 322 | if (!clan) |
| 323 | return o_clan; |
| 324 | |
| 325 | o_clan["id"] = std_to_string(clan->clanid); |
| 326 | o_clan["tag"] = clantag_to_str(clan->tag); |
| 327 | o_clan["clanname"] = clan->clanname; |
| 328 | o_clan["created"] = std_to_string(clan->created); |
| 329 | o_clan["creation_time"] = std_to_string(clan->creation_time); |
| 330 | o_clan["clan_motd"] = clan->clan_motd; |
| 331 | o_clan["channel_type"] = std_to_string(clan->channel_type); // 0--public 1--private |
| 332 | |
| 333 | // clanmembers can be get from api.clan_get_members |
| 334 | |
| 335 | return o_clan; |
| 336 | } |
| 337 | |
| 338 | extern std::map<std::string, std::string> get_clanmember_object(t_clanmember * member) |
| 339 | { |
| 340 | std::map<std::string, std::string> o_member; |
| 341 | |
| 342 | if (!member) |
| 343 | return o_member; |
| 344 | |
| 345 | o_member["username"] = account_get_name(member->memberacc); |
| 346 | o_member["status"] = member->status; |
| 347 | o_member["clan_id"] = std_to_string(member->clan->clanid); |
| 348 | o_member["join_time"] = std_to_string(member->join_time); |
| 349 | o_member["fullmember"] = std_to_string(member->fullmember);// 0 -- clanmember is only invited, 1 -- clanmember is fullmember |
| 350 | |
| 351 | return o_member; |
| 352 | } |
| 353 | |
| 354 | extern std::map<std::string, std::string> get_team_object(t_team * team) |
| 355 | { |
| 356 | std::map<std::string, std::string> o_team; |
| 357 | |
| 358 | if (!team) |
| 359 | return o_team; |
| 360 | |
| 361 | o_team["id"] = std_to_string(team->teamid); |
| 362 | o_team["size"] = std_to_string(team->size); |
| 363 | |
| 364 | o_team["clienttag"] = clienttag_uint_to_str(team->clienttag); |
| 365 | o_team["lastgame"] = std_to_string(team->lastgame); |
| 366 | o_team["wins"] = std_to_string(team->wins); |
| 367 | o_team["losses"] = std_to_string(team->losses); |
| 368 | o_team["xp"] = std_to_string(team->xp); |
| 369 | o_team["level"] = std_to_string(team->level); |
| 370 | o_team["rank"] = std_to_string(team->rank); |
| 371 | |
| 372 | // usernames |
| 373 | std::vector<std::string> members; |
| 374 | if (team->members) |
| 375 | { |
| 376 | for (int i = 0; i < MAX_TEAMSIZE; i++) |
| 377 | members.push_back(account_get_name(team->members[i])); |
| 378 | } |
| 379 | o_team["members"] = join(members.begin(), members.end(), std::string(",")); |
| 380 | |
| 381 | return o_team; |
| 382 | } |
| 383 | |
| 384 | extern std::map<std::string, std::string> get_friend_object(t_friend * f) |
| 385 | { |
| 386 | std::map<std::string, std::string> o_friend; |
| 387 | |
| 388 | if (!f) |
| 389 | return o_friend; |
| 390 | |
| 391 | o_friend = get_account_object(f->friendacc); |
| 392 | return o_friend; |
| 393 | } |
| 394 | |
| 395 | |
| 396 | /* Join two vector objects to string by delimeter */ |
| 397 | template <class T, class A> |
| 398 | T join(const A &begin, const A &end, const T &t) |
| 399 | { |
| 400 | T result; |
| 401 | for (A it = begin; |
| 402 | it != end; |
| 403 | it++) |
| 404 | { |
| 405 | if (!result.empty()) |
| 406 | result.append(t); |
| 407 | result.append(*it); |
| 408 | } |
| 409 | return result; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | #endif |