Example #1
0
        // ASCII format 3 erase: 15
        void LoopThread()
        {
            byte[] chat_buffer = new byte[MSG_BUFFER];
            int    buffer_used = 0;

            while (cli.Connected && running)
            {
                int free_chars = chat_buffer.Length - buffer_used;
                int rec_len    = 0;
                if (cli.Available > 0 && free_chars > 0)
                {
                    rec_len      = stream.Read(chat_buffer, buffer_used, free_chars);
                    buffer_used += rec_len;
                }

                Thread.Sleep(50);
                if (rec_len > 1)
                {
                    last_ping = 360;                     // Refill last ping
                }
                while (true)
                {
                    // Search for newline in buffer
                    int found_line = Array.IndexOf(chat_buffer, (byte)'\n', 0, buffer_used);

                    if (found_line == -1)
                    {
                        // Reset buffer, discard.
                        if (buffer_used == chat_buffer.Length)
                        {
                            L.Log("E::LoopThread, Line too long, reset.", true);
                            chat_buffer = new byte[MSG_BUFFER];
                            buffer_used = 0;
                        }
                        break;
                    }
                    found_line++;

                    // Add message to the chat buffer, remove '\n' and beginning ':'
                    int offset = (chat_buffer[0] == ':') ? 1 : 0;
                    int length = found_line - 2 - offset;

                    string query = enc.GetString(chat_buffer, offset, length);

                    // Shift back in array
                    for (int i = 0; i < buffer_used - found_line; i++)
                    {
                        chat_buffer[i] = chat_buffer[found_line + i];
                    }

                    buffer_used -= found_line;

                    try {
                        FetchChat(query);
                    } catch (Exception e) {
                        Console.WriteLine(query);
                        Console.WriteLine(e.ToString());
                        L.Dump("E::FetchChat", query, e.ToString());
                    }
                    manager.SetActiveChannel(null);
                }
            }
            L.Log("E::LoopThread, Disconnected", true);
        }
Example #2
0
        void OnServerMessage(string status, string destination, string content)
        {
            L.Log('[' + status + "] " + content);

            if (content == "*** Checking Ident")
            {
                status = "001";
            }

            switch (status)
            {
            case "001":             // Welcome
            case "002":             // Your host
            case "003":             // Server creation date
            case "439":             // ??
                NickAuth(G.settings["nickname"]);
                break;

                #region Nicklist
            case "353": {
                manager.SetActiveChannel(destination);

                for (int i = 0; i < content.Length; ++i)
                {
                    int end_pos = content.IndexOf(' ', i);
                    if (end_pos == -1)
                    {
                        end_pos = content.Length;
                    }

                    if (RANK_CHAR.Contains(content[i].ToString()))
                    {
                        i++;                                 // Skip rank characters
                    }
                    string nick = content.Substring(i, end_pos - i);
                    manager.OnUserJoin(nick, "?");

                    i = end_pos;
                }
            }
            break;

                #endregion
            case "376":             // End of MOTD
                if (G.settings["password"].Length <= 1 && !ready_sent)
                {
                    OnBotReady();                     // Join channels without identification
                }
                break;

            case "396":             // Hostmask changed
                // Requires HostServ to be available and set up
                if (!ready_sent)
                {
                    OnBotReady();
                }
                break;

            case "MODE":
                if (destination == G.settings["nickname"])
                {
                    int pw_len = G.settings["password"].Length;

                    if (!identified)
                    {
                        if (pw_len > 1)
                        {
                            Say("NickServ", "identify " + G.settings["password"]);
                        }
                        identified = true;
                    }

                    if ((pw_len <= 1 || Utils.isYes(G.settings["hostserv"]) == 0) &&
                        content == "+r" && !ready_sent)
                    {
                        OnBotReady();
                    }
                }
                break;

            case "INVITE":
                Join(content);
                break;
            }
        }