test client added + some changes

This commit is contained in:
Yaroslav Sidlovsky 2018-09-10 19:16:35 +03:00
parent 762c2ec707
commit 3de478f8ba
2 changed files with 55 additions and 11 deletions

View file

@ -9,9 +9,8 @@ module Imap
@logger : Logger @logger : Logger
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(STDERR)
@logger.level = loglevel @logger.level = loglevel
@mailboxes = [] of String
@socket = TCPSocket.new(host, port) @socket = TCPSocket.new(host, port)
tls_socket = OpenSSL::SSL::Socket::Client.new(@socket.as(TCPSocket), sync_close: true, hostname: host) tls_socket = OpenSSL::SSL::Socket::Client.new(@socket.as(TCPSocket), sync_close: true, hostname: host)
@ -33,15 +32,24 @@ module Imap
end end
private def command(command : String, *parameters, &block : String -> Bool) private def command(command : String, *parameters, &block : String -> Bool)
command_and_parameter = "tag #{command}" command_and_parameter = case command
when "DONE"
command
else
"tag #{command}"
end
if parameters.size > 0
params = parameters.join(" ") params = parameters.join(" ")
command_and_parameter += " #{params}" if params && params.size > 0 command_and_parameter += " #{params}"
@logger.info "=====> #{command_and_parameter}" end
socket << command_and_parameter << "\r\n" socket << command_and_parameter << "\r\n"
socket.flush socket.flush
@logger.debug "Sent: #{command_and_parameter}"
while (line = socket.gets) while (line = socket.gets)
@logger.debug " Got: #{line}"
break unless block.call(line) break unless block.call(line)
end end
end end
@ -78,13 +86,27 @@ module Imap
# Sends an IDLE command, # Sends an IDLE command,
# raises exception if server doesn't support IDLE # raises exception if server doesn't support IDLE
def idle def idle(&block : String, UInt32 ->)
raise "Server doesn't support IDLE" unless @caps.includes?("IDLE") raise "Server doesn't support IDLE" unless @caps.includes?("IDLE")
spawn do
command("IDLE") do |line| command("IDLE") do |line|
puts line if line =~ /\+ idling/
# nothing to do
elsif line =~ /\* (\d+) ([A-Z]+)/
block.call($2, $1.to_u32)
else
next false
end
true true
end end
end end
end
# Sends a DONE command
def done
command("DONE")
end
# 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.
@ -166,7 +188,7 @@ module Imap
end end
end end
if ip && from if ip && from
@logger.info "from: #{from} ip: #{ip}" @logger.debug "from: #{from} ip: #{ip}"
from = nil from = nil
ip = nil ip = nil
end end

22
test.cr Normal file
View file

@ -0,0 +1,22 @@
require "logger"
require "./imap"
if ARGV.size < 2
STDERR.puts "Usage: #{PROGRAM_NAME} <username> <password>"
exit 1
end
imap = Imap::Client.new(host: "imap.gmail.com", port: 993, username: ARGV[0], password: ARGV[1], loglevel: Logger::DEBUG)
mailboxes = imap.list
if mailboxes.includes?("INBOX")
imap.select("INBOX")
imap.idle do |name, value|
puts "#{name} => #{value}"
end
end
while true
sleep 5
imap.done
end