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.IO;
- using System.Net.Sockets;
- using System.Collections;
- using System.Diagnostics;
- using Fruffi.Bot;
- namespace Fruffi.Irc
- {
- class Irc
- {
- public const string SPAMSCANNER = "SPAMSCANNER";
- public const string NICKSERV = "NICKSERV";
- public const string CHANSERV = "CHANSERV";
- public const string SERVER = "SERVER";
- public const int SENDER_PING = 10;
- public const int SENDER_SERVER = 20;
- public const int SENDER_NICKSERV = 30;
- public const int SENDER_CHANSERV = 40;
- public const int SENDER_SPAMSCANNER = 50;
- public const int SENDER_USER = 100;
- public const int PRIVMSG_FROM_SELF = 10;
- public const int PRIVMSG_FROM_OWNER = 20;
- public const int PRIVMSG_FROM_USER = 30;
- public const int PRIVMSG_FROM_CHANNELOWNER = 30;
- public const int PRIVMSG_FROM_CHANNELADMIN = 40;
- public const int PRIVMSG_FROM_CHANNELOP = 50;
- public const int PRIVMSG_FROM_CHANNELHOP = 60;
- public const int PRIVMSG_FROM_CHANNELVOICE = 70;
- public const int PRIVMSG_FROM_CHANNELNORMAL = 80;
- public const int READ_ID_SERVERCONFIG = 005;
- public const int READ_ID_TOPIC = 332;
- public const int READ_ID_TOPICOWNER = 333;
- public const int READ_ID_NAMESLIST = 353;
- public const int READ_ID_NAMESLISTEND = 366;
- public const int READ_ID_MOTD = 372;
- public const int READ_ID_MOTD_START = 375;
- public const int READ_ID_MOTD_END = 376;
- public const int READ_ID_NICKINUSE = 433;
- public const string READ_JOIN = "JOIN";
- public const string READ_PART = "PART";
- public const string READ_NICK = "NICK";
- public const string READ_QUIT = "QUIT";
- public const string READ_KICK = "KICK";
- public const string READ_MODE = "MODE";
- public const string READ_NOTICE = "NOTICE";
- public const string READ_PRIVMSG = "PRIVMSG";
- public const string READ_INVITE = "INVITE";
- public const string READ_PING = "PING";
- public const string READ_GHOST_SUCCESS = ":ghost";
- public const string READ_VHOST_SUCCESS = ":setting";
- //public const string READ_CTCP = "\u0001VERSION\u0001";
- //http://www.irchelp.org/irchelp/rfc/ctcpspec.html
- public const string SEND_USER = "USER {0} {1} illi.be :{2}";
- public const string SEND_NICK = "NICK {0}";
- public const string SEND_JOIN = "JOIN {0} {1}";
- public const string SEND_PART = "PART {0} :{1}";
- public const string SEND_QUIT = "QUIT :{0}";
- public const string SEND_PRIVMSG = "PRIVMSG {0} :{1}";
- public const string SEND_NOTICE = "NOTICE {0} :{1}";
- public const string SEND_ACTION = "PRIVMSG {0} :\u0001" + "ACTION {1}\u0001";
- public const string SEND_PONG = "PONG {0}";
- public const string SEND_IDENTIFY = "NICKSERV identify {0}";
- public const string SEND_GHOST = "NICKSERV ghost {0} {1}";
- public const string SEND_MODE_CHANNEL_ADD = "MODE {0} +{1} {2}";
- public const string SEND_MODE_CHANNEL_DEL = "MODE {0} -{1} {2}";
- public const string SEND_MODE_USER_ADD = "MODE {0} +{1}";
- public const string SEND_MODE_USER_DEL = "MODE {0} -{1}";
- public const string CONSOLE_CHANNEL_TEXT = "{0} <{1}>\t{2}";
- public const string ENV_REPLACE_REPLYTO = "%REPLYTO%";
- private TcpClient Tcp;
- private NetworkStream Stream;
- private StreamWriter Writer;
- private StreamReader Reader;
- private IrcSend __Send = new IrcSend();
- private IrcRead __Read = new IrcRead();
- private static Irc __Instance;
- private IrcServerConfig __ServerConfig;
- public static Irc Instance
- {
- get { return __Instance; }
- }
- public IrcSend Send
- {
- get { return this.__Send; }
- }
- public IrcRead Read
- {
- get { return this.__Read; }
- }
- public IrcServerConfig ServerConfig
- {
- get { return this.__ServerConfig; }
- }
- public static bool __IsRunning = true;
- public static void sleep()
- {
- Irc.__IsRunning = false;
- }
- public static void wakeup()
- {
- Irc.__IsRunning = true;
- }
- public Irc()
- {
- Fruffi.sayBlock(new string[] {"Connecting to IRC"});
- __Instance = this;
- while (true)
- {
- this.openStream();
- string line;
- while ((line = this.Reader.ReadLine()) != null)
- this.read(line);
- this.closeStream();
- try
- {
- }
- catch (Exception e)
- {
- Fruffi.sayError(e);
- }
- }
- }
- public void read(string line)
- {
- string[] extract = IrcHelper.extract(line);
- string sender = extract[0];
- string handle = extract[1].ToUpper();
- string response;
- IrcChannelResponse channelResponse;
- //Fruffi.say(line, 0);
- switch (IrcHelper.sender(sender))
- {
- case Irc.SENDER_SPAMSCANNER:
- Fruffi.say(line);
- break;
- case Irc.SENDER_PING:
- Fruffi.say(line);
- this.write(this.Send.Pong(extract));
- break;
- case Irc.SENDER_USER:
- switch (handle)
- {
- case Irc.READ_JOIN:
- try
- {
- channelResponse = this.Read.Join(extract);
- response = channelResponse.Brain.Response;
- response = channelResponse.Brain.Response.Replace(Irc.ENV_REPLACE_REPLYTO, channelResponse.Nick);
- this.write(this.Send.WriteChannel(channelResponse.Channel, response));
- }
- catch (NullReferenceException)
- {
- }
- break;
- case Irc.READ_NICK:
- // update user
- break;
- case Irc.READ_PART:
- // remove user
- break;
- case Irc.READ_KICK:
- // remove user
- break;
- case Irc.READ_QUIT:
- // remove user
- break;
- case Irc.READ_NOTICE:
- // parse
- break;
- case Irc.READ_PRIVMSG:
- channelResponse = this.Read.Priv
- (
- IrcHelper.extract
- (
- IrcHelper.RemoveChannelWar
- (
- IrcHelper.ConvertUtf8ToIso(line)
- )
- )
- );
- try
- {
- response = channelResponse.Brain.Response;
- if (channelResponse.BypassEnv == false)
- response = channelResponse.Brain.Response.Replace(Irc.ENV_REPLACE_REPLYTO, channelResponse.Nick);
- if (channelResponse.Brain.isAction == false)
- this.write(this.Send.WriteChannel(channelResponse.Channel, response));
- else
- this.write(this.Send.WriteActionChannel(channelResponse.Channel, response));
- Fruffi.sayNotImplemented(new string[] { "SAY",
- String.Format(Irc.CONSOLE_CHANNEL_TEXT, channelResponse.Channel, channelResponse.Nick, response)
- });
- }
- catch (NullReferenceException)
- {
- }
- // parse
- break;
- case Irc.READ_INVITE:
- // parse
- break;
- case Irc.READ_MODE:
- // parse
- default:
- break;
- }
- break;
- case Irc.SENDER_SERVER:
- try
- {
- try
- {
- int h = 0;
- h = Convert.ToInt32(handle);
- switch (h)
- {
- case Irc.READ_ID_NICKINUSE:
- Fruffi.Configs.Nick.isAlternate = true;
- this.write(this.Send.Nick(Fruffi.Configs.Nick.AltNick));
- break;
- case Irc.READ_ID_MOTD_END:
- if (Fruffi.Configs.Nick.isAlternate == true)
- this.write(this.Send.NickServ.Ghost(Fruffi.Configs.Nick.Nick, Fruffi.Configs.Nick.Password));
- else
- this.write(this.Send.NickServ.Identify(Fruffi.Configs.Nick.Password));
- break;
- case Irc.READ_ID_SERVERCONFIG:
- Fruffi.sayNotImplemented(new string[] { "SERVERCONFIG" });
- this.__ServerConfig = new IrcServerConfig(line);
- break;
- case Irc.READ_ID_NAMESLISTEND:
- case Irc.READ_ID_TOPIC:
- case Irc.READ_ID_NAMESLIST:
- case Irc.READ_ID_TOPICOWNER:
- case Irc.READ_ID_MOTD_START:
- case Irc.READ_ID_MOTD:
- default:
- break;
- }
- }
- catch(EncoderFallbackException)
- {
- }
- }
- catch (Exception)
- {
- switch (handle)
- {
- case Irc.READ_NOTICE:
- /**
- * if umode +B the following lines will be sent by the irc-server:
- *
- * :irc.ham.de.euirc.net NOTICE AUTH :- irc.ham.de.euirc.net Bot Message of the Day -
- * :irc.ham.de.euirc.net NOTICE AUTH :- 28/4/2008 18:03
- * :irc.ham.de.euirc.net NOTICE AUTH :End of /BOTMOTD command.
- * */
- Fruffi.say(line);
- break;
- }
- }
- break;
- case Irc.SENDER_CHANSERV:
- switch (handle)
- {
- case Irc.READ_KICK:
- Fruffi.sayNotImplemented(new string[] { "CS Kick" });
- break;
- case Irc.READ_MODE:
- Fruffi.sayNotImplemented(new string[] { "CS MODE" });
- break;
- default:
- break;
- }
- break;
- case Irc.SENDER_NICKSERV:
- switch (handle)
- {
- case Irc.READ_NOTICE:
- string serverresponse = extract[3];
- if (Fruffi.Configs.Nick.isAlternate)
- if (IrcHelper.find(serverresponse, Irc.READ_GHOST_SUCCESS)) // ghost success
- {
- this.write(this.Send.Nick(Fruffi.Configs.Nick.Nick));
- this.write(this.Send.NickServ.Identify(Fruffi.Configs.Nick.Password));
- Fruffi.Configs.Nick.isAlternate = false;
- Fruffi.sayBlock(new string[] { "GHOST: SUCCESS" });
- }
- if (IrcHelper.find(serverresponse, Irc.READ_VHOST_SUCCESS)) // vhost applied
- if (IrcHelper.find(line, Fruffi.Configs.Nick.Vhost))
- {
- Fruffi.sayBlock(new string[] { "VHOST: APPLIED" });
- this.write(this.Send.Join(Fruffi.Configs.Home.Channel, Fruffi.Configs.Home.Key));
- this.write(this.Send.ModeUserAdd(Fruffi.Configs.Nick.Nick, 'p'));
- this.write(this.Send.ModeUserAdd(Fruffi.Configs.Nick.Nick, 'B'));
- this.write(this.Send.Join("fluchen"));
- //this.write(this.Send.Join("gayfamily"));
- //this.write(this.Send.Join("php.de"));
- //this.write(this.Send.Join("mikrocontroller.net"));
- this.write(this.Send.Join("frei-bier-bude"));
- //this.write(this.Send.Join("marvin"));
- //this.write(this.Send.Join("bot"));
- }
- break;
- case Irc.READ_KICK:
- Fruffi.sayNotImplemented(new string[] { "NS Kick" });
- break;
- case Irc.READ_MODE:
- Fruffi.sayNotImplemented(new string[] { "NS Mode" });
- break;
- default:
- break;
- }
- break;
- }
- }
- public void write(string line)
- {
- if (line == "")
- return;
- if (Irc.__IsRunning == false)
- return;
- System.Threading.Thread.Sleep(2000);
- line = IrcHelper.RemoveChannelWar(IrcHelper.ConvertUtf8ToIso(line));
- Fruffi.say("RAW: " + line, 1);
- this.Writer.WriteLine(line);
- this.Writer.Flush();
- }
- private bool openStream()
- {
- Fruffi.sayBlock(new string[] { "open Stream", Fruffi.Configs.Server.Uri, Fruffi.Configs.Server.Port.ToString(), Fruffi.Configs.Socket.Encoding });
- this.Tcp = new TcpClient(Fruffi.Configs.Server.Uri, Convert.ToInt32(Fruffi.Configs.Server.Port));
- this.Stream = this.Tcp.GetStream();
- this.Reader = new StreamReader(this.Stream, Encoding.GetEncoding(Fruffi.Configs.Socket.Encoding));
- this.Writer = new StreamWriter(this.Stream, Encoding.GetEncoding(Fruffi.Configs.Socket.Encoding));
- this.write(this.Send.User(Fruffi.Configs.Nick.isInvisible, Fruffi.Configs.Nick.Nick, Fruffi.Configs.Nick.Name));
- this.write(this.Send.Nick(Fruffi.Configs.Nick.Nick));
- return true;
- }
- private bool closeStream()
- {
- this.Writer.Close();
- this.Reader.Close();
- this.Tcp.Close();
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement