Skip to content

Instantly share code, notes, and snippets.

@arika
Created March 27, 2012 10:59
Show Gist options
  • Select an option

  • Save arika/2214904 to your computer and use it in GitHub Desktop.

Select an option

Save arika/2214904 to your computer and use it in GitHub Desktop.
UNIX USER 2002年2月号のRuby特集の第3章「実践プログラミング〜メール編」で示したサンプルコード
#!/usr/bin/ruby
require 'net/pop'
def rc_parse(io)
r = []
io.each {|line|
line.strip!
if /^(?:(.*)@)?(\S+)\s+(.*)$/ =~ line
r << if $1
[$1, $2, $3]
else
[ENV['USER'], $2, $3]
end
end
}
r
end
def popstat(server, port, user, pass)
begin
Net::APOP.start(server, port, user, pass) {|pop|
if pop.mails.empty?
puts "#{user}@#{server} has no mail"
else
i = 0
puts "#{user}@#{server} has mails:"
pop.each {|mail|
i += 1
if mail.size < 1024
puts " #{i}: #{mail.size}b (UIDL: #{mail.uidl})"
else
puts " #{i}: #{mail.size/1024}Kb (UIDL: #{mail.uidl})"
end
}
end
}
rescue SocketError
$stderr.puts "could not find host: #{server}"
rescue Errno::ECONNREFUSED
$stderr.puts "could not connect to #{server}"
rescue Net::ProtoAuthError
$stderr.puts "could not login to #{server} by #{user}"
end
end
def main(rc)
begin
io = open(File.expand_path(rc))
rc_parse(io).each {|user, server, pass|
popstat(server, 110, user, pass)
}
rescue Errno::ENOENT
$stderr.puts "#{rc} does not exist."
exit 1
end
end
rc = '~/.poplsrc'
main(rc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment