Example #1
0
        /// <summary>
        /// New user from string.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="network">The Network.</param>
        /// <returns></returns>
        public static IrcUser NewFromString(string source, IIrc network)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (network == null)
            {
                throw new ArgumentNullException("network");
            }

            string user, host;
            string nick = user = host = null;
            try
            {
                if ((source.Contains("@")) && (source.Contains("!")))
                {
                    char[] splitSeparators = {'!', '@'};
                    string[] sourceSegment = source.Split(splitSeparators, 3);
                    nick = sourceSegment[0];
                    user = sourceSegment[1];
                    host = sourceSegment[2];
                }
                else if (source.Contains("@"))
                {
                    char[] splitSeparators = {'@'};
                    string[] sourceSegment = source.Split(splitSeparators, 2);
                    nick = sourceSegment[0];
                    host = sourceSegment[1];
                }
                else
                {
                    nick = source;
                }
            }
            catch (IndexOutOfRangeException)
            {
                //TODO: do soemthing;
            }

            var ret = new IrcUser
                           {
                               HostName = host,
                               Nickname = nick,
                               UserName = user,
                               Network = network,
                           };
            return ret;
        }
Example #2
0
        private bool Parse()
        {
            if (String.IsNullOrEmpty(_data))
            {
                _prefix = null;
                _command = null;
                _args = null;
                return false;
            }

            var raw = _data;

            if (raw[0] == ':') // prefix is present if the first char is a :
            {
                string[] split1 = raw.Split(new[] {' '}, 2);
                    //TODO: check there'rawData a space, and this returns two items;
                _prefix = IrcUser.NewFromString(split1[0], _network);
                raw = split1[1];

            }
            else
            {
                _prefix = null;
            }

            // next is a command, with an OPTIONAL(!) list of arguments following, up to 15. We're gonna assume infinite for the purposes of this code

            if (!raw.Contains(" "))
            {
                _command = raw;
                _args = new string[0];
                return true;
            } // ok, there'rawData arguments

            var split2 = raw.Split(new[] {' '}, 2);
            _command = split2[0];
            raw = split2[1];

            // handle the arguments
            {
                var xargs = new List<string>();

                if (raw.Contains(" :"))
                {
                    // split on first instance of " :"
                    var split3 = raw.Split(new[] {" :"}, 2, StringSplitOptions.None);
                    var split4 = split3[0].Split(' ');

                    xargs.AddRange(split4);
                    xargs.Add(split3[1]);
                }
                else
                {
                    var split5 = raw.Split(' ');
                    xargs.AddRange(split5);
                }

                _args = xargs.ToArray();

            }

            return true;
        }