コード例 #1
0
ファイル: Connect.cs プロジェクト: cmpct/cmpctircd-tests
        public void TestBasicConnectAndRegistration()
        {
            var client = new StandardIrcClient();

            using (var connectedEvent = new ManualResetEventSlim(false)) {
                var registrationInfo = new IrcUserRegistrationInfo()
                {
                    NickName = "cmpct_test",
                    Password = "",
                    UserName = "******",
                    RealName = "cmpct_test"
                };
                client.Connected += (sender, e) => connectedEvent.Set();
                client.Connect("127.0.0.1", 6667, false, registrationInfo);

                if (!connectedEvent.Wait(100))
                {
                    client.Dispose();
                    Assert.Fail("ConnectedEvent not called: server not listening?");
                }

                using (var registrationEvent = new ManualResetEventSlim(false)) {
                    client.Registered += (sender, e) => registrationEvent.Set();

                    if (!registrationEvent.Wait(500))
                    {
                        client.Dispose();
                        Assert.Fail("RegistrationEvent not called: not registered?");
                    }
                }
            }
        }
コード例 #2
0
        protected void Connect(string server, IrcRegistrationInfo registrationInfo)
        {
            // Create new IRC client and connect to given server.
            var client = new StandardIrcClient();

            client.FloodPreventer = new IrcStandardFloodPreventer(4, 2000);
            client.Connected     += IrcClient_Connected;
            client.Disconnected  += IrcClient_Disconnected;
            client.Registered    += IrcClient_Registered;

            // Wait until connection has succeeded or timed out.
            using (var connectedEvent = new ManualResetEventSlim(false))
            {
                client.Connected += (sender2, e2) => connectedEvent.Set();
                client.Connect(server, false, registrationInfo);
                if (!connectedEvent.Wait(10000))
                {
                    client.Dispose();
                    ConsoleUtilities.WriteError("Connection to '{0}' timed out.", server);
                    return;
                }
            }

            // Add new client to collection.
            this.allClients.Add(client);

            Console.Out.WriteLine("Now connected to '{0}'.", server);
        }
コード例 #3
0
        internal void Start(string parChannel, string parNickname)
        {
            if (Client != null)
            {
                return;
            }
            Channel  = parChannel.ToLower();
            Nickname = parNickname;
            var reg = new IrcUserRegistrationInfo
            {
                RealName = Nickname,
                NickName = Nickname,
                UserName = Nickname
            };

            Client = new StandardIrcClient {
                FloodPreventer = new IrcStandardFloodPreventer(4, 2000)
            };

            Client.ErrorMessageReceived += (sender, args) =>
            {
                Console.WriteLine(args.Message);
            };

            Client.Connected += (sender, args) =>
            {
            };

            Client.Disconnected += (sender, args) =>
            {
                Client.Dispose();
                Client = null;
                Thread.Sleep(120000);
                Start(Channel, Nickname);
            };

            Client.Registered += (sender, args) =>
            {
                var tmp = (StandardIrcClient)sender;
                tmp.LocalUser.JoinedChannel += (o, JoinedChannelArgs) =>
                {
                    if (JoinedChannelArgs.Channel.Name == Channel)
                    {
                        tmp.RawMessageReceived += (sender1, eventArgs) =>
                        {
                            if (eventArgs.Message.Command != "PRIVMSG")
                            {
                                return;
                            }
                            var msgArgs = new MessageArgs(eventArgs.Message.Parameters[1], eventArgs.Message.Source.Name);
                            MessageReceivedHandler(msgArgs);
                        };
                    }
                };
                tmp.Channels.Join(Channel);
            };
            Client.Connect(Server, false, reg);
        }
コード例 #4
0
ファイル: IrcBot.cs プロジェクト: Homesweethome/SwagCore
 public void Dispose()
 {
     if (_ircClient != null)
     {
         _ircClient.Quit(_clientTimeout, QuitMessage);
         _ircClient.Dispose();
     }
     GC.SuppressFinalize(this);
 }
コード例 #5
0
ファイル: Connect.cs プロジェクト: cmpct/cmpctircd-tests
        public void TestJoinChannel()
        {
            var client           = new StandardIrcClient();
            var registrationInfo = new IrcUserRegistrationInfo()
            {
                NickName = "cmpct_test",
                Password = "",
                UserName = "******",
                RealName = "cmpct_test"
            };

            client.Connect("127.0.0.1", 6667, false, registrationInfo);


            using (var registrationEvent = new ManualResetEventSlim(false)) {
                // Need to include the registration check for the delay
                client.Registered += (sender, e) => registrationEvent.Set();

                if (!registrationEvent.Wait(500))
                {
                    client.Dispose();
                    Assert.Fail("RegistrationEvent not called: not registered?");
                }
            }

            using (var joinedChannelEvent = new ManualResetEventSlim(false)) {
                client.LocalUser.JoinedChannel += (sender, e) => joinedChannelEvent.Set();
                client.Channels.Join("#test");

                if (!joinedChannelEvent.Wait(500))
                {
                    client.Dispose();
                    Assert.Fail("JoinedChannelEvent not called: channel not joined?");
                }
            }
        }
コード例 #6
0
 private bool ConnectToServer(StandardIrcClient client, string server)
 {
     // Wait until connection has succeeded or timed out.
     using (var connectedEvent = new ManualResetEventSlim(false))
     {
         client.Connected += (sender2, e2) => connectedEvent.Set();
         client.Connect(server, false, RegistrationInfo);
         if (!connectedEvent.Wait(10000))
         {
             client.Dispose();
             //ConsoleUtilities.WriteError("Connection to '{0}' timed out.", server);
             return(false);
         }
     }
     return(true);
 }
コード例 #7
0
ファイル: Program.cs プロジェクト: ziccardi/fehbot
        public async void Run()
        {
            StandardIrcClient client = new StandardIrcClient();

            client.Connected          += Connected;
            client.Registered         += Registered;
            client.Disconnected       += Disconnected;
            client.RawMessageReceived += (object sender, IrcRawMessageEventArgs e) => {
                if (e.RawContent.Contains("NickServ") && e.RawContent.Contains("You are now identified for"))
                {
                    client.Channels.Join(infoFactory.Channels);
                }
            };

            var mongo = new MongoClient();

            db = mongo.GetDatabase("fehBot");

            RESTServer server = new RESTServer(db, 18080);

            Thread serverThread = new Thread(server.StartListening);

            serverThread.Start();

            // Wait until connection has succeeded or timed out.
            using (var connectedEvent = new ManualResetEventSlim(false))
            {
                client.Connected += (sender2, e2) => connectedEvent.Set();

                var registrationInfo = infoFactory.Registration;

                client.Connect(infoFactory.Server, false, registrationInfo);

                if (!connectedEvent.Wait(10000))
                {
                    client.Dispose();
                    stopWaitHandle.Set();
                    Console.Error.WriteLine("Connection Timeout");
                    return;
                }
            }

            stopWaitHandle.WaitOne();
        }
コード例 #8
0
        public void HandleCommands(string commandstring)
        {
            ConnectMethod(commandstring);

            if (commandstring.StartsWith("#/joinc"))
            {
                var channelname = commandstring.Replace("#/joinc", "").Trim();
                var selected    = myclient.Channels.First(item => item.Name == channelname);
                selected.MessageReceived += RecieveMessage;
                selected.NoticeReceived  += (sender, args) => { OutputLog += args.GetText(); };
                myclient.Channels.Join(channelname);
            }

            if (commandstring == ("#/quit"))
            {
                myclient.Quit("exiting");
                myclient.Dispose();
            }
        }
コード例 #9
0
        private void DisposeClient()
        {
            if (_client == null)
            {
                return;
            }

            if (_client.IsConnected)
            {
                _client.Quit(1000, "I'm outta here");
            }

            _client.ErrorMessageReceived -= OnClientErrorMessageReceived;
            _client.Connected            -= OnClientConnected;
            _client.ConnectFailed        -= OnClientConnectFailed;
            _client.Disconnected         -= OnClientDisconnected;
            _client.Registered           -= OnClientRegistered;
            _client.Error += OnError;
            _client.Dispose();
            _client = null;
        }
