Skip to content

Commit 153e69f

Browse files
hsbtnevans
authored andcommitted
Import sample code from ruby/ruby
1 parent d6725eb commit 153e69f

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed

sample/net-imap.rb

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
require 'net/imap'
2+
require "getoptlong"
3+
4+
$stdout.sync = true
5+
$port = nil
6+
$user = ENV["USER"] || ENV["LOGNAME"]
7+
$auth = "login"
8+
$ssl = false
9+
$starttls = false
10+
11+
def usage
12+
<<EOF
13+
usage: #{$0} [options] <host>
14+
15+
--help print this message
16+
--port=PORT specifies port
17+
--user=USER specifies user
18+
--auth=AUTH specifies auth type
19+
--starttls use starttls
20+
--ssl use ssl
21+
EOF
22+
end
23+
24+
begin
25+
require 'io/console'
26+
rescue LoadError
27+
def _noecho(&block)
28+
system("stty", "-echo")
29+
begin
30+
yield STDIN
31+
ensure
32+
system("stty", "echo")
33+
end
34+
end
35+
else
36+
def _noecho(&block)
37+
STDIN.noecho(&block)
38+
end
39+
end
40+
41+
def get_password
42+
print "password: "
43+
begin
44+
return _noecho(&:gets).chomp
45+
ensure
46+
puts
47+
end
48+
end
49+
50+
def get_command
51+
printf("%s@%s> ", $user, $host)
52+
if line = gets
53+
return line.strip.split(/\s+/)
54+
else
55+
return nil
56+
end
57+
end
58+
59+
parser = GetoptLong.new
60+
parser.set_options(['--debug', GetoptLong::NO_ARGUMENT],
61+
['--help', GetoptLong::NO_ARGUMENT],
62+
['--port', GetoptLong::REQUIRED_ARGUMENT],
63+
['--user', GetoptLong::REQUIRED_ARGUMENT],
64+
['--auth', GetoptLong::REQUIRED_ARGUMENT],
65+
['--starttls', GetoptLong::NO_ARGUMENT],
66+
['--ssl', GetoptLong::NO_ARGUMENT])
67+
begin
68+
parser.each_option do |name, arg|
69+
case name
70+
when "--port"
71+
$port = arg
72+
when "--user"
73+
$user = arg
74+
when "--auth"
75+
$auth = arg
76+
when "--ssl"
77+
$ssl = true
78+
when "--starttls"
79+
$starttls = true
80+
when "--debug"
81+
Net::IMAP.debug = true
82+
when "--help"
83+
usage
84+
exit
85+
end
86+
end
87+
rescue
88+
abort usage
89+
end
90+
91+
$host = ARGV.shift
92+
unless $host
93+
abort usage
94+
end
95+
96+
imap = Net::IMAP.new($host, :port => $port, :ssl => $ssl)
97+
begin
98+
imap.starttls if $starttls
99+
class << password = method(:get_password)
100+
alias to_str call
101+
end
102+
imap.authenticate($auth, $user, password)
103+
while true
104+
cmd, *args = get_command
105+
break unless cmd
106+
begin
107+
case cmd
108+
when "list"
109+
for mbox in imap.list("", args[0] || "*")
110+
if mbox.attr.include?(Net::IMAP::NOSELECT)
111+
prefix = "!"
112+
elsif mbox.attr.include?(Net::IMAP::MARKED)
113+
prefix = "*"
114+
else
115+
prefix = " "
116+
end
117+
print prefix, mbox.name, "\n"
118+
end
119+
when "select"
120+
imap.select(args[0] || "inbox")
121+
print "ok\n"
122+
when "close"
123+
imap.close
124+
print "ok\n"
125+
when "summary"
126+
unless messages = imap.responses["EXISTS"][-1]
127+
puts "not selected"
128+
next
129+
end
130+
if messages > 0
131+
for data in imap.fetch(1..-1, ["ENVELOPE"])
132+
print data.seqno, ": ", data.attr["ENVELOPE"].subject, "\n"
133+
end
134+
else
135+
puts "no message"
136+
end
137+
when "fetch"
138+
if args[0]
139+
data = imap.fetch(args[0].to_i, ["RFC822.HEADER", "RFC822.TEXT"])[0]
140+
puts data.attr["RFC822.HEADER"]
141+
puts data.attr["RFC822.TEXT"]
142+
else
143+
puts "missing argument"
144+
end
145+
when "logout", "exit", "quit"
146+
break
147+
when "help", "?"
148+
print <<EOF
149+
list [pattern] list mailboxes
150+
select [mailbox] select mailbox
151+
close close mailbox
152+
summary display summary
153+
fetch [msgno] display message
154+
logout logout
155+
help, ? display help message
156+
EOF
157+
else
158+
print "unknown command: ", cmd, "\n"
159+
end
160+
rescue Net::IMAP::Error
161+
puts $!
162+
end
163+
end
164+
ensure
165+
imap.logout
166+
imap.disconnect
167+
end

0 commit comments

Comments
 (0)