From 47a48ba025bf621962ce5c1a79823d8a049dbcf9 Mon Sep 17 00:00:00 2001 From: Cris Ward Date: Thu, 7 Sep 2017 22:19:45 +0100 Subject: [PATCH] removing superflous stuff, trying to bring closer to ruby imap --- README.md | 2 +- src/imap.cr | 28 +++------------------------- 2 files changed, 4 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index ddb2d81..75d16c2 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ dependencies: require "imap" 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 mailbox = mailboxes[0] imap.select(mailbox) diff --git a/src/imap.cr b/src/imap.cr index e41d8f2..f56ac94 100644 --- a/src/imap.cr +++ b/src/imap.cr @@ -6,13 +6,11 @@ module Imap class Client @socket : TCPSocket | OpenSSL::SSL::Socket::Client | Nil = nil @logger : Logger - @mailbox : String? def initialize(host = "imap.gmail.com", port = 993, username = "", password = "", loglevel = Logger::ERROR) @logger = Logger.new(STDOUT) @logger.level = loglevel @mailboxes = [] of String - @mailbox = nil @command_history = [] of String @socket = TCPSocket.new(host, port) @@ -50,7 +48,6 @@ module Imap # Sends a SELECT command to select a +mailbox+ so that messages # in the +mailbox+ can be accessed. def select(mailbox) - @mailbox = mailbox command("SELECT", mailbox) end @@ -58,7 +55,6 @@ module Imap # 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 @@ -73,8 +69,9 @@ module Imap command("RENAME", mailbox, newname) end + # Returns an array of mailbox names - def get_mailboxes : Array(String) + def list : Array(String) mailboxes = [] of String res = command(%{LIST "" "*"}) res.each do |line| @@ -86,25 +83,6 @@ module Imap return mailboxes 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 # `mailbox`. `attr` is a list of one or more attributes whose # statuses are to be requested. Supported attributes include: @@ -131,7 +109,7 @@ module Imap end return vals end - + private def process_mail_headers(res) ip = nil from = nil