1999-10-13 03:29:15 -04:00
|
|
|
=begin
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
= net/pop.rb
|
1999-10-13 03:29:15 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
1999-10-13 03:29:15 -04:00
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
This library is distributed under the terms of the Ruby license.
|
|
|
|
You can freely distribute/modify this library.
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2000-02-21 10:25:37 -05:00
|
|
|
require 'net/protocol'
|
1999-09-22 03:32:33 -04:00
|
|
|
require 'md5'
|
|
|
|
|
|
|
|
|
|
|
|
module Net
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
|
1999-10-13 03:29:15 -04:00
|
|
|
=begin
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
== Net::POP3
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
=== Super Class
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
Net::Protocol
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
=== Class Methods
|
|
|
|
|
|
|
|
: new( address = 'localhost', port = 110 )
|
1999-12-29 06:14:04 -05:00
|
|
|
This method create a new POP3 object.
|
1999-12-17 10:00:13 -05:00
|
|
|
This will not open connection yet.
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
=== Methods
|
|
|
|
|
|
|
|
: start( account, password )
|
1999-12-29 06:14:04 -05:00
|
|
|
This method start POP3.
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
: each{|popmail| ...}
|
1999-12-29 06:14:04 -05:00
|
|
|
This method is equals to "pop3.mails.each"
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
: mails
|
1999-12-17 10:00:13 -05:00
|
|
|
This method returns an array of ((URL:#POPMail)).
|
1999-10-13 03:29:15 -04:00
|
|
|
This array is renewed when login.
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
class POP3 < Protocol
|
1999-12-17 10:00:13 -05:00
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
protocol_param :port, '110'
|
|
|
|
protocol_param :command_type, '::Net::POP3Command'
|
|
|
|
|
|
|
|
protocol_param :mail_type, '::Net::POPMail'
|
|
|
|
|
|
|
|
def initialize( addr = nil, port = nil )
|
|
|
|
super
|
|
|
|
@mails = [].freeze
|
|
|
|
end
|
1999-12-17 10:00:13 -05:00
|
|
|
|
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
attr :mails
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
def each
|
|
|
|
@mails.each {|m| yield m }
|
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
|
|
def do_start( acnt, pwd )
|
1999-12-29 06:14:04 -05:00
|
|
|
@command.auth( acnt, pwd )
|
|
|
|
t = self.type.mail_type
|
1999-09-22 03:32:33 -04:00
|
|
|
@mails = []
|
1999-12-29 06:14:04 -05:00
|
|
|
@command.list.each_with_index do |size,idx|
|
1999-09-22 03:32:33 -04:00
|
|
|
if size then
|
1999-12-29 06:14:04 -05:00
|
|
|
@mails.push t.new( idx, size, @command )
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
@mails.freeze
|
|
|
|
end
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
POP = POP3
|
|
|
|
POPSession = POP3
|
|
|
|
POP3Session = POP3
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
|
1999-10-13 03:29:15 -04:00
|
|
|
=begin
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
== Net::POPMail
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
A class of mail which exists on POP server.
|
|
|
|
|
|
|
|
=== Super Class
|
|
|
|
|
|
|
|
Object
|
|
|
|
|
|
|
|
|
|
|
|
=== Method
|
|
|
|
|
2000-02-21 10:25:37 -05:00
|
|
|
: all( dest = '' )
|
1999-10-13 03:29:15 -04:00
|
|
|
: pop
|
|
|
|
: mail
|
2000-02-21 10:25:37 -05:00
|
|
|
This method fetches a mail and write to 'dest' using '<<' method.
|
|
|
|
|
|
|
|
# usage example
|
|
|
|
|
|
|
|
mailarr = []
|
|
|
|
POP3.start( 'localhost', 110 ) do |pop|
|
|
|
|
pop.each do |popm|
|
|
|
|
mailarr.push popm.pop # all() returns 'dest' (this time, string)
|
|
|
|
# or, you can also
|
|
|
|
# popm.pop( $stdout ) # write mail to stdout
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
: all {|str| .... }
|
|
|
|
You can use all/pop/mail as the iterator.
|
|
|
|
argument 'str' is a read string (a part of mail).
|
|
|
|
|
|
|
|
# usage example
|
|
|
|
|
|
|
|
POP3.start( 'localhost', 110 ) do |pop|
|
|
|
|
pop.mails[0].pop do |str| # pop only first mail...
|
|
|
|
_do_anything_( str )
|
|
|
|
end
|
|
|
|
end
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
: header
|
|
|
|
This method fetches only mail header.
|
|
|
|
|
|
|
|
: top( lines )
|
|
|
|
This method fetches mail header and 'lines' lines body.
|
|
|
|
|
|
|
|
: delete
|
|
|
|
: delete!
|
|
|
|
This method deletes mail.
|
|
|
|
|
|
|
|
: size
|
|
|
|
size of mail(bytes)
|
|
|
|
|
|
|
|
: deleted?
|
|
|
|
true if mail was deleted
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
class POPMail
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
def initialize( n, s, cmd )
|
|
|
|
@num = n
|
|
|
|
@size = s
|
|
|
|
@command = cmd
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
@deleted = false
|
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
attr :size
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
def all( dest = '' )
|
2000-02-21 10:25:37 -05:00
|
|
|
if iterator? then
|
|
|
|
dest = ReadAdapter.new( Proc.new )
|
|
|
|
end
|
1999-12-29 06:14:04 -05:00
|
|
|
@command.retr( @num, dest )
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
|
|
|
alias pop all
|
|
|
|
alias mail all
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
def top( lines, dest = '' )
|
1999-12-29 06:14:04 -05:00
|
|
|
@command.top( @num, lines, dest )
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
1999-10-18 05:03:16 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
def header( dest = '' )
|
|
|
|
top( 0, dest )
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
def delete
|
1999-12-29 06:14:04 -05:00
|
|
|
@command.dele( @num )
|
1999-12-17 10:00:13 -05:00
|
|
|
@deleted = true
|
|
|
|
end
|
|
|
|
alias delete! delete
|
1999-10-13 03:29:15 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
def deleted?
|
|
|
|
@deleted
|
|
|
|
end
|
|
|
|
|
|
|
|
def uidl
|
1999-12-29 06:14:04 -05:00
|
|
|
@command.uidl @num
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
1999-10-13 03:29:15 -04:00
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-10-13 03:29:15 -04:00
|
|
|
=begin
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
== Net::APOP
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2000-02-21 10:25:37 -05:00
|
|
|
This class defines no new methods.
|
|
|
|
Only difference from POP3 is using APOP authentification.
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
=== Super Class
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
Net::POP3
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
=end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
class APOP < POP3
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
protocol_param :command_type, 'Net::APOPCommand'
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
APOPSession = APOP
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
|
|
|
|
class POP3Command < Command
|
|
|
|
|
|
|
|
def initialize( sock )
|
|
|
|
super
|
|
|
|
check_reply SuccessCode
|
|
|
|
end
|
|
|
|
|
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
def auth( acnt, pass )
|
1999-12-29 06:14:04 -05:00
|
|
|
@socket.writeline 'USER ' + acnt
|
1999-09-22 03:32:33 -04:00
|
|
|
check_reply_auth
|
|
|
|
|
|
|
|
@socket.writeline( 'PASS ' + pass )
|
|
|
|
ret = check_reply_auth
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def list
|
1999-12-29 06:14:04 -05:00
|
|
|
getok 'LIST'
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
arr = []
|
|
|
|
@socket.read_pendlist do |line|
|
|
|
|
num, siz = line.split( / +/o )
|
|
|
|
arr[ num.to_i ] = siz.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
return arr
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def rset
|
1999-12-29 06:14:04 -05:00
|
|
|
getok 'RSET'
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def top( num, lines = 0, dest = '' )
|
1999-12-29 06:14:04 -05:00
|
|
|
getok sprintf( 'TOP %d %d', num, lines )
|
|
|
|
@socket.read_pendstr( dest )
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def retr( num, dest = '', &block )
|
1999-12-29 06:14:04 -05:00
|
|
|
getok sprintf( 'RETR %d', num )
|
|
|
|
@socket.read_pendstr( dest, &block )
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def dele( num )
|
1999-12-29 06:14:04 -05:00
|
|
|
getok sprintf( 'DELE %d', num )
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
1999-10-18 05:03:16 -04:00
|
|
|
def uidl( num )
|
1999-12-29 06:14:04 -05:00
|
|
|
rep = getok( sprintf 'UIDL %d', num )
|
1999-10-18 05:03:16 -04:00
|
|
|
uid = rep.msg.split(' ')[1]
|
|
|
|
|
|
|
|
uid
|
|
|
|
end
|
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
|
|
def do_quit
|
1999-12-29 06:14:04 -05:00
|
|
|
getok 'QUIT'
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def check_reply_auth
|
|
|
|
begin
|
|
|
|
cod = check_reply( SuccessCode )
|
|
|
|
rescue ProtocolError
|
|
|
|
raise ProtoAuthError, 'Fail to POP authentication'
|
|
|
|
end
|
|
|
|
|
|
|
|
return cod
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def get_reply
|
|
|
|
str = @socket.readline
|
|
|
|
|
|
|
|
if /\A\+/ === str then
|
|
|
|
return SuccessCode.new( str[0,3], str[3, str.size - 3].strip )
|
|
|
|
else
|
|
|
|
return ErrorCode.new( str[0,4], str[4, str.size - 4].strip )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class APOPCommand < POP3Command
|
|
|
|
|
|
|
|
def initialize( sock )
|
|
|
|
rep = super( sock )
|
|
|
|
|
|
|
|
/<[^@]+@[^@>]+>/o === rep.msg
|
|
|
|
@stamp = $&
|
|
|
|
unless @stamp then
|
|
|
|
raise ProtoAuthError, "This is not APOP server: can't login"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def auth( acnt, pass )
|
|
|
|
@socket.writeline( "APOP #{acnt} #{digest(@stamp + pass)}" )
|
|
|
|
return check_reply_auth
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def digest( str )
|
|
|
|
temp = MD5.new( str ).digest
|
|
|
|
|
|
|
|
ret = ''
|
|
|
|
temp.each_byte do |i|
|
|
|
|
ret << sprintf( '%02x', i )
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|