class Redwood::Source

Attributes

id[RW]
uri[R]
usual[R]

Public Class Methods

new(uri, usual=true, archived=false, id=nil) click to toggle source
# File lib/sup/source.rb, line 60
def initialize uri, usual=true, archived=false, id=nil
  raise ArgumentError, "id must be an integer: #{id.inspect}" unless id.is_a? Integer if id

  @uri = uri
  @usual = usual
  @archived = archived
  @id = id

  @poll_lock = Monitor.new
end
parse_raw_email_header(f) click to toggle source

utility method to read a raw email header from an IO stream and turn it into a hash of key-value pairs. minor special semantics for certain headers.

THIS IS A SPEED-CRITICAL SECTION. Everything you do here will have a significant effect on Sup's processing speed of email from ALL sources. Little things like string interpolation, regexp interpolation, += vs <<, all have DRAMATIC effects. BE CAREFUL WHAT YOU DO!

# File lib/sup/source.rb, line 130
def self.parse_raw_email_header f
  header = {}
  last = nil

  while(line = f.gets)
    case line
    ## these three can occur multiple times, and we want the first one
    when /^(Delivered-To|X-Original-To|Envelope-To):\s*(.*?)\s*$/i; header[last = $1.downcase] ||= $2
    ## regular header: overwrite (not that we should see more than one)
    ## TODO: figure out whether just using the first occurrence changes
    ## anything (which would simplify the logic slightly)
    when /^([^:\s]+):\s*(.*?)\s*$/i; header[last = $1.downcase] = $2
    when /^\r*$/; break # blank line signifies end of header
    else
      if last
        header[last] << " " unless header[last].empty?
        header[last] << line.strip
      end
    end
  end

  %w(subject from to cc bcc).each do |k|
    v = header[k] or next
    next unless Rfc2047.is_encoded? v
    header[k] = begin
      Rfc2047.decode_to $encoding, v
    rescue Errno::EINVAL, Iconv::InvalidEncoding, Iconv::IllegalSequence => e
      #debug "warning: error decoding RFC 2047 header (#{e.class.name}): #{e.message}"
      v
    end
  end
  header
end

Protected Class Methods

expand_filesystem_uri(uri) click to toggle source
# File lib/sup/source.rb, line 169
def Source.expand_filesystem_uri uri
  uri.gsub "~", File.expand_path("~")
end

Public Instance Methods

==(o;) click to toggle source
# File lib/sup/source.rb, line 75
def == o; o.uri == uri; end
file_path() click to toggle source

overwrite me if you have a disk incarnation

# File lib/sup/source.rb, line 72
def file_path; nil end
go_idle() click to toggle source

release resources that are easy to reacquire. it is called after processing a source (e.g. polling) to prevent resource leaks (esp. file descriptors).

# File lib/sup/source.rb, line 83
def go_idle; end
is_source_for?(uri;) click to toggle source
# File lib/sup/source.rb, line 76
def is_source_for? uri; uri == @uri; end
labels?(info;) click to toggle source

Returns an array containing all the labels that are currently in the location filename

# File lib/sup/source.rb, line 91
def labels? info; [] end
poll() click to toggle source

Yields values of the form [Symbol, Hash] add: info, labels, progress delete: info, progress

# File lib/sup/source.rb, line 96
def poll
  unimplemented
end
read?() click to toggle source
# File lib/sup/source.rb, line 78
def read?; false; end
supported_labels?() click to toggle source

Returns an array containing all the labels that are natively supported by this source

# File lib/sup/source.rb, line 87
def supported_labels?; [] end
synchronize(&block) click to toggle source
# File lib/sup/source.rb, line 104
def synchronize &block
  @poll_lock.synchronize &block
end
to_s() click to toggle source
# File lib/sup/source.rb, line 74
def to_s; @uri.to_s; end
try_lock() click to toggle source
# File lib/sup/source.rb, line 108
def try_lock
  acquired = @poll_lock.try_enter
  if acquired
    debug "lock acquired for: #{self}"
  else
    debug "could not acquire lock for: #{self}"
  end
  acquired
end
unlock() click to toggle source
# File lib/sup/source.rb, line 118
def unlock
  @poll_lock.exit
  debug "lock released for: #{self}"
end
valid?(info) click to toggle source
# File lib/sup/source.rb, line 100
def valid? info
  true
end

Protected Instance Methods

parse_raw_email_header(f;) click to toggle source

convenience function

# File lib/sup/source.rb, line 167
def parse_raw_email_header f; self.class.parse_raw_email_header f end