Skip to content

Commit

Permalink
Implement proper locking and fix headers
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed May 8, 2012
1 parent 923c917 commit 6107ae8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
25 changes: 12 additions & 13 deletions bin/mbox-daemon
Expand Up @@ -42,32 +42,33 @@ end.parse!
end
}

class Connection < EventMachine::Protocols::LineAndTextProtocol
@@unread = {}
@@unread_checking = false
$unread = {}

class Connection < EventMachine::Protocols::LineAndTextProtocol
attr_accessor :boxes

def receive_line (line)
whole, target, command, rest = line.match(/^(.*?)\s+(.*?)(?:\s+(.+))?$/).to_a

boxes = target == '*' ? @boxes : @boxes.select { |box| target.include? box.name }
boxes = target == '*' ? $boxes : $boxes.select { |box| target.include? box.name }

if command == 'list'
command = rest

if command == 'unread'
send_response boxes.select {|box|
if !@@unread[box] || @@unread[box].last_check < [File.ctime(box.path), File.mtime(box.path)].max
unless @@unread_checking
if !$unread[box] || $unread[box].last_check < [File.ctime(box.path), File.mtime(box.path)].max
unless $unread[:checking]
$unread[:checking] = true

EM.defer {
@@unread[box] = Struct.new(:status, :last_check).new(box.has_unread?, Time.new)
@@unread_checking = false
$unread[box] = Struct.new(:status, :last_check).new(box.has_unread?, Time.new)
$unread[:checking] = false
}
end
end

@@unread[box].status rescue false
$unread[box].status rescue false
}.map(&:name)
end
end
Expand All @@ -81,13 +82,11 @@ class Connection < EventMachine::Protocols::LineAndTextProtocol
end

EM.run {
boxes = options[:mail][:boxes].map {|name|
$boxes = options[:mail][:boxes].map {|name|
Mbox.open("#{options[:mail][:directory]}/#{name}")
}

EM.start_server options[:host], options[:port], Connection do |c|
c.boxes = boxes
end
EM.start_server options[:host], options[:port], Connection

EM.add_periodic_timer options[:every] do
EM.system 'fetchmail'
Expand Down
2 changes: 2 additions & 0 deletions lib/mbox/mail/headers.rb
Expand Up @@ -47,6 +47,8 @@ def == (other)
to_sym == Name.parse(other).to_sym
end

alias eql? ==

def hash
to_sym.hash
end
Expand Down
8 changes: 8 additions & 0 deletions lib/mbox/mbox.rb
Expand Up @@ -67,9 +67,17 @@ def close
def each (opts = {})
@input.seek 0

if @input.respond_to? :flock
@input.flock File::LOCK_SH
end

while mail = Mail.parse(@input, options.merge(opts))
yield mail
end

if @input.respond_to? :flock
@input.flock File::LOCK_UN
end
end

def [] (index, opts = {})
Expand Down

0 comments on commit 6107ae8

Please sign in to comment.