/**
 * Get all variables of the current user from Wordpress and add it to $UMC_USER
 */
function umc_wp_get_vars()
{
    XMPP_ERROR_trace(__FUNCTION__, func_get_args());
    global $UMC_USERS, $UMC_USER, $UMC_ENV, $user_email, $user_login;
    if ($UMC_ENV !== 'wordpress') {
        XMPP_ERROR_trigger("Tried to get wordpress vars, but environment did not match: " . var_export($UMC_ENV, true));
        die('umc_wp_get_vars');
    }
    get_currentuserinfo();
    if (!isset($user_login) || $user_login == '' || $user_email == '') {
        $UMC_USER = false;
    } else {
        if (!function_exists('umc_get_uuid_level')) {
            XMPP_ERROR_send_msg("Could not get uuid_level, Env = {$UMC_ENV}");
            require_once '/home/minecraft/server/bin/core_include.php';
        }
        $uuid = umc_wp_get_uuid_for_currentuser();
        $UMC_USER['ip'] = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_FLAG_IPV4);
        if (!$uuid) {
            // we have a guest who is trying to register
            $UMC_USER['username'] = $user_login;
            $UMC_USER['email'] = $user_email;
            $uuid = umc_user2uuid($user_login);
            $UMC_USER['uuid'] = $uuid;
            $UMC_USER['userlevel'] = 'Guest';
        } else {
            // there is a logged-in user
            umc_uuid_check_usernamechange($uuid);
            $UMC_USER['email'] = $user_email;
            $UMC_USER['username'] = umc_uuid_getone($uuid, 'username');
            $UMC_USER['uuid'] = $uuid;
            $UMC_USER['userlevel'] = umc_get_uuid_level($uuid);
            if (strstr($UMC_USER['userlevel'], 'DonatorPlus')) {
                $UMC_USER['donator'] = 'DonatorPlus';
            } else {
                if (strstr($UMC_USER['userlevel'], 'Donator')) {
                    $UMC_USER['donator'] = 'Donator';
                } else {
                    $UMC_USER['donator'] = false;
                }
            }
        }
        // if we did not get any UUID
        if (!$uuid) {
            $UMC_USER['username'] = $user_login;
            $UMC_USER['uuid'] = false;
            $UMC_USER['userlevel'] = 'Guest';
        }
    }
    //$UMC_USERS[$uuid] = new UMC_User($uuid);
    //$UMC_USERS[$uuid]->set_username($username);
    //$UMC_USERS[$uuid]->set_userlevel($userlevel);
}
function umc_ws_eventhandler($event)
{
    global $WS_INIT, $UMC_USER;
    XMPP_ERROR_trace(__FUNCTION__, func_get_args());
    $player = $UMC_USER['username'];
    // iterate all plugins
    foreach ($WS_INIT as $data) {
        // check if there is a setting for the current event
        if ($data['events'] != false && isset($data['events'][$event])) {
            // execute function
            $function = $data['events'][$event];
            if (!is_string($function)) {
                XMPP_ERROR_trigger("plugin eventhandler failed event {$event}");
            }
            // execute the function
            $function();
        }
    }
    // non-plugin events
    switch ($event) {
        case 'PlayerQuitEvent':
            umc_log('system', 'logout', "{$player} logged out");
            umc_uuid_record_usertimes('lastlogout');
            break;
        case 'PlayerJoinEvent':
            umc_uuid_check_usernamechange($UMC_USER['uuid']);
            umc_donation_level($player);
            umc_promote_citizen($player, false);
            umc_log('system', 'login', "{$player} logged in");
            umc_uuid_record_usertimes('lastlogin');
            // check if the user has a skin stored, if not, get it
            umc_usericon_get($UMC_USER['uuid'], false);
            break;
        case 'PlayerPreLoginEvent':
            // nothing needed since the fact that websend is called makes it register the UUID already
            break;
        default:
            // all the events not covered above
            // XMPP_ERROR_send_msg("Event $event not assigned to action (umc_ws_eventhandler)");
    }
}