added more commands, brought commands inline with ruby lib
This commit is contained in:
parent
65f87822fc
commit
0dc0de50c2
2 changed files with 32 additions and 11 deletions
|
@ -21,7 +21,7 @@ imap = Imap::Client.new(host: "imap.gmail.com", port: 993, username: "email@gmai
|
||||||
mailboxes = imap.get_mailboxes
|
mailboxes = imap.get_mailboxes
|
||||||
if mailboxes.size > 0
|
if mailboxes.size > 0
|
||||||
mailbox = mailboxes[0]
|
mailbox = mailboxes[0]
|
||||||
imap.set_mailbox(mailbox)
|
imap.select(mailbox)
|
||||||
message_count = imap.get_message_count
|
message_count = imap.get_message_count
|
||||||
puts "There are #{message_count} message in #{mailbox}"
|
puts "There are #{message_count} message in #{mailbox}"
|
||||||
end
|
end
|
||||||
|
|
41
src/imap.cr
41
src/imap.cr
|
@ -32,9 +32,10 @@ module Imap
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private def command(command : String, parameter : String? = nil)
|
private def command(command : String, *parameters)
|
||||||
command_and_parameter = command
|
command_and_parameter = "tag #{command}"
|
||||||
command_and_parameter += " " + parameter if parameter
|
params = parameters.join(" ")
|
||||||
|
command_and_parameter += " #{params}" if params && params.size > 0
|
||||||
@command_history << command_and_parameter
|
@command_history << command_and_parameter
|
||||||
@logger.info "=====> #{command_and_parameter}"
|
@logger.info "=====> #{command_and_parameter}"
|
||||||
socket << command_and_parameter << "\r\n"
|
socket << command_and_parameter << "\r\n"
|
||||||
|
@ -43,19 +44,39 @@ module Imap
|
||||||
end
|
end
|
||||||
|
|
||||||
private def login(username, password)
|
private def login(username, password)
|
||||||
command("tag login #{username} #{password}")
|
command("login", username, password)
|
||||||
end
|
end
|
||||||
|
|
||||||
# sets the current mailbox
|
# Sends a SELECT command to select a +mailbox+ so that messages
|
||||||
def set_mailbox(mailbox)
|
# in the +mailbox+ can be accessed.
|
||||||
|
def select(mailbox)
|
||||||
@mailbox = mailbox
|
@mailbox = mailbox
|
||||||
command("tag SELECT #{mailbox}")
|
command("SELECT", mailbox)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Sends a EXAMINE command to select a +mailbox+ so that messages
|
||||||
|
# in the +mailbox+ can be accessed. Behaves the same as #select(),
|
||||||
|
# except that the selected +mailbox+ is identified as read-only.
|
||||||
|
def examine(mailbox)
|
||||||
|
@mailbox = mailbox
|
||||||
|
command("EXAMINE", mailbox)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Sends a DELETE command to remove the +mailbox+.
|
||||||
|
def delete(mailbox)
|
||||||
|
command("DELETE", mailbox)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Sends a RENAME command to change the name of the +mailbox+ to
|
||||||
|
# +newname+.
|
||||||
|
def rename(mailbox, newname)
|
||||||
|
command("RENAME", mailbox, newname)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns an array of mailbox names
|
# Returns an array of mailbox names
|
||||||
def get_mailboxes : Array(String)
|
def get_mailboxes : Array(String)
|
||||||
mailboxes = [] of String
|
mailboxes = [] of String
|
||||||
res = command(%{tag LIST "" "*"})
|
res = command(%{LIST "" "*"})
|
||||||
res.each do |line|
|
res.each do |line|
|
||||||
if line =~ /HasNoChildren/
|
if line =~ /HasNoChildren/
|
||||||
name = line.match(/"([^"]+)"$/)
|
name = line.match(/"([^"]+)"$/)
|
||||||
|
@ -71,7 +92,7 @@ module Imap
|
||||||
if !mailbox
|
if !mailbox
|
||||||
raise "No Mailbox set"
|
raise "No Mailbox set"
|
||||||
end
|
end
|
||||||
res = command("tag STATUS #{mailbox} (MESSAGES)")
|
res = command("STATUS #{mailbox} (MESSAGES)")
|
||||||
# eg (MESSAGES 3)
|
# eg (MESSAGES 3)
|
||||||
res.each do |line|
|
res.each do |line|
|
||||||
if line =~ /MESSAGES/
|
if line =~ /MESSAGES/
|
||||||
|
@ -125,7 +146,7 @@ module Imap
|
||||||
|
|
||||||
# Closes the imap connection
|
# Closes the imap connection
|
||||||
def close
|
def close
|
||||||
command("tag LOGOUT")
|
command("LOGOUT")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue