Example #1
0
CString CDir::CheckPathPrefix(const CString& sPath, const CString& sAdd, const CString& sHomeDir) {
	CString sPrefix = sPath.Replace_n("//", "/").TrimRight_n("/") + "/";
	CString sAbsolutePath = ChangeDir(sPrefix, sAdd, sHomeDir);

	if (!sAbsolutePath.StartsWith(sPrefix))
		return "";
	return sAbsolutePath;
}
Example #2
0
set<CChan*> CClient::MatchChans(const CString& sPatterns) const {
    VCString vsPatterns;
    sPatterns.Replace_n(",", " ")
        .Split(" ", vsPatterns, false, "", "", true, true);

    set<CChan*> sChans;
    for (const CString& sPattern : vsPatterns) {
        vector<CChan*> vChans = m_pNetwork->FindChans(sPattern);
        sChans.insert(vChans.begin(), vChans.end());
    }
    return sChans;
}
CString CDir::CheckPathPrefix(const CString& sPath, const CString& sAdd, const CString& sHomeDir) {
#ifdef _WIN32
	const CString sPrefix = ChangeDir(sPath, "./", sHomeDir);
	const CString sAbsolutePath = ChangeDir(sPath, sAdd, sHomeDir);
#else
	CString sPrefix = sPath.Replace_n("//", "/").TrimRight_n("/") + "/";
	CString sAbsolutePath = ChangeDir(sPrefix, sAdd, sHomeDir);
#endif

	if (sAbsolutePath.Left(sPrefix.length()) != sPrefix)
		return "";
	return sAbsolutePath;
}
Example #4
0
File: log.cpp Project: sorbits/znc
void CLogMod::PutLog(const CString& sLine, const CString& sWindow /*= "Status"*/)
{
	CString sPath;
	time_t curtime;
	tm* timeinfo;
	char buffer[1024];

	time(&curtime);
	// Don't forget the user's timezone offset
	setenv("TZ", m_pUser->GetTimezone().c_str(), 1);
	timeinfo = localtime(&curtime);

	// Generate file name
	if (!strftime(buffer, sizeof(buffer), m_sLogPath.c_str(), timeinfo))
	{
		DEBUG("Could not format log path [" << sPath << "]");
		return;
	}
	sPath = buffer;

	// $WINDOW has to be handled last, since it can contain %
	sPath.Replace("$NETWORK", (m_pNetwork ? m_pNetwork->GetName() : "znc"));
	sPath.Replace("$WINDOW", sWindow.Replace_n("/", "?"));
	sPath.Replace("$USER", (m_pUser ? m_pUser->GetUserName() : "UNKNOWN"));

	// Check if it's allowed to write in this specific path
	sPath = CDir::CheckPathPrefix(GetSavePath(), sPath);
	if (sPath.empty())
	{
		DEBUG("Invalid log path ["<<m_sLogPath<<"].");
		return;
	}

	CFile LogFile(sPath);
	CString sLogDir = LogFile.GetDir();
	if (!CFile::Exists(sLogDir)) CDir::MakeDir(sLogDir);
	if (LogFile.Open(O_WRONLY | O_APPEND | O_CREAT))
	{
		snprintf(buffer, sizeof(buffer), "[%02d:%02d:%02d] ",
				timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);

		LogFile.Write(buffer + sLine + "\n");
	} else
		DEBUG("Could not open log file [" << sPath << "]: " << strerror(errno));
}
void CFile::SetFileName(const CString& sLongName) {
#ifdef _WIN32
	// :TODO: Convert filenames to Unicode. See Issue 1.
	m_sLongName = sLongName.Replace_n("/", "\\");
	m_sLongName.TrimRight("\\");

	m_sShortName = PathFindFileName(m_sLongName.c_str());
#else
	m_sLongName = sLongName;

	m_sShortName = sLongName;
	m_sShortName.TrimRight("/");

	CString::size_type uPos = m_sShortName.rfind('/');
	if (uPos != CString::npos) {
		m_sShortName = m_sShortName.substr(uPos +1);
	}
#endif
}
Example #6
0
File: log.cpp Project: kylef/znc
void CLogMod::PutLog(const CString& sLine, const CString& sWindow /*= "Status"*/)
{
    CString sPath;
    time_t curtime;

    time(&curtime);
    // Generate file name
    sPath = CUtils::FormatTime(curtime, m_sLogPath, m_pUser->GetTimezone());
    if (sPath.empty())
    {
        DEBUG("Could not format log path [" << sPath << "]");
        return;
    }

    // $WINDOW has to be handled last, since it can contain %
    sPath.Replace("$NETWORK", (m_pNetwork ? m_pNetwork->GetName() : "znc"));
    sPath.Replace("$WINDOW", sWindow.Replace_n("/", "?"));
    sPath.Replace("$USER", (m_pUser ? m_pUser->GetUserName() : "UNKNOWN"));

    // Check if it's allowed to write in this specific path
    sPath = CDir::CheckPathPrefix(GetSavePath(), sPath);
    if (sPath.empty())
    {
        DEBUG("Invalid log path ["<<m_sLogPath<<"].");
        return;
    }

    CFile LogFile(sPath);
    CString sLogDir = LogFile.GetDir();
    if (!CFile::Exists(sLogDir)) CDir::MakeDir(sLogDir);
    if (LogFile.Open(O_WRONLY | O_APPEND | O_CREAT))
    {
        LogFile.Write(CUtils::FormatTime(curtime, "[%H:%M:%S] ", m_pUser->GetTimezone()) + sLine + "\n");
    } else
        DEBUG("Could not open log file [" << sPath << "]: " << strerror(errno));
}