Advertisement
Guest User

my IRC bot

a guest
Mar 23rd, 2015
1,591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ircclient
  4. {
  5. class MainClass
  6. {
  7. public static void Main(string[] args)
  8. {
  9. int port;
  10. string buf, nick, owner, server, chan;
  11. System.Net.Sockets.TcpClient sock = new System.Net.Sockets.TcpClient();
  12. System.IO.TextReader input;
  13. System.IO.TextWriter output;
  14.  
  15. //Get nick, owner, server, port, and channel from user
  16. Console.Write("Enter bot nick: ");
  17. nick = Console.ReadLine();
  18. Console.Write("Enter bot owner name: ");
  19. owner = Console.ReadLine();
  20. Console.Write("Enter server name: ");
  21. server = Console.ReadLine();
  22. Console.Write("Enter port number: ");
  23. port = Convert.ToInt32(Console.ReadLine());
  24. Console.Write("Channel: ");
  25. chan = Console.ReadLine();
  26.  
  27. //Connect to irc server and get input and output text streams from TcpClient.
  28. sock.Connect(server, port);
  29. if (!sock.Connected)
  30. {
  31. Console.WriteLine("Failed to connect!");
  32. return;
  33. }
  34. input = new System.IO.StreamReader(sock.GetStream());
  35. output = new System.IO.StreamWriter(sock.GetStream());
  36.  
  37. //Starting USER and NICK login commands
  38. output.Write(
  39. "USER " + nick + " 0 * :" + owner + "\r\n" +
  40. "NICK " + nick + "\r\n"
  41. );
  42. output.Flush();
  43.  
  44. //Process each line received from irc server
  45. for (buf = input.ReadLine(); ; buf = input.ReadLine())
  46. {
  47.  
  48. //Display received irc message
  49. Console.WriteLine(buf);
  50.  
  51. //Send pong reply to any ping messages
  52. if (buf.StartsWith("PING ")) { output.Write(buf.Replace("PING", "PONG") + "\r\n"); output.Flush(); }
  53. if (buf[0] != ':') continue;
  54.  
  55. /* IRC commands come in one of these formats:
  56. * :NICK!USER@HOST COMMAND ARGS ... :DATA\r\n
  57. * :SERVER COMAND ARGS ... :DATA\r\n
  58. */
  59.  
  60. //After server sends 001 command, we can set mode to bot and join a channel
  61. if (buf.Split(' ')[1] == "001")
  62. {
  63. output.Write(
  64. "MODE " + nick + " +B\r\n" +
  65. "JOIN " + chan + "\r\n"
  66. );
  67. output.Flush();
  68. }
  69. }
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement