Browse files

Allow specifying a readonly mailbox

Needs documentation and tests.
  • Loading branch information...
1 parent a88c4e4 commit 9748caf06df41f9b067b3d88a0deba02b2b6f2d0 @joerixaop joerixaop committed May 3, 2012
Showing with 25 additions and 12 deletions.
  1. +14 −7 lib/gmail/client/base.rb
  2. +3 −1 lib/gmail/mailbox.rb
  3. +8 −4 lib/gmail/message.rb
View
21 lib/gmail/client/base.rb
@@ -151,18 +151,21 @@ def deliver!(mail=nil, &block)
# mailbox.count(:all)
# ...
# end
- def mailbox(name, &block)
+ def mailbox(name, examine = false, &block)
@mailbox_mutex.synchronize do
name = name.to_s
- mailbox = (mailboxes[name] ||= Mailbox.new(self, name))
- switch_to_mailbox(name) if @current_mailbox != name
+ switch_to_mailbox(name, examine) if mailbox_stack.last != [name, examine]
+ mailbox = (mailboxes[[name, examine]] ||= Mailbox.new(self, name, examine))
if block_given?
- mailbox_stack << @current_mailbox
+ mailbox_stack << [@current_mailbox, examine]
result = block.arity == 1 ? block.call(mailbox) : block.call
mailbox_stack.pop
- switch_to_mailbox(mailbox_stack.last)
+ switch_to_mailbox(*mailbox_stack.last)
return result
+ else
+ mailbox_stack.pop
+ mailbox_stack.push [@current_mailbox, examine]
end
return mailbox
@@ -196,10 +199,14 @@ def mail_domain
private
- def switch_to_mailbox(mailbox)
+ def switch_to_mailbox(mailbox, examine = false)
if mailbox
mailbox = Net::IMAP.encode_utf7(mailbox)
- conn.select(mailbox)
+ if examine
+ conn.examine(mailbox)
+ else
+ conn.select(mailbox)
+ end
end
@current_mailbox = mailbox
end
View
4 lib/gmail/mailbox.rb
@@ -18,11 +18,13 @@ class Mailbox
attr_reader :name
attr_reader :external_name
+ attr_reader :examine
- def initialize(gmail, name="INBOX")
+ def initialize(gmail, name="INBOX", examine = false)
@name = name
@external_name = Net::IMAP.decode_utf7(name)
@gmail = gmail
+ @examine = examine
end
# Returns list of emails which meets given criteria.
View
12 lib/gmail/message.rb
@@ -23,12 +23,12 @@ def uid
# Mark message with given flag.
def flag(name)
- !!@gmail.mailbox(@mailbox.name) { @gmail.conn.uid_store(uid, "+FLAGS", [name]) }
+ !!with_mailbox { @gmail.conn.uid_store(uid, "+FLAGS", [name]) }
end
# Unmark message.
def unflag(name)
- !!@gmail.mailbox(@mailbox.name) { @gmail.conn.uid_store(uid, "-FLAGS", [name]) }
+ !!with_mailbox { @gmail.conn.uid_store(uid, "-FLAGS", [name]) }
end
# Do commonly used operations on message.
@@ -152,17 +152,21 @@ def respond_to?(meth, *args, &block)
end
def envelope
- @envelope ||= @gmail.mailbox(@mailbox.name) {
+ @envelope ||= with_mailbox {
@gmail.conn.uid_fetch(uid, "ENVELOPE")[0].attr["ENVELOPE"]
}
end
def message
- @message ||= Mail.new(@gmail.mailbox(@mailbox.name) {
+ @message ||= Mail.new(with_mailbox {
@gmail.conn.uid_fetch(uid, "RFC822")[0].attr["RFC822"] # RFC822
})
end
alias_method :raw_message, :message
+ def with_mailbox(&block)
+ @gmail.mailbox(@mailbox.name, @mailbox.examine, &block)
+ end
+
end # Message
end # Gmail

0 comments on commit 9748caf

Please sign in to comment.