<commit_before>/*
 * Copyright (C) 2004-2015 ZNC, see the NOTICE file for details.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <znc/IRCSock.h>
#include <znc/Chan.h>
#include <znc/User.h>
#include <znc/IRCNetwork.h>
#include <znc/Server.h>
#include <znc/Query.h>

using std::set;
using std::vector;
using std::map;

#define IRCSOCKMODULECALL(macFUNC, macEXITER) NETWORKMODULECALL(macFUNC, m_pNetwork->GetUser(), m_pNetwork, nullptr, macEXITER)
// These are used in OnGeneralCTCP()
const time_t CIRCSock::m_uCTCPFloodTime = 5;
const unsigned int CIRCSock::m_uCTCPFloodCount = 5;

// It will be bad if user sets it to 0.00000000000001
// If you want no flood protection, set network's flood rate to -1
// TODO move this constant to CIRCNetwork?
static const double FLOOD_MINIMAL_RATE = 0.3;

class CIRCFloodTimer : public CCron {
		CIRCSock* m_pSock;
	public:
		CIRCFloodTimer(CIRCSock* pSock) : m_pSock(pSock) {
			StartMaxCycles(m_pSock->m_fFloodRate, 0);
		}
		CIRCFloodTimer(const CIRCFloodTimer&) = delete;
		CIRCFloodTimer& operator=(const CIRCFloodTimer&) = delete;
		void RunJob() override {
			if (m_pSock->m_iSendsAllowed < m_pSock->m_uFloodBurst) {
				m_pSock->m_iSendsAllowed++;
			}
			m_pSock->TrySend();
		}
};

bool CIRCSock::IsFloodProtected(double fRate) {
	return fRate > FLOOD_MINIMAL_RATE;
}

CIRCSock::CIRCSock(CIRCNetwork* pNetwork)
		: CIRCSocket(),
		  m_bAuthed(false),
		  m_bNamesx(false),
		  m_bUHNames(false),
		  m_sPerms("*!@%+"),
		  m_sPermModes("qaohv"),
		  m_scUserModes(),
		  m_mueChanModes(),
		  m_pNetwork(pNetwork),
		  m_Nick(),
		  m_sPass(""),
		  m_msChans(),
		  m_uMaxNickLen(9),
		  m_uCapPaused(0),
		  m_ssAcceptedCaps(),
		  m_ssPendingCaps(),
		  m_lastCTCP(0),
		  m_uNumCTCP(0),
		  m_mISupport(),
		  m_vsSendQueue(),
		  m_iSendsAllowed(pNetwork->GetFloodBurst()),
		  m_uFloodBurst(pNetwork->GetFloodBurst()),
		  m_fFloodRate(pNetwork->GetFloodRate()),
		  m_bFloodProtection(IsFloodProtected(pNetwork->GetFloodRate()))
{
	EnableReadLine();
	m_Nick.SetIdent(m_pNetwork->GetIdent());
	m_Nick.SetHost(m_pNetwork->GetBindHost());
	SetEncoding(m_pNetwork->GetEncoding());

	m_mueChanModes['b'] = ListArg;
	m_mueChanModes['e'] = ListArg;
	m_mueChanModes['I'] = ListArg;
	m_mueChanModes['k'] = HasArg;
	m_mueChanModes['l'] = ArgWhenSet;
	m_mueChanModes['p'] = NoArg;
	m_mueChanModes['s'] = NoArg;
	m_mueChanModes['t'] = NoArg;
	m_mueChanModes['i'] = NoArg;
	m_mueChanModes['n'] = NoArg;

	pNetwork->SetIRCSocket(this);

	// RFC says a line can have 512 chars max, but we don't care ;)
	SetMaxBufferThreshold(1024);
	if (m_bFloodProtection) {
		AddCron(new CIRCFloodTimer(this));
	}<commit_msg>Bump accepted line length to 2048<commit_after>/*
 * Copyright (C) 2004-2015 ZNC, see the NOTICE file for details.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <znc/IRCSock.h>
#include <znc/Chan.h>
#include <znc/User.h>
#include <znc/IRCNetwork.h>
#include <znc/Server.h>
#include <znc/Query.h>

using std::set;
using std::vector;
using std::map;

#define IRCSOCKMODULECALL(macFUNC, macEXITER) NETWORKMODULECALL(macFUNC, m_pNetwork->GetUser(), m_pNetwork, nullptr, macEXITER)
// These are used in OnGeneralCTCP()
const time_t CIRCSock::m_uCTCPFloodTime = 5;
const unsigned int CIRCSock::m_uCTCPFloodCount = 5;

// It will be bad if user sets it to 0.00000000000001
// If you want no flood protection, set network's flood rate to -1
// TODO move this constant to CIRCNetwork?
static const double FLOOD_MINIMAL_RATE = 0.3;

class CIRCFloodTimer : public CCron {
		CIRCSock* m_pSock;
	public:
		CIRCFloodTimer(CIRCSock* pSock) : m_pSock(pSock) {
			StartMaxCycles(m_pSock->m_fFloodRate, 0);
		}
		CIRCFloodTimer(const CIRCFloodTimer&) = delete;
		CIRCFloodTimer& operator=(const CIRCFloodTimer&) = delete;
		void RunJob() override {
			if (m_pSock->m_iSendsAllowed < m_pSock->m_uFloodBurst) {
				m_pSock->m_iSendsAllowed++;
			}
			m_pSock->TrySend();
		}
};

bool CIRCSock::IsFloodProtected(double fRate) {
	return fRate > FLOOD_MINIMAL_RATE;
}

CIRCSock::CIRCSock(CIRCNetwork* pNetwork)
		: CIRCSocket(),
		  m_bAuthed(false),
		  m_bNamesx(false),
		  m_bUHNames(false),
		  m_sPerms("*!@%+"),
		  m_sPermModes("qaohv"),
		  m_scUserModes(),
		  m_mueChanModes(),
		  m_pNetwork(pNetwork),
		  m_Nick(),
		  m_sPass(""),
		  m_msChans(),
		  m_uMaxNickLen(9),
		  m_uCapPaused(0),
		  m_ssAcceptedCaps(),
		  m_ssPendingCaps(),
		  m_lastCTCP(0),
		  m_uNumCTCP(0),
		  m_mISupport(),
		  m_vsSendQueue(),
		  m_iSendsAllowed(pNetwork->GetFloodBurst()),
		  m_uFloodBurst(pNetwork->GetFloodBurst()),
		  m_fFloodRate(pNetwork->GetFloodRate()),
		  m_bFloodProtection(IsFloodProtected(pNetwork->GetFloodRate()))
{
	EnableReadLine();
	m_Nick.SetIdent(m_pNetwork->GetIdent());
	m_Nick.SetHost(m_pNetwork->GetBindHost());
	SetEncoding(m_pNetwork->GetEncoding());

	m_mueChanModes['b'] = ListArg;
	m_mueChanModes['e'] = ListArg;
	m_mueChanModes['I'] = ListArg;
	m_mueChanModes['k'] = HasArg;
	m_mueChanModes['l'] = ArgWhenSet;
	m_mueChanModes['p'] = NoArg;
	m_mueChanModes['s'] = NoArg;
	m_mueChanModes['t'] = NoArg;
	m_mueChanModes['i'] = NoArg;
	m_mueChanModes['n'] = NoArg;

	pNetwork->SetIRCSocket(this);

	// RFC says a line can have 512 chars max + 512 chars for message tags, but we don't care ;)
	SetMaxBufferThreshold(2048);
	if (m_bFloodProtection) {
		AddCron(new CIRCFloodTimer(this));
	}
{'fraction_non_alphanumeric': 0.12771611692981086, 'fraction_numerical': 0.014850711270908238, 'mean_word_length': 4.774368231046932, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2706366', 'n_tokens_mistral': 2763, 'n_tokens_neox': 2451, 'n_words': 538}