2003-08-05 09:22:20 -04:00
|
|
|
# = net/pop.rb
|
|
|
|
#
|
2007-03-16 09:32:41 -04:00
|
|
|
# Copyright (c) 1999-2007 Yukihiro Matsumoto.
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2007-03-16 09:32:41 -04:00
|
|
|
# Copyright (c) 1999-2007 Minero Aoki.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# Written & maintained by Minero Aoki <aamine@loveruby.net>.
|
|
|
|
#
|
|
|
|
# Documented by William Webber and Minero Aoki.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-05 09:22:20 -04:00
|
|
|
# This program is free software. You can re-distribute and/or
|
|
|
|
# modify this program under the same terms as Ruby itself,
|
2004-03-06 12:08:21 -05:00
|
|
|
# Ruby Distribute License.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-03-06 12:08:21 -05:00
|
|
|
# NOTE: You can find Japanese version of this document at:
|
2008-04-15 04:12:30 -04:00
|
|
|
# http://www.ruby-lang.org/ja/man/html/net_pop.html
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# $Id$
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# See Net::POP3 for documentation.
|
2004-04-13 03:48:14 -04:00
|
|
|
#
|
2003-08-05 12:12:05 -04:00
|
|
|
|
2000-06-12 12:42:46 -04:00
|
|
|
require 'net/protocol'
|
2001-12-01 09:07:01 -05:00
|
|
|
require 'digest/md5'
|
2004-03-06 12:08:21 -05:00
|
|
|
require 'timeout'
|
2000-06-12 12:42:46 -04:00
|
|
|
|
2004-04-13 03:48:14 -04:00
|
|
|
begin
|
2009-02-17 11:09:44 -05:00
|
|
|
require "openssl"
|
2004-04-13 03:48:14 -04:00
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
2000-06-12 12:42:46 -04:00
|
|
|
module Net
|
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
# Non-authentication POP3 protocol error
|
|
|
|
# (reply code "-ERR", except authentication).
|
2003-05-02 10:35:01 -04:00
|
|
|
class POPError < ProtocolError; end
|
2003-08-05 09:22:20 -04:00
|
|
|
|
|
|
|
# POP3 authentication error.
|
2003-05-02 10:35:01 -04:00
|
|
|
class POPAuthenticationError < ProtoAuthError; end
|
2003-08-05 09:22:20 -04:00
|
|
|
|
|
|
|
# Unexpected response from the server.
|
2003-07-01 22:34:39 -04:00
|
|
|
class POPBadResponse < POPError; end
|
2003-05-02 10:35:01 -04:00
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# == What is This Library?
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
|
|
|
# This library provides functionality for retrieving
|
2004-05-09 10:42:43 -04:00
|
|
|
# email via POP3, the Post Office Protocol version 3. For details
|
|
|
|
# of POP3, see [RFC1939] (http://www.ietf.org/rfc/rfc1939.txt).
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# == Examples
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
|
|
|
# === Retrieving Messages
|
|
|
|
#
|
|
|
|
# This example retrieves messages from the server and deletes them
|
2004-05-09 10:42:43 -04:00
|
|
|
# on the server.
|
2003-08-05 09:22:20 -04:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# Messages are written to files named 'inbox/1', 'inbox/2', ....
|
|
|
|
# Replace 'pop.example.com' with your POP3 server address, and
|
|
|
|
# 'YourAccount' and 'YourPassword' with the appropriate account
|
|
|
|
# details.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# require 'net/pop'
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# pop = Net::POP3.new('pop.example.com')
|
|
|
|
# pop.start('YourAccount', 'YourPassword') # (1)
|
|
|
|
# if pop.mails.empty?
|
|
|
|
# puts 'No mail.'
|
|
|
|
# else
|
|
|
|
# i = 0
|
|
|
|
# pop.each_mail do |m| # or "pop.mails.each ..." # (2)
|
|
|
|
# File.open("inbox/#{i}", 'w') do |f|
|
|
|
|
# f.write m.pop
|
|
|
|
# end
|
|
|
|
# m.delete
|
|
|
|
# i += 1
|
|
|
|
# end
|
|
|
|
# puts "#{pop.mails.size} mails popped."
|
|
|
|
# end
|
|
|
|
# pop.finish # (3)
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# 1. Call Net::POP3#start and start POP session.
|
|
|
|
# 2. Access messages by using POP3#each_mail and/or POP3#mails.
|
|
|
|
# 3. Close POP session by calling POP3#finish or use the block form of #start.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# === Shortened Code
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# The example above is very verbose. You can shorten the code by using
|
|
|
|
# some utility methods. First, the block form of Net::POP3.start can
|
|
|
|
# be used instead of POP3.new, POP3#start and POP3#finish.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# require 'net/pop'
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# Net::POP3.start('pop.example.com', 110,
|
|
|
|
# 'YourAccount', 'YourPassword') do |pop|
|
|
|
|
# if pop.mails.empty?
|
|
|
|
# puts 'No mail.'
|
|
|
|
# else
|
|
|
|
# i = 0
|
|
|
|
# pop.each_mail do |m| # or "pop.mails.each ..."
|
|
|
|
# File.open("inbox/#{i}", 'w') do |f|
|
|
|
|
# f.write m.pop
|
|
|
|
# end
|
|
|
|
# m.delete
|
|
|
|
# i += 1
|
|
|
|
# end
|
|
|
|
# puts "#{pop.mails.size} mails popped."
|
|
|
|
# end
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# POP3#delete_all is an alternative for #each_mail and #delete.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# require 'net/pop'
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# Net::POP3.start('pop.example.com', 110,
|
|
|
|
# 'YourAccount', 'YourPassword') do |pop|
|
|
|
|
# if pop.mails.empty?
|
|
|
|
# puts 'No mail.'
|
|
|
|
# else
|
|
|
|
# i = 1
|
|
|
|
# pop.delete_all do |m|
|
|
|
|
# File.open("inbox/#{i}", 'w') do |f|
|
|
|
|
# f.write m.pop
|
|
|
|
# end
|
|
|
|
# i += 1
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# And here is an even shorter example.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# require 'net/pop'
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# i = 0
|
|
|
|
# Net::POP3.delete_all('pop.example.com', 110,
|
|
|
|
# 'YourAccount', 'YourPassword') do |m|
|
|
|
|
# File.open("inbox/#{i}", 'w') do |f|
|
|
|
|
# f.write m.pop
|
|
|
|
# end
|
|
|
|
# i += 1
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# === Memory Space Issues
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# All the examples above get each message as one big string.
|
|
|
|
# This example avoids this.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# require 'net/pop'
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# i = 1
|
|
|
|
# Net::POP3.delete_all('pop.example.com', 110,
|
|
|
|
# 'YourAccount', 'YourPassword') do |m|
|
|
|
|
# File.open("inbox/#{i}", 'w') do |f|
|
|
|
|
# m.pop do |chunk| # get a message little by little.
|
|
|
|
# f.write chunk
|
|
|
|
# end
|
|
|
|
# i += 1
|
|
|
|
# end
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# === Using APOP
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# The net/pop library supports APOP authentication.
|
|
|
|
# To use APOP, use the Net::APOP class instead of the Net::POP3 class.
|
|
|
|
# You can use the utility method, Net::POP3.APOP(). For example:
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# require 'net/pop'
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# # Use APOP authentication if $isapop == true
|
|
|
|
# pop = Net::POP3.APOP($is_apop).new('apop.example.com', 110)
|
|
|
|
# pop.start(YourAccount', 'YourPassword') do |pop|
|
|
|
|
# # Rest of the code is the same.
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# === Fetch Only Selected Mail Using 'UIDL' POP Command
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# If your POP server provides UIDL functionality,
|
|
|
|
# you can grab only selected mails from the POP server.
|
|
|
|
# e.g.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# def need_pop?( id )
|
|
|
|
# # determine if we need pop this mail...
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# Net::POP3.start('pop.example.com', 110,
|
|
|
|
# 'Your account', 'Your password') do |pop|
|
|
|
|
# pop.mails.select { |m| need_pop?(m.unique_id) }.each do |m|
|
|
|
|
# do_something(m.pop)
|
|
|
|
# end
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# The POPMail#unique_id() method returns the unique-id of the message as a
|
|
|
|
# String. Normally the unique-id is a hash of the message.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-05-02 11:19:20 -04:00
|
|
|
class POP3 < Protocol
|
2003-05-02 10:35:01 -04:00
|
|
|
|
2011-05-16 18:09:23 -04:00
|
|
|
# svn revision of this library
|
2003-05-02 10:35:01 -04:00
|
|
|
Revision = %q$Revision$.split[1]
|
2003-03-09 11:53:30 -05:00
|
|
|
|
2003-05-02 10:35:01 -04:00
|
|
|
#
|
|
|
|
# Class Parameters
|
|
|
|
#
|
2003-03-09 11:53:30 -05:00
|
|
|
|
2011-05-16 18:09:23 -04:00
|
|
|
# returns the port for POP3
|
2003-05-02 10:35:01 -04:00
|
|
|
def POP3.default_port
|
2004-04-13 03:48:14 -04:00
|
|
|
default_pop3_port()
|
|
|
|
end
|
|
|
|
|
|
|
|
# The default port for POP3 connections, port 110
|
|
|
|
def POP3.default_pop3_port
|
2003-05-02 10:35:01 -04:00
|
|
|
110
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2004-04-13 03:48:14 -04:00
|
|
|
# The default port for POP3S connections, port 995
|
|
|
|
def POP3.default_pop3s_port
|
|
|
|
995
|
|
|
|
end
|
2000-06-12 12:42:46 -04:00
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
def POP3.socket_type #:nodoc: obsolete
|
2003-05-02 10:35:01 -04:00
|
|
|
Net::InternetMessageIO
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Utilities
|
|
|
|
#
|
2000-06-12 12:42:46 -04:00
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Returns the APOP class if +isapop+ is true; otherwise, returns
|
|
|
|
# the POP class. For example:
|
|
|
|
#
|
|
|
|
# # Example 1
|
|
|
|
# pop = Net::POP3::APOP($is_apop).new(addr, port)
|
|
|
|
#
|
|
|
|
# # Example 2
|
2004-05-09 10:42:43 -04:00
|
|
|
# Net::POP3::APOP($is_apop).start(addr, port) do |pop|
|
2003-08-05 09:22:20 -04:00
|
|
|
# ....
|
2004-05-09 10:42:43 -04:00
|
|
|
# end
|
2003-08-05 12:12:05 -04:00
|
|
|
#
|
2004-03-28 00:23:13 -05:00
|
|
|
def POP3.APOP(isapop)
|
2002-11-21 06:50:09 -05:00
|
|
|
isapop ? APOP : POP3
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
2001-12-13 14:15:21 -05:00
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Starts a POP3 session and iterates over each POPMail object,
|
|
|
|
# yielding it to the +block+.
|
|
|
|
# This method is equivalent to:
|
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# Net::POP3.start(address, port, account, password) do |pop|
|
2003-08-05 09:22:20 -04:00
|
|
|
# pop.each_mail do |m|
|
|
|
|
# yield m
|
|
|
|
# end
|
2004-05-09 10:42:43 -04:00
|
|
|
# end
|
2003-08-05 09:22:20 -04:00
|
|
|
#
|
|
|
|
# This method raises a POPAuthenticationError if authentication fails.
|
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# === Example
|
|
|
|
#
|
2003-08-05 09:22:20 -04:00
|
|
|
# Net::POP3.foreach('pop.example.com', 110,
|
|
|
|
# 'YourAccount', 'YourPassword') do |m|
|
|
|
|
# file.write m.pop
|
|
|
|
# m.delete if $DELETE
|
|
|
|
# end
|
2003-08-05 12:12:05 -04:00
|
|
|
#
|
2004-03-28 00:23:13 -05:00
|
|
|
def POP3.foreach(address, port = nil,
|
|
|
|
account = nil, password = nil,
|
|
|
|
isapop = false, &block) # :yields: message
|
2003-05-02 10:35:01 -04:00
|
|
|
start(address, port, account, password, isapop) {|pop|
|
2003-07-01 22:34:39 -04:00
|
|
|
pop.each_mail(&block)
|
2002-02-19 07:33:52 -05:00
|
|
|
}
|
|
|
|
end
|
2000-12-22 13:40:55 -05:00
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Starts a POP3 session and deletes all messages on the server.
|
|
|
|
# If a block is given, each POPMail object is yielded to it before
|
|
|
|
# being deleted.
|
|
|
|
#
|
|
|
|
# This method raises a POPAuthenticationError if authentication fails.
|
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# === Example
|
|
|
|
#
|
2003-08-05 09:22:20 -04:00
|
|
|
# Net::POP3.delete_all('pop.example.com', 110,
|
|
|
|
# 'YourAccount', 'YourPassword') do |m|
|
|
|
|
# file.write m.pop
|
|
|
|
# end
|
2003-08-05 12:12:05 -04:00
|
|
|
#
|
2004-03-28 00:23:13 -05:00
|
|
|
def POP3.delete_all(address, port = nil,
|
|
|
|
account = nil, password = nil,
|
|
|
|
isapop = false, &block)
|
2003-05-02 10:35:01 -04:00
|
|
|
start(address, port, account, password, isapop) {|pop|
|
2003-07-01 22:34:39 -04:00
|
|
|
pop.delete_all(&block)
|
2002-02-19 07:33:52 -05:00
|
|
|
}
|
|
|
|
end
|
2001-06-26 19:49:21 -04:00
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Opens a POP3 session, attempts authentication, and quits.
|
|
|
|
#
|
|
|
|
# This method raises POPAuthenticationError if authentication fails.
|
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# === Example: normal POP3
|
|
|
|
#
|
2003-08-05 09:22:20 -04:00
|
|
|
# Net::POP3.auth_only('pop.example.com', 110,
|
|
|
|
# 'YourAccount', 'YourPassword')
|
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# === Example: APOP
|
|
|
|
#
|
2003-08-05 09:22:20 -04:00
|
|
|
# Net::POP3.auth_only('pop.example.com', 110,
|
|
|
|
# 'YourAccount', 'YourPassword', true)
|
2003-08-05 12:12:05 -04:00
|
|
|
#
|
2004-03-28 00:23:13 -05:00
|
|
|
def POP3.auth_only(address, port = nil,
|
|
|
|
account = nil, password = nil,
|
|
|
|
isapop = false)
|
2003-05-02 10:35:01 -04:00
|
|
|
new(address, port, isapop).auth_only account, password
|
2000-12-22 13:40:55 -05:00
|
|
|
end
|
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Starts a pop3 session, attempts authentication, and quits.
|
|
|
|
# This method must not be called while POP3 session is opened.
|
|
|
|
# This method raises POPAuthenticationError if authentication fails.
|
2004-03-28 00:23:13 -05:00
|
|
|
def auth_only(account, password)
|
2004-03-29 02:54:38 -05:00
|
|
|
raise IOError, 'opening previously opened POP session' if started?
|
2002-11-21 06:50:09 -05:00
|
|
|
start(account, password) {
|
2003-07-01 22:34:39 -04:00
|
|
|
;
|
2001-12-30 14:18:45 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2004-04-21 05:02:04 -04:00
|
|
|
#
|
|
|
|
# SSL
|
|
|
|
#
|
|
|
|
|
2007-12-22 01:03:20 -05:00
|
|
|
@ssl_params = nil
|
2004-04-21 05:02:04 -04:00
|
|
|
|
2011-08-10 08:28:19 -04:00
|
|
|
# :call-seq:
|
2007-12-22 01:03:20 -05:00
|
|
|
# Net::POP.enable_ssl(params = {})
|
|
|
|
#
|
2004-04-21 05:02:04 -04:00
|
|
|
# Enable SSL for all new instances.
|
2007-12-22 03:31:53 -05:00
|
|
|
# +params+ is passed to OpenSSL::SSLContext#set_params.
|
2007-12-22 01:03:20 -05:00
|
|
|
def POP3.enable_ssl(*args)
|
|
|
|
@ssl_params = create_ssl_params(*args)
|
|
|
|
end
|
|
|
|
|
2011-05-16 18:09:23 -04:00
|
|
|
# Constructs proper parameters from arguments
|
2007-12-22 01:03:20 -05:00
|
|
|
def POP3.create_ssl_params(verify_or_params = {}, certs = nil)
|
|
|
|
begin
|
|
|
|
params = verify_or_params.to_hash
|
|
|
|
rescue NoMethodError
|
|
|
|
params = {}
|
|
|
|
params[:verify_mode] = verify_or_params
|
|
|
|
if certs
|
|
|
|
if File.file?(certs)
|
|
|
|
params[:ca_file] = certs
|
|
|
|
elsif File.directory?(certs)
|
|
|
|
params[:ca_path] = certs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return params
|
2004-04-21 05:02:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Disable SSL for all new instances.
|
|
|
|
def POP3.disable_ssl
|
2007-12-22 01:03:20 -05:00
|
|
|
@ssl_params = nil
|
|
|
|
end
|
|
|
|
|
2011-05-16 18:09:23 -04:00
|
|
|
# returns the SSL Parameters
|
|
|
|
#
|
|
|
|
# see also POP3.enable_ssl
|
2007-12-22 01:03:20 -05:00
|
|
|
def POP3.ssl_params
|
|
|
|
return @ssl_params
|
2004-04-21 05:02:04 -04:00
|
|
|
end
|
|
|
|
|
2011-05-16 18:09:23 -04:00
|
|
|
# returns +true+ if POP3.ssl_params is set
|
2004-04-21 05:02:04 -04:00
|
|
|
def POP3.use_ssl?
|
2007-12-22 01:03:20 -05:00
|
|
|
return !@ssl_params.nil?
|
2004-04-21 05:02:04 -04:00
|
|
|
end
|
|
|
|
|
2011-05-16 18:09:23 -04:00
|
|
|
# returns whether verify_mode is enable from POP3.ssl_params
|
2004-04-21 05:02:04 -04:00
|
|
|
def POP3.verify
|
2007-12-22 01:03:20 -05:00
|
|
|
return @ssl_params[:verify_mode]
|
2004-04-21 05:02:04 -04:00
|
|
|
end
|
|
|
|
|
2012-11-16 01:37:06 -05:00
|
|
|
# returns the :ca_file or :ca_path from POP3.ssl_params
|
2004-04-21 05:02:04 -04:00
|
|
|
def POP3.certs
|
2007-12-22 01:03:20 -05:00
|
|
|
return @ssl_params[:ca_file] || @ssl_params[:ca_path]
|
2004-04-21 05:02:04 -04:00
|
|
|
end
|
|
|
|
|
2001-12-30 14:18:45 -05:00
|
|
|
#
|
2003-05-02 10:35:01 -04:00
|
|
|
# Session management
|
2001-12-30 14:18:45 -05:00
|
|
|
#
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
# Creates a new POP3 object and open the connection. Equivalent to
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
|
|
|
# Net::POP3.new(address, port, isapop).start(account, password)
|
2003-08-05 09:22:20 -04:00
|
|
|
#
|
|
|
|
# If +block+ is provided, yields the newly-opened POP3 object to it,
|
|
|
|
# and automatically closes it at the end of the session.
|
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# === Example
|
2003-08-05 09:22:20 -04:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# Net::POP3.start(addr, port, account, password) do |pop|
|
2003-08-05 09:22:20 -04:00
|
|
|
# pop.each_mail do |m|
|
|
|
|
# file.write m.pop
|
|
|
|
# m.delete
|
|
|
|
# end
|
2004-05-09 10:42:43 -04:00
|
|
|
# end
|
2003-08-05 09:22:20 -04:00
|
|
|
#
|
2004-03-28 00:23:13 -05:00
|
|
|
def POP3.start(address, port = nil,
|
|
|
|
account = nil, password = nil,
|
|
|
|
isapop = false, &block) # :yield: pop
|
2003-05-02 10:35:01 -04:00
|
|
|
new(address, port, isapop).start(account, password, &block)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Creates a new POP3 object.
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
|
|
|
# +address+ is the hostname or ip address of your POP3 server.
|
|
|
|
#
|
2004-04-13 03:48:14 -04:00
|
|
|
# The optional +port+ is the port to connect to.
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2003-08-05 09:22:20 -04:00
|
|
|
# The optional +isapop+ specifies whether this connection is going
|
|
|
|
# to use APOP authentication; it defaults to +false+.
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2003-08-05 09:22:20 -04:00
|
|
|
# This method does *not* open the TCP connection.
|
2004-03-28 00:23:13 -05:00
|
|
|
def initialize(addr, port = nil, isapop = false)
|
2003-05-02 10:35:01 -04:00
|
|
|
@address = addr
|
2007-12-22 01:03:20 -05:00
|
|
|
@ssl_params = POP3.ssl_params
|
|
|
|
@port = port
|
2003-05-02 10:35:01 -04:00
|
|
|
@apop = isapop
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-05-02 10:35:01 -04:00
|
|
|
@command = nil
|
|
|
|
@socket = nil
|
|
|
|
@started = false
|
|
|
|
@open_timeout = 30
|
|
|
|
@read_timeout = 60
|
|
|
|
@debug_output = nil
|
|
|
|
|
2000-10-12 05:54:32 -04:00
|
|
|
@mails = nil
|
2003-05-06 04:48:51 -04:00
|
|
|
@n_mails = nil
|
|
|
|
@n_bytes = nil
|
2000-06-12 12:42:46 -04:00
|
|
|
end
|
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Does this instance use APOP authentication?
|
2003-05-02 10:35:01 -04:00
|
|
|
def apop?
|
|
|
|
@apop
|
|
|
|
end
|
|
|
|
|
2004-04-13 03:48:14 -04:00
|
|
|
# does this instance use SSL?
|
|
|
|
def use_ssl?
|
2007-12-22 01:03:20 -05:00
|
|
|
return !@ssl_params.nil?
|
2004-04-13 03:48:14 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2011-08-10 08:28:19 -04:00
|
|
|
# :call-seq:
|
2007-12-22 01:03:20 -05:00
|
|
|
# Net::POP#enable_ssl(params = {})
|
|
|
|
#
|
2004-04-13 03:48:14 -04:00
|
|
|
# Enables SSL for this instance. Must be called before the connection is
|
|
|
|
# established to have any effect.
|
2007-12-22 01:03:20 -05:00
|
|
|
# +params[:port]+ is port to establish the SSL connection on; Defaults to 995.
|
2007-12-22 03:31:53 -05:00
|
|
|
# +params+ (except :port) is passed to OpenSSL::SSLContext#set_params.
|
2007-12-22 01:03:20 -05:00
|
|
|
def enable_ssl(verify_or_params = {}, certs = nil, port = nil)
|
|
|
|
begin
|
|
|
|
@ssl_params = verify_or_params.to_hash.dup
|
|
|
|
@port = @ssl_params.delete(:port) || @port
|
|
|
|
rescue NoMethodError
|
|
|
|
@ssl_params = POP3.create_ssl_params(verify_or_params, certs)
|
|
|
|
@port = port || @port
|
|
|
|
end
|
2004-04-13 03:48:14 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2011-05-16 18:09:23 -04:00
|
|
|
# Disable SSL for all new instances.
|
2004-04-13 03:48:14 -04:00
|
|
|
def disable_ssl
|
2007-12-22 01:03:20 -05:00
|
|
|
@ssl_params = nil
|
2004-04-13 03:48:14 -04:00
|
|
|
end
|
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Provide human-readable stringification of class state.
|
2003-05-02 10:35:01 -04:00
|
|
|
def inspect
|
|
|
|
"#<#{self.class} #{@address}:#{@port} open=#{@started}>"
|
|
|
|
end
|
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# *WARNING*: This method causes a serious security hole.
|
|
|
|
# Use this method only for debugging.
|
|
|
|
#
|
|
|
|
# Set an output stream for debugging.
|
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# === Example
|
|
|
|
#
|
2003-08-05 09:22:20 -04:00
|
|
|
# pop = Net::POP.new(addr, port)
|
|
|
|
# pop.set_debug_output $stderr
|
2004-05-09 10:42:43 -04:00
|
|
|
# pop.start(account, passwd) do |pop|
|
2003-08-05 09:22:20 -04:00
|
|
|
# ....
|
2004-05-09 10:42:43 -04:00
|
|
|
# end
|
2003-08-05 12:12:05 -04:00
|
|
|
#
|
2004-03-28 00:23:13 -05:00
|
|
|
def set_debug_output(arg)
|
2003-05-02 10:35:01 -04:00
|
|
|
@debug_output = arg
|
|
|
|
end
|
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# The address to connect to.
|
2003-05-02 10:35:01 -04:00
|
|
|
attr_reader :address
|
2003-08-05 09:22:20 -04:00
|
|
|
|
|
|
|
# The port number to connect to.
|
2007-12-22 01:03:20 -05:00
|
|
|
def port
|
|
|
|
return @port || (use_ssl? ? POP3.default_pop3s_port : POP3.default_pop3_port)
|
|
|
|
end
|
2003-05-02 10:35:01 -04:00
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Seconds to wait until a connection is opened.
|
|
|
|
# If the POP3 object cannot open a connection within this time,
|
2013-03-30 21:47:47 -04:00
|
|
|
# it raises a Net::OpenTimeout exception. The default value is 30 seconds.
|
2003-05-02 10:35:01 -04:00
|
|
|
attr_accessor :open_timeout
|
2003-08-05 09:22:20 -04:00
|
|
|
|
|
|
|
# Seconds to wait until reading one block (by one read(1) call).
|
|
|
|
# If the POP3 object cannot complete a read() within this time,
|
2013-03-30 21:47:47 -04:00
|
|
|
# it raises a Net::ReadTimeout exception. The default value is 60 seconds.
|
2003-05-02 10:35:01 -04:00
|
|
|
attr_reader :read_timeout
|
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Set the read timeout.
|
2004-03-28 00:23:13 -05:00
|
|
|
def read_timeout=(sec)
|
2003-05-02 10:35:01 -04:00
|
|
|
@command.socket.read_timeout = sec if @command
|
|
|
|
@read_timeout = sec
|
|
|
|
end
|
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# +true+ if the POP3 session has started.
|
2003-05-02 10:35:01 -04:00
|
|
|
def started?
|
|
|
|
@started
|
|
|
|
end
|
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
alias active? started? #:nodoc: obsolete
|
2003-05-02 10:35:01 -04:00
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Starts a POP3 session.
|
|
|
|
#
|
|
|
|
# When called with block, gives a POP3 object to the block and
|
|
|
|
# closes the session after block call finishes.
|
|
|
|
#
|
|
|
|
# This method raises a POPAuthenticationError if authentication fails.
|
2004-03-28 00:23:13 -05:00
|
|
|
def start(account, password) # :yield: pop
|
2003-05-04 02:04:03 -04:00
|
|
|
raise IOError, 'POP session already started' if @started
|
2003-05-02 10:35:01 -04:00
|
|
|
if block_given?
|
|
|
|
begin
|
|
|
|
do_start account, password
|
|
|
|
return yield(self)
|
|
|
|
ensure
|
2003-09-29 14:12:20 -04:00
|
|
|
do_finish
|
2003-05-02 10:35:01 -04:00
|
|
|
end
|
|
|
|
else
|
2003-07-01 12:34:58 -04:00
|
|
|
do_start account, password
|
2003-05-02 10:35:01 -04:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
end
|
2001-12-30 14:18:45 -05:00
|
|
|
|
2011-05-16 18:09:23 -04:00
|
|
|
# internal method for Net::POP3.start
|
2011-05-24 20:30:04 -04:00
|
|
|
def do_start(account, password) # :nodoc:
|
2012-02-28 00:15:54 -05:00
|
|
|
s = Timeout.timeout(@open_timeout, Net::OpenTimeout) do
|
|
|
|
TCPSocket.open(@address, port)
|
|
|
|
end
|
2004-04-21 05:02:04 -04:00
|
|
|
if use_ssl?
|
|
|
|
raise 'openssl library not installed' unless defined?(OpenSSL)
|
2007-12-22 03:31:53 -05:00
|
|
|
context = OpenSSL::SSL::SSLContext.new
|
|
|
|
context.set_params(@ssl_params)
|
2004-04-21 05:02:04 -04:00
|
|
|
s = OpenSSL::SSL::SSLSocket.new(s, context)
|
2004-04-13 03:48:14 -04:00
|
|
|
s.sync_close = true
|
|
|
|
s.connect
|
2007-10-25 15:22:27 -04:00
|
|
|
if context.verify_mode != OpenSSL::SSL::VERIFY_NONE
|
2007-10-08 07:14:41 -04:00
|
|
|
s.post_connection_check(@address)
|
|
|
|
end
|
2004-04-13 03:48:14 -04:00
|
|
|
end
|
|
|
|
@socket = InternetMessageIO.new(s)
|
2004-03-06 12:08:21 -05:00
|
|
|
logging "POP session started: #{@address}:#{@port} (#{@apop ? 'APOP' : 'POP'})"
|
|
|
|
@socket.read_timeout = @read_timeout
|
|
|
|
@socket.debug_output = @debug_output
|
2003-05-02 10:35:01 -04:00
|
|
|
on_connect
|
|
|
|
@command = POP3Command.new(@socket)
|
|
|
|
if apop?
|
|
|
|
@command.apop account, password
|
|
|
|
else
|
|
|
|
@command.auth account, password
|
|
|
|
end
|
|
|
|
@started = true
|
2003-09-29 14:12:20 -04:00
|
|
|
ensure
|
2004-04-21 05:02:04 -04:00
|
|
|
# Authentication failed, clean up connection.
|
|
|
|
unless @started
|
|
|
|
s.close if s and not s.closed?
|
|
|
|
@socket = nil
|
|
|
|
@command = nil
|
|
|
|
end
|
2001-12-30 14:18:45 -05:00
|
|
|
end
|
2003-05-02 10:35:01 -04:00
|
|
|
private :do_start
|
2001-12-30 14:18:45 -05:00
|
|
|
|
2011-05-16 18:09:23 -04:00
|
|
|
# Does nothing
|
2011-05-24 20:30:04 -04:00
|
|
|
def on_connect # :nodoc:
|
2002-11-21 06:50:09 -05:00
|
|
|
end
|
2003-05-02 10:35:01 -04:00
|
|
|
private :on_connect
|
2002-11-21 06:50:09 -05:00
|
|
|
|
2003-09-29 10:39:54 -04:00
|
|
|
# Finishes a POP3 session and closes TCP connection.
|
2003-05-02 10:35:01 -04:00
|
|
|
def finish
|
2004-03-29 02:54:38 -05:00
|
|
|
raise IOError, 'POP session not yet started' unless started?
|
2003-09-29 14:12:20 -04:00
|
|
|
do_finish
|
|
|
|
end
|
|
|
|
|
2011-05-16 18:09:23 -04:00
|
|
|
# nil's out the:
|
|
|
|
# - mails
|
|
|
|
# - number counter for mails
|
|
|
|
# - number counter for bytes
|
|
|
|
# - quits the current command, if any
|
2011-05-24 20:30:04 -04:00
|
|
|
def do_finish # :nodoc:
|
2001-12-30 14:18:45 -05:00
|
|
|
@mails = nil
|
2008-04-03 16:59:51 -04:00
|
|
|
@n_mails = nil
|
|
|
|
@n_bytes = nil
|
2003-05-02 10:35:01 -04:00
|
|
|
@command.quit if @command
|
2003-09-29 14:12:20 -04:00
|
|
|
ensure
|
|
|
|
@started = false
|
2003-05-02 10:35:01 -04:00
|
|
|
@command = nil
|
|
|
|
@socket.close if @socket and not @socket.closed?
|
|
|
|
@socket = nil
|
2001-06-26 19:49:21 -04:00
|
|
|
end
|
2003-09-29 14:12:20 -04:00
|
|
|
private :do_finish
|
2001-06-26 19:49:21 -04:00
|
|
|
|
2011-05-16 18:09:23 -04:00
|
|
|
# Returns the current command.
|
|
|
|
#
|
|
|
|
# Raises IOError if there is no active socket
|
2011-05-24 20:30:04 -04:00
|
|
|
def command # :nodoc:
|
2003-05-02 10:35:01 -04:00
|
|
|
raise IOError, 'POP session not opened yet' \
|
|
|
|
if not @socket or @socket.closed?
|
|
|
|
@command
|
|
|
|
end
|
|
|
|
private :command
|
|
|
|
|
2001-12-30 14:18:45 -05:00
|
|
|
#
|
2003-05-02 10:35:01 -04:00
|
|
|
# POP protocol wrapper
|
2001-12-30 14:18:45 -05:00
|
|
|
#
|
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Returns the number of messages on the POP server.
|
2003-05-06 04:48:51 -04:00
|
|
|
def n_mails
|
|
|
|
return @n_mails if @n_mails
|
|
|
|
@n_mails, @n_bytes = command().stat
|
|
|
|
@n_mails
|
2003-03-09 11:53:30 -05:00
|
|
|
end
|
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Returns the total size in bytes of all the messages on the POP server.
|
2003-05-06 04:48:51 -04:00
|
|
|
def n_bytes
|
|
|
|
return @n_bytes if @n_bytes
|
|
|
|
@n_mails, @n_bytes = command().stat
|
|
|
|
@n_bytes
|
2003-03-09 11:53:30 -05:00
|
|
|
end
|
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Returns an array of Net::POPMail objects, representing all the
|
|
|
|
# messages on the server. This array is renewed when the session
|
|
|
|
# restarts; otherwise, it is fetched from the server the first time
|
|
|
|
# this method is called (directly or indirectly) and cached.
|
|
|
|
#
|
|
|
|
# This method raises a POPError if an error occurs.
|
2001-12-30 14:18:45 -05:00
|
|
|
def mails
|
2003-05-02 10:35:01 -04:00
|
|
|
return @mails.dup if @mails
|
2003-05-06 04:48:51 -04:00
|
|
|
if n_mails() == 0
|
2003-03-09 11:53:30 -05:00
|
|
|
# some popd raises error for LIST on the empty mailbox.
|
|
|
|
@mails = []
|
2003-05-02 10:35:01 -04:00
|
|
|
return []
|
2003-03-09 11:53:30 -05:00
|
|
|
end
|
2001-12-30 14:18:45 -05:00
|
|
|
|
2003-05-02 10:35:01 -04:00
|
|
|
@mails = command().list.map {|num, size|
|
2003-07-01 22:34:39 -04:00
|
|
|
POPMail.new(num, size, self, command())
|
2003-05-02 10:35:01 -04:00
|
|
|
}
|
|
|
|
@mails.dup
|
2001-12-30 14:18:45 -05:00
|
|
|
end
|
2000-06-12 12:42:46 -04:00
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Yields each message to the passed-in block in turn.
|
|
|
|
# Equivalent to:
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-08-05 09:22:20 -04:00
|
|
|
# pop3.mails.each do |popmail|
|
|
|
|
# ....
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# This method raises a POPError if an error occurs.
|
2004-03-28 00:23:13 -05:00
|
|
|
def each_mail(&block) # :yield: message
|
2002-11-21 06:50:09 -05:00
|
|
|
mails().each(&block)
|
2000-12-22 13:40:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
alias each each_mail
|
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Deletes all messages on the server.
|
|
|
|
#
|
|
|
|
# If called with a block, yields each message in turn before deleting it.
|
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# === Example
|
|
|
|
#
|
2003-08-05 09:22:20 -04:00
|
|
|
# n = 1
|
|
|
|
# pop.delete_all do |m|
|
2004-05-09 10:42:43 -04:00
|
|
|
# File.open("inbox/#{n}") do |f|
|
2003-08-05 09:22:20 -04:00
|
|
|
# f.write m.pop
|
2004-05-09 10:42:43 -04:00
|
|
|
# end
|
2003-08-05 09:22:20 -04:00
|
|
|
# n += 1
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# This method raises a POPError if an error occurs.
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
|
|
|
def delete_all # :yield: message
|
2001-12-30 14:18:45 -05:00
|
|
|
mails().each do |m|
|
2000-12-22 13:40:55 -05:00
|
|
|
yield m if block_given?
|
|
|
|
m.delete unless m.deleted?
|
|
|
|
end
|
2000-10-12 05:54:32 -04:00
|
|
|
end
|
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Resets the session. This clears all "deleted" marks from messages.
|
|
|
|
#
|
|
|
|
# This method raises a POPError if an error occurs.
|
2000-10-12 05:54:32 -04:00
|
|
|
def reset
|
2001-12-30 14:18:45 -05:00
|
|
|
command().rset
|
|
|
|
mails().each do |m|
|
2003-03-09 11:53:30 -05:00
|
|
|
m.instance_eval {
|
2003-07-01 22:34:39 -04:00
|
|
|
@deleted = false
|
2003-03-09 11:53:30 -05:00
|
|
|
}
|
2000-10-12 05:54:32 -04:00
|
|
|
end
|
2000-06-12 12:42:46 -04:00
|
|
|
end
|
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
def set_all_uids #:nodoc: internal use only (called from POPMail#uidl)
|
2008-06-19 07:42:05 -04:00
|
|
|
uidl = command().uidl
|
|
|
|
@mails.each {|m| m.uid = uidl[m.number] }
|
2000-10-12 05:54:32 -04:00
|
|
|
end
|
|
|
|
|
2013-12-12 21:38:55 -05:00
|
|
|
# debugging output for +msg+
|
2004-03-06 12:08:21 -05:00
|
|
|
def logging(msg)
|
2004-05-09 12:19:35 -04:00
|
|
|
@debug_output << msg + "\n" if @debug_output
|
2004-03-06 12:08:21 -05:00
|
|
|
end
|
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
end # class POP3
|
2000-06-12 12:42:46 -04:00
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
# class aliases
|
2013-02-01 23:32:25 -05:00
|
|
|
POP = POP3 # :nodoc:
|
|
|
|
POPSession = POP3 # :nodoc:
|
|
|
|
POP3Session = POP3 # :nodoc:
|
2000-06-12 12:42:46 -04:00
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
#
|
2003-08-05 09:22:20 -04:00
|
|
|
# This class is equivalent to POP3, except that it uses APOP authentication.
|
2003-08-05 12:12:05 -04:00
|
|
|
#
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 02:04:40 -04:00
|
|
|
class APOP < POP3
|
2003-08-05 09:22:20 -04:00
|
|
|
# Always returns true.
|
2003-05-02 10:35:01 -04:00
|
|
|
def apop?
|
|
|
|
true
|
2002-02-19 07:33:52 -05:00
|
|
|
end
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 02:04:40 -04:00
|
|
|
end
|
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
# class aliases
|
2002-02-19 07:33:52 -05:00
|
|
|
APOPSession = APOP
|
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
#
|
2003-08-05 09:22:20 -04:00
|
|
|
# This class represents a message which exists on the POP server.
|
|
|
|
# Instances of this class are created by the POP3 class; they should
|
|
|
|
# not be directly created by the user.
|
2003-08-05 12:12:05 -04:00
|
|
|
#
|
1999-12-17 10:00:13 -05:00
|
|
|
class POPMail
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2004-03-28 00:23:13 -05:00
|
|
|
def initialize(num, len, pop, cmd) #:nodoc:
|
2003-05-02 10:35:01 -04:00
|
|
|
@number = num
|
2003-07-01 22:34:39 -04:00
|
|
|
@length = len
|
2003-05-02 10:35:01 -04:00
|
|
|
@pop = pop
|
1999-12-29 06:14:04 -05:00
|
|
|
@command = cmd
|
1999-12-17 10:00:13 -05:00
|
|
|
@deleted = false
|
2003-05-02 10:35:01 -04:00
|
|
|
@uid = nil
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# The sequence number of the message on the server.
|
2003-05-02 10:35:01 -04:00
|
|
|
attr_reader :number
|
2003-08-05 09:22:20 -04:00
|
|
|
|
|
|
|
# The length of the message in octets.
|
2003-07-01 22:34:39 -04:00
|
|
|
attr_reader :length
|
|
|
|
alias size length
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Provide human-readable stringification of class state.
|
2000-06-12 12:42:46 -04:00
|
|
|
def inspect
|
2003-05-02 10:35:01 -04:00
|
|
|
"#<#{self.class} #{@number}#{@deleted ? ' deleted' : ''}>"
|
2000-06-12 12:42:46 -04:00
|
|
|
end
|
|
|
|
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2003-08-05 09:22:20 -04:00
|
|
|
# This method fetches the message. If called with a block, the
|
|
|
|
# message is yielded to the block one chunk at a time. If called
|
2009-03-05 22:56:38 -05:00
|
|
|
# without a block, the message is returned as a String. The optional
|
2003-08-05 09:22:20 -04:00
|
|
|
# +dest+ argument will be prepended to the returned String; this
|
|
|
|
# argument is essentially obsolete.
|
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# === Example without block
|
2004-03-28 00:23:13 -05:00
|
|
|
#
|
2003-08-05 09:22:20 -04:00
|
|
|
# POP3.start('pop.example.com', 110,
|
2004-05-09 10:42:43 -04:00
|
|
|
# 'YourAccount, 'YourPassword') do |pop|
|
2003-08-05 09:22:20 -04:00
|
|
|
# n = 1
|
|
|
|
# pop.mails.each do |popmail|
|
2004-05-09 10:42:43 -04:00
|
|
|
# File.open("inbox/#{n}", 'w') do |f|
|
2009-03-05 22:56:38 -05:00
|
|
|
# f.write popmail.pop
|
2004-05-09 10:42:43 -04:00
|
|
|
# end
|
2003-08-05 09:22:20 -04:00
|
|
|
# popmail.delete
|
|
|
|
# n += 1
|
|
|
|
# end
|
2004-05-09 10:42:43 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# === Example with block
|
2003-08-05 09:22:20 -04:00
|
|
|
#
|
|
|
|
# POP3.start('pop.example.com', 110,
|
2004-05-09 10:42:43 -04:00
|
|
|
# 'YourAccount, 'YourPassword') do |pop|
|
2003-08-05 09:22:20 -04:00
|
|
|
# n = 1
|
|
|
|
# pop.mails.each do |popmail|
|
2004-05-09 10:42:43 -04:00
|
|
|
# File.open("inbox/#{n}", 'w') do |f|
|
2003-08-05 09:22:20 -04:00
|
|
|
# popmail.pop do |chunk| ####
|
|
|
|
# f.write chunk
|
|
|
|
# end
|
2004-05-09 10:42:43 -04:00
|
|
|
# end
|
2003-08-05 09:22:20 -04:00
|
|
|
# n += 1
|
|
|
|
# end
|
2004-05-09 10:42:43 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# This method raises a POPError if an error occurs.
|
2003-08-05 09:22:20 -04:00
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
def pop( dest = '', &block ) # :yield: message_chunk
|
2003-07-01 22:34:39 -04:00
|
|
|
if block_given?
|
|
|
|
@command.retr(@number, &block)
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
@command.retr(@number) do |chunk|
|
|
|
|
dest << chunk
|
|
|
|
end
|
|
|
|
dest
|
|
|
|
end
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
2001-12-07 05:04:25 -05:00
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
alias all pop #:nodoc: obsolete
|
|
|
|
alias mail pop #:nodoc: obsolete
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
# Fetches the message header and +lines+ lines of body.
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2003-08-05 09:22:20 -04:00
|
|
|
# The optional +dest+ argument is obsolete.
|
|
|
|
#
|
|
|
|
# This method raises a POPError if an error occurs.
|
2004-03-28 00:23:13 -05:00
|
|
|
def top(lines, dest = '')
|
2003-07-01 22:34:39 -04:00
|
|
|
@command.top(@number, lines) do |chunk|
|
|
|
|
dest << chunk
|
|
|
|
end
|
|
|
|
dest
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
1999-10-18 05:03:16 -04:00
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
# Fetches the message header.
|
2004-05-09 10:42:43 -04:00
|
|
|
#
|
2003-08-05 09:22:20 -04:00
|
|
|
# The optional +dest+ argument is obsolete.
|
|
|
|
#
|
|
|
|
# This method raises a POPError if an error occurs.
|
2004-03-28 00:23:13 -05:00
|
|
|
def header(dest = '')
|
2003-05-02 10:35:01 -04:00
|
|
|
top(0, dest)
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Marks a message for deletion on the server. Deletion does not
|
|
|
|
# actually occur until the end of the session; deletion may be
|
|
|
|
# cancelled for _all_ marked messages by calling POP3#reset().
|
|
|
|
#
|
|
|
|
# This method raises a POPError if an error occurs.
|
|
|
|
#
|
2004-05-09 10:42:43 -04:00
|
|
|
# === Example
|
|
|
|
#
|
2003-08-05 09:22:20 -04:00
|
|
|
# POP3.start('pop.example.com', 110,
|
2004-05-09 10:42:43 -04:00
|
|
|
# 'YourAccount, 'YourPassword') do |pop|
|
2003-08-05 09:22:20 -04:00
|
|
|
# n = 1
|
|
|
|
# pop.mails.each do |popmail|
|
2004-05-09 10:42:43 -04:00
|
|
|
# File.open("inbox/#{n}", 'w') do |f|
|
2003-08-05 09:22:20 -04:00
|
|
|
# f.write popmail.pop
|
2004-05-09 10:42:43 -04:00
|
|
|
# end
|
2003-08-05 09:22:20 -04:00
|
|
|
# popmail.delete ####
|
|
|
|
# n += 1
|
|
|
|
# end
|
2004-05-09 10:42:43 -04:00
|
|
|
# end
|
2003-08-05 12:12:05 -04:00
|
|
|
#
|
1999-12-17 10:00:13 -05:00
|
|
|
def delete
|
2003-05-02 10:35:01 -04:00
|
|
|
@command.dele @number
|
1999-12-17 10:00:13 -05:00
|
|
|
@deleted = true
|
|
|
|
end
|
2000-10-12 05:54:32 -04:00
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
alias delete! delete #:nodoc: obsolete
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# True if the mail has been deleted.
|
1999-12-17 10:00:13 -05:00
|
|
|
def deleted?
|
|
|
|
@deleted
|
|
|
|
end
|
|
|
|
|
2003-08-05 09:22:20 -04:00
|
|
|
# Returns the unique-id of the message.
|
|
|
|
# Normally the unique-id is a hash string of the message.
|
|
|
|
#
|
|
|
|
# This method raises a POPError if an error occurs.
|
2003-05-02 10:35:01 -04:00
|
|
|
def unique_id
|
|
|
|
return @uid if @uid
|
|
|
|
@pop.set_all_uids
|
|
|
|
@uid
|
|
|
|
end
|
|
|
|
|
|
|
|
alias uidl unique_id
|
|
|
|
|
2004-03-28 00:23:13 -05:00
|
|
|
def uid=(uid) #:nodoc: internal use only
|
2003-05-02 10:35:01 -04:00
|
|
|
@uid = uid
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
end # class POPMail
|
1999-10-13 03:29:15 -04:00
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2003-08-05 12:12:05 -04:00
|
|
|
class POP3Command #:nodoc: internal use only
|
1999-12-17 10:00:13 -05:00
|
|
|
|
2004-03-28 00:23:13 -05:00
|
|
|
def initialize(sock)
|
2003-05-02 10:35:01 -04:00
|
|
|
@socket = sock
|
2013-05-19 10:50:47 -04:00
|
|
|
@error_occurred = false
|
2003-05-02 10:35:01 -04:00
|
|
|
res = check_response(critical { recv_response() })
|
2008-10-13 22:22:46 -04:00
|
|
|
@apop_stamp = res.slice(/<[!-~]+@[!-~]+>/)
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
|
|
|
|
2008-08-12 03:09:08 -04:00
|
|
|
attr_reader :socket
|
|
|
|
|
2003-05-02 10:35:01 -04:00
|
|
|
def inspect
|
|
|
|
"#<#{self.class} socket=#{@socket}>"
|
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2004-03-28 00:23:13 -05:00
|
|
|
def auth(account, password)
|
2003-07-01 22:34:39 -04:00
|
|
|
check_response_auth(critical {
|
2004-03-28 00:17:13 -05:00
|
|
|
check_response_auth(get_response('USER %s', account))
|
|
|
|
get_response('PASS %s', password)
|
2003-07-01 22:34:39 -04:00
|
|
|
})
|
2003-05-02 10:35:01 -04:00
|
|
|
end
|
|
|
|
|
2004-03-28 00:23:13 -05:00
|
|
|
def apop(account, password)
|
2003-05-04 02:04:03 -04:00
|
|
|
raise POPAuthenticationError, 'not APOP server; cannot login' \
|
2003-05-02 10:35:01 -04:00
|
|
|
unless @apop_stamp
|
|
|
|
check_response_auth(critical {
|
2003-07-01 22:34:39 -04:00
|
|
|
get_response('APOP %s %s',
|
|
|
|
account,
|
|
|
|
Digest::MD5.hexdigest(@apop_stamp + password))
|
2003-05-02 10:35:01 -04:00
|
|
|
})
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def list
|
2003-05-02 10:35:01 -04:00
|
|
|
critical {
|
2003-07-01 22:34:39 -04:00
|
|
|
getok 'LIST'
|
|
|
|
list = []
|
|
|
|
@socket.each_list_item do |line|
|
|
|
|
m = /\A(\d+)[ \t]+(\d+)/.match(line) or
|
|
|
|
raise POPBadResponse, "bad response: #{line}"
|
|
|
|
list.push [m[1].to_i, m[2].to_i]
|
|
|
|
end
|
|
|
|
return list
|
2003-03-09 11:53:30 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def stat
|
2003-05-02 10:35:01 -04:00
|
|
|
res = check_response(critical { get_response('STAT') })
|
|
|
|
m = /\A\+OK\s+(\d+)\s+(\d+)/.match(res) or
|
|
|
|
raise POPBadResponse, "wrong response format: #{res}"
|
|
|
|
[m[1].to_i, m[2].to_i]
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def rset
|
2004-03-28 00:23:13 -05:00
|
|
|
check_response(critical { get_response('RSET') })
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2004-03-28 00:23:13 -05:00
|
|
|
def top(num, lines = 0, &block)
|
2003-05-02 10:35:01 -04:00
|
|
|
critical {
|
2003-07-01 22:34:39 -04:00
|
|
|
getok('TOP %d %d', num, lines)
|
|
|
|
@socket.each_message_chunk(&block)
|
2000-03-27 10:52:27 -05:00
|
|
|
}
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2004-03-28 00:23:13 -05:00
|
|
|
def retr(num, &block)
|
2003-05-02 10:35:01 -04:00
|
|
|
critical {
|
2003-07-01 22:34:39 -04:00
|
|
|
getok('RETR %d', num)
|
|
|
|
@socket.each_message_chunk(&block)
|
2000-03-27 10:52:27 -05:00
|
|
|
}
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2004-03-28 00:23:13 -05:00
|
|
|
def dele(num)
|
2003-05-02 10:35:01 -04:00
|
|
|
check_response(critical { get_response('DELE %d', num) })
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2004-03-28 00:23:13 -05:00
|
|
|
def uidl(num = nil)
|
2003-05-02 10:35:01 -04:00
|
|
|
if num
|
|
|
|
res = check_response(critical { get_response('UIDL %d', num) })
|
2003-07-01 22:34:39 -04:00
|
|
|
return res.split(/ /)[1]
|
2003-05-02 10:35:01 -04:00
|
|
|
else
|
|
|
|
critical {
|
2003-07-01 22:34:39 -04:00
|
|
|
getok('UIDL')
|
|
|
|
table = {}
|
|
|
|
@socket.each_list_item do |line|
|
|
|
|
num, uid = line.split
|
|
|
|
table[num.to_i] = uid
|
|
|
|
end
|
|
|
|
return table
|
2003-05-02 10:35:01 -04:00
|
|
|
}
|
|
|
|
end
|
1999-10-18 05:03:16 -04:00
|
|
|
end
|
|
|
|
|
2000-03-27 10:52:27 -05:00
|
|
|
def quit
|
2003-05-02 10:35:01 -04:00
|
|
|
check_response(critical { get_response('QUIT') })
|
2000-03-27 10:52:27 -05:00
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2000-03-27 10:52:27 -05:00
|
|
|
private
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2004-03-28 00:23:13 -05:00
|
|
|
def getok(fmt, *fargs)
|
2003-07-01 22:34:39 -04:00
|
|
|
@socket.writeline sprintf(fmt, *fargs)
|
2003-05-02 10:35:01 -04:00
|
|
|
check_response(recv_response())
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2004-03-28 00:23:13 -05:00
|
|
|
def get_response(fmt, *fargs)
|
2003-07-01 22:34:39 -04:00
|
|
|
@socket.writeline sprintf(fmt, *fargs)
|
2003-05-02 10:35:01 -04:00
|
|
|
recv_response()
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2003-05-02 10:35:01 -04:00
|
|
|
def recv_response
|
|
|
|
@socket.readline
|
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2004-03-28 00:23:13 -05:00
|
|
|
def check_response(res)
|
|
|
|
raise POPError, res unless /\A\+OK/i =~ res
|
2003-05-02 10:35:01 -04:00
|
|
|
res
|
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2004-03-28 00:23:13 -05:00
|
|
|
def check_response_auth(res)
|
|
|
|
raise POPAuthenticationError, res unless /\A\+OK/i =~ res
|
2003-05-02 10:35:01 -04:00
|
|
|
res
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2003-05-02 10:35:01 -04:00
|
|
|
def critical
|
2013-05-19 10:50:47 -04:00
|
|
|
return '+OK dummy ok response' if @error_occurred
|
2003-07-01 22:34:39 -04:00
|
|
|
begin
|
|
|
|
return yield()
|
|
|
|
rescue Exception
|
2013-05-19 10:50:47 -04:00
|
|
|
@error_occurred = true
|
2003-07-01 22:34:39 -04:00
|
|
|
raise
|
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2003-07-01 22:34:39 -04:00
|
|
|
end # class POP3Command
|
1999-09-22 03:32:33 -04:00
|
|
|
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 02:04:40 -04:00
|
|
|
end # module Net
|