Advertisement
fruffl

fruffi

Nov 16th, 2011
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Net.Sockets;
  7. using System.Collections;
  8. using System.Diagnostics;
  9. using Fruffi.Bot;
  10.  
  11. namespace Fruffi.Irc
  12. {
  13.     class Irc
  14.     {
  15.         public const string SPAMSCANNER = "SPAMSCANNER";
  16.         public const string NICKSERV = "NICKSERV";
  17.         public const string CHANSERV = "CHANSERV";
  18.         public const string SERVER = "SERVER";
  19.  
  20.         public const int SENDER_PING = 10;
  21.         public const int SENDER_SERVER = 20;
  22.         public const int SENDER_NICKSERV = 30;
  23.         public const int SENDER_CHANSERV = 40;
  24.         public const int SENDER_SPAMSCANNER = 50;
  25.         public const int SENDER_USER = 100;
  26.  
  27.         public const int PRIVMSG_FROM_SELF = 10;
  28.         public const int PRIVMSG_FROM_OWNER = 20;
  29.         public const int PRIVMSG_FROM_USER = 30;
  30.  
  31.         public const int PRIVMSG_FROM_CHANNELOWNER = 30;
  32.         public const int PRIVMSG_FROM_CHANNELADMIN = 40;
  33.         public const int PRIVMSG_FROM_CHANNELOP = 50;
  34.         public const int PRIVMSG_FROM_CHANNELHOP = 60;
  35.         public const int PRIVMSG_FROM_CHANNELVOICE = 70;
  36.         public const int PRIVMSG_FROM_CHANNELNORMAL = 80;
  37.  
  38.         public const int READ_ID_SERVERCONFIG       = 005;
  39.         public const int READ_ID_TOPIC              = 332;
  40.         public const int READ_ID_TOPICOWNER         = 333;
  41.         public const int READ_ID_NAMESLIST          = 353;
  42.         public const int READ_ID_NAMESLISTEND       = 366;
  43.         public const int READ_ID_MOTD               = 372;
  44.         public const int READ_ID_MOTD_START         = 375;
  45.         public const int READ_ID_MOTD_END           = 376;
  46.         public const int READ_ID_NICKINUSE          = 433;
  47.  
  48.         public const string READ_JOIN = "JOIN";
  49.         public const string READ_PART = "PART";
  50.         public const string READ_NICK = "NICK";
  51.         public const string READ_QUIT = "QUIT";
  52.         public const string READ_KICK = "KICK";
  53.         public const string READ_MODE = "MODE";
  54.         public const string READ_NOTICE = "NOTICE";
  55.         public const string READ_PRIVMSG = "PRIVMSG";
  56.         public const string READ_INVITE = "INVITE";
  57.         public const string READ_PING = "PING";
  58.         public const string READ_GHOST_SUCCESS = ":ghost";
  59.         public const string READ_VHOST_SUCCESS = ":setting";
  60.         //public const string READ_CTCP = "\u0001VERSION\u0001";
  61.         //http://www.irchelp.org/irchelp/rfc/ctcpspec.html
  62.  
  63.         public const string SEND_USER = "USER {0} {1} illi.be :{2}";
  64.         public const string SEND_NICK = "NICK {0}";
  65.         public const string SEND_JOIN = "JOIN {0} {1}";
  66.         public const string SEND_PART = "PART {0} :{1}";
  67.         public const string SEND_QUIT = "QUIT :{0}";
  68.         public const string SEND_PRIVMSG = "PRIVMSG {0} :{1}";
  69.         public const string SEND_NOTICE = "NOTICE {0} :{1}";
  70.         public const string SEND_ACTION = "PRIVMSG {0} :\u0001" + "ACTION {1}\u0001";
  71.         public const string SEND_PONG = "PONG {0}";
  72.         public const string SEND_IDENTIFY = "NICKSERV identify {0}";
  73.         public const string SEND_GHOST = "NICKSERV ghost {0} {1}";
  74.  
  75.         public const string SEND_MODE_CHANNEL_ADD = "MODE {0} +{1} {2}";
  76.         public const string SEND_MODE_CHANNEL_DEL = "MODE {0} -{1} {2}";
  77.         public const string SEND_MODE_USER_ADD = "MODE {0} +{1}";
  78.         public const string SEND_MODE_USER_DEL = "MODE {0} -{1}";
  79.  
  80.         public const string CONSOLE_CHANNEL_TEXT = "{0} <{1}>\t{2}";
  81.  
  82.         public const string ENV_REPLACE_REPLYTO = "%REPLYTO%";
  83.  
  84.  
  85.         private TcpClient Tcp;
  86.         private NetworkStream Stream;
  87.         private StreamWriter Writer;
  88.         private StreamReader Reader;
  89.  
  90.         private IrcSend __Send = new IrcSend();
  91.         private IrcRead __Read = new IrcRead();
  92.         private static Irc __Instance;
  93.  
  94.         private IrcServerConfig __ServerConfig;
  95.  
  96.         public static Irc Instance
  97.         {
  98.             get { return __Instance; }
  99.         }
  100.  
  101.         public IrcSend Send
  102.         {
  103.             get { return this.__Send; }
  104.         }
  105.         public IrcRead Read
  106.         {
  107.             get { return this.__Read; }
  108.         }
  109.         public IrcServerConfig ServerConfig
  110.         {
  111.             get { return this.__ServerConfig; }
  112.         }
  113.  
  114.         public static bool __IsRunning = true;
  115.  
  116.         public static void sleep()
  117.         {
  118.             Irc.__IsRunning = false;
  119.         }
  120.  
  121.         public static void wakeup()
  122.         {
  123.             Irc.__IsRunning = true;
  124.         }
  125.  
  126.         public Irc()
  127.         {
  128.             Fruffi.sayBlock(new string[] {"Connecting to IRC"});
  129.             __Instance = this;
  130.             while (true)
  131.             {
  132.                 this.openStream();
  133.                 string line;
  134.                 while ((line = this.Reader.ReadLine()) != null)
  135.                     this.read(line);
  136.  
  137.                 this.closeStream();
  138.                 try
  139.                 {
  140.                 }
  141.                 catch (Exception e)
  142.                 {
  143.                     Fruffi.sayError(e);
  144.                 }
  145.             }
  146.         }
  147.        
  148.         public void read(string line)
  149.         {
  150.             string[] extract = IrcHelper.extract(line);
  151.             string sender = extract[0];
  152.             string handle = extract[1].ToUpper();
  153.             string response;
  154.             IrcChannelResponse channelResponse;
  155.             //Fruffi.say(line, 0);
  156.  
  157.             switch (IrcHelper.sender(sender))
  158.             {
  159.                 case Irc.SENDER_SPAMSCANNER:
  160.                     Fruffi.say(line);
  161.                     break;
  162.                 case Irc.SENDER_PING:
  163.                     Fruffi.say(line);
  164.                     this.write(this.Send.Pong(extract));
  165.                     break;
  166.                 case Irc.SENDER_USER:
  167.                     switch (handle)
  168.                     {
  169.                         case Irc.READ_JOIN:
  170.                             try
  171.                             {
  172.                                 channelResponse = this.Read.Join(extract);
  173.                                 response = channelResponse.Brain.Response;
  174.                                 response = channelResponse.Brain.Response.Replace(Irc.ENV_REPLACE_REPLYTO, channelResponse.Nick);
  175.                                 this.write(this.Send.WriteChannel(channelResponse.Channel, response));
  176.                             }
  177.                             catch (NullReferenceException)
  178.                             {
  179.                             }
  180.  
  181.                             break;
  182.                         case Irc.READ_NICK:
  183.                             // update user
  184.                             break;
  185.                         case Irc.READ_PART:
  186.                             // remove user
  187.                             break;
  188.                         case Irc.READ_KICK:
  189.                             // remove user
  190.                             break;
  191.                         case Irc.READ_QUIT:
  192.                             // remove user
  193.                             break;
  194.                         case Irc.READ_NOTICE:
  195.                             // parse
  196.                             break;
  197.                         case Irc.READ_PRIVMSG:
  198.                             channelResponse = this.Read.Priv
  199.                                 (
  200.                                     IrcHelper.extract
  201.                                     (
  202.                                         IrcHelper.RemoveChannelWar
  203.                                         (
  204.                                             IrcHelper.ConvertUtf8ToIso(line)
  205.                                         )
  206.                                     )
  207.                                 );
  208.  
  209.                             try
  210.                             {
  211.                                 response = channelResponse.Brain.Response;
  212.                                 if (channelResponse.BypassEnv == false)
  213.                                     response = channelResponse.Brain.Response.Replace(Irc.ENV_REPLACE_REPLYTO, channelResponse.Nick);
  214.  
  215.                                 if (channelResponse.Brain.isAction == false)
  216.                                     this.write(this.Send.WriteChannel(channelResponse.Channel, response));
  217.                                 else
  218.                                     this.write(this.Send.WriteActionChannel(channelResponse.Channel, response));
  219.  
  220.                                 Fruffi.sayNotImplemented(new string[] { "SAY",
  221.                                    String.Format(Irc.CONSOLE_CHANNEL_TEXT, channelResponse.Channel, channelResponse.Nick, response)
  222.                                 });
  223.                             }
  224.                             catch (NullReferenceException)
  225.                             {
  226.                             }
  227.                             // parse
  228.                             break;
  229.                         case Irc.READ_INVITE:
  230.                             // parse
  231.                             break;
  232.                         case Irc.READ_MODE:
  233.                         // parse
  234.                         default:
  235.                             break;
  236.                     }
  237.                     break;
  238.                 case Irc.SENDER_SERVER:
  239.                     try
  240.                     {
  241.                         try
  242.                         {
  243.                             int h = 0;
  244.                             h = Convert.ToInt32(handle);
  245.                             switch (h)
  246.                             {
  247.                                 case Irc.READ_ID_NICKINUSE:
  248.                                     Fruffi.Configs.Nick.isAlternate = true;
  249.                                     this.write(this.Send.Nick(Fruffi.Configs.Nick.AltNick));
  250.                                     break;
  251.                                 case Irc.READ_ID_MOTD_END:
  252.                                     if (Fruffi.Configs.Nick.isAlternate == true)
  253.                                         this.write(this.Send.NickServ.Ghost(Fruffi.Configs.Nick.Nick, Fruffi.Configs.Nick.Password));
  254.                                     else
  255.                                         this.write(this.Send.NickServ.Identify(Fruffi.Configs.Nick.Password));
  256.                                     break;
  257.                                 case Irc.READ_ID_SERVERCONFIG:
  258.                                     Fruffi.sayNotImplemented(new string[] { "SERVERCONFIG" });
  259.                                     this.__ServerConfig = new IrcServerConfig(line);
  260.                                     break;
  261.                                 case Irc.READ_ID_NAMESLISTEND:
  262.                                 case Irc.READ_ID_TOPIC:
  263.                                 case Irc.READ_ID_NAMESLIST:
  264.                                 case Irc.READ_ID_TOPICOWNER:
  265.                                 case Irc.READ_ID_MOTD_START:
  266.                                 case Irc.READ_ID_MOTD:
  267.                                 default:
  268.                                     break;
  269.                             }
  270.                         }
  271.                         catch(EncoderFallbackException)
  272.                         {
  273.                         }
  274.  
  275.                     }
  276.                     catch (Exception)
  277.                     {
  278.                         switch (handle)
  279.                         {
  280.                             case Irc.READ_NOTICE:
  281.                                 /**
  282.                                  * if umode +B the following lines will be sent by the irc-server:
  283.                                  *
  284.                                  * :irc.ham.de.euirc.net NOTICE AUTH :- irc.ham.de.euirc.net Bot Message of the Day -
  285.                                  * :irc.ham.de.euirc.net NOTICE AUTH :- 28/4/2008 18:03
  286.                                  * :irc.ham.de.euirc.net NOTICE AUTH :End of /BOTMOTD command.
  287.                                  * */
  288.                                 Fruffi.say(line);
  289.                                 break;
  290.                         }
  291.                     }
  292.                     break;
  293.                 case Irc.SENDER_CHANSERV:
  294.                     switch (handle)
  295.                     {
  296.                         case Irc.READ_KICK:
  297.                             Fruffi.sayNotImplemented(new string[] { "CS Kick" });
  298.                             break;
  299.                         case Irc.READ_MODE:
  300.                             Fruffi.sayNotImplemented(new string[] { "CS MODE" });
  301.                             break;
  302.                         default:
  303.                             break;
  304.                     }
  305.                     break;
  306.                 case Irc.SENDER_NICKSERV:
  307.                     switch (handle)
  308.                     {
  309.                         case Irc.READ_NOTICE:
  310.                             string serverresponse = extract[3];
  311.                             if (Fruffi.Configs.Nick.isAlternate)
  312.                                 if (IrcHelper.find(serverresponse, Irc.READ_GHOST_SUCCESS)) // ghost success
  313.                                 {
  314.                                     this.write(this.Send.Nick(Fruffi.Configs.Nick.Nick));
  315.                                     this.write(this.Send.NickServ.Identify(Fruffi.Configs.Nick.Password));
  316.                                     Fruffi.Configs.Nick.isAlternate = false;
  317.                                     Fruffi.sayBlock(new string[] { "GHOST: SUCCESS" });
  318.                                 }
  319.  
  320.                             if (IrcHelper.find(serverresponse, Irc.READ_VHOST_SUCCESS)) // vhost applied
  321.                                 if (IrcHelper.find(line, Fruffi.Configs.Nick.Vhost))
  322.                                 {
  323.                                     Fruffi.sayBlock(new string[] { "VHOST: APPLIED" });
  324.                                     this.write(this.Send.Join(Fruffi.Configs.Home.Channel, Fruffi.Configs.Home.Key));
  325.                                     this.write(this.Send.ModeUserAdd(Fruffi.Configs.Nick.Nick, 'p'));
  326.                                     this.write(this.Send.ModeUserAdd(Fruffi.Configs.Nick.Nick, 'B'));
  327.                                     this.write(this.Send.Join("fluchen"));
  328.                                     //this.write(this.Send.Join("gayfamily"));
  329.                                     //this.write(this.Send.Join("php.de"));
  330.                                     //this.write(this.Send.Join("mikrocontroller.net"));
  331.                                     this.write(this.Send.Join("frei-bier-bude"));
  332.                                     //this.write(this.Send.Join("marvin"));
  333.                                     //this.write(this.Send.Join("bot"));
  334.                                 }
  335.                             break;
  336.                         case Irc.READ_KICK:
  337.                             Fruffi.sayNotImplemented(new string[] { "NS Kick" });
  338.                             break;
  339.                         case Irc.READ_MODE:
  340.                             Fruffi.sayNotImplemented(new string[] { "NS Mode" });
  341.                             break;
  342.                         default:
  343.                             break;
  344.                     }
  345.                     break;
  346.             }
  347.         }
  348.  
  349.         public void write(string line)
  350.         {
  351.             if (line == "")
  352.                 return;
  353.  
  354.             if (Irc.__IsRunning == false)
  355.                 return;
  356.            
  357.             System.Threading.Thread.Sleep(2000);
  358.  
  359.  
  360.             line = IrcHelper.RemoveChannelWar(IrcHelper.ConvertUtf8ToIso(line));
  361.             Fruffi.say("RAW: " + line, 1);
  362.             this.Writer.WriteLine(line);
  363.             this.Writer.Flush();
  364.         }
  365.  
  366.         private bool openStream()
  367.         {
  368.             Fruffi.sayBlock(new string[] { "open Stream", Fruffi.Configs.Server.Uri, Fruffi.Configs.Server.Port.ToString(), Fruffi.Configs.Socket.Encoding });
  369.  
  370.             this.Tcp    = new TcpClient(Fruffi.Configs.Server.Uri, Convert.ToInt32(Fruffi.Configs.Server.Port));
  371.             this.Stream = this.Tcp.GetStream();
  372.             this.Reader = new StreamReader(this.Stream, Encoding.GetEncoding(Fruffi.Configs.Socket.Encoding));
  373.             this.Writer = new StreamWriter(this.Stream, Encoding.GetEncoding(Fruffi.Configs.Socket.Encoding));
  374.  
  375.             this.write(this.Send.User(Fruffi.Configs.Nick.isInvisible, Fruffi.Configs.Nick.Nick, Fruffi.Configs.Nick.Name));
  376.             this.write(this.Send.Nick(Fruffi.Configs.Nick.Nick));
  377.             return true;
  378.         }
  379.  
  380.         private bool closeStream()
  381.         {
  382.             this.Writer.Close();
  383.             this.Reader.Close();
  384.             this.Tcp.Close();
  385.             return false;
  386.         }
  387.     }
  388. }
  389.  
  390.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement