Author: gedmurphy Date: Sun Aug 10 08:06:58 2008 New Revision: 35264
URL: http://svn.reactos.org/svn/reactos?rev=35264&view=rev Log: Give Techbot the ability to check for and ghost existing bots, then rename itself
Modified: trunk/irc/TechBot/TechBot.IRCLibrary/IRC.cs trunk/irc/TechBot/TechBot.IRCLibrary/IrcClient.cs trunk/irc/TechBot/TechBot.Library/TechBotIrcService.cs
Modified: trunk/irc/TechBot/TechBot.IRCLibrary/IRC.cs URL: http://svn.reactos.org/svn/reactos/trunk/irc/TechBot/TechBot.IRCLibrary/IRC.... ============================================================================== --- trunk/irc/TechBot/TechBot.IRCLibrary/IRC.cs [iso-8859-1] (original) +++ trunk/irc/TechBot/TechBot.IRCLibrary/IRC.cs [iso-8859-1] Sun Aug 10 08:06:58 2008 @@ -17,10 +17,12 @@ public const string PRIVMSG = "PRIVMSG"; public const string USER = "USER"; public const string PASS = "PASS"; - public const string GHOST = "MSG NICKSERV GHOST"; + public const string GHOST = "NICKSERV GHOST"; + public const string NOTICE = "NOTICE";
public const string RPL_NAMREPLY = "353"; public const string RPL_ENDOFNAMES = "366"; + public const string ERR_NICKNAMEINUSE = "433";
#endregion
Modified: trunk/irc/TechBot/TechBot.IRCLibrary/IrcClient.cs URL: http://svn.reactos.org/svn/reactos/trunk/irc/TechBot/TechBot.IRCLibrary/IrcC... ============================================================================== --- trunk/irc/TechBot/TechBot.IRCLibrary/IrcClient.cs [iso-8859-1] (original) +++ trunk/irc/TechBot/TechBot.IRCLibrary/IrcClient.cs [iso-8859-1] Sun Aug 10 08:06:58 2008 @@ -191,6 +191,7 @@
#region Private fields private bool firstPingReceived = false; + private bool awaitingGhostDeath = false; private System.Text.Encoding encoding = System.Text.Encoding.UTF8; private TcpClient tcpClient; private NetworkStream networkStream; @@ -198,6 +199,9 @@ private LineBuffer messageStream; private ArrayList ircCommandEventRegistrations = new ArrayList(); private ArrayList channels = new ArrayList(); + private string reqNickname; + private string curNickname; + private string password; #endregion
#region Public events @@ -240,6 +244,16 @@ } }
+ /// <summary> + /// Nickname for the bot. + /// </summary> + public string Nickname + { + get + { + return curNickname; + } + } #endregion
#region Private methods @@ -361,6 +375,24 @@ firstPingReceived = true; }
+ /// <summary> + /// Send a PONG message when a PING message is received. + /// </summary> + /// <param name="message">Received IRC message.</param> + private void NoticeMessageReceived(IrcMessage message) + { + if (awaitingGhostDeath) + { + string str = string.Format("{0} has been ghosted", reqNickname); + if (message.Parameters.Contains(str)) + { + ChangeNick(reqNickname); + SubmitPassword(password); + awaitingGhostDeath = false; + } + } + } + /// <summary> /// Process RPL_NAMREPLY message. /// </summary> @@ -472,6 +504,31 @@ } }
+ /// <summary> + /// Process ERR_NICKNAMEINUSE message. + /// </summary> + /// <param name="message">Received IRC message.</param> + private void ERR_NICKNAMEINUSEMessageReceived(IrcMessage message) + { + try + { + if (message.Parameters == null) + { + System.Diagnostics.Debug.WriteLine(String.Format("Message has no parameters.")); + return; + } + + /* Connect with a different name */ + string[] parameters = message.Parameters.Split(new char[] { ' ' }); + string nickname = parameters[1]; + ChangeNick(nickname + "__"); + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine(String.Format("Ex. {0}", ex)); + } + } + #endregion
/// <summary> @@ -500,10 +557,14 @@ } /* Install PING message handler */ MonitorCommand(IRC.PING, new MessageReceivedHandler(PingMessageReceived)); + /* Install NOTICE message handler */ + MonitorCommand(IRC.NOTICE, new MessageReceivedHandler(NoticeMessageReceived)); /* Install RPL_NAMREPLY message handler */ MonitorCommand(IRC.RPL_NAMREPLY, new MessageReceivedHandler(RPL_NAMREPLYMessageReceived)); /* Install RPL_ENDOFNAMES message handler */ MonitorCommand(IRC.RPL_ENDOFNAMES, new MessageReceivedHandler(RPL_ENDOFNAMESMessageReceived)); + /* Install ERR_NICKNAMEINUSE message handler */ + MonitorCommand(IRC.ERR_NICKNAMEINUSE, new MessageReceivedHandler(ERR_NICKNAMEINUSEMessageReceived)); /* Start receiving data */ Receive(); } @@ -520,8 +581,6 @@ } else { - - connected = false; tcpClient.Close(); tcpClient = null; @@ -606,10 +665,32 @@ if (nickname == null) throw new ArgumentNullException("nickname", "Nickname cannot be null.");
+ Console.WriteLine("Changing nick to {0}\n", nickname); + curNickname = nickname; + /* NICK <nickname> [ <hopcount> ] */ SendMessage(new IrcMessage(IRC.NICK, nickname)); }
+ /// <summary> + /// Ghost nickname. + /// </summary> + /// <param name="nickname">Nickname.</param> + public void GhostNick(string nickname, + string password) + { + if (nickname == null) + throw new ArgumentNullException("nickname", "Nickname cannot be null."); + + if (password == null) + throw new ArgumentNullException("password", "Password cannot be null."); + + awaitingGhostDeath = true; + + /* GHOST <nickname> <password> */ + SendMessage(new IrcMessage(IRC.GHOST, nickname + " " + password)); + } + /// <summary> /// Submit password to identify user. /// </summary> @@ -618,6 +699,8 @@ { if (password == null) throw new ArgumentNullException("password", "Password cannot be null."); + + this.password = password;
/* PASS <password> */ SendMessage(new IrcMessage(IRC.PASS, password)); @@ -635,12 +718,10 @@ { if (nickname == null) throw new ArgumentNullException("nickname", "Nickname cannot be null."); + reqNickname = nickname; firstPingReceived = false; if (password != null) { - /* First ghost ourself and then register */ - if (nickname != null) - SendMessage(new IrcMessage(IRC.GHOST, nickname + " " + password)); SubmitPassword(password); } ChangeNick(nickname);
Modified: trunk/irc/TechBot/TechBot.Library/TechBotIrcService.cs URL: http://svn.reactos.org/svn/reactos/trunk/irc/TechBot/TechBot.Library/TechBot... ============================================================================== --- trunk/irc/TechBot/TechBot.Library/TechBotIrcService.cs [iso-8859-1] (original) +++ trunk/irc/TechBot/TechBot.Library/TechBotIrcService.cs [iso-8859-1] Sun Aug 10 08:06:58 2008 @@ -88,7 +88,15 @@ m_IrcClient.Connect(hostname, port);
m_IrcClient.Register(botname, password, null); - Console.WriteLine("Registered as {0}...", botname); + Console.WriteLine("Registered as {0}...", m_IrcClient.Nickname); + + /* Did we get the nick we wanted? */ + if (m_IrcClient.Nickname != botname) + { + /* there must have been an existing one, kill it */ + m_IrcClient.GhostNick(botname, password);; + } + JoinChannels();
while (!isStopped) @@ -279,7 +287,7 @@ injectMessage, GetMessageSource(context))); InjectMessage(context, - injectMessage); + injectMessage); } else {