Beispiel #1
0
        public ConnectionWorker(IRCServer server)
        {
            this.server = server;
            program     = Program.INSTANCE;
            server.SetWorker(this);
            try
            {
                connection = new TcpClient(server.IP, server.PORT);
                stream     = connection.GetStream();
                if (server.SSL)
                {
                    stream = Protocol.Secure(server, stream);
                }
                Console.WriteLine("Starting " + server.IP);
                this.reader = new StreamReader(stream);
                this.writer = new StreamWriter(stream);


                while (!stream.CanWrite)
                {
                    ;
                }
                this.writer.Write("NICK " + server.Nick + "\r\n");
                this.writer.Write("USER " + server.RealName + " 0 * :" + server.RealName + "\r\n");
                this.writer.Flush();
                Working = true;
            }
            catch (IOException)
            {
                Console.WriteLine("Unable to connect to " + server.IP + ", possibly SSL mismatch or host doesn't exist.");
            }
        }
        public IncomingMessage(IRCServer Server, string Raw, string Source, string Command, string Target, string Message)
        {
            this.Server  = Server;
            this.Raw     = Raw;
            this.Source  = Source;
            this.Command = Command;
            if (Command.Equals("PRIVMSG") || Command.Equals("NICK") || Command.Equals("JOIN") || Command.Equals("PART") || Command.Equals("QUIT") && Server.Connected)
            {
                Sender = Source.Substring(0, Source.IndexOf("!"));
                Host   = Source.Substring(Source.IndexOf("@") + 1);
            }
            if (Target != null)
            {
                string[] dummy = Target.Split(' ');

                this.Target  = dummy[0];
                TargetParams = (dummy.Length == 2 ? dummy[1] : null);
            }
            else
            {
                this.Target = TargetParams = null;
            }
            if (Message == null)
            {
                this.Message = "";
            }
            else
            {
                this.Message = Message.Trim();
            }
        }
        public ConnectionWorker(IRCServer server)
        {
            this.server = server;
            program = Program.INSTANCE;
            server.SetWorker(this);
            try
            {
                connection = new TcpClient(server.IP, server.PORT);
                stream = connection.GetStream();
                if (server.SSL)
                {

                    stream = Protocol.Secure(server, stream);
                }
                Console.WriteLine("Starting " + server.IP);
                this.reader = new StreamReader(stream);
                this.writer = new StreamWriter(stream);

                while (!stream.CanWrite) ;
                this.writer.Write("NICK " + server.Nick + "\r\n");
                this.writer.Write("USER " + server.RealName + " 0 * :" + server.RealName + "\r\n");
                this.writer.Flush();
                Working = true;
            }
            catch (IOException)
            {
                Console.WriteLine("Unable to connect to " + server.IP + ", possibly SSL mismatch or host doesn't exist.");
            }
        }
        public IncomingMessage(IRCServer Server, string Raw, string Source, string Command, string Target, string Message)
        {
            this.Server = Server;
            this.Raw = Raw;
            this.Source = Source;
            this.Command = Command;
            if (Command.Equals("PRIVMSG") || Command.Equals("NICK") || Command.Equals("JOIN") || Command.Equals("PART") || Command.Equals("QUIT") && Server.Connected)
            {
                Sender = Source.Substring(0, Source.IndexOf("!"));
                Host = Source.Substring(Source.IndexOf("@") + 1);
            }
            if (Target != null)
            {
                string[] dummy = Target.Split(' ');

                this.Target = dummy[0];
                TargetParams = (dummy.Length == 2 ? dummy[1] : null);
            }
            else
            {
                this.Target = TargetParams = null;
            }
            if (Message == null)
            {
                this.Message = "";
            }
            else
            {
                this.Message = Message.Trim();
            }
        }
Beispiel #5
0
        public static void SetKeyValue(IRCServer server, string key, object value)
        {
            SQLiteCommand cmd = new SQLiteCommand("insert into keyvalues (server, key, value) values (@server, @key, @value)", GetConnection());

            cmd.Parameters.AddWithValue("@server", server.IP);
            cmd.Parameters.AddWithValue("@key", key);
            cmd.Parameters.AddWithValue("@value", value.ToString());
            cmd.ExecuteNonQuery();
        }
Beispiel #6
0
        public static List<IRCServer> LoadServers()
        {
            string[] files = Directory.GetFiles("servers/");
            if (files.Length > 0)
            {
                List<IRCServer> servers = new List<IRCServer>(files.Length);
                foreach (string file in files)
                {
                    XDocument xd = XDocument.Load(file);
                    Console.Write("Loading " + file + "\n");

                    XElement el = xd.Element("connection");
                    string ip = el.Attribute("ip").Value;
                    int port = int.Parse(el.Attribute("port").Value);

                    string nick = el.Element("name").Value;
                    string pass = el.Element("pass").Value;
                    string altnick = el.Element("altnick").Value;
                    string realname = el.Element("realname").Value;
                    bool loggingRaw = bool.Parse(el.Element("lograw").Value);
                    bool ssl = bool.Parse(el.Element("ssl").Value);
                    List<IRCChannel> channels = new List<IRCChannel>();

                    XElement cc = el.Element("channels");
                    foreach (XElement dd in cc.Elements())
                    {
                        string channel = dd.Element("name").Value;
                        bool reconnect = bool.Parse(dd.Element("reconnect").Value);
                        IRCChannel chan = new IRCChannel(channel, reconnect);
                        channels.Add(chan);
                    }

                    XElement users = el.Element("users");
                    Dictionary<string, UserAccess> serverUsers = new Dictionary<string, UserAccess>();
                    foreach (XElement user in users.Elements())
                    {
                        string host = user.Element("host").Value;
                        string access = user.Element("access").Value;
                        UserAccess ua = UserAccessAttr.GetByAccess(int.Parse(access));
                        serverUsers.Add(host, ua);
                    }
                    IRCServer server = new IRCServer(file, ip, port, channels, ssl, nick, pass, altnick, realname, serverUsers, loggingRaw);
                    servers.Add(server);
                }
                Console.WriteLine("Loaded " + servers.Count + " servers");
                return servers;
            }
            else
            {
                return null;
            }
        }
Beispiel #7
0
        public static List <IRCServer> LoadServers()
        {
            string[] files = Directory.GetFiles("servers/");
            if (files.Length > 0)
            {
                List <IRCServer> servers = new List <IRCServer>(files.Length);
                foreach (string file in files)
                {
                    XDocument xd = XDocument.Load(file);
                    Console.Write("Loading " + file + "\n");

                    XElement el   = xd.Element("connection");
                    string   ip   = el.Attribute("ip").Value;
                    int      port = int.Parse(el.Attribute("port").Value);

                    string            nick       = el.Element("name").Value;
                    string            pass       = el.Element("pass").Value;
                    string            altnick    = el.Element("altnick").Value;
                    string            realname   = el.Element("realname").Value;
                    bool              loggingRaw = bool.Parse(el.Element("lograw").Value);
                    bool              ssl        = bool.Parse(el.Element("ssl").Value);
                    List <IRCChannel> channels   = new List <IRCChannel>();

                    XElement cc = el.Element("channels");
                    foreach (XElement dd in cc.Elements())
                    {
                        string     channel   = dd.Element("name").Value;
                        bool       reconnect = bool.Parse(dd.Element("reconnect").Value);
                        IRCChannel chan      = new IRCChannel(channel, reconnect);
                        channels.Add(chan);
                    }

                    XElement users = el.Element("users");
                    Dictionary <string, UserAccess> serverUsers = new Dictionary <string, UserAccess>();
                    foreach (XElement user in users.Elements())
                    {
                        string     host   = user.Element("host").Value;
                        string     access = user.Element("access").Value;
                        UserAccess ua     = UserAccessAttr.GetByAccess(int.Parse(access));
                        serverUsers.Add(host, ua);
                    }
                    IRCServer server = new IRCServer(file, ip, port, channels, ssl, nick, pass, altnick, realname, serverUsers, loggingRaw);
                    servers.Add(server);
                }
                Console.WriteLine("Loaded " + servers.Count + " servers");
                return(servers);
            }
            else
            {
                return(null);
            }
        }
Beispiel #8
0
        public static string GetKeyValue(IRCServer server, string key)
        {
            SQLiteCommand cmd = new SQLiteCommand("select value from keyvalues where server = @ip and key = @key", GetConnection());
            cmd.Parameters.AddWithValue("@ip", server.IP);
            cmd.Parameters.AddWithValue("@key", key);
            SQLiteDataReader rs = cmd.ExecuteReader();
            if (rs.HasRows)
            {
                if (rs.Read())
                {
                    return rs["value"].ToString();
                }
            }

            return null;
        }
Beispiel #9
0
        public static string GetKeyValue(IRCServer server, string key)
        {
            SQLiteCommand cmd = new SQLiteCommand("select value from keyvalues where server = @ip and key = @key", GetConnection());

            cmd.Parameters.AddWithValue("@ip", server.IP);
            cmd.Parameters.AddWithValue("@key", key);
            SQLiteDataReader rs = cmd.ExecuteReader();

            if (rs.HasRows)
            {
                if (rs.Read())
                {
                    return(rs["value"].ToString());
                }
            }

            return(null);
        }
Beispiel #10
0
        public static Stream Secure(IRCServer server, Stream stream)
        {
            RemoteCertificateValidationCallback certValidation = delegate { return true; };

            RemoteCertificateValidationCallback certValidationWithIrcAsSender = delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            {
                return certValidation(server, certificate, chain, sslPolicyErrors);
            };
            LocalCertificateSelectionCallback selectionCallback = delegate (object sender, string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate, string[] acceptableIssuers)
            {
                if (localCertificates == null || localCertificates.Count == 0)
                {
                    return null;
                }
                return localCertificates[0];
            };
            SslStream sslStream = new SslStream(stream, false, certValidationWithIrcAsSender, selectionCallback);
            sslStream.AuthenticateAsClient(server.IP);
            return sslStream;
        }
Beispiel #11
0
        public static bool Save(IRCServer server)
        {
            XDocument xd         = new XDocument();
            XElement  connection = new XElement("connection", new XAttribute("ip", server.IP), new XAttribute("port", server.PORT));

            connection.Add(new XElement("name", server.Nick));
            connection.Add(new XElement("pass", server.Pass));
            connection.Add(new XElement("altnick", server.AltNick));
            connection.Add(new XElement("realname", server.RealName));
            connection.Add(new XElement("ssl", server.SSL));
            connection.Add(new XElement("lograw", server.LoggingRaw));
            xd.Add(connection);
            XElement channels = new XElement("channels");

            foreach (IRCChannel chan in server.GetChannels())
            {
                XElement channel = new XElement("channel");
                channel.Add(new XElement("name", chan.Channel)); channel.Add(new XElement("reconnect", chan.Reconnect));
                channels.Add(channel);
            }
            connection.Add(channels);

            XElement users = new XElement("users");

            int index = 0;

            foreach (KeyValuePair <string, UserAccess> access in server.Users)
            {
                XElement user = new XElement("user");
                if (index++ == server.Users.Count)
                {
                    break;
                }
                user.Add(new XElement("host", access.Key));
                user.Add(new XElement("access", UserAccessAttr.GetByValue(access.Value)));
                users.Add(user);
            }
            connection.Add(users);
            xd.Save(server.File);
            return(true);
        }
Beispiel #12
0
        public static Stream Secure(IRCServer server, Stream stream)
        {
            RemoteCertificateValidationCallback certValidation = delegate { return(true); };

            RemoteCertificateValidationCallback certValidationWithIrcAsSender = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            {
                return(certValidation(server, certificate, chain, sslPolicyErrors));
            };
            LocalCertificateSelectionCallback selectionCallback = delegate(object sender, string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate, string[] acceptableIssuers)
            {
                if (localCertificates == null || localCertificates.Count == 0)
                {
                    return(null);
                }
                return(localCertificates[0]);
            };
            SslStream sslStream = new SslStream(stream, false, certValidationWithIrcAsSender, selectionCallback);

            sslStream.AuthenticateAsClient(server.IP);
            return(sslStream);
        }
Beispiel #13
0
        public static bool Save(IRCServer server)
        {
            XDocument xd = new XDocument();
            XElement connection = new XElement("connection", new XAttribute("ip", server.IP), new XAttribute("port", server.PORT));
            connection.Add(new XElement("name", server.Nick));
            connection.Add(new XElement("pass", server.Pass));
            connection.Add(new XElement("altnick", server.AltNick));
            connection.Add(new XElement("realname", server.RealName));
            connection.Add(new XElement("ssl", server.SSL));
            connection.Add(new XElement("lograw", server.LoggingRaw));
            xd.Add(connection);
            XElement channels = new XElement("channels");
            foreach (IRCChannel chan in server.GetChannels())
            {
                XElement channel = new XElement("channel");
                channel.Add(new XElement("name", chan.Channel)); channel.Add(new XElement("reconnect", chan.Reconnect));
                channels.Add(channel);
            }
            connection.Add(channels);

            XElement users = new XElement("users");

            int index = 0;
            foreach (KeyValuePair<string, UserAccess> access in server.Users)
            {
                XElement user = new XElement("user");
                if (index++ == server.Users.Count)
                {
                    break;
                }
                user.Add(new XElement("host", access.Key));
                user.Add(new XElement("access", UserAccessAttr.GetByValue(access.Value)));
                users.Add(user);
            }
            connection.Add(users);
            xd.Save(server.File);
            return true;
        }
Beispiel #14
0
 public static void SetKeyValue(IRCServer server, string key, object value)
 {
     SQLiteCommand cmd = new SQLiteCommand("insert into keyvalues (server, key, value) values (@server, @key, @value)", GetConnection());
     cmd.Parameters.AddWithValue("@server", server.IP);
     cmd.Parameters.AddWithValue("@key", key);
     cmd.Parameters.AddWithValue("@value", value.ToString());
     cmd.ExecuteNonQuery();
 }
Beispiel #15
0
 public void OnConnected(IRCServer server)
 {
     connected.Add(server);
     toConnect.Remove(server);
     Console.WriteLine("Established connection to " + server.ToString());
 }
Beispiel #16
0
 public void OnDispose(IRCServer server)
 {
     connected.Remove(server);
 }
Beispiel #17
0
 public void OnDispose(IRCServer server)
 {
     connected.Remove(server);
 }
Beispiel #18
0
 public void OnConnected(IRCServer server)
 {
     connected.Add(server);
     toConnect.Remove(server);
     Console.WriteLine("Established connection to " + server.ToString());
 }