Class: RIMS::MailFolder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rims/mail_store.rb

Defined Under Namespace

Classes: MessageStruct

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mbox_id, mail_store, read_only: false) ⇒ MailFolder

Returns a new instance of MailFolder.



376
377
378
379
380
381
382
383
384
385
# File 'lib/rims/mail_store.rb', line 376

def initialize(mbox_id, mail_store, read_only: false)
  @mbox_id = mbox_id
  @mail_store = mail_store
  @read_only = read_only

  # late loding
  @cnum = nil
  @msg_list = nil
  @uid_map = nil
end

Instance Attribute Details

#mbox_idObject (readonly)

Returns the value of attribute mbox_id.



387
388
389
# File 'lib/rims/mail_store.rb', line 387

def mbox_id
  @mbox_id
end

#read_onlyObject (readonly) Also known as: read_only?

Returns the value of attribute read_only.



473
474
475
# File 'lib/rims/mail_store.rb', line 473

def read_only
  @read_only
end

Class Method Details

.parse_msg_seq(msg_seq_desc, last_number) ⇒ Object



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/rims/mail_store.rb', line 542

def self.parse_msg_seq(msg_seq_desc, last_number)
  case (msg_seq_desc)
  when /\A (\d+|\*) \z/x
    msg_seq_pair = [ $&, $& ]
  when /\A (\d+|\*):(\d+|\*) \z/x
    msg_seq_pair = [ $1, $2 ]
  else
    raise MessageSetSyntaxError, "invalid message sequence format: #{msg_seq_desc}"
  end

  msg_seq_pair.map!{|num|
    case (num)
    when '*'
      last_number
    else
      n = num.to_i
      if (n < 1) then
        raise MessageSetSyntaxError, "out of range of message sequence number: #{msg_seq_desc}"
      end
      n
    end
  }

  Range.new(msg_seq_pair[0], msg_seq_pair[1])
end

.parse_msg_set(msg_set_desc, last_number) ⇒ Object



568
569
570
571
572
573
574
575
576
577
578
# File 'lib/rims/mail_store.rb', line 568

def self.parse_msg_set(msg_set_desc, last_number)
  msg_set = [].to_set
  msg_set_desc.split(/,/).each do |msg_seq_desc|
    msg_range = parse_msg_seq(msg_seq_desc, last_number)
    msg_range.step do |n|
      msg_set << n
    end
  end

  msg_set
end

Instance Method Details

#[](msg_idx) ⇒ Object



431
432
433
# File 'lib/rims/mail_store.rb', line 431

def [](msg_idx)
  @msg_list[msg_idx]
end

#alive?Boolean

Returns:

  • (Boolean)


389
390
391
# File 'lib/rims/mail_store.rb', line 389

def alive?
  ! @mail_store.mbox_name(@mbox_id).nil?
end

#attach(server_response_channel) ⇒ Object



397
398
399
400
# File 'lib/rims/mail_store.rb', line 397

def attach(server_response_channel)
  @pub, @sub = server_response_channel.make_pub_sub_pair(@mbox_id)
  self
end

#close(&block) ⇒ Object



506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/rims/mail_store.rb', line 506

def close(&block)
  unless (@read_only) then
    expunge_mbox(&block)
    @mail_store.each_msg_uid(@mbox_id) do |msg_id|
      if (@mail_store.msg_flag(@mbox_id, msg_id, 'recent')) then
        @mail_store.set_msg_flag(@mbox_id, msg_id, 'recent', false)
      end
    end
  end
  @cnum = nil
  @msg_list = nil
  @uid_map = nil
  self
end

#detachObject



521
522
523
524
525
526
# File 'lib/rims/mail_store.rb', line 521

def detach
  @mail_store = nil
  @pub.detach
  @sub.detach
  self
end

#each_msgObject



435
436
437
438
439
440
441
# File 'lib/rims/mail_store.rb', line 435

def each_msg
  return enum_for(:each_msg) unless block_given?
  for msg in @msg_list
    yield(msg)
  end
  self
end

#expunge_mboxObject



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/rims/mail_store.rb', line 476

def expunge_mbox
  if (@mail_store.mbox_flag_num(@mbox_id, 'deleted') > 0) then
    if (block_given?) then
      uid2num = {}
      for msg in @msg_list
        uid2num[msg.uid] = msg.num
      end

      msg_num_list = []
      @mail_store.expunge_mbox(@mbox_id) do |uid|
        num = uid2num[uid] or raise "internal error: not found a message: #{@mbox_id},#{uid}"
        msg_num_list << num
      end

      # to prevent to decrement message sequence numbers that
      # appear in a set of successive expunge responses, expunge
      # command should early return an expunge response of larger
      # message sequence number.
      msg_num_list.sort!
      msg_num_list.reverse_each do |num|
        yield(num)
      end
    else
      @mail_store.expunge_mbox(@mbox_id)
    end
  end

  self
end

#msg_find_all(msg_set, uid: false) ⇒ Object



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/rims/mail_store.rb', line 443

def msg_find_all(msg_set, uid: false)
  if (msg_set.size < @msg_list.length) then
    if (uid) then
      msg_set.inject([]) {|msg_list, id|
        if (msg = @uid_map[id]) then
          msg_list << msg
        end
        msg_list
      }
    else
      msg_set.inject([]) {|msg_list, num|
        if (1 <= num && num <= @msg_list.length) then
          msg_list << @msg_list[num - 1]
        end
        msg_list
      }
    end
  else
    if (uid) then
      @msg_list.find_all{|msg|
        msg_set.include? msg.uid
      }
    else
      @msg_list.find_all{|msg|
        msg_set.include? msg.num
      }
    end
  end
end

#parse_msg_set(msg_set_desc, uid: false) ⇒ Object



528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/rims/mail_store.rb', line 528

def parse_msg_set(msg_set_desc, uid: false)
  if (@msg_list.empty?) then
    last_number = 0
  else
    if (uid) then
      last_number = @msg_list[-1].uid
    else
      last_number = @msg_list[-1].num
    end
  end

  self.class.parse_msg_set(msg_set_desc, last_number)
end

#reloadObject



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/rims/mail_store.rb', line 408

def reload
  @cnum = @mail_store.cnum

  msg_id_list = @mail_store.each_msg_uid(@mbox_id).to_a
  msg_id_list.sort!

  @msg_list = Array.new(msg_id_list.length)
  @uid_map = {}

  msg_id_list.each_with_index do |id, i|
    num = i.succ
    msg = MessageStruct.new(id, num)
    @msg_list[i] = msg
    @uid_map[id] = msg
  end

  self
end

#should_be_aliveObject



393
394
395
# File 'lib/rims/mail_store.rb', line 393

def should_be_alive
  alive? or raise "deleted folder: #{@mbox_id}"
end

#updated?Boolean

Returns:

  • (Boolean)


427
428
429
# File 'lib/rims/mail_store.rb', line 427

def updated?
  @mail_store.cnum != @cnum
end