1999-10-13 11:29:15 +04:00
|
|
|
=begin
|
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
= net/pop.rb
|
1999-10-13 11:29:15 +04:00
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
1999-10-13 11:29:15 +04:00
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
This library is distributed under the terms of the Ruby license.
|
|
|
|
You can freely distribute/modify this library.
|
1999-10-13 11:29:15 +04:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2000-02-21 18:25:37 +03:00
|
|
|
require 'net/protocol'
|
1999-09-22 11:32:33 +04:00
|
|
|
require 'md5'
|
|
|
|
|
|
|
|
|
|
|
|
module Net
|
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
|
1999-10-13 11:29:15 +04:00
|
|
|
=begin
|
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
== Net::POP3
|
1999-10-13 11:29:15 +04:00
|
|
|
|
|
|
|
=== Super Class
|
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
Net::Protocol
|
1999-10-13 11:29:15 +04:00
|
|
|
|
|
|
|
=== Class Methods
|
|
|
|
|
|
|
|
: new( address = 'localhost', port = 110 )
|
1999-12-29 14:14:04 +03:00
|
|
|
This method create a new POP3 object.
|
1999-12-17 18:00:13 +03:00
|
|
|
This will not open connection yet.
|
1999-09-22 11:32:33 +04:00
|
|
|
|
1999-10-13 11:29:15 +04:00
|
|
|
|
|
|
|
=== Methods
|
|
|
|
|
|
|
|
: start( account, password )
|
1999-12-29 14:14:04 +03:00
|
|
|
This method start POP3.
|
1999-10-13 11:29:15 +04:00
|
|
|
|
|
|
|
: each{|popmail| ...}
|
1999-12-29 14:14:04 +03:00
|
|
|
This method is equals to "pop3.mails.each"
|
1999-10-13 11:29:15 +04:00
|
|
|
|
|
|
|
: mails
|
1999-12-17 18:00:13 +03:00
|
|
|
This method returns an array of ((URL:#POPMail)).
|
1999-10-13 11:29:15 +04:00
|
|
|
This array is renewed when login.
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
class POP3 < Protocol
|
1999-12-17 18:00:13 +03:00
|
|
|
|
1999-12-29 14:14:04 +03: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 18:00:13 +03:00
|
|
|
|
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
attr :mails
|
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
def each
|
|
|
|
@mails.each {|m| yield m }
|
|
|
|
end
|
1999-09-22 11:32:33 +04:00
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
|
|
def do_start( acnt, pwd )
|
1999-12-29 14:14:04 +03:00
|
|
|
@command.auth( acnt, pwd )
|
2000-03-27 19:52:27 +04:00
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
@mails = []
|
2000-03-27 19:52:27 +04:00
|
|
|
t = type.mail_type
|
1999-12-29 14:14:04 +03:00
|
|
|
@command.list.each_with_index do |size,idx|
|
1999-09-22 11:32:33 +04:00
|
|
|
if size then
|
1999-12-29 14:14:04 +03:00
|
|
|
@mails.push t.new( idx, size, @command )
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
@mails.freeze
|
|
|
|
end
|
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
end
|
1999-09-22 11:32:33 +04:00
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
POP = POP3
|
|
|
|
POPSession = POP3
|
|
|
|
POP3Session = POP3
|
1999-09-22 11:32:33 +04:00
|
|
|
|
|
|
|
|
1999-10-13 11:29:15 +04:00
|
|
|
=begin
|
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
== Net::POPMail
|
1999-10-13 11:29:15 +04:00
|
|
|
|
|
|
|
A class of mail which exists on POP server.
|
|
|
|
|
|
|
|
=== Super Class
|
|
|
|
|
|
|
|
Object
|
|
|
|
|
|
|
|
|
|
|
|
=== Method
|
|
|
|
|
2000-02-21 18:25:37 +03:00
|
|
|
: all( dest = '' )
|
1999-10-13 11:29:15 +04:00
|
|
|
: pop
|
|
|
|
: mail
|
2000-02-21 18:25:37 +03: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 11: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 18:00:13 +03:00
|
|
|
class POPMail
|
1999-09-22 11:32:33 +04:00
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
def initialize( n, s, cmd )
|
|
|
|
@num = n
|
|
|
|
@size = s
|
|
|
|
@command = cmd
|
1999-09-22 11:32:33 +04:00
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
@deleted = false
|
|
|
|
end
|
1999-09-22 11:32:33 +04:00
|
|
|
|
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
attr :size
|
1999-09-22 11:32:33 +04:00
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
def all( dest = '' )
|
2000-02-21 18:25:37 +03:00
|
|
|
if iterator? then
|
|
|
|
dest = ReadAdapter.new( Proc.new )
|
|
|
|
end
|
1999-12-29 14:14:04 +03:00
|
|
|
@command.retr( @num, dest )
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
|
|
|
alias pop all
|
|
|
|
alias mail all
|
1999-09-22 11:32:33 +04:00
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
def top( lines, dest = '' )
|
1999-12-29 14:14:04 +03:00
|
|
|
@command.top( @num, lines, dest )
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
1999-10-18 13:03:16 +04:00
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
def header( dest = '' )
|
|
|
|
top( 0, dest )
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
def delete
|
1999-12-29 14:14:04 +03:00
|
|
|
@command.dele( @num )
|
1999-12-17 18:00:13 +03:00
|
|
|
@deleted = true
|
|
|
|
end
|
|
|
|
alias delete! delete
|
1999-10-13 11:29:15 +04:00
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
def deleted?
|
|
|
|
@deleted
|
|
|
|
end
|
|
|
|
|
|
|
|
def uidl
|
1999-12-29 14:14:04 +03:00
|
|
|
@command.uidl @num
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
1999-10-13 11:29:15 +04:00
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
|
1999-10-13 11:29:15 +04:00
|
|
|
=begin
|
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
== Net::APOP
|
1999-10-13 11:29:15 +04:00
|
|
|
|
2000-02-21 18:25:37 +03:00
|
|
|
This class defines no new methods.
|
|
|
|
Only difference from POP3 is using APOP authentification.
|
1999-10-13 11:29:15 +04:00
|
|
|
|
|
|
|
=== Super Class
|
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
Net::POP3
|
1999-10-13 11:29:15 +04:00
|
|
|
|
|
|
|
=end
|
1999-09-22 11:32:33 +04:00
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
class APOP < POP3
|
1999-09-22 11:32:33 +04:00
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
protocol_param :command_type, 'Net::APOPCommand'
|
1999-09-22 11:32:33 +04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
1999-12-29 14:14:04 +03:00
|
|
|
APOPSession = APOP
|
1999-09-22 11:32:33 +04:00
|
|
|
|
|
|
|
|
1999-12-17 18:00:13 +03:00
|
|
|
|
|
|
|
class POP3Command < Command
|
|
|
|
|
|
|
|
def initialize( sock )
|
|
|
|
super
|
2000-03-27 19:52:27 +04:00
|
|
|
critical {
|
|
|
|
check_reply SuccessCode
|
|
|
|
}
|
1999-12-17 18:00:13 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
def auth( acnt, pass )
|
2000-03-27 19:52:27 +04:00
|
|
|
critical {
|
|
|
|
@socket.writeline 'USER ' + acnt
|
|
|
|
check_reply_auth
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2000-03-27 19:52:27 +04:00
|
|
|
@socket.writeline 'PASS ' + pass
|
|
|
|
check_reply_auth
|
|
|
|
}
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def list
|
|
|
|
arr = []
|
2000-03-27 19:52:27 +04:00
|
|
|
critical {
|
|
|
|
getok 'LIST'
|
|
|
|
@socket.read_pendlist do |line|
|
|
|
|
num, siz = line.split( / +/o )
|
|
|
|
arr[ num.to_i ] = siz.to_i
|
|
|
|
end
|
|
|
|
}
|
|
|
|
arr
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def rset
|
2000-03-27 19:52:27 +04:00
|
|
|
critical {
|
|
|
|
getok 'RSET'
|
|
|
|
}
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def top( num, lines = 0, dest = '' )
|
2000-03-27 19:52:27 +04:00
|
|
|
critical {
|
|
|
|
getok sprintf( 'TOP %d %d', num, lines )
|
|
|
|
@socket.read_pendstr( dest )
|
|
|
|
}
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def retr( num, dest = '', &block )
|
2000-03-27 19:52:27 +04:00
|
|
|
critical {
|
|
|
|
getok sprintf( 'RETR %d', num )
|
|
|
|
@socket.read_pendstr( dest, &block )
|
|
|
|
}
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def dele( num )
|
2000-03-27 19:52:27 +04:00
|
|
|
critical {
|
|
|
|
getok sprintf( 'DELE %d', num )
|
|
|
|
}
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
1999-10-18 13:03:16 +04:00
|
|
|
def uidl( num )
|
2000-03-27 19:52:27 +04:00
|
|
|
critical {
|
|
|
|
getok( sprintf 'UIDL %d', num ).msg.split(' ')[1]
|
|
|
|
}
|
1999-10-18 13:03:16 +04:00
|
|
|
end
|
|
|
|
|
1999-09-22 11:32:33 +04:00
|
|
|
|
2000-03-27 19:52:27 +04:00
|
|
|
def quit
|
|
|
|
critical {
|
|
|
|
getok 'QUIT'
|
|
|
|
}
|
|
|
|
end
|
1999-09-22 11:32:33 +04:00
|
|
|
|
|
|
|
|
2000-03-27 19:52:27 +04:00
|
|
|
private
|
1999-09-22 11:32:33 +04:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2000-03-31 17:02:40 +04:00
|
|
|
return Response.new( SuccessCode, str[0,3], str[3, str.size - 3].strip )
|
1999-09-22 11:32:33 +04:00
|
|
|
else
|
2000-03-31 17:02:40 +04:00
|
|
|
return Response.new( ErrorCode, str[0,4], str[4, str.size - 4].strip )
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class APOPCommand < POP3Command
|
|
|
|
|
|
|
|
def initialize( sock )
|
|
|
|
rep = super( sock )
|
|
|
|
|
2000-04-14 14:41:35 +04:00
|
|
|
m = /<.+>/.match( rep.msg )
|
|
|
|
unless m then
|
1999-09-22 11:32:33 +04:00
|
|
|
raise ProtoAuthError, "This is not APOP server: can't login"
|
|
|
|
end
|
2000-04-14 14:41:35 +04:00
|
|
|
@stamp = m[0]
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2000-05-17 22:39:43 +04:00
|
|
|
def auth( account, pass )
|
2000-03-27 19:52:27 +04:00
|
|
|
critical {
|
2000-05-17 22:39:43 +04:00
|
|
|
@socket.writeline sprintf( 'APOP %s %s',
|
|
|
|
account, MD5.new(@stamp + pass).hexdigest )
|
2000-03-27 19:52:27 +04:00
|
|
|
check_reply_auth
|
|
|
|
}
|
1999-09-22 11:32:33 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|