PsyOps

IRCBot.cs

Nov 4th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Sockets;
  6. using System.IO;
  7.  
  8. namespace IRCBot
  9. {
  10.     struct IRCConfig
  11.     {
  12.         public string server;
  13.         public int port;
  14.         public string nick;
  15.         public string name;
  16.  
  17.     }
  18.     class IRCBot
  19.     {
  20.         TcpClient IRCConnection = null;
  21.         IRCConfig config;
  22.         NetworkStream ns = null;
  23.         StreamReader sr = null;
  24.         StreamWriter sw = null;
  25.  
  26.         public IRCBot(IRCConfig config)
  27.         {
  28.             this.config = config;
  29.             try
  30.             {
  31.                 IRCConnection = new TcpClient(config.server, config.port);
  32.                 sendData("USER", config.nick + " swivvet.com " + " swivvet.com" + " :" + config.name);
  33.                 sendData("NICK", config.nick);
  34.             }
  35.             catch
  36.             {
  37.                 Console.WriteLine("Connection Error");
  38.             }
  39.  
  40.             try
  41.             {
  42.                 ns = IRCConnection.GetStream();
  43.                 sr = new StreamReader(ns);
  44.                 sw = new StreamWriter(ns);
  45.                 sendData("USER", config.nick + " swivvet.com " + " swivvet.com" + " :" + config.name);
  46.                 sendData("NICK", config.nick);
  47.             }
  48.             catch
  49.             {
  50.                 Console.WriteLine("Communication error");
  51.             }
  52.             finally
  53.             {
  54.                 if (sr != null)
  55.                     sr.Close();
  56.                 if (sw != null)
  57.                     sw.Close();
  58.                 if (ns != null)
  59.                     ns.Close();
  60.                 if (IRCConnection != null)
  61.                     IRCConnection.Close();
  62.             }
  63.         }
  64.         public void sendData(string cmd, string param)
  65.         {
  66.             if (param == null)
  67.             {
  68.                 sw.WriteLine(cmd);
  69.                 sw.Flush();
  70.                 Console.WriteLine(cmd);
  71.             }
  72.             else
  73.             {
  74.                 sw.WriteLine(cmd + " " + param);
  75.                 sw.Flush();
  76.                 Console.WriteLine(cmd + " " + param);
  77.             }
  78.         }
  79.         public void IRCWork()
  80.         {
  81.             string[] ex;
  82.             string data;
  83.             bool shouldRun = true;
  84.             while (shouldRun)
  85.             {
  86.                 data = sr.ReadLine();
  87.                 Console.WriteLine(data);
  88.                 char[] charSeparator = new char[] { ' ' };
  89.                 ex = data.Split(charSeparator, 5);
  90.  
  91.                 if (ex[0] == "PING")
  92.                 {
  93.                     sendData("PONG", ex[1]);
  94.                 }
  95.  
  96.                 if (ex.Length > 4) //is the command received long enough to be a bot command?
  97.                 {
  98.                     string command = ex[3]; //grab the command sent
  99.  
  100.                     switch (command)
  101.                     {
  102.                         case ":!join":
  103.                             sendData("JOIN", ex[4]); //if the command is !join send the "JOIN" command to the server with the parameters set by the user
  104.                             break;
  105.                         case ":!say":
  106.                             sendData("PRIVMSG", ex[2] + " " + ex[4]); //if the command is !say, send a message to the chan (ex[2]) followed by the actual message (ex[4]).
  107.                             break;
  108.                         case ":!quit":
  109.                             sendData("QUIT", ex[4]); //if the command is quit, send the QUIT command to the server with a quit message
  110.                             shouldRun = false; //turn shouldRun to false - the server will stop sending us data so trying to read it will not work and result in an error. This stops the loop from running and we will close off the connections properly
  111.                             break;
  112.                     }
  113.                 }
  114.                 if (ex.Length > 3)
  115.                 {
  116.                     string command = ex[3];
  117.  
  118.                     switch (command)
  119.                     {
  120.                         case ":!part":
  121.                             sendData("PART", ex[2]);
  122.                             break;
  123.                     }
  124.                 }
  125.             }
  126.         }
  127.     }
  128.     class Program
  129.     {
  130.         static void Main(string[] args)
  131.         {
  132.             IRCConfig conf = new IRCConfig();
  133.             conf.name = "PsyBot";
  134.             conf.nick = "PsyBot";
  135.             conf.port = 6667;
  136.             conf.server = "irc.uk.mibbit.net";
  137.             new IRCBot(conf);
  138.             Console.WriteLine("Bot quit/crashed");
  139.             Console.ReadLine();
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment