Example #1
0
bool CIRCNetwork::Connect() {
    if (!GetIRCConnectEnabled() || m_pIRCSock || !HasServers()) return false;

    CServer* pServer = GetNextServer();
    if (!pServer) return false;

    if (CZNC::Get().GetServerThrottle(pServer->GetName())) {
        // Can't connect right now, schedule retry later
        CZNC::Get().AddNetworkToQueue(this);
        return false;
    }

    CZNC::Get().AddServerThrottle(pServer->GetName());

    bool bSSL = pServer->IsSSL();
#ifndef HAVE_LIBSSL
    if (bSSL) {
        PutStatus("Cannot connect to [" + pServer->GetString(false) +
                  "], ZNC is not compiled with SSL.");
        CZNC::Get().AddNetworkToQueue(this);
        return false;
    }
#endif

    CIRCSock* pIRCSock = new CIRCSock(this);
    pIRCSock->SetPass(pServer->GetPass());
    pIRCSock->SetSSLTrustedPeerFingerprints(m_ssTrustedFingerprints);
    pIRCSock->SetTrustAllCerts(GetTrustAllCerts());
    pIRCSock->SetTrustPKI(GetTrustPKI());

    DEBUG("Connecting user/network [" << m_pUser->GetUserName() << "/"
                                      << m_sName << "]");

    bool bAbort = false;
    NETWORKMODULECALL(OnIRCConnecting(pIRCSock), m_pUser, this, nullptr,
                      &bAbort);
    if (bAbort) {
        DEBUG("Some module aborted the connection attempt");
        PutStatus("Some module aborted the connection attempt");
        delete pIRCSock;
        CZNC::Get().AddNetworkToQueue(this);
        return false;
    }

    CString sSockName = "IRC::" + m_pUser->GetUserName() + "::" + m_sName;
    CZNC::Get().GetManager().Connect(pServer->GetName(), pServer->GetPort(),
                                     sSockName, 120, bSSL, GetBindHost(),
                                     pIRCSock);

    return true;
}