public override void HandleCommand(IrcLine line, Nick nick, Channel channel)
        {
            try {
                Seen     seen      = Seen.Fetch(nick);
                DateTime checkTime = DateTime.Now;
                checkTime.AddDays(-7);
                if (seen.FirstSeenAt == DateTime.MinValue || seen.FirstSeenAt > checkTime)
                {
                    conn.SendPrivmsg(nick.Name, String.Format("Sorry, {0}, but you aren't allowed to use the mugshots functions yet. :(", nick.DisplayName));
                    return;
                }

                Mugshot mugshot = Mugshot.Fetch(nick.Account);
                if (mugshot == null)
                {
                    conn.SendPrivmsg(nick.Name, "You don't have a mugshot in the database to clear! :o");
                    return;
                }

                nick.Account.MostRecentNick = nick;

                mugshot.IsDeleted = true;
                mugshot.Save();

                conn.SendPrivmsg(nick.Name, "Your mugshot has been cleared. :(");
            } catch (Exception e) {
                logger.Error(e);
                conn.SendPrivmsg(nick.Name, "Oof… I've got indigestion or something, I can't do that right now. :(");
            }
        }
Exemplo n.º 2
0
        public override void HandleCommand(IrcLine line, Nick nick, Channel channel)
        {
            try {
                Seen     seen      = Seen.Fetch(nick);
                DateTime checkTime = DateTime.Now;
                checkTime.AddDays(-7);
                if (seen.FirstSeenAt == DateTime.MinValue || seen.FirstSeenAt > checkTime)
                {
                    conn.SendPrivmsg(nick.Name, String.Format("Sorry, {0}, but you aren't allowed to use the mugshots functions yet. :(", nick.DisplayName));
                    return;
                }

                Regex r = new Regex(@"^!setinfo ?");
                line.Args = r.Replace(line.Args, "").Trim();
                if (line.Args.Length <= 0)   // Whaaaat??
                {
                    conn.SendPrivmsg(nick.Name, "Usage: !setinfo <your message here>");
                }
                else
                {
                    nick.Account.MostRecentNick = nick;

                    Info info = Info.FetchOrCreate(nick.Account);
                    info.InfoTxt   = line.Args;
                    info.IsActive  = true;
                    info.IsDeleted = false;
                    info.Save();

                    conn.SendPrivmsg(nick.Name, "Your info has been saved! :D");
                }
            } catch (Exception e) {
                logger.Error(e);
                conn.SendPrivmsg(nick.Name, "Oof… My neck is killing me, I can't do that right now. :(");
            }
        }
Exemplo n.º 3
0
        public override void HandleCommand(IrcLine line, Nick nick, Channel channel)
        {
            try {
                if (nick.Account == null)
                {
                    conn.SendPrivmsg(nick.Name, String.Format("Sorry, {0}, but you need to be identified with services to clear your info.", nick.DisplayName));
                    return;
                }

                Seen     seen      = Seen.Fetch(nick);
                DateTime checkTime = DateTime.Now;
                checkTime.AddDays(-7);
                if (seen.FirstSeenAt == DateTime.MinValue || seen.FirstSeenAt > checkTime)
                {
                    conn.SendPrivmsg(nick.Name, String.Format("Sorry, {0}, but you aren't allowed to use the mugshots functions yet. :(", nick.DisplayName));
                    return;
                }

                Info info = Info.Fetch(nick.Account);
                if (info == null)
                {
                    conn.SendPrivmsg(nick.Name, "You don't have any info in the database to clear! :o");
                    return;
                }

                nick.Account.MostRecentNick = nick;

                info.IsDeleted = true;
                info.Save();

                conn.SendPrivmsg(nick.Name, "Your info has been cleared. :(");
            } catch (Exception e) {
                logger.Error(e);
                conn.SendPrivmsg(nick.Name, "Oof… I banged my knee and it don't half hurt, I can't do that right now. :(");
            }
        }
