/// <summary> /// Start the irc connection /// </summary> /// <returns>True if is ok</returns> public bool Start() { var ret = false; Logger.Info("Starting..."); try { _irc = new IrcFeatures(); _irc.OnConnected += OnConnected; _irc.OnDisconnected += OnDisconnected; _irc.OnReadLine += OnReadLine; _irc.OnChannelMessage += OnMessage; _irc.AutoRetry = true; _irc.AutoRetryLimit = 0; _irc.AutoRetryDelay = 120; _irc.AutoReconnect = true; _irc.AutoRelogin = true; _irc.AutoRejoin = true; _irc.AutoNickHandling = false; _irc.ActiveChannelSyncing = true; _irc.SendDelay = 250; _ircServer = Config.GetStringValue(Globals.YokXmlIrcserver); _ircPort = Config.GetIntValue(Globals.YokXmlIrcport); _ircChannel = Config.GetStringValue(Globals.YokXmlIrcchannel); _ircNick = Config.GetStringValue(Globals.YokXmlIrcnick); _irc.Connect(_ircServer, _ircPort); _irc.Login("yok_" + _ircNick, "remote yok", 0, "IRCYOK"); _irclisten = new Thread(IrcListenThread); _irclisten.Start(); Directory.CreateDirectory(Utils.GetBasePath() + "\\scripts"); Logger.Info("Started Succesfully"); ret = true; } catch (Exception ex) { Logger.Error(ex); } return(ret); }
/// <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; }
/// <inheritdoc /> public string Connect() { if (Connected() || !IRCConfig.Enabled) { return(null); } lock (IRCLock) { try { try { irc.Connect(IRCConfig.URL, IRCConfig.Port); } catch (Exception e) { return("IRC server unreachable: " + e.ToString()); } try { irc.Login(IRCConfig.Nickname, IRCConfig.Nickname); } catch (Exception e) { return("Bot name is already taken: " + e.ToString()); } Login(); JoinChannels(); new Thread(new ThreadStart(IRCListen)) { IsBackground = true }.Start(); return(null); } catch (Exception e) { return(e.ToString()); } } }
/// <summary> /// Disconnect callback /// </summary> /// <param name="o"></param> /// <param name="e"></param> private void OnDisconnected(object o, EventArgs e) { try { Logger.Info("Trying to disconnect..."); if (Config.GetBoolValue(Globals.YokXmlUseirc)) { Logger.Info("NOT Disconnected"); _irc.Connect(_ircServer, _ircPort); _irc.Login("yok_" + _ircNick, "remote yok", 0, "IRCYOK"); } else { Logger.Info("Disconnected"); } } catch (Exception ex) { Logger.Error(ex); } }
/// <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);