Advertisement
Guest User

Untitled

a guest
May 11th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Meebey.SmartIrc4net;
  5. using System.Threading;
  6.  
  7. namespace Minecraft_Server
  8. {
  9.     class IRCBot
  10.     {
  11.         static IrcClient irc = new IrcClient();
  12.         static string server = Properties.ircServer;
  13.         static string channel = Properties.ircChannel;
  14.         static string nick = Properties.ircNick;
  15.         static Thread ircThread;
  16.  
  17.         static string[] names;
  18.  
  19.         public IRCBot()
  20.         {
  21.             // the irc must run in a seperate thread, or else the server will freeze.
  22.             ircThread = new Thread(new ThreadStart(delegate
  23.             {
  24.                 // attach event handlers
  25.                 irc.OnConnecting += new EventHandler(OnConnecting);
  26.                 irc.OnConnected += new EventHandler(OnConnected);
  27.                 irc.OnChannelMessage += new IrcEventHandler(OnChanMessage);
  28.                 irc.OnJoin += new JoinEventHandler(OnJoin);
  29.                 irc.OnPart += new PartEventHandler(OnPart);
  30.                 irc.OnQuit += new QuitEventHandler(OnQuit);
  31.                 irc.OnNickChange += new NickChangeEventHandler(OnNickChange);
  32.                 //irc.OnDisconnected += new EventHandler(OnDisconnected);
  33.                 irc.OnQueryMessage += new IrcEventHandler(OnPrivMsg);
  34.                 irc.OnNames += new NamesEventHandler(OnNames);
  35.                 irc.OnChannelAction += new ActionEventHandler(OnAction);
  36.  
  37.                 // Attempt to connect to the IRC server
  38.                 try { irc.Connect(server, 6667); }
  39.                 catch (Exception ex) { Console.WriteLine("Unnable to connect to IRC server: {0}", ex.Message); }
  40.             }));
  41.             ircThread.Start();
  42.         }
  43.  
  44.         // While connecting
  45.         void OnConnecting(object sender, EventArgs e)
  46.         {
  47.             Server.Log("Connecting to IRC");
  48.         }
  49.         // When connected
  50.         void OnConnected(object sender, EventArgs e)
  51.         {
  52.             Server.Log("Connected to IRC");
  53.             irc.Login(nick, nick, 0, nick);
  54.             Server.Log("Joining channel");
  55.             irc.RfcJoin(channel);
  56.             irc.Listen();
  57.         }
  58.         void OnNames(object sender, NamesEventArgs e)
  59.         {
  60.             names = e.UserList;
  61.         }
  62.         //void OnDisconnected(object sender, EventArgs e)
  63.         //{
  64.         //    try { irc.Connect(server, 6667); }
  65.         //    catch (Exception ex) { Console.WriteLine("Failed to reconnect to IRC"); }
  66.         //}
  67.         // On public channel message
  68.         void OnChanMessage(object sender, IrcEventArgs e)
  69.         {
  70.  
  71.             string temp = e.Data.Message;
  72.            
  73.             string allowedchars = "1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./!@#$%^*()_+QWERTYUIOPASDFGHJKL:\"ZXCVBNM<>? ";
  74.  
  75.             foreach (char ch in temp)
  76.             {
  77.                 if (allowedchars.IndexOf(ch) == -1)
  78.                     temp = temp.Replace(ch.ToString(), "");
  79.             }
  80.  
  81.             Server.Log("IRC: " + e.Data.Nick + ": " + temp);
  82.             Player.GlobalMessage("IRC: &1" + e.Data.Nick + ": &f" + temp);
  83.  
  84.             //Server.Log("IRC: " + e.Data.Nick + ": " + e.Data.Message);
  85.             //Player.GlobalMessage("IRC: &1" + e.Data.Nick + ": &f" + e.Data.Message);
  86.         }
  87.         // When someone joins the IRC
  88.         void OnJoin(object sender, JoinEventArgs e)
  89.         {
  90.             Server.Log(e.Data.Nick + " has joined the IRC");
  91.             Player.GlobalMessage(e.Data.Nick + " has joined the IRC");
  92.             irc.RfcNames(channel);
  93.         }
  94.         // When someone leaves the IRC
  95.         void OnPart(object sender, PartEventArgs e)
  96.         {
  97.             Server.Log(e.Data.Nick + " has left the IRC");
  98.             Player.GlobalMessage(e.Data.Nick + " has left the IRC");
  99.             irc.RfcNames(channel);
  100.         }
  101.         void OnQuit(object sender, QuitEventArgs e)
  102.         {
  103.             Server.Log(e.Data.Nick + " has Left the IRC");
  104.             Player.GlobalMessage(e.Data.Nick + " has left the IRC");
  105.             irc.RfcNames(channel);
  106.         }
  107.         void OnPrivMsg(object sender, IrcEventArgs e)
  108.         {
  109.             Server.Log("IRC RECIEVING MESSESGE");
  110.             if (Server.ircControllers.Contains(e.Data.Nick))
  111.             {
  112.                 string cmd;
  113.                 string msg;
  114.                 int len = e.Data.Message.Split(' ').Length;
  115.                 cmd = e.Data.Message.Split(' ')[0];
  116.                 if (len > 1)
  117.                     msg = e.Data.Message.Substring(e.Data.Message.IndexOf(' ')).Trim();
  118.                 else
  119.                     msg = "";
  120.  
  121.                 //Console.WriteLine(cmd + " : " + msg);
  122.                 Server.Log(cmd + " : " + msg);
  123.                 switch (cmd)
  124.                 {
  125.                     case "kick":
  126.                         Command.all.Find("kick").Use(null, msg); break;
  127.                     case "ban":
  128.                         Command.all.Find("ban").Use(null, msg); break;
  129.                     case "banip":
  130.                         Command.all.Find("banip").Use(null, msg); break;
  131.                     case "guest":
  132.                         Command.all.Find("guest").Use(null, msg); break;
  133.                     case "builder":
  134.                         Command.all.Find("builder").Use(null, msg); break;
  135.                     case "say":
  136.                         irc.SendMessage(SendType.Message, channel, msg); break;
  137.                     default:
  138.                         irc.SendMessage(SendType.CtcpReply, e.Data.Nick, "Fail No Such Command"); break;
  139.                 }
  140.             }
  141.         }
  142.         void OnNickChange(object sender, NickChangeEventArgs e)
  143.         {
  144.             string key;
  145.             if (e.NewNickname.Split('|').Length == 2)
  146.             {
  147.                 key = e.NewNickname.Split('|')[1];
  148.                 if (key != null && key != "")
  149.                 {
  150.                     switch (key)
  151.                     {
  152.                         case "AFK":
  153.                             Player.GlobalMessage("IRC: " + e.OldNickname + " is AFK"); Server.afkset.Add(e.OldNickname); break;
  154.                         case "Away":
  155.                             Player.GlobalMessage("IRC: " + e.OldNickname + " is Away"); Server.afkset.Add(e.OldNickname); break;
  156.                     }
  157.                 }
  158.             }
  159.             else if (Server.afkset.Contains(e.NewNickname))
  160.             {
  161.                 Player.GlobalMessage("IRC: " + e.NewNickname + " is no longer away");
  162.                 Server.afkset.Remove(e.NewNickname);
  163.             }
  164.             else
  165.                 Player.GlobalMessage("IRC: " + e.OldNickname + " is now known as " + e.NewNickname);
  166.  
  167.             irc.RfcNames(channel);
  168.         }
  169.         void OnAction(object sender, ActionEventArgs e)
  170.         {
  171.             Player.GlobalMessage("* " + e.Data.Nick + " " + e.ActionMessage);
  172.         }
  173.        
  174.        
  175.         /// <summary>
  176.         /// A simple say method for use outside the bot class
  177.         /// </summary>
  178.         /// <param name="msg">what to send</param>
  179.         public static void Say(string msg)
  180.         {
  181.             if (irc != null && irc.IsConnected && Properties.irc)
  182.                 irc.SendMessage(SendType.Message, channel, msg);
  183.         }
  184.         public static bool IsConnected()
  185.         {
  186.             if (irc.IsConnected)
  187.                 return true;
  188.             else
  189.                 return false;
  190.         }
  191.         public static void Reset()
  192.         {
  193.             if (irc.IsConnected)
  194.                 irc.Disconnect();
  195.             ircThread = new Thread(new ThreadStart(delegate
  196.             {
  197.                 try { irc.Connect(server, 6667); }
  198.                 catch (Exception e)
  199.                 {
  200.                     Server.Log("Error Connecting to IRC");
  201.                     Server.Log(e.ToString());
  202.                 }
  203.             }));
  204.             ircThread.Start();
  205.         }
  206.         public static string[] GetConnectedUsers()
  207.         {
  208.             return names;
  209.         }
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement