function listen($_user)
{
    global $CONFIG, $GROUPS, $INTERNAL, $USER, $INTLIST, $INTBUSY;
    $USER = $_user;
    if (!IS_FILTERED) {
        if ($USER->Browsers[0]->ChatId == 0) {
            $USER->Browsers[0]->SetChatId();
        }
        if ($USER->Browsers[0]->Status == CHAT_STATUS_OPEN) {
            initData(true, false, false, false);
            if (isset($_POST[POST_EXTERN_USER_GROUP]) && empty($USER->Browsers[0]->DesiredChatGroup)) {
                $USER->Browsers[0]->DesiredChatGroup = AJAXDecode($_POST[POST_EXTERN_USER_GROUP]);
            }
            $USER->Browsers[0]->SetCookieGroup();
            getInternal();
            if (count($INTLIST) + $INTBUSY > 0) {
                $USER->AddFunctionCall("lz_chat_set_id('" . $USER->Browsers[0]->ChatId . "');", false);
                $chatPosition = getQueuePosition($USER->UserId, $USER->Browsers[0]->DesiredChatGroup);
                $chatWaitingTime = getQueueWaitingTime($chatPosition, $INTBUSY);
                login();
                $USER->Browsers[0]->SetWaiting(!($chatPosition == 1 && count($INTLIST) > 0 && !(!empty($USER->Browsers[0]->DesiredChatPartner) && $INTERNAL[$USER->Browsers[0]->DesiredChatPartner]->Status == USER_STATUS_BUSY)));
                if (!$USER->Browsers[0]->Waiting) {
                    $USER->AddFunctionCall("lz_chat_show_connected();", false);
                    $USER->AddFunctionCall("lz_chat_set_status(lz_chat_data.STATUS_ALLOCATED);", false);
                    if ($CONFIG["gl_alloc_mode"] != ALLOCATION_MODE_ALL || !empty($USER->Browsers[0]->DesiredChatPartner)) {
                        $USER->Browsers[0]->CreateChat($INTERNAL[$USER->Browsers[0]->DesiredChatPartner], $USER);
                    } else {
                        foreach ($INTLIST as $intid => $am) {
                            $USER->Browsers[0]->CreateChat($INTERNAL[$intid], $USER);
                        }
                    }
                } else {
                    $USER->AddFunctionCall("lz_chat_show_queue_position(" . $chatPosition . "," . min($chatWaitingTime, 30) . ");", false);
                }
            }
        } else {
            activeListen();
        }
    } else {
        displayFiltered();
    }
    return $USER;
}
function listen($_user)
{
    global $CONFIG, $GROUPS, $INTERNAL, $USER, $INTLIST, $INTBUSY;
    $USER = $_user;
    if (!IS_FILTERED) {
        if (isnull($USER->Browsers[0]->Chat)) {
            getData(true, false, false, false);
            $USER->Browsers[0]->SetCookieGroup();
            if (isset($_POST[POST_EXTERN_USER_GROUP])) {
                $USER->Browsers[0]->DesiredChatGroup = utf8_decode($_POST[POST_EXTERN_USER_GROUP]);
                $USER->Browsers[0]->SetCookieGroup();
            }
            getInternal();
            $chatId = getChatId($USER, $USER->Browsers[0]->DesiredChatGroup);
            $chatPosition = getQueuePosition($chatId, $USER->Browsers[0]->DesiredChatGroup);
            $chatWaitingTime = getQueueWaitingTime($chatPosition, $INTBUSY);
            login();
            $USER->Browsers[0]->Waiting = !($chatPosition == 1 && count($INTLIST) > 0 && !(!isnull($USER->Browsers[0]->DesiredChatPartner) && $INTERNAL[$USER->Browsers[0]->DesiredChatPartner]->Status != USER_STATUS_ONLINE));
            if (!$USER->Browsers[0]->Waiting) {
                $USER->AddFunctionCall("lz_chat_show_connected();", false);
                $USER->Browsers[0]->DestroyChatFiles();
                $USER->AddFunctionCall("lz_chat_set_status(lz_chat_data.STATUS_ALLOCATED);", false);
                if ($CONFIG["gl_alloc_mode"] != ALLOCATION_MODE_ALL || !isnull($USER->Browsers[0]->DesiredChatPartner)) {
                    $USER->Browsers[0]->CreateChat($INTERNAL[$USER->Browsers[0]->DesiredChatPartner], $chatId);
                } else {
                    foreach ($INTLIST as $intid => $am) {
                        $USER->Browsers[0]->CreateChat($INTERNAL[$intid], $chatId);
                    }
                }
            } else {
                $USER->AddFunctionCall("lz_chat_show_queue_position(" . $chatPosition . "," . $chatWaitingTime . ");", false);
            }
            closeOpenChatLog($USER->Browsers[0]->DesiredChatPartner, $USER->Browsers[0]->SystemId);
        } else {
            activeListen();
        }
    } else {
        displayFiltered();
    }
    return $USER;
}
function listen($_user, $init = false)
{
    global $CONFIG, $GROUPS, $INTERNAL, $USER, $INTLIST, $INTBUSY, $VOUCHER, $DEFAULT_BROWSER_LANGUAGE;
    $USER = $_user;
    if (!IS_FILTERED) {
        if (!empty($_POST["p_tid"])) {
            $VOUCHER = VisitorChat::GetMatchingVoucher(base64UrlDecode($_POST[POST_EXTERN_USER_GROUP]), base64UrlDecode($_POST["p_tid"]));
            if ($VOUCHER != null) {
                $USER->Browsers[0]->ChatVoucherId = $VOUCHER->Id;
            }
        }
        if (empty($USER->Browsers[0]->ChatId)) {
            $result = queryDB(true, "SELECT `visit_id` FROM `" . DB_PREFIX . DATABASE_VISITOR_BROWSERS . "` WHERE `visitor_id`='" . DBManager::RealEscape($USER->Browsers[0]->UserId) . "' AND `id`='" . DBManager::RealEscape($USER->Browsers[0]->BrowserId) . "' LIMIT 1;");
            if ($result && ($row = DBManager::FetchArray($result)) && $row["visit_id"] != $USER->Browsers[0]->VisitId && !empty($USER->Browsers[0]->VisitId)) {
                $USER->Browsers[0]->CloseChat(2);
                $USER->AddFunctionCall("lz_chat_set_status(lz_chat_data.STATUS_STOPPED);", false);
                $USER->AddFunctionCall("lz_chat_add_system_text(99,'" . base64_encode("Your browser session has expired (" . $row["visit_id"] . "-" . $USER->Browsers[0]->VisitId . "). Please close this browser instance and try again.") . "');", false);
                $USER->AddFunctionCall("lz_chat_stop_system();", false);
                return $USER;
            }
            $USER->Browsers[0]->SetChatId();
            $init = true;
        }
        if ($USER->Browsers[0]->Status == CHAT_STATUS_OPEN) {
            initData(array("INTERNAL"));
            if (!empty($_POST[POST_EXTERN_USER_GROUP]) && (empty($USER->Browsers[0]->DesiredChatGroup) || $init)) {
                $USER->Browsers[0]->DesiredChatGroup = base64UrlDecode($_POST[POST_EXTERN_USER_GROUP]);
            }
            $USER->Browsers[0]->SetCookieGroup();
            if (!setOperator() && $INTBUSY == 0) {
                $USER->AddFunctionCall("lz_chat_add_system_text(8,null);", false);
                $USER->AddFunctionCall("lz_chat_stop_system();", false);
            } else {
                if (count($INTLIST) + $INTBUSY > 0) {
                    $USER->AddFunctionCall("lz_chat_set_id('" . $USER->Browsers[0]->ChatId . "');", false);
                    $chatPosition = getQueuePosition($USER->UserId, $USER->Browsers[0]->DesiredChatGroup);
                    $chatWaitingTime = getQueueWaitingTime($chatPosition, $INTBUSY);
                    login();
                    $USER->Browsers[0]->SetWaiting(!($chatPosition == 1 && count($INTLIST) > 0 && !(!empty($USER->Browsers[0]->DesiredChatPartner) && $INTERNAL[$USER->Browsers[0]->DesiredChatPartner]->Status == USER_STATUS_BUSY)));
                    if (!$USER->Browsers[0]->Waiting) {
                        $USER->AddFunctionCall("lz_chat_show_connected();", false);
                        $USER->AddFunctionCall("lz_chat_set_status(lz_chat_data.STATUS_ALLOCATED);", false);
                        if ($CONFIG["gl_alloc_mode"] != ALLOCATION_MODE_ALL || !empty($USER->Browsers[0]->DesiredChatPartner)) {
                            $USER->Browsers[0]->CreateChat($INTERNAL[$USER->Browsers[0]->DesiredChatPartner], $USER, true);
                        } else {
                            $run = 0;
                            foreach ($INTLIST as $intid => $am) {
                                $USER->Browsers[0]->CreateChat($INTERNAL[$intid], $USER, false, "", "", true, $run++ == 0);
                            }
                        }
                    } else {
                        if (!empty($_GET["acid"])) {
                            $USER->AddFunctionCall("lz_chat_show_connected();", false);
                            $pchatid = base64UrlDecode($_GET["acid"]);
                            $result = queryDB(true, "SELECT * FROM `" . DB_PREFIX . DATABASE_VISITOR_CHATS . "` WHERE `visitor_id`='" . DBManager::RealEscape($USER->Browsers[0]->UserId) . "' AND `chat_id`='" . DBManager::RealEscape($pchatid) . "' AND (`exit` > " . (time() - 30) . " OR `exit`=0) LIMIT 1;");
                            if ($result && DBManager::GetRowCount($result) == 1) {
                                $row = DBManager::FetchArray($result);
                                if (!empty($row["waiting"])) {
                                    $posts = unserialize($row["queue_posts"]);
                                    foreach ($posts as $post) {
                                        $USER->AddFunctionCall("lz_chat_repost_from_queue('" . $post[0] . "');", false);
                                    }
                                    $USER->AddFunctionCall("lz_chat_data.QueuePostsAdded = true;", false);
                                }
                            }
                        }
                        if ($USER->Browsers[0]->IsMaxWaitingTime(true)) {
                            displayDeclined();
                            return $USER;
                        }
                        $pdm = getPredefinedMessage($GROUPS[$USER->Browsers[0]->DesiredChatGroup]->PredefinedMessages, $USER != null ? $USER->Language : "");
                        if ($pdm != null && !empty($pdm->QueueMessage) && time() - $USER->Browsers[0]->FirstActive > $pdm->QueueMessageTime && !$USER->Browsers[0]->QueueMessageShown) {
                            $USER->Browsers[0]->QueueMessageShown = true;
                            if (empty($_GET["acid"])) {
                                $USER->AddFunctionCall("lz_chat_add_system_text(99,'" . base64_encode($pdm->QueueMessage) . "');", false);
                            }
                        }
                        if (empty($_GET["acid"])) {
                            $USER->AddFunctionCall("lz_chat_show_queue_position(" . $chatPosition . "," . min($chatWaitingTime, 30) . ");", false);
                        }
                        $USER->Browsers[0]->CreateArchiveEntry(null, $USER);
                    }
                }
            }
        } else {
            $action = $USER->Browsers[0]->GetMaxWaitingTimeAction(false);
            if ($action == "MESSAGE" || $action == "FORWARD" && !$USER->Browsers[0]->CreateAutoForward()) {
                $USER->Browsers[0]->InternalDecline($USER->Browsers[0]->InternalUser->SystemId);
                displayDeclined();
            } else {
                if (!$USER->Browsers[0]->ArchiveCreated && !empty($USER->Browsers[0]->DesiredChatPartner)) {
                    $USER->Browsers[0]->CreateChat($INTERNAL[$USER->Browsers[0]->DesiredChatPartner], $USER, true);
                }
                activeListen();
            }
        }
        if ($USER->Browsers[0]->Status <= CHAT_STATUS_WAITING && empty($_POST["p_wls"])) {
            $USER->AddFunctionCall("lz_chat_show_waiting_links('" . base64_encode($wl = $GROUPS[$USER->Browsers[0]->DesiredChatGroup]->GetWaitingLinks($USER->Browsers[0]->Question, $DEFAULT_BROWSER_LANGUAGE)) . "');", false);
        }
    } else {
        displayFiltered();
    }
    return $USER;
}