Sha256: b569adf3adfc9f271fe73c82c3a1f8acecc7d6c077107e197c5fe2813e96ec5a

Contents?: true

Size: 1.87 KB

Versions: 39

Compression:

Stored size: 1.87 KB

Contents

require 'faraday'
require 'open-uri'
require 'action_view'
require 'pdf-reader'
require 'humanize-bytes'

module Plugins
  class PDFinfo
    include Cinch::Plugin
    include Cinch::Helpers
    include ActionView::Helpers::DateHelper

    enable_acl(:nobody, false)

    self.plugin_name = 'PDF Information'
    self.help = 'This plugin will check a url for a pdf link and fetch metadata from it'


    # Default list of URL regexps to ignore.
    DEFAULT_ALLOWED = [/\.pdf$/i].freeze
    FILE_SIZE_LIMIT = 4000000


    match %r{(https?://.*?)(?:\s|$|,|\.\s|\.$)}, :use_prefix => false

    def execute(msg, url)

      allowedlist = DEFAULT_ALLOWED.dup
      allowedlist.concat(config[:blacklist]) if config[:blacklist]

      return unless allowedlist.any? { |entry| url =~ entry }
      debug "URL matched: #{url}"

      # Grab header and check filesize
      # one line to make request
      head = Faraday.head url

      # example with headers
      file_size = head.headers['Content-Length']
      humanize_size = Humanize::Byte.new(file_size.to_i).to_k.to_s.to_i.round(2)
      if file_size.to_i > FILE_SIZE_LIMIT
        return msg.reply("PDF → Unable to parse. file too big #{humanize_size}kb")
      end


      # Get file and parse metadata
      open(url, "rb") do |io|
        reader = PDF::Reader.new(io)
        creator = reader.info[:Creator] || 'Anon'
        producer = reader.info[:Producer] || 'Anon'
        creation = reader.info[:CreationDate] || 'now'
        modification = reader.info[:ModDate] || 'now'
        title = reader.info[:Title] || nil
        display = title ? title : "Title: None <> Creator: #{creator} <> Producer: #{producer} <> Creation: #{creation}"
        msg.reply "PDF (#{humanize_size}kb) → #{display}"
      end


    rescue => e
      error "#{e.class.name}: #{e.message}"
    end


  end

end

# AutoLoad
Bot.config.plugins.plugins.push Plugins::PDFinfo

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
zetabot-2.1.2 lib/Zeta/plugins/pdfinfo.rb
zetabot-2.1.1 lib/Zeta/plugins/pdfinfo.rb
zetabot-2.1.0 lib/Zeta/plugins/pdfinfo.rb
zetabot-2.0.9 lib/Zeta/plugins/pdfinfo.rb
zetabot-2.0.8 lib/Zeta/plugins/pdfinfo.rb
zetabot-2.0.7 lib/Zeta/plugins/pdfinfo.rb
zetabot-2.0.6 lib/Zeta/plugins/pdfinfo.rb
zetabot-2.0.5 lib/Zeta/plugins/pdfinfo.rb
zetabot-2.0.4 lib/Zeta/plugins/pdfinfo.rb
zetabot-2.0.3 lib/Zeta/plugins/pdfinfo.rb
zetabot-2.0.2 lib/Zeta/plugins/pdfinfo.rb
zetabot-2.0.1 lib/Zeta/plugins/pdfinfo.rb
zetabot-2.0.0 lib/Zeta/plugins/pdfinfo.rb
zetabot-1.1.0 lib/Zeta/plugins/pdfinfo.rb
zetabot-1.0.3 lib/Zeta/plugins/pdfinfo.rb
zetabot-1.0.2 lib/Zeta/plugins/pdfinfo.rb
zetabot-1.0.1 lib/Zeta/plugins/pdfinfo.rb
zetabot-1.0.0 lib/Zeta/plugins/pdfinfo.rb
zetabot-0.0.22 lib/Zeta/plugins/pdfinfo.rb
zetabot-0.0.21 lib/Zeta/plugins/pdfinfo.rb