Exemplo n.º 4
0
        static void ProcessLine(string line)
        {
            string     logNick    = null;
            string     logTime    = null;
            string     logDate    = null;
            string     message    = null;
            IrcCommand ircCommand = IrcCommand.UNKNOWN;

            Regex r;
            Match m;

            switch (type)
            {
            case LogType.znc:
                logDate = (date.HasValue ? date.Value.ToString("yyyy-MM-dd") : null);
                r       = new Regex(@"^\[(?<time>\d\d:\d\d:\d\d)\] <(?<nick>[^>]+)> (?<message>.*)$", RegexOptions.ExplicitCapture);
                m       = r.Match(line);
                if (m.Success)
                {
                    logTime    = m.Groups["time"].Value;
                    logNick    = m.Groups["nick"].Value;
                    ircCommand = IrcCommand.PRIVMSG;
                    message    = m.Groups["message"].Value;
                }

                r = new Regex(@"^\[(?<time>\d\d:\d\d:\d\d)\] (?<message>\* (?<nick>[^ ]+) .*)$", RegexOptions.ExplicitCapture);
                m = r.Match(line);
                if (m.Success)
                {
                    logTime    = m.Groups["time"].Value;
                    logNick    = m.Groups["nick"].Value;
                    ircCommand = IrcCommand.PRIVMSG;
                    message    = m.Groups["message"].Value;
                }

                r = new Regex(@"^\[(?<time>\d\d:\d\d:\d\d)\] \*\*\* Joins: (?<nick>[^ ]+) ", RegexOptions.ExplicitCapture);
                m = r.Match(line);
                if (m.Success)
                {
                    logTime    = m.Groups["time"].Value;
                    logNick    = m.Groups["nick"].Value;
                    ircCommand = IrcCommand.JOIN;
                    message    = null;
                }

                r = new Regex(@"^\[(?<time>\d\d:\d\d:\d\d)\] \*\*\* Quits: (?<nick>[^ ]+) \(.*\) \(Quit: (?<message>.*)\)$", RegexOptions.ExplicitCapture);
                m = r.Match(line);
                if (m.Success)
                {
                    logTime    = m.Groups["time"].Value;
                    logNick    = m.Groups["nick"].Value;
                    ircCommand = IrcCommand.QUIT;
                    message    = m.Groups["message"].Value;
                }

                r = new Regex("^\\[(?<time>\\d\\d:\\d\\d:\\d\\d)\\] \\*\\*\\* Parts: (?<nick>[^ ]+) \\(.*\\) \\(\"?(?<message>.*)\"?\\)$", RegexOptions.ExplicitCapture);
                m = r.Match(line);
                if (m.Success)
                {
                    logTime    = m.Groups["time"].Value;
                    logNick    = m.Groups["nick"].Value;
                    ircCommand = IrcCommand.PART;
                    message    = m.Groups["message"].Value;
                }

                if (logNick == null || logTime == null)
                {
                    // F**k-all to do here!
                    return;
                }
                break;

            case LogType.irssi:
                // Irssi uses a spastic date format:
                // --- Log opened Wed Jun 20 16:11:34 2012
                r = new Regex(@"^--- Log opened ([^ ]+) (?<month>[^ ]+) (?<day>[^ ]+) (?<time>[^ ]+) (?<year>.*)$", RegexOptions.ExplicitCapture);
                m = r.Match(line);
                if (m.Success)
                {
                    string lineDate = string.Format("{0} {1} {2} {3}", m.Groups["day"].Value, m.Groups["month"].Value, m.Groups["year"].Value, m.Groups["time"].Value);
                    date = DateTime.Parse(lineDate);
                    return;
                }

                r = new Regex(@"^--- Day changed ([^ ]+) (?<month>[^ ]+) (?<day>[^ ]+) (?<time>[^ ]+) (?<year>.*)$", RegexOptions.ExplicitCapture);
                m = r.Match(line);
                if (m.Success)
                {
                    string lineDate = string.Format("{0} {1} {2} {3}", m.Groups["day"].Value, m.Groups["month"].Value, m.Groups["year"].Value, m.Groups["time"].Value);
                    date = DateTime.Parse(lineDate);
                    return;
                }

                if (date != null)
                {
                    logDate = date.Value.ToString("yyyy-MM-dd");
                }

                if (string.IsNullOrWhiteSpace(logDate))
                {
                    throw new Exception("Date not yet set, unable to continue processing log file.");
                }

                r = new Regex(@"^(?<time>\d\d:\d\d) <.(?<nick>[^>]+)> (?<message>.*)$", RegexOptions.ExplicitCapture);
                m = r.Match(line);
                if (m.Success)
                {
                    logTime    = m.Groups["time"].Value + ":00";
                    logNick    = m.Groups["nick"].Value;
                    ircCommand = IrcCommand.PRIVMSG;
                    message    = m.Groups["message"].Value;
                }

                r = new Regex(@"^(?<time>\d\d:\d\d) (?<message>\* (?<nick>[^ ]+)> .*)$", RegexOptions.ExplicitCapture);
                m = r.Match(line);
                if (m.Success)
                {
                    logTime    = m.Groups["time"].Value + ":00";
                    logNick    = m.Groups["nick"].Value;
                    ircCommand = IrcCommand.PRIVMSG;
                    message    = m.Groups["message"].Value;
                }

                r = new Regex(@"^(?<time>\d\d:\d\d) -!- (?<nick>[^ ]+) .* has joined #", RegexOptions.ExplicitCapture);
                m = r.Match(line);
                if (m.Success)
                {
                    logTime    = m.Groups["time"].Value + ":00";
                    logNick    = m.Groups["nick"].Value;
                    ircCommand = IrcCommand.JOIN;
                    message    = null;
                }

                r = new Regex(@"^(?<time>\d\d:\d\d) -!- (?<nick>[^ ]+) .* has quit \[Quit: (?<message>.*)\]", RegexOptions.ExplicitCapture);
                m = r.Match(line);
                if (m.Success)
                {
                    logTime    = m.Groups["time"].Value + ":00";
                    logNick    = m.Groups["nick"].Value;
                    ircCommand = IrcCommand.QUIT;
                    message    = m.Groups["message"].Value;
                }

                r = new Regex("^(?<time>\\d\\d:\\d\\d) -!- (?<nick>[^ ]+) .* has left #([^ ]+) \\[\"?(?<message>.*)\"?\\]", RegexOptions.ExplicitCapture);
                m = r.Match(line);
                if (m.Success)
                {
                    logTime    = m.Groups["time"].Value + ":00";
                    logNick    = m.Groups["nick"].Value;
                    ircCommand = IrcCommand.PART;
                    message    = m.Groups["message"].Value;
                }

                if (logNick == null || logTime == null)
                {
                    // F**k-all to do here!
                    return;
                }
                break;

            default:
                throw new Exception(string.Format("Unknown log type `{0}`", type));
            }

            Nick    nick    = Nick.FetchOrCreate(logNick, server);
            Account account = nick.Account;
            Seen    seen    = Seen.Fetch(nick) ?? new Seen();

            DateTime logDateTime = DateTime.Parse(logDate + " " + logTime);

            if (seen.Nick == null)
            {
                seen.Nick = nick;
            }
            if (seen.Account == null)
            {
                seen.Account = account;
            }
            if (seen.Channel == null)
            {
                seen.Channel = channel;
            }
            if (seen.Server == null)
            {
                seen.Server = server;
            }
            if (seen.FirstSeenAt == DateTime.MinValue || logDateTime < seen.FirstSeenAt)
            {
                seen.FirstSeenAt = logDateTime;
            }
            if (seen.LastSeenAt == DateTime.MaxValue || logDateTime > seen.LastSeenAt)
            {
                seen.LastSeenAt = logDateTime;
            }

            seen.Save();

            Log log = Log.Create(server);

            log.Nick       = nick;
            log.Account    = account;
            log.Channel    = channel;
            log.IrcCommand = ircCommand;
            log.LoggedAt   = logDateTime;
            log.Trail      = message;
            log.Line       = line;
            log.Save();
        }
