📄 chat.cpp
字号:
send(pChat->hSocket,strText,strText.GetLength(),0);
return;
}
// Make sure we are not already dealing with a file.
if (pChat->nTransferType != CHAT_TRANSFER_NONE)
{
strText.Format("%cCurrently transferring a file.%c",
(unsigned char)CHAT_FILE_DENY,
(unsigned char)CHAT_END_OF_COMMAND);
send(pChat->hSocket,strText,strText.GetLength(),0);
return;
}
char *pszSize = NULL;
char *pszFilename = strtok(pszBuf,",");
if (pszFilename != NULL)
pszSize = strtok(NULL,",");
if (pszFilename == NULL || pszSize == NULL)
{
strText.Format("%cInvalid start packet.%c",
(unsigned char)CHAT_FILE_DENY,
(unsigned char)CHAT_END_OF_COMMAND);
send(pChat->hSocket,strText,strText.GetLength(),0);
return;
}
// Make sure the file does not already exist.
CString strFilename(_config.strDownloadPath+pszFilename);
CFileStatus fs;
if (CFile::GetStatus(strFilename,fs))
{
strText.Format("%cFile already exists.%c",
(unsigned char)CHAT_FILE_DENY,
(unsigned char)CHAT_END_OF_COMMAND);
send(pChat->hSocket,strText,strText.GetLength(),0);
return;
}
// Open the file for writing.
if (!pChat->file.Open(strFilename,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary))
{
strText.Format("%cUnable to create the file.%c",
(unsigned char)CHAT_FILE_DENY,
(unsigned char)CHAT_END_OF_COMMAND);
send(pChat->hSocket,strText,strText.GetLength(),0);
return;
}
char *ptr = strText.GetBuffer(pChat->strName.GetLength()+strFilename.GetLength()+80);
int nResult = sprintf(ptr,"\n<CHAT> Receiving a file from %s -- Filename: %s, Length: %u\n",
(const char *)pChat->strName,
(const char *)strFilename,
atoi(pszSize));
strText.ReleaseBuffer(nResult);
ChatPrint(strText);
pChat->nTransferType = CHAT_TRANSFER_RECEIVE;
pChat->nFileLength = atoi(pszSize);
pChat->nBytesWritten = 0;
pChat->tTransferStart = time(NULL);
// Request the first block.
strText.Format("%c%c",
(unsigned char)CHAT_FILE_BLOCK_REQUEST,
(unsigned char)CHAT_END_OF_COMMAND);
send(pChat->hSocket,strText,strText.GetLength(),0);
}
void CChat::IncomingFileDeny(CHAT *pChat, const char *pszBuf)
{
if (pChat->nTransferType != CHAT_TRANSFER_SEND)
return;
CString strText;
char *ptr = strText.GetBuffer(strlen(pszBuf)+80);
int nResult = sprintf(ptr,"\n<CHAT> File start denied from %s: %s\n",
(const char *)pChat->strName,pszBuf);
strText.ReleaseBuffer(nResult);
ChatPrint(strText);
pChat->nTransferType = CHAT_TRANSFER_NONE;
pChat->file.Close();
}
void CChat::IncomingFileBlockRequest(CHAT *pChat, const char *pszBuf)
{
if (pChat->nTransferType != CHAT_TRANSFER_SEND)
return;
m_pSendBuf[0] = (unsigned char)CHAT_FILE_BLOCK;
int nBytes = pChat->file.Read(m_pSendBuf+1,CHAT_BLOCKSIZE);
m_pSendBuf[CHAT_BLOCKSIZE+1] = (unsigned char)CHAT_END_OF_COMMAND;
send(pChat->hSocket,m_pSendBuf,CHAT_BLOCKSIZE+2,0);
// Check to see if we are at the end of the file.
if (nBytes < CHAT_BLOCKSIZE)
{
CString strText;
strText.Format("%c%c",
(unsigned char)CHAT_FILE_END,
(unsigned char)CHAT_END_OF_COMMAND);
send(pChat->hSocket,strText,strText.GetLength(),0);
time_t tNow = time(NULL);
time_t tDif = tNow - pChat->tTransferStart;
char *ptr = strText.GetBuffer(pChat->file.GetFileName().GetLength()+160);
int nResult = sprintf(ptr,"\n<CHAT> File transfer complete. File: %s, Length: %u, Time: %d:%02d\n",
(const char *)pChat->file.GetFileName(),
pChat->nFileLength,
tDif / 60,
tDif % 60);
strText.ReleaseBuffer(nResult);
ChatPrint(strText);
pChat->nTransferType = CHAT_TRANSFER_NONE;
pChat->file.Close();
}
}
void CChat::IncomingFileBlock(CHAT *pChat, const char *pszBuf)
{
if (pChat->nTransferType != CHAT_TRANSFER_RECEIVE)
return;
// Figure out how many bytes the block should have in it.
int nBytes = pChat->nFileLength - pChat->nBytesWritten;
if (nBytes > CHAT_BLOCKSIZE)
nBytes = CHAT_BLOCKSIZE;
// Write it out and keep track of bytes written.
if (nBytes)
{
pChat->file.Write(pszBuf,nBytes);
pChat->nBytesWritten += nBytes;
}
// Request another block. Need to keep requesting blocks even
// if this side thinks it is done with the file. This will
// cause the sender to eventually give us the end of file
// command.
char pBuf[2];
pBuf[0] = (unsigned char)CHAT_FILE_BLOCK_REQUEST;
pBuf[1] = (unsigned char)CHAT_END_OF_COMMAND;
send(pChat->hSocket,pBuf,2,0);
}
void CChat::IncomingFileEnd(CHAT *pChat)
{
if (pChat->nTransferType != CHAT_TRANSFER_RECEIVE)
return;
CString strText;
time_t tNow = time(NULL);
time_t tDif = tNow - pChat->tTransferStart;
char *ptr = strText.GetBuffer(pChat->file.GetFileName().GetLength()+80);
int nResult = sprintf(ptr,"\n<CHAT> File transfer complete. File: %s, Length: %u, Time: %d:%02d\n",
(const char *)pChat->file.GetFileName(),
pChat->nFileLength,
tDif / 60,
tDif % 60);
strText.ReleaseBuffer(nResult);
ChatPrint(strText);
pChat->nTransferType = CHAT_TRANSFER_NONE;
pChat->file.Close();
}
void CChat::IncomingFileCancel(CHAT *pChat)
{
if (pChat->nTransferType == CHAT_TRANSFER_NONE)
return;
pChat->nTransferType = CHAT_TRANSFER_NONE;
pChat->file.Close();
ChatPrint("\n<CHAT> File transfer cancelled.\n");
}
BOOL CChat::Ping(int nIndex)
{
if (nIndex < 0 || nIndex >= m_nCount)
return(FALSE);
CHAT *pChat = (CHAT *)m_ptrList.GetAt(nIndex);
return(Ping(pChat));
}
BOOL CChat::Ping(const char *pszName)
{
CHAT *pChat = FindName(pszName);
if (pChat == NULL)
return(FALSE);
return(Ping(pChat));
}
BOOL CChat::Ping(CHAT *pChat)
{
int nClock = clock();
CString strText;
strText.Format("%c%d%c",
(unsigned char)CHAT_PING_REQUEST,
nClock,
(unsigned char)CHAT_END_OF_COMMAND);
send(pChat->hSocket,strText,strText.GetLength(),0);
ChatPrint("\n<CHAT> Ping request sent.\n");
return(TRUE);
}
void CChat::IncomingPingRequest(const CHAT *pChat, const char *pszBuf)
{
// pszBuf will have the clock ticks elapsed sent from the person
// requesting the ping. Send it back to them and I don't have
// to keep track of the ping requests on the sending machine.
CString strText;
strText.Format("%c%s%c",
(unsigned char)CHAT_PING_RESPONSE,
pszBuf,
(unsigned char)CHAT_END_OF_COMMAND);
send(pChat->hSocket,strText,strText.GetLength(),0);
}
void CChat::IncomingPingResponse(const CHAT *pChat, const char *pszBuf)
{
clock_t nClockNow = clock();
long nStart = atoi(pszBuf);
long nDif = nClockNow - nStart;
long nDiv = CLOCKS_PER_SEC;
double dElapsed = nDif / (double)CLOCKS_PER_SEC;
CString strText;
char *pBuf = strText.GetBuffer(pChat->strName.GetLength()+80);
int nResult = sprintf(pBuf,"\n<CHAT> Ping returned from %s: %.3f\n",
(const char *)pChat->strName,
dElapsed);
strText.ReleaseBuffer(nResult);
ChatPrint(strText);
}
BOOL CChat::PeekConnections(const char *pszName)
{
CHAT *pChat = FindName(pszName);
if (pChat == NULL)
return(FALSE);
else
return(PeekConnections(pChat));
}
BOOL CChat::PeekConnections(int nIndex)
{
if (nIndex < 0 || nIndex >= m_nCount)
return(FALSE);
return(PeekConnections((CHAT *)m_ptrList.GetAt(nIndex)));
}
BOOL CChat::PeekConnections(const CHAT *pChat)
{
CString strText;
strText.Format("%c%c",
(unsigned char)CHAT_PEEK_CONNECTIONS,
(unsigned char)CHAT_END_OF_COMMAND);
send(pChat->hSocket,strText,strText.GetLength(),0);
return(TRUE);
}
void CChat::SendPublicPeek(const CHAT *pChat)
{
CString strConnects;
CString strTemp;
CString strAddress;
CHAT *pTemp;
for (int i=0;i<m_nCount;i++)
{
pTemp = (CHAT *)m_ptrList.GetAt(i);
if (pChat != pTemp && !pTemp->bPrivate)
{
if (pTemp->strAddress.GetLength() < 8)
strAddress = "<Unknown>";
else
strAddress = pTemp->strAddress;
strTemp.Format("%s~%u~%s~",
(const char *)strAddress,
pTemp->nPort,
(const char *)pTemp->strName);
strConnects += strTemp;
}
}
if (!strConnects.IsEmpty())
{
CString strText;
char *ptr = strText.GetBuffer(strConnects.GetLength()+5);
int nResult = sprintf(ptr,"%c%s%c",
(unsigned char)CHAT_PEEK_LIST,
(const char *)strConnects,
(unsigned char)CHAT_END_OF_COMMAND);
strText.ReleaseBuffer(nResult);
send(pChat->hSocket,strText,strText.GetLength(),0);
}
else
{
CString strText;
strText.Format("\n<CHAT> %s doesn't have any other connections.\n",
(const char *)m_strChatName);
SendMessage(pChat,strText);
}
}
void CChat::GetPeekParam(CString &strText, CString &strParam)
{
int nIndex = strText.Find('~');
if (nIndex == -1)
{
strText.Empty();
strParam.Empty();
return;
}
strParam = strText.Left(nIndex);
strText = strText.Right(strText.GetLength()-nIndex-1);
}
void CChat::IncomingPeekList(const CHAT *pChat, char *pszBuf)
{
int nCount = 0;
int nIndex = 0;
while(*(pszBuf+nIndex))
{
if (*(pszBuf+nIndex) == '~')
nCount++;
nIndex++;
}
nCount /= 3;
CString strBuf(pszBuf);
CString strText;
strText.Format("\n<CHAT> Peek found %d connections.\n\n",nCount);
ChatPrint(strText);
CommandPrintOn();
_terminal.Print(" Name Address Port \n");
_terminal.Print(" ==================== =============== =====\n");
CString strIP, strPort, strName;
nCount = 1;
char *pBuf;
int nResult;
GetPeekParam(strBuf,strIP);
GetPeekParam(strBuf,strPort);
GetPeekParam(strBuf,strName);
while(!strIP.IsEmpty() && !strPort.IsEmpty() && !strName.IsEmpty())
{
if (strName.GetLength() > 20)
strName = strName.Left(20);
pBuf = strText.GetBuffer(strName.GetLength()+80);
nResult = sprintf(pBuf,"%s%03d:%s %-20s %-15s %s\n",
ANSI_F_BOLDWHITE,
nCount,
ANSI_RESET,
(const char *)strName,
(const char *)strIP,
(const char *)strPort);
strText.ReleaseBuffer(nResult);
_terminal.Print(strText);
GetPeekParam(strBuf,strIP);
GetPeekParam(strBuf,strPort);
GetPeekParam(strBuf,strName);
nCount++;
}
CommandPrintOff();
}
void CChat::SendMessage(const CHAT *pChat, const char *pszMessage)
{
char *pBuf;
int nResult;
CString strText;
pBuf = strText.GetBuffer(strlen(pszMessage)+10);
nResult = sprintf(pBuf,"%c%s%c",
(unsigned char)CHAT_MESSAGE,
pszMessage,
(unsigned char)CHAT_END_OF_COMMAND);
strText.ReleaseBuffer(nResult);
send(pChat->hSocket,strText,nResult,0);
}
void CChat::Snoop(const CHAT *pChat)
{
CString strText;
strText.Format("%c%c",
(unsigned char)CHAT_SNOOP,
(unsigned char)CHAT_END_OF_COMMAND);
send(pChat->hSocket,strText,strText.GetLength(),0);
}
void CChat::IncomingSnoop(CHAT *pChat)
{
char *pBuf;
int nResult;
CString strText;
if (!pChat->bAllowSnoop)
{
strText.Format("\n<CHAT> %s has not given you permission to snoop.\n",
(const char *)m_strChatName);
SendMessage(pChat,strText);
return;
}
if (pChat->bSnooped)
{
pBuf = strText.GetBuffer(160);
nResult = sprintf(pBuf,"\n<CHAT> %s has stopped snooping you.\n",
(const char *)pChat->strName);
strText.ReleaseBuffer(nResult);
pChat->bSnooped = FALSE;
m_nSnoopCount--;
}
else
{
pBuf = strText.GetBuffer(160);
nResult = sprintf(pBuf,"\n<CHAT> You are being snooped by %s.\n",
(const char *)pChat->strName);
strText.ReleaseBuffer(nResult);
pChat->bSnooped = TRUE;
m_nSnoopCount++;
}
ChatPrint(pBuf);
}
#define MAX_SNOOP_LEN 200
void CChat::IncomingSnoopData(const CHAT *pChat, const char *pszBuf)
{
CString strText;
CString strPrompt;
char szLine[MAX_SNOOP_LEN+1];
char szColor[3];
int nIndex;
int nFore, nBack;
WORD wFore, wBack, wAttr;
strPrompt.Format("%s>>",ANSI_F_BOLDGREEN);
szColor[2] = '\x0';
// Foreground color.
szColor[0] = *pszBuf++;
szColor[1] = *pszBuf++;
nFore = atoi(szColor);
wFore = ForeColorAttribute(nFore);
// Background color.
szColor[0] = *pszBuf++;
szColor[1] = *pszBuf++;
nBack = atoi(szColor);
wBack = BackColorAttribute(nBack);
wAttr = wFore | wBack;
if (*pszBuf == '\n')
{
pszBuf++;
_terminal.Print("\n");
}
CommandPrintOn();
_terminal.Print(strPrompt);
_terminal.SetColor(wAttr);
nIndex = 0;
while(*pszBuf)
{
szLine[nIndex] = *pszBuf;
nIndex++;
if (*pszBuf == '\n' || nIndex == MAX_SNOOP_LEN)
{
szLine[nIndex] = '\x0';
_terminal.Print(szLine);
nIndex = 0;
wAttr = _terminal.GetColor();
_terminal.Print(strPrompt);
_terminal.SetColor(wAttr);
}
pszBuf++;
}
// Print the left overs.
if (nIndex)
{
szLine[nIndex] = '\x0';
_terminal.Print(szLine);
}
CommandPrintOff();
}
void CChat::SendSnoop(const char *pszText)
{
CHAT *pTemp;
char *pBuf;
int nResult;
CString strText;
CString strColor;
pBuf = strText.GetBuffer(strlen(pszText)+20);
nResult = sprintf(pBuf,"%c%02d%02d%s%c",
(unsigned char)CHAT_SNOOP_DATA,
_nForeColor,
_nBackColor,
pszText,
(unsigned char)CHAT_END_OF_COMMAND);
strText.ReleaseBuffer(nResult);
for (int i=0;i<m_nCount;i++)
{
pTemp = (CHAT *)m_ptrList.GetAt(i);
if (pTemp->bSnooped)
send(pTemp->hSocket,strText,strText.GetLength(),0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -