Guest User

Untitled

a guest
Jul 21st, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'rubygems'
  4. require 'mail'
  5.  
  6. # this is kept in ~/scripts but the /tmp/ directory used is
  7. # actually ~/tmp. I guess webfaction runs the mail script
  8. # from the home folder no matter where the script actually is
  9.  
  10. # TODO deal with encoding issues
  11. # (real quation marks, hyphens, brackets, etc)
  12.  
  13. begin
  14. # empty the error output
  15. File.open('tmp/pinboard-error.txt', "w") do |f|
  16. f.puts "#{Time.new.inspect}"
  17. f.puts "NO ERROR\n\n"
  18. end
  19.  
  20. # have to write to a file then read that in
  21. # the Mail gem throws an error reading from STDIN/ARGF
  22. # (with webfaction anyway)
  23. File.open('tmp/pinboard-input.txt', "w") do |f|
  24. f.puts STDIN.read
  25. end
  26.  
  27. #mail_in = Mail.read(STDIN.read) # ERROR file name too long
  28. mail = Mail.read('tmp/pinboard-input.txt')
  29.  
  30. if mail.from.to_s == 'YOU@YOUR_DOMAIN.COM'
  31. # get rid of the 'Article: ' that Instapaper puts in front of the subject
  32. title = mail.subject.to_s.gsub(/^Article: /, "")
  33. recipient = 'YOUR_SECRET_EMAIL@pinboard.in'
  34. else
  35. title = "PINBOARD ERROR from " + mail.from.to_s + " -- " + mail.subject
  36. recipient = 'YOU@YOUR_DOMAIN.COM'
  37. end
  38.  
  39. if mail.multipart?
  40. link_quote_tags = mail.text_part.body.to_s
  41. else
  42. link_quote_tags = mail.body.decoded
  43. end
  44.  
  45. pinboard_mail = Mail.new do
  46. from 'YOU@YOUR_DOMAIN.COM'
  47. to recipient
  48. subject title
  49. body link_quote_tags
  50. end
  51.  
  52. pinboard_mail.delivery_method :sendmail
  53.  
  54. pinboard_mail.deliver
  55.  
  56. rescue Exception => e
  57. File.open('tmp/pinboard-error.txt', "w") do |f|
  58. f.puts "#{Time.new.inspect}"
  59. f.puts "ERROR:\n\n"
  60. f.puts e
  61. end
  62. end
Add Comment
Please, Sign In to add comment