예제 #1
0
파일: IRC.cs 프로젝트: TheDireMaster/fCraft
        static IRCMessageType GetMessageType([NotNull] string rawline, [NotNull] string actualBotNick)
        {
            if (rawline == null)
            {
                throw new ArgumentNullException("rawline");
            }
            if (actualBotNick == null)
            {
                throw new ArgumentNullException("actualBotNick");
            }

            Match found = ReplyCodeRegex.Match(rawline);

            if (found.Success)
            {
                string       code      = found.Groups[1].Value;
                IRCReplyCode replycode = (IRCReplyCode)int.Parse(code);

                // check if this replycode is known in the RFC
                if (Array.IndexOf(ReplyCodes, replycode) == -1)
                {
                    return(IRCMessageType.Unknown);
                }

                switch (replycode)
                {
                case IRCReplyCode.Welcome:
                case IRCReplyCode.YourHost:
                case IRCReplyCode.Created:
                case IRCReplyCode.MyInfo:
                case IRCReplyCode.Bounce:
                    return(IRCMessageType.Login);

                case IRCReplyCode.LuserClient:
                case IRCReplyCode.LuserOp:
                case IRCReplyCode.LuserUnknown:
                case IRCReplyCode.LuserMe:
                case IRCReplyCode.LuserChannels:
                    return(IRCMessageType.Info);

                case IRCReplyCode.MotdStart:
                case IRCReplyCode.Motd:
                case IRCReplyCode.EndOfMotd:
                    return(IRCMessageType.Motd);

                case IRCReplyCode.NamesReply:
                case IRCReplyCode.EndOfNames:
                    return(IRCMessageType.Name);

                case IRCReplyCode.WhoReply:
                case IRCReplyCode.EndOfWho:
                    return(IRCMessageType.Who);

                case IRCReplyCode.ListStart:
                case IRCReplyCode.List:
                case IRCReplyCode.ListEnd:
                    return(IRCMessageType.List);

                case IRCReplyCode.BanList:
                case IRCReplyCode.EndOfBanList:
                    return(IRCMessageType.BanList);

                case IRCReplyCode.Topic:
                case IRCReplyCode.TopicSetBy:
                case IRCReplyCode.NoTopic:
                    return(IRCMessageType.Topic);

                case IRCReplyCode.WhoIsUser:
                case IRCReplyCode.WhoIsServer:
                case IRCReplyCode.WhoIsOperator:
                case IRCReplyCode.WhoIsIdle:
                case IRCReplyCode.WhoIsChannels:
                case IRCReplyCode.EndOfWhoIs:
                    return(IRCMessageType.WhoIs);

                case IRCReplyCode.WhoWasUser:
                case IRCReplyCode.EndOfWhoWas:
                    return(IRCMessageType.WhoWas);

                case IRCReplyCode.UserModeIs:
                    return(IRCMessageType.UserMode);

                case IRCReplyCode.ChannelModeIs:
                    return(IRCMessageType.ChannelMode);

                default:
                    if (((int)replycode >= 400) &&
                        ((int)replycode <= 599))
                    {
                        return(IRCMessageType.ErrorMessage);
                    }
                    else
                    {
                        return(IRCMessageType.Unknown);
                    }
                }
            }

            found = PingRegex.Match(rawline);
            if (found.Success)
            {
                return(IRCMessageType.Ping);
            }

            found = ErrorRegex.Match(rawline);
            if (found.Success)
            {
                return(IRCMessageType.Error);
            }

            found = ActionRegex.Match(rawline);
            if (found.Success)
            {
                switch (found.Groups[1].Value)
                {
                case "#":
                case "!":
                case "&":
                case "+":
                    return(IRCMessageType.ChannelAction);

                default:
                    return(IRCMessageType.QueryAction);
                }
            }

            found = CtcpRequestRegex.Match(rawline);
            if (found.Success)
            {
                return(IRCMessageType.CtcpRequest);
            }

            found = MessageRegex.Match(rawline);
            if (found.Success)
            {
                switch (found.Groups[1].Value)
                {
                case "#":
                case "!":
                case "&":
                case "+":
                    return(IRCMessageType.ChannelMessage);

                default:
                    return(IRCMessageType.QueryMessage);
                }
            }

            found = CtcpReplyRegex.Match(rawline);
            if (found.Success)
            {
                return(IRCMessageType.CtcpReply);
            }

            found = NoticeRegex.Match(rawline);
            if (found.Success)
            {
                switch (found.Groups[1].Value)
                {
                case "#":
                case "!":
                case "&":
                case "+":
                    return(IRCMessageType.ChannelNotice);

                default:
                    return(IRCMessageType.QueryNotice);
                }
            }

            found = InviteRegex.Match(rawline);
            if (found.Success)
            {
                return(IRCMessageType.Invite);
            }

            found = JoinRegex.Match(rawline);
            if (found.Success)
            {
                return(IRCMessageType.Join);
            }

            found = TopicRegex.Match(rawline);
            if (found.Success)
            {
                return(IRCMessageType.TopicChange);
            }

            found = NickRegex.Match(rawline);
            if (found.Success)
            {
                return(IRCMessageType.NickChange);
            }

            found = KickRegex.Match(rawline);
            if (found.Success)
            {
                return(IRCMessageType.Kick);
            }

            found = PartRegex.Match(rawline);
            if (found.Success)
            {
                return(IRCMessageType.Part);
            }

            found = ModeRegex.Match(rawline);
            if (found.Success)
            {
                if (found.Groups[1].Value == actualBotNick)
                {
                    return(IRCMessageType.UserModeChange);
                }
                else
                {
                    return(IRCMessageType.ChannelModeChange);
                }
            }

            found = QuitRegex.Match(rawline);
            if (found.Success)
            {
                return(IRCMessageType.Quit);
            }

            found = KillRegex.Match(rawline);
            return(found.Success ? IRCMessageType.Kill : IRCMessageType.Unknown);
        }
예제 #2
0
        /// <summary>
        /// Analyse source file for logging calls. Logging calls may be updated and the source modified. Behaviour depends on update mode.
        /// </summary>
        public void Analyse(ISourceFile file)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            var lines = file.ReadAllLines();

            if (lines == null)
            {
                return;
            }

            var writeRequired = false;

            for (var lineNumber = 0; lineNumber < lines.Length; ++lineNumber)
            {
                var line         = lines[lineNumber];
                var previousLine = lineNumber > 0 ? lines[lineNumber - 1] : string.Empty;

                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                var callMatch = CallRegex.Match(line);
                if (callMatch.Success == false)
                {
                    continue;
                }

                var messageMatch = MessageRegex.Match(previousLine);
                if (RequireMessage && messageMatch.Success == false)
                {
                    throw new Exception($"Failed to parse message for log call at line {lineNumber} of file '{file.Path}'");
                }

                LogLevel level;
                if (Enum.TryParse(callMatch.Groups[2].Value, out level) == false)
                {
                    throw new Exception($"Failed to parse log level '{callMatch.Groups[2].Value}' at line {lineNumber} of file '{file.Path}'");
                }

                var callInfo = new CallInfo
                {
                    Id   = callMatch.Groups[3].Value,
                    Lvl  = level,
                    Msg  = messageMatch.Success ? messageMatch.Groups[1].Value : null,
                    File = file.Path,
                    Line = lineNumber + 1
                };

                switch (UpdateMode)
                {
                case UpdateMode.All:
                    callInfo.Id = Guid.NewGuid().ToString();

                    UpdateLine(lines, lineNumber, callMatch, callInfo);
                    writeRequired = true;
                    break;

                case UpdateMode.NonUnique:
                    if (LogCallMap.ContainsKey(callInfo.Id) || string.IsNullOrWhiteSpace(callInfo.Id))
                    {
                        callInfo.Id = Guid.NewGuid().ToString();

                        UpdateLine(lines, lineNumber, callMatch, callInfo);
                        writeRequired = true;
                    }
                    break;

                default:
                    if (string.IsNullOrWhiteSpace(callInfo.Id))
                    {
                        throw new Exception($"Zero length log identifier in file '{file.Path}'");
                    }

                    if (LogCallMap.ContainsKey(callInfo.Id))
                    {
                        throw new Exception($"Duplicate log identifier '{callInfo.Id}' in file '{file.Path}'");
                    }

                    break;
                }

                LogCallMap.Add(callInfo.Id, callInfo);
            }

            if (writeRequired)
            {
                file.WriteAllLines(lines);
            }
        }