PsyOps

IRCMain.cs

Nov 5th, 2013
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.50 KB | None | 0 0
  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.  
  30.         public void HandleSSChat(object sender, ChatEvent c)
  31.         {
  32.             if (c.PlayerName == "PsyOps")
  33.                 ChatQ.Enqueue(c);
  34.         }
  35.  
  36.         // this method we will use to analyse queries (also known as private messages)
  37.         public static void OnQueryMessage(object sender, IrcEventArgs e)
  38.         {
  39.             switch (e.Data.MessageArray[0])
  40.             {
  41.                 // debug stuff
  42.                 case "dump_channel":
  43.                     string requested_channel = e.Data.MessageArray[1];
  44.                     // getting the channel (via channel sync feature)
  45.                     Channel channel = irc.GetChannel(requested_channel);
  46.  
  47.                     // here we send messages
  48.                     irc.SendMessage(SendType.Message, e.Data.Nick, "<channel '" + requested_channel + "'>");
  49.  
  50.                     irc.SendMessage(SendType.Message, e.Data.Nick, "Name: '" + channel.Name + "'");
  51.                     irc.SendMessage(SendType.Message, e.Data.Nick, "Topic: '" + channel.Topic + "'");
  52.                     irc.SendMessage(SendType.Message, e.Data.Nick, "Mode: '" + channel.Mode + "'");
  53.                     irc.SendMessage(SendType.Message, e.Data.Nick, "Key: '" + channel.Key + "'");
  54.                     irc.SendMessage(SendType.Message, e.Data.Nick, "UserLimit: '" + channel.UserLimit + "'");
  55.  
  56.                     // here we go through all users of the channel and show their
  57.                     // hashtable key and nickname
  58.                     string nickname_list = "";
  59.                     nickname_list += "Users: ";
  60.                     foreach (DictionaryEntry de in channel.Users)
  61.                     {
  62.                         string key = (string)de.Key;
  63.                         ChannelUser channeluser = (ChannelUser)de.Value;
  64.                         nickname_list += "(";
  65.                         if (channeluser.IsOp)
  66.                         {
  67.                             nickname_list += "@";
  68.                         }
  69.                         if (channeluser.IsVoice)
  70.                         {
  71.                             nickname_list += "+";
  72.                         }
  73.                         nickname_list += ")" + key + " => " + channeluser.Nick + ", ";
  74.                     }
  75.                     irc.SendMessage(SendType.Message, e.Data.Nick, nickname_list);
  76.  
  77.                     irc.SendMessage(SendType.Message, e.Data.Nick, "</channel>");
  78.                     break;
  79.                 case "gc":
  80.                     GC.Collect();
  81.                     break;
  82.                 // typical commands
  83.                 case "join":
  84.                     irc.RfcJoin(e.Data.MessageArray[1]);
  85.                     break;
  86.                 case "part":
  87.                     irc.RfcPart(e.Data.MessageArray[1]);
  88.                     break;
  89.                 case "die":
  90.                     Exit();
  91.                     break;
  92.             }
  93.         }
  94.  
  95.         // this method handles when we receive "ERROR" from the IRC server
  96.         public static void OnError(object sender, ErrorEventArgs e)
  97.         {
  98.             System.Console.WriteLine("Error: " + e.ErrorMessage);
  99.             Exit();
  100.         }
  101.  
  102.         // this method will get all IRC messages
  103.         public static void OnRawMessage(object sender, IrcEventArgs e)
  104.         {
  105.             System.Console.WriteLine("Received: " + e.Data.RawMessage);
  106.         }
  107.  
  108.         public void Main(ChatEvent c)
  109.         {
  110.             Thread.CurrentThread.Name = "Main";
  111.  
  112.             // UTF-8 test
  113.             irc.Encoding = System.Text.Encoding.UTF8;
  114.  
  115.             // wait time between messages, we can set this lower on own irc servers
  116.             irc.SendDelay = 200;
  117.  
  118.             // we use channel sync, means we can use irc.GetChannel() and so on
  119.             irc.ActiveChannelSyncing = true;
  120.  
  121.             // here we connect the events of the API to our written methods
  122.             // most have own event handler types, because they ship different data
  123.             irc.OnQueryMessage += new IrcEventHandler(OnQueryMessage);
  124.             irc.OnError += new ErrorEventHandler(OnError);
  125.             irc.OnRawMessage += new IrcEventHandler(OnRawMessage);
  126.  
  127.             string[] serverlist;
  128.             // the server we want to connect to, could be also a simple string
  129.             serverlist = new string[] { "irc.mibbit.net" };
  130.             int port = 6667;
  131.             string channel = "#msguild";
  132.             try
  133.             {
  134.                 // here we try to connect to the server and exceptions get handled
  135.                 irc.Connect(serverlist, port);
  136.             }
  137.             catch (ConnectionException e)
  138.             {
  139.                 // something went wrong, the reason will be shown
  140.                 System.Console.WriteLine("couldn't connect! Reason: " + e.Message);
  141.                 Exit();
  142.             }
  143.  
  144.             try
  145.             {
  146.                 // here we logon and register our nickname and so on
  147.                 irc.Login("PsyBot", "Chat bridge- ss and irc");
  148.                 // join the channel
  149.                 irc.RfcJoin(channel);
  150.  
  151.                 //for (int i = 0; i < 3; i++)
  152.                 //{
  153.                 //    // here we send just 3 different types of messages, 3 times for
  154.                 //    // testing the delay and flood protection (messagebuffer work)
  155.                 //    irc.SendMessage(SendType.Message, channel, "test message (" + i.ToString() + ")");
  156.                 //    irc.SendMessage(SendType.Action, channel, "thinks this is cool (" + i.ToString() + ")");
  157.                 //    irc.SendMessage(SendType.Notice, channel, "SmartIrc4net rocks (" + i.ToString() + ")");
  158.                 //}
  159.  
  160.                 // spawn a new thread to read the stdin of the console, this we use
  161.                 // for reading IRC commands from the keyboard while the IRC connection
  162.                 // stays in its own thread
  163.                 new Thread(new ThreadStart(ReadCommands)).Start();
  164.  
  165.                 // here we tell the IRC API to go into a receive mode, all events
  166.                 // will be triggered by _this_ thread (main thread in this case)
  167.                 // Listen() blocks by default, you can also use ListenOnce() if you
  168.                 // need that does one IRC operation and then returns, so you need then
  169.                 // an own loop
  170.                 irc.Listen();
  171.  
  172.                 // when Listen() returns our IRC session is over, to be sure we call
  173.                 // disconnect manually
  174.                 irc.Disconnect();
  175.             }
  176.             catch (ConnectionException)
  177.             {
  178.                 // this exception is handled because Disconnect() can throw a not
  179.                 // connected exception
  180.                 Exit();
  181.             }
  182.             catch (Exception e)
  183.             {
  184.                 // this should not happen by just in case we handle it nicely
  185.                 System.Console.WriteLine("Error occurred! Message: " + e.Message);
  186.                 System.Console.WriteLine("Exception: " + e.StackTrace);
  187.                 Exit();
  188.             }
  189.         }
  190.  
  191.         public void ReadCommands()
  192.         {
  193.             // here we read the commands from the stdin and send it to the IRC API
  194.             // WARNING, it uses WriteLine() means you need to enter RFC commands
  195.             // like "JOIN #test" and then "PRIVMSG #test :hello to you"
  196.             while (true)
  197.             {
  198.                 if (ChatQ.Count > 0)
  199.                     irc.SendMessage(SendType.Message, "#msguild", ChatQ.Dequeue().Message);
  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