Exemplo n.º 5
0
        public override void HandleCommand(IrcLine line, Nick nick, Channel channel)
        {
            try {
                Setting largeImageBasePath = Setting.Fetch("Mugshots", "LargeImageBasePath");
                Setting originalBasePath   = Setting.Fetch("Mugshots", "OriginalBasePath");
                Setting thumbnailBasePath  = Setting.Fetch("Mugshots", "ThumbnailBasePath");

                Setting maxLargeHeightSetting = Setting.Fetch("Mugshots", "MaxLargeHeight");
                Setting maxLargeWidthSetting  = Setting.Fetch("Mugshots", "MaxLargeWidth");
                Setting thumbWidthSetting     = Setting.Fetch("Mugshots", "ThumbWidth");
                Setting thumbHeightSetting    = Setting.Fetch("Mugshots", "ThumbHeight");

                if (largeImageBasePath == null)
                {
                    throw new Exception("Mugshots/LargeImageBasePath is missing from Settings table");
                }
                if (originalBasePath == null)
                {
                    throw new Exception("Mugshots/OriginalBasePath is missing from Settings table");
                }
                if (thumbnailBasePath == null)
                {
                    throw new Exception("Mugshots/ThumbnailBasePath is missing from Settings table");
                }

                if (maxLargeHeightSetting == null)
                {
                    throw new Exception("Mugshots/MaxLargeHeight is missing from Settings table");
                }
                if (maxLargeWidthSetting == null)
                {
                    throw new Exception("Mugshots/MaxLargeWidth is missing from Settings table");
                }
                if (thumbWidthSetting == null)
                {
                    throw new Exception("Mugshots/ThumbWidth is missing from Settings table");
                }
                if (thumbHeightSetting == null)
                {
                    throw new Exception("Mugshots/ThumbHeight is missing from Settings table");
                }

                int maxLargeHeight = Int32.Parse(maxLargeHeightSetting.Value);
                int maxLargeWidth  = Int32.Parse(maxLargeWidthSetting.Value);
                int thumbHeight    = Int32.Parse(thumbHeightSetting.Value);
                int thumbWidth     = Int32.Parse(thumbWidthSetting.Value);

                if (maxLargeHeight < 1)
                {
                    throw new Exception("Mugshots/MaxLargeHeight in Settings table must be > 0");
                }
                if (maxLargeWidth < 1)
                {
                    throw new Exception("Mugshots/MaxLargeWidth in Settings table must be > 0");
                }
                if (thumbWidth < 1)
                {
                    throw new Exception("Mugshots/ThumbWidth in Settings table must be > 0");
                }
                if (thumbHeight < 1)
                {
                    throw new Exception("Mugshots/ThumbHeight in Settings table must be > 0");
                }

                if (!Directory.Exists(originalBasePath.Value))
                {
                    throw new Exception($"Directory `{originalBasePath.Value}` does not exist");
                }
                if (!Directory.Exists(largeImageBasePath.Value))
                {
                    throw new Exception($"Directory `{largeImageBasePath.Value}` does not exist");
                }
                if (!Directory.Exists(thumbnailBasePath.Value))
                {
                    throw new Exception($"Directory `{thumbnailBasePath.Value}` does not exist");
                }

                Seen     seen      = Seen.Fetch(nick);
                DateTime checkTime = DateTime.Now;
                checkTime.AddDays(-7);
                if (seen.FirstSeenAt == DateTime.MinValue || seen.FirstSeenAt > checkTime)
                {
                    conn.SendPrivmsg(nick.Name, String.Format("Sorry, {0}, but you aren't allowed to use the mugshots functions yet. :(", nick.DisplayName));
                    return;
                }

                Regex  r        = new Regex(@"^!setmugshot ?");
                string imageUri = r.Replace(line.Args, "").Trim();
                if (imageUri.Length <= 0)   // Whaaaat??
                {
                    conn.SendPrivmsg(nick.Name, "Usage: !setmugshot <image_url_here>");
                    return;
                }

                r = new Regex(@"^https?://.*\.(png|gif|jpe?g)");
                Match m = r.Match(imageUri);
                if (!m.Success)
                {
                    conn.SendPrivmsg(nick.Name, "Usage: !setmugshot <image_url_here> - the image must be a PNG, GIF, or JPEG file.");
                    return;
                }

                r = new Regex(@"^https?://(www.)?dropbox.com/.*\?dl=0");
                m = r.Match(imageUri);
                if (m.Success)
                {
                    imageUri = imageUri.Replace("?dl=0", "?dl=1");
                }

                Image original = GetImageFromUrl(imageUri);
                if (original == null)
                {
                    throw new Exception(String.Format("Unable to get image from URI `{0}`", imageUri));
                }

                float fileRatio = ((float)original.Width) / ((float)original.Height);
                logger.Trace("Got image! {0}x{1}, {2}", original.Width, original.Height, fileRatio);

                float maxRatio = thumbWidth / thumbHeight;
                int   width    = 0;
                int   height   = 0;
                if (fileRatio < maxRatio)
                {
                    height = maxLargeHeight;
                    width  = (int)Math.Ceiling(height * fileRatio);
                }
                else
                {
                    width  = maxLargeWidth;
                    height = (int)Math.Ceiling(width / fileRatio);
                }
                logger.Trace("Calculate resize dimensions: {0}x{1}", width, height);

                var rect  = new Rectangle(0, 0, width, height);
                var image = new Bitmap(width, height);
                try {
                    image.SetResolution(original.HorizontalResolution, original.VerticalResolution);
                } catch (Exception e) {
                    logger.Debug("image.SetResolution failed.");
                    logger.Trace(e);
                }
                using (var graphics = Graphics.FromImage(image))
                {
                    graphics.CompositingMode    = CompositingMode.SourceCopy;
                    graphics.CompositingQuality = CompositingQuality.HighQuality;
                    graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                    graphics.SmoothingMode      = SmoothingMode.HighQuality;
                    graphics.PixelOffsetMode    = PixelOffsetMode.HighQuality;

                    using (var wrapMode = new ImageAttributes())
                    {
                        wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                        graphics.DrawImage(original, rect, 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, wrapMode);
                    }
                }

                r = new Regex(@"\.([a-z0-9]{3})$");
                string newFileName = r.Replace(Path.GetRandomFileName(), ".png");
                Image  thumb       = original.GetThumbnailFixedSize(thumbWidth, thumbHeight, true);

                ImageCodecInfo info    = GetEncoderInfo("image/png");
                Encoder        encoder = Encoder.Quality;

                EncoderParameters encoderParams = new EncoderParameters(1);
                encoderParams.Param[0] = new EncoderParameter(encoder, 100);

                thumb.Save(Path.Combine(thumbnailBasePath.Value, newFileName), info, encoderParams);
                image.Save(Path.Combine(largeImageBasePath.Value, newFileName), info, encoderParams);
                original.Save(Path.Combine(originalBasePath.Value, newFileName), info, encoderParams);

                nick.Account.MostRecentNick = nick;

                Mugshot mugshot = Mugshot.FetchOrCreate(nick.Account);
                mugshot.FileName         = newFileName;
                mugshot.OriginalImageUri = imageUri;
                mugshot.IsActive         = true;
                mugshot.IsDeleted        = false;
                mugshot.LastModifiedAt   = DateTime.Now;
                mugshot.Save();

                conn.SendPrivmsg(nick.Name, "Your mugshot has been set! :D");
            } catch (SetMugshotException e) {
                logger.Error(e);
                conn.SendPrivmsg(nick.Name, "Sorry, but I was unable to download your mugshot photo from the URL you provided. Please try another URL, or poke TwoWholeWorms if this error continues.");
            } catch (Exception e) {
                logger.Error(e);
                conn.SendPrivmsg(nick.Name, "Oof… I shouldn't have eaten that pie, I can't do that right now. :(");
            }
        }