Beispiel #1
0
        /// <returns>The currently active channel</returns>
        public Channel GetChannel()
        {
            if (m_active_channel == null)
            {
                return(null);
            }

            Channel channel = GetChannel(m_active_channel);

            if (channel == null)
            {
                channel = m_tmp_channel;
            }

            if (channel == null)
            {
                // Temporary channel for private messages
                m_tmp_channel = new Channel(m_active_channel);
                channel       = m_tmp_channel;
                if (!channel.IsPrivate())
                {
                    L.Log("Manager::GetChannel() Channel not found: " + m_active_channel +
                          ". Creating temporary object.", true);
                }
            }
            return(channel);
        }
Beispiel #2
0
        /// <summary>Changes the per-thread channel</summary>
        /// <param name="channel_name">Channel name: null clears garbage</param>
        public void SetActiveChannel(string channel_name)
        {
            var tid = Thread.CurrentThread.ManagedThreadId;

            if (channel_name == null)
            {
                m_active_channel.Remove(tid);
                return;
            }

            Channel channel = GetChannel(channel_name);

            if (channel == null)
            {
                channel = new Channel(channel_name);
                if (!channel.IsPrivate())
                {
                    L.Log("Manager::SetActiveChannel channel '" + channel_name +
                          "' not found! Creating dummy channel.");
                }
            }
            m_active_channel[tid] = channel;
        }
Beispiel #3
0
        void Cmd_timebomb(string nick, string message)
        {
            Channel chan = p_manager.GetChannel();

            if (chan.IsPrivate())
            {
                return;
            }

            string channel = chan.GetName();

            if (m_timers.ContainsKey(channel))
            {
                chan.Say("Only one timebomb is allowed at a time.");
                return;
            }
            if (m_cooldown.ContainsKey(channel))
            {
                chan.Say("Assembling a new bomb. Please wait... (" +
                         (int)(m_cooldown[channel].GetRemaining() / 1000.0) + "s)");
                return;
            }

            string dst_name = Chatcommand.GetNext(ref message);

            dst_name = chan.FindNickname(dst_name, false);
            if (dst_name == null)
            {
                chan.Say(nick + ": Unknown or invalid nickname specified.");
                return;
            }

            if (dst_name == G.settings["nickname"])
            {
                E.Notice(nick, "You fell for it, fool! Thunder Cross Split Attack!");
                dst_name = nick;
            }

            // Take a random amount from "colors"
            string[] choices    = new string[Utils.random.Next(2, 5)];
            string   choice_str = "";

            for (int i = 0; i < choices.Length; ++i)
            {
                choices[i] = (string)Utils.RandomIn(colors);
                // Format chat output
                choice_str += choices[i];
                if (i < choices.Length - 1)
                {
                    choice_str += ", ";
                }
            }
            string color = (string)Utils.RandomIn(choices);

            var data = new DisarmData(dst_name, color, Utils.random.Next(50, 90) * 1000.0);

            data.timer.Elapsed += delegate {
                BoomTimerElapsed(channel);
            };

            m_timers[channel] = data;
            chan.Say(dst_name + ": Tick tick.. " + (int)(data.timer.Interval / 1000.0) +
                     "s until explosion. Try $cutwire <color> from one of these colors: " + choice_str);
        }
Beispiel #4
0
        string LuaRun(string nick, Channel chan, string message)
        {
            if (!System.IO.File.Exists("plugins/security.lua"))
            {
                return("Error: File 'plugins/security.lua' does not exist");
            }

            bool is_private = chan.IsPrivate();

            // Initialize packet lock, packet and start time
            lua_lock   = false;
            lua_packet = new System.Text.StringBuilder();

            SE.ResetLua();
            SE.RegisterLuaFunction(l_print, "print");
            SE.RegisterLuaFunction(l_stringldistance, "stringldistance");
            SE.RegisterLuaFunction(l_getUserstatus, "getUserstatus");
            Lua.lua_atpanic(SE.L, l_panic);

            #region Nick list
            Lua.lua_newtable(SE.L);

            int index = 0;
            if (!is_private)
            {
                foreach (var user in chan.users)
                {
                    Lua.lua_pushinteger(SE.L, ++index);
                    Lua.lua_newtable(SE.L);

                    Lua.lua_pushstring(SE.L, "nick");
                    Lua.lua_pushstring(SE.L, user.Key);
                    Lua.lua_settable(SE.L, -3);

                    Lua.lua_pushstring(SE.L, "hostmask");
                    Lua.lua_pushstring(SE.L, user.Value.hostmask);
                    Lua.lua_settable(SE.L, -3);

                    Lua.lua_settable(SE.L, -3);
                }
            }
            Lua.lua_setglobal(SE.L, "N");
            #endregion

            #region Public chat variables variables
            SE.CreateLuaTable("L");
            Lua.lua_getglobal(SE.L, "L");
            SE.SetTableField("channel", chan.GetName());
            SE.SetTableField("nick", nick);
            SE.SetTableField("botname", G.settings["nickname"]);
            SE.SetTableField("hostmask", chan.GetUserData(nick).hostmask);
            SE.SetTableField("isprivate", is_private);
            SE.SetTableField("online", index);
            SE.SetTableField("owner_hostmask", G.settings["owner_hostmask"]);
            Lua.lua_settop(SE.L, 0);
            #endregion

            int    lua_error  = 1;
            string lua_output = null;

            lua_error = Lua.luaL_dofile(SE.L, "plugins/security.lua");
            if (lua_error == 0)
            {
                lua_error = Lua.luaL_dostring(SE.L, message);
            }

            while (lua_lock)
            {
                System.Threading.Thread.Sleep(100);
            }

            int type = Lua.lua_type(SE.L, -1);

            if (type == Lua.LUA_TSTRING)
            {
                int length = Lua.lua_strlen(SE.L, -1);
                if (length > LUA_TEXT_MAX)
                {
                    lua_output = "<too long message>";
                    Lua.lua_pop(SE.L, 1);
                    type = Lua.LUA_TNONE;
                }
            }
            if (type != Lua.LUA_TNIL && type != Lua.LUA_TNONE)
            {
                lua_output = Lua.lua_tostring(SE.L, -1);
            }

            lua_lock = true;
            if (lua_output != null)
            {
                lua_packet.Append(lua_output);
            }

            if (lua_error > 0)
            {
                L.Log("m_Lua::LuaRun, errorcode = " + lua_error, true);
            }

            lua_lock = false;

            SE.CloseLua();
            lua_timer.Reset();

            #region Remove control characters, '\n' to space
            char[] answer = new char[LUA_TEXT_MAX];
            int    pos    = 0;
            for (int i = 0; i < lua_packet.Length && pos < answer.Length; i++)
            {
                char cur = lua_packet[i];
                if (cur == '\t' || cur == '\n')
                {
                    cur = ' ';
                }

                if (cur == 0 || cur == '\r')
                {
                    continue;
                }

                answer[pos] = cur;
                pos++;
            }
            #endregion
            if (pos == answer.Length)
            {
                answer[--pos] = '.';
                answer[--pos] = '.';
                answer[--pos] = ' ';
                pos          += 3;
            }

            string answer_s = new string(answer, 0, pos);
            if (pos == 0)
            {
                answer_s = nick + ": <no return text>";
            }
            return(answer_s);
        }
Beispiel #5
0
        void FetchChat(string line)
        {
            //<host> 353 <NICKNAME> = #nimg_lobby :<nick> @<nick> +<nick>
            string[] args = line.Split(new char[] { ' ' }, 6);

            #region Nickname and Hostmask
            int read_pos = args[0].IndexOf('!');

            string nick = "";
            if (read_pos > 0)
            {
                nick = args[0].Substring(0, read_pos++);
            }
            else
            {
                read_pos = 0;
            }

            string hostmask = args[0].Substring(read_pos);
            #endregion

            string status = args[1].ToUpper();

            #region Ping Pong
            if (hostmask == "PING")
            {
                status = status.ToLower();
                if (status[0] == ':')
                {
                    status = status.Substring(1);
                }

                L.Log("PONG to " + status);
                send("PONG " + status);

                if (OnPong != null)
                {
                    OnPong();
                }
                return;
            }
            if (hostmask == "PONG")
            {
                return;
            }
            #endregion

            if (status == "QUIT")
            {
                OnUserEvent(nick, hostmask, status, "");
                return;
            }

            // <nick!host> <status> <destination> ...
            string destination = args[2];

            #region JOIN NICK PART
            if (status == "JOIN" || status == "NICK" || status == "PART")
            {
                // <nick!host> PART <channel>
                // <nick!host> JOIN :<channel>

                if (destination[0] == ':')
                {
                    destination = destination.Substring(1);
                }

                OnUserEvent(nick, hostmask, status, destination);
                return;
            }
            #endregion

            #region Normal Text-based
            // Check if it's possible to get the text right now
            bool is_text_only = false;
            for (int i = 0; i < TEXT_STATUS.Length; i++)
            {
                if (TEXT_STATUS[i] == status)
                {
                    is_text_only = true;
                    break;
                }
            }

            if (is_text_only)
            {
                // <nick!host> <status> <destination> :text text
                int min_start = args[0].Length + args[1].Length + args[2].Length + 3;

                read_pos = line.IndexOf(':', min_start) + 1;
                if (read_pos < 0)
                {
                    Console.WriteLine(line);
                    return;
                }

                string message = line.Substring(read_pos);

                if (status == "PONG")
                {
                    L.Log(">> PONG " + hostmask);
                    return;
                }

                if ((status == "PRIVMSG" || status == "NOTICE") &&
                    destination.ToUpper() != "AUTH" &&
                    destination != "*" &&
                    nick != "")
                {
                    if (Channel.IsPrivate(destination))
                    {
                        manager.SetActiveChannel(nick);                         // The tables have turned
                    }
                    else
                    {
                        manager.SetActiveChannel(destination);
                    }

                    Channel channel = manager.GetChannel();
                    // Update hostmask or add user
                    {
                        UserData user = channel.GetUserData(nick);
                        if (user == null)
                        {
                            user = new UserData(hostmask);
                        }
                        else
                        {
                            user.hostmask = hostmask;
                        }
                        channel.users[nick] = user;
                    }

                    OnUserSay(nick, message);
                }
                else                     // numbers, AUTH request
                {
                    OnServerMessage(status, destination, message);
                }
                return;
            }
            #endregion

            #region User Events
            // Userlist
            if (status == "332" ||
                status == "353" ||
                status == "366" ||
                status == "396")
            {
                // <nick!host> 353 <destination> = <channel> :Bottybot figther212 @boots +Trivia foobar
                // <nick!host> <status> <destination> <channel> :Text text

                int min_start  = 0;
                int text_start = (status == "353") ? 5 : 4;

                for (int i = 0; i < text_start; i++)
                {
                    min_start += args[i].Length + 1;
                }

                read_pos = line.IndexOf(':', min_start) + 1;
                if (read_pos < 0)
                {
                    L.Log("FetchChat() " + line, true);
                    return;
                }

                string message = line.Substring(read_pos);
                OnServerMessage(status, args[text_start - 1], message);
                return;
            }

            if (status == "KICK")
            {
                // <nick!host> KICK <channel> <nick> :Kick reason

                OnUserEvent(args[3], "", status, destination);
                return;
            }
            #endregion

            if (status == "004" ||
                status == "005" ||
                status == "252" ||
                status == "254" ||
                status == "265" ||
                status == "266" ||
                status == "333")
            {
                // Ignore stuff that's not supported yet
                return;
            }

            Console.WriteLine(line);
        }