Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using Fruffi.Bot.Irc;
- namespace Fruffi.Bot.Hlp
- {
- class Helpers
- {
- #region private vars
- private Connect connect;
- #endregion
- #region props
- public Connect Connect
- {
- get { return this.connect; }
- set { this.connect = value; }
- }
- #endregion
- #region constructor
- public Helpers(Connect connect)
- {
- this.Connect = connect;
- }
- #endregion
- public bool is_Server(string server)
- {
- return (this.Connect.Config.Server == server);
- }
- public bool is_Nickserv(string ex0)
- {
- Regex r = new Regex("Nickserv", RegexOptions.IgnoreCase);
- MatchCollection m = r.Matches(ex0);
- if (m.Count == 1)
- return true;
- return false;
- }
- public bool is_Chanserv(string ex0)
- {
- Regex r = new Regex("Chanserv", RegexOptions.IgnoreCase);
- MatchCollection m = r.Matches(ex0);
- if (m.Count == 1)
- return true;
- return false;
- }
- public bool find(string data, string find)
- {
- Regex r = new Regex("(.*)" + find + "(.*)", RegexOptions.IgnoreCase);
- MatchCollection m = r.Matches(data);
- if (m.Count == 1)
- return true;
- return false;
- }
- public string ChannelName(string name)
- {
- return name.Replace("#", "").Replace(":", "");
- }
- public string SharpChannelName(string name)
- {
- return "#" + ChannelName(name);
- }
- public string RemoveColon(string s)
- {
- if (s.Substring(0, 1) == ":")
- s = s.Remove(0, 1);
- return s;
- }
- public string ArrayToString(string[] s, int b = 0)
- {
- string h = "";
- if(s.Length > b)
- for (int i = b; i < s.Length; i++)
- h += s[i] + " ";
- return this.RemoveColon(h).Trim();
- }
- public string Nick(string s)
- {
- return s.Split('!')[0];
- }
- }
- }
- ---------------------------------
- using System;
- using System.IO;
- using System.Net.Sockets;
- using System.Text;
- using Fruffi.Bot.DataStorage;
- using Fruffi.Bot.Hlp;
- using Fruffi.Bot.Irc;
- using Fruffi.Bot.Chan;
- using Fruffi.Files;
- using System.Collections;
- using System.Diagnostics;
- namespace Fruffi.Bot
- {
- class Connect
- {
- #region private vars
- private bool is_running;
- private Owner owner;
- private Config config;
- private Helpers helpers;
- private TcpClient tcp;
- private NetworkStream stream;
- private StreamWriter writer;
- private StreamReader reader;
- private IrcCommands cmd;
- private Parser parser;
- private Storage storage;
- private ChannelList channels;
- #region autonomy by lines
- private int idle;
- private int next;
- private bool IsIdle = false;
- private int idle_minor = 150;
- private int idle_major = 1000;
- #endregion
- #endregion
- #region props
- public TcpClient Tcp
- {
- get { return this.tcp; }
- set { this.tcp = value; }
- }
- public NetworkStream Stream
- {
- get { return this.stream; }
- set { this.stream = value; }
- }
- public StreamWriter Writer
- {
- get { return this.writer; }
- set { this.writer = value; }
- }
- public StreamReader Reader
- {
- get { return this.reader; }
- set { this.reader = value; }
- }
- public Owner Owner
- {
- get { return this.owner; }
- set { this.owner = value; }
- }
- public Config Config
- {
- get { return this.config; }
- set { this.config = value; }
- }
- public Helpers Helpers
- {
- get { return this.helpers; }
- set { this.helpers = value; }
- }
- public IrcCommands Cmd
- {
- get { return this.cmd; }
- set { this.cmd = value; }
- }
- public Parser Parser
- {
- get { return this.parser; }
- set { this.parser = value; }
- }
- public Storage Storage
- {
- get { return this.storage; }
- set { this.storage = value; }
- }
- public ChannelList Channels
- {
- get { return this.channels; }
- set { this.channels = value; }
- }
- public bool IsRunning
- {
- get { return this.is_running; }
- set { this.is_running = value; }
- }
- #endregion
- #region constructor
- public Connect()
- {
- this.Config = new Config();
- this.Owner = new Owner();
- this.Helpers = new Helpers(this);
- this.Cmd = new IrcCommands(this.Helpers);
- this.Parser = new Parser(this);
- this.Storage = new Storage();
- this.Channels = new ChannelList();
- this.Storage.Ignore.Save("Nickserv");
- this.Storage.Ignore.Save("Chanserv");
- this.Storage.Ignore.Save("Spamscanner");
- this.Storage.Ignore.Save("Global");
- this.Storage.Ignore.Save("Services");
- this.next = 0;
- this.idle = 0;
- while (true)
- {
- this.openStream();
- string r;
- while ((r = this.Reader.ReadLine()) != null)
- {
- string[] c = new string[r.Split(' ').Length];
- c = r.Split(' ');
- if (c[0].Substring(0, 1) == ":")
- c[0] = c[0].Remove(0, 1);
- Console.WriteLine(r);
- #region message
- #region from server
- if (this.Helpers.is_Server(c[0]))
- {
- switch (c[1])
- {
- case "332": this.Parser.Topic(c); break;
- case "333": this.Parser.TopicOwner(c); break;
- case "353": this.Parser.Nameslist(c); break;
- case "366": // end names
- case "372": break;
- case "376": this.KillOrIdentify(); break;
- case "433": this.NickInUse(); break;
- }
- }
- #endregion
- #region from nickserv
- else if (this.Helpers.is_Nickserv(c[0]))
- {
- switch (c[1])
- {
- case "NOTICE":
- if (this.Helpers.find(c[3], ":ghost")) // ghost success
- if (this.Config.IsAlternate)
- {
- Console.WriteLine("Kill applied");
- this.KillNickSuccess();
- }
- if (this.Helpers.find(c[3], ":setting")) // vhost applied
- if (this.Helpers.find(r, this.Config.Vhost))
- {
- Console.WriteLine("Vhost applied");
- this.Connected();
- }
- break;
- case "KICK": this.Parser.Kick(c); break;
- case "MODE": this.Parser.Mode(c); break;
- }
- }
- #endregion
- #region from chanserv
- else if (this.Helpers.is_Chanserv(c[0]))
- {
- switch (c[1])
- {
- case "KICK": this.Parser.Kick(c); break;
- case "MODE": this.Parser.Mode(c); break;
- }
- }
- #endregion
- #region normal message
- else
- {
- switch (c[1])
- {
- case "JOIN": this.Parser.Join(c); break;
- case "PART": this.Parser.Part(c); break;
- case "MODE": this.Parser.Mode(c); break;
- case "NICK": this.Parser.Nick(c); break;
- case "KICK": this.Parser.Kick(c); break;
- case "QUIT": this.Parser.Quit(c); break;
- case "NOTICE":
- case "PRIVMSG": this.Parser.Priv(c); break;
- case "INVITE": this.Parser.Inv(c); break;
- }
- }
- #endregion
- #endregion
- #region ping
- if (c[0] == "PING")
- {
- this.Ping(c);
- }
- #endregion
- }
- this.closeStream();
- }
- }
- #endregion
- #region idler
- public void Idler()
- {
- if (!this.IsIdle)
- return;
- if (this.next == this.idle)
- {
- Console.WriteLine("AUTONOM: " + this.idle + " lines passed: do stuff");
- this.next = 0;
- this.idle = 0;
- if (this.idle == 0)
- {
- Random rand = new Random();
- this.next = rand.Next(this.idle_minor, this.idle_major);
- Console.WriteLine("AUTONOM: next autonom action in " + this.next + " lines");
- }
- }
- this.idle++;
- }
- #endregion
- #region streams
- public void openStream()
- {
- Console.WriteLine("Connect to {0}:{1}", this.Config.Server, this.Config.Port);
- this.IsRunning = true;
- this.Tcp = new TcpClient(this.Config.Server, Convert.ToInt32(this.Config.Port));
- this.Stream = this.Tcp.GetStream();
- this.Reader = new StreamReader(this.Stream, Encoding.GetEncoding(this.Config.Encoding));
- this.Writer = new StreamWriter(this.Stream, Encoding.GetEncoding(this.Config.Encoding));
- this.ApplyUser(this.Config.Nick);
- this.ApplyNick(this.Config.Nick);
- }
- public void closeStream()
- {
- this.IsRunning = false;
- Console.WriteLine("Close Stream...");
- this.Writer.Close();
- this.Reader.Close();
- this.Tcp.Close();
- }
- public void write(string line)
- {
- if (line == "")
- return;
- System.Threading.Thread.Sleep(2000);
- Console.WriteLine("RAW: " + line);
- this.Writer.WriteLine(line);
- this.Writer.Flush();
- }
- #endregion
- #region Ping
- private void Ping(string[] c)
- {
- string PingHash = "";
- for (int intI = 1; intI < c.Length; intI++)
- {
- PingHash += c[intI] + " ";
- }
- this.write("PONG " + PingHash);
- }
- #endregion
- #region Server Messages
- private void NickInUse()
- {
- Console.WriteLine("NICK is in use.");
- this.Config.IsAlternate = true;
- this.ApplyNick(this.Config.Alternate);
- }
- private void KillOrIdentify()
- {
- if (this.Config.IsAlternate)
- {
- Console.WriteLine("Try to kill user...");
- this.KillNick(this.Config.Nick, this.Config.Password);
- }
- else
- {
- Console.WriteLine("Identify nick...");
- this.IdentifyNick(this.Config.Password);
- }
- }
- #endregion
- #region own nick
- private void ApplyUser(string nick)
- {
- Console.WriteLine("Use nick " + nick);
- this.write(this.Cmd.user(this.Config.IsInvisible, nick, this.Config.Name));
- }
- private void ApplyNick(string nick)
- {
- Console.WriteLine("Apply nick " + nick);
- string c = this.Cmd.nick(nick);
- Console.WriteLine(c);
- this.write(c);
- }
- private void IdentifyNick(string pass)
- {
- Console.WriteLine("Identify nick");
- this.write(this.Cmd.Nickserv.identify(pass));
- }
- private void KillNick(string nick, string pass)
- {
- Console.WriteLine("Kill nick " + nick);
- string c = this.Cmd.Nickserv.ghost(nick, pass);
- Console.WriteLine(c);
- this.write(c);
- }
- private void KillNickSuccess()
- {
- Console.WriteLine("Kill complete");
- this.Config.IsAlternate = false;
- this.ApplyNick(this.Config.Nick);
- this.KillOrIdentify();
- }
- #endregion
- #region connected
- private void Connected()
- {
- Console.WriteLine("Connected");
- Console.WriteLine("Identify Home-Channel");
- this.write(this.Cmd.Chanserv.identify(this.Config.Home, this.Config.Homepass));
- Console.WriteLine("Join Home-Channel");
- this.write(this.Cmd.join(this.Config.Home, this.Config.Homekey));
- this.IsIdle = true;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement