server.c 40.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/***************************************************************************
                          server.c  -  description
                             -------------------
    begin                : 03/03/11  
    copyright            : (C) 2003 by Michael Speck
    email                : kulkanie@gmx.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

/***** INCLUDES ************************************************************/

#include <dirent.h>
#include "server.h"
#include "server_game.h"

/***** EXTERNAL VARIABLES **************************************************/

extern int net_buffer_cur_size, msg_read_pos;

/***** FORWARDED DECLARATIONS **********************************************/

#ifdef NETWORK_ENABLED
static void signal_handler( int signal );
static void broadcast_all( int len, char *data );
#endif

/***** LOCAL TYPE DEFINITIONS **********************************************/

enum { CHANNEL_MAIN_ID = 1 };

/***** LOCAL VARIABLES *****************************************************/

int server_port = 8000;    /* server is listening at this port */
42 43 44
List *channels = 0;       /* chat channels */
List *games = 0;           /* running games */
List *levelsets = 0;       /* loaded levelsets */
45
int global_id = 1;         /* global id counter increased each type an object 
46
                  (user,channel,game) is added (id:1 is channel MAIN) */
47 48
int server_halt = 0;       /* when set, server will go down after some seconds */
int server_halt_since = 0; /* global time (in secs) passed since halt command */
49 50
char errbuf[128];       /* used to compile error messages */
char msgbuf[MAX_MSG_SIZE];       /* used to compile messages */
51
int  msglen = 0; 
52 53 54
int  user_limit = 50;        /* maximum number of users that may be logged in (0: unlimited)*/
int  user_count = 0;        /* number of logged in users */
char greetings[256]; /* welcome message send to user on login */
55 56
char admin_pwd[20] = "";    /* admin password (if any) */
char datadir[128] = SRC_DIR;    /* levels are loaded from here */
57
int  server_fps = 20;          /* communication frame rate */
58 59
int  server_frame_delay = 50; /* delay between server frames */
int  server_recv_limit = -1; /* number of packets parsed in one go (-1 == unlimited) */
60
int  server_def_bot_num = 1; /* number of 800's and 1000's bots to be 
61 62 63 64 65 66 67
                                created on startup */

/* these default channels are known by the client and the only
 * ones shown in the list. there id starts at 1 for MAIN increased
 * by 1 with each step */
ServerChannel *main_channel = 0;  /* pointer to MAIN */
int default_channel_count = 1;
68
char *default_channels[1];
69 70 71 72 73 74 75

/***** LOCAL FUNCTIONS *****************************************************/

#ifdef NETWORK_ENABLED

static void server_init_halt()
{
76 77 78 79 80 81
    printf( _("server is going down...\n") );
    server_halt_since = time(0);
    server_halt = 1;
    errbuf[0] = MSG_ERROR;
    sprintf( errbuf+1, _("SERVER IS GOING DOWN!!!") );
    broadcast_all( 2+strlen(errbuf+1), errbuf );
82 83 84 85
}

void send_info( ServerUser *user, int type, char *format, ... )
{
86 87 88 89 90 91 92 93 94 95
    va_list args;

    if ( user->no_comm ) return;
    
    va_start( args, format );
    vsnprintf( msgbuf+1, MAX_MSG_SIZE-1, format, args );
    va_end( args );
    msgbuf[0] = type;
    
    socket_transmit( &user->socket, CODE_BLUE, 2+strlen(msgbuf+1), msgbuf );
96 97 98 99 100 101 102 103 104
}

/* channel_add/delete don't require client updates as the only
 * channels that are shown in the list are already known by name
 * and id by the client. additional channels can be entered by typing
 * in the name. A pointer is returned to simplify transfer of users.
 */
static ServerChannel* channel_add( char *name )
{
105
    ServerChannel *channel = salloc( 1, sizeof( ServerChannel ) );
106

107 108 109
    strncpy(channel->name,name,20);
    channel->id    = global_id++;
    channel->users = list_create( LIST_AUTO_DELETE, LIST_NO_CALLBACK );
110

111 112
    printf( _("channel added: %s\n"), channel->name );
    list_add( channels, channel );
113

114
    return channel;
115 116 117
}
static void channel_delete( void *ptr )
{
118 119 120 121 122 123 124
    ServerChannel *channel = (ServerChannel*)ptr;
    if ( channel ) {
        printf( _("channel deleted: %s (%i users)\n"), channel->name, channel->users->count );
        if ( channel->users )
            list_delete( channel->users );
        free( channel );
    }
125 126 127 128
}

ServerChannel *channel_find_by_name( char *name )
{
129 130 131 132 133 134
    ServerChannel *channel = 0;
    list_reset( channels );
    while ( ( channel = list_next( channels ) ) )
        if ( !strcmp( channel->name, name ) )
            return channel;
    return 0;
135 136 137 138 139
}

static void channel_add_user( ServerChannel *channel, ServerUser *user );
static void channel_add_bot( ServerChannel *channel, char *name, int level )
{
140 141 142 143 144
    NetAddr addr;
    ServerUser *user = salloc( 1, sizeof( ServerUser) );
    
    /* add a bot user to channel which can be challenged
     * but does nothing else */
145
        strncpy(user->name,name,16);
146
    user->id = global_id++;
147
        user->no_comm = 1;
148
    user->bot = 1;
149
        user->bot_level = level; /* playing level */
150 151 152
    net_build_addr( &addr, "localhost", 8000 );
    socket_init( &user->socket, &addr );
    channel_add_user( channel, user );
153 154 155 156
}

static void create_default_channels()
{
157 158 159 160 161
    int i = 0;
    
    for ( i = 0; i < default_channel_count; i++ )
        channel_add( default_channels[i] );
    main_channel = (ServerChannel*)list_first( channels );
162 163 164 165
}

static int is_default_channel( ServerChannel *channel )
{
166 167 168 169 170
    int i = 0;
    
    for ( i = 0; i < default_channel_count; i++ )
        if ( !strcmp( default_channels[i], channel->name ) ) return 1;
    return 0;
171 172 173 174
}

static void channel_broadcast( ServerChannel *channel, int len, char *data )
{
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    int urgent = 0;
    ServerUser *user;

    /* urgent messages are always sent even to hidden users */
    if ( data[0] == MSG_ERROR || 
         data[0] == MSG_ADD_USER || data[0] == MSG_REMOVE_USER ||
         data[0] == MSG_SET_COMM_DELAY )
        urgent = 1;
    
    /* deliver it */
    list_reset( channel->users );
    while ( ( user = list_next( channel->users ) ) )
        if ( !user->no_comm )
        if ( urgent || !user->hidden )
            socket_transmit( &user->socket, CODE_BLUE, len, data );
190 191 192 193 194
}

/* broadcast message to all users in all channels even the hidden ones */
static void broadcast_all( int len, char *data )
{
195 196 197 198 199
    ServerChannel *channel;
    
    list_reset( channels );
    while ( ( channel = list_next( channels ) ) )
        channel_broadcast( channel, len, data );
200 201 202 203
}

static void channel_add_user( ServerChannel *channel, ServerUser *user )
{
204 205 206 207 208 209 210 211 212 213 214 215 216
    if ( channel == 0 ) return;
    if ( user == 0 ) return;
    
    list_add( channel->users, user );
    printf( _("user added: %s (%i) from %s\n"),
        user->name, user->id, net_addr_to_string( &user->socket.remote_addr ) );
    
    /* inform all users in channel (including this one if not hidden) */
    msg_begin_writing( msgbuf, &msglen, 128 );
    msg_write_int8( MSG_ADD_USER );
    msg_write_int32( user->id );
    msg_write_string( user->name );
    channel_broadcast( channel, msglen, msgbuf );
217 218 219 220 221 222 223

        /* don't count dummies */
        if ( !user->bot ) user_count++;
}

void channel_remove_user( ServerChannel *channel, ServerUser *user )
{
224 225 226 227 228 229 230 231 232 233
    if ( channel == 0 ) return;
    if ( user == 0 ) return;
    
    user->hidden = 1; /* this user does not require the following update */
    
    /* inform all users in channel */
    msg_begin_writing( msgbuf, &msglen, 128 );
    msg_write_int8( MSG_REMOVE_USER );
    msg_write_int32( user->id );
    channel_broadcast( channel, msglen, msgbuf );
234 235 236

        /* don't count dummies */
        if ( !user->bot ) user_count--;
237 238 239
    /* remove */
    printf( _("user removed: %s (%i)\n"), user->name, user->id );
    list_delete_item( channel->users, user );
240

241 242 243
    /* if empty channel and not default channel delete it */
    if ( channel->users->count == 0 && !is_default_channel( channel ) )
        list_delete_item( channels, channel );
244 245 246 247
}

void channel_hide_user( ServerChannel *channel, ServerUser *user, int hide )
{
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    if ( channel == 0 ) return;
    if ( user == 0 ) return;
    if ( user->hidden == hide ) return; /* nothing changes */

    
    /* broadcast update to all users in channel */
    if ( hide ) {
        msg_begin_writing( msgbuf, &msglen, 128 );
        msg_write_int8( MSG_REMOVE_USER );
        msg_write_int32( user->id );
        channel_broadcast( channel, msglen, msgbuf );
        user->hidden = hide;
    } else {
        user->hidden = hide;
        msg_begin_writing( msgbuf, &msglen, 128 );
        msg_write_int8( MSG_ADD_USER );
        msg_write_int32( user->id );
        msg_write_string( user->name );
        channel_broadcast( channel, msglen, msgbuf );
    }
268 269 270 271
}

static void channel_kick_user( ServerChannel *channel, ServerUser *user, char *reason )
{
272 273
    if ( channel == 0 ) return;
    if ( user == 0 ) return;
274

275 276
    snprintf( errbuf, 128, _("You have been kicked! Reason: %s"), reason );
    send_info( user, MSG_ERROR, errbuf );
277

278 279 280 281 282
    errbuf[0] = MSG_DISCONNECT;
    socket_transmit( &user->socket, CODE_BLUE, 1, errbuf );
    
    printf( _("user kicked (%s): %s (%i)\n"), reason, user->name, user->id );
    channel_remove_user( channel, user );
283 284 285 286 287 288
}

/* transfer user to new channel and send nescessary updates */
void send_full_update( ServerUser *user, ServerChannel *channel );
void channel_transfer_user( ServerChannel *old, ServerChannel *new, ServerUser *user )
{
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
    /* same channel? */
    if ( old == new ) return;

    /* mute user as he will receive a complete update
       after the transfer */
    user->hidden = 1;
    
    /* transfer */
    msg_begin_writing( msgbuf, &msglen, 128 );
    msg_write_int8( MSG_REMOVE_USER );
    msg_write_int32( user->id );
    channel_broadcast( old, msglen, msgbuf );

    list_transfer( old->users, new->users, user );
    
    if ( old->users->count == 0 && !is_default_channel( old ) )
        list_delete_item( channels, old );
    
    msg_begin_writing( msgbuf, &msglen, 128 );
    msg_write_int8( MSG_ADD_USER );
    msg_write_int32( user->id );
    channel_broadcast( new, msglen, msgbuf );

    /* update */
    user->hidden = 0;
    msg_begin_writing( msgbuf, &msglen, 128 );
    msg_write_int8( MSG_SERVER_INFO );
    msg_printf( _("you have entered a new channel: %s"), new->name );
    socket_transmit( &user->socket, CODE_BLUE, msglen, msgbuf );
    msg_begin_writing( msgbuf, &msglen, 128 );
    msg_write_int8( MSG_SET_CHANNEL );
    msg_write_string( new->name );
    socket_transmit( &user->socket, CODE_BLUE, msglen, msgbuf );
    
    send_full_update( user, new );
324 325 326 327 328 329 330 331
}
            
/* Send a list of all users in user's channel including itself.
 *
 * FIXME: Sending each single entry is a not very nice.
 */
void send_full_update( ServerUser *user, ServerChannel *channel )
{
332 333 334 335 336 337 338 339 340 341 342 343 344 345
    ServerUser *u;

    msgbuf[0] = MSG_PREPARE_FULL_UPDATE;
    socket_transmit( &user->socket, CODE_BLUE, 1, msgbuf );

    /* users */
    list_reset( channel->users );
    while ( ( u = list_next( channel->users ) ) ) {
        msg_begin_writing( msgbuf, &msglen, 32 );
        msg_write_int8( MSG_ADD_USER );
        msg_write_int32( u->id );
        msg_write_string( u->name );
        socket_transmit( &user->socket, CODE_BLUE, msglen, msgbuf );
    }
346
}
347
    
348 349

/* ServerUser *find_user_by_name
350
 * IN    char    *name
351 352 353 354 355
 *
 * Search all channels for a user by that name.
 */
static ServerUser* find_user_by_name( char *name )
{
356 357 358 359 360 361 362 363 364 365 366 367
    ServerUser *user;
    ServerChannel *channel;
    
    list_reset( channels );
    while ( ( channel = list_next( channels ) ) ) {
        list_reset( channel->users );
        while ( ( user = list_next( channel->users ) ) )
            if ( !strcmp( user->name, name ) )
                return user;
    }
    
    return 0;
368 369 370
}

/* ServerUser *search_user
371
 * IN    char    *name
372 373 374 375 376 377
 *
 * Search all channels for a user by that name and return
 * the channel as well.
 */
static ServerUser* search_user( char *name, ServerChannel **channel )
{
378 379 380 381 382 383 384 385 386 387 388
    ServerUser *user;
    
    list_reset( channels );
    while ( ( *channel = list_next( channels ) ) ) {
        list_reset( (*channel)->users );
        while ( ( user = list_next( (*channel)->users ) ) )
            if ( !strcmp( user->name, name ) )
                return user;
    }
    
    return 0;
389 390 391
}

/* ServerUser *find_user_by_id
392
 * IN    int    id
393 394 395 396 397
 *
 * Search all channels for a user by that id.
 */
static ServerUser* find_user_by_id( int id )
{
398 399 400 401 402 403 404 405 406 407 408 409
    ServerUser *user;
    ServerChannel *channel;
    
    list_reset( channels );
    while ( ( channel = list_next( channels ) ) ) {
        list_reset( channel->users );
        while ( ( user = list_next( channel->users ) ) )
            if ( user->id == id )
                return user;
    }
    
    return 0;
410 411 412
}

/* ServerUser *find_user_by_addr
413
 * IN    NetAddr        *addr
414 415 416 417 418
 *
 * Search wether a user already uses this net address.
 */
static ServerUser* find_user_by_addr( NetAddr *addr )
{
419 420 421 422 423 424 425 426 427 428 429 430
    ServerUser *user;
    ServerChannel *channel;
    
    list_reset( channels );
    while ( ( channel = list_next( channels ) ) ) {
        list_reset( channel->users );
        while ( ( user = list_next( channel->users ) ) )
            if ( net_compare_addr( addr, &user->socket.remote_addr ) )
                return user;
    }
    
    return 0;
431 432 433 434 435 436 437 438 439 440 441
}

/* void handle_connectionless_packet
 *
 * By now only connection attempts can be found in this category.
 * So check wether the packet contains a valid request (or send
 * error messages if it doesn't) and add a new user to channel
 * MAIN.
 */
static void handle_connectionless_packet( void )
{
442 443 444 445 446 447
    char        name[20], pwd[20], buf[128];
    int        protocol;
    ServerUser    *user = 0;
    int         i;
    
    msg_begin_connectionless_reading();
448

449
    if ( msg_read_int8() != MSG_CONNECT ) return;
450

451
    protocol = msg_read_int8();
452 453
        strncpy(name,msg_read_string(),20); name[19] = 0;
        strncpy(pwd,msg_read_string(),20); pwd[19] = 0;
454 455 456 457
    if ( msg_read_failed() ) {
        sprintf( errbuf+1, _("Login data corrupted, please retry.") );
        goto failure;
    }
458 459
        if ( !is_alphanum(name) ) {
            strcpy(errbuf+1, 
460
                _("Your username may only contain letters, digits and underscores.\n") );
461 462
            goto failure;
        }
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
    
    /* check wether this user already exists. if so the LOGIN_OKAY
     * message was dropped. */
    if ( (user = find_user_by_addr( &net_sender_addr )) )
        if ( strcmp( user->name, name ) )
            user = 0; /* somebody else though same box */
    
    /* check data for validity */
    if ( protocol != PROTOCOL ) {
        if ( protocol < PROTOCOL )
            sprintf( errbuf+1, _("Your protocol is out of date, please update.") );
        else
            sprintf( errbuf+1, _("Server uses an older protocol (%i), sorry."),
                PROTOCOL );
        goto failure;
    }
    if ( user_count >= user_limit && user == 0/*else user exists already but wasn't informed*/ ) {
        sprintf( errbuf+1, _("Server is full!") );
        goto failure;
    }
    if ( name[0] == 0 ) {
        sprintf( errbuf+1, _("Please enter a name!") );
        goto failure;
    }
    if ( strchr( name, ' ' ) ) {
        sprintf( errbuf+1, _("Your name must not contain blanks! (But can have underscores.)") );
        goto failure;
    }
    if ( (user==0 && find_user_by_name( name )) || !strcmp( name, _("admin") ) ) {
        sprintf( errbuf+1, _("This name is already in use. Please choose another one.") );
        goto failure;
    }
    /* password is currently unused */

    /* data successfully extracted and checked. if this is not a
     * user whos LOGIN_OKAY was dropped, create a new one. */
    if ( user == 0 ) {
        user = salloc( 1, sizeof( ServerUser ) );
        user->id = global_id++;
        if ( admin_pwd[0] != 0 && !strcmp( admin_pwd, name ) ) {
            strncpy(user->name,_("admin"),20);
            user->admin = 1;
        }
        else
507
                    strncpy(user->name,name,20);
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
        socket_init( &user->socket, &net_sender_addr );
        user->hidden = 1; /* don't get the ADD_USER message */
        channel_add_user( main_channel, user );
        user->hidden = 0;
    }
    
    /* tell user that it is accepted */
    msg_begin_writing( msgbuf, &msglen, 32 );
    msg_write_int8( MSG_LOGIN_OKAY );
    msg_write_int32( user->id );
    msg_write_string( user->name );
    socket_transmit( &user->socket, CODE_BLUE, msglen, msgbuf );
    msg_begin_writing( msgbuf, &msglen, 2+strlen(greetings) );
    msg_write_int8( MSG_SERVER_INFO );
    msg_write_string( greetings );
    socket_transmit( &user->socket, CODE_BLUE, msglen, msgbuf );
524 525 526
        msg_begin_writing( msgbuf, &msglen, 128 );
        msg_write_int8( MSG_SERVER_INFO );
        if ( user_count == 1 )
527
            strcpy ( buf , _("1 user online") );
528
        else
529
            snprintf( buf, 128, _("%i users online"), user_count );
530
        msg_write_string( buf );
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
    socket_transmit( &user->socket, CODE_BLUE, msglen, msgbuf );
    msg_begin_writing( msgbuf, &msglen, 4 );
    msg_write_int8( MSG_SET_COMM_DELAY );
    msg_write_int16( server_frame_delay );
    socket_transmit( &user->socket, CODE_BLUE, msglen, msgbuf );
    
    /* send default channels */
    msg_begin_writing( msgbuf, &msglen, MAX_MSG_SIZE );
    msg_write_int8( MSG_CHANNEL_LIST );
    msg_write_int8( default_channel_count ); 
    for ( i = 0; i < default_channel_count; i++ )
        msg_write_string( default_channels[i] );
    socket_transmit( &user->socket, CODE_BLUE, msglen, msgbuf );

    /* send levelset names */
    msg_begin_writing( msgbuf, &msglen, MAX_MSG_SIZE );
    msg_write_int8( MSG_LEVELSET_LIST );
    msg_write_int8( levelsets->count );
    list_reset( levelsets );
    for ( i = 0; i < levelsets->count; i++ )
        msg_write_string( list_next( levelsets ) );
    socket_transmit( &user->socket, CODE_BLUE, msglen, msgbuf );

    send_full_update( user, main_channel );

    return;
557 558

failure:
559 560 561 562
    /* send error message as connectionless one as we have no
     * connection so far */
    errbuf[0] = MSG_ERROR;
    net_transmit_connectionless( &net_sender_addr, 2+strlen(errbuf+1), errbuf );
563 564 565
}

/* void handle_command
566 567
 * IN    ServerUser    *user
 * IN    char        *cmd_line
568 569 570 571 572 573
 *
 * Handle command send by the user. The admin has more commands available
 * the a normal user.
 */
static void handle_command( ServerUser *user, ServerChannel *channel, char *cmd_line )
{
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
    List *args = parser_explode_string( cmd_line, ' ' );
    char *cmd = list_first( args );
    char *name, *limit, *text;
    int val;
    ServerUser *target, *remote;
    ServerChannel *target_channel;
    LevelSet *lset;
    char buf[128];
    FILE *file;
    
    if ( cmd == 0 ) return;
    
    if ( !strcmp( cmd, _("search") ) ) {
        if ( (name = list_next( args )) == 0 ) {
            send_info( user, MSG_SERVER_INFO, _("search: specify a user name!") );
            return;
        }
        if ( (target = search_user( name, &target_channel )) == 0 )
            send_info( user, MSG_SERVER_INFO, _("search: user is not online.") );
        else {
            sprintf( buf, _("search: %s: in channel %s: %s"),
                target->name, target_channel->name,
                user->game?_("playing"):_("chatting") );
            send_info( user, MSG_SERVER_INFO, buf );
        }
    } else
    if ( !strcmp( cmd, _("version") ) ) {
        sprintf( buf, _("transmission protocol: %i"), PROTOCOL );
        send_info( user, MSG_SERVER_INFO, buf );
    } else
    if ( !strcmp( cmd, _("info") ) ) {
        sprintf( buf, _("user limit: %i#frame rate: %i#packet limit: %i"), 
            user_limit, server_fps, server_recv_limit );
        send_info( user, MSG_SERVER_INFO, buf );
    } else
    if ( !strcmp( cmd, _("addset") ) && user->admin ) {
        if ( (name = list_next( args )) == 0 ) {
            send_info( user, MSG_SERVER_INFO, _("addset: name missing") );
            return;
        }
        /* can find levelset? */
        if ( (file = levelset_open( name, "r" )) ) {
            fclose( file );
            lset = levelset_load( name, 0 );
            if ( lset ) {
                list_add( levelsets, lset );
                msg_begin_writing( msgbuf, &msglen, MAX_MSG_SIZE );
                msg_write_int8( MSG_ADD_LEVELSET );
                msg_write_string( name );
                if ( !msg_write_failed() )
                    broadcast_all( msglen, msgbuf );
            }
            else
                send_info( user, MSG_SERVER_INFO, _("addset: file %s corrupted"), name );
        }
        else
            send_info( user, MSG_SERVER_INFO, _("addset: file %s not found"), name );
    } else
    if ( !strcmp( cmd, _("addbot") ) && user->admin ) {
        if ( (name = list_next( args )) == 0 ) {
            send_info( user, MSG_SERVER_INFO, _("addbot: name missing") );
            return;
        }
        if ( (target = find_user_by_name( name )) ) {
            send_info( user, MSG_SERVER_INFO, _("addbot: bot '%s' exists"), name );
            return;
        }
        if ( (limit = list_next( args )) == 0 ) {
            send_info( user, MSG_SERVER_INFO, _("addbot: strength missing") );
            return;
        }
        channel_add_bot( channel, name, atoi(limit) );
        
    } else
    if ( !strcmp( cmd, _("delbot") ) && user->admin ) {
        if ( (name = list_next( args )) == 0 ) {
            send_info( user, MSG_SERVER_INFO, _("delbot: name missing") );
            return;
        }
        if ( (target = find_user_by_name( name )) == 0 ) {
            send_info( user, MSG_SERVER_INFO, _("delbot: bot '%s' not found"), name );
            return;
        }
        channel_remove_user( channel, target );
    } else
    if ( !strcmp( cmd, _("set") ) && user->admin ) {
        if ( (name = list_next( args )) == 0 ) {
            send_info( user, MSG_SERVER_INFO, _("set: variable missing") );
            return;
        }
        if ( (limit = list_next( args )) == 0 ) {
            send_info( user, MSG_SERVER_INFO, _("set: value missing") );
            return;
        }
        val = atoi( limit );
        if ( !strcmp( name, _("userlimit") ) ) {
            user_limit = val;
            sprintf( buf, _("userlimit: set to %i"), user_limit );
        }
        else
        if ( !strcmp( name, _("packetlimit") ) ) {
            server_recv_limit = val;
            sprintf( buf, _("packetlimit: set to %i"), server_recv_limit );
        }
        else
        if ( !strcmp( name, _("fps") ) ) {
            server_fps = val;
            server_frame_delay = 1000/val;
            sprintf( buf, _("fps: set to %i"), server_fps );

            msg_begin_writing( msgbuf, &msglen, MAX_MSG_SIZE );
            msg_write_int8( MSG_SET_COMM_DELAY );
            msg_write_int16( server_frame_delay );
            broadcast_all( msglen, msgbuf );
        }
        send_info( user, MSG_SERVER_INFO, buf );
    } else
    if ( !strcmp( cmd, _("kick") ) && user->admin ) {
        if ( (name = list_next( args )) == 0 ) {
            send_info( user, MSG_SERVER_INFO, _("kick: specify a user name!") );
            return;
        }
        if ( (target = search_user( name, &target_channel )) == 0 )
            send_info( user, MSG_SERVER_INFO, _("kick: user is not online.") );
        else {
            if ( target->game ) {
                /* bring em out of the game and tell the remote
                 * that the game is killed*/
                remote = ((ServerGame*)target->game)->users[0];
                if ( remote == target )
                    remote = ((ServerGame*)target->game)->users[1];
                errbuf[0] = MSG_ERROR;
                sprintf( errbuf+1, _("Sorry, but your opponent has been kicked!") );
                socket_transmit( 
                    &remote->socket, CODE_BLUE, 
                    2+strlen(errbuf+1), errbuf );
                server_game_remove( (ServerGame*)target->game ); 
            }
            msg_begin_writing( msgbuf, &msglen, MAX_MSG_SIZE );
            msg_write_int8( MSG_SERVER_INFO );
            msg_printf( _("ADMIN has kicked %s."), target->name );
            broadcast_all( msglen, msgbuf );
            channel_kick_user( target_channel, target, _("admin kick") );
        }
        
    } else
    if ( !strcmp( cmd, _("admin_says") ) && user->admin ) {
        if ( (text = list_next( args )) == 0 ) {
            send_info( user, MSG_SERVER_INFO, _("info: a message is required!") );
            return;
        }
        /* don't show just the first word */
        if ( (text = strchr( cmd_line, ' ' )) == 0 ) return; /* will never happen */
        msg_begin_writing( msgbuf, &msglen, MAX_MSG_SIZE );
        msg_write_int8( MSG_SERVER_INFO );
        msg_printf( _("ADMIN says: %s"), text+1 /*don't double the space*/ );
        broadcast_all( msglen, msgbuf );
    } else
    if ( !strcmp( cmd, _("halt") ) && user->admin ) {
        server_init_halt();
    }
    else {
        send_info( user, MSG_SERVER_INFO, _("unknown command: %s"), cmd );
    }
738 739 740
}

/* void parse_packet_channel
741 742
 * IN    ServerChannel    *channel
 * IN    ServerUser    *user
743 744 745 746 747 748 749 750 751
 *
 * Check all messages in packet from user who is located in channel and
 * not playing. The header has been successfully processed and the read 
 * pointer is at the beginning of the first message. If a message occurs
 * that is not handled, the rest of the packet is skipped as we don't
 * know its further format.
 */
static void parse_packet_channel( ServerUser *user, ServerChannel *channel )
{
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
    int id;
    unsigned char type;
    char name[16];
    ServerUser *recv;
    ServerGameCtx ctx;
    ServerChannel *newchannel;
    
    while ( 1 ) {
        type = (unsigned)msg_read_int8();
        
        if ( msg_read_failed() ) break; /* no more messages */

        switch ( type ) {
            case MSG_HEARTBEAT:
                /* updates the socket information automatically
                 * so connection is not closed */
                break;
            case MSG_DISCONNECT:
                user->no_comm = 1; /* receive no more messages */
                                printf( _("%s (%i) disconnected\n"), user->name, user->id );
                channel_remove_user( channel, user );
                break;
            case MSG_QUIT_GAME:
                /* if player looks at error message and breaks up
                 * game (e.g. waiting for stats) it will send this
                 * message which is simply ignored */
                break;
            case MSG_COMMAND:
                handle_command( user, channel, msg_read_string() );
                break;
            case MSG_UNHIDE:
                if ( user->hidden )
                    channel_hide_user( channel, user, 0 );
                break;
            case MSG_CHATTER:
                /* if UNHIDE was dropped user can become visible 
                 * again by simply chatting */
                if ( user->hidden )
                    channel_hide_user( channel, user, 0 );
                
                msg_begin_writing( msgbuf, &msglen, 128 );
                msg_write_int8( MSG_CHATTER );
                msg_printf( "<%s> %s", user->name, msg_read_string() );
                if ( !msg_write_failed() )
                    channel_broadcast( channel, msglen, msgbuf );
                break;
            case MSG_WHISPER:
                id = msg_read_int32();
                recv = find_user_by_id( id ); /* all channels */
                if ( recv == 0 ) {
                    sprintf( errbuf, _("There is no user by that name.") );
                    send_info( user, MSG_ERROR, errbuf );
                } else {
                    msg_begin_writing( msgbuf, &msglen, MAX_MSG_SIZE );
                    msg_write_int8( MSG_CHATTER );
                    msg_printf( "<%s> %s", user->name, msg_read_string() );
                    if ( !msg_write_failed() ) {
                        socket_transmit( 
                            &recv->socket, 
                            CODE_BLUE, msglen, msgbuf );
                        socket_transmit( 
                            &user->socket, 
                            CODE_BLUE, msglen, msgbuf );
                    }
                }
                break;
            case MSG_ENTER_CHANNEL:
819
                                strncpy(name,msg_read_string(),16);
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
                if ( strchr( name, ' ' ) ) {
                    sprintf( errbuf, _("Channel name must not contain blanks!") );
                    send_info( user, MSG_ERROR, errbuf );
                    break;
                }
                newchannel = channel_find_by_name( name );
                if ( newchannel == 0 ) newchannel = channel_add( name );
                channel_transfer_user( channel, newchannel, user );
                break;
            case MSG_OPEN_GAME:
                ctx.challenger = user;
                id = msg_read_int32();
                ctx.challenged = find_user_by_id( id );
                strncpy(ctx.name,msg_read_string(),20);
                ctx.name[19] = 0;
                ctx.diff = msg_read_int8();
                ctx.rounds = msg_read_int8();
                ctx.frags = msg_read_int8();
                ctx.balls = msg_read_int8();
                errbuf[0] = 0;
                if ( msg_read_failed() )
                    sprintf( errbuf, _("OpenGame message corrupted!") );
                if ( ctx.challenged == 0 )
                    sprintf( errbuf, _("User with id %i does not exist!"), id );
                if ( errbuf[0] != 0 )
                    send_info( user, MSG_ERROR, errbuf );
                else
                    server_game_add( channel, &ctx );
                break;
            default:
                printf( _("channel %i: %s: invalid message %x: skipping %i bytes\n"),
                    channel->id, 
                    net_addr_to_string( &user->socket.remote_addr), type, 
                    net_buffer_cur_size - msg_read_pos );
                msg_read_pos = net_buffer_cur_size;
                break;

        }
    }
859 860 861
}

/* void find_send_user
862
 * OUT    ServerUser    **user
863 864 865 866 867 868 869 870
 *
 * Check all channels and games for the user who's socket address equals 
 * net_sender_addr and return a pointer to it or set '*user' 0 else.
 *
 * This is a linear search and should be improved.
 */
static void find_send_user( ServerUser **user, ServerChannel **channel, ServerGame **game )
{
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
    *user = 0;    
    *channel = 0;
    *game = 0;
    
    list_reset( channels );
    while ( ( *channel = list_next( channels ) ) ) {
        list_reset( (*channel)->users );
        while ( ( *user = list_next( (*channel)->users ) ) )
            if ( net_compare_addr( &net_sender_addr, &(*user)->socket.remote_addr ) ) {
                /* we have found the user. check if it is playing a game */
                if ( (*user)->game )
                    *game = (ServerGame*)(*user)->game; /* is a void pointer */
                return;
            }
    }
886 887 888 889 890 891 892 893
}

/* void remove_zombies
 * 
 * Close overflowed connections and users that were idle for too long.
 */
static void remove_zombies( void )
{
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
    ServerUser *user, *peer;
    ServerChannel *channel;
    int    cur_time = time(0); 
    
    list_reset( channels );
    while ( ( channel = list_next( channels ) ) ) {
        list_reset( channel->users );
        while ( ( user = list_next( channel->users ) ) ) {
            if ( user->bot ) continue; /* are never removed */
            if ( user->socket.fatal_error ||
                 cur_time >= user->socket.idle_since + 60 ) {
                /* either the code red buffer overflowed or the
                 * user did not send the heartbeat: a zombie! */
                /* bring users to channel if playing */
                if ( user->game ) {
                    peer = user->player_id==0?
                           ((ServerGame*)user->game)->users[1]:
                           ((ServerGame*)user->game)->users[0];
                    send_info( peer, MSG_ERROR, 
                        _("Remote player has disconnected!") ); 
                    server_game_remove( (ServerGame*)user->game ); 
                }
                channel_kick_user( channel, user, _("zombie") );
            }
        }
    }
920 921 922
}

/* void handle
923
 * IN    int    ms    milliseconds passed since last call
924 925 926 927 928 929 930 931 932 933 934
 *
 * Receive all packets at the single UDP socket and check wether it is
 * connectionless (connection requests) or belongs to a connection (socket).
 * Find the user and parse all messages in the packet.
 *
 * Remove any zombies. (no heartbeat, buffer overflow)
 *
 * Update games.
 */
static void handle( int ms )
{
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
    int        recv_limit;
    ServerUser    *user = 0;
    ServerChannel    *channel = 0;
    ServerGame     *game = 0;
    
    recv_limit = server_recv_limit; /* limited number of packets if not -1 */
    while ( net_recv_packet() && ( recv_limit==-1 || recv_limit > 0) ) {
        /* handle connectionless packets (login requests) */
        if ( msg_is_connectionless() ) {
            handle_connectionless_packet();
            continue;
        }

        /* find the sending user and its channel by comparing 
         * net_sender_addr. */
        find_send_user( &user, &channel, &game );
        if ( user == 0 ) continue;

        /* check if this is a valid packet and update the socket */
        if ( !socket_process_header( &user->socket ) ) continue;

        /* extract the messages */
        if ( game )
            parse_packet_game( game, user );
        else
        if ( channel )
            parse_packet_channel( user, channel );
        
        if ( recv_limit != -1 ) recv_limit--;
    }

    remove_zombies();

    update_games( ms );
969 970 971 972 973
}

/* (re)load all levelsets from the datadir */
static int load_levelsets( void )
{
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
    DIR *hdir;
    struct dirent *dirent;
    
    list_clear( levelsets );

    hdir = opendir( SRC_DIR "/levels" );
    if ( hdir == 0 ) {
        printf( _("couldn't open directory %s!\n"), SRC_DIR "/levels" );
        return 0;
    }

    while ( (dirent = readdir( hdir )) ) {
        if ( dirent->d_name[0] == '.' ) continue;
        if ( dirent->d_name[0] != 'N' || dirent->d_name[1] != '_' ) continue;
        list_add( levelsets, levelset_load( dirent->d_name, 0 ) );
    }
    printf( _("loaded %i levelsets from directory %s\n"), 
        levelsets->count, SRC_DIR "/levels" );
    
    closedir( hdir );
    return 1;
995 996 997 998 999
}

/* display help of command line options */
static void display_help()
{
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
    printf( _("Usage:\n  lbreakout2server\n") );
    printf( _("    [-p <SERVER_PORT>]      Bind server to this port (Default: %i).\n"), server_port );
    printf( _("    [-l <USER_LIMIT>]       Maximum number of users that can login to server.\n") );
    printf( _("    [-m <WELCOME_FILE>]     The text in this file is send to new users on login.\n") );
    printf( _("    [-a <ADMIN_PWD>]        The user logging in as <ADMIN_PWD> will become\n") );
    printf( _("                            the administrator named 'admin'.\n") );
/*    printf( _("    [-D <DATADIR>]          In this directory the network levelsets are located.\n") );
    printf( _("                            Note: To upload levelsets as admin the directory\n") );
    printf( _("                            must be writeable.\n") );*/
    printf( _("    [-f <FRAMERATE>]        Number of send/recv handlings in a second.\n") );
    printf( _("                            (Default: 33)\n") );
        printf( _("    [-b <BOTNUM>]           Number of paddle bots with 800 and 1000 strength\n"));
        printf( _("                            each. (Default: 0)\n") );
    exit( 0 );
1014 1015 1016 1017 1018
}

/* Parse the command line. */
static void parse_args( int argc, char **argv )
{
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
    int i, len;
    FILE *file;
    
    for ( i = 0; i < argc; i++ ) {
        if ( !strcmp( "-p", argv[i] ) )
            if ( argv[i + 1] )
                server_port = atoi( argv[i + 1] );
        if ( !strcmp( "-l", argv[i] ) )
            if ( argv[i + 1] )
                user_limit = atoi( argv[i + 1] );
        if ( !strcmp( "-f", argv[i] ) )
            if ( argv[i + 1] ) {
                server_fps = atoi(argv[i + 1]);
                server_frame_delay = 1000/server_fps;
            }
        if ( !strcmp( "-D", argv[i] ) )
            if ( argv[i + 1] )
                strncpy(datadir,argv[i + 1],128);
        if ( !strcmp( "-h", argv[i] ) || !strcmp( "--help", argv[i] ) )
            display_help();
        if ( !strcmp( "-m", argv[i] ) )
            if ( argv[i + 1] ) {
                file = fopen( argv[i+1], "r" );
                if ( file == 0 )
                {
                    printf( _("greetings file not found, setting directly: %s\n"), argv[i+1] );
                    snprintf(greetings,256,"%s",argv[i+1]);
                }
                else {
                    len = fread( greetings, 1, 255, file );
                    greetings[len] = 0;
                    fclose( file );
                    printf( _("greetings loaded: %s\n"), argv[i+1] );
                }
            }
        if ( !strcmp( "-a", argv[i] ) )
            if ( argv[i + 1] )
                strncpy(admin_pwd,argv[i + 1],15);
1057 1058 1059
                if ( !strcmp( "-b", argv[i] ) )
                    if ( argv[i + 1] )
                        server_def_bot_num = atoi(argv[i + 1]);
1060
    }
1061 1062 1063 1064 1065
}

/* Initiate network connection and lists. */
static void finalize()
{
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
    /* disconnect all users */
    errbuf[0] = MSG_DISCONNECT;
    broadcast_all( 1, errbuf );
    
    /* free lists */
    if ( channels )
        list_delete( channels );
    if ( games )
        list_delete( games ); 
    if ( levelsets )
        list_delete( levelsets ); 
    
    /* close server socket */
    net_shutdown();

    /* free default channel title */    
    free(default_channels[0]);

    printf( _("server halted\n") );
1085 1086 1087 1088 1089
}
static void init( int argc, char **argv )
{
    char name[16];
    int  id = 1, j;
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129

    /* i18n */
#ifdef ENABLE_NLS
    setlocale (LC_ALL, "");
    bindtextdomain (PACKAGE, LOCALEDIR);
    textdomain (PACKAGE);
#endif
    
    /* initiate sdl timer */
    SDL_Init( SDL_INIT_TIMER );
    
    /* set signal handler to cleanly shutdown by CTRL-C */
    signal( SIGINT, signal_handler );

    /* welcome message */
    snprintf(greetings,256,_("Welcome to LBreakout2 online!"));

    /* default channel */
    default_channels[0] = strdup(_("MAIN"));
    
    /* parse command line options */
    parse_args( argc, argv );
    
    /* open single UDP socket */
    if ( !net_init( server_port ) ) exit(1);
    
    /* create empty lists */
    channels = list_create( LIST_AUTO_DELETE, channel_delete );
    games    = list_create( LIST_AUTO_DELETE, server_game_delete );
    levelsets= list_create( LIST_AUTO_DELETE, levelset_list_delete );
    if ( channels == 0 || games == 0 || levelsets == 0 ) exit(1);
    
    /* load levelset names */
    load_levelsets();

    printf( _("user limit is %i\n"), user_limit );
    printf( _("FPS: %i (delay: %i ms)\n"), 1000/server_frame_delay, server_frame_delay );
    
    /* add default channels */
    create_default_channels();
1130 1131 1132 1133

        /* add default bots */
        for ( j = 0; j < server_def_bot_num; j++,id++ )
        {
1134
            snprintf( name, 16, _("BOT%i-800"), id );
1135 1136 1137 1138
            channel_add_bot( main_channel, name, 800 );
        }
        for ( j = 0; j < server_def_bot_num; j++,id++ )
        {
1139
            snprintf( name, 16, _("BOT%i-1000"), id );
1140 1141 1142
            channel_add_bot( main_channel, name, 1000 );
        }
        
1143 1144
    /* build angle table */
    init_angles();
1145 1146 1147 1148
}

static void signal_handler( int signal )
{
1149 1150 1151 1152 1153 1154
    switch ( signal ) {
        case SIGINT:
            if ( server_halt ) break;
            server_init_halt();
            break;
    }
1155 1156 1157 1158 1159 1160 1161 1162 1163
}

#endif

/***** PUBLIC FUNCTIONS ****************************************************/

int main( int argc, char **argv )
{
#ifdef NETWORK_ENABLED
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
    int last_ticks, cur_ticks;
    int ms = 0;

    set_random_seed(); /* set random seed */
    init( argc, argv );
 
    /* loop and handle messages until shutdown */
    last_ticks = cur_ticks = SDL_GetTicks();
    while ( 1 ) {
        last_ticks = cur_ticks; cur_ticks = SDL_GetTicks(); 
        ms += cur_ticks - last_ticks;
        
        if ( ms > server_frame_delay ) {
            handle( ms );
            ms -= server_frame_delay;
        }
        
        if ( server_halt && time( 0 ) > server_halt_since + 5 )
            break;
        
        SDL_Delay( 5 );
    }
    
    finalize();
1188
#else
1189
    printf( _("LBreakout2 has been compiled without network support.\n") );
1190 1191
#endif

1192
    return 0;
1193
}