Dessyreqt

IRC client written in C#

Jun 6th, 2012
538
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net.Sockets;
  5. using System.Threading;
  6.  
  7. namespace Mnemosyne.Irc
  8. {
  9.     public delegate void CommandReceived(string ircCommand);
  10.     public delegate void CommandSent(string ircCommand);
  11.     public delegate void TopicSet(string ircChannel, string ircTopic);
  12.     public delegate void TopicOwner(string ircChannel, string ircUser, string topicDate);
  13.     public delegate void NamesList(string userNames);
  14.     public delegate void ServerMessage(string serverMessage);
  15.     public delegate void Join(string ircChannel, string ircUser);
  16.     public delegate void Part(string ircChannel, string ircUser);
  17.     public delegate void Mode(string ircChannel, string ircUser, string userMode);
  18.     public delegate void NickChange(string userOldNick, string userNewNick);
  19.     public delegate void Kick(string ircChannel, string userKicker, string userKicked, string kickMessage);
  20.     public delegate void Quit(string userQuit, string quitMessage);
  21.     public delegate void Disconnected();
  22.  
  23.     public class IrcClient
  24.     {
  25.         public event CommandReceived CommandReceived;
  26.         public event CommandSent CommandSent;
  27.         public event TopicSet TopicSet;
  28.         public event TopicOwner TopicOwner;
  29.         public event NamesList NamesList;
  30.         public event ServerMessage ServerMessage;
  31.         public event Join Join;
  32.         public event Part Part;
  33.         public event Mode Mode;
  34.         public event NickChange NickChange;
  35.         public event Kick Kick;
  36.         public event Quit Quit;
  37.         public event Disconnected Disconnected;
  38.  
  39.         public string Server { get; set; }
  40.         public int Port { get; set; }
  41.         public string Nick { get; set; }
  42.         public string NickPass { get; set; }
  43.         public string User { get; set; }
  44.         public string RealName { get; set; }
  45.         public string Channel { get; set; }
  46.         public bool IsInvisble { get; set; }
  47.         public TcpClient Connection { get; set; }
  48.         public NetworkStream Stream { get; set; }
  49.         public StreamWriter Writer { get; set; }
  50.         public StreamReader Reader { get; set; }
  51.         public bool Connected { get; set; }
  52.         public Dictionary<string, List<string>> ChannelUsers { get; set; }
  53.  
  54.         private Thread listenThread;
  55.         private bool disconnecting;
  56.  
  57.         public IrcClient(string nick, string nickPass, string channel)
  58.         {
  59.             Nick = nick;
  60.             NickPass = nickPass;
  61.             User = nick;
  62.             RealName = "MnemosyneIrcBot";
  63.             Channel = channel;
  64.             IsInvisble = false;
  65.             Connected = false;
  66.             disconnecting = false;
  67.             ChannelUsers = new Dictionary<string, List<string>>();
  68.         }
  69.  
  70.         public void Connect(string ircServer, int ircPort)
  71.         {
  72.             Server = ircServer;
  73.             Port = ircPort;
  74.  
  75.             // Connect with the IRC server.
  76.             Connection = new TcpClient(Server, Port);
  77.             Stream = Connection.GetStream();
  78.             Reader = new StreamReader(Stream);
  79.             Writer = new StreamWriter(Stream);
  80.  
  81.             // Authenticate our user
  82.             string isInvisible = IsInvisble ? "8" : "0";
  83.             SendCommand(String.Format("USER {0} {1} * :{2}", User, isInvisible, RealName));
  84.             SetNick(Nick);
  85.  
  86.             Connected = true;
  87.  
  88.             // Listen for commands
  89.             listenThread = new Thread(Listen);
  90.             listenThread.Start();
  91.            
  92.         }
  93.                                                    
  94.         public void JoinChannel(string channel)
  95.         {
  96.             var channels = channel.Split(',');
  97.  
  98.             foreach (var chan in channels)
  99.             {
  100.                 SendCommand(String.Format("JOIN {0}", chan));
  101.             }
  102.         }
  103.  
  104.         public void LeaveChannel(string channel)
  105.         {
  106.             var channels = channel.Split(',');
  107.  
  108.             foreach (var chan in channels)
  109.             {
  110.                 SendCommand(String.Format("PART {0}", chan));
  111.             }
  112.         }
  113.  
  114.         public void GetChannelUsers(string channel)
  115.         {
  116.             if (!ChannelUsers.ContainsKey(channel))
  117.             {
  118.                 ChannelUsers.Add(channel, new List<string>());
  119.             }
  120.             else
  121.             {
  122.                 ChannelUsers[channel].Clear();                
  123.             }
  124.  
  125.             SendCommand(string.Format("NAMES {0}", channel));
  126.         }
  127.  
  128.         private void SetNick(string nick)
  129.         {
  130.             SendCommand(string.Format("NICK {0}", nick));
  131.         }
  132.  
  133.         private void SetPass(string nickPass)
  134.         {
  135.             SendCommand(string.Format("PRIVMSG NICKSERV IDENTIFY {0}", nickPass));
  136.         }
  137.  
  138.         public void GhostNick(string nick, string nickPass)
  139.         {
  140.             SetNick(nick + DateTime.Now.Ticks);
  141.             SendCommand(string.Format("PRIVMSG NICKSERV GHOST {0} {1}", nick, nickPass));
  142.             SetNick(nick);
  143.             SetNick(nickPass);
  144.         }
  145.  
  146.         public void Disconnect()
  147.         {
  148.             disconnecting = true;
  149.             Reader.Close();
  150.         }
  151.  
  152.         public void SendNotice(string user, string message)
  153.         {
  154.             SendCommand(string.Format("NOTICE {0} :{1}", user, message));
  155.         }
  156.  
  157.         public void SendMessage(string destination, string message)
  158.         {
  159.             SendCommand(string.Format("PRIVMSG {0} :{1}", destination, message));
  160.         }
  161.  
  162.         public void SendCommand(string ircCommand)
  163.         {
  164.             Writer.WriteLine(ircCommand);
  165.             Writer.Flush();
  166.             if (CommandSent != null)
  167.                 CommandSent(ircCommand);
  168.         }
  169.  
  170.         private void Listen()
  171.         {
  172.             while (!disconnecting)
  173.             {
  174.                 string ircCommand;
  175.                 try
  176.                 {
  177.  
  178.                     while (!disconnecting && (ircCommand = Reader.ReadLine()) != null)
  179.                     {
  180.                         if (CommandReceived != null)
  181.                         {
  182.                             CommandReceived(ircCommand);
  183.                         }
  184.  
  185.                         string[] commandParts = ircCommand.Split(' ');
  186.                         if (commandParts[0].Substring(0, 1) == ":")
  187.                         {
  188.                             commandParts[0] = commandParts[0].Remove(0, 1);
  189.                         }
  190.  
  191.                         if (commandParts[0].EndsWith(Server.Substring(Server.IndexOf('.'))))
  192.                         {
  193.                             // Server message
  194.                             switch (commandParts[1])
  195.                             {
  196.                                 case "001":
  197.                                     RegisterUser();
  198.                                     break;
  199.                                 case "332":
  200.                                     IrcTopic(commandParts);
  201.                                     break;
  202.                                 case "333":
  203.                                     IrcTopicOwner(commandParts);
  204.                                     break;
  205.                                 case "353":
  206.                                     IrcNamesList(commandParts);
  207.                                     break;
  208.                                 case "366": /*this.IrcEndNamesList(commandParts);*/
  209.                                     break;
  210.                                 case "372": /*this.IrcMOTD(commandParts);*/
  211.                                     break;
  212.                                 case "376": /*this.IrcEndMOTD(commandParts);*/
  213.                                     break;
  214.                                 default:
  215.                                     IrcServerMessage(commandParts);
  216.                                     break;
  217.                             }
  218.                         }
  219.                         else if (commandParts[0] == "PING")
  220.                         {
  221.                             // Server PING, send PONG back
  222.                             IrcPing(commandParts);
  223.                         }
  224.                         else
  225.                         {
  226.                             // Normal message
  227.                             string commandAction = commandParts[1];
  228.                             switch (commandAction)
  229.                             {
  230.                                 case "JOIN":
  231.                                     IrcJoin(commandParts);
  232.                                     break;
  233.                                 case "PART":
  234.                                     IrcPart(commandParts);
  235.                                     break;
  236.                                 case "MODE":
  237.                                     IrcMode(commandParts);
  238.                                     break;
  239.                                 case "NICK":
  240.                                     IrcNickChange(commandParts);
  241.                                     break;
  242.                                 case "KICK":
  243.                                     IrcKick(commandParts);
  244.                                     break;
  245.                                 case "QUIT":
  246.                                     IrcQuit(commandParts);
  247.                                     break;
  248.                             }
  249.                         }
  250.                     }
  251.                 }
  252.                 catch (ObjectDisposedException)
  253.                 {
  254.                     //This means we got disconnected, carry on.
  255.                 }
  256.                 catch (IOException)
  257.                 {
  258.                     //Do nothing, this is probably because the stream closed.
  259.                 }
  260.  
  261.                 Writer.Close();
  262.                 Reader.Close();
  263.                 Connection.Close();
  264.  
  265.                 if (Disconnected != null)
  266.                 {
  267.                     Disconnected();
  268.                 }
  269.             }
  270.  
  271.             Connected = false;
  272.             disconnecting = false;
  273.         }
  274.  
  275.         private void RegisterUser()
  276.         {
  277.             SetPass(NickPass);
  278.             JoinChannel(Channel);
  279.         }
  280.  
  281.         private void IrcTopic(string[] ircCommand)
  282.         {
  283.             string ircChannel = ircCommand[3];
  284.             string ircTopic = "";
  285.             for (int intI = 4; intI < ircCommand.Length; intI++)
  286.             {
  287.                 ircTopic += ircCommand[intI] + " ";
  288.             }
  289.             if (TopicSet != null)
  290.             {
  291.                 TopicSet(ircChannel, ircTopic.Remove(0, 1).Trim());
  292.             }
  293.         }
  294.  
  295.         private void IrcTopicOwner(string[] ircCommand)
  296.         {
  297.             string ircChannel = ircCommand[3];
  298.             string ircUser = ircCommand[4].Split('!')[0];
  299.             string topicDate = ircCommand[5];
  300.             if (TopicOwner != null)
  301.             {
  302.                 TopicOwner(ircChannel, ircUser, topicDate);
  303.             }
  304.         }
  305.  
  306.         private void IrcNamesList(string[] ircCommand)
  307.         {
  308.             string userNames = "";
  309.             for (int intI = 5; intI < ircCommand.Length; intI++)
  310.             {
  311.                 userNames += ircCommand[intI] + " ";
  312.             }
  313.             if (NamesList != null)
  314.             {
  315.                 NamesList(userNames.Remove(0, 1).Trim());
  316.             }
  317.         }
  318.  
  319.         private void IrcServerMessage(string[] ircCommand)
  320.         {
  321.             string serverMessage = "";
  322.             for (int intI = 1; intI < ircCommand.Length; intI++)
  323.             {
  324.                 serverMessage += ircCommand[intI] + " ";
  325.             }
  326.             if (ServerMessage != null)
  327.             {
  328.                 ServerMessage(serverMessage.Trim());
  329.             }
  330.         }
  331.  
  332.         private void IrcPing(string[] ircCommand)
  333.         {
  334.             string pingHash = "";
  335.             for (int intI = 1; intI < ircCommand.Length; intI++)
  336.             {
  337.                 pingHash += ircCommand[intI] + " ";
  338.             }
  339.             SendCommand("PONG " + pingHash);
  340.         }
  341.  
  342.         private void IrcJoin(string[] ircCommand)
  343.         {
  344.             string ircChannel = ircCommand[2];
  345.             string ircUser = ircCommand[0].Split('!')[0];
  346.             if (Join != null)
  347.             {
  348.                 Join(ircChannel.Remove(0, 1), ircUser);
  349.             }
  350.         }
  351.  
  352.         private void IrcPart(string[] ircCommand)
  353.         {
  354.             string ircChannel = ircCommand[2];
  355.             string ircUser = ircCommand[0].Split('!')[0];
  356.             if (Part != null)
  357.             {
  358.                 Part(ircChannel, ircUser);
  359.             }
  360.         }
  361.  
  362.         private void IrcMode(string[] ircCommand)
  363.         {
  364.             string ircChannel = ircCommand[2];
  365.             string ircUser = ircCommand[0].Split('!')[0];
  366.             string userMode = "";
  367.             for (int intI = 3; intI < ircCommand.Length; intI++)
  368.             {
  369.                 userMode += ircCommand[intI] + " ";
  370.             }
  371.             if (userMode.Substring(0, 1) == ":")
  372.             {
  373.                 userMode = userMode.Remove(0, 1);
  374.             }
  375.             if (Mode != null)
  376.             {
  377.                 Mode(ircChannel, ircUser, userMode.Trim());
  378.             }
  379.         }
  380.  
  381.         private void IrcNickChange(string[] ircCommand)
  382.         {
  383.             string userOldNick = ircCommand[0].Split('!')[0];
  384.             string userNewNick = ircCommand[2].Remove(0, 1);
  385.             if (NickChange != null)
  386.             {
  387.                 NickChange(userOldNick, userNewNick);
  388.             }
  389.         }
  390.  
  391.         private void IrcKick(string[] ircCommand)
  392.         {
  393.             string userKicker = ircCommand[0].Split('!')[0];
  394.             string userKicked = ircCommand[3];
  395.             string ircChannel = ircCommand[2];
  396.             string kickMessage = "";
  397.             for (int intI = 4; intI < ircCommand.Length; intI++)
  398.             {
  399.                 kickMessage += ircCommand[intI] + " ";
  400.             }
  401.             if (Kick != null)
  402.             {
  403.                 Kick(ircChannel, userKicker, userKicked, kickMessage.Remove(0, 1).Trim());
  404.             }
  405.         }
  406.  
  407.         private void IrcQuit(string[] ircCommand)
  408.         {
  409.             string userQuit = ircCommand[0].Split('!')[0];
  410.             string quitMessage = "";
  411.             for (int intI = 2; intI < ircCommand.Length; intI++)
  412.             {
  413.                 quitMessage += ircCommand[intI] + " ";
  414.             }
  415.             if (Quit != null)
  416.             {
  417.                 Quit(userQuit, quitMessage.Remove(0, 1).Trim());
  418.             }
  419.         }
  420.     }
  421. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment