Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Server;
- using System.IO;
- using System.Net;
- using Server.Network;
- using System.Threading;
- using System.Net.Sockets;
- namespace Server.Misc
- {
- public class StatusBot
- {
- public static string Nickname = "Genesis";
- public static string Username = "Genesis";
- public static string Realname = "GenesisBot";
- public static string Channel = "#Genesis";
- public static string Host = "irc.wildirc.net";
- public static int Port = 6667;
- public static string NickservPassword = null; // If you want to register StatusBot with nickserv, enter the password you registered the nickname to here.
- public static bool ReconnectOnDisc = true;
- public static bool Connected = false;
- public static TimeSpan SpamDelay = TimeSpan.FromSeconds(5.0); // StatusBot only accepts one command every 5 seconds, so users won't spam it.
- public static DateTime LastCommand;
- public static string Message = "";
- public static void Initialize()
- {
- Thread t = new Thread(new ThreadStart(Connect));
- t.Start();
- }
- private static StreamReader SR;
- private static StreamWriter SW;
- public static string FormatTimeSpan(TimeSpan ts)
- {
- return String.Format("{0:D2} Days, {1:D2} Hours, {2:D2} Minutes, and {3:D2} Seconds", ts.Days, ts.Hours % 24, ts.Minutes % 60, ts.Seconds % 60);
- }
- public static void Connect()
- {
- try
- {
- TcpClient C = new TcpClient(Host, Port);
- NetworkStream S = C.GetStream();
- SW = new StreamWriter(S);
- SR = new StreamReader(S);
- }
- catch
- {
- Thread.Sleep(60000);
- Connect();
- }
- Connected = true;
- SendRaw("USER " + Username + " 0 * :" + Realname);
- SetNick(Nickname);
- if (NickservPassword != null)
- Identify(NickservPassword);
- JoinChannel(Channel);
- GetInput();
- }
- public static void Stop()
- {
- Connected = false;
- SR.Close();
- SW.Close();
- }
- private static void GetInput()
- {
- try
- {
- while (Connected && ParseIncoming(SR.ReadLine())) ;
- }
- catch (Exception)
- {
- // Something's Wrong... Probably Disconnected
- Connected = false;
- }
- if (ReconnectOnDisc)
- {
- Thread.Sleep(60000);
- Connect();
- }
- }
- private static bool ParseIncoming(string inputLine)
- {
- if (inputLine == null)
- return false;
- Console.WriteLine(inputLine);
- string[] parts = inputLine.Split(':');
- if (parts.Length == 3)
- Message = parts[2];
- else
- Message = "";
- if (inputLine.StartsWith("PING"))
- SendRaw("PONG " + inputLine.Substring(6));
- if (Message.StartsWith("!") && LastCommand + SpamDelay <= DateTime.Now)
- ReadCommand(Message);
- return true;
- }
- private static void ReadCommand(string inputLine)
- {
- int userCount = NetState.Instances.Count;
- string uptime = FormatTimeSpan(DateTime.Now - Server.Items.Clock.ServerStart);
- LastCommand = DateTime.Now;
- switch (inputLine.ToLower())
- {
- case "!status":
- {
- SendMessage(Channel, String.Format("Users Online: {0} .:. Uptime {1} .:. -[StatusBot]-", userCount, uptime));
- break;
- }
- }
- }
- private static void SendRaw(string Message)
- {
- try
- {
- if (Connected)
- {
- SW.WriteLine(Message);
- SW.Flush();
- }
- }
- catch
- {
- // Something's Wrong... Probably Disconnected
- Connected = false;
- }
- }
- private static void Identify(string Password)
- {
- SendMessage("Nickserv", "Identify " + Password);
- }
- private static void JoinChannel(string C)
- {
- SendRaw("JOIN " + C);
- }
- private static void PartChannel(string C)
- {
- SendRaw("PART " + C);
- }
- private static void PartChannel(string C, string Message)
- {
- SendRaw("PART " + C + " :" + Message);
- }
- private static void Quit()
- {
- SendRaw("QUIT");
- Stop();
- }
- private static void Quit(string Message)
- {
- SendRaw("QUIT :" + Message);
- Stop();
- }
- private static void SendMessage(string Target, string Message)
- {
- SendRaw("PRIVMSG " + Target + " :" + Message);
- }
- private static void SendNotice(string Target, string Message)
- {
- SendRaw("NOTICE " + Target + " :" + Message);
- }
- private static void SetMode(string Target, string Modes)
- {
- SendRaw("MODE " + Target + " :" + Modes);
- }
- private static void SetNick(string N)
- {
- SendRaw("NICK " + N);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement