mirror of
https://github.com/reactos/reactos.git
synced 2025-02-22 08:25:03 +00:00
Give Techbot the ability to check for and ghost existing bots, then rename itself
svn path=/trunk/; revision=35264
This commit is contained in:
parent
b2a074adbf
commit
197cc3f634
3 changed files with 99 additions and 8 deletions
|
@ -17,10 +17,12 @@ namespace TechBot.IRCLibrary
|
||||||
public const string PRIVMSG = "PRIVMSG";
|
public const string PRIVMSG = "PRIVMSG";
|
||||||
public const string USER = "USER";
|
public const string USER = "USER";
|
||||||
public const string PASS = "PASS";
|
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_NAMREPLY = "353";
|
||||||
public const string RPL_ENDOFNAMES = "366";
|
public const string RPL_ENDOFNAMES = "366";
|
||||||
|
public const string ERR_NICKNAMEINUSE = "433";
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
|
@ -191,6 +191,7 @@ namespace TechBot.IRCLibrary
|
||||||
|
|
||||||
#region Private fields
|
#region Private fields
|
||||||
private bool firstPingReceived = false;
|
private bool firstPingReceived = false;
|
||||||
|
private bool awaitingGhostDeath = false;
|
||||||
private System.Text.Encoding encoding = System.Text.Encoding.UTF8;
|
private System.Text.Encoding encoding = System.Text.Encoding.UTF8;
|
||||||
private TcpClient tcpClient;
|
private TcpClient tcpClient;
|
||||||
private NetworkStream networkStream;
|
private NetworkStream networkStream;
|
||||||
|
@ -198,6 +199,9 @@ namespace TechBot.IRCLibrary
|
||||||
private LineBuffer messageStream;
|
private LineBuffer messageStream;
|
||||||
private ArrayList ircCommandEventRegistrations = new ArrayList();
|
private ArrayList ircCommandEventRegistrations = new ArrayList();
|
||||||
private ArrayList channels = new ArrayList();
|
private ArrayList channels = new ArrayList();
|
||||||
|
private string reqNickname;
|
||||||
|
private string curNickname;
|
||||||
|
private string password;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Public events
|
#region Public events
|
||||||
|
@ -240,6 +244,16 @@ namespace TechBot.IRCLibrary
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Nickname for the bot.
|
||||||
|
/// </summary>
|
||||||
|
public string Nickname
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return curNickname;
|
||||||
|
}
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Private methods
|
#region Private methods
|
||||||
|
@ -361,6 +375,24 @@ namespace TechBot.IRCLibrary
|
||||||
firstPingReceived = true;
|
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>
|
/// <summary>
|
||||||
/// Process RPL_NAMREPLY message.
|
/// Process RPL_NAMREPLY message.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -472,6 +504,31 @@ namespace TechBot.IRCLibrary
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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
|
#endregion
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -500,10 +557,14 @@ namespace TechBot.IRCLibrary
|
||||||
}
|
}
|
||||||
/* Install PING message handler */
|
/* Install PING message handler */
|
||||||
MonitorCommand(IRC.PING, new MessageReceivedHandler(PingMessageReceived));
|
MonitorCommand(IRC.PING, new MessageReceivedHandler(PingMessageReceived));
|
||||||
|
/* Install NOTICE message handler */
|
||||||
|
MonitorCommand(IRC.NOTICE, new MessageReceivedHandler(NoticeMessageReceived));
|
||||||
/* Install RPL_NAMREPLY message handler */
|
/* Install RPL_NAMREPLY message handler */
|
||||||
MonitorCommand(IRC.RPL_NAMREPLY, new MessageReceivedHandler(RPL_NAMREPLYMessageReceived));
|
MonitorCommand(IRC.RPL_NAMREPLY, new MessageReceivedHandler(RPL_NAMREPLYMessageReceived));
|
||||||
/* Install RPL_ENDOFNAMES message handler */
|
/* Install RPL_ENDOFNAMES message handler */
|
||||||
MonitorCommand(IRC.RPL_ENDOFNAMES, new MessageReceivedHandler(RPL_ENDOFNAMESMessageReceived));
|
MonitorCommand(IRC.RPL_ENDOFNAMES, new MessageReceivedHandler(RPL_ENDOFNAMESMessageReceived));
|
||||||
|
/* Install ERR_NICKNAMEINUSE message handler */
|
||||||
|
MonitorCommand(IRC.ERR_NICKNAMEINUSE, new MessageReceivedHandler(ERR_NICKNAMEINUSEMessageReceived));
|
||||||
/* Start receiving data */
|
/* Start receiving data */
|
||||||
Receive();
|
Receive();
|
||||||
}
|
}
|
||||||
|
@ -520,8 +581,6 @@ namespace TechBot.IRCLibrary
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
connected = false;
|
connected = false;
|
||||||
tcpClient.Close();
|
tcpClient.Close();
|
||||||
tcpClient = null;
|
tcpClient = null;
|
||||||
|
@ -606,10 +665,32 @@ namespace TechBot.IRCLibrary
|
||||||
if (nickname == null)
|
if (nickname == null)
|
||||||
throw new ArgumentNullException("nickname", "Nickname cannot be null.");
|
throw new ArgumentNullException("nickname", "Nickname cannot be null.");
|
||||||
|
|
||||||
|
Console.WriteLine("Changing nick to {0}\n", nickname);
|
||||||
|
curNickname = nickname;
|
||||||
|
|
||||||
/* NICK <nickname> [ <hopcount> ] */
|
/* NICK <nickname> [ <hopcount> ] */
|
||||||
SendMessage(new IrcMessage(IRC.NICK, nickname));
|
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>
|
/// <summary>
|
||||||
/// Submit password to identify user.
|
/// Submit password to identify user.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -619,6 +700,8 @@ namespace TechBot.IRCLibrary
|
||||||
if (password == null)
|
if (password == null)
|
||||||
throw new ArgumentNullException("password", "Password cannot be null.");
|
throw new ArgumentNullException("password", "Password cannot be null.");
|
||||||
|
|
||||||
|
this.password = password;
|
||||||
|
|
||||||
/* PASS <password> */
|
/* PASS <password> */
|
||||||
SendMessage(new IrcMessage(IRC.PASS, password));
|
SendMessage(new IrcMessage(IRC.PASS, password));
|
||||||
}
|
}
|
||||||
|
@ -635,12 +718,10 @@ namespace TechBot.IRCLibrary
|
||||||
{
|
{
|
||||||
if (nickname == null)
|
if (nickname == null)
|
||||||
throw new ArgumentNullException("nickname", "Nickname cannot be null.");
|
throw new ArgumentNullException("nickname", "Nickname cannot be null.");
|
||||||
|
reqNickname = nickname;
|
||||||
firstPingReceived = false;
|
firstPingReceived = false;
|
||||||
if (password != null)
|
if (password != null)
|
||||||
{
|
{
|
||||||
/* First ghost ourself and then register */
|
|
||||||
if (nickname != null)
|
|
||||||
SendMessage(new IrcMessage(IRC.GHOST, nickname + " " + password));
|
|
||||||
SubmitPassword(password);
|
SubmitPassword(password);
|
||||||
}
|
}
|
||||||
ChangeNick(nickname);
|
ChangeNick(nickname);
|
||||||
|
|
|
@ -88,7 +88,15 @@ namespace TechBot.Library
|
||||||
m_IrcClient.Connect(hostname, port);
|
m_IrcClient.Connect(hostname, port);
|
||||||
|
|
||||||
m_IrcClient.Register(botname, password, null);
|
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();
|
JoinChannels();
|
||||||
|
|
||||||
while (!isStopped)
|
while (!isStopped)
|
||||||
|
@ -279,7 +287,7 @@ namespace TechBot.Library
|
||||||
injectMessage,
|
injectMessage,
|
||||||
GetMessageSource(context)));
|
GetMessageSource(context)));
|
||||||
InjectMessage(context,
|
InjectMessage(context,
|
||||||
injectMessage);
|
injectMessage);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue