Advertisement
Guest User

Untitled

a guest
May 1st, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Net;
  10. using System.Threading;
  11. using System.Net.Sockets;
  12. using System.Windows.Forms;
  13.  
  14. namespace WindowsFormsApplication1
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         class MySocket
  24.         {
  25.             public static ArrayList SERVER_1 = new ArrayList();
  26.             public static int totalBytesReceived;
  27.             public static string SERV;
  28.             public static int PORT = 6667;
  29.             public static string NICK;
  30.             public static string PASS;
  31.             public static string USER;
  32.             public static string REAL;
  33.             public static byte[] receivedBytes = new byte[10240];
  34.             public static IPHostEntry ipHost;
  35.             public static IPAddress ipAddress;
  36.             public static IPEndPoint ipEndPoint;
  37.             public static Socket sender = new Socket(AddressFamily.InterNetwork,
  38.                   SocketType.Stream,
  39.                   ProtocolType.Tcp);
  40.         }
  41.  
  42.  
  43.         private static void Write_Line(String text)
  44.         {
  45.             Form1 Form1 = new Form1();
  46.            Form1.textBox1.AppendText(text + "\r\n");
  47.         }
  48.  
  49.          class CTCP
  50.          {
  51.              public static void VER(string text)
  52.              {
  53.                  string SENDER = text.Substring(text.IndexOf(":") + 1, text.IndexOf("!") - 1);
  54.                  MySocket.sender.Send(Encoding.ASCII.GetBytes("NOTICE " + SENDER + " :VERSION R-IRCv0.0 " + System.Environment.OSVersion.ToString() + "\x0D\x0A"));
  55.              }
  56.              public static void TIME(string text)
  57.              {
  58.                  string SENDER = text.Substring(text.IndexOf(":") + 1, text.IndexOf("!") - 1);
  59.                  MySocket.sender.Send(Encoding.ASCII.GetBytes("NOTICE " + SENDER + " : " + System.DateTime.Now.ToString() + "\x0D\x0A"));
  60.              }
  61.             public static void PING(string text)
  62.             {
  63.                
  64.                         Write_Line("Obtained ping request!\r\n" + "Replying...");
  65.                         int len = text.Length;
  66.                         string POC = text.Substring(Encoding.ASCII.GetString(MySocket.receivedBytes, 0, MySocket.totalBytesReceived).IndexOf(":") + 1);
  67.                         Write_Line("PONG: " + POC + "\x0D\x0A");
  68.                         MySocket.sender.Send(Encoding.ASCII.GetBytes("PONG :" + POC + "\x0D\x0A"));
  69.             }
  70.  
  71.         }
  72.  
  73.         private void display_AppData()
  74.         {
  75.             Write_Line(
  76. "MMMMMMMMMMMMO                MMMM    MMMMMMMMMMMM?           MMMMMMMM        \r\n" +
  77. "MMMMMMMMMMMMMMM              MMMM    MMMMMMMMMMMMMMM      ~MMMMMMMMMMMM      \r\n" +
  78. "MMMM       MMMMM             MMMM    MMMM       MMMM+    NMMMM     MMMMM     \r\n" +
  79. "MMMM        MMMM             MMMM    MMMM        MMMM   ~MMMM        MMMM    \r\n" +
  80. "MMMM        MMMM             MMMM    MMMM       ,MMMM   MMMM                 \r\n" +
  81. "MMMMMMMMMMMMMMM,             MMMM    MMMMMMMMMMMMMMM    MMMM                 \r\n" +
  82. "MMMMMMMMMMMMMM               MMMM    MMMMMMMMMMMMM?     MMMM                 \r\n" +
  83. "MMMM   MMMMM       MMMMMMMM  MMMM    MMMM  :MMMMM       MMMM                 \r\n" +
  84. "MMMM     MMMMM     MMMMMMMM  MMMM    MMMM    =MMMM      MMMM         MMM     \r\n" +
  85. "MMMM      MMMMM              MMMM    MMMM      MMMM      MMMM       MMMMM    \r\n" +
  86. "MMMM       MMMMM             MMMM    MMMM       MMMM     ,MMMMM7 :MMMMMM     \r\n" +
  87. "MMMM        MMMMN            MMMM    MMMM        MMMM,     MMMMMMMMMMMO      \r\n" +
  88. "++++         ++++=           ++++    ++++         ++++        7MMMM?         \r\n\r\n"+
  89. "#########                       ########                    ####      ### \r\n" +
  90. "##     ###     #####   ##   ##  ##        #####   ##   ##  ##   #    #### \r\n" +
  91. "#########     ##   ##   ## ##   #####    ##   ##   ## ##   ####     ## ## \r\n" +
  92. "##    ###     ##   ##    ###    ##       ##   ##    ###    ##  ##  #######\r\n" +
  93. "##     ###    ##   ##   ## ##   ##       ##   ##   ## ##   ##  ##      ## \r\n" +
  94. "##      ###    #####   ##   ##  ##        #####   ##   ##   ####       ## \r\n"
  95.             );
  96.         }
  97.  
  98.         private int connect()
  99.         {
  100.             MySocket.SERV = textBox3.Text;
  101.             MySocket.PORT = Convert.ToInt32(numericUpDown1.Value);
  102.             MySocket.NICK = textBox5.Text;
  103.             MySocket.USER = textBox7.Text;
  104.             MySocket.PASS = textBox4.Text;
  105.  
  106.             Write_Line("Resolving Host...");
  107.             try
  108.             {
  109.                 MySocket.ipHost = Dns.Resolve(MySocket.SERV);
  110.             }
  111.             catch (Exception ex)
  112.             {
  113.                 Write_Line("Error: " + ex.Message);
  114.                 return 0;
  115.  
  116.             }
  117.             Write_Line("Looking up Network");
  118.             MySocket.ipAddress = MySocket.ipHost.AddressList[0];
  119.             MySocket.ipEndPoint = new IPEndPoint(MySocket.ipAddress, MySocket.PORT);
  120.  
  121.             Write_Line("Connecting...");
  122.             try
  123.             {
  124.                 MySocket.sender.Connect(MySocket.ipEndPoint);
  125.             }
  126.             catch (Exception ex)
  127.             {
  128.                 Write_Line(ex.Message);
  129.                 return 0;
  130.             }
  131.             Write_Line("Connected!\r\n");
  132.             return 1;
  133.  
  134.  
  135.         }
  136.  
  137.         private void Register()
  138.         {
  139.             if (MySocket.sender.Connected)
  140.             {
  141.                 if (MySocket.PASS.Length > 0)
  142.                 {
  143.                     byte[] PASS = Encoding.ASCII.GetBytes("PASS " + MySocket.PASS + "\x0D\x0A");
  144.                     MySocket.sender.Send(PASS);
  145.                 }
  146.                 byte[] NICK = Encoding.ASCII.GetBytes("NICK " + MySocket.NICK + "\x0D\x0A");
  147.                 MySocket.sender.Send(NICK);
  148.                 byte[] USER = Encoding.ASCII.GetBytes("USER " + MySocket.USER + " 3 * :R-IRC User" + "\x0D\x0A");
  149.                 MySocket.sender.Send(USER);
  150.  
  151.             }
  152.         }
  153.  
  154.         private void Update()
  155.         {
  156.             if (MySocket.sender.Connected)
  157.             {
  158.                 if (MySocket.sender.Available > 0)
  159.                 {
  160.                     string LINE;
  161.                     string SENDER;
  162.                     string MSG;
  163.  
  164.                     MySocket.totalBytesReceived = MySocket.sender.Receive(MySocket.receivedBytes);
  165.                     LINE = Encoding.ASCII.GetString(MySocket.receivedBytes, 0, MySocket.totalBytesReceived);
  166.  
  167.                     if (Encoding.ASCII.GetString(MySocket.receivedBytes, 0, MySocket.totalBytesReceived).Contains("PING :"))
  168.                     {
  169.                         Write_Line("Obtained ping request!\r\n" + "Replying...");
  170.                         int len = Encoding.ASCII.GetString(MySocket.receivedBytes, 0, MySocket.totalBytesReceived).Length;
  171.                         string POC = Encoding.ASCII.GetString(MySocket.receivedBytes, 0, MySocket.totalBytesReceived).Substring(Encoding.ASCII.GetString(MySocket.receivedBytes, 0, MySocket.totalBytesReceived).IndexOf(":") + 1);
  172.                         Write_Line("PONG: " + POC + "\x0D\x0A");
  173.                         MySocket.sender.Send(Encoding.ASCII.GetBytes("PONG :" + POC + "\x0D\x0A"));
  174.                     }
  175.  
  176.                     if (Encoding.ASCII.GetString(MySocket.receivedBytes, 0, MySocket.totalBytesReceived).Contains("PRIVMSG"))
  177.                     {
  178.                         SENDER = LINE.Substring(LINE.IndexOf(":") + 1, LINE.IndexOf("!") - 1);
  179.                         MSG = LINE.Substring(LINE.IndexOf(":",LINE.IndexOf(":")+1) + 1);
  180.                         if (LINE.Contains("VERSION"))
  181.                         {
  182.                             CTCP.VER(LINE);
  183.                         }
  184.                         if (LINE.Contains("PING"))
  185.                         {
  186.                             CTCP.PING(LINE);
  187.                         }
  188.                         else
  189.                         {
  190.                             Write_Line(SENDER + ": " + MSG);
  191.                         }
  192.                     }
  193.                     else
  194.                     {
  195.                         Write_Line(Encoding.ASCII.GetString(MySocket.receivedBytes, 0, MySocket.totalBytesReceived));
  196.                     }
  197.                 }
  198.                    
  199.             }
  200.         }
  201.  
  202.         private void Scan()
  203.         {
  204.             while (MySocket.sender.Connected)
  205.             {
  206.                 Update();
  207.                 Thread.Sleep(3000);
  208.             }
  209.  
  210.         }
  211.  
  212.  
  213.         private void button2_Click(object sender, EventArgs e)
  214.         {
  215.             if (textBox2.Text != "")
  216.             {
  217.                 send_msg(textBox2.Text);
  218.                 textBox2.Clear();
  219.             }
  220.         }
  221.  
  222.         private void button1_Click(object sender, EventArgs e)
  223.         {
  224.             connect();
  225.             Register();
  226.             timer1.Start();
  227.             button1.Enabled = false;
  228.         }
  229.  
  230.         private void Form1_Load(object sender, EventArgs e)
  231.         {
  232.             display_AppData();
  233.         }
  234.  
  235.         private void button3_Click(object sender, EventArgs e)
  236.         {
  237.             timer1.Stop();
  238.             if (MySocket.sender.Connected)
  239.             {
  240.                 MySocket.sender.Send(Encoding.ASCII.GetBytes("QUIT PROJECT R-IRC IS GO!!! \x0D\x0A"));
  241.                 MySocket.sender.Disconnect(true);
  242.  
  243.             }
  244.         }
  245.  
  246.         private void timer1_Tick(object sender, EventArgs e)
  247.         {
  248.             Update();
  249.         }
  250.  
  251.         private void send_msg(string text)
  252.         {
  253.                 if (text.Substring(1, text.IndexOf(" ")-1).ToUpper() == "JOIN")
  254.                 {
  255.                     MySocket.sender.Send(
  256.                     Encoding.ASCII.GetBytes(
  257.                     text.Substring(1, text.IndexOf(" ")) +
  258.                     text.Substring(text.IndexOf(" "))
  259.                     + "\x0D\x0A"
  260.                     ));
  261.                     Write_Line(
  262.                     text.Substring(1, text.IndexOf(" ")) +
  263.                     text.Substring(text.IndexOf(" "))
  264.                     + "\x0D\x0A");
  265.                    
  266.                 }
  267.                 else if (text.Substring(1, text.IndexOf(" ")-1).ToUpper() == "MSG")
  268.                 {
  269.                     string TARGET = text.Substring(text.IndexOf(" ")+1, text.IndexOf(" ", text.IndexOf(" ") + 1) - text.IndexOf(" "));
  270.                     string MSG = text.Substring(text.IndexOf(" ", text.IndexOf(" ") + 1) + 1);
  271.                     MySocket.sender.Send(
  272.                     Encoding.ASCII.GetBytes(
  273.                     "PRIVMSG " +
  274.                     TARGET + " :" +
  275.                     MSG + "\x0D\x0A"
  276.                     ));
  277.  
  278.                     Write_Line(
  279.                     "PRIVMSG " +
  280.                     TARGET + " :" +
  281.                     MSG + "\r\n");
  282.  
  283.                 }
  284.                 else
  285.                 {
  286.                     MySocket.sender.Send(
  287. Encoding.ASCII.GetBytes(
  288. text.Substring(1, text.IndexOf(" ")) +
  289. text.Substring(text.IndexOf(" "))
  290. + "\x0D\x0A"
  291. ));
  292.                     Write_Line(
  293.                     text.Substring(1, text.IndexOf(" ")) +
  294.                     text.Substring(text.IndexOf(" "))
  295.                     + "\x0D\x0A");
  296.                 }
  297.         }
  298.  
  299.         private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
  300.         {
  301.             if (e.KeyChar == 13)
  302.             {
  303.                 if (textBox2.Text != "")
  304.                 {
  305.                     send_msg(textBox2.Text);
  306.                     textBox2.Clear();
  307.                 }
  308.             }
  309.         }
  310.  
  311.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  312.         {
  313.             timer1.Stop();
  314.             if (MySocket.sender.Connected)
  315.             {
  316.                 MySocket.sender.Send(Encoding.ASCII.GetBytes("QUIT PROJECT R-IRC IS GO!!! \x0D\x0A"));
  317.                 MySocket.sender.Disconnect(true);
  318.             }
  319.         }
  320.  
  321.         private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
  322.         {
  323.  
  324.         }
  325.  
  326.         private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
  327.         {
  328.             System.Diagnostics.Process.Start(e.LinkText);
  329.         }
  330.  
  331.         private void tableLayoutPanel2_Paint(object sender, PaintEventArgs e)
  332.         {
  333.  
  334.         }
  335.  
  336.         private void tableLayoutPanel3_Paint(object sender, PaintEventArgs e)
  337.         {
  338.  
  339.         }
  340.     }
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement