BOOL CDCNeighbour::OnHubName(CDCPacket *pPacket) { // Name of hub // $HubName Title [Description]| CString sDescription; int nHubInfo = pPacket->Find( ' ', 9 ); if ( nHubInfo != -1 ) { m_sServerName = UTF8Decode( (LPCSTR)&pPacket->m_pBuffer[ 9 ], nHubInfo - 9 ); sDescription = UTF8Decode( (LPCSTR)&pPacket->m_pBuffer[ nHubInfo + 1 ], pPacket->m_nLength - nHubInfo - 2 ).TrimLeft( _T(" -") ); } else m_sServerName = UTF8Decode( (LPCSTR)&pPacket->m_pBuffer[ 9 ], pPacket->m_nLength - 9 - 1 ); if ( CHostCacheHostPtr pServer = HostCache.DC.Find( &m_pHost.sin_addr ) ) { pServer->m_sName = m_sServerName; pServer->m_sDescription = sDescription; HostCache.DC.m_nCookie++; } ChatCore.OnMessage( this, pPacket ); return TRUE; }
BOOL CDCNeighbour::OnConnectToMe(LPSTR szParams) { // Client connection request // $ConnectToMe MyNick SenderIp:SenderPort| // or // $ConnectToMe SenderNick MyNick SenderIp:SenderPort| if ( LPSTR szSenderNick = szParams ) { if ( LPSTR szMyNick = strchr( szSenderNick, ' ' ) ) { *szMyNick++ = 0; LPSTR szAddress = strchr( szMyNick, ' ' ); if ( szAddress ) { *szAddress++ = 0; } else { szAddress = szMyNick; szMyNick = szSenderNick; szSenderNick = ""; } CString sMyNick( UTF8Decode( szMyNick) ); CString sSenderNick( UTF8Decode( szSenderNick ) ); if ( LPSTR szPort = strchr( szAddress, ':' ) ) { *szPort++ = 0; int nPort = atoi( szPort ); IN_ADDR nAddress; nAddress.s_addr = inet_addr( szAddress ); if ( m_sNick == sMyNick ) { // Ok DCClients.ConnectTo( &nAddress, (WORD)nPort, this, sSenderNick ); } else { // Wrong nick, bad IP } } } } return TRUE; }
BOOL CDCNeighbour::OnQuit(LPSTR szNick) { // User leave hub // $Quit nick| if ( szNick ) { CString sNick = UTF8Decode( szNick ); CChatUser* pUser; if ( m_oUsers.Lookup( sNick, pUser ) ) { m_oUsers.RemoveKey( sNick ); delete pUser; } if ( m_nNodeType == ntHub ) { HostCache.DC.Add( &m_pHost.sin_addr, htons( m_pHost.sin_port ), 0, 0, 0, GetUserCount() ); } // Notify chat window ChatCore.OnDeleteUser( this, new CString( sNick ) ); } return TRUE; }
BOOL CDCNeighbour::OnHello(LPSTR szNick) { // User logged-in // $Hello Nick| m_nState = nrsConnected; m_bNickValid = TRUE; m_sNick = UTF8Decode( szNick ); if ( CHostCacheHostPtr pServer = HostCache.DC.Find( &m_pHost.sin_addr ) ) { pServer->m_sUser = m_sNick; HostCache.DC.m_nCookie++; } // NMDC version if ( CDCPacket* pPacket = CDCPacket::New() ) { pPacket->Write( _P("$Version 1,0091|") ); Send( pPacket ); } SendUserInfo(); // Request nick list if ( CDCPacket* pPacket = CDCPacket::New() ) { pPacket->Write( _P("$GetNickList|") ); Send( pPacket ); } return TRUE; }
// Takes access to a string, default peek false to move a line from the buffer to the string, and default CP_ACP to read ASCII text // Looks for bytes like "line\r\n" in the buffer, and moves them from the buffer to the string, throwing away the "\r\n" part // Returns true if a line was found and moved from the buffer to the string, false if there isn't a '\n' in the buffer right now BOOL CBuffer::ReadLine(CString& strLine, BOOL bPeek) { // Empty the string, making it blank strLine.Empty(); // If this buffer is empty, tell the caller we didn't find a complete line if ( ! m_nLength ) return FALSE; // Scan down each byte in the buffer DWORD nLength = 0; for ( ; nLength < m_nLength ; nLength++ ) { // If the byte at this length is the newline character '\n', exit the loop if ( m_pBuffer[ nLength ] == '\n' ) break; } // If the loop didn't find a '\n' and instead stopped because nLength grew to equal m_nLength if ( nLength >= m_nLength ) return FALSE; // No '\n' in buffer, tell the caller we didn't find a complete line strLine = UTF8Decode( (LPCSTR)m_pBuffer, nLength ); strLine.TrimRight( _T("\r\n") ); // Now that the line has been copied into the string, remove it and the '\n' from the buffer if ( ! bPeek ) Remove( nLength + 1 ); // Unless we're peeking, then leave it in the buffer // Report that we found a line and moved it from the buffer to the string return TRUE; }
/** ** Checks for the ServerConf values before we actually decode. ** ** This function is really a ServerConf wrapper for UTF8Decode. ** ** @param ServerConf the server configuration ** @param start the start of the URI ** @param end the end of the URI ** @param ptr the current pointer into the URI ** ** @return integer ** ** @retval char the decode/undecoded byte. ** ** @see GetByte() */ static int UnicodeDecode(HI_SESSION *Session, const u_char *start, const u_char *end, const u_char **ptr, int iFirst, URI_NORM_STATE *norm_state, uint16_t *encodeType) { HTTPINSPECT_CONF *ServerConf = Session->server_conf; int iNorm = iFirst; if(ServerConf->iis_unicode.on || ServerConf->utf_8.on) { iNorm = UTF8Decode(Session, start, end, ptr, iFirst, norm_state, encodeType); } return iNorm; }
BOOL CDCNeighbour::OnRevConnectToMe(LPSTR szParams) { // Callback connection request // $RevConnectToMe RemoteNick MyNick| if ( LPSTR szRemoteNick = szParams ) { if ( LPSTR szMyNick = strchr( szRemoteNick, ' ' ) ) { *szMyNick++ = 0; CString sNick( UTF8Decode( szMyNick ) ); CString sRemoteNick( UTF8Decode( szRemoteNick ) ); if ( m_bNickValid && m_sNick == sNick ) { ConnectToMe( sRemoteNick ); } } } return TRUE; }
BOOL CDCNeighbour::OnChat(CDCPacket* pPacket) { // Chat message // <Nick> Message| if ( LPCSTR szMessage = strchr( (LPCSTR)pPacket->m_pBuffer, '>' ) ) { int nNickLen = szMessage - (LPCSTR)pPacket->m_pBuffer - 1; CString sNick( UTF8Decode( (LPCSTR)&pPacket->m_pBuffer[ 1 ], nNickLen ) ); if ( nNickLen > 0 && m_sNick != sNick ) { ChatCore.OnMessage( this, pPacket ); } } return TRUE; }
BOOL CDCNeighbour::OnForceMove(LPSTR szParams) { // User redirection // $ForceMove IP:Port| if ( LPSTR szAddress = szParams ) { int nPort = protocolPorts[ PROTOCOL_DC ]; if ( LPSTR szPort = strchr( szAddress, ':' ) ) { *szPort++ = 0; nPort = atoi( szPort ); } Network.ConnectTo( UTF8Decode( szAddress ), nPort, PROTOCOL_DC ); } return TRUE; }
CXMLElement* CXMLElement::FromBytes(BYTE* pByte, DWORD nByte, BOOL bHeader) { CString strXML; if ( nByte >= 2 && ( ( pByte[0] == 0xFE && pByte[1] == 0xFF ) || ( pByte[0] == 0xFF && pByte[1] == 0xFE ) ) ) { nByte = nByte / 2 - 1; if ( pByte[0] == 0xFE && pByte[1] == 0xFF ) { pByte += 2; for ( DWORD nSwap = 0; nSwap < nByte; nSwap ++ ) { register CHAR nTemp = pByte[ ( nSwap << 1 ) + 0 ]; pByte[ ( nSwap << 1 ) + 0 ] = pByte[ ( nSwap << 1 ) + 1 ]; pByte[ ( nSwap << 1 ) + 1 ] = nTemp; } } else { pByte += 2; } CopyMemory( strXML.GetBuffer( nByte ), pByte, nByte * sizeof( TCHAR ) ); strXML.ReleaseBuffer( nByte ); } else { if ( nByte >= 3 && pByte[0] == 0xEF && pByte[1] == 0xBB && pByte[2] == 0xBF ) { pByte += 3; nByte -= 3; } strXML = UTF8Decode( (LPCSTR)pByte, nByte ); } return FromString( strXML, bHeader ); }
int C86ZW::Search(const char* book, const char* author, char *bookUri) { char name[256] = {0}; char request[256] = {0}; int r = url_encode(UTF8Decode(book), -1, name, sizeof(name)); sprintf(request, "searchkey=%s&searchtype=articlename", name); const char* uri = "http://www.86zw.org/modules/article/search.php"; r = SearchBook(this, uri, request, book, author, name); if(0 == r) { // book: http://www.86zw.org/html/2/2355.html // index: http://www.86zw.org/html/2/2355/index.html char* p = strrchr(name, '.'); if(p) { *p = 0; sprintf(bookUri, "%s/index.html", name); } } return 0; }
BOOL CDCNeighbour::OnUserIP(LPSTR szIP) { // User address // $UserIP MyNick IP| if ( LPSTR szMyNick = szIP ) { if ( LPSTR szAddress = strchr( szMyNick, ' ' ) ) { *szAddress++ = 0; CString sNick( UTF8Decode( szMyNick ) ); if ( m_bNickValid && m_sNick == sNick ) { IN_ADDR nAddress; nAddress.s_addr = inet_addr( szAddress ); Network.AcquireLocalAddress( nAddress ); } } } return TRUE; }
void CRemote::Output(LPCTSTR pszName) { if ( _tcsstr( pszName, L".." ) || _tcschr( pszName, L'/' ) ) return; CString strValue = Settings.General.Path + L"\\Remote\\" + pszName + L".html"; CFile hFile; if ( ! hFile.Open( strValue, CFile::modeRead ) ) return; int nBytes = (int)hFile.GetLength(); CAutoVectorPtr< BYTE > pBytes( new BYTE[ nBytes ] ); hFile.Read( pBytes, nBytes ); hFile.Close(); LPCSTR pBody = (LPCSTR)(BYTE*)pBytes; if ( nBytes > 3 && pBytes[0] == 0xEF && pBytes[1] == 0xBB && pBytes[2] == 0xBF ) { // Skip BOM pBody += 3; nBytes -= 3; } CString strBody = UTF8Decode( pBody, nBytes ); CList<BOOL> pDisplayStack; for ( BOOL bDisplay = TRUE; ; ) { int nStart = strBody.Find( L"<%" ); if ( nStart < 0 ) { if ( bDisplay ) m_sResponse += strBody; break; } else if ( nStart >= 0 ) { if ( bDisplay && nStart > 0 ) m_sResponse += strBody.Left( nStart ); strBody = strBody.Mid( nStart + 2 ); } int nEnd = strBody.Find( L"%>" ); if ( nEnd < 0 ) break; CString strKey = strBody.Left( nEnd ); strBody = strBody.Mid( nEnd + 2 ); strKey.Trim(); ToLower( strKey ); if ( strKey.IsEmpty() ) { // Nothing } else if ( strKey.GetAt( 0 ) == L'=' && bDisplay ) { strKey = strKey.Mid( 1 ); strKey.Trim(); if ( m_pKeys.Lookup( strKey, strValue ) ) m_sResponse += strValue; } else if ( strKey.GetAt( 0 ) == L'?' ) { strKey = strKey.Mid( 1 ); strKey.Trim(); if ( strKey.IsEmpty() ) { if ( ! pDisplayStack.IsEmpty() ) bDisplay = pDisplayStack.RemoveTail(); } else { if ( strKey.GetAt( 0 ) == L'!' ) { strKey = strKey.Mid( 1 ); strKey.Trim(); if ( ! m_pKeys.Lookup( strKey, strValue ) ) strValue.Empty(); pDisplayStack.AddTail( bDisplay ); bDisplay = bDisplay && strValue.IsEmpty(); } else { if ( ! m_pKeys.Lookup( strKey, strValue ) ) strValue.Empty(); pDisplayStack.AddTail( bDisplay ); bDisplay = bDisplay && ! strValue.IsEmpty(); } } } } }
BOOL CDCNeighbour::OnUserInfo(LPSTR szInfo) { // User info // $MyINFO $ALL nick description<tag>$ $connection$e-mail$sharesize$| if ( strncmp( szInfo, _P("$ALL ") ) == 0 ) { LPSTR szNick = szInfo + 5; if ( LPSTR szDescription = strchr( szNick, ' ' ) ) { *szDescription++ = 0; CString sNick( UTF8Decode( szNick ) ); CChatUser* pUser; if ( ! m_oUsers.Lookup( sNick, pUser ) ) { pUser = new CChatUser; m_oUsers.SetAt( sNick, pUser ); } pUser->m_bType = ( sNick == m_sNick ) ? cutMe : cutUser; pUser->m_sNick = sNick; if ( LPSTR szConnection = strchr( szDescription, '$' ) ) { *szConnection++ = 0; if ( LPSTR szVendor = strchr( szDescription, '<' ) ) { if ( *(szConnection - 2) == '>' ) { *szVendor++ = 0; *(szConnection - 2) = 0; CStringA sVersion; if ( LPSTR szTags = strchr( szVendor, ' ' ) ) { *szTags++ = 0; for ( CStringA sTags( szTags ); ! sTags.IsEmpty(); ) { CStringA sTag = sTags.SpanExcluding( "," ); sTags = sTags.Mid( sTag.GetLength() + 1 ); if ( sTag.IsEmpty() ) continue; int nPos = sTag.Find( ':' ); if ( nPos > 0 ) { CStringA sTagName = sTag.Left( nPos ); sTag = sTag.Mid( nPos + 1 ); if ( sTagName == "V" ) { // Version sVersion = sTag; } else if ( sTagName == "M" ) { // Mode } else if ( sTagName == "H" ) { // Hubs } else if ( sTagName == "S" ) { // Slots } } } } pUser->m_sUserAgent = UTF8Decode( szVendor ); if ( ! sVersion.IsEmpty() ) pUser->m_sUserAgent += _T(" ") + UTF8Decode( sVersion ); } } } pUser->m_sDescription = UTF8Decode( szDescription ); if ( m_nNodeType == ntHub ) { HostCache.DC.Add( &m_pHost.sin_addr, htons( m_pHost.sin_port ), 0, 0, 0, GetUserCount() ); } // Notify chat window ChatCore.OnAddUser( this, new CChatUser( *pUser ) ); } } return TRUE; }
BOOL CDCNeighbour::OnLock(LPSTR szParams) { // $Lock [EXTENDEDPROTOCOL]Challenge Pk=Vendor| if ( LPSTR szLock = szParams ) { m_bExtended = ( strncmp( szParams, _P("EXTENDEDPROTOCOL") ) == 0 ); if ( LPSTR szUserAgent = strstr( szParams, " Pk=" ) ) { // Good way *szUserAgent = 0; szUserAgent += 4; m_sUserAgent = UTF8Decode( szUserAgent ); } else { // Bad way if ( LPSTR szUserAgent = strchr( szParams, ' ' ) ) { *szUserAgent++ = 0; m_sUserAgent = UTF8Decode( szUserAgent ); } } if ( m_nState < nrsHandshake2 ) { m_nState = nrsHandshake2; // Waiting for $Hello } if ( m_nNodeType == ntHub ) { HostCache.DC.Add( &m_pHost.sin_addr, htons( m_pHost.sin_port ) ); } if ( m_bExtended ) { if ( CDCPacket* pPacket = CDCPacket::New() ) { pPacket->Write( _P(DC_HUB_SUPPORTS) ); Send( pPacket ); } } std::string strKey = DCClients.MakeKey( szLock ); if ( CDCPacket* pPacket = CDCPacket::New() ) { pPacket->Write( _P("$Key ") ); pPacket->Write( strKey.c_str(), (DWORD)strKey.size() ); pPacket->Write( _P("|") ); Send( pPacket ); } m_bNickValid = FALSE; if ( CHostCacheHostPtr pServer = HostCache.DC.Find( &m_pHost.sin_addr ) ) { m_sNick = pServer->m_sUser; } m_sNick = DCClients.CreateNick( m_sNick ); if ( CDCPacket* pPacket = CDCPacket::New() ) { pPacket->Write( _P("$ValidateNick ") ); pPacket->WriteString( m_sNick, FALSE ); pPacket->Write( _P("|") ); Send( pPacket ); } } return TRUE; }