PsyOps

IRCMain.cs

Nov 6th, 2013
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using Meebey.SmartIrc4net;
  7. using BattleCore;
  8. using BattleCore.Events;
  9. using BattleCorePsyOps;
  10. using System.Collections;
  11. using System.Threading;
  12.  
  13. namespace IrcBridge
  14. {
  15.     // Add the attribute and the base class
  16.     [Behavior("IRCBMain", "true", "0.01", "PsyOps", "Irc bridge. SS & IRC.")]
  17.  
  18.     public class IRCBMain: BotEventListener
  19.     {
  20.         public IRCBMain()
  21.         {
  22.             RegisterCommand("!irc", Main);
  23.         }
  24.  
  25.         // make an instance of the high-level API
  26.         public static IrcClient irc = new IrcClient();
  27.  
  28.         Queue<ChatEvent> ChatQ = new Queue<ChatEvent>();
  29.         ShortChat msg = new ShortChat();
  30.  
  31.         public void HandleSSChat(object sender, ChatEvent c)
  32.         {
  33.             if (c.PlayerName == "PsyOps")
  34.                 ChatQ.Enqueue(c);
  35.         }
  36.  
  37.         // this method we will use to analyse queries (also known as private messages)
  38.         public static void OnQueryMessage(object sender, IrcEventArgs e)
  39.         {
  40.             switch (e.Data.MessageArray[0])
  41.             {
  42.                 // debug stuff
  43.                 case "dump_channel":
  44.                     string requested_channel = e.Data.MessageArray[1];
  45.                     // getting the channel (via channel sync feature)
  46.                     Channel channel = irc.GetChannel(requested_channel);
  47.  
  48.                     // here we send messages
  49.                     irc.SendMessage(SendType.Message, e.Data.Nick, "<channel '" + requested_channel + "'>");
  50.  
  51.                     irc.SendMessage(SendType.Message, e.Data.Nick, "Name: '" + channel.Name + "'");
  52.                     irc.SendMessage(SendType.Message, e.Data.Nick, "Topic: '" + channel.Topic + "'");
  53.                     irc.SendMessage(SendType.Message, e.Data.Nick, "Mode: '" + channel.Mode + "'");
  54.                     irc.SendMessage(SendType.Message, e.Data.Nick, "Key: '" + channel.Key + "'");
  55.                     irc.SendMessage(SendType.Message, e.Data.Nick, "UserLimit: '" + channel.UserLimit + "'");
  56.  
  57.                     // here we go through all users of the channel and show their
  58.                     // hashtable key and nickname
  59.                     string nickname_list = "";
  60.                     nickname_list += "Users: ";
  61.                     foreach (DictionaryEntry de in channel.Users)
  62.                     {
  63.                         string key = (string)de.Key;
  64.                         ChannelUser channeluser = (ChannelUser)de.Value;
  65.                         nickname_list += "(";
  66.                         if (channeluser.IsOp)
  67.                         {
  68.                             nickname_list += "@";
  69.                         }
  70.                         if (channeluser.IsVoice)
  71.                         {
  72.                             nickname_list += "+";
  73.                         }
  74.                         nickname_list += ")" + key + " => " + channeluser.Nick + ", ";
  75.                     }
  76.                     irc.SendMessage(SendType.Message, e.Data.Nick, nickname_list);
  77.  
  78.                     irc.SendMessage(SendType.Message, e.Data.Nick, "</channel>");
  79.                     break;
  80.                 case "gc":
  81.                     GC.Collect();
  82.                     break;
  83.                 // typical commands
  84.                 case "join":
  85.                     irc.RfcJoin(e.Data.MessageArray[1]);
  86.                     break;
  87.                 case "part":
  88.                     irc.RfcPart(e.Data.MessageArray[1]);
  89.                     break;
  90.                 case "die":
  91.                     Exit();
  92.                     break;
  93.             }
  94.         }
  95.  
  96.         // this method handles when we receive "ERROR" from the IRC server
  97.         public static void OnError(object sender, ErrorEventArgs e)
  98.         {
  99.             System.Console.WriteLine("Error: " + e.ErrorMessage);
  100.             Exit();
  101.         }
  102.  
  103.         // this method will get all IRC messages
  104.         public void OnRawMessage(object sender, IrcEventArgs e)
  105.         {
  106.             //System.Console.WriteLine("Received: " + e.Data.RawMessage);
  107.             Game(msg.arena("Message received: " + e.Data.RawMessage));
  108.         }
  109.  
  110.         public void Main(ChatEvent c)
  111.         {
  112.             Thread.CurrentThread.Name = "Main";
  113.  
  114.             // UTF-8 test
  115.             irc.Encoding = System.Text.Encoding.UTF8;
  116.  
  117.             // wait time between messages, we can set this lower on own irc servers
  118.             irc.SendDelay = 200;
  119.  
  120.             // we use channel sync, means we can use irc.GetChannel() and so on
  121.             irc.ActiveChannelSyncing = true;
  122.  
  123.             // here we connect the events of the API to our written methods
  124.             // most have own event handler types, because they ship different data
  125.             irc.OnQueryMessage += new IrcEventHandler(OnQueryMessage);
  126.             irc.OnError += new ErrorEventHandler(OnError);
  127.             irc.OnRawMessage += new IrcEventHandler(OnRawMessage);
  128.  
  129.             string[] serverlist;
  130.             // the server we want to connect to, could be also a simple string
  131.             serverlist = new string[] { "irc.mibbit.net" };
  132.             int port = 6667;
  133.             string channel = "#msguild";
  134.             try
  135.             {
  136.                 // here we try to connect to the server and exceptions get handled
  137.                 irc.Connect(serverlist, port);
  138.             }
  139.             catch (ConnectionException e)
  140.             {
  141.                 // something went wrong, the reason will be shown
  142.                 System.Console.WriteLine("couldn't connect! Reason: " + e.Message);
  143.                 Exit();
  144.             }
  145.  
  146.             try
  147.             {
  148.                 // here we logon and register our nickname and so on
  149.                 irc.Login("PsyBot", "Chat bridge- ss and irc");
  150.                 // join the channel
  151.                 irc.RfcJoin(channel);
  152.  
  153.                 //for (int i = 0; i < 3; i++)
  154.                 //{
  155.                 //    // here we send just 3 different types of messages, 3 times for
  156.                 //    // testing the delay and flood protection (messagebuffer work)
  157.                 //    irc.SendMessage(SendType.Message, channel, "test message (" + i.ToString() + ")");
  158.                 //    irc.SendMessage(SendType.Action, channel, "thinks this is cool (" + i.ToString() + ")");
  159.                 //    irc.SendMessage(SendType.Notice, channel, "SmartIrc4net rocks (" + i.ToString() + ")");
  160.                 //}
  161.  
  162.                 // spawn a new thread to read the stdin of the console, this we use
  163.                 // for reading IRC commands from the keyboard while the IRC connection
  164.                 // stays in its own thread
  165.                 new Thread(new ThreadStart(ReadCommands)).Start();
  166.  
  167.                 // here we tell the IRC API to go into a receive mode, all events
  168.                 // will be triggered by _this_ thread (main thread in this case)
  169.                 // Listen() blocks by default, you can also use ListenOnce() if you
  170.                 // need that does one IRC operation and then returns, so you need then
  171.                 // an own loop
  172.                 irc.Listen();
  173.  
  174.                 // when Listen() returns our IRC session is over, to be sure we call
  175.                 // disconnect manually
  176.                 irc.Disconnect();
  177.             }
  178.             catch (ConnectionException)
  179.             {
  180.                 // this exception is handled because Disconnect() can throw a not
  181.                 // connected exception
  182.                 Exit();
  183.             }
  184.             catch (Exception e)
  185.             {
  186.                 // this should not happen by just in case we handle it nicely
  187.                 System.Console.WriteLine("Error occurred! Message: " + e.Message);
  188.                 System.Console.WriteLine("Exception: " + e.StackTrace);
  189.                 Exit();
  190.             }
  191.         }
  192.  
  193.         public static void ReadCommands()
  194.         {
  195.             // here we read the commands from the stdin and send it to the IRC API
  196.             // WARNING, it uses WriteLine() means you need to enter RFC commands
  197.             // like "JOIN #test" and then "PRIVMSG #test :hello to you"
  198.             while (true)
  199.             {
  200.  
  201.                 string cmd = System.Console.ReadLine();
  202.                 if (cmd != null && cmd.StartsWith("/list"))
  203.                 {
  204.                     int pos = cmd.IndexOf(" ");
  205.                     string channel = null;
  206.                     if (pos != -1)
  207.                     {
  208.                         channel = cmd.Substring(pos + 1);
  209.                     }
  210.  
  211.                     IList<ChannelInfo> channelInfos = irc.GetChannelList(channel);
  212.                     Console.WriteLine("channel count: {0}", channelInfos.Count);
  213.                     foreach (ChannelInfo channelInfo in channelInfos)
  214.                     {
  215.                         Console.WriteLine("channel: {0} user count: {1} topic: {2}",
  216.                                           channelInfo.Channel,
  217.                                           channelInfo.UserCount,
  218.                                           channelInfo.Topic);
  219.                     }
  220.                 }
  221.                 else
  222.                 {
  223.                     irc.WriteLine(cmd);
  224.                 }
  225.             }
  226.         }
  227.  
  228.         public static void Exit()
  229.         {
  230.             // we are done, lets exit...
  231.             System.Console.WriteLine("Exiting...");
  232.             System.Environment.Exit(0);
  233.         }
  234.         public override void Dispose()
  235.         {
  236.             throw new NotImplementedException();
  237.         }
  238.     }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment