| 1 | /* |
| 2 | * Copyright (C) 1998 Mark Baysinger ([email protected]) |
| 3 | * Copyright (C) 1998,1999,2000 Ross Combs ([email protected]) |
| 4 | * Copyright (C) 2000,2001 Marco Ziech ([email protected]) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | #define CHANNEL_INTERNAL_ACCESS |
| 21 | #include "common/setup_before.h" |
| 22 | #include "channel.h" |
| 23 | |
| 24 | #include <cstring> |
| 25 | #include <cerrno> |
| 26 | #include <cstdlib> |
| 27 | |
| 28 | #include "compat/strdup.h" |
| 29 | #include "compat/strcasecmp.h" |
| 30 | #include "common/eventlog.h" |
| 31 | #include "common/list.h" |
| 32 | #include "common/util.h" |
| 33 | #include "common/token.h" |
| 34 | #include "common/tag.h" |
| 35 | #include "common/xalloc.h" |
| 36 | |
| 37 | #include "connection.h" |
| 38 | #include "message.h" |
| 39 | #include "account.h" |
| 40 | #include "account_wrap.h" |
| 41 | #include "prefs.h" |
| 42 | #include "irc.h" |
| 43 | #include "i18n.h" |
| 44 | #include "common/setup_after.h" |
| 45 | |
| 46 | #ifdef WITH_LUA |
| 47 | #include "luainterface.h" |
| 48 | #endif |
| 49 | |
| 50 | namespace pvpgn |
| 51 | { |
| 52 | |
| 53 | namespace bnetd |
| 54 | { |
| 55 | |
| 56 | static t_list * channellist_head = NULL; |
| 57 | |
| 58 | static t_channelmember * memberlist_curr = NULL; |
| 59 | static int totalcount = 0; |
| 60 | |
| 61 | |
| 62 | static int channellist_load_permanent(char const * filename); |
| 63 | static t_channel * channellist_find_channel_by_fullname(char const * name); |
| 64 | static char * channel_format_name(char const * sname, char const * country, char const * realmname, unsigned int id); |
| 65 | |
| 66 | extern int channel_set_userflags(t_connection * c); |
| 67 | |
| 68 | extern t_channel * channel_create(char const * fullname, char const * shortname, t_clienttag clienttag, int permflag, int botflag, int operflag, int logflag, char const * country, char const * realmname, int maxmembers, int moderated, int clanflag, int autoname, t_list * channellist) |
| 69 | { |
| 70 | t_channel * channel; |
| 71 | |
| 72 | if (!fullname) |
| 73 | { |
| 74 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL fullname"); |
| 75 | return NULL; |
| 76 | } |
| 77 | if (fullname[0] == '\0') |
| 78 | { |
| 79 | eventlog(eventlog_level_error, __FUNCTION__, "got empty fullname"); |
| 80 | return NULL; |
| 81 | } |
| 82 | if (shortname && shortname[0] == '\0') |
| 83 | { |
| 84 | eventlog(eventlog_level_error, __FUNCTION__, "got empty shortname"); |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | /* non-permanent already checks for this in conn_set_channel */ |
| 89 | if (permflag) |
| 90 | { |
| 91 | if ((channel = channellist_find_channel_by_fullname(fullname))) |
| 92 | { |
| 93 | if ((channel_get_clienttag(channel)) && (clienttag) && (channel_get_clienttag(channel) == clienttag)) |
| 94 | { |
| 95 | eventlog(eventlog_level_error, __FUNCTION__, "could not create duplicate permanent channel (fullname \"{}\")", fullname); |
| 96 | return NULL; |
| 97 | } |
| 98 | else if (((channel->flags & channel_flags_allowbots) != (botflag ? channel_flags_allowbots : 0)) || |
| 99 | ((channel->flags & channel_flags_allowopers) != (operflag ? channel_flags_allowopers : 0)) || |
| 100 | (channel->maxmembers != maxmembers) || |
| 101 | ((channel->flags & channel_flags_moderated) != (moderated ? channel_flags_moderated : 0)) || |
| 102 | (channel->logname && logflag == 0) || (!(channel->logname) && logflag == 1)) |
| 103 | { |
| 104 | eventlog(eventlog_level_error, __FUNCTION__, "channel parameters do not match for \"{}\" and \"{}\"", fullname, channel->name); |
| 105 | return NULL; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | channel = (t_channel*)xmalloc(sizeof(t_channel)); |
| 111 | |
| 112 | if (permflag) |
| 113 | { |
| 114 | channel->flags = channel_flags_public; |
| 115 | if (clienttag && maxmembers != -1) /* approximation.. we want things like "Starcraft USA-1" */ |
| 116 | channel->flags |= channel_flags_system; |
| 117 | } |
| 118 | else |
| 119 | channel->flags = channel_flags_none; |
| 120 | |
| 121 | if (moderated) |
| 122 | channel->flags |= channel_flags_moderated; |
| 123 | |
| 124 | if (shortname && (!strcasecmp(shortname, CHANNEL_NAME_KICKED) |
| 125 | || !strcasecmp(shortname, CHANNEL_NAME_BANNED))) |
| 126 | channel->flags |= channel_flags_thevoid; |
| 127 | |
| 128 | eventlog(eventlog_level_debug, __FUNCTION__, "creating new channel \"{}\" shortname={}{}{} clienttag={}{}{} country={}{}{} realm={}{}{}", fullname, |
| 129 | shortname ? "\"" : "(", /* all this is doing is printing the name in quotes else "none" in parens */ |
| 130 | shortname ? shortname : "none", |
| 131 | shortname ? "\"" : ")", |
| 132 | clienttag ? "\"" : "(", |
| 133 | clienttag ? clienttag_uint_to_str(clienttag) : "none", |
| 134 | clienttag ? "\"" : ")", |
| 135 | country ? "\"" : "(", |
| 136 | country ? country : "none", |
| 137 | country ? "\"" : ")", |
| 138 | realmname ? "\"" : "(", |
| 139 | realmname ? realmname : "none", |
| 140 | realmname ? "\"" : ")"); |
| 141 | |
| 142 | |
| 143 | channel->name = xstrdup(fullname); |
| 144 | |
| 145 | if (!shortname) |
| 146 | channel->shortname = NULL; |
| 147 | else |
| 148 | channel->shortname = xstrdup(shortname); |
| 149 | |
| 150 | channel->clienttag = clienttag; |
| 151 | |
| 152 | if (country) |
| 153 | channel->country = xstrdup(country); |
| 154 | else |
| 155 | channel->country = NULL; |
| 156 | |
| 157 | if (realmname) |
| 158 | channel->realmname = xstrdup(realmname); |
| 159 | else |
| 160 | channel->realmname = NULL; |
| 161 | |
| 162 | channel->banlist = list_create(); |
| 163 | |
| 164 | totalcount++; |
| 165 | if (totalcount == 0) /* if we wrap (yeah right), don't use id 0 */ |
| 166 | totalcount = 1; |
| 167 | channel->id = totalcount; |
| 168 | channel->maxmembers = maxmembers; |
| 169 | channel->currmembers = 0; |
| 170 | channel->memberlist = NULL; |
| 171 | |
| 172 | if (permflag) channel->flags |= channel_flags_permanent; |
| 173 | if (botflag) channel->flags |= channel_flags_allowbots; |
| 174 | if (operflag) channel->flags |= channel_flags_allowopers; |
| 175 | if (clanflag) channel->flags |= channel_flags_clan; |
| 176 | if (autoname) channel->flags |= channel_flags_autoname; |
| 177 | |
| 178 | if (logflag) |
| 179 | { |
| 180 | std::time_t now; |
| 181 | struct std::tm * tmnow; |
| 182 | char dstr[64]; |
| 183 | char timetemp[CHANLOG_TIME_MAXLEN]; |
| 184 | |
| 185 | now = std::time(NULL); |
| 186 | |
| 187 | if (!(tmnow = std::localtime(&now))) |
| 188 | dstr[0] = '\0'; |
| 189 | else |
| 190 | std::sprintf(dstr, "%04d%02d%02d%02d%02d%02d", |
| 191 | 1900 + tmnow->tm_year, |
| 192 | tmnow->tm_mon + 1, |
| 193 | tmnow->tm_mday, |
| 194 | tmnow->tm_hour, |
| 195 | tmnow->tm_min, |
| 196 | tmnow->tm_sec); |
| 197 | |
| 198 | channel->logname = (char*)xmalloc(std::strlen(prefs_get_chanlogdir()) + 9 + std::strlen(dstr) + 1 + 6 + 1); /* dir + "/chanlog-" + dstr + "-" + id + NUL */ |
| 199 | std::sprintf(channel->logname, "%s/chanlog-%s-%06u", prefs_get_chanlogdir(), dstr, channel->id); |
| 200 | |
| 201 | if (!(channel->log = std::fopen(channel->logname, "w"))) |
| 202 | eventlog(eventlog_level_error, __FUNCTION__, "could not open channel log \"{}\" for writing (std::fopen: {})", channel->logname, std::strerror(errno)); |
| 203 | else |
| 204 | { |
| 205 | std::fprintf(channel->log, "name=\"%s\"\n", channel->name); |
| 206 | if (channel->shortname) |
| 207 | std::fprintf(channel->log, "shortname=\"%s\"\n", channel->shortname); |
| 208 | else |
| 209 | std::fprintf(channel->log, "shortname=none\n"); |
| 210 | std::fprintf(channel->log, "permanent=\"%s\"\n", (channel->flags & channel_flags_permanent) ? "true" : "false"); |
| 211 | std::fprintf(channel->log, "allowbotse=\"%s\"\n", (channel->flags & channel_flags_allowbots) ? "true" : "false"); |
| 212 | std::fprintf(channel->log, "allowopers=\"%s\"\n", (channel->flags & channel_flags_allowopers) ? "true" : "false"); |
| 213 | if (channel->clienttag) |
| 214 | std::fprintf(channel->log, "clienttag=\"%s\"\n", clienttag_uint_to_str(channel->clienttag)); |
| 215 | else |
| 216 | std::fprintf(channel->log, "clienttag=none\n"); |
| 217 | |
| 218 | if (tmnow) |
| 219 | std::strftime(timetemp, sizeof(timetemp), CHANLOG_TIME_FORMAT, tmnow); |
| 220 | else |
| 221 | std::strcpy(timetemp, "?"); |
| 222 | std::fprintf(channel->log, "created=\"%s\"\n\n", timetemp); |
| 223 | std::fflush(channel->log); |
| 224 | } |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | channel->logname = NULL; |
| 229 | channel->log = NULL; |
| 230 | } |
| 231 | |
| 232 | channel->gameType = 0; |
| 233 | channel->gameExtension = NULL; |
| 234 | |
| 235 | if (channellist) |
| 236 | list_append_data(channellist, channel); |
| 237 | else |
| 238 | DEBUG0("channel was not added into any channellist"); |
| 239 | |
| 240 | eventlog(eventlog_level_debug, __FUNCTION__, "channel created successfully"); |
| 241 | return channel; |
| 242 | } |
| 243 | |
| 244 | extern t_channel * channel_create(char const * fullname, char const * shortname, t_clienttag clienttag, int permflag, int botflag, int operflag, int logflag, char const * country, char const * realmname, int maxmembers, int moderated, int clanflag, int autoname) |
| 245 | { |
| 246 | return channel_create(fullname, shortname, clienttag, permflag, botflag, operflag, logflag, country, realmname, maxmembers, moderated, clanflag, autoname, channellist_head); |
| 247 | } |
| 248 | |
| 249 | extern int channel_destroy(t_channel * channel, t_elem ** curr) |
| 250 | { |
| 251 | t_elem * ban; |
| 252 | |
| 253 | if (!channel) |
| 254 | { |
| 255 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | if (channel->memberlist) |
| 260 | { |
| 261 | eventlog(eventlog_level_debug, __FUNCTION__, "channel is not empty, deferring"); |
| 262 | channel->flags &= ~channel_flags_permanent; /* make it go away when the last person leaves */ |
| 263 | return -1; |
| 264 | } |
| 265 | |
| 266 | if (list_remove_data(channellist_head, channel, curr) < 0) |
| 267 | { |
| 268 | // eventlog(eventlog_level_error,__FUNCTION__,"could not remove item from list"); |
| 269 | eventlog(eventlog_level_info, __FUNCTION__, "channel was not removed from any list"); |
| 270 | // return -1; |
| 271 | } |
| 272 | |
| 273 | eventlog(eventlog_level_info, __FUNCTION__, "destroying channel \"{}\"", channel->name); |
| 274 | |
| 275 | if (channel->gameExtension) |
| 276 | xfree(channel->gameExtension); |
| 277 | |
| 278 | LIST_TRAVERSE(channel->banlist, ban) |
| 279 | { |
| 280 | char const * banned; |
| 281 | |
| 282 | if (!(banned = (char*)elem_get_data(ban))) |
| 283 | eventlog(eventlog_level_error, __FUNCTION__, "found NULL name in banlist"); |
| 284 | else |
| 285 | xfree((void *)banned); /* avoid warning */ |
| 286 | if (list_remove_elem(channel->banlist, &ban) < 0) |
| 287 | eventlog(eventlog_level_error, __FUNCTION__, "unable to remove item from list"); |
| 288 | } |
| 289 | list_destroy(channel->banlist); |
| 290 | |
| 291 | if (channel->log) |
| 292 | { |
| 293 | std::time_t now; |
| 294 | struct std::tm * tmnow; |
| 295 | char timetemp[CHANLOG_TIME_MAXLEN]; |
| 296 | |
| 297 | now = std::time(NULL); |
| 298 | if ((!(tmnow = std::localtime(&now)))) |
| 299 | std::strcpy(timetemp, "?"); |
| 300 | else |
| 301 | std::strftime(timetemp, sizeof(timetemp), CHANLOG_TIME_FORMAT, tmnow); |
| 302 | std::fprintf(channel->log, "\ndestroyed=\"%s\"\n", timetemp); |
| 303 | |
| 304 | if (std::fclose(channel->log) < 0) |
| 305 | eventlog(eventlog_level_error, __FUNCTION__, "could not close channel log \"{}\" after writing (std::fclose: {})", channel->logname, std::strerror(errno)); |
| 306 | } |
| 307 | |
| 308 | if (channel->logname) |
| 309 | xfree((void *)channel->logname); /* avoid warning */ |
| 310 | |
| 311 | if (channel->country) |
| 312 | xfree((void *)channel->country); /* avoid warning */ |
| 313 | |
| 314 | if (channel->realmname) |
| 315 | xfree((void *)channel->realmname); /* avoid warning */ |
| 316 | |
| 317 | if (channel->shortname) |
| 318 | xfree((void *)channel->shortname); /* avoid warning */ |
| 319 | |
| 320 | xfree((void *)channel->name); /* avoid warning */ |
| 321 | |
| 322 | xfree(channel); |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | |
| 328 | extern char const * channel_get_name(t_channel const * channel) |
| 329 | { |
| 330 | if (!channel) |
| 331 | { |
| 332 | eventlog(eventlog_level_warn, __FUNCTION__, "got NULL channel"); |
| 333 | return ""; |
| 334 | } |
| 335 | |
| 336 | return channel->name; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | extern t_clienttag channel_get_clienttag(t_channel const * channel) |
| 341 | { |
| 342 | if (!channel) |
| 343 | { |
| 344 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | return channel->clienttag; |
| 349 | } |
| 350 | |
| 351 | |
| 352 | extern unsigned channel_get_flags(t_channel const * channel) |
| 353 | { |
| 354 | if (!channel) |
| 355 | { |
| 356 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 357 | return channel_flags_none; |
| 358 | } |
| 359 | |
| 360 | return channel->flags; |
| 361 | } |
| 362 | |
| 363 | extern int channel_set_flags(t_channel * channel, unsigned flags) |
| 364 | { |
| 365 | if (!channel) |
| 366 | { |
| 367 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 368 | return -1; |
| 369 | } |
| 370 | channel->flags = flags; |
| 371 | |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | extern int channel_get_permanent(t_channel const * channel) |
| 376 | { |
| 377 | if (!channel) |
| 378 | { |
| 379 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | return (channel->flags & channel_flags_permanent); |
| 384 | } |
| 385 | |
| 386 | |
| 387 | extern unsigned int channel_get_channelid(t_channel const * channel) |
| 388 | { |
| 389 | if (!channel) |
| 390 | { |
| 391 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 392 | return 0; |
| 393 | } |
| 394 | return channel->id; |
| 395 | } |
| 396 | |
| 397 | |
| 398 | extern int channel_set_channelid(t_channel * channel, unsigned int channelid) |
| 399 | { |
| 400 | if (!channel) |
| 401 | { |
| 402 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 403 | return -1; |
| 404 | } |
| 405 | channel->id = channelid; |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | extern int channel_rejoin(t_connection * conn) |
| 410 | { |
| 411 | t_channel const * channel; |
| 412 | char const * temp; |
| 413 | char const * chname; |
| 414 | |
| 415 | if (!(channel = conn_get_channel(conn))) |
| 416 | return -1; |
| 417 | |
| 418 | if (!(temp = channel_get_name(channel))) |
| 419 | return -1; |
| 420 | |
| 421 | chname = xstrdup(temp); |
| 422 | conn_part_channel(conn); |
| 423 | if (conn_set_channel(conn, chname) < 0) |
| 424 | conn_set_channel(conn, CHANNEL_NAME_BANNED); |
| 425 | xfree((void *)chname); |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | |
| 430 | extern int channel_add_connection(t_channel * channel, t_connection * connection) |
| 431 | { |
| 432 | t_channelmember * member; |
| 433 | t_connection * user; |
| 434 | |
| 435 | if (!channel) |
| 436 | { |
| 437 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 438 | return -1; |
| 439 | } |
| 440 | if (!connection) |
| 441 | { |
| 442 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection"); |
| 443 | return -1; |
| 444 | } |
| 445 | |
| 446 | if (channel_check_banning(channel, connection)) |
| 447 | { |
| 448 | channel_message_log(channel, connection, 0, "JOIN FAILED (banned)"); |
| 449 | return -1; |
| 450 | } |
| 451 | |
| 452 | member = (t_channelmember*)xmalloc(sizeof(t_channelmember)); |
| 453 | member->connection = connection; |
| 454 | member->next = channel->memberlist; |
| 455 | channel->memberlist = member; |
| 456 | channel->currmembers++; |
| 457 | |
| 458 | channel_message_log(channel, connection, 0, "JOINED"); |
| 459 | |
| 460 | message_send_text(connection, message_type_channel, connection, channel_get_name(channel)); |
| 461 | |
| 462 | if ((!(channel->flags & channel_flags_permanent)) |
| 463 | && (!(channel->flags & channel_flags_thevoid)) |
| 464 | && (!(channel->flags & channel_flags_clan)) |
| 465 | && (channel->currmembers == 1) |
| 466 | && (account_is_operator_or_admin(conn_get_account(connection), channel_get_name(channel)) == 0)) |
| 467 | { |
| 468 | message_send_text(connection, message_type_info, connection, localize(connection, "you are now tempOP for this channel")); |
| 469 | conn_set_tmpOP_channel(connection, (char *)channel_get_name(channel)); |
| 470 | channel_update_userflags(connection); |
| 471 | } |
| 472 | if (!(channel_get_flags(channel) & channel_flags_thevoid)) |
| 473 | for (user = channel_get_first(channel); user; user = channel_get_next()) |
| 474 | { |
| 475 | message_send_text(connection, message_type_adduser, user, NULL); |
| 476 | /* In WOL gamechannels we send JOINGAME ack explicitely to self */ |
| 477 | if (!conn_get_game(connection)) |
| 478 | message_send_text(user, message_type_join, connection, NULL); |
| 479 | } |
| 480 | else { |
| 481 | if (!conn_get_game(connection)) |
| 482 | message_send_text(connection, message_type_join, connection, NULL); |
| 483 | } |
| 484 | |
| 485 | if (conn_is_irc_variant(connection) && (!conn_get_game(connection))) { |
| 486 | channel_set_userflags(connection); |
| 487 | message_send_text(connection, message_type_topic, connection, NULL); |
| 488 | message_send_text(connection, message_type_namreply, connection, NULL); |
| 489 | } |
| 490 | |
| 491 | /* please don't remove this notice */ |
| 492 | if (channel->log) |
| 493 | message_send_text(connection, message_type_info, connection, prefs_get_log_notice()); |
| 494 | |
| 495 | #ifdef WITH_LUA |
| 496 | lua_handle_channel(channel, connection, NULL, message_type_null, luaevent_channel_userjoin); |
| 497 | #endif |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | |
| 503 | extern int channel_del_connection(t_channel * channel, t_connection * connection, t_message_type type, char const * text) |
| 504 | { |
| 505 | t_channelmember * curr; |
| 506 | t_channelmember * temp; |
| 507 | t_elem * curr2; |
| 508 | |
| 509 | if (!channel) |
| 510 | { |
| 511 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 512 | return -1; |
| 513 | } |
| 514 | if (!connection) |
| 515 | { |
| 516 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection"); |
| 517 | return -1; |
| 518 | } |
| 519 | |
| 520 | channel_message_send(channel, type, connection, text); |
| 521 | channel_message_log(channel, connection, 0, "PARTED"); |
| 522 | |
| 523 | curr = channel->memberlist; |
| 524 | if (curr->connection == connection) |
| 525 | { |
| 526 | channel->memberlist = channel->memberlist->next; |
| 527 | xfree(curr); |
| 528 | } |
| 529 | else |
| 530 | { |
| 531 | while (curr->next && curr->next->connection != connection) |
| 532 | curr = curr->next; |
| 533 | |
| 534 | if (curr->next) |
| 535 | { |
| 536 | temp = curr->next; |
| 537 | curr->next = curr->next->next; |
| 538 | xfree(temp); |
| 539 | } |
| 540 | else |
| 541 | { |
| 542 | eventlog(eventlog_level_error, __FUNCTION__, "[{}] connection not in channel member list", conn_get_socket(connection)); |
| 543 | return -1; |
| 544 | } |
| 545 | } |
| 546 | channel->currmembers--; |
| 547 | |
| 548 | if (conn_get_tmpOP_channel(connection) && |
| 549 | std::strcmp(conn_get_tmpOP_channel(connection), channel_get_name(channel)) == 0) |
| 550 | { |
| 551 | conn_set_tmpOP_channel(connection, NULL); |
| 552 | } |
| 553 | |
| 554 | #ifdef WITH_LUA |
| 555 | lua_handle_channel(channel, connection, text, type, luaevent_channel_userleft); |
| 556 | #endif |
| 557 | |
| 558 | if (!channel->memberlist && !(channel->flags & channel_flags_permanent)) /* if channel is empty, delete it unless it's a permanent channel */ |
| 559 | { |
| 560 | channel_destroy(channel, &curr2); |
| 561 | } |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | |
| 567 | extern void channel_update_latency(t_connection * me) |
| 568 | { |
| 569 | t_channel * channel; |
| 570 | t_message * message; |
| 571 | t_connection * c; |
| 572 | |
| 573 | if (!me) |
| 574 | { |
| 575 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection"); |
| 576 | return; |
| 577 | } |
| 578 | if (!(channel = conn_get_channel(me))) |
| 579 | { |
| 580 | eventlog(eventlog_level_error, __FUNCTION__, "connection has no channel"); |
| 581 | return; |
| 582 | } |
| 583 | |
| 584 | if (!(message = message_create(message_type_userflags, me, NULL))) /* handles NULL text */ |
| 585 | return; |
| 586 | |
| 587 | for (c = channel_get_first(channel); c; c = channel_get_next()) |
| 588 | if (conn_get_class(c) == conn_class_bnet) |
| 589 | message_send(message, c); |
| 590 | message_destroy(message); |
| 591 | } |
| 592 | |
| 593 | |
| 594 | extern void channel_update_userflags(t_connection * me) |
| 595 | { |
| 596 | t_channel * channel; |
| 597 | t_message * message; |
| 598 | t_connection * c; |
| 599 | |
| 600 | if (!me) |
| 601 | { |
| 602 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection"); |
| 603 | return; |
| 604 | } |
| 605 | if (!(channel = conn_get_channel(me))) |
| 606 | { |
| 607 | eventlog(eventlog_level_error, __FUNCTION__, "connection has no channel"); |
| 608 | return; |
| 609 | } |
| 610 | |
| 611 | if (!(message = message_create(message_type_userflags, me, NULL))) /* handles NULL text */ |
| 612 | return; |
| 613 | |
| 614 | for (c = channel_get_first(channel); c; c = channel_get_next()) |
| 615 | message_send(message, c); |
| 616 | |
| 617 | message_destroy(message); |
| 618 | } |
| 619 | |
| 620 | |
| 621 | extern void channel_message_log(t_channel const * channel, t_connection * me, int fromuser, char const * text) |
| 622 | { |
| 623 | if (!channel) |
| 624 | { |
| 625 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 626 | return; |
| 627 | } |
| 628 | if (!me) |
| 629 | { |
| 630 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection"); |
| 631 | return; |
| 632 | } |
| 633 | |
| 634 | if (channel->log) |
| 635 | { |
| 636 | std::time_t now; |
| 637 | struct std::tm * tmnow; |
| 638 | char timetemp[CHANLOG_TIME_MAXLEN]; |
| 639 | |
| 640 | now = std::time(NULL); |
| 641 | if ((!(tmnow = std::localtime(&now)))) |
| 642 | std::strcpy(timetemp, "?"); |
| 643 | else |
| 644 | std::strftime(timetemp, sizeof(timetemp), CHANLOGLINE_TIME_FORMAT, tmnow); |
| 645 | |
| 646 | if (fromuser) |
| 647 | std::fprintf(channel->log, "%s: \"%s\" \"%s\"\n", timetemp, conn_get_username(me), text); |
| 648 | else |
| 649 | std::fprintf(channel->log, "%s: \"%s\" %s\n", timetemp, conn_get_username(me), text); |
| 650 | std::fflush(channel->log); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | |
| 655 | extern void channel_message_send(t_channel const * channel, t_message_type type, t_connection * me, char const * text) |
| 656 | { |
| 657 | t_connection * c; |
| 658 | unsigned int heard; |
| 659 | char const * tname; |
| 660 | t_message * message1; //send to people with clienttag matching channel clienttag |
| 661 | // or everyone when channel has no clienttag set |
| 662 | t_message * message2; //send to people with clienttag not matching channel clienttag |
| 663 | t_message * message_to_send; |
| 664 | t_account * acc; |
| 665 | |
| 666 | |
| 667 | if (!channel) |
| 668 | { |
| 669 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 670 | return; |
| 671 | } |
| 672 | if (!me) |
| 673 | { |
| 674 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection"); |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | acc = conn_get_account(me); |
| 679 | |
| 680 | if (channel_get_flags(channel) & channel_flags_thevoid) // no talking in the void |
| 681 | if (type != message_type_join && type != message_type_part) |
| 682 | return; |
| 683 | |
| 684 | // if user is muted |
| 685 | if (account_get_auth_mute(acc) == 1) |
| 686 | { |
| 687 | std::string msgtemp; |
| 688 | msgtemp = localize(me, "You can't talk on the channel. Your account has been muted"); |
| 689 | msgtemp += account_get_mutetext(me, acc, false); |
| 690 | message_send_text(me, message_type_error, me, msgtemp); |
| 691 | return; |
| 692 | } |
| 693 | |
| 694 | if (channel_get_flags(channel) & channel_flags_moderated) // moderated channel - only admins,OPs and voices may talk |
| 695 | { |
| 696 | if (type == message_type_talk || type == message_type_emote) |
| 697 | { |
| 698 | if (!((account_is_operator_or_admin(acc, channel_get_name(channel))) || |
| 699 | (channel_conn_has_tmpVOICE(channel, me)) || (account_get_auth_voice(acc, channel_get_name(channel)) == 1))) |
| 700 | { |
| 701 | message_send_text(me, message_type_error, me, localize(me, "This channel is moderated")); |
| 702 | return; |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | if (!channel->clienttag){ |
| 708 | if (!(message1 = message_create(type, me, text))) |
| 709 | { |
| 710 | eventlog(eventlog_level_error, __FUNCTION__, "could not create message1"); |
| 711 | return; |
| 712 | } |
| 713 | message2 = NULL; |
| 714 | } |
| 715 | else { |
| 716 | if (!(message1 = message_create(type, me, text))) |
| 717 | { |
| 718 | eventlog(eventlog_level_error, __FUNCTION__, "could not create message1"); |
| 719 | return; |
| 720 | } |
| 721 | if (!(message2 = message_create(type, me, text))) |
| 722 | { |
| 723 | eventlog(eventlog_level_error, __FUNCTION__, "could not create message2"); |
| 724 | message_destroy(message1); |
| 725 | return; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | |
| 730 | heard = 0; |
| 731 | tname = conn_get_chatname(me); |
| 732 | for (c = channel_get_first(channel); c; c = channel_get_next()) |
| 733 | { |
| 734 | if (c == me && (type == message_type_talk || type == message_type_gameopt_talk)) |
| 735 | continue; /* ignore ourself */ |
| 736 | if (c == me && (!conn_is_irc_variant(c)) && type == message_type_part) |
| 737 | continue; /* only on irc we need to inform ourself about leaving the channel */ |
| 738 | if (c != me && (!conn_is_irc_variant(c)) && (channel_get_flags(channel) & channel_flags_thevoid) && (type == message_type_join || type == message_type_part)) |
| 739 | continue; /* make sure we even get join part information about self in The Void */ |
| 740 | if ((type == message_type_talk || type == message_type_whisper || type == message_type_emote || type == message_type_broadcast) && |
| 741 | conn_check_ignoring(c, tname) == 1) |
| 742 | continue; /* ignore squelched players */ |
| 743 | |
| 744 | if (!channel->clienttag || channel->clienttag == conn_get_clienttag(c)) { |
| 745 | message_to_send = message1; |
| 746 | } |
| 747 | else { |
| 748 | message_to_send = message2; |
| 749 | } |
| 750 | |
| 751 | if (message_send(message_to_send, c) == 0 && c != me) |
| 752 | heard = 1; |
| 753 | } |
| 754 | |
| 755 | conn_unget_chatname(me, tname); |
| 756 | |
| 757 | message_destroy(message1); |
| 758 | if (message2) |
| 759 | message_destroy(message2); |
| 760 | |
| 761 | if ((conn_get_wol(me) == 0)) |
| 762 | { |
| 763 | if (!heard && (type == message_type_talk || type == message_type_emote)) |
| 764 | message_send_text(me, message_type_info, me, localize(me, "No one hears you.")); |
| 765 | } |
| 766 | |
| 767 | #ifdef WITH_LUA |
| 768 | if (type != message_type_part) |
| 769 | lua_handle_channel((t_channel*)channel, me, text, type, luaevent_channel_message); |
| 770 | #endif |
| 771 | } |
| 772 | |
| 773 | extern int channel_ban_user(t_channel * channel, char const * user) |
| 774 | { |
| 775 | t_elem const * curr; |
| 776 | char * temp; |
| 777 | |
| 778 | if (!channel) |
| 779 | { |
| 780 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 781 | return -1; |
| 782 | } |
| 783 | if (!user) |
| 784 | { |
| 785 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL user"); |
| 786 | return -1; |
| 787 | } |
| 788 | if (!channel->name) |
| 789 | { |
| 790 | eventlog(eventlog_level_error, __FUNCTION__, "got channel with NULL name"); |
| 791 | return -1; |
| 792 | } |
| 793 | |
| 794 | if (strcasecmp(channel->name, CHANNEL_NAME_BANNED) == 0 || |
| 795 | strcasecmp(channel->name, CHANNEL_NAME_KICKED) == 0) |
| 796 | return -1; |
| 797 | |
| 798 | LIST_TRAVERSE_CONST(channel->banlist, curr) |
| 799 | if (strcasecmp((char*)elem_get_data(curr), user) == 0) |
| 800 | return 0; |
| 801 | |
| 802 | temp = xstrdup(user); |
| 803 | list_append_data(channel->banlist, temp); |
| 804 | |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | |
| 809 | extern int channel_unban_user(t_channel * channel, char const * user) |
| 810 | { |
| 811 | t_elem * curr; |
| 812 | |
| 813 | if (!channel) |
| 814 | { |
| 815 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 816 | return -1; |
| 817 | } |
| 818 | if (!user) |
| 819 | { |
| 820 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL user"); |
| 821 | return -1; |
| 822 | } |
| 823 | |
| 824 | LIST_TRAVERSE(channel->banlist, curr) |
| 825 | { |
| 826 | char const * banned; |
| 827 | |
| 828 | if (!(banned = (char*)elem_get_data(curr))) |
| 829 | { |
| 830 | eventlog(eventlog_level_error, __FUNCTION__, "found NULL name in banlist"); |
| 831 | continue; |
| 832 | } |
| 833 | if (strcasecmp(banned, user) == 0) |
| 834 | { |
| 835 | if (list_remove_elem(channel->banlist, &curr) < 0) |
| 836 | { |
| 837 | eventlog(eventlog_level_error, __FUNCTION__, "unable to remove item from list"); |
| 838 | return -1; |
| 839 | } |
| 840 | xfree((void *)banned); /* avoid warning */ |
| 841 | return 0; |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | return -1; |
| 846 | } |
| 847 | |
| 848 | |
| 849 | extern int channel_check_banning(t_channel const * channel, t_connection const * user) |
| 850 | { |
| 851 | t_elem const * curr; |
| 852 | |
| 853 | if (!channel) |
| 854 | { |
| 855 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 856 | return -1; |
| 857 | } |
| 858 | if (!user) |
| 859 | { |
| 860 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL user"); |
| 861 | return -1; |
| 862 | } |
| 863 | |
| 864 | if (!(channel->flags & channel_flags_allowbots) && conn_get_class(user) == conn_class_bot) |
| 865 | return 1; |
| 866 | |
| 867 | LIST_TRAVERSE_CONST(channel->banlist, curr) |
| 868 | if (conn_match(user, (char*)elem_get_data(curr)) == 1) |
| 869 | return 1; |
| 870 | |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | |
| 875 | extern int channel_get_length(t_channel const * channel) |
| 876 | { |
| 877 | t_channelmember const * curr; |
| 878 | int count; |
| 879 | |
| 880 | for (curr = channel->memberlist, count = 0; curr; curr = curr->next, count++); |
| 881 | |
| 882 | return count; |
| 883 | } |
| 884 | |
| 885 | |
| 886 | extern t_connection * channel_get_first(t_channel const * channel) |
| 887 | { |
| 888 | if (!channel) |
| 889 | { |
| 890 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 891 | return NULL; |
| 892 | } |
| 893 | |
| 894 | memberlist_curr = channel->memberlist; |
| 895 | |
| 896 | return channel_get_next(); |
| 897 | } |
| 898 | |
| 899 | extern t_connection * channel_get_next(void) |
| 900 | { |
| 901 | |
| 902 | t_channelmember * member; |
| 903 | |
| 904 | if (memberlist_curr) |
| 905 | { |
| 906 | member = memberlist_curr; |
| 907 | memberlist_curr = memberlist_curr->next; |
| 908 | |
| 909 | return member->connection; |
| 910 | } |
| 911 | return NULL; |
| 912 | } |
| 913 | |
| 914 | |
| 915 | extern t_list * channel_get_banlist(t_channel const * channel) |
| 916 | { |
| 917 | if (!channel) |
| 918 | { |
| 919 | eventlog(eventlog_level_warn, __FUNCTION__, "got NULL channel"); |
| 920 | return NULL; |
| 921 | } |
| 922 | |
| 923 | return channel->banlist; |
| 924 | } |
| 925 | |
| 926 | |
| 927 | extern char const * channel_get_shortname(t_channel const * channel) |
| 928 | { |
| 929 | if (!channel) |
| 930 | { |
| 931 | eventlog(eventlog_level_warn, __FUNCTION__, "got NULL channel"); |
| 932 | return NULL; |
| 933 | } |
| 934 | |
| 935 | return channel->shortname; |
| 936 | } |
| 937 | |
| 938 | |
| 939 | static int channellist_load_permanent(char const * filename) |
| 940 | { |
| 941 | std::FILE * fp; |
| 942 | unsigned int line; |
| 943 | unsigned int pos; |
| 944 | int botflag; |
| 945 | int operflag; |
| 946 | int logflag; |
| 947 | unsigned int modflag; |
| 948 | char * buff; |
| 949 | char * name; |
| 950 | char * sname; |
| 951 | char * tag; |
| 952 | t_clienttag clienttag; |
| 953 | char * bot; |
| 954 | char * oper; |
| 955 | char * log; |
| 956 | char * country; |
| 957 | char * max; |
| 958 | char * moderated; |
| 959 | char * newname; |
| 960 | char * realmname; |
| 961 | |
| 962 | if (!filename) |
| 963 | { |
| 964 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL filename"); |
| 965 | return -1; |
| 966 | } |
| 967 | |
| 968 | if (!(fp = std::fopen(filename, "r"))) |
| 969 | { |
| 970 | eventlog(eventlog_level_error, __FUNCTION__, "could not open channel file \"{}\" for reading (std::fopen: {})", filename, std::strerror(errno)); |
| 971 | return -1; |
| 972 | } |
| 973 | |
| 974 | for (line = 1; (buff = file_get_line(fp)); line++) |
| 975 | { |
| 976 | if (buff[0] == '#' || buff[0] == '\0') |
| 977 | { |
| 978 | continue; |
| 979 | } |
| 980 | pos = 0; |
| 981 | if (!(name = next_token(buff, &pos))) |
| 982 | { |
| 983 | eventlog(eventlog_level_error, __FUNCTION__, "missing name in line {} in file \"{}\"", line, filename); |
| 984 | continue; |
| 985 | } |
| 986 | if (!(sname = next_token(buff, &pos))) |
| 987 | { |
| 988 | eventlog(eventlog_level_error, __FUNCTION__, "missing sname in line {} in file \"{}\"", line, filename); |
| 989 | continue; |
| 990 | } |
| 991 | if (!(tag = next_token(buff, &pos))) |
| 992 | { |
| 993 | eventlog(eventlog_level_error, __FUNCTION__, "missing tag in line {} in file \"{}\"", line, filename); |
| 994 | continue; |
| 995 | } |
| 996 | if (!(bot = next_token(buff, &pos))) |
| 997 | { |
| 998 | eventlog(eventlog_level_error, __FUNCTION__, "missing bot in line {} in file \"{}\"", line, filename); |
| 999 | continue; |
| 1000 | } |
| 1001 | if (!(oper = next_token(buff, &pos))) |
| 1002 | { |
| 1003 | eventlog(eventlog_level_error, __FUNCTION__, "missing oper in line {} in file \"{}\"", line, filename); |
| 1004 | continue; |
| 1005 | } |
| 1006 | if (!(log = next_token(buff, &pos))) |
| 1007 | { |
| 1008 | eventlog(eventlog_level_error, __FUNCTION__, "missing log in line {} in file \"{}\"", line, filename); |
| 1009 | continue; |
| 1010 | } |
| 1011 | if (!(country = next_token(buff, &pos))) |
| 1012 | { |
| 1013 | eventlog(eventlog_level_error, __FUNCTION__, "missing country in line {} in file \"{}\"", line, filename); |
| 1014 | continue; |
| 1015 | } |
| 1016 | if (!(realmname = next_token(buff, &pos))) |
| 1017 | { |
| 1018 | eventlog(eventlog_level_error, __FUNCTION__, "missing realmname in line {} in file \"{}\"", line, filename); |
| 1019 | continue; |
| 1020 | } |
| 1021 | if (!(max = next_token(buff, &pos))) |
| 1022 | { |
| 1023 | eventlog(eventlog_level_error, __FUNCTION__, "missing max in line {} in file \"{}\"", line, filename); |
| 1024 | continue; |
| 1025 | } |
| 1026 | if (!(moderated = next_token(buff, &pos))) |
| 1027 | { |
| 1028 | eventlog(eventlog_level_error, __FUNCTION__, "missing mod in line {} in file \"{}\"", line, filename); |
| 1029 | continue; |
| 1030 | } |
| 1031 | |
| 1032 | switch (str_get_bool(bot)) |
| 1033 | { |
| 1034 | case 1: |
| 1035 | botflag = 1; |
| 1036 | break; |
| 1037 | case 0: |
| 1038 | botflag = 0; |
| 1039 | break; |
| 1040 | default: |
| 1041 | eventlog(eventlog_level_error, __FUNCTION__, "invalid boolean value \"{}\" for field 4 on line {} in file \"{}\"", bot, line, filename); |
| 1042 | continue; |
| 1043 | } |
| 1044 | |
| 1045 | switch (str_get_bool(oper)) |
| 1046 | { |
| 1047 | case 1: |
| 1048 | operflag = 1; |
| 1049 | break; |
| 1050 | case 0: |
| 1051 | operflag = 0; |
| 1052 | break; |
| 1053 | default: |
| 1054 | eventlog(eventlog_level_error, __FUNCTION__, "invalid boolean value \"{}\" for field 5 on line {} in file \"{}\"", oper, line, filename); |
| 1055 | continue; |
| 1056 | } |
| 1057 | |
| 1058 | switch (str_get_bool(log)) |
| 1059 | { |
| 1060 | case 1: |
| 1061 | logflag = 1; |
| 1062 | break; |
| 1063 | case 0: |
| 1064 | logflag = 0; |
| 1065 | break; |
| 1066 | default: |
| 1067 | eventlog(eventlog_level_error, __FUNCTION__, "invalid boolean value \"{}\" for field 5 on line {} in file \"{}\"", log, line, filename); |
| 1068 | continue; |
| 1069 | } |
| 1070 | |
| 1071 | switch (str_get_bool(moderated)) |
| 1072 | { |
| 1073 | case 1: |
| 1074 | modflag = 1; |
| 1075 | break; |
| 1076 | case 0: |
| 1077 | modflag = 0; |
| 1078 | break; |
| 1079 | default: |
| 1080 | eventlog(eventlog_level_error, __FUNCTION__, "invalid boolean value \"{}\" for field 10 on line {} in file \"{}\"", moderated, line, filename); |
| 1081 | continue; |
| 1082 | } |
| 1083 | |
| 1084 | if (std::strcmp(sname, "NULL") == 0) |
| 1085 | sname = NULL; |
| 1086 | if (std::strcmp(tag, "NULL") == 0) |
| 1087 | clienttag = 0; |
| 1088 | else |
| 1089 | clienttag = clienttag_str_to_uint(tag); |
| 1090 | if (std::strcmp(name, "NONE") == 0) |
| 1091 | name = NULL; |
| 1092 | if (std::strcmp(country, "NULL") == 0) |
| 1093 | country = NULL; |
| 1094 | if (std::strcmp(realmname, "NULL") == 0) |
| 1095 | realmname = NULL; |
| 1096 | |
| 1097 | if (name) |
| 1098 | { |
| 1099 | channel_create(name, sname, clienttag, 1, botflag, operflag, logflag, country, realmname, std::atoi(max), modflag, 0, 0); |
| 1100 | } |
| 1101 | else |
| 1102 | { |
| 1103 | newname = channel_format_name(sname, country, realmname, 1); |
| 1104 | if (newname) |
| 1105 | { |
| 1106 | channel_create(newname, sname, clienttag, 1, botflag, operflag, logflag, country, realmname, std::atoi(max), modflag, 0, 1); |
| 1107 | xfree(newname); |
| 1108 | } |
| 1109 | else |
| 1110 | { |
| 1111 | eventlog(eventlog_level_error, __FUNCTION__, "cannot format channel name"); |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | /* FIXME: call channel_delete() on current perm channels and do a |
| 1116 | channellist_find_channel() and set the long name, perm flag, etc, |
| 1117 | otherwise call channel_create(). This will make HUPing the server |
| 1118 | handle re-reading this file correctly. */ |
| 1119 | } |
| 1120 | |
| 1121 | file_get_line(NULL); // clear file_get_line buffer |
| 1122 | if (std::fclose(fp) < 0) |
| 1123 | eventlog(eventlog_level_error, __FUNCTION__, "could not close channel file \"{}\" after reading (std::fclose: {})", filename, std::strerror(errno)); |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | static char * channel_format_name(char const * sname, char const * country, char const * realmname, unsigned int id) |
| 1128 | { |
| 1129 | char * fullname; |
| 1130 | unsigned int len; |
| 1131 | |
| 1132 | if (!sname) |
| 1133 | { |
| 1134 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL sname"); |
| 1135 | return NULL; |
| 1136 | } |
| 1137 | len = std::strlen(sname) + 1; /* FIXME: check lengths and format */ |
| 1138 | if (country) |
| 1139 | len = len + std::strlen(country) + 1; |
| 1140 | if (realmname) |
| 1141 | len = len + std::strlen(realmname) + 1; |
| 1142 | len = len + 32 + 1; |
| 1143 | |
| 1144 | fullname = (char*)xmalloc(len); |
| 1145 | std::sprintf(fullname, "%s%s%s%s%s-%u", |
| 1146 | realmname ? realmname : "", |
| 1147 | realmname ? " " : "", |
| 1148 | sname, |
| 1149 | country ? " " : "", |
| 1150 | country ? country : "", |
| 1151 | id); |
| 1152 | return fullname; |
| 1153 | } |
| 1154 | |
| 1155 | extern int channellist_reload(void) |
| 1156 | { |
| 1157 | t_elem * curr; |
| 1158 | t_channel * channel, *old_channel; |
| 1159 | t_channelmember * memberlist, *member, *old_member; |
| 1160 | t_list * channellist_old; |
| 1161 | |
| 1162 | if (channellist_head) |
| 1163 | { |
| 1164 | |
| 1165 | channellist_old = list_create(); |
| 1166 | |
| 1167 | /* First pass - get members */ |
| 1168 | LIST_TRAVERSE(channellist_head, curr) |
| 1169 | { |
| 1170 | if (!(channel = (t_channel*)elem_get_data(curr))) |
| 1171 | { |
| 1172 | eventlog(eventlog_level_error, __FUNCTION__, "channel list contains NULL item"); |
| 1173 | continue; |
| 1174 | } |
| 1175 | /* Trick to avoid automatic channel destruction */ |
| 1176 | channel->flags |= channel_flags_permanent; |
| 1177 | if (channel->memberlist) |
| 1178 | { |
| 1179 | /* we need only channel name and memberlist */ |
| 1180 | |
| 1181 | old_channel = (t_channel *)xmalloc(sizeof(t_channel)); |
| 1182 | old_channel->shortname = xstrdup(channel->shortname); |
| 1183 | old_channel->memberlist = NULL; |
| 1184 | member = channel->memberlist; |
| 1185 | |
| 1186 | /* First pass */ |
| 1187 | while (member) |
| 1188 | { |
| 1189 | old_member = (t_channelmember*)xmalloc(sizeof(t_channelmember)); |
| 1190 | old_member->connection = member->connection; |
| 1191 | |
| 1192 | if (old_channel->memberlist) |
| 1193 | old_member->next = old_channel->memberlist; |
| 1194 | else |
| 1195 | old_member->next = NULL; |
| 1196 | |
| 1197 | old_channel->memberlist = old_member; |
| 1198 | member = member->next; |
| 1199 | } |
| 1200 | |
| 1201 | /* Second pass - remove connections from channel */ |
| 1202 | member = old_channel->memberlist; |
| 1203 | while (member) |
| 1204 | { |
| 1205 | channel_del_connection(channel, member->connection, message_type_quit, NULL); |
| 1206 | conn_set_channel_var(member->connection, NULL); |
| 1207 | member = member->next; |
| 1208 | } |
| 1209 | |
| 1210 | list_prepend_data(channellist_old, old_channel); |
| 1211 | } |
| 1212 | |
| 1213 | /* Channel is empty - Destroying it */ |
| 1214 | channel->flags &= ~channel_flags_permanent; |
| 1215 | if (channel_destroy(channel, &curr) < 0) |
| 1216 | eventlog(eventlog_level_error, __FUNCTION__, "could not destroy channel"); |
| 1217 | |
| 1218 | } |
| 1219 | |
| 1220 | /* Cleanup and reload */ |
| 1221 | |
| 1222 | if (list_destroy(channellist_head) < 0) |
| 1223 | return -1; |
| 1224 | |
| 1225 | channellist_head = NULL; |
| 1226 | channellist_create(); |
| 1227 | |
| 1228 | /* Now put all users on their previous channel */ |
| 1229 | |
| 1230 | LIST_TRAVERSE(channellist_old, curr) |
| 1231 | { |
| 1232 | if (!(channel = (t_channel*)elem_get_data(curr))) |
| 1233 | { |
| 1234 | eventlog(eventlog_level_error, __FUNCTION__, "old channel list contains NULL item"); |
| 1235 | continue; |
| 1236 | } |
| 1237 | |
| 1238 | memberlist = channel->memberlist; |
| 1239 | while (memberlist) |
| 1240 | { |
| 1241 | member = memberlist; |
| 1242 | memberlist = memberlist->next; |
| 1243 | conn_set_channel(member->connection, channel->shortname); |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | |
| 1248 | /* Ross don't blame me for this but this way the code is cleaner */ |
| 1249 | |
| 1250 | LIST_TRAVERSE(channellist_old, curr) |
| 1251 | { |
| 1252 | if (!(channel = (t_channel*)elem_get_data(curr))) |
| 1253 | { |
| 1254 | eventlog(eventlog_level_error, __FUNCTION__, "old channel list contains NULL item"); |
| 1255 | continue; |
| 1256 | } |
| 1257 | |
| 1258 | memberlist = channel->memberlist; |
| 1259 | while (memberlist) |
| 1260 | { |
| 1261 | member = memberlist; |
| 1262 | memberlist = memberlist->next; |
| 1263 | xfree((void*)member); |
| 1264 | } |
| 1265 | |
| 1266 | if (channel->shortname) |
| 1267 | xfree((void*)channel->shortname); |
| 1268 | |
| 1269 | if (list_remove_data(channellist_old, channel, &curr) < 0) |
| 1270 | eventlog(eventlog_level_error, __FUNCTION__, "could not remove item from list"); |
| 1271 | xfree((void*)channel); |
| 1272 | |
| 1273 | } |
| 1274 | |
| 1275 | if (list_destroy(channellist_old) < 0) |
| 1276 | return -1; |
| 1277 | } |
| 1278 | return 0; |
| 1279 | |
| 1280 | } |
| 1281 | |
| 1282 | extern int channellist_create(void) |
| 1283 | { |
| 1284 | channellist_head = list_create(); |
| 1285 | |
| 1286 | return channellist_load_permanent(prefs_get_channelfile()); |
| 1287 | } |
| 1288 | |
| 1289 | |
| 1290 | extern int channellist_destroy(void) |
| 1291 | { |
| 1292 | t_channel * channel; |
| 1293 | t_elem * curr; |
| 1294 | |
| 1295 | if (channellist_head) |
| 1296 | { |
| 1297 | LIST_TRAVERSE(channellist_head, curr) |
| 1298 | { |
| 1299 | if (!(channel = (t_channel*)elem_get_data(curr))) /* should not happen */ |
| 1300 | { |
| 1301 | eventlog(eventlog_level_error, __FUNCTION__, "channel list contains NULL item"); |
| 1302 | continue; |
| 1303 | } |
| 1304 | |
| 1305 | channel_destroy(channel, &curr); |
| 1306 | } |
| 1307 | |
| 1308 | if (list_destroy(channellist_head) < 0) |
| 1309 | return -1; |
| 1310 | channellist_head = NULL; |
| 1311 | } |
| 1312 | |
| 1313 | return 0; |
| 1314 | } |
| 1315 | |
| 1316 | |
| 1317 | extern t_list * channellist(void) |
| 1318 | { |
| 1319 | return channellist_head; |
| 1320 | } |
| 1321 | |
| 1322 | |
| 1323 | extern int channellist_get_length(void) |
| 1324 | { |
| 1325 | return list_get_length(channellist_head); |
| 1326 | } |
| 1327 | |
| 1328 | extern int channel_get_max(t_channel const * channel) |
| 1329 | { |
| 1330 | if (!channel) |
| 1331 | { |
| 1332 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |
| 1336 | return channel->maxmembers; |
| 1337 | } |
| 1338 | |
| 1339 | extern int channel_set_max(t_channel * channel, int maxmembers) |
| 1340 | { |
| 1341 | if (!channel) |
| 1342 | { |
| 1343 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 1344 | return 0; |
| 1345 | } |
| 1346 | |
| 1347 | if (maxmembers) |
| 1348 | channel->maxmembers = maxmembers; |
| 1349 | return 1; |
| 1350 | } |
| 1351 | |
| 1352 | extern int channel_get_curr(t_channel const * channel) |
| 1353 | { |
| 1354 | if (!channel) |
| 1355 | { |
| 1356 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 1357 | return 0; |
| 1358 | } |
| 1359 | |
| 1360 | return channel->currmembers; |
| 1361 | |
| 1362 | } |
| 1363 | |
| 1364 | extern int channel_conn_is_tmpOP(t_channel const * channel, t_connection * c) |
| 1365 | { |
| 1366 | if (!channel) |
| 1367 | { |
| 1368 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 1369 | return 0; |
| 1370 | } |
| 1371 | |
| 1372 | if (!c) |
| 1373 | { |
| 1374 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 1375 | return 0; |
| 1376 | } |
| 1377 | |
| 1378 | if (!conn_get_tmpOP_channel(c)) return 0; |
| 1379 | |
| 1380 | if (std::strcmp(conn_get_tmpOP_channel(c), channel_get_name(channel)) == 0) return 1; |
| 1381 | |
| 1382 | return 0; |
| 1383 | } |
| 1384 | |
| 1385 | extern int channel_conn_has_tmpVOICE(t_channel const * channel, t_connection * c) |
| 1386 | { |
| 1387 | if (!channel) |
| 1388 | { |
| 1389 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 1390 | return 0; |
| 1391 | } |
| 1392 | |
| 1393 | if (!c) |
| 1394 | { |
| 1395 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL account"); |
| 1396 | return 0; |
| 1397 | } |
| 1398 | |
| 1399 | if (!conn_get_tmpVOICE_channel(c)) return 0; |
| 1400 | |
| 1401 | if (std::strcmp(conn_get_tmpVOICE_channel(c), channel_get_name(channel)) == 0) return 1; |
| 1402 | |
| 1403 | return 0; |
| 1404 | } |
| 1405 | |
| 1406 | static t_channel * channellist_find_channel_by_fullname(char const * name) |
| 1407 | { |
| 1408 | t_channel * channel; |
| 1409 | t_elem const * curr; |
| 1410 | |
| 1411 | if (channellist_head) |
| 1412 | { |
| 1413 | LIST_TRAVERSE(channellist_head, curr) |
| 1414 | { |
| 1415 | channel = (t_channel*)elem_get_data(curr); |
| 1416 | if (!channel->name) |
| 1417 | { |
| 1418 | eventlog(eventlog_level_error, __FUNCTION__, "found channel with NULL name"); |
| 1419 | continue; |
| 1420 | } |
| 1421 | |
| 1422 | if (strcasecmp(channel->name, name) == 0) |
| 1423 | return channel; |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | return NULL; |
| 1428 | } |
| 1429 | |
| 1430 | |
| 1431 | /* Find a channel based on the name. |
| 1432 | * Create a new channel if it is a permanent-type channel and all others |
| 1433 | * are full. |
| 1434 | */ |
| 1435 | extern t_channel * channellist_find_channel_by_name(char const * name, char const * country, char const * realmname) |
| 1436 | { |
| 1437 | t_channel * channel; |
| 1438 | t_elem const * curr; |
| 1439 | int foundperm; |
| 1440 | int foundlang; |
| 1441 | int maxchannel; /* the number of "rollover" channels that exist */ |
| 1442 | char const * saveshortname; |
| 1443 | char const * savespecialname; |
| 1444 | t_clienttag savetag; |
| 1445 | int savebotflag; |
| 1446 | int saveoperflag; |
| 1447 | int savelogflag; |
| 1448 | unsigned int savemoderated; |
| 1449 | char const * savecountry; |
| 1450 | char const * saverealmname; |
| 1451 | int savemaxmembers; |
| 1452 | t_channel * special_channel; |
| 1453 | |
| 1454 | // try to make gcc happy and initialize all variables |
| 1455 | saveshortname = savespecialname = savecountry = saverealmname = NULL; |
| 1456 | savetag = savebotflag = saveoperflag = savelogflag = savemaxmembers = savemoderated = 0; |
| 1457 | |
| 1458 | maxchannel = 0; |
| 1459 | foundperm = 0; |
| 1460 | foundlang = 0; |
| 1461 | if (channellist_head) |
| 1462 | { |
| 1463 | LIST_TRAVERSE(channellist_head, curr) |
| 1464 | { |
| 1465 | channel = (t_channel*)elem_get_data(curr); |
| 1466 | if (!channel->name) |
| 1467 | { |
| 1468 | eventlog(eventlog_level_error, __FUNCTION__, "found channel with NULL name"); |
| 1469 | continue; |
| 1470 | } |
| 1471 | |
| 1472 | if (strcasecmp(channel->name, name) == 0) |
| 1473 | { |
| 1474 | // eventlog(eventlog_level_debug,__FUNCTION__,"found exact match for \"%s\"",name); |
| 1475 | return channel; |
| 1476 | } |
| 1477 | |
| 1478 | if (channel->shortname && strcasecmp(channel->shortname, name) == 0) |
| 1479 | { |
| 1480 | special_channel = channellist_find_channel_by_name(channel->name, country, realmname); |
| 1481 | if (special_channel) channel = special_channel; |
| 1482 | |
| 1483 | /* FIXME: what should we do if the client doesn't have a country? For now, just take the first |
| 1484 | * channel that would otherwise match. */ |
| 1485 | if (((!channel->country && !foundlang) || !country || |
| 1486 | (channel->country && country && (std::strcmp(channel->country, country) == 0))) && |
| 1487 | (!channel->realmname || !realmname || !std::strcmp(channel->realmname, realmname))) |
| 1488 | { |
| 1489 | if (channel->maxmembers == -1 || channel->currmembers < channel->maxmembers) |
| 1490 | { |
| 1491 | eventlog(eventlog_level_debug, __FUNCTION__, "found permanent channel \"{}\" for \"{}\"", channel->name, name); |
| 1492 | return channel; |
| 1493 | } |
| 1494 | |
| 1495 | if (!foundlang && (channel->country)) //remember we had found a language specific channel but it was full |
| 1496 | { |
| 1497 | foundlang = 1; |
| 1498 | if (!(channel->flags & channel_flags_autoname)) |
| 1499 | savespecialname = channel->name; |
| 1500 | maxchannel = 0; |
| 1501 | } |
| 1502 | |
| 1503 | maxchannel++; |
| 1504 | } |
| 1505 | |
| 1506 | // eventlog(eventlog_level_debug,__FUNCTION__,"countries didn't match"); |
| 1507 | |
| 1508 | foundperm = 1; |
| 1509 | |
| 1510 | /* save off some info in case we need to create a new copy */ |
| 1511 | saveshortname = channel->shortname; |
| 1512 | savetag = channel->clienttag; |
| 1513 | savebotflag = channel->flags & channel_flags_allowbots; |
| 1514 | saveoperflag = channel->flags & channel_flags_allowopers; |
| 1515 | if (channel->logname) |
| 1516 | savelogflag = 1; |
| 1517 | else |
| 1518 | savelogflag = 0; |
| 1519 | if (country) |
| 1520 | savecountry = country; |
| 1521 | else |
| 1522 | savecountry = channel->country; |
| 1523 | if (realmname) |
| 1524 | saverealmname = realmname; |
| 1525 | else |
| 1526 | saverealmname = channel->realmname; |
| 1527 | savemaxmembers = channel->maxmembers; |
| 1528 | savemoderated = channel->flags & channel_flags_moderated; |
| 1529 | } |
| 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | /* we've gone thru the whole list and either there was no match or the |
| 1534 | * channels are all full. |
| 1535 | */ |
| 1536 | |
| 1537 | if (foundperm) /* All the channels were full, create a new one */ |
| 1538 | { |
| 1539 | char * channelname; |
| 1540 | |
| 1541 | if (!foundlang || !savespecialname) |
| 1542 | { |
| 1543 | if (!(channelname = channel_format_name(saveshortname, savecountry, saverealmname, maxchannel + 1))) |
| 1544 | return NULL; |
| 1545 | } |
| 1546 | else |
| 1547 | { |
| 1548 | if (!(channelname = channel_format_name(savespecialname, NULL, saverealmname, maxchannel + 1))) |
| 1549 | return NULL; |
| 1550 | } |
| 1551 | |
| 1552 | channel = channel_create(channelname, saveshortname, savetag, 1, savebotflag, saveoperflag, savelogflag, savecountry, saverealmname, savemaxmembers, savemoderated, 0, 1); |
| 1553 | xfree(channelname); |
| 1554 | |
| 1555 | eventlog(eventlog_level_debug, __FUNCTION__, "created copy \"{}\" of channel \"{}\"", (channel) ? (channel->name) : ("<failed>"), name); |
| 1556 | return channel; |
| 1557 | } |
| 1558 | |
| 1559 | /* no match */ |
| 1560 | eventlog(eventlog_level_debug, __FUNCTION__, "could not find channel \"{}\"", name); |
| 1561 | return NULL; |
| 1562 | } |
| 1563 | |
| 1564 | |
| 1565 | extern t_channel * channellist_find_channel_bychannelid(unsigned int channelid) |
| 1566 | { |
| 1567 | t_channel * channel; |
| 1568 | t_elem const * curr; |
| 1569 | |
| 1570 | if (channellist_head) |
| 1571 | { |
| 1572 | LIST_TRAVERSE(channellist_head, curr) |
| 1573 | { |
| 1574 | channel = (t_channel*)elem_get_data(curr); |
| 1575 | if (!channel->name) |
| 1576 | { |
| 1577 | eventlog(eventlog_level_error, __FUNCTION__, "found channel with NULL name"); |
| 1578 | continue; |
| 1579 | } |
| 1580 | if (channel->id == channelid) |
| 1581 | return channel; |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | return NULL; |
| 1586 | } |
| 1587 | |
| 1588 | extern int channel_set_userflags(t_connection * c) |
| 1589 | { |
| 1590 | unsigned int newflags; |
| 1591 | char const * channel; |
| 1592 | t_account * acc; |
| 1593 | |
| 1594 | if (!c) return -1; // user not connected, no need to update his flags |
| 1595 | |
| 1596 | acc = conn_get_account(c); |
| 1597 | |
| 1598 | /* well... unfortunatly channel_get_name never returns NULL but "" instead |
| 1599 | so we first have to check if user is in a channel at all */ |
| 1600 | if ((!conn_get_channel(c)) || (!(channel = channel_get_name(conn_get_channel(c))))) |
| 1601 | return -1; |
| 1602 | |
| 1603 | if (account_get_auth_admin(acc, channel) == 1 || account_get_auth_admin(acc, NULL) == 1) |
| 1604 | newflags = MF_BLIZZARD; |
| 1605 | else if (account_get_auth_operator(acc, channel) == 1 || |
| 1606 | account_get_auth_operator(acc, NULL) == 1) |
| 1607 | newflags = MF_BNET; |
| 1608 | else if (channel_conn_is_tmpOP(conn_get_channel(c), c)) |
| 1609 | newflags = MF_GAVEL; |
| 1610 | else if ((account_get_auth_voice(acc, channel) == 1) || |
| 1611 | (channel_conn_has_tmpVOICE(conn_get_channel(c), c))) |
| 1612 | newflags = MF_VOICE; |
| 1613 | else |
| 1614 | if ((conn_get_clienttag(c) == CLIENTTAG_WARCRAFT3_UINT) || |
| 1615 | (conn_get_clienttag(c) == CLIENTTAG_WAR3XP_UINT)) |
| 1616 | newflags = W3_ICON_SET; |
| 1617 | else |
| 1618 | newflags = 0; |
| 1619 | |
| 1620 | if (conn_get_flags(c) != newflags) { |
| 1621 | conn_set_flags(c, newflags); |
| 1622 | channel_update_userflags(c); |
| 1623 | } |
| 1624 | |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
| 1628 | /** |
| 1629 | * Westwood Online Extensions |
| 1630 | */ |
| 1631 | extern int channel_get_min(t_channel const * channel) |
| 1632 | { |
| 1633 | if (!channel) { |
| 1634 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 1635 | return 0; |
| 1636 | } |
| 1637 | |
| 1638 | return channel->minmembers; |
| 1639 | } |
| 1640 | |
| 1641 | extern int channel_set_min(t_channel * channel, int minmembers) |
| 1642 | { |
| 1643 | if (!channel) { |
| 1644 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL channel"); |
| 1645 | return 0; |
| 1646 | } |
| 1647 | |
| 1648 | if (minmembers) |
| 1649 | channel->minmembers = minmembers; |
| 1650 | return 1; |
| 1651 | } |
| 1652 | |
| 1653 | extern int channel_wol_get_game_type(t_channel const * channel) |
| 1654 | { |
| 1655 | if (!channel) |
| 1656 | { |
| 1657 | ERROR0("got NULL channel"); |
| 1658 | return -1; |
| 1659 | } |
| 1660 | |
| 1661 | return channel->gameType; |
| 1662 | } |
| 1663 | |
| 1664 | extern int channel_wol_set_game_type(t_channel * channel, int gameType) |
| 1665 | { |
| 1666 | if (!channel) |
| 1667 | { |
| 1668 | ERROR0("got NULL channel"); |
| 1669 | return -1; |
| 1670 | } |
| 1671 | |
| 1672 | if (gameType) |
| 1673 | channel->gameType = gameType; |
| 1674 | |
| 1675 | return 0; |
| 1676 | } |
| 1677 | |
| 1678 | extern char const * channel_wol_get_game_extension(t_channel const * channel) |
| 1679 | { |
| 1680 | if (!channel) |
| 1681 | { |
| 1682 | ERROR0("got NULL channel"); |
| 1683 | return 0; |
| 1684 | } |
| 1685 | |
| 1686 | return channel->gameExtension; |
| 1687 | } |
| 1688 | |
| 1689 | extern int channel_wol_set_game_extension(t_channel * channel, char const * gameExtension) |
| 1690 | { |
| 1691 | if (!channel) |
| 1692 | { |
| 1693 | ERROR0("got NULL channel"); |
| 1694 | return -1; |
| 1695 | } |
| 1696 | |
| 1697 | if (!gameExtension) |
| 1698 | { |
| 1699 | ERROR0("got NULL gameExtension"); |
| 1700 | return -1; |
| 1701 | } |
| 1702 | |
| 1703 | if (channel->gameExtension) |
| 1704 | xfree(channel->gameExtension); |
| 1705 | |
| 1706 | channel->gameExtension = xstrdup(gameExtension); |
| 1707 | |
| 1708 | return 0; |
| 1709 | } |
| 1710 | |
| 1711 | |
| 1712 | } |
| 1713 | |
| 1714 | } |