mlist 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. data/CHANGELOG +59 -0
  2. data/README +204 -0
  3. data/Rakefile +27 -0
  4. data/TODO +36 -0
  5. data/VERSION.yml +4 -0
  6. data/lib/mlist/email.rb +69 -0
  7. data/lib/mlist/email_post.rb +126 -0
  8. data/lib/mlist/email_server/base.rb +33 -0
  9. data/lib/mlist/email_server/default.rb +31 -0
  10. data/lib/mlist/email_server/fake.rb +16 -0
  11. data/lib/mlist/email_server/pop.rb +28 -0
  12. data/lib/mlist/email_server/smtp.rb +24 -0
  13. data/lib/mlist/email_server.rb +2 -0
  14. data/lib/mlist/email_subscriber.rb +6 -0
  15. data/lib/mlist/list.rb +183 -0
  16. data/lib/mlist/mail_list.rb +277 -0
  17. data/lib/mlist/manager/database.rb +48 -0
  18. data/lib/mlist/manager/notifier.rb +31 -0
  19. data/lib/mlist/manager.rb +30 -0
  20. data/lib/mlist/message.rb +150 -0
  21. data/lib/mlist/server.rb +62 -0
  22. data/lib/mlist/thread.rb +98 -0
  23. data/lib/mlist/util/email_helpers.rb +155 -0
  24. data/lib/mlist/util/header_sanitizer.rb +71 -0
  25. data/lib/mlist/util/quoting.rb +70 -0
  26. data/lib/mlist/util/tmail_builder.rb +42 -0
  27. data/lib/mlist/util/tmail_methods.rb +138 -0
  28. data/lib/mlist/util.rb +12 -0
  29. data/lib/mlist.rb +46 -0
  30. data/lib/pop_ssl.rb +999 -0
  31. data/rails/init.rb +22 -0
  32. data/spec/fixtures/schema.rb +94 -0
  33. data/spec/integration/date_formats_spec.rb +12 -0
  34. data/spec/integration/mlist_spec.rb +232 -0
  35. data/spec/integration/pop_email_server_spec.rb +22 -0
  36. data/spec/integration/proof_spec.rb +74 -0
  37. data/spec/matchers/equal_tmail.rb +53 -0
  38. data/spec/matchers/have_address.rb +48 -0
  39. data/spec/matchers/have_header.rb +104 -0
  40. data/spec/models/email_post_spec.rb +100 -0
  41. data/spec/models/email_server/base_spec.rb +11 -0
  42. data/spec/models/email_spec.rb +54 -0
  43. data/spec/models/mail_list_spec.rb +469 -0
  44. data/spec/models/message_spec.rb +109 -0
  45. data/spec/models/thread_spec.rb +83 -0
  46. data/spec/models/util/email_helpers_spec.rb +47 -0
  47. data/spec/models/util/header_sanitizer_spec.rb +19 -0
  48. data/spec/models/util/quoting_spec.rb +96 -0
  49. data/spec/spec_helper.rb +76 -0
  50. metadata +103 -0
data/lib/pop_ssl.rb ADDED
@@ -0,0 +1,999 @@
1
+ # = net/pop.rb
2
+ #
3
+ # Copyright (c) 1999-2007 Yukihiro Matsumoto.
4
+ #
5
+ # Copyright (c) 1999-2007 Minero Aoki.
6
+ #
7
+ # Written & maintained by Minero Aoki <aamine@loveruby.net>.
8
+ #
9
+ # Documented by William Webber and Minero Aoki.
10
+ #
11
+ # This program is free software. You can re-distribute and/or
12
+ # modify this program under the same terms as Ruby itself,
13
+ # Ruby Distribute License.
14
+ #
15
+ # NOTE: You can find Japanese version of this document at:
16
+ # http://www.ruby-lang.org/ja/man/html/net_pop.html
17
+ #
18
+ # $Id$
19
+ #
20
+ # See Net::POP3 for documentation.
21
+ #
22
+
23
+ require 'net/protocol'
24
+ require 'digest/md5'
25
+ require 'timeout'
26
+
27
+ begin
28
+ require "openssl/ssl"
29
+ rescue LoadError
30
+ end
31
+
32
+ module Net
33
+
34
+ # Non-authentication POP3 protocol error
35
+ # (reply code "-ERR", except authentication).
36
+ class POPError < ProtocolError; end
37
+
38
+ # POP3 authentication error.
39
+ class POPAuthenticationError < ProtoAuthError; end
40
+
41
+ # Unexpected response from the server.
42
+ class POPBadResponse < POPError; end
43
+
44
+ #
45
+ # = Net::POP3
46
+ #
47
+ # == What is This Library?
48
+ #
49
+ # This library provides functionality for retrieving
50
+ # email via POP3, the Post Office Protocol version 3. For details
51
+ # of POP3, see [RFC1939] (http://www.ietf.org/rfc/rfc1939.txt).
52
+ #
53
+ # == Examples
54
+ #
55
+ # === Retrieving Messages
56
+ #
57
+ # This example retrieves messages from the server and deletes them
58
+ # on the server.
59
+ #
60
+ # Messages are written to files named 'inbox/1', 'inbox/2', ....
61
+ # Replace 'pop.example.com' with your POP3 server address, and
62
+ # 'YourAccount' and 'YourPassword' with the appropriate account
63
+ # details.
64
+ #
65
+ # require 'net/pop'
66
+ #
67
+ # pop = Net::POP3.new('pop.example.com')
68
+ # pop.start('YourAccount', 'YourPassword') # (1)
69
+ # if pop.mails.empty?
70
+ # puts 'No mail.'
71
+ # else
72
+ # i = 0
73
+ # pop.each_mail do |m| # or "pop.mails.each ..." # (2)
74
+ # File.open("inbox/#{i}", 'w') do |f|
75
+ # f.write m.pop
76
+ # end
77
+ # m.delete
78
+ # i += 1
79
+ # end
80
+ # puts "#{pop.mails.size} mails popped."
81
+ # end
82
+ # pop.finish # (3)
83
+ #
84
+ # 1. Call Net::POP3#start and start POP session.
85
+ # 2. Access messages by using POP3#each_mail and/or POP3#mails.
86
+ # 3. Close POP session by calling POP3#finish or use the block form of #start.
87
+ #
88
+ # === Shortened Code
89
+ #
90
+ # The example above is very verbose. You can shorten the code by using
91
+ # some utility methods. First, the block form of Net::POP3.start can
92
+ # be used instead of POP3.new, POP3#start and POP3#finish.
93
+ #
94
+ # require 'net/pop'
95
+ #
96
+ # Net::POP3.start('pop.example.com', 110,
97
+ # 'YourAccount', 'YourPassword') do |pop|
98
+ # if pop.mails.empty?
99
+ # puts 'No mail.'
100
+ # else
101
+ # i = 0
102
+ # pop.each_mail do |m| # or "pop.mails.each ..."
103
+ # File.open("inbox/#{i}", 'w') do |f|
104
+ # f.write m.pop
105
+ # end
106
+ # m.delete
107
+ # i += 1
108
+ # end
109
+ # puts "#{pop.mails.size} mails popped."
110
+ # end
111
+ # end
112
+ #
113
+ # POP3#delete_all is an alternative for #each_mail and #delete.
114
+ #
115
+ # require 'net/pop'
116
+ #
117
+ # Net::POP3.start('pop.example.com', 110,
118
+ # 'YourAccount', 'YourPassword') do |pop|
119
+ # if pop.mails.empty?
120
+ # puts 'No mail.'
121
+ # else
122
+ # i = 1
123
+ # pop.delete_all do |m|
124
+ # File.open("inbox/#{i}", 'w') do |f|
125
+ # f.write m.pop
126
+ # end
127
+ # i += 1
128
+ # end
129
+ # end
130
+ # end
131
+ #
132
+ # And here is an even shorter example.
133
+ #
134
+ # require 'net/pop'
135
+ #
136
+ # i = 0
137
+ # Net::POP3.delete_all('pop.example.com', 110,
138
+ # 'YourAccount', 'YourPassword') do |m|
139
+ # File.open("inbox/#{i}", 'w') do |f|
140
+ # f.write m.pop
141
+ # end
142
+ # i += 1
143
+ # end
144
+ #
145
+ # === Memory Space Issues
146
+ #
147
+ # All the examples above get each message as one big string.
148
+ # This example avoids this.
149
+ #
150
+ # require 'net/pop'
151
+ #
152
+ # i = 1
153
+ # Net::POP3.delete_all('pop.example.com', 110,
154
+ # 'YourAccount', 'YourPassword') do |m|
155
+ # File.open("inbox/#{i}", 'w') do |f|
156
+ # m.pop do |chunk| # get a message little by little.
157
+ # f.write chunk
158
+ # end
159
+ # i += 1
160
+ # end
161
+ # end
162
+ #
163
+ # === Using APOP
164
+ #
165
+ # The net/pop library supports APOP authentication.
166
+ # To use APOP, use the Net::APOP class instead of the Net::POP3 class.
167
+ # You can use the utility method, Net::POP3.APOP(). For example:
168
+ #
169
+ # require 'net/pop'
170
+ #
171
+ # # Use APOP authentication if $isapop == true
172
+ # pop = Net::POP3.APOP($is_apop).new('apop.example.com', 110)
173
+ # pop.start(YourAccount', 'YourPassword') do |pop|
174
+ # # Rest of the code is the same.
175
+ # end
176
+ #
177
+ # === Fetch Only Selected Mail Using 'UIDL' POP Command
178
+ #
179
+ # If your POP server provides UIDL functionality,
180
+ # you can grab only selected mails from the POP server.
181
+ # e.g.
182
+ #
183
+ # def need_pop?( id )
184
+ # # determine if we need pop this mail...
185
+ # end
186
+ #
187
+ # Net::POP3.start('pop.example.com', 110,
188
+ # 'Your account', 'Your password') do |pop|
189
+ # pop.mails.select { |m| need_pop?(m.unique_id) }.each do |m|
190
+ # do_something(m.pop)
191
+ # end
192
+ # end
193
+ #
194
+ # The POPMail#unique_id() method returns the unique-id of the message as a
195
+ # String. Normally the unique-id is a hash of the message.
196
+ #
197
+ class POP3 < Protocol
198
+
199
+ Revision = %q$Revision$.split[1]
200
+
201
+ #
202
+ # Class Parameters
203
+ #
204
+
205
+ def POP3.default_port
206
+ default_pop3_port()
207
+ end
208
+
209
+ # The default port for POP3 connections, port 110
210
+ def POP3.default_pop3_port
211
+ 110
212
+ end
213
+
214
+ # The default port for POP3S connections, port 995
215
+ def POP3.default_pop3s_port
216
+ 995
217
+ end
218
+
219
+ def POP3.socket_type #:nodoc: obsolete
220
+ Net::InternetMessageIO
221
+ end
222
+
223
+ #
224
+ # Utilities
225
+ #
226
+
227
+ # Returns the APOP class if +isapop+ is true; otherwise, returns
228
+ # the POP class. For example:
229
+ #
230
+ # # Example 1
231
+ # pop = Net::POP3::APOP($is_apop).new(addr, port)
232
+ #
233
+ # # Example 2
234
+ # Net::POP3::APOP($is_apop).start(addr, port) do |pop|
235
+ # ....
236
+ # end
237
+ #
238
+ def POP3.APOP(isapop)
239
+ isapop ? APOP : POP3
240
+ end
241
+
242
+ # Starts a POP3 session and iterates over each POPMail object,
243
+ # yielding it to the +block+.
244
+ # This method is equivalent to:
245
+ #
246
+ # Net::POP3.start(address, port, account, password) do |pop|
247
+ # pop.each_mail do |m|
248
+ # yield m
249
+ # end
250
+ # end
251
+ #
252
+ # This method raises a POPAuthenticationError if authentication fails.
253
+ #
254
+ # === Example
255
+ #
256
+ # Net::POP3.foreach('pop.example.com', 110,
257
+ # 'YourAccount', 'YourPassword') do |m|
258
+ # file.write m.pop
259
+ # m.delete if $DELETE
260
+ # end
261
+ #
262
+ def POP3.foreach(address, port = nil,
263
+ account = nil, password = nil,
264
+ isapop = false, &block) # :yields: message
265
+ start(address, port, account, password, isapop) {|pop|
266
+ pop.each_mail(&block)
267
+ }
268
+ end
269
+
270
+ # Starts a POP3 session and deletes all messages on the server.
271
+ # If a block is given, each POPMail object is yielded to it before
272
+ # being deleted.
273
+ #
274
+ # This method raises a POPAuthenticationError if authentication fails.
275
+ #
276
+ # === Example
277
+ #
278
+ # Net::POP3.delete_all('pop.example.com', 110,
279
+ # 'YourAccount', 'YourPassword') do |m|
280
+ # file.write m.pop
281
+ # end
282
+ #
283
+ def POP3.delete_all(address, port = nil,
284
+ account = nil, password = nil,
285
+ isapop = false, &block)
286
+ start(address, port, account, password, isapop) {|pop|
287
+ pop.delete_all(&block)
288
+ }
289
+ end
290
+
291
+ # Opens a POP3 session, attempts authentication, and quits.
292
+ #
293
+ # This method raises POPAuthenticationError if authentication fails.
294
+ #
295
+ # === Example: normal POP3
296
+ #
297
+ # Net::POP3.auth_only('pop.example.com', 110,
298
+ # 'YourAccount', 'YourPassword')
299
+ #
300
+ # === Example: APOP
301
+ #
302
+ # Net::POP3.auth_only('pop.example.com', 110,
303
+ # 'YourAccount', 'YourPassword', true)
304
+ #
305
+ def POP3.auth_only(address, port = nil,
306
+ account = nil, password = nil,
307
+ isapop = false)
308
+ new(address, port, isapop).auth_only account, password
309
+ end
310
+
311
+ # Starts a pop3 session, attempts authentication, and quits.
312
+ # This method must not be called while POP3 session is opened.
313
+ # This method raises POPAuthenticationError if authentication fails.
314
+ def auth_only(account, password)
315
+ raise IOError, 'opening previously opened POP session' if started?
316
+ start(account, password) {
317
+ ;
318
+ }
319
+ end
320
+
321
+ #
322
+ # SSL
323
+ #
324
+
325
+ @ssl_params = nil
326
+
327
+ # call-seq:
328
+ # Net::POP.enable_ssl(params = {})
329
+ #
330
+ # Enable SSL for all new instances.
331
+ # +params+ is passed to OpenSSL::SSLContext#set_params.
332
+ def POP3.enable_ssl(*args)
333
+ @ssl_params = create_ssl_params(*args)
334
+ end
335
+
336
+ def POP3.create_ssl_params(verify_or_params = {}, certs = nil)
337
+ begin
338
+ params = verify_or_params.to_hash
339
+ rescue NoMethodError
340
+ params = {}
341
+ params[:verify_mode] = verify_or_params
342
+ if certs
343
+ if File.file?(certs)
344
+ params[:ca_file] = certs
345
+ elsif File.directory?(certs)
346
+ params[:ca_path] = certs
347
+ end
348
+ end
349
+ end
350
+ return params
351
+ end
352
+
353
+ # Disable SSL for all new instances.
354
+ def POP3.disable_ssl
355
+ @ssl_params = nil
356
+ end
357
+
358
+ def POP3.ssl_params
359
+ return @ssl_params
360
+ end
361
+
362
+ def POP3.use_ssl?
363
+ return !@ssl_params.nil?
364
+ end
365
+
366
+ def POP3.verify
367
+ return @ssl_params[:verify_mode]
368
+ end
369
+
370
+ def POP3.certs
371
+ return @ssl_params[:ca_file] || @ssl_params[:ca_path]
372
+ end
373
+
374
+ #
375
+ # Session management
376
+ #
377
+
378
+ # Creates a new POP3 object and open the connection. Equivalent to
379
+ #
380
+ # Net::POP3.new(address, port, isapop).start(account, password)
381
+ #
382
+ # If +block+ is provided, yields the newly-opened POP3 object to it,
383
+ # and automatically closes it at the end of the session.
384
+ #
385
+ # === Example
386
+ #
387
+ # Net::POP3.start(addr, port, account, password) do |pop|
388
+ # pop.each_mail do |m|
389
+ # file.write m.pop
390
+ # m.delete
391
+ # end
392
+ # end
393
+ #
394
+ def POP3.start(address, port = nil,
395
+ account = nil, password = nil,
396
+ isapop = false, &block) # :yield: pop
397
+ new(address, port, isapop).start(account, password, &block)
398
+ end
399
+
400
+ # Creates a new POP3 object.
401
+ #
402
+ # +address+ is the hostname or ip address of your POP3 server.
403
+ #
404
+ # The optional +port+ is the port to connect to.
405
+ #
406
+ # The optional +isapop+ specifies whether this connection is going
407
+ # to use APOP authentication; it defaults to +false+.
408
+ #
409
+ # This method does *not* open the TCP connection.
410
+ def initialize(addr, port = nil, isapop = false)
411
+ @address = addr
412
+ @ssl_params = POP3.ssl_params
413
+ @port = port
414
+ @apop = isapop
415
+
416
+ @command = nil
417
+ @socket = nil
418
+ @started = false
419
+ @open_timeout = 30
420
+ @read_timeout = 60
421
+ @debug_output = nil
422
+
423
+ @mails = nil
424
+ @n_mails = nil
425
+ @n_bytes = nil
426
+ end
427
+
428
+ # Does this instance use APOP authentication?
429
+ def apop?
430
+ @apop
431
+ end
432
+
433
+ # does this instance use SSL?
434
+ def use_ssl?
435
+ return !@ssl_params.nil?
436
+ end
437
+
438
+ # call-seq:
439
+ # Net::POP#enable_ssl(params = {})
440
+ #
441
+ # Enables SSL for this instance. Must be called before the connection is
442
+ # established to have any effect.
443
+ # +params[:port]+ is port to establish the SSL connection on; Defaults to 995.
444
+ # +params+ (except :port) is passed to OpenSSL::SSLContext#set_params.
445
+ def enable_ssl(verify_or_params = {}, certs = nil, port = nil)
446
+ begin
447
+ @ssl_params = verify_or_params.to_hash.dup
448
+ @port = @ssl_params.delete(:port) || @port
449
+ rescue NoMethodError
450
+ @ssl_params = POP3.create_ssl_params(verify_or_params, certs)
451
+ @port = port || @port
452
+ end
453
+ end
454
+
455
+ def disable_ssl
456
+ @ssl_params = nil
457
+ end
458
+
459
+ # Provide human-readable stringification of class state.
460
+ def inspect
461
+ "#<#{self.class} #{@address}:#{@port} open=#{@started}>"
462
+ end
463
+
464
+ # *WARNING*: This method causes a serious security hole.
465
+ # Use this method only for debugging.
466
+ #
467
+ # Set an output stream for debugging.
468
+ #
469
+ # === Example
470
+ #
471
+ # pop = Net::POP.new(addr, port)
472
+ # pop.set_debug_output $stderr
473
+ # pop.start(account, passwd) do |pop|
474
+ # ....
475
+ # end
476
+ #
477
+ def set_debug_output(arg)
478
+ @debug_output = arg
479
+ end
480
+
481
+ # The address to connect to.
482
+ attr_reader :address
483
+
484
+ # The port number to connect to.
485
+ def port
486
+ return @port || (use_ssl? ? POP3.default_pop3s_port : POP3.default_pop3_port)
487
+ end
488
+
489
+ # Seconds to wait until a connection is opened.
490
+ # If the POP3 object cannot open a connection within this time,
491
+ # it raises a TimeoutError exception.
492
+ attr_accessor :open_timeout
493
+
494
+ # Seconds to wait until reading one block (by one read(1) call).
495
+ # If the POP3 object cannot complete a read() within this time,
496
+ # it raises a TimeoutError exception.
497
+ attr_reader :read_timeout
498
+
499
+ # Set the read timeout.
500
+ def read_timeout=(sec)
501
+ @command.socket.read_timeout = sec if @command
502
+ @read_timeout = sec
503
+ end
504
+
505
+ # +true+ if the POP3 session has started.
506
+ def started?
507
+ @started
508
+ end
509
+
510
+ alias active? started? #:nodoc: obsolete
511
+
512
+ # Starts a POP3 session.
513
+ #
514
+ # When called with block, gives a POP3 object to the block and
515
+ # closes the session after block call finishes.
516
+ #
517
+ # This method raises a POPAuthenticationError if authentication fails.
518
+ def start(account, password) # :yield: pop
519
+ raise IOError, 'POP session already started' if @started
520
+ if block_given?
521
+ begin
522
+ do_start account, password
523
+ return yield(self)
524
+ ensure
525
+ do_finish
526
+ end
527
+ else
528
+ do_start account, password
529
+ return self
530
+ end
531
+ end
532
+
533
+ def do_start(account, password)
534
+ s = timeout(@open_timeout) { TCPSocket.open(@address, port) }
535
+ if use_ssl?
536
+ raise 'openssl library not installed' unless defined?(OpenSSL)
537
+ context = OpenSSL::SSL::SSLContext.new
538
+ # Commented out by Adam to make it work with Ruby 1.8
539
+ # context.set_params(@ssl_params)
540
+ s = OpenSSL::SSL::SSLSocket.new(s, context)
541
+ s.sync_close = true
542
+ s.connect
543
+ if context.verify_mode != OpenSSL::SSL::VERIFY_NONE
544
+ s.post_connection_check(@address)
545
+ end
546
+ end
547
+ @socket = InternetMessageIO.new(s)
548
+ logging "POP session started: #{@address}:#{@port} (#{@apop ? 'APOP' : 'POP'})"
549
+ @socket.read_timeout = @read_timeout
550
+ @socket.debug_output = @debug_output
551
+ on_connect
552
+ @command = POP3Command.new(@socket)
553
+ if apop?
554
+ @command.apop account, password
555
+ else
556
+ @command.auth account, password
557
+ end
558
+ @started = true
559
+ ensure
560
+ # Authentication failed, clean up connection.
561
+ unless @started
562
+ s.close if s and not s.closed?
563
+ @socket = nil
564
+ @command = nil
565
+ end
566
+ end
567
+ private :do_start
568
+
569
+ def on_connect
570
+ end
571
+ private :on_connect
572
+
573
+ # Finishes a POP3 session and closes TCP connection.
574
+ def finish
575
+ raise IOError, 'POP session not yet started' unless started?
576
+ do_finish
577
+ end
578
+
579
+ def do_finish
580
+ @mails = nil
581
+ @n_mails = nil
582
+ @n_bytes = nil
583
+ @command.quit if @command
584
+ ensure
585
+ @started = false
586
+ @command = nil
587
+ @socket.close if @socket and not @socket.closed?
588
+ @socket = nil
589
+ end
590
+ private :do_finish
591
+
592
+ def command
593
+ raise IOError, 'POP session not opened yet' \
594
+ if not @socket or @socket.closed?
595
+ @command
596
+ end
597
+ private :command
598
+
599
+ #
600
+ # POP protocol wrapper
601
+ #
602
+
603
+ # Returns the number of messages on the POP server.
604
+ def n_mails
605
+ return @n_mails if @n_mails
606
+ @n_mails, @n_bytes = command().stat
607
+ @n_mails
608
+ end
609
+
610
+ # Returns the total size in bytes of all the messages on the POP server.
611
+ def n_bytes
612
+ return @n_bytes if @n_bytes
613
+ @n_mails, @n_bytes = command().stat
614
+ @n_bytes
615
+ end
616
+
617
+ # Returns an array of Net::POPMail objects, representing all the
618
+ # messages on the server. This array is renewed when the session
619
+ # restarts; otherwise, it is fetched from the server the first time
620
+ # this method is called (directly or indirectly) and cached.
621
+ #
622
+ # This method raises a POPError if an error occurs.
623
+ def mails
624
+ return @mails.dup if @mails
625
+ if n_mails() == 0
626
+ # some popd raises error for LIST on the empty mailbox.
627
+ @mails = []
628
+ return []
629
+ end
630
+
631
+ @mails = command().list.map {|num, size|
632
+ POPMail.new(num, size, self, command())
633
+ }
634
+ @mails.dup
635
+ end
636
+
637
+ # Yields each message to the passed-in block in turn.
638
+ # Equivalent to:
639
+ #
640
+ # pop3.mails.each do |popmail|
641
+ # ....
642
+ # end
643
+ #
644
+ # This method raises a POPError if an error occurs.
645
+ def each_mail(&block) # :yield: message
646
+ mails().each(&block)
647
+ end
648
+
649
+ alias each each_mail
650
+
651
+ # Deletes all messages on the server.
652
+ #
653
+ # If called with a block, yields each message in turn before deleting it.
654
+ #
655
+ # === Example
656
+ #
657
+ # n = 1
658
+ # pop.delete_all do |m|
659
+ # File.open("inbox/#{n}") do |f|
660
+ # f.write m.pop
661
+ # end
662
+ # n += 1
663
+ # end
664
+ #
665
+ # This method raises a POPError if an error occurs.
666
+ #
667
+ def delete_all # :yield: message
668
+ mails().each do |m|
669
+ yield m if block_given?
670
+ m.delete unless m.deleted?
671
+ end
672
+ end
673
+
674
+ # Resets the session. This clears all "deleted" marks from messages.
675
+ #
676
+ # This method raises a POPError if an error occurs.
677
+ def reset
678
+ command().rset
679
+ mails().each do |m|
680
+ m.instance_eval {
681
+ @deleted = false
682
+ }
683
+ end
684
+ end
685
+
686
+ def set_all_uids #:nodoc: internal use only (called from POPMail#uidl)
687
+ uidl = command().uidl
688
+ @mails.each {|m| m.uid = uidl[m.number] }
689
+ end
690
+
691
+ def logging(msg)
692
+ @debug_output << msg + "\n" if @debug_output
693
+ end
694
+
695
+ end # class POP3
696
+
697
+ # class aliases
698
+ POP = POP3
699
+ POPSession = POP3
700
+ POP3Session = POP3
701
+
702
+ #
703
+ # This class is equivalent to POP3, except that it uses APOP authentication.
704
+ #
705
+ class APOP < POP3
706
+ # Always returns true.
707
+ def apop?
708
+ true
709
+ end
710
+ end
711
+
712
+ # class aliases
713
+ APOPSession = APOP
714
+
715
+ #
716
+ # This class represents a message which exists on the POP server.
717
+ # Instances of this class are created by the POP3 class; they should
718
+ # not be directly created by the user.
719
+ #
720
+ class POPMail
721
+
722
+ def initialize(num, len, pop, cmd) #:nodoc:
723
+ @number = num
724
+ @length = len
725
+ @pop = pop
726
+ @command = cmd
727
+ @deleted = false
728
+ @uid = nil
729
+ end
730
+
731
+ # The sequence number of the message on the server.
732
+ attr_reader :number
733
+
734
+ # The length of the message in octets.
735
+ attr_reader :length
736
+ alias size length
737
+
738
+ # Provide human-readable stringification of class state.
739
+ def inspect
740
+ "#<#{self.class} #{@number}#{@deleted ? ' deleted' : ''}>"
741
+ end
742
+
743
+ #
744
+ # This method fetches the message. If called with a block, the
745
+ # message is yielded to the block one chunk at a time. If called
746
+ # without a block, the message is returned as a String. The optional
747
+ # +dest+ argument will be prepended to the returned String; this
748
+ # argument is essentially obsolete.
749
+ #
750
+ # === Example without block
751
+ #
752
+ # POP3.start('pop.example.com', 110,
753
+ # 'YourAccount, 'YourPassword') do |pop|
754
+ # n = 1
755
+ # pop.mails.each do |popmail|
756
+ # File.open("inbox/#{n}", 'w') do |f|
757
+ # f.write popmail.pop
758
+ # end
759
+ # popmail.delete
760
+ # n += 1
761
+ # end
762
+ # end
763
+ #
764
+ # === Example with block
765
+ #
766
+ # POP3.start('pop.example.com', 110,
767
+ # 'YourAccount, 'YourPassword') do |pop|
768
+ # n = 1
769
+ # pop.mails.each do |popmail|
770
+ # File.open("inbox/#{n}", 'w') do |f|
771
+ # popmail.pop do |chunk| ####
772
+ # f.write chunk
773
+ # end
774
+ # end
775
+ # n += 1
776
+ # end
777
+ # end
778
+ #
779
+ # This method raises a POPError if an error occurs.
780
+ #
781
+ def pop( dest = '', &block ) # :yield: message_chunk
782
+ if block_given?
783
+ @command.retr(@number, &block)
784
+ nil
785
+ else
786
+ @command.retr(@number) do |chunk|
787
+ dest << chunk
788
+ end
789
+ dest
790
+ end
791
+ end
792
+
793
+ alias all pop #:nodoc: obsolete
794
+ alias mail pop #:nodoc: obsolete
795
+
796
+ # Fetches the message header and +lines+ lines of body.
797
+ #
798
+ # The optional +dest+ argument is obsolete.
799
+ #
800
+ # This method raises a POPError if an error occurs.
801
+ def top(lines, dest = '')
802
+ @command.top(@number, lines) do |chunk|
803
+ dest << chunk
804
+ end
805
+ dest
806
+ end
807
+
808
+ # Fetches the message header.
809
+ #
810
+ # The optional +dest+ argument is obsolete.
811
+ #
812
+ # This method raises a POPError if an error occurs.
813
+ def header(dest = '')
814
+ top(0, dest)
815
+ end
816
+
817
+ # Marks a message for deletion on the server. Deletion does not
818
+ # actually occur until the end of the session; deletion may be
819
+ # cancelled for _all_ marked messages by calling POP3#reset().
820
+ #
821
+ # This method raises a POPError if an error occurs.
822
+ #
823
+ # === Example
824
+ #
825
+ # POP3.start('pop.example.com', 110,
826
+ # 'YourAccount, 'YourPassword') do |pop|
827
+ # n = 1
828
+ # pop.mails.each do |popmail|
829
+ # File.open("inbox/#{n}", 'w') do |f|
830
+ # f.write popmail.pop
831
+ # end
832
+ # popmail.delete ####
833
+ # n += 1
834
+ # end
835
+ # end
836
+ #
837
+ def delete
838
+ @command.dele @number
839
+ @deleted = true
840
+ end
841
+
842
+ alias delete! delete #:nodoc: obsolete
843
+
844
+ # True if the mail has been deleted.
845
+ def deleted?
846
+ @deleted
847
+ end
848
+
849
+ # Returns the unique-id of the message.
850
+ # Normally the unique-id is a hash string of the message.
851
+ #
852
+ # This method raises a POPError if an error occurs.
853
+ def unique_id
854
+ return @uid if @uid
855
+ @pop.set_all_uids
856
+ @uid
857
+ end
858
+
859
+ alias uidl unique_id
860
+
861
+ def uid=(uid) #:nodoc: internal use only
862
+ @uid = uid
863
+ end
864
+
865
+ end # class POPMail
866
+
867
+
868
+ class POP3Command #:nodoc: internal use only
869
+
870
+ def initialize(sock)
871
+ @socket = sock
872
+ @error_occured = false
873
+ res = check_response(critical { recv_response() })
874
+ @apop_stamp = res.slice(/<.+>/)
875
+ end
876
+
877
+ def inspect
878
+ "#<#{self.class} socket=#{@socket}>"
879
+ end
880
+
881
+ def auth(account, password)
882
+ check_response_auth(critical {
883
+ check_response_auth(get_response('USER %s', account))
884
+ get_response('PASS %s', password)
885
+ })
886
+ end
887
+
888
+ def apop(account, password)
889
+ raise POPAuthenticationError, 'not APOP server; cannot login' \
890
+ unless @apop_stamp
891
+ check_response_auth(critical {
892
+ get_response('APOP %s %s',
893
+ account,
894
+ Digest::MD5.hexdigest(@apop_stamp + password))
895
+ })
896
+ end
897
+
898
+ def list
899
+ critical {
900
+ getok 'LIST'
901
+ list = []
902
+ @socket.each_list_item do |line|
903
+ m = /\A(\d+)[ \t]+(\d+)/.match(line) or
904
+ raise POPBadResponse, "bad response: #{line}"
905
+ list.push [m[1].to_i, m[2].to_i]
906
+ end
907
+ return list
908
+ }
909
+ end
910
+
911
+ def stat
912
+ res = check_response(critical { get_response('STAT') })
913
+ m = /\A\+OK\s+(\d+)\s+(\d+)/.match(res) or
914
+ raise POPBadResponse, "wrong response format: #{res}"
915
+ [m[1].to_i, m[2].to_i]
916
+ end
917
+
918
+ def rset
919
+ check_response(critical { get_response('RSET') })
920
+ end
921
+
922
+ def top(num, lines = 0, &block)
923
+ critical {
924
+ getok('TOP %d %d', num, lines)
925
+ @socket.each_message_chunk(&block)
926
+ }
927
+ end
928
+
929
+ def retr(num, &block)
930
+ critical {
931
+ getok('RETR %d', num)
932
+ @socket.each_message_chunk(&block)
933
+ }
934
+ end
935
+
936
+ def dele(num)
937
+ check_response(critical { get_response('DELE %d', num) })
938
+ end
939
+
940
+ def uidl(num = nil)
941
+ if num
942
+ res = check_response(critical { get_response('UIDL %d', num) })
943
+ return res.split(/ /)[1]
944
+ else
945
+ critical {
946
+ getok('UIDL')
947
+ table = {}
948
+ @socket.each_list_item do |line|
949
+ num, uid = line.split
950
+ table[num.to_i] = uid
951
+ end
952
+ return table
953
+ }
954
+ end
955
+ end
956
+
957
+ def quit
958
+ check_response(critical { get_response('QUIT') })
959
+ end
960
+
961
+ private
962
+
963
+ def getok(fmt, *fargs)
964
+ @socket.writeline sprintf(fmt, *fargs)
965
+ check_response(recv_response())
966
+ end
967
+
968
+ def get_response(fmt, *fargs)
969
+ @socket.writeline sprintf(fmt, *fargs)
970
+ recv_response()
971
+ end
972
+
973
+ def recv_response
974
+ @socket.readline
975
+ end
976
+
977
+ def check_response(res)
978
+ raise POPError, res unless /\A\+OK/i =~ res
979
+ res
980
+ end
981
+
982
+ def check_response_auth(res)
983
+ raise POPAuthenticationError, res unless /\A\+OK/i =~ res
984
+ res
985
+ end
986
+
987
+ def critical
988
+ return '+OK dummy ok response' if @error_occured
989
+ begin
990
+ return yield()
991
+ rescue Exception
992
+ @error_occured = true
993
+ raise
994
+ end
995
+ end
996
+
997
+ end # class POP3Command
998
+
999
+ end # module Net