removing superflous stuff, trying to bring closer to ruby imap

This commit is contained in:
Cris Ward 2017-09-07 22:19:45 +01:00
parent 529717b654
commit 47a48ba025
2 changed files with 4 additions and 26 deletions

View file

@ -18,7 +18,7 @@ dependencies:
require "imap" require "imap"
imap = Imap::Client.new(host: "imap.gmail.com", port: 993, username: "email@gmail.com", password: "*******") imap = Imap::Client.new(host: "imap.gmail.com", port: 993, username: "email@gmail.com", password: "*******")
mailboxes = imap.get_mailboxes mailboxes = imap.list
if mailboxes.size > 0 if mailboxes.size > 0
mailbox = mailboxes[0] mailbox = mailboxes[0]
imap.select(mailbox) imap.select(mailbox)

View file

@ -6,13 +6,11 @@ module Imap
class Client class Client
@socket : TCPSocket | OpenSSL::SSL::Socket::Client | Nil = nil @socket : TCPSocket | OpenSSL::SSL::Socket::Client | Nil = nil
@logger : Logger @logger : Logger
@mailbox : String?
def initialize(host = "imap.gmail.com", port = 993, username = "", password = "", loglevel = Logger::ERROR) def initialize(host = "imap.gmail.com", port = 993, username = "", password = "", loglevel = Logger::ERROR)
@logger = Logger.new(STDOUT) @logger = Logger.new(STDOUT)
@logger.level = loglevel @logger.level = loglevel
@mailboxes = [] of String @mailboxes = [] of String
@mailbox = nil
@command_history = [] of String @command_history = [] of String
@socket = TCPSocket.new(host, port) @socket = TCPSocket.new(host, port)
@ -50,7 +48,6 @@ module Imap
# Sends a SELECT command to select a +mailbox+ so that messages # Sends a SELECT command to select a +mailbox+ so that messages
# in the +mailbox+ can be accessed. # in the +mailbox+ can be accessed.
def select(mailbox) def select(mailbox)
@mailbox = mailbox
command("SELECT", mailbox) command("SELECT", mailbox)
end end
@ -58,7 +55,6 @@ module Imap
# in the +mailbox+ can be accessed. Behaves the same as #select(), # in the +mailbox+ can be accessed. Behaves the same as #select(),
# except that the selected +mailbox+ is identified as read-only. # except that the selected +mailbox+ is identified as read-only.
def examine(mailbox) def examine(mailbox)
@mailbox = mailbox
command("EXAMINE", mailbox) command("EXAMINE", mailbox)
end end
@ -73,8 +69,9 @@ module Imap
command("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 list : Array(String)
mailboxes = [] of String mailboxes = [] of String
res = command(%{LIST "" "*"}) res = command(%{LIST "" "*"})
res.each do |line| res.each do |line|
@ -86,25 +83,6 @@ module Imap
return mailboxes return mailboxes
end end
# Returns the number of messages in the current mailbox
def get_message_count
mailbox = @mailbox
if !mailbox
raise "No Mailbox set"
end
res = command("STATUS #{mailbox} (MESSAGES)")
# eg (MESSAGES 3)
res.each do |line|
if line =~ /MESSAGES/
match = line.match(/MESSAGES ([0-9]+)/)
if match
return match[1].to_i
end
end
end
return 0
end
# Sends a STATUS command, and returns the status of the indicated # Sends a STATUS command, and returns the status of the indicated
# `mailbox`. `attr` is a list of one or more attributes whose # `mailbox`. `attr` is a list of one or more attributes whose
# statuses are to be requested. Supported attributes include: # statuses are to be requested. Supported attributes include: