Advertisement
fruffl

fruffi

Aug 6th, 2011
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using Fruffi.Bot.Irc;
  7.  
  8. namespace Fruffi.Bot.Hlp
  9. {
  10.     class Helpers
  11.     {
  12.         #region private vars
  13.         private Connect connect;
  14.         #endregion
  15.  
  16.         #region props
  17.         public Connect Connect
  18.         {
  19.             get { return this.connect; }
  20.             set { this.connect = value; }
  21.         }
  22.         #endregion
  23.        
  24.         #region constructor
  25.         public Helpers(Connect connect)
  26.         {
  27.             this.Connect = connect;
  28.         }
  29.         #endregion
  30.  
  31.         public bool is_Server(string server)
  32.         {
  33.             return (this.Connect.Config.Server == server);
  34.         }
  35.  
  36.         public bool is_Nickserv(string ex0)
  37.         {
  38.             Regex r = new Regex("Nickserv", RegexOptions.IgnoreCase);
  39.             MatchCollection m = r.Matches(ex0);
  40.  
  41.             if (m.Count == 1)
  42.                 return true;
  43.  
  44.             return false;
  45.         }
  46.  
  47.         public bool is_Chanserv(string ex0)
  48.         {
  49.             Regex r = new Regex("Chanserv", RegexOptions.IgnoreCase);
  50.             MatchCollection m = r.Matches(ex0);
  51.  
  52.             if (m.Count == 1)
  53.                 return true;
  54.  
  55.             return false;
  56.         }
  57.  
  58.         public bool find(string data, string find)
  59.         {
  60.             Regex r = new Regex("(.*)" + find + "(.*)", RegexOptions.IgnoreCase);
  61.             MatchCollection m = r.Matches(data);
  62.  
  63.             if (m.Count == 1)
  64.                 return true;
  65.  
  66.             return false;
  67.         }
  68.  
  69.         public string ChannelName(string name)
  70.         {
  71.             return name.Replace("#", "").Replace(":", "");
  72.         }
  73.  
  74.         public string SharpChannelName(string name)
  75.         {
  76.             return "#" + ChannelName(name);
  77.         }
  78.  
  79.         public string RemoveColon(string s)
  80.         {
  81.             if (s.Substring(0, 1) == ":")
  82.                 s = s.Remove(0, 1);
  83.             return s;
  84.         }
  85.  
  86.         public string ArrayToString(string[] s, int b = 0)
  87.         {
  88.             string h = "";
  89.             if(s.Length > b)
  90.                 for (int i = b; i < s.Length; i++)
  91.                     h += s[i] + " ";
  92.  
  93.             return this.RemoveColon(h).Trim();
  94.         }
  95.  
  96.         public string Nick(string s)
  97.         {
  98.             return s.Split('!')[0];
  99.         }
  100.     }
  101. }
  102.  
  103.  
  104. ---------------------------------
  105. using System;
  106. using System.IO;
  107. using System.Net.Sockets;
  108. using System.Text;
  109. using Fruffi.Bot.DataStorage;
  110. using Fruffi.Bot.Hlp;
  111. using Fruffi.Bot.Irc;
  112. using Fruffi.Bot.Chan;
  113. using Fruffi.Files;
  114. using System.Collections;
  115. using System.Diagnostics;
  116.  
  117. namespace Fruffi.Bot
  118. {
  119.     class Connect
  120.     {
  121.         #region private vars
  122.         private bool is_running;
  123.         private Owner owner;
  124.         private Config config;
  125.         private Helpers helpers;
  126.         private TcpClient tcp;
  127.         private NetworkStream stream;
  128.         private StreamWriter writer;
  129.         private StreamReader reader;
  130.         private IrcCommands cmd;
  131.         private Parser parser;
  132.         private Storage storage;
  133.         private ChannelList channels;
  134.         #region autonomy by lines
  135.         private int idle;
  136.         private int next;
  137.         private bool IsIdle = false;
  138.         private int idle_minor = 150;
  139.         private int idle_major = 1000;
  140.         #endregion
  141.         #endregion
  142.  
  143.         #region props
  144.         public TcpClient Tcp
  145.         {
  146.             get { return this.tcp; }
  147.             set { this.tcp = value; }
  148.         }
  149.         public NetworkStream Stream
  150.         {
  151.             get { return this.stream; }
  152.             set { this.stream = value; }
  153.         }
  154.         public StreamWriter Writer
  155.         {
  156.             get { return this.writer; }
  157.             set { this.writer = value; }
  158.         }
  159.         public StreamReader Reader
  160.         {
  161.             get { return this.reader; }
  162.             set { this.reader = value; }
  163.         }
  164.         public Owner Owner
  165.         {
  166.             get { return this.owner; }
  167.             set { this.owner = value; }
  168.         }
  169.         public Config Config
  170.         {
  171.             get { return this.config; }
  172.             set { this.config = value; }
  173.         }
  174.         public Helpers Helpers
  175.         {
  176.             get { return this.helpers; }
  177.             set { this.helpers = value; }
  178.         }
  179.         public IrcCommands Cmd
  180.         {
  181.             get { return this.cmd; }
  182.             set { this.cmd = value; }
  183.         }
  184.         public Parser Parser
  185.         {
  186.             get { return this.parser; }
  187.             set { this.parser = value; }
  188.         }
  189.         public Storage Storage
  190.         {
  191.             get { return this.storage; }
  192.             set { this.storage = value; }
  193.         }
  194.         public ChannelList Channels
  195.         {
  196.             get { return this.channels; }
  197.             set { this.channels = value; }
  198.         }
  199.         public bool IsRunning
  200.         {
  201.             get { return this.is_running; }
  202.             set { this.is_running = value; }
  203.         }
  204.         #endregion
  205.  
  206.  
  207.         #region constructor
  208.         public Connect()
  209.         {
  210.  
  211.             this.Config = new Config();
  212.             this.Owner = new Owner();
  213.             this.Helpers = new Helpers(this);
  214.             this.Cmd = new IrcCommands(this.Helpers);
  215.             this.Parser = new Parser(this);
  216.             this.Storage = new Storage();
  217.             this.Channels = new ChannelList();
  218.  
  219.  
  220.             this.Storage.Ignore.Save("Nickserv");
  221.             this.Storage.Ignore.Save("Chanserv");
  222.             this.Storage.Ignore.Save("Spamscanner");
  223.             this.Storage.Ignore.Save("Global");
  224.             this.Storage.Ignore.Save("Services");
  225.  
  226.             this.next = 0;
  227.             this.idle = 0;
  228.  
  229.             while (true)
  230.             {
  231.                 this.openStream();
  232.  
  233.                 string r;
  234.  
  235.                 while ((r = this.Reader.ReadLine()) != null)
  236.                 {
  237.                     string[] c = new string[r.Split(' ').Length];
  238.                     c = r.Split(' ');
  239.  
  240.                     if (c[0].Substring(0, 1) == ":")
  241.                         c[0] = c[0].Remove(0, 1);
  242.  
  243.                     Console.WriteLine(r);
  244.  
  245.                     #region message
  246.                     #region from server
  247.                     if (this.Helpers.is_Server(c[0]))
  248.                     {
  249.                         switch (c[1])
  250.                         {
  251.                             case "332": this.Parser.Topic(c); break;
  252.                             case "333": this.Parser.TopicOwner(c); break;
  253.                             case "353": this.Parser.Nameslist(c); break;
  254.                             case "366": // end names
  255.                             case "372": break;
  256.                             case "376": this.KillOrIdentify(); break;
  257.                             case "433": this.NickInUse(); break;
  258.                         }
  259.                     }
  260.                     #endregion
  261.                     #region from nickserv
  262.                     else if (this.Helpers.is_Nickserv(c[0]))
  263.                     {
  264.                         switch (c[1])
  265.                         {
  266.                             case "NOTICE":
  267.                                 if (this.Helpers.find(c[3], ":ghost")) // ghost success
  268.                                     if (this.Config.IsAlternate)
  269.                                     {
  270.                                         Console.WriteLine("Kill applied");
  271.                                         this.KillNickSuccess();
  272.                                     }
  273.  
  274.                                 if (this.Helpers.find(c[3], ":setting")) // vhost applied
  275.                                     if (this.Helpers.find(r, this.Config.Vhost))
  276.                                     {
  277.                                         Console.WriteLine("Vhost applied");
  278.                                         this.Connected();
  279.                                     }
  280.  
  281.                                 break;
  282.                             case "KICK": this.Parser.Kick(c); break;
  283.                             case "MODE": this.Parser.Mode(c); break;
  284.                         }
  285.                     }
  286.                     #endregion
  287.                     #region from chanserv
  288.                     else if (this.Helpers.is_Chanserv(c[0]))
  289.                     {
  290.                         switch (c[1])
  291.                         {
  292.                             case "KICK": this.Parser.Kick(c); break;
  293.                             case "MODE": this.Parser.Mode(c); break;
  294.                         }
  295.                     }
  296.                     #endregion
  297.                     #region normal message
  298.                     else
  299.                     {
  300.                         switch (c[1])
  301.                         {
  302.                             case "JOIN": this.Parser.Join(c); break;
  303.                             case "PART": this.Parser.Part(c); break;
  304.                             case "MODE": this.Parser.Mode(c); break;
  305.                             case "NICK": this.Parser.Nick(c); break;
  306.                             case "KICK": this.Parser.Kick(c); break;
  307.                             case "QUIT": this.Parser.Quit(c); break;
  308.                             case "NOTICE":
  309.                             case "PRIVMSG": this.Parser.Priv(c); break;
  310.                             case "INVITE": this.Parser.Inv(c); break;
  311.                         }
  312.                     }
  313.                     #endregion
  314.                     #endregion
  315.  
  316.                     #region ping
  317.                     if (c[0] == "PING")
  318.                     {
  319.                         this.Ping(c);
  320.                     }
  321.                     #endregion
  322.                 }
  323.  
  324.                 this.closeStream();
  325.             }
  326.         }
  327.         #endregion
  328.  
  329.         #region idler
  330.         public void Idler()
  331.         {
  332.             if (!this.IsIdle)
  333.                 return;
  334.  
  335.             if (this.next == this.idle)
  336.             {
  337.                 Console.WriteLine("AUTONOM: " + this.idle + " lines passed: do stuff");
  338.                 this.next = 0;
  339.                 this.idle = 0;
  340.  
  341.                 if (this.idle == 0)
  342.                 {
  343.                     Random rand = new Random();
  344.                     this.next = rand.Next(this.idle_minor, this.idle_major);
  345.                     Console.WriteLine("AUTONOM: next autonom action in " + this.next + " lines");
  346.                 }
  347.             }
  348.  
  349.             this.idle++;
  350.  
  351.         }
  352.         #endregion
  353.  
  354.         #region streams
  355.         public void openStream()
  356.         {
  357.             Console.WriteLine("Connect to {0}:{1}", this.Config.Server, this.Config.Port);
  358.             this.IsRunning = true;
  359.  
  360.             this.Tcp = new TcpClient(this.Config.Server, Convert.ToInt32(this.Config.Port));
  361.             this.Stream = this.Tcp.GetStream();
  362.             this.Reader = new StreamReader(this.Stream, Encoding.GetEncoding(this.Config.Encoding));
  363.             this.Writer = new StreamWriter(this.Stream, Encoding.GetEncoding(this.Config.Encoding));
  364.  
  365.             this.ApplyUser(this.Config.Nick);
  366.             this.ApplyNick(this.Config.Nick);
  367.         }
  368.         public void closeStream()
  369.         {
  370.             this.IsRunning = false;
  371.             Console.WriteLine("Close Stream...");
  372.             this.Writer.Close();
  373.             this.Reader.Close();
  374.             this.Tcp.Close();
  375.         }
  376.  
  377.         public void write(string line)
  378.         {
  379.             if (line == "")
  380.                 return;
  381.             System.Threading.Thread.Sleep(2000);
  382.  
  383.             Console.WriteLine("RAW: " + line);
  384.             this.Writer.WriteLine(line);
  385.             this.Writer.Flush();
  386.         }
  387.         #endregion
  388.  
  389.         #region Ping
  390.         private void Ping(string[] c)
  391.         {
  392.             string PingHash = "";
  393.             for (int intI = 1; intI < c.Length; intI++)
  394.             {
  395.                 PingHash += c[intI] + " ";
  396.             }
  397.  
  398.             this.write("PONG " + PingHash);
  399.         }
  400.         #endregion
  401.  
  402.         #region Server Messages
  403.         private void NickInUse()
  404.         {
  405.             Console.WriteLine("NICK is in use.");
  406.             this.Config.IsAlternate = true;
  407.             this.ApplyNick(this.Config.Alternate);
  408.         }
  409.         private void KillOrIdentify()
  410.         {
  411.             if (this.Config.IsAlternate)
  412.             {
  413.                 Console.WriteLine("Try to kill user...");
  414.                 this.KillNick(this.Config.Nick, this.Config.Password);
  415.             }
  416.             else
  417.             {
  418.                 Console.WriteLine("Identify nick...");
  419.                 this.IdentifyNick(this.Config.Password);
  420.             }
  421.         }
  422.         #endregion
  423.  
  424.         #region own nick
  425.         private void ApplyUser(string nick)
  426.         {
  427.             Console.WriteLine("Use nick " + nick);
  428.             this.write(this.Cmd.user(this.Config.IsInvisible, nick, this.Config.Name));
  429.         }
  430.  
  431.         private void ApplyNick(string nick)
  432.         {
  433.             Console.WriteLine("Apply nick " + nick);
  434.             string c = this.Cmd.nick(nick);
  435.             Console.WriteLine(c);
  436.             this.write(c);
  437.         }
  438.         private void IdentifyNick(string pass)
  439.         {
  440.             Console.WriteLine("Identify nick");
  441.             this.write(this.Cmd.Nickserv.identify(pass));
  442.         }
  443.  
  444.         private void KillNick(string nick, string pass)
  445.         {
  446.             Console.WriteLine("Kill nick " + nick);
  447.             string c = this.Cmd.Nickserv.ghost(nick, pass);
  448.             Console.WriteLine(c);
  449.             this.write(c);
  450.         }
  451.  
  452.         private void KillNickSuccess()
  453.         {
  454.             Console.WriteLine("Kill complete");
  455.             this.Config.IsAlternate = false;
  456.             this.ApplyNick(this.Config.Nick);
  457.             this.KillOrIdentify();
  458.         }
  459.         #endregion
  460.  
  461.         #region connected
  462.  
  463.         private void Connected()
  464.         {
  465.             Console.WriteLine("Connected");
  466.  
  467.             Console.WriteLine("Identify Home-Channel");
  468.             this.write(this.Cmd.Chanserv.identify(this.Config.Home, this.Config.Homepass));
  469.  
  470.             Console.WriteLine("Join Home-Channel");
  471.             this.write(this.Cmd.join(this.Config.Home, this.Config.Homekey));
  472.  
  473.             this.IsIdle = true;
  474.         }
  475.  
  476.         #endregion
  477.  
  478.     }
  479. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement