Пример #1
0
        public static NmdcUserCommandCommand Parse(string message)
        {
            if (string.IsNullOrWhiteSpace(message))
            {
                return(null);
            }

            var match = ParseRegex.Match(message);

            if (match == null || !match.Success)
            {
                return(null);
            }

            var groups = match.Groups;

            var commandType = (NmdcUserCommandType)byte.Parse(groups[TypeGroupName].Value);
            var context     = (NmdcUserCommandContextType)byte.Parse(groups[ContextGroupName].Value);

            var titleGroup = groups[TitleGroupName];
            var title      = titleGroup.Success
                ? titleGroup.Value
                : string.Empty;

            UserCommandsGenerator commandsGenerator = null;
            var lines         = Enumerable.Empty <string>();
            var commandsGroup = groups[CommandsGroupName];

            if (commandsGroup.Success)
            {
                var gluedMessages = commandsGroup.Value;

                var lineMatch = LineVariableParseRegex.Match(gluedMessages);
                if (lineMatch.Success)
                {
                    lines = lineMatch.Captures.Cast <Capture>().Select(capture => capture.Value);
                }

                var commandsMessages = UnescapeArgument(gluedMessages).Split(new[] { StopChar }, StringSplitOptions.RemoveEmptyEntries);
                commandsGenerator = (nickReplacement, myNickReplacement, lineReplacements) =>
                {
                    var replacements = (lineReplacements ?? new Dictionary <string, string>())
                                       .ToDictionary(line => GetLineVariable(line.Key), line => line.Value);
                    replacements.Add(NickVariable, nickReplacement);
                    replacements.Add(MyNickVariable, myNickReplacement);

                    var regex = new Regex($"({string.Join("|", replacements.Keys.Select(Regex.Escape))})");

                    return((commandsMessages ?? Enumerable.Empty <string>())
                           .Select(m => NmdcCommandParser.Parse(regex.Replace(m, mm => replacements[mm.Value]))));
                };
            }

            return(new NmdcUserCommandCommand(commandType, context, title, lines, commandsGenerator));
        }
Пример #2
0
 public NmdcUserCommandCommand(
     NmdcUserCommandType commandType,
     NmdcUserCommandContextType context,
     string title,
     IEnumerable <string> lines,
     UserCommandsGenerator commandsGenerator)
 {
     CommandType       = commandType;
     Context           = context;
     Title             = title;
     Lines             = lines;
     CommandsGenerator = commandsGenerator;
 }
Пример #3
0
 public NmdcUserCommandCommand(
     NmdcUserCommandType commandType,
     NmdcUserCommandContextType context,
     string title,
     IEnumerable<string> lines,
     UserCommandsGenerator commandsGenerator)
 {
     CommandType = commandType;
     Context = context;
     Title = title;
     Lines = lines;
     CommandsGenerator = commandsGenerator;
 }