Exemple #1
0
        static TitlingResult HandleHtml(
            TitlingRequest request,
            HtmlPage page,
            Func <TitlingRequest, string, TitlingResult> handler)
        {
            const int maxTitleLength = 1024;

            ReportCharsets(request, page);

            string htmlTitle = WebTools.GetTitle(page.Content);

            if (string.IsNullOrWhiteSpace(htmlTitle))
            {
                request.AddMessage("No <title> found, or title element was empty/whitespace.");
                return(request.CreateResult(false));
            }
            if (htmlTitle.Length > maxTitleLength)
            {
                request.AddMessage("HTML title length was in excess of 1024 characters, assuming spam.");
                return(request.CreateResult(false));
            }
            // If defined and not of ridiculous length make it available to TitleBuilder.
            request.IrcTitle.HtmlTitle = htmlTitle;
            return(handler(request, page.Content));
        }
Exemple #2
0
        // To conform to the signature we accept the HTML document. We don't need it.
        TitlingResult GenericHandler(TitlingRequest req, string htmlDoc)
        {
            // Because the similarity can only be 1 max, allow all titles to be printed if Threshold is set to 1 or
            // higher. The similarity would always be equal to or less than 1.
            if (Threshold >= 1)
            {
                return(req.CreateResult(true));
            }
            // If Threshold is set to 0 that would still mean that titles that had 0 similarity with their URLs would
            // get printed. Set to a negative value to never print any title.
            if (Threshold < 0)
            {
                return(req.CreateResult(false));
            }

            double urlTitleSimilarity = UrlTitle.Similarity(req.Url, req.IrcTitle.HtmlTitle);

            req.AddMessage(
                string.Format("URL-Title Similarity: {0} [Threshold: {1}]", urlTitleSimilarity, Threshold)
                );

            if (urlTitleSimilarity <= Threshold)
            {
                return(req.CreateResult(true));
            }

            return(req.CreateResult(false));
        }
Exemple #3
0
        static void ReportCharsets(TitlingRequest req, HtmlPage page)
        {
            var encInfo = string.Format("(HTTP) \"{0}\" -> {1} ; (HTML) \"{2}\" -> {3}",
                                        page.HeadersCharset, page.EncHeaders,
                                        page.HtmlCharset, page.EncHtml);

            req.AddMessage(encInfo);
        }
Exemple #4
0
        public static TitlingResult BinaryToIrc(TitlingRequest req, WebBytes wb)
        {
            req.Resource = wb;
            if (!wb.Success)
            {
                return(req.CreateResult(false));
            }

            var media = MediaDispatch.Parse(wb.Data);

            string type;

            switch (media.Type)
            {
            case MediaType.Jpeg:
                type = "JPEG";
                break;

            case MediaType.Png:
                type = "PNG";
                break;

            case MediaType.Gif:
                type = "GIF";
                break;

            case MediaType.Matroska:
                type = "Matroska";
                break;

            case MediaType.Webm:
                type = "WebM";
                break;

            default:
                type = wb.ContentType;
                req.AddMessage("Binary format not supported.");
                break;
            }

            FormatBinaryInfo(req.IrcTitle, type, media, wb.ContentLength);
            return(req.CreateResult(true));
        }
Exemple #5
0
        public TitlingResult ThreadTopicToIrc(TitlingRequest req)
        {
            ChanPost post;

            switch (GetSource(req.Url))
            {
            case Source.FourChan:
                post = FourChan.GetPost(req.Url);
                break;

            case Source.ArchiveMoe:
                post = ArchiveMoe.GetPost(req.Url);
                break;

            default:
                // Fail loudly for now, this might change in the future.
                throw new NotSupportedException("Passed TitlingRequest not supported.");
            }
            req.Resource = post;

            if (post.Success)
            {
                string topic = ConstructTopic(post, req.Url);
                if (topic == null)
                {
                    req.AddMessage("Post contained neither subject or comment.");
                    return(req.CreateResult(false));
                }

                req.IrcTitle.SetFormat("[ /{0}/ - {1} ] [ {2} ]", post.Board, post.BoardName, topic);
                return(req.CreateResult(true));
            }
            else
            {
                return(req.CreateResult(false));
            }
        }