Class: VPOPMail::IMAPDB

Inherits:
Object
  • Object
show all
Defined in:
lib/vpopmail/imapdb.rb

Overview


class: IMAPDB {{{ ++ The IMAPDB class is used to manipulate the status of Message object in a Folder

Constant Summary collapse

@@logger =

class attribute: logger {{{

nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p_folder) ⇒ IMAPDB


method: initialize {{{ ++ Creates a new IMAPDB object attached to the Folder p_folder

Uses VPOPMail::CFG[“IMAPDB”] as the name of the file that contains the IMAP database.

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
# File 'lib/vpopmail/imapdb.rb', line 43

def initialize(p_folder)
	raise ArgumentError unless p_folder.kind_of?(Folder)
	@name     = VPOPMail::CFG["IMAPDB"]
	@folder   = p_folder
	@path     = @folder.path
	@filename = @folder.path + File::SEPARATOR + @name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



26
27
28
# File 'lib/vpopmail/imapdb.rb', line 26

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



26
27
28
# File 'lib/vpopmail/imapdb.rb', line 26

def path
  @path
end

Class Method Details

.loggerObject



31
# File 'lib/vpopmail/imapdb.rb', line 31

def self.logger ;            @@logger ; end

.logger=(p_object) ⇒ Object



32
# File 'lib/vpopmail/imapdb.rb', line 32

def self.logger=(p_object) ; @@logger = p_object ; end

Instance Method Details

#add(p_message) ⇒ Object


method: add {{{ ++ Adds the Message p_message to the IMAP Database.

Raises:

  • (ArgumentError)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/vpopmail/imapdb.rb', line 75

def add(p_message)
	raise ArgumentError unless p_message.kind_of?(Message)
	lines = self.load
	return if lines.empty?

	# Search the message in the IMAPDB
	lines.each {|line| return if line =~ /^\d+ #{p_message.id}/}

	# Add the message at the end with the highest uid
	lines << "#{@newuid} #{p_message.id}\n"
	@newuid = @newuid.to_i + 1
	lines[0] = "1 #{@uidv} #{@newuid}\n"

	self.save(lines)
end

#delete(p_message) ⇒ Object


method: delete {{{ ++ Deletes the Message p_message from the IMAP Database.

Raises:

  • (ArgumentError)


95
96
97
98
99
100
101
102
103
104
105
# File 'lib/vpopmail/imapdb.rb', line 95

def delete(p_message)
	raise ArgumentError unless p_message.kind_of?(Message)
	lines = self.load
	return if lines.empty?

	# Search and destroy the message in the IMAPDB
	deleted = lines.delete_if {|line| line =~ /^\d+ #{p_message.id}/}
	return if deleted.empty?

	self.save(lines)
end

#loadObject


method: load {{{ ++ Loads the IMAPDB in memory from its file.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/vpopmail/imapdb.rb', line 127

def load
	begin
		lines = IO.readlines(@filename)
	rescue Errno::ENOENT
		logger.warn "Folder #{@folder.name}: IMAPDB does not exist, nothing to do" if !@@logger.nil?
		return Array.new
	end
	if lines[0] !~ /^1 (\d+) (\d+)/ then
		logger.error "Folder #{@folder.name}: Wrong IMAPDB version, aborting" if !@@logger.nil?
		return Array.new
	end
	@uidv   = $1
	@newuid = $2
	logger.info "Folder #{@folder.name}: uidv=#{@uidv}, new uid=#{@newuid}" if !@@logger.nil?
	return lines
end

#loggerObject



33
# File 'lib/vpopmail/imapdb.rb', line 33

def logger ;                 @@logger ; end

#logger=(p_object) ⇒ Object



34
# File 'lib/vpopmail/imapdb.rb', line 34

def logger=(p_object) ;      @@logger = p_object ; end

#save(p_lines) ⇒ Object


method: save {{{ ++ Saves the IMAPDB to its file.



148
149
150
151
# File 'lib/vpopmail/imapdb.rb', line 148

def save(p_lines)
	File.open(@filename, 'w') { |file| file.puts p_lines }
	logger.info "Folder #{@folder.name}: IMAPDB saved" if !@@logger.nil?
end

#to_sObject


method: to_s {{{ ++ Returns the String representation of the Domain object



119
120
121
# File 'lib/vpopmail/imapdb.rb', line 119

def to_s
	return "IMAP Database @name, path=\"#{@path}\""
end

#to_xmlObject


method: to_xml {{{ ++ Returns the REXML::Document that represents the IMAPDB object



111
112
113
# File 'lib/vpopmail/imapdb.rb', line 111

def to_xml
	return REXML::Document.new("<IMAPDB name=\"#{@name}\" path=\"#{@path}\" />")
end

#update(p_message) ⇒ Object


method: update {{{ ++ Marks the Message p_message as updated in the IMAP Database.

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/vpopmail/imapdb.rb', line 55

def update(p_message)
	raise ArgumentError unless p_message.kind_of?(Message)
	lines = self.load
	return if lines.empty?

	# Search and destroy the message in the IMAPDB
	lines.delete_if {|line| line =~ /^\d+ #{p_message.id}/}

	# Add the message at the end with the highest uid
	lines << "#{@newuid} #{p_message.id}\n"
	@newuid = @newuid.to_i + 1
	lines[0] = "1 #{@uidv} #{@newuid}\n"

	self.save(lines)
end