Class: Server

Inherits:
Object
  • Object
show all
Defined in:
lib/imap-feeder/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Server

Returns a new instance of Server.



9
10
11
12
13
# File 'lib/imap-feeder/server.rb', line 9

def initialize(params)
  @connection = Net::IMAP.new(params[:host], params[:port], params[:use_ssl])
  result = @connection.(params[:user], params[:pass])
  @format = params[:format]
end

Instance Method Details

#create_folder(folder) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/imap-feeder/server.rb', line 65

def create_folder(folder)
  path = ''
  folder.split('.').each do |part|
    path << part
    @connection.create(path) unless has_folder?(path)
    path << '.'
  end
end

#delete(message, folder = "INBOX") ⇒ Object



80
81
82
83
84
# File 'lib/imap-feeder/server.rb', line 80

def delete(message, folder="INBOX")
  @connection.select(folder)
  @connection.store(message.id, "+FLAGS", [:Deleted])
  @connection.expunge
end

#delete_folder(folder) ⇒ Object



74
75
76
77
78
# File 'lib/imap-feeder/server.rb', line 74

def delete_folder(folder)
  #Switch to root so we can delete the folder
  @connection.examine("INBOX")
  @connection.delete(folder)
end

#disconnectObject



15
16
17
# File 'lib/imap-feeder/server.rb', line 15

def disconnect
  @connection.disconnect if @connection
end

#has?(title, folder) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/imap-feeder/server.rb', line 54

def has?(title, folder)
  retrieve(title, folder) != nil
end

#has_folder?(folder) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
# File 'lib/imap-feeder/server.rb', line 58

def has_folder?(folder)
  @connection.examine(folder)
  true
rescue Net::IMAP::NoResponseError
  false
end

#retrieve(title, folder = "INBOX") ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/imap-feeder/server.rb', line 32

def retrieve(title, folder="INBOX")
  @connection.examine(folder)
  found = @connection.search(["SUBJECT", title]).first
  return if not found

  imap_header = @connection.fetch([found], "BODY[HEADER.FIELDS (SUBJECT)]")
  retr_title = imap_header.first.attr["BODY[HEADER.FIELDS (SUBJECT)]"]
  retr_title.gsub!(/(^Subject: )|[\n\r]/, "")

  Message.new(:title => base64decode(retr_title), :id => found)
end

#send(message, folder = "INBOX") ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/imap-feeder/server.rb', line 19

def send(message, folder="INBOX")
  @connection.select(folder)

  if @format == :html
    message = message.as_html
  else
    message = message.format
  end

  message.gsub!(/\n/, "\r\n")
  @connection.append(folder, message, nil, Time.now)
end