class GxG::Networking::Pop3Client

POP3 Classes:

Public Class Methods

new(host=nil,port=nil,options={}) click to toggle source
# File lib/gxg/net_clients.rb, line 1626
def initialize(host=nil,port=nil,options={})
  if port.is_a?(::Hash)
    options = port
    port = nil
  end
  @user = nil
  @password = nil
  @client = nil
  @host = host
  unless @host
    raise ArgumentError, "You MUST provide a HOST, like this: <host>,(<port>),(<options>)"
  end
  if options[:use_ssl] == true
    @port = (port || 995)
  else
    @port = (port || 110)
  end
  @client = ::Net::POP3.new(@host, @port, (options[:use_apop] || false))
  if options[:use_ssl] == true
    if options[:ignore_ssl_errors] == true
      @client.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
    else
      @client.enable_ssl(OpenSSL::SSL::VERIFY_PEER)
    end
  end
  #
  self
end

Public Instance Methods

login(user_id=nil,password=nil,options={}) click to toggle source
# File lib/gxg/net_clients.rb, line 1655
def login(user_id=nil,password=nil,options={})
  if @client
    @user = user_id
    @password = password
    unless @password
      raise ArgumentError, "You MUST provide a PASSWORD, like this: <user_id>,<password>,(<options>)"
    end
    @client.start(@user, @password)
    true
  else
    false
  end
  #
end
logout() click to toggle source
# File lib/gxg/net_clients.rb, line 1670
def logout()
  if @client
    @client.finish
    @client = nil
    true
  end
end
message_count() click to toggle source
# File lib/gxg/net_clients.rb, line 1678
def message_count()
  # Return the number of emails on the server.
  if @client
    @client.n_mails()
  else
    0
  end
end
retrieve_messages(options={:read_only=>false},&block) click to toggle source
# File lib/gxg/net_clients.rb, line 1687
def retrieve_messages(options={:read_only=>false},&block)
  result = []
  begin
    if @client
      if @client.started?
        if @client.active?
          parse_message = Proc.new do |the_raw_message|
            header = {}
            body = []
            attachments = []
            parser = ::Mail.new(the_raw_message.pop)
            parser.header_fields.each do |field|
              the_key = field.name.to_s.to_sym
              the_data = field.to_s.transcode({:replace => "."},::Encoding::UTF_8)
              the_data.gsub!("         ","")
              the_data.gsub!("        ","")
              the_data.gsub!("   ","")
              the_data.gsub!("\r\n","")
              if header[(the_key)]
                unless header[(the_key)].is_a?(::Array)
                  header[(the_key)] = [(header[(the_key)])]
                end
                header[(the_key)] << the_data
              else
                header[(the_key)] = the_data
              end
            end
            if parser.multipart?
              link_db = []
              body << {:preamble => parser.body.preamble}
              parser.parts.each do |part|
                if part.multipart?
                  link_db << part
                else
                  if part.attachment?
                    attachments << {:content_type => {:type => part.mime_type, :parameters => part.content_type_parameters}, :filename => part.filename, :path => nil, :content => ::GxG::ByteArray.new(part.decoded)}
                  else
                    body << {:content_type => {:type => part.mime_type, :parameters => part.content_type_parameters}, :content => part.body.to_s.transcode({:replace => "."},::Encoding::UTF_8)}
                  end
                end
              end
              while link_db.size > 0 do
                part = link_db.shift
                if part.multipart?
                  part.parts.each do |the_sub_part|
                    link_db << the_sub_part
                  end
                else
                  if part.attachment?
                    attachments << {:content_type => {:type => part.mime_type, :parameters => part.content_type_parameters}, :filename => part.filename, :path => nil, :content => ::GxG::ByteArray.new(part.decoded)}
                  else
                    body << {:content_type => {:type => part.mime_type, :parameters => part.content_type_parameters}, :content => part.body.to_s.transcode({:replace => "."},::Encoding::UTF_8)}
                  end
                end
              end
              body << {:epilogue => parser.body.epilogue}
            else
              body << {:preamble => parser.body.preamble.to_s.transcode({:replace => "."},::Encoding::UTF_8)}
              mime_type = "text/plain"
              parameters = {"charset" => "UTF-8"}
              body_text = parser.body.to_s.transcode({:replace => "."},::Encoding::UTF_8)
              if body_text.xml?
                mime_type = "text/xml"
              end
              if body_text.html?
                mime_type = "text/html"
              end
              body << {:content_type => {:type => mime_type, :parameters => parameters}, :content => body_text}
              body << {:epilogue => parser.body.epilogueto_s.transcode({:replace => "."},::Encoding::UTF_8)}
            end
            gxg_message = new_message
            gxg_message[:sender] = parser.from
            gxg_message[:id] = parser.message_id
            gxg_message[:to] = parser.to
            gxg_message[:cc] = parser.cc
            gxg_message[:bcc] = parser.bcc
            gxg_message[:subject] = parser.subject
            gxg_message[:date] = parser.date
            gxg_message[:header] = header
            gxg_message[:body] = body
            gxg_message[:attachments] = attachments
            gxg_message
          end
          @client.each_mail do |raw_message|
            if block.respond_to?(:call)
              block.call(parse_message.call(raw_message))
            else
              result << parse_message.call(raw_message)
            end
            unless options[:read_only] == true
              raw_message.delete
            end
          end
        end
      end
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {}})
  end
  result
end