Method: CARPS::IMAP#read

Defined in:
lib/carps/email/imap.rb

#readObject

Return the a list of email message bodies

Blocks until messages become available



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/carps/email/imap.rb', line 67

def read
   if $DEBUG
      puts "Reading mail."
   end
   mails = []
   # Block 'till we get one
   while mails.empty?
      with_connection do |imap|
         imap.select("inbox")
         # Get all mails
         messages = imap.search(["ALL"])
         if messages.empty?
            sleep delay
         else
            mails = imap.fetch messages, "BODY[TEXT]"
            mails = mails.map do |mail|
               from_mail mail.attr["BODY[TEXT]"]
            end
            # Delete all mails
            messages.each do |message_id|
               imap.store(message_id, "+FLAGS", [:Deleted])
            end
            imap.expunge
         end
      end
   end
   mails
end