Advertisement
timeshifter

Untitled

Apr 17th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.10 KB | None | 0 0
  1.     public static TcpClient socket;
  2.     public static StreamReader reader;
  3.     public static StreamWriter writer;
  4.  
  5.     static void Main() {
  6.         /* Attempt to connect to IRC server */
  7.  
  8.         try {
  9.             socket = new TcpClient(hostname, port);
  10.             socket.ReceiveBufferSize = 1024;
  11.             Console.WriteLine("Connected");
  12.             NetworkStream stream = socket.GetStream();
  13.             reader = new StreamReader(stream);
  14.             writer = new StreamWriter(stream);
  15.             write("USER " + username + " 8 * :" + description);
  16.             write("NICK " + username);
  17.             write("PRIVMSG NickServ :identify " + password);
  18.             read(reader);
  19.             reader.Close();
  20.             writer.Close();
  21.             stream.Close();
  22.         }
  23.         catch {
  24.             Console.WriteLine("Failed to connect");
  25.         }
  26.     }
  27.  
  28.     /* Reading data from the IRC server */
  29.     static void read(StreamReader reader) {
  30.         try {
  31.             while (running) {
  32.                 interpret(reader.ReadLine());
  33.             }
  34.         }
  35.         catch (Exception ex) {
  36.             Console.WriteLine("Unable to read from server");
  37.         }
  38.     }
  39.  
  40.  
  41.     /* Interpreting data from the IRC server */
  42.     static void interpret(String data) {
  43.         Console.WriteLine(data);
  44.         string[] interpretData = data.Split(' ');
  45.         string text = "";
  46.         for (int i = 3; i < interpretData.Length; i++)
  47.             text += interpretData[i] + " ";
  48.         if (interpretData[0].Equals("PING"))
  49.             onPing(interpretData[1]);
  50.         else if (interpretData[1] == "JOIN") {
  51.             string user = interpretData[0].Split('!')[0].Substring(1);
  52.             if (user == username) {
  53.                 string chnl = interpretData[2].Substring(1);
  54.                 channelList.Add(chnl);
  55.                 write("PRIVMSG timeshifter :I've joined " + chnl);
  56.                 uptimeList.Add(new UptimeObject(chnl));
  57.             }
  58.         }
  59.         else if (interpretData[1] == "PART") {
  60.             string chnl = interpretData[2];
  61.             string user = interpretData[0].Split('!')[0].Substring(1);
  62.             if (user == username) {
  63.                 channelList.Remove(chnl);
  64.                 write("PRIVMSG timeshifter :I've left " + interpretData[2]);
  65.                 int id = -1;
  66.                 for (int i = 0; i < uptimeList.Count; i++) {
  67.                     if (uptimeList[i].Channel == interpretData[2])
  68.                         id = i;
  69.                 }
  70.  
  71.                 uptimeList.RemoveAt(id);
  72.             }
  73.  
  74.         }
  75.         else if (interpretData[1].Equals("PRIVMSG")) {
  76.             // Public message in channel
  77.             if (interpretData[2].StartsWith("#")) {
  78.                 onPublicMessage(text, interpretData[2], interpretData[0]);
  79.             }
  80.             // Private message
  81.             else if (interpretData[2].Equals(username)) {
  82.                 onPrivateMessage(interpretData[0], text);
  83.             }
  84.         }
  85.         else if (interpretData[1] == "INVITE") {
  86.             //channelList.Add(interpretData[3].Substring(1));
  87.             write("JOIN " + interpretData[3].Substring(1));
  88.         }
  89.         else if (interpretData[1] == "NOTICE") {
  90.             if (text.StartsWith(":You are now identified for"))
  91.                 OnAuth();
  92.         }
  93.     }
  94.  
  95.     static void OnAuth() {
  96.         write("PRIVMSG timeshifter :I'm authenticated.");
  97.         //write("JOIN " + channel, writer);
  98.     }
  99.  
  100.     /* Writing data to the IRC server */
  101.     static void write(string data) {
  102.         try {
  103.             writer.WriteLine(data);
  104.             Console.WriteLine(">>> " + data);
  105.             writer.Flush();
  106.         }
  107.         catch {
  108.             Console.WriteLine("Error!");
  109.         }
  110.     }
  111.  
  112.     /* On ping request write pong back to the server */
  113.     static void onPing(string pong) {
  114.         pong = "PONG " + pong;
  115.         write(pong);
  116.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement