Ejemplo n.º 1
0
 /// <inheritdoc />
 public string SendMessageDirect(string message, string channel)
 {
     try
     {
         if (!Connected())
         {
             return("Disconnected.");
         }
         lock (IRCLock)
         {
             SendType st;
             if (channel.Contains(PrivateMessageMarker))
             {
                 channel = channel.Replace(PrivateMessageMarker, "");
                 st      = SendType.Notice;
             }
             else
             {
                 st = SendType.Message;
             }
             irc.SendMessage(st, channel, message);
         }
         return(null);
     }
     catch (Exception e)
     {
         return(e.ToString());
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Gibt eine Nachricht in allen Channels bekannt
 /// </summary>
 /// <param name="message">Die Nachricht</param>
 public void Announce(string message)
 {
     foreach (ServerChannel channel in Settings.Channels)
     {
         _connection.SendMessage(SendType.Message, channel.Name, message);
         Log.Information("An {Receiver:l}: {Message:l}", channel.Name, message);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Baut eine Verbindung mit den angegebenen Daten auf
        /// </summary>
        public void Connect()
        {
            _connection = new IrcFeatures();

            _connection.ActiveChannelSyncing = true;

            _connection.CtcpSource       = "Frag Suchiman in freenode";
            _connection.CtcpUrl          = _connection.CtcpSource;
            _connection.CtcpUserInfo     = "Ich bin ein automatisch denkendes Wesen auch bekannt als Bot";
            _connection.CtcpVersion      = "FritzBot:v3:" + Environment.OSVersion.Platform.ToString();
            _connection.Encoding         = Encoding.GetEncoding("iso-8859-1");
            _connection.EnableUTF8Recode = true;

            _connection.OnChannelAction  += _connection_OnMessage;
            _connection.OnChannelMessage += _connection_OnMessage;
            _connection.OnChannelNotice  += _connection_OnMessage;

            _connection.OnQueryAction  += _connection_OnMessage;
            _connection.OnQueryMessage += _connection_OnMessage;
            _connection.OnQueryNotice  += _connection_OnMessage;

            _connection.OnNickChange += _connection_OnNickChange;
            _connection.OnJoin       += _connection_OnJoin;
            _connection.OnKick       += _connection_OnKick;
            _connection.OnPart       += _connection_OnPart;
            _connection.OnQuit       += _connection_OnQuit;

            _connection.OnConnectionError += _connection_OnConnectionError;

            _connection.Connect(Settings.Address, Settings.Port);
            _connection.Login(Settings.Nickname, Settings.Nickname, 0, Settings.Nickname);

            if (Settings.NickServPassword != null)
            {
                _connection.SendMessage(SendType.Message, "nickserv", "identify " + Settings.NickServPassword);
            }

            foreach (ServerChannel channel in Settings.Channels)
            {
                _connection.RfcJoin(channel.Name, Priority.Critical);
            }

            _listener = Toolbox.SafeThreadStart("ListenThread " + Settings.Address, true, _connection.Listen);

            Connected = true;
        }
Ejemplo n.º 4
0
        /// <inheritdoc />
        public override Task <bool> Connect(CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
        {
            disconnecting = false;
            lock (this)
                try
                {
                    client.Connect(address, port);

                    cancellationToken.ThrowIfCancellationRequested();

                    if (passwordType == IrcPasswordType.Server)
                    {
                        client.Login(nickname, nickname, 0, nickname, password);
                    }
                    else
                    {
                        if (passwordType == IrcPasswordType.Sasl)
                        {
                            client.WriteLine("CAP REQ :sasl", Priority.Critical);                             // needs to be put in the buffer before anything else
                            cancellationToken.ThrowIfCancellationRequested();
                        }

                        client.Login(nickname, nickname, 0, nickname);
                    }

                    if (passwordType == IrcPasswordType.NickServ)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        client.SendMessage(SendType.Message, "NickServ", String.Format(CultureInfo.InvariantCulture, "IDENTIFY {0}", password));
                    }
                    else if (passwordType == IrcPasswordType.Sasl)
                    {
                        // wait for the sasl ack or timeout
                        var recievedAck    = false;
                        var recievedPlus   = false;
                        client.OnReadLine += (sender, e) =>
                        {
                            if (e.Line.Contains("ACK :sasl", StringComparison.Ordinal))
                            {
                                recievedAck = true;
                            }
                            else if (e.Line.Contains("AUTHENTICATE +", StringComparison.Ordinal))
                            {
                                recievedPlus = true;
                            }
                        };

                        var startTime = DateTimeOffset.Now;
                        var endTime   = DateTimeOffset.Now.AddSeconds(TimeoutSeconds);
                        cancellationToken.ThrowIfCancellationRequested();

                        var listenTimeSpan = TimeSpan.FromMilliseconds(10);
                        for (; !recievedAck && DateTimeOffset.Now <= endTime; asyncDelayer.Delay(listenTimeSpan, cancellationToken).GetAwaiter().GetResult())
                        {
                            client.Listen(false);
                        }

                        client.WriteLine("AUTHENTICATE PLAIN", Priority.Critical);
                        cancellationToken.ThrowIfCancellationRequested();

                        for (; !recievedPlus && DateTimeOffset.Now <= endTime; asyncDelayer.Delay(listenTimeSpan, cancellationToken).GetAwaiter().GetResult())
                        {
                            client.Listen(false);
                        }

                        // Stolen! https://github.com/znc/znc/blob/1e697580155d5a38f8b5a377f3b1d94aaa979539/modules/sasl.cpp#L196
                        var authString = String.Format(CultureInfo.InvariantCulture, "{0}{1}{0}{1}{2}", nickname, '\0', password);
                        var b64        = Convert.ToBase64String(Encoding.UTF8.GetBytes(authString));
                        var authLine   = String.Format(CultureInfo.InvariantCulture, "AUTHENTICATE {0}", b64);
                        var chars      = authLine.ToCharArray();
                        client.WriteLine(authLine, Priority.Critical);

                        cancellationToken.ThrowIfCancellationRequested();
                        client.WriteLine("CAP END", Priority.Critical);
                    }

                    client.Listen(false);

                    listenTask = Task.Factory.StartNew(() =>
                    {
                        while (!disconnecting && client.IsConnected && client.Nickname != nickname)
                        {
                            client.ListenOnce(true);
                            if (disconnecting || !client.IsConnected)
                            {
                                break;
                            }
                            client.Listen(false);

                            // ensure we have the correct nick
                            if (client.GetIrcUser(nickname) == null)
                            {
                                client.RfcNick(nickname);
                            }
                        }

                        client.Listen();
                    }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
                }
                catch (OperationCanceledException)
                {
                    throw;
                }
            catch (Exception e)
            {
                Logger.LogWarning("Unable to connect to IRC: {0}", e);
                return(false);
            }

            return(true);
        }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
Ejemplo n.º 5
0
        /// <summary>
        ///     Lua callback
        /// </summary>
        /// <param name="text">string from lua vm</param>
        private void LuaCallback(string text)
        {
            Logger.Info("[LUA] " + text);

            _irc.SendMessage(SendType.Message, _ircChannel, text);
        }