class MailLinks

show me the urls.

Public Class Methods

new(mail = nil) click to toggle source
# File lib/maillinks.rb, line 33
def initialize(mail = nil)

  init_logger(STDOUT)
  # a file may be piped in from the command-line...
  @log.debug('parameter is ' << (mail ? mail : ' N I L' ))
  if(mail && '-' != mail.strip)
    msg = File_Checking.file_check(mail, :exist, :file, :readable)
    if(msg)
      @log.error(msg)
      usage
      exit false
    else
      @log.debug('will work on mail-file ' << mail)
      @mail = mail

      File::open(OUT_FILE, 'w+') {|of| of << File::read(@mail) }
    end
  elsif(!$<.eof?)     
    @log.debug('Reading data from STDIN. Hit Ctrl+D to interrupt')
    input = $<.read
    @log.debug('input length is ' << input.length.to_s)
    if(input.length < 10)
      @log.error('insufficient input data')
      @log.error('No mail text provided')
      usage()
      exit false
    end
    # ... provide a temporary file in that case,
    # @mail = OUT_FILE
    File.open(OUT_FILE, 'w+') {|of| of << input}
    @mail = OUT_FILE.dup
    @log.debug('Temporary eml-file is written.')
  end

  procede
end

Private Instance Methods

handle_mail() click to toggle source
# File lib/maillinks.rb, line 86
def handle_mail
  begin
    mail_content = File.read(OUT_FILE).encode(:invalid=>:replace, :undef=>:replace)
    mail_body = mail_content.split("</head>")[1].split("</html>")[0]
    mail_body = Mail::Encodings::QuotedPrintable.decode(mail_body)
  rescue Exception => ex
    @log.error('Cannot find the html-body in this mail. Maybe it is not a HTML-mail?')
    @log.debug(ex.message)
  end
  if !mail_body
    mail_body = mail_content
  end

  html_mail = Nokogiri::HTML::Document.new.fragment(mail_body)

  a_nodes = html_mail.xpath(".//a")
  if(! a_nodes || a_nodes.empty? )
    @log.info('no links in this mail')
    exit true
  end
  begin
    # See idiots in constants.rb
    body = nil
    Block_Tags.each do |bt|
      body = html_mail.xpath('.//' << bt )[0]
      break if body
    end
    # abort, if not
    body.add_child("<hr/>")
    body.add_child("<h2>HTML-links in this mail</h2>")[0]
    if(!body)
      @log.error('cannot find a body-tag in this mail')
      exit false
    end
    dl = body.add_child("<dl id='links'></dl>")[0]
    if(!dl)
      @log.error('cannot add a list-tag')
      exit false
    end
    links_hash = Hash.new     
    a_nodes.each_with_index do |a, i|
      @log.debug('a is ' << a.to_s.gsub("=\" ", ''))
      caption = a.inner_text.to_s.strip
      href = a.attribute('href')
      if(href)
        @log.debug('href is: ' << href.to_s)
        link = href.content
        @log.debug('link is ex href.content: ' << link.to_s)
        a.add_next_sibling("<span>[" << (i.next).to_s << "]</span>")
        dl.add_child("<dt>%i) %s</dt>"%[i.next, caption])
        dl.add_child("<dd style='white-space:nowrap;'><a href='%s'>%s</a></dd>"%[link,link])
      end
    end
  rescue Exception => ex
    @log.error("cannot handle the HTML in this mail: " << ex.message)
    exit false
  end

  File.open(OUT_FILE, 'w') do |mf| 
    mf << html_mail
  end
end
procede() click to toggle source
# File lib/maillinks.rb, line 71
def procede
  handle_mail
  show_mail
end
show_mail() click to toggle source
# File lib/maillinks.rb, line 81
def show_mail
  cmd = Escape.shell_command([BROWSER, OUT_FILE])
  system(cmd)
end
usage() click to toggle source
# File lib/maillinks.rb, line 75
def usage
  msg =  'Usage: ' << $0 << ' ' << '<mail-file>'
  msg << "\n   or: " << 'cat <mail-file> |' << $0
  puts msg
end