From b37a15ac1f29a898699180ffd90a796798a6ba63 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Tue, 26 Jul 2016 20:36:36 +0200 Subject: [PATCH] Introduce "--images" flag to enable image link parsing and disable image link parsing by default. --- main.go | 12 +++++++++++- parsers/web/parser.go | 36 +++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index c5ae6fb..fda8e1b 100644 --- a/main.go +++ b/main.go @@ -32,6 +32,8 @@ func main() { var soundcloudClientId string var soundcloudClientSecret string + var webEnableImages bool + var debug bool var useTLS bool var server string @@ -58,9 +60,14 @@ func main() { // Youtube config kingpin.Flag("youtube-key", "The API key to use to access the YouTube API.").StringVar(&youtubeApiKey) + + // SoundCloud config kingpin.Flag("soundcloud-id", "The SoundCloud ID.").StringVar(&soundcloudClientId) kingpin.Flag("soundcloud-secret", "The SoundCloud secret.").StringVar(&soundcloudClientSecret) + // Web parser config + kingpin.Flag("images", "Enables parsing links of images. Disabled by default for legal reasons.").BoolVar(&webEnableImages) + kingpin.Parse() if len(nickname) == 0 { @@ -100,7 +107,10 @@ func main() { must(m.RegisterParser(new(wikipedia.Parser))) // Load web parser - must(m.RegisterParser(new(web.Parser))) + webParser := &web.Parser{ + EnableImages: webEnableImages, + } + must(m.RegisterParser(webParser)) // IRC conn := m.AntifloodIrcConn(irc.IRC(nickname, ident)) diff --git a/parsers/web/parser.go b/parsers/web/parser.go index 0097859..25d593b 100644 --- a/parsers/web/parser.go +++ b/parsers/web/parser.go @@ -33,7 +33,9 @@ const ( maxHtmlSize = 8 * 1024 ) -type Parser struct{} +type Parser struct { + EnableImages bool +} func (p *Parser) Init() error { return nil @@ -119,23 +121,27 @@ func (p *Parser) Parse(u *url.URL, referer *url.URL) (result parsers.ParseResult result.Information[0]["Title"] = noTitleStr } case "image/png", "image/jpeg", "image/gif": + if p.EnableImages { - // No need to limit the reader to a specific size here as - // image.DecodeConfig only reads as much as needed anyways. - if m, imgType, err := image.DecodeConfig(resp.Body); err != nil { - result.UserError = ErrCorruptedImage - } else { - info := map[string]interface{}{ - "IsUpload": true, - "ImageSize": image.Point{X: m.Width, Y: m.Height}, - "ImageType": strings.ToUpper(imgType), + // No need to limit the reader to a specific size here as + // image.DecodeConfig only reads as much as needed anyways. + if m, imgType, err := image.DecodeConfig(resp.Body); err != nil { + result.UserError = ErrCorruptedImage + } else { + info := map[string]interface{}{ + "IsUpload": true, + "ImageSize": image.Point{X: m.Width, Y: m.Height}, + "ImageType": strings.ToUpper(imgType), + } + if resp.ContentLength > 0 { + info["Size"] = uint64(resp.ContentLength) + } + result.Information = []map[string]interface{}{info} } - if resp.ContentLength > 0 { - info["Size"] = uint64(resp.ContentLength) - } - result.Information = []map[string]interface{}{info} - log.Printf("Got through: %+v!", info) + break } + + fallthrough default: // TODO - Implement generic head info? result.Ignored = true