Beispiel #1
0
        public void AddVariable(Variable variable)
        {
            if (!variable.name.StartsWith("%")) return;

            Variable v = _variables.Find(
                delegate(Variable var)
                {
                    return var.name == variable.name;
                }
            );
            if (v == null)
                _variables.Add(variable);
            else
                v.value = variable.value;
        }
Beispiel #2
0
        /// <summary>
        /// Parse out $identifiers for outgoing commands
        /// </summary>
        /// <param name="connection">Which Connection it is for</param>
        /// <param name="data">The data to be parsed</param>
        /// <returns></returns>
        private string ParseIdentifiers(IRCConnection connection, string data, string dataPassed)
        {
            string[] changedData = null;

            try
            {
                //parse the initial identifiers
                data = ParseIdentifier(connection, data);

                //parse out the $1,$2.. identifiers
                data = ParseIdentifierValue(data, dataPassed);

                //$+ is a joiner identifier, great for joining 2 words together
                data = data.Replace(" $+ ", string.Empty);

                //parse out the current channel #
                if (CurrentWindowStyle == IceTabPage.WindowType.Channel)
                {
                    data = data.Replace(" # ", " " + CurrentWindow.TabCaption + " ");
                }

                //split up the data into words
                string[] parsedData = data.Split(' ');

                //the data that was passed for parsing identifiers
                string[] passedData = dataPassed.Split(' ');

                //will hold the updates message/data after identifiers are parsed
                changedData = data.Split(' ');

                int count = -1;
                string extra = "";
                bool askExtra = false;
                bool askSecure = false;

                foreach (string word in parsedData)
                {
                    count++;

                    if (word.StartsWith("//") && count == 0)
                        changedData[count] = word.Substring(1);

                    if (askExtra)
                    {
                        //continueing a $?=
                        extra += " " + word;
                        changedData[count] = null;
                        if (extra[extra.Length - 1] == extra[0])
                        {
                            askExtra = false;
                            //ask the question
                            InputBoxDialog i = new InputBoxDialog();
                            i.PasswordChar = askSecure;
                            i.FormCaption = "Enter Value";
                            i.FormPrompt = extra.Substring(1,extra.Length-2);

                            i.ShowDialog();
                            if (i.InputResponse.Length > 0)
                                changedData[count] = i.InputResponse;
                            i.Dispose();
                        }
                    }

                    //parse out identifiers (start with a $)
                    if (word.StartsWith("$"))
                    {
                        switch (word)
                        {

                            default:
                                int result;
                                if (word.StartsWith("$?=") && word.Length > 5)
                                {
                                    //check for 2 quotes (single or double)
                                    string ask = word.Substring(3);
                                    //check what kind of a quote it is
                                    char quote = ask[0];
                                    if (quote == ask[ask.Length - 1])
                                    {
                                        //ask the question
                                        extra = ask;
                                        InputBoxDialog i = new InputBoxDialog();
                                        i.FormCaption = "Enter Value";
                                        i.FormPrompt = extra.Substring(1, extra.Length - 2);

                                        i.ShowDialog();
                                        if (i.InputResponse.Length > 0)
                                            changedData[count] = i.InputResponse;
                                        else
                                            changedData[count] = null;
                                        i.Dispose();
                                    }
                                    else
                                    {
                                        //go to the next word until we find a quote at the end
                                        extra = ask;
                                        askExtra = true;
                                        changedData[count] = null;
                                    }
                                }

                                //check for $?*="" // password char
                                if (word.StartsWith("$?*=") && word.Length > 6)
                                {
                                    //check for 2 quotes (single or double)
                                    string ask = word.Substring(4);
                                    //check what kind of a quote it is
                                    char quote = ask[0];
                                    if (quote == ask[ask.Length - 1])
                                    {
                                        //ask the question
                                        extra = ask;
                                        InputBoxDialog i = new InputBoxDialog();
                                        i.PasswordChar = true;
                                        i.FormCaption = "Enter Value";
                                        i.FormPrompt = extra.Substring(1, extra.Length - 2);

                                        i.ShowDialog();
                                        if (i.InputResponse.Length > 0)
                                            changedData[count] = i.InputResponse;
                                        else
                                            changedData[count] = null;
                                        i.Dispose();
                                    }
                                    else
                                    {
                                        //go to the next word until we find a quote at the end
                                        extra = ask;
                                        askExtra = true;
                                        askSecure = true;
                                        changedData[count] = null;
                                    }
                                }

                                if (word.StartsWith("$md5(") && word.IndexOf(')') > word.IndexOf('('))
                                {
                                    string input = ReturnBracketValue(word);
                                    changedData[count] = MD5(input);
                                }

                                if (word.StartsWith("$net(") && word.IndexOf(')') > word.IndexOf('('))
                                {
                                    string input = ReturnBracketValue(word);
                                    try
                                    {
                                        // //echo $net(System.Environment.CurrentDirectory)
                                        string[] thevalue = input.Split('.');
                                        if (thevalue.Length > 1)
                                        {
                                            string Class = String.Join(".", thevalue, 0, thevalue.Length - 1);
                                            string value = input.Substring(Class.Length + 1);
                                            //System.Diagnostics.Debug.WriteLine(Class + "::" + value);
                                            if (Class.Length > 0)
                                            {
                                                //Type.GetType(string for the type).GetProperty(n ame of the property).GetValue(null)
                                                Type t = Type.GetType(Class);

                                                PropertyInfo info = t.GetProperty(value);
                                                if (info != null)
                                                {
                                                    //System.Diagnostics.Debug.WriteLine("name=" +info.Name + ":" + info.GetValue(t, null));
                                                    changedData[count] = info.GetValue(t, null).ToString();
                                                }
                                                else
                                                {
                                                    //System.Diagnostics.Debug.WriteLine("null info");
                                                    changedData[count] = "$null";
                                                }

                                                //System.Diagnostics.Debug.WriteLine(t.ToString());
                                            }
                                            else
                                                changedData[count] = "$null";
                                        }
                                    }
                                    catch (Exception)
                                    {
                                        changedData[count] = "$null";
                                    }
                                }

                                if (word.StartsWith("$rand(") && word.IndexOf(')') > word.IndexOf('('))
                                {
                                    string input = ReturnBracketValue(word);
                                    //look for a comma (,)
                                    if (input.Split(',').Length == 2)
                                    {
                                        string lownum = input.Split(',')[0];
                                        string hinum = input.Split(',')[1];

                                        int lowNum, hiNum;
                                        if (Int32.TryParse(lownum, out lowNum) && Int32.TryParse(hinum, out hiNum))
                                        {
                                            //valid numbers
                                            Random r = new Random();
                                            int randNumber = r.Next(lowNum, hiNum);

                                            changedData[count] = randNumber.ToString();
                                        }
                                        else
                                            changedData[count] = "$null";
                                        Variable v = new Variable();

                                    }
                                    else if (input.IndexOf(',') == -1)
                                    {
                                        //make it a value from 1 - value
                                        int hiNum;
                                        if (Int32.TryParse(input, out hiNum))
                                        {
                                            //valid number
                                            Random r = new Random();
                                            int randNumber = r.Next(1, hiNum);

                                            changedData[count]= randNumber.ToString();
                                        }
                                        else
                                            changedData[count] = "$null";

                                    }
                                    else
                                        changedData[count] = "$null";
                                }

                                if (word.StartsWith("$read(") && word.IndexOf(')') > word.IndexOf('('))
                                {
                                    string file = ReturnBracketValue(word);
                                    //check if we have passed a path or just a filename
                                    if (file.IndexOf(System.IO.Path.DirectorySeparatorChar) > -1)
                                    {
                                        //its a full folder
                                        if (File.Exists(file))
                                        {
                                            //count the number of lines in the file
                                            //load the file in and read a random line from it
                                            string[] lines = File.ReadAllLines(file);
                                            if (lines.Length > 0)
                                            {
                                                //pick a random line
                                                Random r = new Random();
                                                int line = r.Next(0, lines.Length - 1);
                                                changedData[count] = lines[line];
                                            }
                                            else
                                                changedData[count] = "$null";

                                        }
                                        else
                                        {
                                            changedData[count] = "$null";
                                        }
                                    }
                                    else
                                    {
                                        //just check in the Scripts Folder
                                        if (File.Exists(scriptsFolder + System.IO.Path.DirectorySeparatorChar + file))
                                        {
                                            //load the file in and read a random line from it
                                            string[] lines = File.ReadAllLines(scriptsFolder + System.IO.Path.DirectorySeparatorChar + file);
                                            if (lines.Length > 0)
                                            {
                                                //pick a random line
                                                Random r = new Random();
                                                int line = r.Next(0, lines.Length - 1);
                                                changedData[count] = lines[line];
                                            }
                                            else
                                                changedData[count] = "$null";
                                        }
                                        else
                                        {
                                            changedData[count] = "$null";
                                        }
                                    }
                                }

                                if (word.StartsWith("$var(") && word.IndexOf(')') > word.IndexOf('('))
                                {
                                    //get the value between and after the brackets
                                    string variable = ReturnBracketValue(word);
                                    string prop = ReturnPropertyValue(word);

                                    System.Diagnostics.Debug.WriteLine(variable);
                                    //check if we have a connection or not
                                    if (connection == null)
                                    {
                                        changedData[count] = _variables.ReturnValue(variable).ToString();

                                    }
                                }

                                if (word.StartsWith("$plugin(") && word.IndexOf(')') > word.IndexOf('('))
                                {
                                    //get the plugin information
                                    string pluginid = ReturnBracketValue(word);
                                    string prop = ReturnPropertyValue(word);

                                    //tryparse
                                    if (Int32.TryParse(pluginid, out result))
                                    {
                                        for (int i = 0; i < loadedPlugins.Count; i++)
                                        {
                                            if (i == result)
                                            {
                                                IPluginIceChat ipc = ((IceChatPlugin)loadedPlugins[i]).plugin;

                                                switch (prop.ToLower())
                                                {
                                                    case "id":
                                                        changedData[count] = i.ToString();
                                                        break;
                                                    case "name":
                                                        changedData[count] = ipc.Name;
                                                        break;
                                                    case "version":
                                                        changedData[count] = ipc.Version;
                                                        break;
                                                    case "author":
                                                        changedData[count] = ipc.Author;
                                                        break;
                                                    case "enabled":
                                                        changedData[count] = ipc.Enabled.ToString();
                                                        break;
                                                    case "filename":
                                                        changedData[count] = ipc.FileName;
                                                        break;
                                                    default:
                                                        changedData[count] = ipc.Name;
                                                        break;
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        //go by plugin filename, not number
                                        for (int i = 0; i < loadedPlugins.Count; i++)
                                        {
                                            if (((IceChatPlugin)loadedPlugins[i]).plugin.FileName.ToLower() == pluginid.ToLower())
                                            {
                                                IPluginIceChat ipc = ((IceChatPlugin)loadedPlugins[i]).plugin;

                                                switch (prop.ToLower())
                                                {
                                                    case "id":
                                                        changedData[count] = i.ToString();
                                                        break;
                                                    case "name":
                                                        changedData[count] = ipc.Name;
                                                        break;
                                                    case "version":
                                                        changedData[count] = ipc.Version;
                                                        break;
                                                    case "author":
                                                        changedData[count] = ipc.Author;
                                                        break;
                                                    case "enabled":
                                                        changedData[count] = ipc.Enabled.ToString();
                                                        break;
                                                    case "filename":
                                                        changedData[count] = ipc.FileName;
                                                        break;
                                                    default:
                                                        changedData[count] = ipc.Name;
                                                        break;
                                                }
                                            }
                                        }
                                    }
                                }

                                if (connection != null)
                                {
                                    if (word.StartsWith("$ial(") && word.IndexOf(')') > word.IndexOf('('))
                                    {
                                        string nick = ReturnBracketValue(word);
                                        string prop = ReturnPropertyValue(word);

                                        InternalAddressList ial = (InternalAddressList)connection.ServerSetting.IAL[nick];
                                        if (ial != null)
                                        {
                                            if (prop.Length == 0)
                                                changedData[count] = ial.Nick;
                                            else
                                            {
                                                switch (prop.ToLower())
                                                {
                                                    case "nick":
                                                        changedData[count] = ial.Nick;
                                                        break;
                                                    case "host":
                                                        changedData[count] = ial.Host;
                                                        break;
                                                }
                                            }
                                        }
                                        else
                                            changedData[count] = "$null";
                                    }

                                    if (word.StartsWith("$nick(") && word.IndexOf(')') > word.IndexOf('('))
                                    {
                                        //get the value between and after the brackets
                                        string values = ReturnBracketValue(word);
                                        if (values.Split(',').Length == 2)
                                        {
                                            string channel = values.Split(',')[0];
                                            string nickvalue = values.Split(',')[1];

                                            string prop = ReturnPropertyValue(word);

                                            // $nick(#,N)
                                            //find then channel
                                            IceTabPage t = GetWindow(connection, channel, IceTabPage.WindowType.Channel);
                                            if (t != null)
                                            {
                                                User u = null;
                                                if (Int32.TryParse(nickvalue, out result))
                                                {
                                                    if (Convert.ToInt32(nickvalue) == 0)
                                                        changedData[count] = t.Nicks.Count.ToString();
                                                    else
                                                        u = t.GetNick(Convert.ToInt32(nickvalue));
                                                }
                                                else
                                                {
                                                    u = t.GetNick(nickvalue);
                                                }

                                                if (prop.Length == 0 && u != null)
                                                {
                                                    changedData[count] = u.NickName;
                                                }
                                                else if (u != null)
                                                {
                                                    //$nick(#channel,1).op , .voice, .halfop, .admin,.owner.
                                                    //.mode, .host, .nick,.ident
                                                    InternalAddressList ial = (InternalAddressList)connection.ServerSetting.IAL[u.NickName];
                                                    switch (prop.ToLower())
                                                    {
                                                        case "host":
                                                            if (ial != null && ial.Host != null && ial.Host.Length > 0)
                                                                changedData[count] = ial.Host.Substring(ial.Host.IndexOf('@') + 1);
                                                            break;
                                                        case "ident":
                                                            if (ial != null && ial.Host != null && ial.Host.Length > 0)
                                                                changedData[count] = ial.Host.Substring(0,ial.Host.IndexOf('@'));
                                                            break;
                                                        case "nick":
                                                            changedData[count] = u.NickName;
                                                            break;
                                                        case "mode":
                                                            changedData[count] = u.ToString().Replace(u.NickName, "");
                                                            break;
                                                        case "op":
                                                            for (int i = 0; i < u.Level.Length; i++)
                                                            {
                                                                if (connection.ServerSetting.StatusModes[0][i] == 'o')
                                                                {
                                                                    if (u.Level[i] == true)
                                                                        changedData[count] = "$true";
                                                                    else
                                                                        changedData[count] = "$false";
                                                                }
                                                            }
                                                            break;
                                                        case "halfop":
                                                            for (int i = 0; i < u.Level.Length; i++)
                                                            {
                                                                if (connection.ServerSetting.StatusModes[0][i] == 'h')
                                                                {
                                                                    if (u.Level[i] == true)
                                                                        changedData[count] = "$true";
                                                                    else
                                                                        changedData[count] = "$false";
                                                                }
                                                            }
                                                            break;
                                                        case "voice":
                                                            for (int i = 0; i < u.Level.Length; i++)
                                                            {
                                                                if (connection.ServerSetting.StatusModes[0][i] == 'v')
                                                                {
                                                                    if (u.Level[i] == true)
                                                                        changedData[count] = "$true";
                                                                    else
                                                                        changedData[count] = "$false";
                                                                }
                                                            }
                                                            break;
                                                    }
                                                    ial = null;
                                                }
                                            }
                                        }
                                    }

                                    if (word.StartsWith("$chan(") && word.IndexOf(')') > word.IndexOf('('))
                                    {
                                        //get the value between and after the brackets
                                        string channel = ReturnBracketValue(word);
                                        string prop = ReturnPropertyValue(word);

                                        //find then channel
                                        IceTabPage t = GetWindow(connection, channel, IceTabPage.WindowType.Channel);
                                        if (t != null)
                                        {
                                            if (prop.Length == 0)
                                            {
                                                //replace with channel name
                                                changedData[count] = t.TabCaption;
                                            }
                                            else
                                            {
                                                switch (prop.ToLower())
                                                {
                                                    case "mode":
                                                        changedData[count] = t.ChannelModes;
                                                        break;
                                                    case "count":
                                                        changedData[count] = t.Nicks.Count.ToString();
                                                        break;
                                                    case "nicks":
                                                        //return all the nicks seperated by a space
                                                        string nicks = "";
                                                        foreach (string n in t.Nicks.Keys)
                                                            nicks += n + " ";
                                                        changedData[count] = nicks.Trim();
                                                        break;
                                                    case "log":
                                                        changedData[count] = t.TextWindow.LogFileName;
                                                        break;
                                                    default:
                                                        break;
                                                }
                                            }
                                        }
                                    }
                                    if (word.StartsWith("$timer(") && word.IndexOf(')') > word.IndexOf('('))
                                    {
                                        //get the value between and after the brackets
                                        string timerid = ReturnBracketValue(word);
                                        string prop = ReturnPropertyValue(word);

                                        //find the timer
                                        foreach (IrcTimer timer in connection.IRCTimers)
                                        {
                                            if (timer.TimerID == timerid)
                                            {
                                                if (prop.Length == 0)
                                                {
                                                    //replace with timer id
                                                    changedData[count] = timer.TimerID;
                                                }
                                                else
                                                {
                                                    switch (prop.ToLower())
                                                    {
                                                        case "id":
                                                            changedData[count] = timer.TimerID;
                                                            break;
                                                        case "reps":
                                                            changedData[count] = timer.TimerRepetitions.ToString();
                                                            break;
                                                        case "count":
                                                            changedData[count] = timer.TimerCounter.ToString();
                                                            break;
                                                        case "command":
                                                            changedData[count] = timer.TimerCommand;
                                                            break;
                                                        case "interval":
                                                            changedData[count] = timer.TimerInterval.ToString();
                                                            break;
                                                        default:
                                                            break;
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    if (word.StartsWith("$mask(") && word.IndexOf(')') > word.IndexOf('('))
                                    {
                                        //$mask($host,2)
                                        //get the value between and after the brackets
                                        string values = ReturnBracketValue(word);
                                        string prop = ReturnPropertyValue(word);

                                        if (values.Split(',').Length == 2)
                                        {
                                            string full_host = values.Split(',')[0];
                                            string mask_value = values.Split(',')[1];

                                            if (full_host.Length == 0) break;
                                            if (mask_value.Length == 0) break;

                                            if (full_host.IndexOf("@") == -1) break;
                                            if (full_host.IndexOf("!") == -1) break;

                                            switch (mask_value)
                                            {
                                                case "0":   // *!user@host
                                                    changedData[count] = "*!" + full_host.Substring(full_host.IndexOf("!") + 1);
                                                    break;

                                                case "1":   // *!*user@host
                                                    changedData[count] = "*!*" + full_host.Substring(full_host.IndexOf("!") + 1);
                                                    break;

                                                case "2":   // *!*user@*.host
                                                    changedData[count] = "*!*" + full_host.Substring(full_host.IndexOf("@"));
                                                    break;

                                                case "3":   // *!*user@*.host
                                                    break;

                                                case "4":   // *!*@*.host
                                                    break;

                                                case "5":   // nick!user@host
                                                    changedData[count] = full_host;
                                                    break;

                                                case "6":   // nick!*user@host
                                                    break;

                                                case "7":   // nick!*@host
                                                    break;

                                                case "8":   // nick!*user@*.host
                                                    break;

                                                case "9":   // nick!*@*.host
                                                    break;

                                                case "10":  // nick!*@*
                                                    changedData[count] = full_host.Substring(0, full_host.IndexOf("!")) + "!*@*";
                                                    break;

                                                case "11":  // *!user@*
                                                    break;
                                            }

                                        }

                                    }

                                }
                                break;
                        }

                    }

                }
            }
            catch (Exception e)
            {
                WriteErrorFile(connection, "ParseIdentifiers" + data, e);
            }
            //return String.Join(" ", changedData);
            return JoinString(changedData);
        }
Beispiel #3
0
        public void AddVariable(string name, object value)
        {
            if (!name.StartsWith("%")) return;

            if (name.Length > 0)
            {
                //check if we have this variable already
                Variable v = _variables.Find(
                    delegate(Variable var)
                    {
                        return var.name == name;
                    }
                );
                if (v != null)
                {
                    //set the new value?
                    v.value = value;
                }
                else
                {
                    Variable item = new Variable();
                    item.name = name;
                    item.value = value;

                    //is this really necessary ?
                    int result;
                    if (Int32.TryParse(value.ToString(), out result))
                        item.type = typeof(int);
                    else
                        item.type = typeof(string);

                    _variables.Add(item);
                }
            }
        }