Created
November 15, 2011 07:17
-
-
Save thefotios/1366380 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'rubygems' | |
| require 'logger' | |
| require 'chronic' | |
| require 'ri_cal' | |
| require 'action_mailer' | |
| require 'mail' | |
| # Use this gem for getting mail: https://github.com/mikel/mail | |
| # - I will convert sending mail to that shortly | |
| $username = 'followuptest1@gmail.com' | |
| $password = ARGV[0] | |
| ActionMailer::Base.smtp_settings = { | |
| :address => 'smtp.redhat.com', | |
| :port => '587', | |
| } | |
| class Notifier < ActionMailer::Base | |
| default :from => $username | |
| def reminder(hash,event) | |
| attachments['reminder.ics'] = { | |
| :mime_type => 'text/calendar', | |
| :content => event.export | |
| } | |
| mail(:to => hash[:from], :subject => "Reminder: #{hash[:subject]}") do |format| | |
| format.text{ render :text => ''} | |
| format.html{ render :text => ''} | |
| end | |
| end | |
| end | |
| def parsetime(offset, time = Time.now) | |
| puts "Original offset: #{offset}" | |
| # Parse any starting numbers like '1day' | |
| if m = offset.match(/^(\d+)(.*)/) | |
| offset = m.captures.join(' ') | |
| puts "Offset: #{offset}" | |
| puts "after num split" | |
| end | |
| # Make sure 'from' isnt already specified | |
| unless m = offset.match(/from/) | |
| offset << ' from now' | |
| puts "after fro now" | |
| puts "Offset: #{offset}" | |
| end | |
| offset.gsub!('_',' ') | |
| puts "After gsub" | |
| puts "Offset: #{offset}" | |
| # Parse time | |
| Chronic.parse("#{offset}", :now => time) | |
| end | |
| # This hash should have the following information from the email | |
| # - To: Where we get the offset from | |
| # - Date: When the mail was sent | |
| # - Subject: The subject of the reminder | |
| # - Body: The description of the reminder | |
| def create_reminder(hash) | |
| if m = hash[:to].match(/.*\+(.*?)@/) | |
| offset = m.captures.first | |
| if reminder_time = parsetime(offset,hash[:date]) | |
| event = RiCal.Calendar do |cal| | |
| cal.event do |e| | |
| e.summary = hash[:subject] | |
| e.dtstart = reminder_time.getutc | |
| e.dtend = reminder_time.getutc | |
| e.description = hash[:body] | |
| end | |
| end | |
| return event | |
| end | |
| end | |
| end | |
| Mail.defaults do | |
| retriever_method :pop3, :address => 'pop.gmail.com', | |
| :port => 995, | |
| :user_name => $username, | |
| :password => $password, | |
| :enable_ssl => true | |
| end | |
| log = Logger.new(STDOUT) | |
| Mail.all.each do |m| | |
| hash = { | |
| :to => m.to.first, | |
| :from => m.from.first, | |
| :date => Time.parse(m.date.to_s), | |
| :subject => m.subject, | |
| :body => m.body.decoded | |
| } | |
| status = "No event specified" | |
| if event = create_reminder(hash) | |
| Notifier.reminder(hash,event).deliver | |
| status = "Event delivered" | |
| end | |
| log.debug "#{Time.now},#{hash[:to]},#{hash[:from]},#{status}" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment