require 'net/imap' require 'fileutils' class Exg2Cal attr_reader :list @@list = "\n\n" def initialize(servername, mailbox, user, password) @imap = Net::IMAP.new(servername) @imap.login(user, password) @imap.select(mailbox) end def write_cal(filename) File.open(filename + ".ics","a"){ |file| file << @@list } end def extract(range) @imap.fetch(range, "FLAGS").each{ |message| unless message.attr["FLAGS"].to_s.downcase =~ /flagged/ then @imap.fetch(message.seqno,"BODYSTRUCTURE").each{ |item| @subtype=item.attr["BODYSTRUCTURE"].subtype @boundary=item.attr["BODYSTRUCTURE"].param["BOUNDARY"] } @body=@imap.fetch(message.seqno,"BODY[TEXT]").each{ |body| unless body.nil? then unless @subtype == "CALENDAR" then @body=body.attr['BODY[TEXT]'].split(@boundary)[2].split(/\r\n\r\n*/)[1] else @body=body.attr['BODY[TEXT]'] end @@list << @body.to_s.gsub(/\r/, '').gsub(/\\N/,'\n') unless @@list.nil? @@list << "\n\n" else end @imap.store(message.seqno, "+FLAGS", [:FLAGGED]) else end } else end } end def finish @imap.logout @imap.disconnect end end