#!/bin/ruby #電子メールから「来週月曜 デート」みたいなメールを送ってGoogleカレンダーに登録するデモスクリプト $KCODE='utf8' require "yaml" require "net/pop" require 'rubygems' require "googlecalendar/calendar" require "HumanDate" require "nkf" #メールを読み出すサーバの設定 POP_SERVER = "your pop3 server" POP_PORT = 110 POP_ACCOUNT = "pop3 account name" POP_PASSWORD = "pop3 account password" #作成するGoogleカレンダーのの設定 GCAL_ACCOUNT = "your account" GCAL_PASSWORD = "your password" GCAL_FEED = "http://www.google.com/calendar/feeds/default/private/full" #このsubjectのメールだけを処理します。 TARGET_SUBJECT = "gcal" class Mail attr_accessor :subject, :body def initialize(cont) self.subject = nil self.body = nil parse(cont) end SUBJECT = /^Subject: (.*)$/ def parse(cont) head = true bd = [] cont.each_line do |line| if head line.chomp! if line =~ SUBJECT self.subject = $1 elsif line == "" head = false end else bd << line end end self.body = bd.join("") end end server = GoogleCalendar::Service.new(GCAL_ACCOUNT, GCAL_PASSWORD) calendar = GoogleCalendar::Calendar.new(server, GCAL_FEED) def calendar.from_mail(mail) require 'pp' event = self.create_event mailbody = NKF.nkf("-w -Lu",mail.body).split("\n") pp mailbody event.st = parseHumanDate(mailbody[0]) event.en = event.st + (30*60) #終了時刻はきめうち30分後 event.title = mailbody[1] event.desc = mailbody[1..-1].join("\n") event.save! end pop = Net::POP3.APOP(true).new(POP_SERVER, POP_PORT) pop.start(POP_ACCOUNT, POP_PASSWORD) if pop.mails.empty? then puts 'no mail.' else i = 0 pop.each_mail do |m| mail = Mail.new(m.pop) if mail.subject == TARGET_SUBJECT calendar.from_mail(mail) m.delete i += 1 end end puts "#{i} mail(s) processed." end pop.finish