bool SendFile(const CString& sRemoteNick, const CString& sFileName) { CString sFullPath = CDir::ChangeDir(GetSavePath(), sFileName, CZNC::Get().GetHomePath()); CDCCSock* pSock = new CDCCSock(this, sRemoteNick, sFullPath); CFile* pFile = pSock->OpenFile(false); if (!pFile) { delete pSock; return false; } CString sLocalDCCIP = GetUser()->GetLocalDCCIP(); unsigned short uPort = CZNC::Get().GetManager().ListenRand( "DCC::LISTEN::" + sRemoteNick, sLocalDCCIP, false, SOMAXCONN, pSock, 120); if (GetUser()->GetNick().Equals(sRemoteNick)) { PutUser(":*[email protected] PRIVMSG " + sRemoteNick + " :\001DCC SEND " + pFile->GetShortName() + " " + CString(CUtils::GetLongIP(sLocalDCCIP)) + " " + CString(uPort) + " " + CString(pFile->GetSize()) + "\001"); } else { PutIRC("PRIVMSG " + sRemoteNick + " :\001DCC SEND " + pFile->GetShortName() + " " + CString(CUtils::GetLongIP(sLocalDCCIP)) + " " + CString(uPort) + " " + CString(pFile->GetSize()) + "\001"); } PutModule(t_f("Attempting to send [{1}] to [{2}].")( pFile->GetShortName(), sRemoteNick)); return true; }
CFile* CDCCSock::OpenFile(bool bWrite) { if ((m_pFile) || (m_sLocalFile.empty())) { m_pModule->PutModule(((bWrite) ? "DCC <- [" : "DCC -> [") + m_sRemoteNick + "][" + m_sLocalFile + "] - Unable to open file."); return NULL; } m_pFile = new CFile(m_sLocalFile); if (bWrite) { if (m_pFile->Exists()) { delete m_pFile; m_pFile = NULL; m_pModule->PutModule("DCC <- [" + m_sRemoteNick + "] - File already exists [" + m_sLocalFile + "]"); return NULL; } if (!m_pFile->Open(O_WRONLY | O_TRUNC | O_CREAT)) { delete m_pFile; m_pFile = NULL; m_pModule->PutModule("DCC <- [" + m_sRemoteNick + "] - Could not open file [" + m_sLocalFile + "]"); return NULL; } } else { if (!m_pFile->IsReg()) { delete m_pFile; m_pFile = NULL; m_pModule->PutModule("DCC -> [" + m_sRemoteNick + "] - Not a file [" + m_sLocalFile + "]"); return NULL; } if (!m_pFile->Open()) { delete m_pFile; m_pFile = NULL; m_pModule->PutModule("DCC -> [" + m_sRemoteNick + "] - Could not open file [" + m_sLocalFile + "]"); return NULL; } // The DCC specs only allow file transfers with files smaller // than 4GiB (see ReadData()). unsigned long long uFileSize = m_pFile->GetSize(); if (uFileSize > (unsigned long long) 0xffffffff) { delete m_pFile; m_pFile = NULL; m_pModule->PutModule("DCC -> [" + m_sRemoteNick + "] - File too large (>4 GiB) [" + m_sLocalFile + "]"); return NULL; } m_uFileSize = (unsigned long) uFileSize; } m_sFileName = m_pFile->GetShortName(); return m_pFile; }
bool CUser::SendFile(const CString& sRemoteNick, const CString& sFileName, const CString& sModuleName) { CString sFullPath = CDir::ChangeDir(GetDLPath(), sFileName, CZNC::Get().GetHomePath()); CDCCSock* pSock = new CDCCSock(this, sRemoteNick, sFullPath, sModuleName); CFile* pFile = pSock->OpenFile(false); if (!pFile) { delete pSock; return false; } unsigned short uPort = CZNC::Get().GetManager().ListenRand("DCC::LISTEN::" + sRemoteNick, GetLocalDCCIP(), false, SOMAXCONN, pSock, 120); if (GetNick().Equals(sRemoteNick)) { PutUser(":" + GetStatusPrefix() + "[email protected] PRIVMSG " + sRemoteNick + " :\001DCC SEND " + pFile->GetShortName() + " " + CString(CUtils::GetLongIP(GetLocalDCCIP())) + " " + CString(uPort) + " " + CString(pFile->GetSize()) + "\001"); } else { PutIRC("PRIVMSG " + sRemoteNick + " :\001DCC SEND " + pFile->GetShortName() + " " + CString(CUtils::GetLongIP(GetLocalDCCIP())) + " " + CString(uPort) + " " + CString(pFile->GetSize()) + "\001"); } PutModule(sModuleName, "DCC -> [" + sRemoteNick + "][" + pFile->GetShortName() + "] - Attempting Send."); return true; }
CFile* CDCCSock::OpenFile(bool bWrite) { if ((m_pFile) || (m_sLocalFile.empty())) { if (m_bSend) { m_pModule->PutModule( t_f("Sending [{1}] to [{2}]: Unable to open file.")( m_sFileName, m_sRemoteNick)); } else { m_pModule->PutModule( t_f("Receiving [{1}] from [{2}]: Unable to open file.")( m_sFileName, m_sRemoteNick)); } return nullptr; } m_pFile = new CFile(m_sLocalFile); if (bWrite) { if (m_pFile->Exists()) { delete m_pFile; m_pFile = nullptr; m_pModule->PutModule( t_f("Receiving [{1}] from [{2}]: File already exists.")( m_sFileName, m_sRemoteNick)); return nullptr; } if (!m_pFile->Open(O_WRONLY | O_TRUNC | O_CREAT)) { delete m_pFile; m_pFile = nullptr; m_pModule->PutModule( t_f("Receiving [{1}] from [{2}]: Could not open file.")( m_sFileName, m_sRemoteNick)); return nullptr; } } else { if (!m_pFile->IsReg()) { delete m_pFile; m_pFile = nullptr; m_pModule->PutModule( t_f("Sending [{1}] to [{2}]: Not a file.")( m_sFileName, m_sRemoteNick)); return nullptr; } if (!m_pFile->Open()) { delete m_pFile; m_pFile = nullptr; m_pModule->PutModule( t_f("Sending [{1}] to [{2}]: Could not open file.")( m_sFileName, m_sRemoteNick)); return nullptr; } // The DCC specs only allow file transfers with files smaller // than 4GiB (see ReadData()). unsigned long long uFileSize = m_pFile->GetSize(); if (uFileSize > (unsigned long long)0xffffffffULL) { delete m_pFile; m_pFile = nullptr; m_pModule->PutModule( t_f("Sending [{1}] to [{2}]: File too large (>4 GiB).")( m_sFileName, m_sRemoteNick)); return nullptr; } m_uFileSize = uFileSize; } m_sFileName = m_pFile->GetShortName(); return m_pFile; }