test client added + some changes
This commit is contained in:
parent
762c2ec707
commit
3de478f8ba
2 changed files with 55 additions and 11 deletions
44
src/imap.cr
44
src/imap.cr
|
@ -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
|
||||||
params = parameters.join(" ")
|
when "DONE"
|
||||||
command_and_parameter += " #{params}" if params && params.size > 0
|
command
|
||||||
@logger.info "=====> #{command_and_parameter}"
|
else
|
||||||
|
"tag #{command}"
|
||||||
|
end
|
||||||
|
|
||||||
|
if parameters.size > 0
|
||||||
|
params = parameters.join(" ")
|
||||||
|
command_and_parameter += " #{params}"
|
||||||
|
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,14 +86,28 @@ 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")
|
||||||
command("IDLE") do |line|
|
|
||||||
puts line
|
spawn do
|
||||||
true
|
command("IDLE") do |line|
|
||||||
|
if line =~ /\+ idling/
|
||||||
|
# nothing to do
|
||||||
|
elsif line =~ /\* (\d+) ([A-Z]+)/
|
||||||
|
block.call($2, $1.to_u32)
|
||||||
|
else
|
||||||
|
next false
|
||||||
|
end
|
||||||
|
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.
|
||||||
def select(mailbox)
|
def select(mailbox)
|
||||||
|
@ -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
22
test.cr
Normal 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
|
Loading…
Reference in a new issue