コード例 #10
0
        void Run()
        {
            Client = new StandardIrcClient();
            DrawCommandLine();

            var Info = new IrcUserRegistrationInfo();

            Info.NickName = Settings.Default.Name;
            Info.RealName = Settings.Default.Realname;
            Info.UserName = Settings.Default.Realname + "Bot";

            using (var connectedEvent = new ManualResetEventSlim(false))
            {
                Client.Connected += (sender2, e2) => connectedEvent.Set();

                Client.Connect(new Uri(Settings.Default.Server), Info);

                if (!connectedEvent.Wait(10000))
                {
                    Client.Dispose();
                    PrintLine(string.Format("Connection to {0} timed out.", Settings.Default.Server), ConsoleColor.Red);
                    return;
                }

                PrintLine(string.Format("Connected to {0}", Settings.Default.Server));

                // *** POST-INIT
                Client.MotdReceived += delegate(Object Sender, EventArgs E)
                {
                    PrintLine("Joining Channels...");
                    Client.Channels.Join(Settings.Default.Channel);
                };

                // *** DEBUG OUTPUT
                Client.RawMessageReceived += delegate(Object Sender, IrcRawMessageEventArgs Event)
                {
                    PrintLine(Event.RawContent);
                };

                // *** PING
                Client.PingReceived += delegate(Object Sender, IrcPingOrPongReceivedEventArgs Event)
                {
                    Client.Ping(Event.Server);
                };

                // *** CHANNEL JOINING
                Client.LocalUser.JoinedChannel += delegate(Object Sender, IrcChannelEventArgs Event)
                {
                    Event.Channel.MessageReceived += Channel_MessageReceived;
                    SayInChannel(OnJoinActions);
                };

                // *** REJOIN AFTER KICK
                Client.LocalUser.LeftChannel += delegate(Object Sender, IrcChannelEventArgs Event)
                {
                    Client.Channels.Join(Event.Channel.Name);
                };

                Int32 Counter = 0;
                while (Client.IsConnected)
                {
                    Thread.Sleep(5);
                    if (DCTimer > 0)
                    {
                        DCTimer--;
                        if (DCTimer == 0)
                        {
                            Client.Disconnect();
                        }
                    }
                    if (++Counter == 12000)
                    {
                        PrintLine("Manual Ping");
                        Client.Ping();
                        Counter = 0;
                    }
                    while (Console.KeyAvailable)
                    {
                        Console.SetCursorPosition(0, 0);
                        ConsoleKeyInfo Key = Console.ReadKey(true);
                        switch (Key.Key)
                        {
                        case ConsoleKey.Enter:
                            ConsoleCommand();
                            CommandLine = "";
                            break;

                        case ConsoleKey.Backspace:
                            if (CommandLine.Length > 0)
                            {
                                CommandLine = CommandLine.Substring(0, CommandLine.Length - 1);
                            }
                            break;

                        default:
                            CommandLine = CommandLine + Key.KeyChar;
                            break;
                        }
                        DrawCommandLine();
                    }
                }
            }
            DrawCommandLine();
        }
コード例 #11
0
ファイル: IrcBot.cs プロジェクト: izrik/IrcDotNet
        protected void Connect(string server, IrcRegistrationInfo registrationInfo)
        {
            // Create new IRC client and connect to given server.
            var client = new StandardIrcClient();
            client.FloodPreventer = new IrcStandardFloodPreventer(4, 2000);
            client.Connected += IrcClient_Connected;
            client.Disconnected += IrcClient_Disconnected;
            client.Registered += IrcClient_Registered;

            // Wait until connection has succeeded or timed out.
            using (var connectedEvent = new ManualResetEventSlim(false))
            {
                client.Connected += (sender2, e2) => connectedEvent.Set();
                client.Connect(server, false, registrationInfo);
                if (!connectedEvent.Wait(10000))
                {
                    client.Dispose();
                    ConsoleUtilities.WriteError("Connection to '{0}' timed out.", server);
                    return;
                }
            }

            // Add new client to collection.
            this.allClients.Add(client);

            Console.Out.WriteLine("Now connected to '{0}'.", server);
        }